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