]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliCDBManager.cxx
Adding new calibration flavours for trigger
[u/mrichter/AliRoot.git] / STEER / AliCDBManager.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 //   Implementation of AliCDBManager and AliCDBParam classe
17 //   Author: Alberto Colla 
18 //   e-mail: Alberto.Colla@cern.ch
19 //-------------------------------------------------------------------------
20
21 #include "AliCDBManager.h"
22 #include "AliCDBStorage.h"
23 #include "AliLog.h"
24 #include "AliCDBDump.h"
25 #include "AliCDBLocal.h"
26 #include "AliCDBGrid.h"
27 #include "AliCDBEntry.h"
28 #include "AliCDBMetaData.h"
29
30 #include <TObjString.h>
31 #include <TSystem.h>
32
33 ClassImp(AliCDBParam)
34
35 ClassImp(AliCDBManager)
36
37 AliCDBManager* AliCDBManager::fgInstance = 0x0;
38
39 //_____________________________________________________________________________
40 AliCDBManager* AliCDBManager::Instance() {
41 // returns AliCDBManager instance (singleton)
42
43         if (!fgInstance) {
44                 fgInstance = new AliCDBManager();
45                 fgInstance->Init();
46         }
47
48         return fgInstance;
49 }
50
51 //_____________________________________________________________________________
52 void AliCDBManager::Init() {
53 // factory registering
54
55         RegisterFactory(new AliCDBDumpFactory());
56         RegisterFactory(new AliCDBLocalFactory()); 
57         // AliCDBGridFactory is registered only if AliEn libraries are enabled in Root
58         if(!gSystem->Exec("root-config --has-alien |grep yes 2>&1 > /dev/null")){ // returns 0 if yes
59                 AliInfo("AliEn classes enabled in Root. AliCDBGrid factory registered.");
60                 RegisterFactory(new AliCDBGridFactory());
61         }
62 }
63 //_____________________________________________________________________________
64 void AliCDBManager::Destroy() {
65 // delete ALCDBManager instance and active storages
66
67         if (fgInstance) {
68                 //fgInstance->Delete();
69                 delete fgInstance;
70                 fgInstance = 0x0;
71         }
72 }
73
74 //_____________________________________________________________________________
75 AliCDBManager::AliCDBManager():
76         fDefaultStorage(NULL),
77         fDrainStorage(NULL),
78         fCache(kTRUE),
79         fRun(0)
80 {
81 // default constuctor
82         fFactories.SetOwner(1);
83         fEntryCache.SetOwner(1);
84 }
85
86 //_____________________________________________________________________________
87 AliCDBManager::~AliCDBManager() {
88 // destructor
89         ClearCache();
90         DestroyActiveStorages();
91         fDrainStorage = 0x0;
92         fDefaultStorage = 0x0;
93 }
94
95 //_____________________________________________________________________________
96 AliCDBStorage* AliCDBManager::GetActiveStorage(const AliCDBParam* param) {
97 // get a storage object from the list of active storages 
98
99         return (AliCDBStorage*) fActiveStorages.GetValue(param);
100 }
101
102 //_____________________________________________________________________________
103 void AliCDBManager::PutActiveStorage(AliCDBParam* param, AliCDBStorage* storage){
104 // put a storage object into the list of active storages
105
106         fActiveStorages.Add(param, storage);
107         AliDebug(1, Form("Active storages: %d", fActiveStorages.GetEntries()));
108 }
109
110 //_____________________________________________________________________________
111 void AliCDBManager::RegisterFactory(AliCDBStorageFactory* factory) {
112 // add a storage factory to the list of registerd factories
113  
114         if (!fFactories.Contains(factory)) {
115                 fFactories.Add(factory);
116         }
117 }
118
119 //_____________________________________________________________________________
120 Bool_t AliCDBManager::HasStorage(const char* dbString) const {
121 // check if dbString is a URI valid for one of the registered factories 
122
123         TIter iter(&fFactories);
124
125         AliCDBStorageFactory* factory;
126         while ((factory = (AliCDBStorageFactory*) iter.Next())) {
127
128                 if (factory->Validate(dbString)) {
129                         return kTRUE;
130                 }       
131         }
132
133         return kFALSE;
134 }
135
136 //_____________________________________________________________________________
137 AliCDBParam* AliCDBManager::CreateParameter(const char* dbString) const {
138 // create AliCDBParam object from URI string
139
140         TIter iter(&fFactories);
141
142         AliCDBStorageFactory* factory;
143         while ((factory = (AliCDBStorageFactory*) iter.Next())) {
144
145                 AliCDBParam* param = factory->CreateParameter(dbString);
146                 if (param) {
147                         return param;
148                 }
149         }
150
151         return NULL;
152 }
153
154 //_____________________________________________________________________________
155 AliCDBStorage* AliCDBManager::GetStorage(const char* dbString) {
156 // get storage object from URI string
157         
158         AliCDBParam* param = CreateParameter(dbString);
159         if (!param) {
160                 return NULL;
161         }       
162
163         AliCDBStorage* aStorage = GetStorage(param);
164
165         delete param;
166         
167         return aStorage;
168 }
169
170 //_____________________________________________________________________________
171 AliCDBStorage* AliCDBManager::GetStorage(const AliCDBParam* param) {
172 // get storage object from AliCDBParam object
173
174         // if the list of active storages already contains 
175         // the requested storage, return it
176         AliCDBStorage* aStorage = GetActiveStorage(param);
177         if (aStorage) {
178                 return aStorage;
179         }
180
181         TIter iter(&fFactories);
182
183         AliCDBStorageFactory* factory;
184         
185         // loop on the list of registered factories
186         while ((factory = (AliCDBStorageFactory*) iter.Next())) {
187
188                 // each factory tries to create its storage from the parameter
189                 aStorage = factory->Create(param);
190                 if (aStorage) {
191                         PutActiveStorage(param->CloneParam(), aStorage);
192                         // if default storage is not set, set to this storage
193                         if(!fDefaultStorage){
194                                 fDefaultStorage=aStorage;
195                                 AliInfo(Form("Default storage set to: %s",(param->GetURI()).Data()));
196                         }
197                         return aStorage;
198                 }
199         }
200
201         return NULL;
202 }
203
204 //_____________________________________________________________________________
205 TList* AliCDBManager::GetActiveStorages() {
206 // return list of active storages
207
208         TList* result = new TList();
209
210         TIter iter(fActiveStorages.GetTable()); 
211         TPair* aPair;
212         while ((aPair = (TPair*) iter.Next())) {
213                 result->Add(aPair->Value());
214         }
215
216         return result;
217 }
218
219 //_____________________________________________________________________________
220 void AliCDBManager::SetDrain(const char* dbString) {
221 // set drain storage from URI string
222
223         fDrainStorage = GetStorage(dbString);   
224 }
225
226 //_____________________________________________________________________________
227 void AliCDBManager::SetDrain(const AliCDBParam* param) {
228 // set drain storage from AliCDBParam
229         
230         fDrainStorage = GetStorage(param);
231 }
232
233 //_____________________________________________________________________________
234 void AliCDBManager::SetDrain(AliCDBStorage* storage) {
235 // set drain storage from another active storage
236         
237         fDrainStorage = storage;
238 }
239
240 //_____________________________________________________________________________
241 Bool_t AliCDBManager::Drain(AliCDBEntry *entry) {
242 // drain retrieved object to drain storage
243
244         AliInfo("Draining into drain storage...");
245         return fDrainStorage->Put(entry);
246 }
247
248 //_____________________________________________________________________________
249 void AliCDBManager::SetDefaultStorage(const char* dbString) {
250 // sets default storage from URI string
251
252         fDefaultStorage = GetStorage(dbString); 
253 }
254
255 //_____________________________________________________________________________
256 void AliCDBManager::SetDefaultStorage(const AliCDBParam* param) {
257 // set default storage from AliCDBParam object
258         
259         fDrainStorage = GetStorage(param);
260 }
261
262 //_____________________________________________________________________________
263 void AliCDBManager::SetDefaultStorage(AliCDBStorage* storage) {
264 // set default storage from another active storage
265         
266         fDefaultStorage = storage;
267 }
268
269 //_____________________________________________________________________________
270 void AliCDBManager::SetSpecificStorage(const char* detName, const char* dbString) {
271 // sets storage specific for detector (works with AliCDBManager::Get(...))
272
273         AliCDBParam *aPar = CreateParameter(dbString);
274         if(!aPar) return;
275         SetSpecificStorage(detName, aPar);
276         delete aPar;
277 }
278
279 //_____________________________________________________________________________
280 void AliCDBManager::SetSpecificStorage(const char* detName, AliCDBParam* param) {
281 // sets storage specific for detector (works with AliCDBManager::Get(...))
282
283         if(!fDefaultStorage) {
284                 AliError("Please activate a default storage first!");   
285                 return;
286         }
287         
288         TObjString *objDetName = new TObjString(detName);
289         AliCDBParam *checkPar = (AliCDBParam*) fSpecificStorages.GetValue(objDetName);
290         if(checkPar){
291                 AliWarning(Form("%s storage already activated! It will be replaced by the new one",objDetName->String().Data()));
292                 fSpecificStorages.Remove(objDetName);   
293                 delete checkPar;
294         }
295         GetStorage(param);
296         fSpecificStorages.Add(objDetName, param->CloneParam());
297 }
298
299 //_____________________________________________________________________________
300 AliCDBStorage* AliCDBManager::GetSpecificStorage(const char* detName) {
301 // get storage specific for detector 
302
303         AliCDBParam *checkPar = (AliCDBParam*) fSpecificStorages.GetValue(detName);
304         if(!checkPar){
305                 AliError(Form("%s storage not found!",detName));
306                 return NULL;
307         } else {
308                 return GetStorage(checkPar);
309         }
310         
311 }
312
313 //_____________________________________________________________________________
314 AliCDBEntry* AliCDBManager::Get(const AliCDBPath& path, Int_t runNumber, 
315         Int_t version, Int_t subVersion) {
316 // get an AliCDBEntry object from the database
317
318         return Get(AliCDBId(path, runNumber, runNumber, version, subVersion));
319 }
320
321 //_____________________________________________________________________________
322 AliCDBEntry* AliCDBManager::Get(const AliCDBPath& path, 
323         const AliCDBRunRange& runRange, Int_t version,
324         Int_t subVersion) {
325 // get an AliCDBEntry object from the database!
326
327         return Get(AliCDBId(path, runRange, version, subVersion));
328 }
329
330 //_____________________________________________________________________________
331 AliCDBEntry* AliCDBManager::Get(const AliCDBId& query) {        
332 // get an AliCDBEntry object from the database
333         
334         if(!fDefaultStorage) {
335                 AliError("No storage set!");
336                 return NULL;
337         }
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         TObjString objStrLev0(query.GetLevel0());
354         AliCDBParam *aPar = (AliCDBParam*) fSpecificStorages.GetValue(&objStrLev0);
355         AliCDBStorage *aStorage;
356         
357         if(aPar) {
358                 aStorage=GetStorage(aPar);
359                 TString str = aPar->GetURI();
360                 AliDebug(2,Form("Looking into storage: %s",str.Data()));
361                 
362         } else {
363                 aStorage=GetDefaultStorage();
364                 AliDebug(2,"Looking into default storage");     
365         }
366                         
367         return aStorage->Get(query);
368 }
369
370 //_____________________________________________________________________________
371 TList* AliCDBManager::GetAll(const AliCDBPath& path, Int_t runNumber, 
372         Int_t version, Int_t subVersion) {
373 // get multiple AliCDBEntry objects from the database
374
375         return GetAll(AliCDBId(path, runNumber, runNumber, version,     
376                         subVersion));
377 }
378
379 //_____________________________________________________________________________
380 TList* AliCDBManager::GetAll(const AliCDBPath& path, 
381         const AliCDBRunRange& runRange, Int_t version, Int_t subVersion) {
382 // get multiple AliCDBEntry objects from the database
383
384         return GetAll(AliCDBId(path, runRange, version, subVersion));
385 }
386
387 //_____________________________________________________________________________
388 TList* AliCDBManager::GetAll(const AliCDBId& query) {
389 // get multiple AliCDBEntry objects from the database
390
391         if(!fDefaultStorage) {
392                 AliError("No storage set!");
393                 return NULL;
394         }
395
396         if (!query.IsValid()) {
397                 AliError(Form("Invalid query: %s", query.ToString().Data()));
398                 return NULL;
399         }
400
401         if(query.GetPath().BeginsWith('*')){
402                 AliError("Query too generic in this context!");
403                 return NULL;            
404         }
405
406         if (query.IsAnyRange()) {
407                 AliError(Form("Unspecified run or runrange: %s",
408                                 query.ToString().Data()));      
409                 return NULL;
410         }       
411         
412         TObjString objStrLev0(query.GetLevel0());
413         AliCDBParam *aPar = (AliCDBParam*) fSpecificStorages.GetValue(&objStrLev0);
414         AliCDBStorage *aStorage;
415         
416         if(aPar) {
417                 aStorage=GetStorage(aPar);
418                 TString str = aPar->GetURI();
419                 AliDebug(2,Form("Looking into storage: %s",str.Data()));
420                 
421         } else {
422                 aStorage=GetDefaultStorage();
423                 AliDebug(2,"Looking into default storage");     
424         }
425
426         TList *result = aStorage->GetAll(query);
427
428         return result;
429 }
430
431 //_____________________________________________________________________________
432 Bool_t AliCDBManager::Put(TObject* object, AliCDBId& id,  AliCDBMetaData* metaData){
433 // store an AliCDBEntry object into the database
434
435         AliCDBEntry anEntry(object, id, metaData);
436         return Put(&anEntry);
437
438 }
439
440
441 //_____________________________________________________________________________
442 Bool_t AliCDBManager::Put(AliCDBEntry* entry){
443 // store an AliCDBEntry object into the database
444
445         if(!fDefaultStorage) {
446                 AliError("No storage set!");
447                 return kFALSE;
448         }
449
450         if (!entry){
451                 AliError("No entry!");
452                 return kFALSE;
453         }
454
455         if (!entry->GetId().IsValid()) {
456                 AliError(Form("Invalid entry ID: %s", 
457                         entry->GetId().ToString().Data()));
458                 return kFALSE;
459         }       
460
461         if (!entry->GetId().IsSpecified()) {
462                 AliError(Form("Unspecified entry ID: %s", 
463                         entry->GetId().ToString().Data()));
464                 return kFALSE;
465         }
466
467         AliCDBId id = entry->GetId();
468         TObjString objStrLev0(id.GetLevel0());
469         AliCDBParam *aPar = (AliCDBParam*) fSpecificStorages.GetValue(&objStrLev0);
470         AliCDBStorage *aStorage;
471         
472         if(aPar) {
473                 aStorage=GetStorage(aPar);
474                 TString str = aPar->GetURI();
475                 AliDebug(2,Form("Storing object into storage: %s",str.Data()));
476                 
477         } else {
478                 aStorage=GetDefaultStorage();
479                 AliDebug(2,"Storing object into default storage");      
480         }
481
482         return aStorage->Put(entry);
483
484
485 }
486
487 //_____________________________________________________________________________
488 AliCDBEntry* AliCDBManager::Get(const char* path)
489 {
490 // get an AliCDBEntry object from the database, using fRun as run number
491     
492         if (fRun < 0)
493         {
494                 AliError("Run number not set! Use AliCDBManager::SetRun.");
495                 return 0;
496         }
497         
498         AliCDBEntry *entry;
499   
500         // first look into map of cached objects
501         entry = (AliCDBEntry*) fEntryCache.GetValue(path);
502         if(entry) return entry;
503
504         // Entry is not in cache -> retrieve it from CDB and cache it!!
505         entry = Get(path, fRun); 
506         if (!entry) return 0;
507    
508         if(fCache) CacheEntry(path, entry);
509   
510         return entry;
511
512 }
513
514 //_____________________________________________________________________________
515 void AliCDBManager::CacheEntry(const char* path, AliCDBEntry* entry)
516 {
517 // cache AliCDBEntry. Cache is valid until run number is changed.
518
519         AliDebug(2,Form("Filling cache with entry %s",path));
520         fEntryCache.Add(new TObjString(path), entry);
521         AliDebug(2,Form("Cache entries: %d",fEntryCache.GetEntries()));
522
523 }
524
525 //_____________________________________________________________________________
526 void AliCDBManager::SetRun(Long64_t run)
527 {
528 //
529 // Sets current run number.  
530 // When the run number changes the caching is cleared.
531 //
532   
533         if (fRun == run)
534                 return;
535   
536         fRun = run;
537         ClearCache();
538 }
539
540 //_____________________________________________________________________________
541 void AliCDBManager::ClearCache(){
542 // clear AliCDBEntry cache
543
544         AliDebug(2,Form("Clearing cache!"));
545         fEntryCache.DeleteAll();
546         AliDebug(2,Form("Cache entries: %d",fEntryCache.GetEntries()));
547
548 }
549
550 //_____________________________________________________________________________
551 void AliCDBManager::DestroyActiveStorages() {
552 // delete list of active storages
553
554         fActiveStorages.DeleteAll();
555         fSpecificStorages.DeleteAll();
556 }
557
558 //_____________________________________________________________________________
559 void AliCDBManager::DestroyActiveStorage(AliCDBStorage* /*storage*/) {
560 // destroys active storage
561
562 /*
563         TIter iter(fActiveStorages.GetTable()); 
564         TPair* aPair;
565         while ((aPair = (TPair*) iter.Next())) {
566                 if(storage == (AliCDBStorage*) aPair->Value())
567                         delete fActiveStorages.Remove(aPair->Key());
568                         storage->Delete(); storage=0x0;
569         }
570 */      
571
572 }
573
574 ///////////////////////////////////////////////////////////
575 // AliCDBManager Parameter class                         //
576 // interface to specific AliCDBParameter class           //
577 // (AliCDBGridParam, AliCDBLocalParam, AliCDBDumpParam)  //
578 ///////////////////////////////////////////////////////////
579
580 AliCDBParam::AliCDBParam() {
581 // constructor
582
583 }
584
585 //_____________________________________________________________________________
586 AliCDBParam::~AliCDBParam() {
587 // destructor
588
589 }
590