]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliCDBManager.cxx
Modifications in AliESDMuonTrack:
[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 //TODO OCDB and Reference folder should not be fully hardcoded but built from run number (or year/LHC period)
38 TString AliCDBManager::fgkCondUri("alien://folder=/alice/cern.ch/user/a/aliprod/testCDB/CDB?user=aliprod");
39 TString AliCDBManager::fgkRefUri("alien://folder=/alice/cern.ch/user/a/aliprod/testCDB/Reference?user=aliprod");
40 AliCDBManager* AliCDBManager::fgInstance = 0x0;
41
42 //_____________________________________________________________________________
43 AliCDBManager* AliCDBManager::Instance()
44 {
45 // returns AliCDBManager instance (singleton)
46
47         if (!fgInstance) {
48                 fgInstance = new AliCDBManager();
49                 fgInstance->Init();
50         }
51
52         return fgInstance;
53 }
54
55 //_____________________________________________________________________________
56 void AliCDBManager::Init() {
57 // factory registering
58
59         RegisterFactory(new AliCDBDumpFactory());
60         RegisterFactory(new AliCDBLocalFactory()); 
61         // AliCDBGridFactory is registered only if AliEn libraries are enabled in Root
62         if(!gSystem->Exec("root-config --has-alien |grep yes 2>&1 > /dev/null")){ // returns 0 if yes
63                 AliInfo("AliEn classes enabled in Root. AliCDBGrid factory registered.");
64                 RegisterFactory(new AliCDBGridFactory());
65                 fCondParam = CreateParameter(fgkCondUri);
66                 fRefParam = CreateParameter(fgkRefUri);
67         }
68
69         InitShortLived();
70 }
71 //_____________________________________________________________________________
72 void AliCDBManager::Destroy() {
73 // delete ALCDBManager instance and active storages
74
75         if (fgInstance) {
76                 //fgInstance->Delete();
77                 delete fgInstance;
78                 fgInstance = 0x0;
79         }
80 }
81
82 //_____________________________________________________________________________
83 AliCDBManager::AliCDBManager():
84   TObject(),
85   fCondParam(0),
86   fRefParam(0),
87   fFactories(),
88   fActiveStorages(),
89   fSpecificStorages(),
90   fDefaultStorage(NULL),
91   fRemoteStorage(NULL),
92   fDrainStorage(NULL),
93   fEntryCache(),
94   fCache(kTRUE),
95   fRun(-1),
96   fShortLived(0)
97 {
98 // default constuctor
99         fFactories.SetOwner(1);
100         fActiveStorages.SetOwner(1);
101         fSpecificStorages.SetOwner(1);
102         fEntryCache.SetOwner(1);
103 }
104
105 //_____________________________________________________________________________
106 AliCDBManager::~AliCDBManager() {
107 // destructor
108         ClearCache();
109         DestroyActiveStorages();
110         fFactories.Delete();
111         fDrainStorage = 0x0;
112         fDefaultStorage = 0x0;
113         fRemoteStorage = 0x0;
114         delete fCondParam;
115         delete fRefParam;
116         delete fShortLived; fShortLived = 0x0;
117 }
118
119 //_____________________________________________________________________________
120 void AliCDBManager::PutActiveStorage(AliCDBParam* param, AliCDBStorage* storage){
121 // put a storage object into the list of active storages
122
123         fActiveStorages.Add(param, storage);
124         AliDebug(1, Form("Active storages: %d", fActiveStorages.GetEntries()));
125 }
126
127 //_____________________________________________________________________________
128 void AliCDBManager::RegisterFactory(AliCDBStorageFactory* factory) {
129 // add a storage factory to the list of registerd factories
130  
131         if (!fFactories.Contains(factory)) {
132                 fFactories.Add(factory);
133         }
134 }
135
136 //_____________________________________________________________________________
137 Bool_t AliCDBManager::HasStorage(const char* dbString) const {
138 // check if dbString is a URI valid for one of the registered factories 
139
140         TIter iter(&fFactories);
141
142         AliCDBStorageFactory* factory=0;
143         while ((factory = (AliCDBStorageFactory*) iter.Next())) {
144
145                 if (factory->Validate(dbString)) {
146                         return kTRUE;
147                 }
148         }
149
150         return kFALSE;
151 }
152
153 //_____________________________________________________________________________
154 AliCDBParam* AliCDBManager::CreateParameter(const char* dbString) const {
155 // create AliCDBParam object from URI string
156
157         TIter iter(&fFactories);
158
159         AliCDBStorageFactory* factory=0;
160         while ((factory = (AliCDBStorageFactory*) iter.Next())) {
161                 AliCDBParam* param = factory->CreateParameter(dbString);
162                 if(param) return param;
163         }
164
165         return NULL;
166 }
167
168 //_____________________________________________________________________________
169 AliCDBStorage* AliCDBManager::GetStorage(const char* dbString) {
170 // get storage object from URI string
171         
172         AliCDBParam* param = CreateParameter(dbString);
173         if (!param) {
174                 AliError(Form("Failed to activate requested storage! Check URI: %s", dbString));
175                 return NULL;
176         }
177
178         AliCDBStorage* aStorage = GetStorage(param);
179
180         delete param;
181         return aStorage;
182 }
183
184 //_____________________________________________________________________________
185 AliCDBStorage* AliCDBManager::GetStorage(const AliCDBParam* param) {
186 // get storage object from AliCDBParam object
187
188         // if the list of active storages already contains
189         // the requested storage, return it
190         AliCDBStorage* aStorage = GetActiveStorage(param);
191         if (aStorage) {
192                 return aStorage;
193         }
194
195         TIter iter(&fFactories);
196
197         AliCDBStorageFactory* factory=0;
198
199         // loop on the list of registered factories
200         while ((factory = (AliCDBStorageFactory*) iter.Next())) {
201
202                 // each factory tries to create its storage from the parameter
203                 aStorage = factory->Create(param);
204                 if (aStorage) {
205                         PutActiveStorage(param->CloneParam(), aStorage);
206                         aStorage->SetURI(param->GetURI());
207                         if(fRun >= 0) {
208                                 if(aStorage->GetType() == "alien"){
209                                         aStorage->QueryCDB(fRun);
210                                 } else {
211                                         AliDebug(2,
212                                                 "Skipping query for valid files, it is used only in grid...");
213                                 }
214                         }
215                         return aStorage;
216                 }
217         }
218
219         AliError(Form("Failed to activate requested storage! Check URI: %s", param->GetURI().Data()));
220
221         return NULL;
222 }
223
224 //_____________________________________________________________________________
225 AliCDBStorage* AliCDBManager::GetActiveStorage(const AliCDBParam* param) {
226 // get a storage object from the list of active storages
227
228         return dynamic_cast<AliCDBStorage*> (fActiveStorages.GetValue(param));
229 }
230
231 //_____________________________________________________________________________
232 TList* AliCDBManager::GetActiveStorages() {
233 // return list of active storages
234 // user has responsibility to delete returned object
235
236         TList* result = new TList();
237
238         TIter iter(fActiveStorages.GetTable());
239         TPair* aPair=0;
240         while ((aPair = (TPair*) iter.Next())) {
241                 result->Add(aPair->Value());
242         }
243
244         return result;
245 }
246
247 //_____________________________________________________________________________
248 void AliCDBManager::SetDrain(const char* dbString) {
249 // set drain storage from URI string
250
251         fDrainStorage = GetStorage(dbString);   
252 }
253
254 //_____________________________________________________________________________
255 void AliCDBManager::SetDrain(const AliCDBParam* param) {
256 // set drain storage from AliCDBParam
257
258         fDrainStorage = GetStorage(param);
259 }
260
261 //_____________________________________________________________________________
262 void AliCDBManager::SetDrain(AliCDBStorage* storage) {
263 // set drain storage from another active storage
264         
265         fDrainStorage = storage;
266 }
267
268 //_____________________________________________________________________________
269 Bool_t AliCDBManager::Drain(AliCDBEntry *entry) {
270 // drain retrieved object to drain storage
271
272         AliDebug(2, "Draining into drain storage...");
273         return fDrainStorage->Put(entry);
274 }
275
276 //_____________________________________________________________________________
277 void AliCDBManager::SetDefaultStorage(const char* dbString) {
278 // sets default storage from URI string
279
280         AliInfo(Form("Setting Default storage to: %s",dbString));
281         AliCDBStorage* bckStorage = fDefaultStorage;
282
283         fDefaultStorage = GetStorage(dbString);
284
285         if(bckStorage && (fDefaultStorage != bckStorage)){
286                 AliWarning("Existing default storage replaced: clearing cache!");
287                 ClearCache();
288         }
289 }
290
291 //_____________________________________________________________________________
292 void AliCDBManager::SetDefaultStorage(const AliCDBParam* param) {
293 // set default storage from AliCDBParam object
294
295         AliCDBStorage* bckStorage = fDefaultStorage;
296
297         fDefaultStorage = GetStorage(param);
298
299         if(bckStorage && (fDefaultStorage != bckStorage)){
300                 AliWarning("Existing default storage replaced: clearing cache!");
301                 ClearCache();
302         }
303 }
304
305 //_____________________________________________________________________________
306 void AliCDBManager::SetDefaultStorage(AliCDBStorage* storage) {
307 // set default storage from another active storage
308
309         AliCDBStorage* bckStorage = fDefaultStorage;
310
311         fDefaultStorage = storage;
312
313         if(bckStorage && (fDefaultStorage != bckStorage)){
314                 AliWarning("Existing default storage replaced: clearing cache!");
315                 ClearCache();
316         }
317 }
318 //_____________________________________________________________________________
319 void AliCDBManager::SetRemoteStorage(const char* dbString) {
320 // sets remote storage from URI string
321 // Remote storage is queried if it is activated and if object was not found in default storage
322
323         AliInfo(Form("Setting remote storage to: %s",dbString));
324         fRemoteStorage = GetStorage(dbString);
325 }
326
327 //_____________________________________________________________________________
328 void AliCDBManager::SetRemoteStorage(const AliCDBParam* param) {
329 // set remote storage from AliCDBParam object
330 // Remote storage is queried if it is activated and if object was not found in default storage
331
332         fRemoteStorage = GetStorage(param);
333 }
334
335 //_____________________________________________________________________________
336 void AliCDBManager::SetRemoteStorage(AliCDBStorage* storage) {
337 // set remote storage from another active storage
338 // Remote storage is queried if it is activated and if object was not found in default storage
339
340         fRemoteStorage = storage;
341 }
342
343 //_____________________________________________________________________________
344 void AliCDBManager::SetSpecificStorage(const char* calibType, const char* dbString) {
345 // sets storage specific for detector or calibration type (works with AliCDBManager::Get(...))
346
347         AliCDBParam *aPar = CreateParameter(dbString);
348         if(!aPar) return;
349         SetSpecificStorage(calibType, aPar);
350         delete aPar;
351 }
352
353 //_____________________________________________________________________________
354 void AliCDBManager::SetSpecificStorage(const char* calibType, AliCDBParam* param) {
355 // sets storage specific for detector or calibration type (works with AliCDBManager::Get(...))
356 // Default storage should be defined prior to any specific storages, e.g.:
357 // AliCDBManager::instance()->SetDefaultStorage("alien://");
358 // AliCDBManager::instance()->SetSpecificStorage("TPC/*","local://DB_TPC");
359 // AliCDBManager::instance()->SetSpecificStorage("*/Align/*","local://DB_TPCAlign");
360 // calibType must be a valid CDB path! (3 level folder structure)
361
362         if(!fDefaultStorage) {
363                 AliError("Please activate a default storage first!");
364                 return;
365         }
366
367         AliCDBPath aPath(calibType);
368         if(!aPath.IsValid()){
369                 AliError(Form("Not a valid path: %s", calibType));
370                 return;
371         }
372
373         TObjString *objCalibType = new TObjString(aPath.GetPath());
374         if(fSpecificStorages.Contains(objCalibType)){
375                 AliWarning(Form("Storage \"%s\" already activated! It will be replaced by the new one",
376                                         calibType));
377                 AliCDBParam *checkPar = dynamic_cast<AliCDBParam*> (fSpecificStorages.GetValue(calibType));
378                 if(checkPar) delete checkPar;
379                 delete fSpecificStorages.Remove(objCalibType);
380         }
381         GetStorage(param);
382
383         fSpecificStorages.Add(objCalibType, param->CloneParam());
384 }
385
386 //_____________________________________________________________________________
387 AliCDBStorage* AliCDBManager::GetSpecificStorage(const char* calibType) {
388 // get storage specific for detector or calibration type 
389
390         AliCDBPath calibPath(calibType);
391         if(!calibPath.IsValid()) return NULL;
392
393         AliCDBParam *checkPar = (AliCDBParam*) fSpecificStorages.GetValue(calibPath.GetPath());
394         if(!checkPar){
395                 AliError(Form("%s storage not found!", calibType));
396                 return NULL;
397         } else {
398                 return GetStorage(checkPar);
399         }
400
401 }
402
403 //_____________________________________________________________________________
404 AliCDBParam* AliCDBManager::SelectSpecificStorage(const TString& path) {
405 // select storage valid for path from the list of specific storages
406
407         AliCDBPath aPath(path);
408         if(!aPath.IsValid()) return NULL;
409
410         TIter iter(&fSpecificStorages);
411         TObjString *aCalibType=0;
412         AliCDBPath tmpPath("null/null/null");
413         AliCDBParam* aPar=0;
414         while((aCalibType = (TObjString*) iter.Next())){
415                 AliCDBPath calibTypePath(aCalibType->GetName());
416                 if(calibTypePath.Comprises(aPath)) {
417                         if(calibTypePath.Comprises(tmpPath)) continue;
418                         aPar = (AliCDBParam*) fSpecificStorages.GetValue(aCalibType);
419                         tmpPath.SetPath(calibTypePath.GetPath());
420                 }
421         }
422         return aPar;
423 }
424
425 //_____________________________________________________________________________
426 AliCDBEntry* AliCDBManager::Get(const AliCDBPath& path, Int_t runNumber,
427         Int_t version, Int_t subVersion) {
428 // get an AliCDBEntry object from the database
429
430         if(runNumber < 0){
431                 // RunNumber is not specified. Try with fRun
432                 if (fRun < 0){
433                         AliError("Run number neither specified in query nor set in AliCDBManager! Use AliCDBManager::SetRun.");
434                         return NULL;
435                 }
436                 runNumber = fRun;
437         }
438
439         return Get(AliCDBId(path, runNumber, runNumber, version, subVersion));
440 }
441
442 //_____________________________________________________________________________
443 AliCDBEntry* AliCDBManager::Get(const AliCDBPath& path,
444         const AliCDBRunRange& runRange, Int_t version,
445         Int_t subVersion) {
446 // get an AliCDBEntry object from the database!
447
448         return Get(AliCDBId(path, runRange, version, subVersion));
449 }
450
451 //_____________________________________________________________________________
452 AliCDBEntry* AliCDBManager::Get(const AliCDBId& query) {
453 // get an AliCDBEntry object from the database
454         
455         if(!fDefaultStorage) {
456                 AliError("No storage set!");
457                 return NULL;
458         }
459
460         // check if query's path and runRange are valid
461         // query is invalid also if version is not specified and subversion is!
462         if (!query.IsValid()) {
463                 AliError(Form("Invalid query: %s", query.ToString().Data()));
464                 return NULL;
465         }
466         
467         // query is not specified if path contains wildcard or run range= [-1,-1]
468         if (!query.IsSpecified()) {
469                 AliError(Form("Unspecified query: %s",
470                                 query.ToString().Data()));
471                 return NULL;
472         }
473
474         if(fCache && query.GetFirstRun() != fRun)
475                 AliWarning("Run number explicitly set in query: CDB cache temporarily disabled!");
476
477
478         AliCDBEntry *entry=0;
479
480         // first look into map of cached objects
481         if(fCache && query.GetFirstRun() == fRun)
482                 entry = (AliCDBEntry*) fEntryCache.GetValue(query.GetPath());
483
484         if(entry) {
485                 AliDebug(2, Form("Object %s retrieved from cache !!",query.GetPath().Data()));
486                 return entry;
487         }
488
489         // Entry is not in cache -> retrieve it from CDB and cache it!!
490         AliCDBStorage *aStorage=0;
491         AliCDBParam *aPar=SelectSpecificStorage(query.GetPath());
492         Bool_t usedDefStorage=kTRUE;
493
494         if(aPar) {
495                 aStorage=GetStorage(aPar);
496                 TString str = aPar->GetURI();
497                 AliDebug(2,Form("Looking into storage: %s",str.Data()));
498                 usedDefStorage=kFALSE;
499
500         } else {
501                 aStorage=GetDefaultStorage();
502                 AliDebug(2,"Looking into default storage");
503         }
504
505         entry = aStorage->Get(query);
506
507         if (!entry && usedDefStorage && IsRemoteStorageSet()) {
508                 AliWarning(Form("Object not found in default storage: Looking into remote storage!"));
509                 entry = fRemoteStorage->Get(query);
510         }
511
512         if(entry && fCache && (query.GetFirstRun() == fRun)){
513                 CacheEntry(query.GetPath(), entry);
514         }
515
516         return entry;
517
518 }
519
520 //_____________________________________________________________________________
521 AliCDBId* AliCDBManager::GetId(const AliCDBPath& path, Int_t runNumber,
522         Int_t version, Int_t subVersion) {
523 // get the AliCDBId of the valid object from the database (does not retrieve the object)
524 // User must delete returned object!
525
526         if(runNumber < 0){
527                 // RunNumber is not specified. Try with fRun
528                 if (fRun < 0){
529                         AliError("Run number neither specified in query nor set in AliCDBManager! Use AliCDBManager::SetRun.");
530                         return NULL;
531                 }
532                 runNumber = fRun;
533         }
534
535         return GetId(AliCDBId(path, runNumber, runNumber, version, subVersion));
536 }
537
538 //_____________________________________________________________________________
539 AliCDBId* AliCDBManager::GetId(const AliCDBPath& path,
540         const AliCDBRunRange& runRange, Int_t version,
541         Int_t subVersion) {
542 // get the AliCDBId of the valid object from the database (does not retrieve the object)
543 // User must delete returned object!
544
545         return GetId(AliCDBId(path, runRange, version, subVersion));
546 }
547
548 //_____________________________________________________________________________
549 AliCDBId* AliCDBManager::GetId(const AliCDBId& query) {
550 // get the AliCDBId of the valid object from the database (does not retrieve the object)
551 // User must delete returned object!
552
553         if(!fDefaultStorage) {
554                 AliError("No storage set!");
555                 return NULL;
556         }
557
558         // check if query's path and runRange are valid
559         // query is invalid also if version is not specified and subversion is!
560         if (!query.IsValid()) {
561                 AliError(Form("Invalid query: %s", query.ToString().Data()));
562                 return NULL;
563         }
564         
565         // query is not specified if path contains wildcard or run range= [-1,-1]
566         if (!query.IsSpecified()) {
567                 AliError(Form("Unspecified query: %s",
568                                 query.ToString().Data()));
569                 return NULL;
570         }
571
572         if(fCache && query.GetFirstRun() != fRun)
573                 AliWarning("Run number explicitly set in query: CDB cache temporarily disabled!");
574
575         AliCDBEntry* entry = 0;
576
577         // first look into map of cached objects
578         if(fCache && query.GetFirstRun() == fRun)
579                 entry = (AliCDBEntry*) fEntryCache.GetValue(query.GetPath());
580
581         if(entry) {
582                 AliDebug(2, Form("Object %s retrieved from cache !!",query.GetPath().Data()));
583                 return dynamic_cast<AliCDBId*> (entry->GetId().Clone());
584         }
585
586         // Entry is not in cache -> retrieve it from CDB and cache it!!
587         AliCDBStorage *aStorage=0;
588         AliCDBParam *aPar=SelectSpecificStorage(query.GetPath());
589
590         if(aPar) {
591                 aStorage=GetStorage(aPar);
592                 TString str = aPar->GetURI();
593                 AliDebug(2,Form("Looking into storage: %s",str.Data()));
594                 
595         } else {
596                 aStorage=GetDefaultStorage();
597                 AliDebug(2,"Looking into default storage");
598         }
599
600         return aStorage->GetId(query);
601
602 }
603
604 //_____________________________________________________________________________
605 TList* AliCDBManager::GetAll(const AliCDBPath& path, Int_t runNumber,
606         Int_t version, Int_t subVersion) {
607 // get multiple AliCDBEntry objects from the database
608
609         if(runNumber < 0){
610                 // RunNumber is not specified. Try with fRun
611                 if (fRun < 0){
612                         AliError("Run number neither specified in query nor set in AliCDBManager! Use AliCDBManager::SetRun.");
613                         return NULL;
614                 }
615                 runNumber = fRun;
616         }
617
618         return GetAll(AliCDBId(path, runNumber, runNumber, version,     
619                         subVersion));
620 }
621
622 //_____________________________________________________________________________
623 TList* AliCDBManager::GetAll(const AliCDBPath& path,
624         const AliCDBRunRange& runRange, Int_t version, Int_t subVersion) {
625 // get multiple AliCDBEntry objects from the database
626
627         return GetAll(AliCDBId(path, runRange, version, subVersion));
628 }
629
630 //_____________________________________________________________________________
631 TList* AliCDBManager::GetAll(const AliCDBId& query) {
632 // get multiple AliCDBEntry objects from the database
633 // Warning: this method works correctly only for queries of the type "Detector/*"
634 //              and not for more specific queries e.g. "Detector/Calib/*" !
635 // Warning #2: Entries are cached, but GetAll will keep on retrieving objects from OCDB!
636 //              To get an object from cache use Get() function
637
638         if(!fDefaultStorage) {
639                 AliError("No storage set!");
640                 return NULL;
641         }
642
643         if (!query.IsValid()) {
644                 AliError(Form("Invalid query: %s", query.ToString().Data()));
645                 return NULL;
646         }
647
648         if((fSpecificStorages.GetEntries()>0) && query.GetPath().BeginsWith('*')){
649                 // if specific storages are active a query with "*" is ambiguous
650                 AliError("Query too generic in this context!");
651                 return NULL;
652         }
653
654         if (query.IsAnyRange()) {
655                 AliError(Form("Unspecified run or runrange: %s",
656                                 query.ToString().Data()));
657                 return NULL;
658         }
659
660         AliCDBParam *aPar=SelectSpecificStorage(query.GetPath());
661
662         AliCDBStorage *aStorage;
663         if(aPar) {
664                 aStorage=GetStorage(aPar);
665                 AliDebug(2,Form("Looking into storage: %s", aPar->GetURI().Data()));
666
667         } else {
668                 aStorage=GetDefaultStorage();
669                 AliDebug(2,"Looking into default storage");
670         }
671
672         TList *result = 0;
673         if(aStorage) result = aStorage->GetAll(query);
674         if(!result) return 0;
675
676        // loop on result to check whether entries should be re-queried with specific storages
677         if(fSpecificStorages.GetEntries()>0 && ! (fSpecificStorages.GetEntries() == 1 && aPar)) {
678                 AliInfo("Now look into all other specific storages...");
679
680                 TIter iter(result);
681                 AliCDBEntry* chkEntry=0;
682
683                 while((chkEntry = dynamic_cast<AliCDBEntry*> (iter.Next()))){
684                         AliCDBId& chkId = chkEntry->GetId();
685                         AliDebug(2, Form("Checking id %s ", chkId.GetPath().Data()));
686                         AliCDBParam *chkPar=SelectSpecificStorage(chkId.GetPath());
687                         if (!chkPar || aPar == chkPar) continue;
688                         AliCDBStorage *chkStorage = GetStorage(chkPar);
689                         AliDebug(2, Form("Found specific storage! %s", chkPar->GetURI().Data()));
690
691                         AliCDBEntry *newEntry=0;
692                         chkId.SetRunRange(query.GetFirstRun(), query.GetLastRun());
693                         chkId.SetVersion(query.GetVersion());
694                         chkId.SetSubVersion(query.GetSubVersion());
695
696                         if(chkStorage) newEntry = chkStorage->Get(chkId);
697                         if(!newEntry) continue;
698
699                         // object is found in specific storage: replace entry in the result list!
700                         chkEntry->SetOwner(1);
701                         delete result->Remove(chkEntry);
702                         result->AddFirst(newEntry);
703                 }
704
705                 Int_t nEntries = result->GetEntries();
706                 AliInfo("After look into other specific storages, result list is:");
707                 for(int i=0; i<nEntries;i++){
708                         AliCDBEntry *entry = (AliCDBEntry*) result->At(i);
709                         AliInfo(Form("%s",entry->GetId().ToString().Data()));
710                 }
711         }
712
713         // caching entries
714         if(fCache && (query.GetFirstRun() == fRun)){
715
716                 TIter iter(result);
717                 AliCDBEntry* entry=0;
718                 while((entry = dynamic_cast<AliCDBEntry*> (iter.Next()))){
719                         CacheEntry(entry->GetId().GetPath(), entry);
720                 }
721         }
722
723         return result;
724 }
725
726 //_____________________________________________________________________________
727 Bool_t AliCDBManager::Put(TObject* object, AliCDBId& id,  AliCDBMetaData* metaData, DataType type){
728 // store an AliCDBEntry object into the database
729
730         AliCDBEntry anEntry(object, id, metaData);
731         return Put(&anEntry, type);
732
733 }
734
735
736 //_____________________________________________________________________________
737 Bool_t AliCDBManager::Put(AliCDBEntry* entry, DataType type){
738 // store an AliCDBEntry object into the database
739
740         if(type == kPrivate && !fDefaultStorage) {
741                 AliError("No storage set!");
742                 return kFALSE;
743         }
744
745         if (!entry){
746                 AliError("No entry!");
747                 return kFALSE;
748         }
749
750         if (!entry->GetId().IsValid()) {
751                 AliError(Form("Invalid entry ID: %s", 
752                         entry->GetId().ToString().Data()));
753                 return kFALSE;
754         }       
755
756         if (!entry->GetId().IsSpecified()) {
757                 AliError(Form("Unspecified entry ID: %s", 
758                         entry->GetId().ToString().Data()));
759                 return kFALSE;
760         }
761
762         AliCDBId id = entry->GetId();
763         AliCDBParam *aPar = SelectSpecificStorage(id.GetPath());
764
765         AliCDBStorage *aStorage=0;
766         
767         if(aPar) {
768                 aStorage=GetStorage(aPar);
769         } else {
770                 switch(type){
771                         case kCondition:
772                                 aStorage = GetStorage(fCondParam);
773                                 break;
774                         case kReference:
775                                 aStorage = GetStorage(fRefParam);
776                                 break;
777                         case kPrivate:
778                                 aStorage = GetDefaultStorage();
779                                 break;
780                 }
781         }
782
783         AliDebug(2,Form("Storing object into storage: %s", aStorage->GetURI().Data()));
784
785         Bool_t result = aStorage->Put(entry, type);
786
787         if(fRun >= 0) QueryCDB();
788
789         return result;
790
791
792 }
793
794 //_____________________________________________________________________________
795 void AliCDBManager::CacheEntry(const char* path, AliCDBEntry* entry)
796 {
797 // cache AliCDBEntry. Cache is valid until run number is changed.
798
799         AliCDBEntry *chkEntry = dynamic_cast<AliCDBEntry*> (fEntryCache.GetValue(path));
800
801         if(chkEntry) {
802                 AliDebug(2, Form("Object %s already in cache !!", path));
803                 return;
804         } else {
805                 AliDebug(2,Form("Caching entry %s", path));
806         }
807
808         fEntryCache.Add(new TObjString(path), entry);
809         AliDebug(2,Form("Cache entries: %d", fEntryCache.GetEntries()));
810
811 }
812
813 //_____________________________________________________________________________
814 void AliCDBManager::Print(Option_t* /*option*/) const
815 {
816 // Print list of active storages and their URIs
817
818         TString output=Form("Run number = %d; ",fRun);
819         output += "Cache is ";
820         if(!fCache) output += "NOT ";
821         output += Form("ACTIVE; Number of active storages: %d\n",fActiveStorages.GetEntries());
822
823         if(fDefaultStorage) {
824                 output += Form("\t*** Default Storage URI: \"%s\"\n",fDefaultStorage->GetURI().Data());
825 //              AliInfo(output.Data());
826         }
827         if(fSpecificStorages.GetEntries()>0) {
828                 TIter iter(fSpecificStorages.GetTable());
829                 TPair *aPair=0;
830                 Int_t i=1;
831                 while((aPair = (TPair*) iter.Next())){
832                         output += Form("\t*** Specific storage %d: Path \"%s\" -> URI \"%s\"\n",
833                                 i++, ((TObjString*) aPair->Key())->GetName(),
834                                 ((AliCDBParam*) aPair->Value())->GetURI().Data());
835                 }
836         }
837         if(fDrainStorage) {
838                 output += Form("*** Drain Storage URI: %s\n",fDrainStorage->GetURI().Data());
839         }
840         AliInfo(output.Data());
841 }
842
843 //_____________________________________________________________________________
844 void AliCDBManager::SetRun(Int_t run)
845 {
846 // Sets current run number.
847 // When the run number changes the caching is cleared.
848   
849         if (fRun == run)
850                 return;
851   
852         fRun = run;
853         ClearCache();
854         QueryCDB();
855 }
856
857 //_____________________________________________________________________________
858 void AliCDBManager::ClearCache(){
859 // clear AliCDBEntry cache
860
861         AliDebug(2,Form("Clearing cache!"));
862         fEntryCache.DeleteAll();
863         AliDebug(2,Form("Cache entries: %d",fEntryCache.GetEntries()));
864
865 }
866
867 //_____________________________________________________________________________
868 void AliCDBManager::UnloadFromCache(const char* path){
869 // unload cached object
870
871         AliCDBPath queryPath(path);
872         if(!queryPath.IsValid()) return;
873
874         if(!queryPath.IsWildcard()) { // path is not wildcard, get it directly from the cache and unload it!
875                 if(fEntryCache.Contains(path)){
876                         AliInfo(Form("Unloading object \"%s\" from cache", path));
877                         TObjString pathStr(path);
878                         AliCDBEntry *entry = dynamic_cast<AliCDBEntry*> (fEntryCache.GetValue(&pathStr));
879                         if(entry) delete entry;
880                         delete fEntryCache.Remove(&pathStr);
881                 } else {
882                         AliError(Form("Cache does not contain object \"%s\"!", path))
883                 }
884                 AliDebug(2,Form("Cache entries: %d",fEntryCache.GetEntries()));
885                 return;
886         }
887
888         // path is wildcard: loop on the cache and unload all comprised objects!
889         TIter iter(fEntryCache.GetTable());
890         TPair* pair = 0;
891
892         while((pair = dynamic_cast<TPair*> (iter.Next()))){
893                 AliCDBPath entryPath = pair->Key()->GetName();
894                 if(queryPath.Comprises(entryPath)) {
895                         AliInfo(Form("Unloading object \"%s\" from cache", entryPath.GetPath().Data()));
896                         TObjString pathStr(entryPath.GetPath().Data());
897                         AliCDBEntry *entry = dynamic_cast<AliCDBEntry*> (fEntryCache.GetValue(&pathStr));
898                         if(entry) delete entry;
899                         delete fEntryCache.Remove(&pathStr);
900                 }
901         }
902         AliDebug(2,Form("Cache entries: %d",fEntryCache.GetEntries()));
903 }
904
905 //_____________________________________________________________________________
906 void AliCDBManager::DestroyActiveStorages() {
907 // delete list of active storages
908
909         fActiveStorages.DeleteAll();
910         fSpecificStorages.DeleteAll();
911 }
912
913 //_____________________________________________________________________________
914 void AliCDBManager::DestroyActiveStorage(AliCDBStorage* /*storage*/) {
915 // destroys active storage
916
917 /*
918         TIter iter(fActiveStorages.GetTable());
919         TPair* aPair;
920         while ((aPair = (TPair*) iter.Next())) {
921                 if(storage == (AliCDBStorage*) aPair->Value())
922                         delete fActiveStorages.Remove(aPair->Key());
923                         storage->Delete(); storage=0x0;
924         }
925 */
926
927 }
928
929 //_____________________________________________________________________________
930 void AliCDBManager::QueryCDB() {
931 // query default and specific storages for files valid for fRun. Every storage loads the Ids into its list.
932
933         if (fRun < 0){
934                 AliError("Run number not yet set! Use AliCDBManager::SetRun.");
935         return;
936         }
937         if (!fDefaultStorage){
938                 AliError("Default storage is not set! Use AliCDBManager::SetDefaultStorage");
939         return;
940         }
941         if(fDefaultStorage->GetType() == "alien"){
942                 fDefaultStorage->QueryCDB(fRun);
943         } else {
944                 AliDebug(2,"Skipping query for valid files, it used only in grid...");
945         }
946
947         TIter iter(&fSpecificStorages);
948         TObjString *aCalibType=0;
949         AliCDBParam* aPar=0;
950         while((aCalibType = dynamic_cast<TObjString*> (iter.Next()))){
951                 aPar = (AliCDBParam*) fSpecificStorages.GetValue(aCalibType);
952                 if(aPar) {
953                         AliDebug(2,Form("Querying specific storage %s",aCalibType->GetName()));
954                         AliCDBStorage *aStorage = GetStorage(aPar);
955                         if(aStorage->GetType() == "alien"){
956                                 aStorage->QueryCDB(fRun,aCalibType->GetName());
957                         } else {
958                                 AliDebug(2,
959                                         "Skipping query for valid files, it is used only in grid...");
960                         }
961                 }
962         }
963 }
964
965 //______________________________________________________________________________________________
966 const char* AliCDBManager::GetDataTypeName(DataType type)
967 {
968   // returns the name (string) of the data type
969
970       switch (type){
971             case kCondition: return "Conditions";
972             case kReference: return "Reference";
973             case kPrivate: return "Private";
974      }
975      return 0;
976
977 }
978
979 //______________________________________________________________________________________________
980 void AliCDBManager::InitShortLived()
981 {
982   // Init the list of short-lived objects
983   // currently disabled
984
985         fShortLived=0x0;
986
987 //      fShortLived = new TList();
988 //      fShortLived->SetOwner(1);
989 //
990 //      fShortLived->Add(new TObjString("EMCAL/Calib/Data"));
991 // 
992 //      fShortLived->Add(new TObjString("HMPID/Calib/Nmean"));
993 //      fShortLived->Add(new TObjString("HMPID/Calib/Qthre"));
994 // 
995 //      fShortLived->Add(new TObjString("ITS/Calib/CalibSPD"));
996 // 
997 //      fShortLived->Add(new TObjString("MUON/Calib/Gains"));
998 //      fShortLived->Add(new TObjString("MUON/Calib/HV"));
999 //      fShortLived->Add(new TObjString("MUON/Calib/Pedestals"));
1000 // 
1001 //      fShortLived->Add(new TObjString("PHOS/Calib/CpvGainPedestals"));
1002 //      fShortLived->Add(new TObjString("PHOS/Calib/EmcGainPedestals"));
1003 // 
1004 //      fShortLived->Add(new TObjString("PMD/Calib/Data"));
1005 // 
1006 //      fShortLived->Add(new TObjString("TRD/Calib/ChamberGainFactor"));
1007 //      fShortLived->Add(new TObjString("TRD/Calib/LocalGainFactor"));
1008 //      fShortLived->Add(new TObjString("TRD/Calib/ChamberT0"));
1009 //      fShortLived->Add(new TObjString("TRD/Calib/LocalT0"));
1010 //      fShortLived->Add(new TObjString("TRD/Calib/ChamberVdrift"));
1011 //      fShortLived->Add(new TObjString("TRD/Calib/LocalVdrift"));
1012 // 
1013 //      fShortLived->Add(new TObjString("ZDC/Calib/Data"));
1014
1015 }
1016
1017 //______________________________________________________________________________________________
1018 Bool_t AliCDBManager::IsShortLived(const char* path)
1019 {
1020   // returns the name (string) of the data type
1021
1022         if(!fShortLived) return kFALSE;
1023
1024         AliCDBPath aPath(path);
1025         if(!aPath.IsValid()){
1026                 AliError(Form("Not a valid path: %s", path));
1027                 return kFALSE;
1028         }
1029
1030         return fShortLived->Contains(path);
1031
1032 }
1033
1034 ///////////////////////////////////////////////////////////
1035 // AliCDBManager Parameter class                         //
1036 // interface to specific AliCDBParameter class           //
1037 // (AliCDBGridParam, AliCDBLocalParam, AliCDBDumpParam)  //
1038 ///////////////////////////////////////////////////////////
1039
1040 AliCDBParam::AliCDBParam():
1041   fType(),
1042   fURI()
1043 {
1044 // constructor
1045
1046 }
1047
1048 //_____________________________________________________________________________
1049 AliCDBParam::~AliCDBParam() {
1050 // destructor
1051
1052 }
1053