Adding protection against AliFatal for SHUTTLE
[u/mrichter/AliRoot.git] / STEER / AliCDBStorage.cxx
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
16 #include <TKey.h>
17 #include <TH1.h>
18 #include <TTree.h>
19 #include <TNtuple.h>
20 #include <TFile.h>
21 #include "AliCDBStorage.h"
22 #include "AliCDBGrid.h"
23
24 #include "AliCDBEntry.h"
25 #include "AliLog.h"
26
27 ClassImp(AliCDBStorage)
28
29 //_____________________________________________________________________________
30 AliCDBStorage::AliCDBStorage():
31 fValidFileIds(),
32 fRun(-1),
33 fPathFilter(),
34 fVersion(-1),
35 fMetaDataFilter(0),
36 fSelections(),
37 fURI(),
38 fType(),
39 fBaseFolder()
40 {
41 // constructor
42
43         fValidFileIds.SetOwner(1);
44         fSelections.SetOwner(1);
45 }
46
47 //_____________________________________________________________________________
48 AliCDBStorage::~AliCDBStorage() {
49 // destructor
50
51         RemoveAllSelections();
52         fValidFileIds.Clear();
53         delete fMetaDataFilter;
54
55 }
56
57 //_____________________________________________________________________________
58 void AliCDBStorage::GetSelection(/*const*/ AliCDBId* id) {
59 // return required version and subversion from the list of selection criteria
60         
61         TIter iter(&fSelections);
62         AliCDBId* aSelection;
63         
64         // loop on the list of selection criteria
65         while ((aSelection = (AliCDBId*) iter.Next())) {
66                 // check if selection element contains id's path and run (range) 
67                 if (aSelection->Comprises(*id)) {
68                         AliDebug(2,Form("Using selection criterion: %s ", aSelection->ToString().Data()));
69                         // return required version and subversion
70                         
71                         id->SetVersion(aSelection->GetVersion());
72                         id->SetSubVersion(aSelection->GetSubVersion());
73                         return;  
74                 }
75         }
76         
77         // no valid element is found in the list of selection criteria -> return
78         AliDebug(2,"Looking for objects with most recent version");
79         return;
80 }
81
82 //_____________________________________________________________________________
83 void AliCDBStorage::ReadSelectionFromFile(const char *fileName){
84 // read selection criteria list from file
85         
86         RemoveAllSelections();
87         
88         TList *list = GetIdListFromFile(fileName);
89         if(!list) return;
90         
91         list->SetOwner();       
92         Int_t nId = list->GetEntries();
93         AliCDBId *id;
94         TKey *key;
95         
96         for(int i=nId-1;i>=0;i--){
97                 key = (TKey*) list->At(i);
98                 id = (AliCDBId*) key->ReadObj();
99                 if(id) AddSelection(*id);
100         }
101         delete list;
102         AliInfo(Form("Selection criteria list filled with %d entries",fSelections.GetEntries()));
103         PrintSelectionList();
104         
105 }
106
107 //_____________________________________________________________________________
108 void AliCDBStorage::AddSelection(const AliCDBId& selection) {
109 // add a selection criterion
110
111         AliCDBPath path = selection.GetPath();
112         if(!path.IsValid()) return;
113         
114         TIter iter(&fSelections);
115         const AliCDBId *anId;
116         while((anId = (AliCDBId*) iter.Next())){
117                 if(selection.Comprises(*anId)){
118                         AliWarning("This selection is more general than a previous one and will hide it!");
119                         AliWarning(Form("%s", (anId->ToString()).Data()));
120                         fSelections.AddBefore(anId, new AliCDBId(selection));
121                         return;
122                 }
123         
124         }
125         fSelections.AddFirst(new AliCDBId(selection));
126 }
127
128 //_____________________________________________________________________________
129 void AliCDBStorage::AddSelection(const AliCDBPath& path,
130         const AliCDBRunRange& runRange, Int_t version, Int_t subVersion){
131 // add a selection criterion
132
133         AddSelection(AliCDBId(path, runRange, version, subVersion));
134 }
135
136 //_____________________________________________________________________________
137 void AliCDBStorage::AddSelection(const AliCDBPath& path,
138         Int_t firstRun, Int_t lastRun, Int_t version, Int_t subVersion){
139 // add a selection criterion
140         
141         AddSelection(AliCDBId(path, firstRun, lastRun, version, subVersion));
142 }
143
144 //_____________________________________________________________________________
145 void AliCDBStorage::RemoveSelection(const AliCDBId& selection) {
146 // remove a selection criterion
147
148         TIter iter(&fSelections);
149         AliCDBId* aSelection;
150
151         while ((aSelection = (AliCDBId*) iter.Next())) {
152                 if (selection.Comprises(*aSelection)) {
153                         fSelections.Remove(aSelection);
154                 }
155         }
156 }
157
158 //_____________________________________________________________________________
159 void AliCDBStorage::RemoveSelection(const AliCDBPath& path, 
160         const AliCDBRunRange& runRange){
161 // remove a selection criterion
162
163         RemoveSelection(AliCDBId(path, runRange, -1, -1));
164 }
165
166 //_____________________________________________________________________________
167 void AliCDBStorage::RemoveSelection(const AliCDBPath& path,
168         Int_t firstRun, Int_t lastRun){
169 // remove a selection criterion
170
171         RemoveSelection(AliCDBId(path, firstRun, lastRun, -1, -1));
172 }
173
174 //_____________________________________________________________________________
175 void AliCDBStorage::RemoveSelection(int position){
176 // remove a selection criterion from its position in the list
177
178         delete fSelections.RemoveAt(position);
179 }
180
181 //_____________________________________________________________________________
182 void AliCDBStorage::RemoveAllSelections(){
183 // remove all selection criteria
184
185         fSelections.Clear();
186 }
187
188 //_____________________________________________________________________________
189 void AliCDBStorage::PrintSelectionList(){
190 // prints the list of selection criteria
191
192         TIter iter(&fSelections);
193         AliCDBId* aSelection;
194
195         // loop on the list of selection criteria
196         int index=0;
197         while ((aSelection = (AliCDBId*) iter.Next())) {
198                 AliInfo(Form("index %d -> selection: %s",index++, aSelection->ToString().Data()));
199         }
200
201 }
202
203 //_____________________________________________________________________________
204 AliCDBEntry* AliCDBStorage::Get(const AliCDBId& query) {
205 // get an AliCDBEntry object from the database
206
207         // check if query's path and runRange are valid
208         // query is invalid also if version is not specified and subversion is!
209         if (!query.IsValid()) {
210                 AliError(Form("Invalid query: %s", query.ToString().Data()));
211                 return NULL;
212         }
213
214         // query is not specified if path contains wildcard or runrange = [-1,-1]
215         if (!query.IsSpecified()) {
216                 AliError(Form("Unspecified query: %s",
217                                 query.ToString().Data()));
218                 return NULL;
219         }
220
221         // This is needed otherwise TH1  objects (histos, TTree's) are lost when file is closed!
222         Bool_t oldStatus = TH1::AddDirectoryStatus();
223         TH1::AddDirectory(kFALSE);
224
225         AliCDBEntry* entry = GetEntry(query);
226
227         if (oldStatus != kFALSE)
228                 TH1::AddDirectory(kTRUE);
229
230         if (entry) {
231                 // this is to make the SHUTTLE output lighter
232                 if(!(query.GetPath().Contains("SHUTTLE/STATUS")))
233                         AliDebug(2, Form("CDB object retrieved: %s", entry->GetId().ToString().Data()));
234         } else {
235                 // this is to make the SHUTTLE output lighter
236                 if(!(query.GetPath().Contains("SHUTTLE/STATUS"))){
237
238                         if(!(query.GetPath().Contains("SHUTTLE"))){
239                                 AliFatal(Form("No valid CDB object found! request was: %s", query.ToString().Data()));
240                         }
241                         else {
242                                 AliInfo(Form("No valid CDB object found! request was: %s", query.ToString().Data()));
243                         }
244         }
245
246         // if drain storage is set, drain entry into drain storage
247         if(entry && (AliCDBManager::Instance())->IsDrainSet())
248                 AliCDBManager::Instance()->Drain(entry);
249
250         return entry;
251 }
252
253 //_____________________________________________________________________________
254 AliCDBEntry* AliCDBStorage::Get(const AliCDBPath& path, Int_t runNumber,
255         Int_t version, Int_t subVersion) {
256 // get an AliCDBEntry object from the database
257
258         return Get(AliCDBId(path, runNumber, runNumber, version, subVersion));
259 }
260
261 //_____________________________________________________________________________
262 AliCDBEntry* AliCDBStorage::Get(const AliCDBPath& path,
263         const AliCDBRunRange& runRange, Int_t version,
264         Int_t subVersion) {
265 // get an AliCDBEntry object from the database
266
267         return Get(AliCDBId(path, runRange, version, subVersion));
268 }
269
270 //_____________________________________________________________________________
271 TList* AliCDBStorage::GetAll(const AliCDBId& query) {
272 // get multiple AliCDBEntry objects from the database
273
274
275         if (!query.IsValid()) {
276                 AliError(Form("Invalid query: %s", query.ToString().Data()));
277                 return NULL;
278         }
279
280         if (query.IsAnyRange()) {
281                 AliError(Form("Unspecified run or runrange: %s",
282                                 query.ToString().Data()));      
283                 return NULL;
284         }       
285         
286         // This is needed otherwise TH1  objects (histos, TTree's) are lost when file is closed!
287         Bool_t oldStatus = TH1::AddDirectoryStatus();
288         TH1::AddDirectory(kFALSE);
289
290         TList *result = GetEntries(query);
291
292         if (oldStatus != kFALSE)
293                 TH1::AddDirectory(kTRUE);
294
295         Int_t nEntries = result->GetEntries();
296
297         AliInfo(Form("%d objects retrieved. Request was: %s",
298                         nEntries, query.ToString().Data()));
299         for(int i=0; i<nEntries;i++){
300                 AliCDBEntry *entry = (AliCDBEntry*) result->At(i);
301                 AliInfo(Form("%s",entry->GetId().ToString().Data()));
302         }
303
304
305         // if drain storage is set, drain entries into drain storage
306         if((AliCDBManager::Instance())->IsDrainSet()){
307                 for(int i = 0; i<result->GetEntries(); i++){
308                         AliCDBEntry* entry = (AliCDBEntry*) result->At(i);
309                         AliCDBManager::Instance()->Drain(entry);
310                 }
311         }
312         
313
314         return result;
315 }
316
317 //_____________________________________________________________________________
318 TList* AliCDBStorage::GetAll(const AliCDBPath& path, Int_t runNumber, 
319         Int_t version, Int_t subVersion) {
320 // get multiple AliCDBEntry objects from the database
321
322         return GetAll(AliCDBId(path, runNumber, runNumber, version,     
323                         subVersion));
324 }
325
326 //_____________________________________________________________________________
327 TList* AliCDBStorage::GetAll(const AliCDBPath& path, 
328         const AliCDBRunRange& runRange, Int_t version, Int_t subVersion) {
329 // get multiple AliCDBEntry objects from the database
330
331         return GetAll(AliCDBId(path, runRange, version, subVersion));
332 }
333
334 //_____________________________________________________________________________
335 AliCDBId* AliCDBStorage::GetId(const AliCDBId& query) {
336 // get the Id of the valid object from the database (does not open the file)
337
338         // check if query's path and runRange are valid
339         // query is invalid also if version is not specified and subversion is!
340         if (!query.IsValid()) {
341                 AliError(Form("Invalid query: %s", query.ToString().Data()));
342                 return NULL;
343         }
344
345         // query is not specified if path contains wildcard or runrange = [-1,-1]
346         if (!query.IsSpecified()) {
347                 AliError(Form("Unspecified query: %s",
348                                 query.ToString().Data()));
349                 return NULL;
350         }
351
352         AliCDBId* id = GetEntryId(query);
353
354         return id;
355 }
356
357 //_____________________________________________________________________________
358 AliCDBId* AliCDBStorage::GetId(const AliCDBPath& path, Int_t runNumber,
359         Int_t version, Int_t subVersion) {
360 // get the Id of the valid object from the database (does not open the file)
361
362         return GetId(AliCDBId(path, runNumber, runNumber, version, subVersion));
363 }
364
365 //_____________________________________________________________________________
366 AliCDBId* AliCDBStorage::GetId(const AliCDBPath& path,
367         const AliCDBRunRange& runRange, Int_t version,
368         Int_t subVersion) {
369 // get the Id of the valid object from the database (does not open the file)
370
371         return GetId(AliCDBId(path, runRange, version, subVersion));
372 }
373
374 //_____________________________________________________________________________
375 Bool_t AliCDBStorage::Put(TObject* object, AliCDBId& id, AliCDBMetaData* metaData, AliCDBManager::DataType type) {
376 // store an AliCDBEntry object into the database
377         
378         if (object==0x0) {
379                 AliError("Null Entry! No storage will be done!");
380                 return kFALSE;
381         } 
382
383         AliCDBEntry anEntry(object, id, metaData);
384
385         return Put(&anEntry, type);
386 }
387
388 //_____________________________________________________________________________
389 Bool_t AliCDBStorage::Put(AliCDBEntry* entry, AliCDBManager::DataType type) {
390 // store an AliCDBEntry object into the database
391
392         if (!entry){
393                 AliError("No entry!");
394                 return kFALSE;
395         }
396         
397         if (entry->GetObject()==0x0){
398                 AliError("No valid object in CDB entry!");
399                 return kFALSE;
400         }
401
402         if (!entry->GetId().IsValid()) {
403                 AliError(Form("Invalid entry ID: %s",
404                         entry->GetId().ToString().Data()));
405                 return kFALSE;
406         }       
407
408         if (!entry->GetId().IsSpecified()) {
409                 AliError(Form("Unspecified entry ID: %s",
410                         entry->GetId().ToString().Data()));
411                 return kFALSE;
412         }
413
414         AliCDBManager::DataType expectedType = GetDataType();
415
416         if(expectedType != AliCDBManager::kPrivate && type != expectedType) {
417                 AliError(Form("It is forbidden to store %s data into a folder of type %s!",
418                         AliCDBManager::GetDataTypeName(type),
419                         AliCDBManager::GetDataTypeName(expectedType)));
420                         return 0;
421         }
422
423         return PutEntry(entry);
424 }
425
426 //_____________________________________________________________________________
427 void AliCDBStorage::QueryCDB(Int_t run, const char* pathFilter,
428                                 Int_t version, AliCDBMetaData* md){
429 // query CDB for files valid for given run, and fill list fValidFileIds
430 // Actual query is done in virtual function QueryValidFiles()
431
432         fRun = run;
433
434         fPathFilter = pathFilter;
435         if(!fPathFilter.IsValid()) {
436                 AliError(Form("Filter not valid: %s", pathFilter));
437                 fPathFilter = "*";
438                 return;
439         }
440
441         fVersion = version;
442
443         AliInfo(Form("Querying files valid for run %d and path \"%s\" into CDB storage  \"%s://%s\"",
444                                 fRun, pathFilter, fType.Data(), fBaseFolder.Data()));
445
446         // Clear fValidFileIds list (it cannot be filled twice!
447         AliDebug(2, "Clearing list of CDB Id's previously loaded");
448         fValidFileIds.Clear();
449
450         if(fMetaDataFilter) {delete fMetaDataFilter; fMetaDataFilter=0;}
451         if(md) fMetaDataFilter = dynamic_cast<AliCDBMetaData*> (md->Clone());
452
453         QueryValidFiles();
454         AliCDBId queryId(pathFilter,run,run,version);
455
456         AliInfo(Form("%d valid files found!", fValidFileIds.GetEntriesFast()));
457
458 }
459
460 //_____________________________________________________________________________
461 void AliCDBStorage::PrintQueryCDB(){
462 // print parameters used to load list of CDB Id's (fRun, fPathFilter, fVersion)
463
464         AliCDBId paramId(fPathFilter, fRun, fRun, fVersion);
465         AliInfo(Form("**** QueryCDB Parameters **** \n\t<%s>\n",
466                                 paramId.ToString().Data()));
467
468         if(fMetaDataFilter) fMetaDataFilter->PrintMetaData();
469
470
471         TString message = "**** Id's of valid objects found *****\n";
472         TIter iter(&fValidFileIds);
473         AliCDBId* anId=0;
474
475         // loop on the list of selection criteria
476         while ((anId = dynamic_cast<AliCDBId*>(iter.Next()))) {
477                 message += Form("\t%s\n", anId->ToString().Data());
478         }
479         message += Form("\n\tTotal: %d objects found\n", fValidFileIds.GetEntriesFast());
480         AliInfo(Form("%s", message.Data()));
481 }
482
483 //_____________________________________________________________________________
484 AliCDBManager::DataType AliCDBStorage::GetDataType() const {
485 // returns the type of the data that should be stored into this storage:
486 // kConditions: conditions data; kReference: reference data; kPrivate: private (user-defined) data type
487
488         if(GetType() != "alien") return AliCDBManager::kPrivate;
489
490         TString condFolder = ((AliCDBGridParam*) AliCDBManager::Instance()->GetCondParam())->GetDBFolder();
491         TString refFolder = ((AliCDBGridParam*) AliCDBManager::Instance()->GetRefParam())->GetDBFolder();
492
493         if(GetBaseFolder().Contains(condFolder)) return AliCDBManager::kCondition;
494         if(GetBaseFolder().Contains(refFolder)) return AliCDBManager::kReference;
495
496         return AliCDBManager::kPrivate;
497 }
498
499 //_____________________________________________________________________________
500 void AliCDBStorage::LoadTreeFromFile(AliCDBEntry *entry) const {
501 // Checks whether entry contains a TTree and in case loads it into memory
502
503         TObject *obj = (TObject*) entry->GetObject();
504         if (!obj) {
505           AliError("Cannot retrieve the object:");
506           entry->PrintMetaData();
507           return;
508         }
509
510         if (!strcmp(obj->ClassName(),TTree::Class_Name())) {
511
512                 AliWarning("Entry contains a TTree! Loading baskets...");
513
514                 TTree* tree = dynamic_cast<TTree*> (obj);
515
516                 if(!tree) return;
517
518                 tree->LoadBaskets();
519                 tree->SetDirectory(0);
520         }
521         else if (!strcmp(obj->ClassName(),TNtuple::Class_Name())){
522
523                 AliWarning("Entry contains a TNtuple! Loading baskets...");
524
525                 TNtuple* ntu = dynamic_cast<TNtuple*> (obj);
526
527                 if(!ntu) return;
528
529                 ntu->LoadBaskets();
530                 ntu->SetDirectory(0);
531         }
532
533         return;
534 }
535
536 // //_____________________________________________________________________________
537 // void AliCDBStorage::SetTreeToFile(AliCDBEntry *entry, TFile* file) const {
538 // // Checks whether entry contains a TTree and in case assigns it to memory
539 // 
540 //      AliCDBMetaData *md = dynamic_cast<AliCDBMetaData*> (entry->GetMetaData());
541 //      if(!md) return;
542 //      TString objStr = md->GetObjectClassName();
543 //      if(objStr != "TTree") return;
544 //      AliWarning("Entry contains a TTree! Setting file...");
545 // 
546 //      TTree* tree = dynamic_cast<TTree*> (entry->GetObject());
547 // 
548 //      if(!tree) return;
549 // 
550 // //   tree->SetDirectory(file);
551 //      tree->SetDirectory(0);
552 // 
553 //      return;
554 // }