Move to fixed size step manager as default
[u/mrichter/AliRoot.git] / STEER / AliCDBStorage.cxx
CommitLineData
2c8628dd 1/**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3 * *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
6 * *
7 * Permission to use, copy, modify and distribute this software and its *
8 * documentation strictly for non-commercial purposes is hereby granted *
9 * without fee, provided that the above copyright notice appears in all *
10 * copies and that both the copyright notice and this permission notice *
11 * appear in the supporting documentation. The authors make no claims *
12 * about the suitability of this software for any purpose. It is *
13 * provided "as is" without express or implied warranty. *
14 **************************************************************************/
15
b05400be 16#include <TKey.h>
02c4845e 17#include <TH1.h>
9e1ceb13 18#include "AliCDBManager.h"
fe913d8f 19#include "AliCDBStorage.h"
2c8628dd 20
9e1ceb13 21#include "AliCDBEntry.h"
22#include "AliLog.h"
2c8628dd 23
fe913d8f 24ClassImp(AliCDBStorage)
2c8628dd 25
9e1ceb13 26//_____________________________________________________________________________
27AliCDBStorage::AliCDBStorage() {
28// constructor
29
30 fSelections.SetOwner(1);
31}
2c8628dd 32
9e1ceb13 33//_____________________________________________________________________________
34AliCDBStorage::~AliCDBStorage() {
35// destructor
2c8628dd 36
9e1ceb13 37}
2c8628dd 38
39//_____________________________________________________________________________
4b5e0dce 40void AliCDBStorage::GetSelection(/*const*/ AliCDBId* id) {
0f678e35 41// return required version and subversion from the list of selection criteria
9e1ceb13 42
43 TIter iter(&fSelections);
44 AliCDBId* aSelection;
45
46 // loop on the list of selection criteria
47 while ((aSelection = (AliCDBId*) iter.Next())) {
48 // check if selection element contains id's path and run (range)
4b5e0dce 49 if (aSelection->Comprises(*id)) {
b05400be 50 AliDebug(2,Form("Using selection criterion: %s ", aSelection->ToString().Data()));
9e1ceb13 51 // return required version and subversion
4b5e0dce 52
53 id->SetVersion(aSelection->GetVersion());
54 id->SetSubVersion(aSelection->GetSubVersion());
55 return;
9e1ceb13 56 }
57 }
58
59 // no valid element is found in the list of selection criteria -> return
b05400be 60 AliDebug(2,"Looking for objects with most recent version");
4b5e0dce 61 return;
2c8628dd 62}
63
b05400be 64//_____________________________________________________________________________
65void AliCDBStorage::ReadSelectionFromFile(const char *fileName){
66// read selection criteria list from file
67
68 RemoveAllSelections();
69
70 TList *list = GetIdListFromFile(fileName);
71 if(!list) return;
72
73 list->SetOwner();
74 Int_t nId = list->GetEntries();
75 AliCDBId *id;
76 TKey *key;
77
78 for(int i=nId-1;i>=0;i--){
79 key = (TKey*) list->At(i);
80 id = (AliCDBId*) key->ReadObj();
81 if(id) AddSelection(*id);
82 }
83 delete list;
84 AliInfo(Form("Selection criteria list filled with %d entries",fSelections.GetEntries()));
85 PrintSelectionList();
86
87}
88
2c8628dd 89//_____________________________________________________________________________
9e1ceb13 90void AliCDBStorage::AddSelection(const AliCDBId& selection) {
91// add a selection criterion
92
93 AliCDBPath path = selection.GetPath();
94 if(!path.IsValid()) return;
95
96 TIter iter(&fSelections);
97 const AliCDBId *anId;
98 while((anId = (AliCDBId*) iter.Next())){
99 if(selection.Comprises(*anId)){
100 AliWarning("This selection is more general than a previous one and will hide it!");
101 AliWarning(Form("%s", (anId->ToString()).Data()));
102 fSelections.AddBefore(anId, new AliCDBId(selection));
103 return;
104 }
105
106 }
107 fSelections.AddFirst(new AliCDBId(selection));
108}
2c8628dd 109
110//_____________________________________________________________________________
9e1ceb13 111void AliCDBStorage::AddSelection(const AliCDBPath& path,
112 const AliCDBRunRange& runRange, Int_t version, Int_t subVersion){
113// add a selection criterion
2c8628dd 114
9e1ceb13 115 AddSelection(AliCDBId(path, runRange, version, subVersion));
2c8628dd 116}
117
9e1ceb13 118//_____________________________________________________________________________
119void AliCDBStorage::AddSelection(const AliCDBPath& path,
120 Int_t firstRun, Int_t lastRun, Int_t version, Int_t subVersion){
121// add a selection criterion
122
123 AddSelection(AliCDBId(path, firstRun, lastRun, version, subVersion));
124}
2c8628dd 125
9e1ceb13 126//_____________________________________________________________________________
127void AliCDBStorage::RemoveSelection(const AliCDBId& selection) {
128// remove a selection criterion
2c8628dd 129
9e1ceb13 130 TIter iter(&fSelections);
131 AliCDBId* aSelection;
2c8628dd 132
9e1ceb13 133 while ((aSelection = (AliCDBId*) iter.Next())) {
134 if (selection.Comprises(*aSelection)) {
135 fSelections.Remove(aSelection);
136 }
137 }
138}
2c8628dd 139
9e1ceb13 140//_____________________________________________________________________________
141void AliCDBStorage::RemoveSelection(const AliCDBPath& path,
142 const AliCDBRunRange& runRange){
143// remove a selection criterion
2c8628dd 144
9e1ceb13 145 RemoveSelection(AliCDBId(path, runRange, -1, -1));
146}
f05209ee 147
9e1ceb13 148//_____________________________________________________________________________
149void AliCDBStorage::RemoveSelection(const AliCDBPath& path,
150 Int_t firstRun, Int_t lastRun){
151// remove a selection criterion
f05209ee 152
9e1ceb13 153 RemoveSelection(AliCDBId(path, firstRun, lastRun, -1, -1));
154}
f05209ee 155
9e1ceb13 156//_____________________________________________________________________________
eb0b1051 157void AliCDBStorage::RemoveSelection(int position){
9e1ceb13 158// remove a selection criterion from its position in the list
2c8628dd 159
9e1ceb13 160 fSelections.RemoveAt(position);
2c8628dd 161}
162
2c8628dd 163//_____________________________________________________________________________
9e1ceb13 164void AliCDBStorage::RemoveAllSelections(){
165// remove all selection criteria
2c8628dd 166
9e1ceb13 167 fSelections.RemoveAll();
168}
fe913d8f 169
9e1ceb13 170//_____________________________________________________________________________
171void AliCDBStorage::PrintSelectionList(){
172// prints the list of selection criteria
173
174 TIter iter(&fSelections);
175 AliCDBId* aSelection;
176
177 // loop on the list of selection criteria
178 int index=0;
179 while ((aSelection = (AliCDBId*) iter.Next())) {
180 AliInfo(Form("index %d -> selection: %s",index++, aSelection->ToString().Data()));
181 }
fe913d8f 182
9e1ceb13 183}
fe913d8f 184
9e1ceb13 185//_____________________________________________________________________________
0f678e35 186AliCDBEntry* AliCDBStorage::Get(const AliCDBId& query) {
9e1ceb13 187// get an AliCDBEntry object from the database
188
189 // check if query's path and runRange are valid
190 // query is invalid also if version is not specified and subversion is!
191 if (!query.IsValid()) {
192 AliError(Form("Invalid query: %s", query.ToString().Data()));
193 return NULL;
194 }
195
196 // query is not specified if path contains wildcard or runrange = [-1,-1]
197 if (!query.IsSpecified()) {
198 AliError(Form("Unspecified query: %s",
199 query.ToString().Data()));
200 return NULL;
201 }
02c4845e 202
203 // This is needed otherwise TH1 objects (histos, TTree's) are lost when file is closed!
204 Bool_t oldStatus = TH1::AddDirectoryStatus();
205 TH1::AddDirectory(kFALSE);
9e1ceb13 206
207 AliCDBEntry* entry = GetEntry(query);
02c4845e 208
209 if (oldStatus != kFALSE)
210 TH1::AddDirectory(kTRUE);
0f678e35 211
9e1ceb13 212 if (entry) {
b05400be 213 AliInfo(Form("CDB object retrieved: %s", entry->GetId().ToString().Data()));
9e1ceb13 214 } else {
0f678e35 215 // TODO this is to make the SHUTTLE output lighter
216 if(!(query.GetPath().Contains("SHUTTLE/STATUS")))
217 AliInfo(Form("No valid CDB object found! request was: name = <%s>, run = %d",
218 (query.GetPath()).Data(), query.GetFirstRun()));
9e1ceb13 219 }
0f678e35 220
9e1ceb13 221 // if drain storage is set, drain entry into drain storage
222 if(entry && (AliCDBManager::Instance())->IsDrainSet())
223 AliCDBManager::Instance()->Drain(entry);
0f678e35 224
9e1ceb13 225 return entry;
2c8628dd 226}
227
228//_____________________________________________________________________________
9e1ceb13 229AliCDBEntry* AliCDBStorage::Get(const AliCDBPath& path, Int_t runNumber,
230 Int_t version, Int_t subVersion) {
231// get an AliCDBEntry object from the database
2c8628dd 232
9e1ceb13 233 return Get(AliCDBId(path, runNumber, runNumber, version, subVersion));
2c8628dd 234}
235
2c8628dd 236//_____________________________________________________________________________
9e1ceb13 237AliCDBEntry* AliCDBStorage::Get(const AliCDBPath& path,
238 const AliCDBRunRange& runRange, Int_t version,
239 Int_t subVersion) {
240// get an AliCDBEntry object from the database
2c8628dd 241
9e1ceb13 242 return Get(AliCDBId(path, runRange, version, subVersion));
2c8628dd 243}
244
2c8628dd 245//_____________________________________________________________________________
9e1ceb13 246TList* AliCDBStorage::GetAll(const AliCDBId& query) {
247// get multiple AliCDBEntry objects from the database
248
249
250 if (!query.IsValid()) {
251 AliError(Form("Invalid query: %s", query.ToString().Data()));
252 return NULL;
253 }
254
255 if (query.IsAnyRange()) {
256 AliError(Form("Unspecified run or runrange: %s",
257 query.ToString().Data()));
258 return NULL;
259 }
260
02c4845e 261 // This is needed otherwise TH1 objects (histos, TTree's) are lost when file is closed!
262 Bool_t oldStatus = TH1::AddDirectoryStatus();
263 TH1::AddDirectory(kFALSE);
264
9e1ceb13 265 TList *result = GetEntries(query);
266
02c4845e 267 if (oldStatus != kFALSE)
268 TH1::AddDirectory(kTRUE);
269
9e1ceb13 270 Int_t nEntries = result->GetEntries();
271 if (nEntries) {
b05400be 272 AliInfo(Form("%d AliCDBEntry objects retrieved.",nEntries));
9e1ceb13 273 for(int i=0; i<nEntries;i++){
274 AliCDBEntry *entry = (AliCDBEntry*) result->At(i);
275 AliInfo(Form("%s",entry->GetId().ToString().Data()));
276
277 }
278 } else {
b05400be 279 AliInfo(Form("No valid CDB object found! request was: name = <%s>, run = %d, version = %d",
280 (query.GetPath()).Data(), query.GetFirstRun(), query.GetVersion()));
9e1ceb13 281 }
282
283 // if drain storage is set, drain entries into drain storage
284 if((AliCDBManager::Instance())->IsDrainSet()){
285 for(int i = 0; i<result->GetEntries(); i++){
286 AliCDBEntry* entry = (AliCDBEntry*) result->At(i);
287 AliCDBManager::Instance()->Drain(entry);
288 }
289 }
290
291
292 return result;
293}
2c8628dd 294
9e1ceb13 295//_____________________________________________________________________________
296TList* AliCDBStorage::GetAll(const AliCDBPath& path, Int_t runNumber,
297 Int_t version, Int_t subVersion) {
298// get multiple AliCDBEntry objects from the database
2c8628dd 299
9e1ceb13 300 return GetAll(AliCDBId(path, runNumber, runNumber, version,
301 subVersion));
2c8628dd 302}
303
2c8628dd 304//_____________________________________________________________________________
9e1ceb13 305TList* AliCDBStorage::GetAll(const AliCDBPath& path,
306 const AliCDBRunRange& runRange, Int_t version, Int_t subVersion) {
307// get multiple AliCDBEntry objects from the database
2c8628dd 308
9e1ceb13 309 return GetAll(AliCDBId(path, runRange, version, subVersion));
2c8628dd 310}
f05209ee 311
312
313//_____________________________________________________________________________
9e1ceb13 314Bool_t AliCDBStorage::Put(TObject* object, AliCDBId& id, AliCDBMetaData* metaData) {
02c4845e 315// store an AliCDBEntry object into the database
9e1ceb13 316
317 AliCDBEntry anEntry(object, id, metaData);
f05209ee 318
9e1ceb13 319 return Put(&anEntry);
320}
f05209ee 321
9e1ceb13 322//_____________________________________________________________________________
323Bool_t AliCDBStorage::Put(AliCDBEntry* entry) {
02c4845e 324// store an AliCDBEntry object into the database
9e1ceb13 325
02c4845e 326 if (!entry){
327 AliError("No entry!");
328 return kFALSE;
329 }
330
9e1ceb13 331 if (!entry->GetId().IsValid()) {
02c4845e 332 AliError(Form("Invalid entry ID: %s",
9e1ceb13 333 entry->GetId().ToString().Data()));
334 return kFALSE;
335 }
336
337 if (!entry->GetId().IsSpecified()) {
338 AliError(Form("Unspecified entry ID: %s",
339 entry->GetId().ToString().Data()));
340 return kFALSE;
341 }
342
02c4845e 343 // set object's class name into metaData!
344 entry->GetMetaData()->SetObjectClassName(entry->GetObject()->ClassName());
345
9e1ceb13 346 return PutEntry(entry);
f05209ee 347}
fe913d8f 348