]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliCDBStorage.cxx
Parenthesis forgotten.
[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
247         // if drain storage is set, drain entry into drain storage
248         if(entry && (AliCDBManager::Instance())->IsDrainSet())
249                 AliCDBManager::Instance()->Drain(entry);
250
251         return entry;
252 }
253
254 //_____________________________________________________________________________
255 AliCDBEntry* AliCDBStorage::Get(const AliCDBPath& path, Int_t runNumber,
256         Int_t version, Int_t subVersion) {
257 // get an AliCDBEntry object from the database
258
259         return Get(AliCDBId(path, runNumber, runNumber, version, subVersion));
260 }
261
262 //_____________________________________________________________________________
263 AliCDBEntry* AliCDBStorage::Get(const AliCDBPath& path,
264         const AliCDBRunRange& runRange, Int_t version,
265         Int_t subVersion) {
266 // get an AliCDBEntry object from the database
267
268         return Get(AliCDBId(path, runRange, version, subVersion));
269 }
270
271 //_____________________________________________________________________________
272 TList* AliCDBStorage::GetAll(const AliCDBId& query) {
273 // get multiple AliCDBEntry objects from the database
274
275
276         if (!query.IsValid()) {
277                 AliError(Form("Invalid query: %s", query.ToString().Data()));
278                 return NULL;
279         }
280
281         if (query.IsAnyRange()) {
282                 AliError(Form("Unspecified run or runrange: %s",
283                                 query.ToString().Data()));      
284                 return NULL;
285         }       
286         
287         // This is needed otherwise TH1  objects (histos, TTree's) are lost when file is closed!
288         Bool_t oldStatus = TH1::AddDirectoryStatus();
289         TH1::AddDirectory(kFALSE);
290
291         TList *result = GetEntries(query);
292
293         if (oldStatus != kFALSE)
294                 TH1::AddDirectory(kTRUE);
295
296         Int_t nEntries = result->GetEntries();
297
298         AliInfo(Form("%d objects retrieved. Request was: %s",
299                         nEntries, query.ToString().Data()));
300         for(int i=0; i<nEntries;i++){
301                 AliCDBEntry *entry = (AliCDBEntry*) result->At(i);
302                 AliInfo(Form("%s",entry->GetId().ToString().Data()));
303         }
304
305
306         // if drain storage is set, drain entries into drain storage
307         if((AliCDBManager::Instance())->IsDrainSet()){
308                 for(int i = 0; i<result->GetEntries(); i++){
309                         AliCDBEntry* entry = (AliCDBEntry*) result->At(i);
310                         AliCDBManager::Instance()->Drain(entry);
311                 }
312         }
313         
314
315         return result;
316 }
317
318 //_____________________________________________________________________________
319 TList* AliCDBStorage::GetAll(const AliCDBPath& path, Int_t runNumber, 
320         Int_t version, Int_t subVersion) {
321 // get multiple AliCDBEntry objects from the database
322
323         return GetAll(AliCDBId(path, runNumber, runNumber, version,     
324                         subVersion));
325 }
326
327 //_____________________________________________________________________________
328 TList* AliCDBStorage::GetAll(const AliCDBPath& path, 
329         const AliCDBRunRange& runRange, Int_t version, Int_t subVersion) {
330 // get multiple AliCDBEntry objects from the database
331
332         return GetAll(AliCDBId(path, runRange, version, subVersion));
333 }
334
335 //_____________________________________________________________________________
336 AliCDBId* AliCDBStorage::GetId(const AliCDBId& query) {
337 // get the Id of the valid object from the database (does not open the file)
338
339         // check if query's path and runRange are valid
340         // query is invalid also if version is not specified and subversion is!
341         if (!query.IsValid()) {
342                 AliError(Form("Invalid query: %s", query.ToString().Data()));
343                 return NULL;
344         }
345
346         // query is not specified if path contains wildcard or runrange = [-1,-1]
347         if (!query.IsSpecified()) {
348                 AliError(Form("Unspecified query: %s",
349                                 query.ToString().Data()));
350                 return NULL;
351         }
352
353         AliCDBId* id = GetEntryId(query);
354
355         return id;
356 }
357
358 //_____________________________________________________________________________
359 AliCDBId* AliCDBStorage::GetId(const AliCDBPath& path, Int_t runNumber,
360         Int_t version, Int_t subVersion) {
361 // get the Id of the valid object from the database (does not open the file)
362
363         return GetId(AliCDBId(path, runNumber, runNumber, version, subVersion));
364 }
365
366 //_____________________________________________________________________________
367 AliCDBId* AliCDBStorage::GetId(const AliCDBPath& path,
368         const AliCDBRunRange& runRange, Int_t version,
369         Int_t subVersion) {
370 // get the Id of the valid object from the database (does not open the file)
371
372         return GetId(AliCDBId(path, runRange, version, subVersion));
373 }
374
375 //_____________________________________________________________________________
376 Bool_t AliCDBStorage::Put(TObject* object, AliCDBId& id, AliCDBMetaData* metaData, AliCDBManager::DataType type) {
377 // store an AliCDBEntry object into the database
378         
379         if (object==0x0) {
380                 AliError("Null Entry! No storage will be done!");
381                 return kFALSE;
382         } 
383
384         AliCDBEntry anEntry(object, id, metaData);
385
386         return Put(&anEntry, type);
387 }
388
389 //_____________________________________________________________________________
390 Bool_t AliCDBStorage::Put(AliCDBEntry* entry, AliCDBManager::DataType type) {
391 // store an AliCDBEntry object into the database
392
393         if (!entry){
394                 AliError("No entry!");
395                 return kFALSE;
396         }
397         
398         if (entry->GetObject()==0x0){
399                 AliError("No valid object in CDB entry!");
400                 return kFALSE;
401         }
402
403         if (!entry->GetId().IsValid()) {
404                 AliError(Form("Invalid entry ID: %s",
405                         entry->GetId().ToString().Data()));
406                 return kFALSE;
407         }       
408
409         if (!entry->GetId().IsSpecified()) {
410                 AliError(Form("Unspecified entry ID: %s",
411                         entry->GetId().ToString().Data()));
412                 return kFALSE;
413         }
414
415         AliCDBManager::DataType expectedType = GetDataType();
416
417         if(expectedType != AliCDBManager::kPrivate && type != expectedType) {
418                 AliError(Form("It is forbidden to store %s data into a folder of type %s!",
419                         AliCDBManager::GetDataTypeName(type),
420                         AliCDBManager::GetDataTypeName(expectedType)));
421                         return 0;
422         }
423
424         return PutEntry(entry);
425 }
426
427 //_____________________________________________________________________________
428 void AliCDBStorage::QueryCDB(Int_t run, const char* pathFilter,
429                                 Int_t version, AliCDBMetaData* md){
430 // query CDB for files valid for given run, and fill list fValidFileIds
431 // Actual query is done in virtual function QueryValidFiles()
432
433         fRun = run;
434
435         fPathFilter = pathFilter;
436         if(!fPathFilter.IsValid()) {
437                 AliError(Form("Filter not valid: %s", pathFilter));
438                 fPathFilter = "*";
439                 return;
440         }
441
442         fVersion = version;
443
444         AliInfo(Form("Querying files valid for run %d and path \"%s\" into CDB storage  \"%s://%s\"",
445                                 fRun, pathFilter, fType.Data(), fBaseFolder.Data()));
446
447         // Clear fValidFileIds list (it cannot be filled twice!
448         AliDebug(2, "Clearing list of CDB Id's previously loaded");
449         fValidFileIds.Clear();
450
451         if(fMetaDataFilter) {delete fMetaDataFilter; fMetaDataFilter=0;}
452         if(md) fMetaDataFilter = dynamic_cast<AliCDBMetaData*> (md->Clone());
453
454         QueryValidFiles();
455         AliCDBId queryId(pathFilter,run,run,version);
456
457         AliInfo(Form("%d valid files found!", fValidFileIds.GetEntriesFast()));
458
459 }
460
461 //_____________________________________________________________________________
462 void AliCDBStorage::PrintQueryCDB(){
463 // print parameters used to load list of CDB Id's (fRun, fPathFilter, fVersion)
464
465         AliCDBId paramId(fPathFilter, fRun, fRun, fVersion);
466         AliInfo(Form("**** QueryCDB Parameters **** \n\t<%s>\n",
467                                 paramId.ToString().Data()));
468
469         if(fMetaDataFilter) fMetaDataFilter->PrintMetaData();
470
471
472         TString message = "**** Id's of valid objects found *****\n";
473         TIter iter(&fValidFileIds);
474         AliCDBId* anId=0;
475
476         // loop on the list of selection criteria
477         while ((anId = dynamic_cast<AliCDBId*>(iter.Next()))) {
478                 message += Form("\t%s\n", anId->ToString().Data());
479         }
480         message += Form("\n\tTotal: %d objects found\n", fValidFileIds.GetEntriesFast());
481         AliInfo(Form("%s", message.Data()));
482 }
483
484 //_____________________________________________________________________________
485 AliCDBManager::DataType AliCDBStorage::GetDataType() const {
486 // returns the type of the data that should be stored into this storage:
487 // kConditions: conditions data; kReference: reference data; kPrivate: private (user-defined) data type
488
489         if(GetType() != "alien") return AliCDBManager::kPrivate;
490
491         TString condFolder = ((AliCDBGridParam*) AliCDBManager::Instance()->GetCondParam())->GetDBFolder();
492         TString refFolder = ((AliCDBGridParam*) AliCDBManager::Instance()->GetRefParam())->GetDBFolder();
493
494         if(GetBaseFolder().Contains(condFolder)) return AliCDBManager::kCondition;
495         if(GetBaseFolder().Contains(refFolder)) return AliCDBManager::kReference;
496
497         return AliCDBManager::kPrivate;
498 }
499
500 //_____________________________________________________________________________
501 void AliCDBStorage::LoadTreeFromFile(AliCDBEntry *entry) const {
502 // Checks whether entry contains a TTree and in case loads it into memory
503
504         TObject *obj = (TObject*) entry->GetObject();
505         if (!obj) {
506           AliError("Cannot retrieve the object:");
507           entry->PrintMetaData();
508           return;
509         }
510
511         if (!strcmp(obj->ClassName(),TTree::Class_Name())) {
512
513                 AliWarning("Entry contains a TTree! Loading baskets...");
514
515                 TTree* tree = dynamic_cast<TTree*> (obj);
516
517                 if(!tree) return;
518
519                 tree->LoadBaskets();
520                 tree->SetDirectory(0);
521         }
522         else if (!strcmp(obj->ClassName(),TNtuple::Class_Name())){
523
524                 AliWarning("Entry contains a TNtuple! Loading baskets...");
525
526                 TNtuple* ntu = dynamic_cast<TNtuple*> (obj);
527
528                 if(!ntu) return;
529
530                 ntu->LoadBaskets();
531                 ntu->SetDirectory(0);
532         }
533
534         return;
535 }
536
537 // //_____________________________________________________________________________
538 // void AliCDBStorage::SetTreeToFile(AliCDBEntry *entry, TFile* file) const {
539 // // Checks whether entry contains a TTree and in case assigns it to memory
540 // 
541 //      AliCDBMetaData *md = dynamic_cast<AliCDBMetaData*> (entry->GetMetaData());
542 //      if(!md) return;
543 //      TString objStr = md->GetObjectClassName();
544 //      if(objStr != "TTree") return;
545 //      AliWarning("Entry contains a TTree! Setting file...");
546 // 
547 //      TTree* tree = dynamic_cast<TTree*> (entry->GetObject());
548 // 
549 //      if(!tree) return;
550 // 
551 // //   tree->SetDirectory(file);
552 //      tree->SetDirectory(0);
553 // 
554 //      return;
555 // }