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