]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/CDB/AliCDBManager.cxx
initialising from a snapshot must occur before the CDB manager is locked
[u/mrichter/AliRoot.git] / STEER / CDB / 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 <stdlib.h>
22
23 #include "AliCDBManager.h"
24 #include "AliCDBStorage.h"
25 #include "AliLog.h"
26 #include "AliCDBDump.h"
27 #include "AliCDBLocal.h"
28 #include "AliCDBGrid.h"
29 #include "AliCDBEntry.h"
30 #include "AliCDBHandler.h"
31
32 #include <TObjString.h>
33 #include <TSAXParser.h>
34 #include <TFile.h>
35 #include <TKey.h>
36 #include <TUUID.h>
37 #include <TGrid.h>
38
39 ClassImp(AliCDBParam)
40
41 ClassImp(AliCDBManager)
42
43 //TODO OCDB and Reference folder should not be fully hardcoded but built from run number (or year/LHC period)
44 TString AliCDBManager::fgkCondUri("alien://folder=/alice/cern.ch/user/a/aliprod/testCDB/CDB?user=aliprod");
45 TString AliCDBManager::fgkRefUri("alien://folder=/alice/cern.ch/user/a/aliprod/testCDB/Reference?user=aliprod");
46 TString AliCDBManager::fgkMCIdealStorage("alien://folder=/alice/simulation/2008/v4-15-Release/Ideal");
47 TString AliCDBManager::fgkMCFullStorage("alien://folder=/alice/simulation/2008/v4-15-Release/Full");
48 TString AliCDBManager::fgkMCResidualStorage("alien://folder=/alice/simulation/2008/v4-15-Release/Residual");
49 TString AliCDBManager::fgkOCDBFolderXMLfile("alien:///alice/data/OCDBFoldervsRunRange.xml");
50 AliCDBManager* AliCDBManager::fgInstance = 0x0;
51
52 //_____________________________________________________________________________
53 AliCDBManager* AliCDBManager::Instance(TMap *entryCache, Int_t run)
54 {
55 // returns AliCDBManager instance (singleton)
56
57         if (!fgInstance) {
58                 fgInstance = new AliCDBManager();
59                 if (!entryCache)
60                   fgInstance->Init();
61                 else
62                   fgInstance->InitFromCache(entryCache,run);
63         }
64
65         return fgInstance;
66 }
67
68 //_____________________________________________________________________________
69 void AliCDBManager::Init() {
70 // factory registering
71
72         RegisterFactory(new AliCDBDumpFactory());
73         RegisterFactory(new AliCDBLocalFactory()); 
74         // AliCDBGridFactory is registered only if AliEn libraries are enabled in Root
75         if(!gSystem->Exec("root-config --has-alien |grep yes 2>&1 > /dev/null")){ // returns 0 if yes
76                 AliInfo("AliEn classes enabled in Root. AliCDBGrid factory registered.");
77                 RegisterFactory(new AliCDBGridFactory());
78                 fCondParam = CreateParameter(fgkCondUri);
79                 fRefParam = CreateParameter(fgkRefUri);
80         }
81
82         InitShortLived();
83 }
84
85 //_____________________________________________________________________________
86 void AliCDBManager::InitFromCache(TMap *entryCache, Int_t run) {
87 // initialize manager from existing cache
88 // used on the slaves in case of parallel reconstruction
89   SetRun(run);
90
91   TIter iter(entryCache->GetTable());
92   TPair* pair = 0;
93
94   while((pair = dynamic_cast<TPair*> (iter.Next()))){
95     fEntryCache.Add(pair->Key(),pair->Value());
96   }
97   // fEntry is the new owner of the cache
98   fEntryCache.SetOwnerKeyValue(kTRUE,kTRUE);
99   entryCache->SetOwnerKeyValue(kFALSE,kFALSE);
100   AliInfo(Form("%d cache entries have been loaded",fEntryCache.GetEntries()));
101 }
102
103 //_____________________________________________________________________________
104 void  AliCDBManager::DumpToSnapshotFile(const char* snapshotFileName){
105 // 
106 // dump the entries map and the ids list to
107 // the output file
108
109     // open the file
110     TFile *f = TFile::Open(snapshotFileName,"RECREATE");
111     if (!f || f->IsZombie()){
112         AliError(Form("Cannot open file %s",snapshotFileName));
113         return;
114     }
115
116     Printf("\ndumping entriesMap (entries'cache) with %d entries!\n", fEntryCache.GetEntries()); 
117     Printf("\ndumping entriesList with %d entries!\n", fIds->GetEntries());
118
119     f->cd();                                                                                           
120
121     f->WriteObject(&fEntryCache,"CDBentriesMap");
122     f->WriteObject(fIds,"CDBidsList");
123     f->Close();
124     delete f;
125
126     exit(0);
127 }
128
129 //_____________________________________________________________________________
130 Bool_t AliCDBManager::InitFromSnapshot(const char* snapshotFileName, Bool_t overwrite){
131 // initialize manager from a CDB snapshot, that is add the entries
132 // to the entries map and the ids to the ids list taking them from
133 // the map and the list found in the input file
134
135 // if the manager is locked it cannot initialize from a snapshot
136     if(fLock) {
137         AliError("Being locked I cannot initialize from the snapshot!");
138         return kFALSE;
139     }   
140
141     // open the file
142     TString snapshotFile(snapshotFileName);
143     if(snapshotFile.BeginsWith("alien://")){
144         if(!gGrid) {
145             TGrid::Connect("alien://","");
146             if(!gGrid) {
147                 AliError("Connection to alien failed!");
148                 return kFALSE;
149             }
150         }
151     }
152
153     TFile *f = TFile::Open(snapshotFileName);
154     if (!f || f->IsZombie()){
155         AliError(Form("Cannot open file %s",snapshotFileName));
156         return kFALSE;
157     }
158
159     // retrieve entries' map from snapshot file
160     TMap *entriesMap = 0;
161     TIter next(f->GetListOfKeys());
162     TKey *key;
163     while ((key = (TKey*)next())) {
164         if (strcmp(key->GetClassName(),"TMap") != 0) continue;
165         entriesMap = (TMap*)key->ReadObj();
166         break;
167     }
168     if (!entriesMap || entriesMap->GetEntries()==0){
169         AliError("Cannot get valid map of CDB entries from snapshot file");
170         return kFALSE;
171     }
172
173     // retrieve ids' list from snapshot file
174     TList *idsList = 0;
175     TIter nextKey(f->GetListOfKeys());
176     TKey *keyN;
177     while ((keyN = (TKey*)nextKey())) {
178         if (strcmp(keyN->GetClassName(),"TList") != 0) continue;
179         idsList = (TList*)keyN->ReadObj();
180         break;
181     }
182     if (!idsList || idsList->GetEntries()==0){
183         AliError("Cannot get valid list of CDB entries from snapshot file");
184         return kFALSE;
185     }
186
187     // Add each (entry,id) from the snapshot to the memory: entry to the cache, id to the list of ids.
188     // If "overwrite" is false: add the entry to the cache and its id to the list of ids
189     // only if neither of them is already there.
190     // If "overwrite" is true: write the snapshot entry,id in any case. If something
191     // was already there for that calibration type, remove it and issue a warning
192     TIter iterObj(entriesMap->GetTable());
193     TPair* pair = 0;
194     Int_t nAdded=0;
195     while((pair = dynamic_cast<TPair*> (iterObj.Next()))){
196         TObjString* os = (TObjString*) pair->Key();
197         TString path = os->GetString();
198         TIter iterId(idsList);
199         AliCDBId* id=0;
200         AliCDBId* correspondingId=0;
201         while((id = dynamic_cast<AliCDBId*> (iterId.Next()))){
202             TString idpath(id->GetPath());
203             if(idpath==path){
204                 correspondingId=id;
205                 break;
206             }
207         }
208         if(!correspondingId){
209             AliError(Form("id for \"%s\" not found in the snapshot (while entry was). This entry is skipped!",path.Data()));
210             break;
211         }
212         Bool_t cached = fEntryCache.Contains(path.Data());
213         Bool_t registeredId = kFALSE;
214         TIter iter(fIds);
215         AliCDBId *idT = 0;
216         while((idT = dynamic_cast<AliCDBId*> (iter.Next()))){
217             if(idT->GetPath()==path){
218                 registeredId = kTRUE;
219                 break;
220             }
221         }
222
223         if(overwrite){
224             if(cached || registeredId){
225                 AliWarning(Form("An entry was already cached for \"%s\". Removing it before caching from snapshot",path.Data()));
226                 UnloadFromCache(path.Data());
227             }
228             fEntryCache.Add(pair->Key(),pair->Value());
229             fIds->Add(id);
230             nAdded++;
231         }else{
232             if(cached || registeredId){
233                 AliWarning(Form("An entry was already cached for \"%s\". Not adding this object from snapshot",path.Data()));
234             }else{
235                 fEntryCache.Add(pair->Key(),pair->Value());
236                 fIds->Add(id);
237                 nAdded++;
238             }
239         }
240     }
241
242     // fEntry is the new owner of the cache
243     fEntryCache.SetOwnerKeyValue(kTRUE,kTRUE);
244     entriesMap->SetOwnerKeyValue(kFALSE,kFALSE);
245     fIds->SetOwner(kTRUE);
246     idsList->SetOwner(kFALSE);
247     AliInfo(Form("%d new (entry,id) cached. Total number %d",nAdded,fEntryCache.GetEntries()));
248
249     f->Close();
250     delete f;
251
252     return kTRUE;
253 }
254
255 //_____________________________________________________________________________
256 void AliCDBManager::Destroy() {
257 // delete ALCDBManager instance and active storages
258
259         if (fgInstance) {
260                 //fgInstance->Delete();
261                 delete fgInstance;
262                 fgInstance = 0x0;
263         }
264 }
265
266 //_____________________________________________________________________________
267 AliCDBManager::AliCDBManager():
268   TObject(),
269   fFactories(),
270   fActiveStorages(),
271   fSpecificStorages(),
272   fEntryCache(),
273   fIds(0),
274   fStorageMap(0),
275   fShortLived(0),
276   fDefaultStorage(NULL),
277   fDrainStorage(NULL),
278   fCondParam(0),
279   fRefParam(0),
280   fRun(-1),
281   fCache(kTRUE),
282   fLock(kFALSE),
283   fRaw(kFALSE),
284   fStartRunLHCPeriod(-1),
285   fEndRunLHCPeriod(-1),
286   fLHCPeriod(""),
287   fKey(0)
288 {
289 // default constuctor
290         fFactories.SetOwner(1);
291         fActiveStorages.SetOwner(1);
292         fSpecificStorages.SetOwner(1);
293         fEntryCache.SetName("CDBEntryCache");
294         fEntryCache.SetOwnerKeyValue(kTRUE,kTRUE);
295
296         fStorageMap = new TMap();
297         fStorageMap->SetOwner(1);
298         fIds = new TList();
299         fIds->SetOwner(1);
300 }
301
302 //_____________________________________________________________________________
303 AliCDBManager::~AliCDBManager() {
304 // destructor
305         ClearCache();
306         DestroyActiveStorages();
307         fFactories.Delete();
308         fDrainStorage = 0x0;
309         fDefaultStorage = 0x0;
310         delete fStorageMap; fStorageMap = 0;
311         delete fIds; fIds = 0;
312         delete fCondParam;
313         delete fRefParam;
314         delete fShortLived; fShortLived = 0x0;
315 }
316
317 //_____________________________________________________________________________
318 void AliCDBManager::PutActiveStorage(AliCDBParam* param, AliCDBStorage* storage){
319 // put a storage object into the list of active storages
320
321         fActiveStorages.Add(param, storage);
322         AliDebug(1, Form("Active storages: %d", fActiveStorages.GetEntries()));
323 }
324
325 //_____________________________________________________________________________
326 void AliCDBManager::RegisterFactory(AliCDBStorageFactory* factory) {
327 // add a storage factory to the list of registerd factories
328  
329         if (!fFactories.Contains(factory)) {
330                 fFactories.Add(factory);
331         }
332 }
333
334 //_____________________________________________________________________________
335 Bool_t AliCDBManager::HasStorage(const char* dbString) const {
336 // check if dbString is a URI valid for one of the registered factories
337
338         TIter iter(&fFactories);
339
340         AliCDBStorageFactory* factory=0;
341         while ((factory = (AliCDBStorageFactory*) iter.Next())) {
342
343                 if (factory->Validate(dbString)) {
344                         return kTRUE;
345                 }
346         }
347
348         return kFALSE;
349 }
350
351 //_____________________________________________________________________________
352 AliCDBParam* AliCDBManager::CreateParameter(const char* dbString) const {
353 // create AliCDBParam object from URI string
354
355         TIter iter(&fFactories);
356
357         AliCDBStorageFactory* factory=0;
358         while ((factory = (AliCDBStorageFactory*) iter.Next())) {
359                 AliCDBParam* param = factory->CreateParameter(dbString);
360                 if(param) return param;
361         }
362
363         return NULL;
364 }
365
366 //_____________________________________________________________________________
367 AliCDBStorage* AliCDBManager::GetStorage(const char* dbString) {
368 // get storage object from URI string
369                 
370         AliCDBParam* param = CreateParameter(dbString);
371         if (!param) {
372                 AliError(Form("Failed to activate requested storage! Check URI: %s", dbString));
373                 return NULL;
374         }
375
376         AliCDBStorage* aStorage = GetStorage(param);
377
378         delete param;
379         return aStorage;
380 }
381
382 //_____________________________________________________________________________
383 AliCDBStorage* AliCDBManager::GetStorage(const AliCDBParam* param) {
384 // get storage object from AliCDBParam object
385         
386         // if the list of active storages already contains
387         // the requested storage, return it
388         AliCDBStorage* aStorage = GetActiveStorage(param);
389         if (aStorage) {
390                 return aStorage;
391         }
392
393         // if lock is ON, cannot activate more storages!
394         if(fLock) {
395                 if (fDefaultStorage) {
396                         AliFatal("Lock is ON, and default storage is already set: "
397                                 "cannot reset it or activate more storages!");
398                 }
399         }       
400         
401         TIter iter(&fFactories);
402
403         AliCDBStorageFactory* factory=0;
404
405         // loop on the list of registered factories
406         while ((factory = (AliCDBStorageFactory*) iter.Next())) {
407
408                 // each factory tries to create its storage from the parameter
409                 aStorage = factory->Create(param);
410                 if (aStorage) {
411                         PutActiveStorage(param->CloneParam(), aStorage);
412                         aStorage->SetURI(param->GetURI());
413                         if(fRun >= 0) {
414                                 if(aStorage->GetType() == "alien"){
415                                         aStorage->QueryCDB(fRun);
416                                 } else {
417                                         AliDebug(2,
418                                                 "Skipping query for valid files, it is used only in grid...");
419                                 }
420                         }
421                         return aStorage;
422                 }
423         }
424
425         AliError(Form("Failed to activate requested storage! Check URI: %s", param->GetURI().Data()));
426
427         return NULL;
428 }
429
430 //_____________________________________________________________________________
431 AliCDBStorage* AliCDBManager::GetActiveStorage(const AliCDBParam* param) {
432 // get a storage object from the list of active storages
433
434         return dynamic_cast<AliCDBStorage*> (fActiveStorages.GetValue(param));
435 }
436
437 //_____________________________________________________________________________
438 TList* AliCDBManager::GetActiveStorages() {
439 // return list of active storages
440 // user has responsibility to delete returned object
441
442         TList* result = new TList();
443
444         TIter iter(fActiveStorages.GetTable());
445         TPair* aPair=0;
446         while ((aPair = (TPair*) iter.Next())) {
447                 result->Add(aPair->Value());
448         }
449
450         return result;
451 }
452
453 //_____________________________________________________________________________
454 void AliCDBManager::SetDrain(const char* dbString) {
455 // set drain storage from URI string
456
457         fDrainStorage = GetStorage(dbString);   
458 }
459
460 //_____________________________________________________________________________
461 void AliCDBManager::SetDrain(const AliCDBParam* param) {
462 // set drain storage from AliCDBParam
463
464         fDrainStorage = GetStorage(param);
465 }
466
467 //_____________________________________________________________________________
468 void AliCDBManager::SetDrain(AliCDBStorage* storage) {
469 // set drain storage from another active storage
470         
471         fDrainStorage = storage;
472 }
473
474 //_____________________________________________________________________________
475 Bool_t AliCDBManager::Drain(AliCDBEntry *entry) {
476 // drain retrieved object to drain storage
477
478         AliDebug(2, "Draining into drain storage...");
479         return fDrainStorage->Put(entry);
480 }
481
482 //____________________________________________________________________________
483 void AliCDBManager::SetDefaultStorage(const char* dbString) {
484 // sets default storage from URI string
485         
486         // checking whether we are in the raw case
487         TString dbStringTemp(dbString);
488         if (dbStringTemp == "raw://")
489         {
490                 fRaw = kTRUE;
491                 AliInfo("Setting the run-number will set the corresponding OCDB for raw data reconstruction.");
492                 AliInfo("Connecting to the grid...");
493                 if(!gGrid) {
494                         TGrid::Connect("alien://","");
495                         if(!gGrid) {
496                                 AliError("Connection to alien failed!");
497                                 return;
498                         }
499                 }
500                 return;
501         }
502
503         AliCDBStorage* bckStorage = fDefaultStorage;
504         
505         fDefaultStorage = GetStorage(dbString);
506         
507         if(!fDefaultStorage) return;
508         
509         if(bckStorage && (fDefaultStorage != bckStorage)){
510                 AliWarning("Existing default storage replaced: clearing cache!");
511                 ClearCache();
512         }
513         
514         if (fStorageMap->Contains("default")) {
515                 delete fStorageMap->Remove(((TPair*)fStorageMap->FindObject("default"))->Key());
516         }
517         fStorageMap->Add(new TObjString("default"), new TObjString(fDefaultStorage->GetURI()));
518 }
519 //_____________________________________________________________________________
520 void AliCDBManager::SetDefaultStorage(const AliCDBParam* param) {
521 // set default storage from AliCDBParam object
522         
523         AliCDBStorage* bckStorage = fDefaultStorage;
524
525         fDefaultStorage = GetStorage(param);
526
527         if(!fDefaultStorage) return;
528
529         if(bckStorage && (fDefaultStorage != bckStorage)){
530                 AliWarning("Existing default storage replaced: clearing cache!");
531                 ClearCache();
532         }
533
534         if (fStorageMap->Contains("default")) {
535                 delete fStorageMap->Remove(((TPair*)fStorageMap->FindObject("default"))->Key());
536         }
537         fStorageMap->Add(new TObjString("default"), new TObjString(fDefaultStorage->GetURI()));
538 }
539
540 //_____________________________________________________________________________
541 void AliCDBManager::SetDefaultStorage(AliCDBStorage* storage) {
542 // set default storage from another active storage
543         
544         // if lock is ON, cannot activate more storages!
545         if(fLock) {
546                 if (fDefaultStorage) {
547                         AliFatal("Lock is ON, and default storage is already set: "
548                                 "cannot reset it or activate more storages!");
549                 }
550         }       
551         
552         if (!storage) {
553                 UnsetDefaultStorage();
554                 return;
555         }
556         
557         AliCDBStorage* bckStorage = fDefaultStorage;
558
559         fDefaultStorage = storage;
560
561         if(bckStorage && (fDefaultStorage != bckStorage)){
562                 AliWarning("Existing default storage replaced: clearing cache!");
563                 ClearCache();
564         }
565
566         if (fStorageMap->Contains("default")) {
567                 delete fStorageMap->Remove(((TPair*)fStorageMap->FindObject("default"))->Key());
568         }
569         fStorageMap->Add(new TObjString("default"), new TObjString(fDefaultStorage->GetURI()));
570 }
571
572 //_____________________________________________________________________________
573 void AliCDBManager::SetDefaultStorage(const char* mcString, const char* simType) {
574 // sets default storage for MC data
575 // mcString MUST be "MC", 
576 // simType can be "Ideal","Residual","Full"
577         
578         TString strmcString(mcString);
579         TString strsimType(simType);
580         TString dbString; 
581         if (strmcString != "MC"){
582                 AliFatal("Method requires first string to be MC!");
583         }
584         else {
585                 if (strsimType == "Ideal"){
586                         dbString = fgkMCIdealStorage;
587                 }
588                 else if (strsimType == "Full"){
589                         dbString = fgkMCFullStorage;
590                 }
591                 else if (strsimType == "Residual"){
592                         dbString = fgkMCResidualStorage;
593                 }
594                 else {
595                         AliFatal("Error in setting the storage for MC data, second argument MUST be either \"Ideal\" or \"Full\" or \"Residual\".");
596                 }
597
598                 SetDefaultStorage(dbString.Data());
599                 if(!fDefaultStorage) AliFatal(Form("%s storage not there! Please check!",fLHCPeriod.Data()));
600         }
601 }
602 //_____________________________________________________________________________
603 void AliCDBManager::SetDefaultStorageFromRun(Int_t run) {
604 // set default storage from the run number - to be used only with raw data      
605
606         // if lock is ON, cannot activate more storages!
607         if(fLock) {
608                 if (fDefaultStorage) {
609                         AliFatal("Lock is ON, and default storage is already set: "
610                                 "cannot activate default storage from run number");
611                 }
612         }       
613
614         // retrieve XML file from alien
615         if(!gGrid) {
616             TGrid::Connect("alien://","");
617             if(!gGrid) {
618                 AliError("Connection to alien failed!");
619                 return;
620             }
621         }
622         TUUID uuid;
623         TString rndname = "/tmp/";
624         rndname += "OCDBFolderXML.";
625         rndname += uuid.AsString();
626         rndname += ".xml";
627         AliDebug(2, Form("file to be copied = %s", fgkOCDBFolderXMLfile.Data()));
628         if (!TFile::Cp(fgkOCDBFolderXMLfile.Data(), rndname.Data())) {
629                 AliFatal(Form("Cannot make a local copy of OCDBFolder xml file in %s",rndname.Data()));
630         }
631         AliCDBHandler* saxcdb = new AliCDBHandler();
632         saxcdb->SetRun(run);
633         TSAXParser *saxParser = new TSAXParser();
634         saxParser->ConnectToHandler("AliCDBHandler", saxcdb);  
635         saxParser->ParseFile(rndname.Data()); 
636         AliInfo(Form(" LHC folder = %s", saxcdb->GetOCDBFolder().Data()));
637         AliInfo(Form(" LHC period start run = %d", saxcdb->GetStartRunRange()));
638         AliInfo(Form(" LHC period end run = %d", saxcdb->GetEndRunRange()));
639         fLHCPeriod = saxcdb->GetOCDBFolder();
640         fStartRunLHCPeriod = saxcdb->GetStartRunRange();
641         fEndRunLHCPeriod = saxcdb->GetEndRunRange();
642
643         SetDefaultStorage(fLHCPeriod.Data());
644         if(!fDefaultStorage) AliFatal(Form("%s storage not there! Please check!",fLHCPeriod.Data()));
645
646 }
647
648 //_____________________________________________________________________________
649 void AliCDBManager::UnsetDefaultStorage() {
650 // Unset default storage
651         
652         // if lock is ON, action is forbidden!
653         if(fLock) {
654                 if (fDefaultStorage) {
655                         AliFatal("Lock is ON: cannot unset default storage!");
656                 }
657         }
658         
659         if (fDefaultStorage) {
660                 AliWarning("Clearing cache!");
661                 ClearCache();
662         }
663
664         fRun = fStartRunLHCPeriod = fEndRunLHCPeriod = -1;
665         fRaw = kFALSE;
666         
667         fDefaultStorage = 0x0;
668 }
669
670 //_____________________________________________________________________________
671 void AliCDBManager::SetSpecificStorage(const char* calibType, const char* dbString) {
672 // sets storage specific for detector or calibration type (works with AliCDBManager::Get(...))
673
674         AliCDBParam *aPar = CreateParameter(dbString);
675         if(!aPar) return;
676         SetSpecificStorage(calibType, aPar);
677         delete aPar;
678 }
679
680 //_____________________________________________________________________________
681 void AliCDBManager::SetSpecificStorage(const char* calibType, const AliCDBParam* param) {
682 // sets storage specific for detector or calibration type (works with AliCDBManager::Get(...))
683 // Default storage should be defined prior to any specific storages, e.g.:
684 // AliCDBManager::instance()->SetDefaultStorage("alien://");
685 // AliCDBManager::instance()->SetSpecificStorage("TPC/*","local://DB_TPC");
686 // AliCDBManager::instance()->SetSpecificStorage("*/Align/*","local://DB_TPCAlign");
687 // calibType must be a valid CDB path! (3 level folder structure)
688
689
690         if(!fDefaultStorage && !fRaw) {
691                 AliError("Please activate a default storage first!");
692                 return;
693         }
694
695
696         AliCDBPath aPath(calibType);
697         if(!aPath.IsValid()){
698                 AliError(Form("Not a valid path: %s", calibType));
699                 return;
700         }
701
702         TObjString *objCalibType = new TObjString(aPath.GetPath());
703         if(fSpecificStorages.Contains(objCalibType)){
704                 AliWarning(Form("Storage \"%s\" already activated! It will be replaced by the new one",
705                                         calibType));
706                 AliCDBParam *checkPar = dynamic_cast<AliCDBParam*> (fSpecificStorages.GetValue(calibType));
707                 if(checkPar) delete checkPar;
708                 delete fSpecificStorages.Remove(objCalibType);
709         }
710         AliCDBStorage *aStorage = GetStorage(param);
711         if(!aStorage) return;
712
713         fSpecificStorages.Add(objCalibType, param->CloneParam());
714
715         if(fStorageMap->Contains(objCalibType)){
716                 delete fStorageMap->Remove(objCalibType);
717         }
718         fStorageMap->Add(objCalibType->Clone(), new TObjString(param->GetURI()));
719
720 }
721
722 //_____________________________________________________________________________
723 AliCDBStorage* AliCDBManager::GetSpecificStorage(const char* calibType) {
724 // get storage specific for detector or calibration type 
725
726         AliCDBPath calibPath(calibType);
727         if(!calibPath.IsValid()) return NULL;
728
729         AliCDBParam *checkPar = (AliCDBParam*) fSpecificStorages.GetValue(calibPath.GetPath());
730         if(!checkPar){
731                 AliError(Form("%s storage not found!", calibType));
732                 return NULL;
733         } else {
734                 return GetStorage(checkPar);
735         }
736
737 }
738
739 //_____________________________________________________________________________
740 AliCDBParam* AliCDBManager::SelectSpecificStorage(const TString& path) {
741 // select storage valid for path from the list of specific storages
742
743         AliCDBPath aPath(path);
744         if(!aPath.IsValid()) return NULL;
745
746         TIter iter(&fSpecificStorages);
747         TObjString *aCalibType=0;
748         AliCDBPath tmpPath("null/null/null");
749         AliCDBParam* aPar=0;
750         while((aCalibType = (TObjString*) iter.Next())){
751                 AliCDBPath calibTypePath(aCalibType->GetName());
752                 if(calibTypePath.Comprises(aPath)) {
753                         if(calibTypePath.Comprises(tmpPath)) continue;
754                         aPar = (AliCDBParam*) fSpecificStorages.GetValue(aCalibType);
755                         tmpPath.SetPath(calibTypePath.GetPath());
756                 }
757         }
758         return aPar;
759 }
760
761 //_____________________________________________________________________________
762 AliCDBEntry* AliCDBManager::Get(const AliCDBPath& path, Int_t runNumber,
763         Int_t version, Int_t subVersion) {
764 // get an AliCDBEntry object from the database
765
766         if(runNumber < 0){
767                 // RunNumber is not specified. Try with fRun
768                 if (fRun < 0){
769                         AliError("Run number neither specified in query nor set in AliCDBManager! Use AliCDBManager::SetRun.");
770                         return NULL;
771                 }
772                 runNumber = fRun;
773         }
774
775         return Get(AliCDBId(path, runNumber, runNumber, version, subVersion));
776 }
777
778 //_____________________________________________________________________________
779 AliCDBEntry* AliCDBManager::Get(const AliCDBPath& path,
780         const AliCDBRunRange& runRange, Int_t version,
781         Int_t subVersion) {
782 // get an AliCDBEntry object from the database!
783
784         return Get(AliCDBId(path, runRange, version, subVersion));
785 }
786
787 //_____________________________________________________________________________
788 AliCDBEntry* AliCDBManager::Get(const AliCDBId& query) {
789 // get an AliCDBEntry object from the database
790         
791         // check if query's path and runRange are valid
792         // query is invalid also if version is not specified and subversion is!
793         if (!query.IsValid()) {
794                 AliError(Form("Invalid query: %s", query.ToString().Data()));
795                 return NULL;
796         }
797         
798         // query is not specified if path contains wildcard or run range= [-1,-1]
799         if (!query.IsSpecified()) {
800                 AliError(Form("Unspecified query: %s",
801                                 query.ToString().Data()));
802                 return NULL;
803         }
804
805         if(fLock && !(fRun >= query.GetFirstRun() && fRun <= query.GetLastRun())) 
806                 AliFatal("Lock is ON: cannot use different run number than the internal one!");
807         
808         if(fCache && !(fRun >= query.GetFirstRun() && fRun <= query.GetLastRun())) 
809                 AliWarning("Run number explicitly set in query: CDB cache temporarily disabled!");
810
811         AliCDBEntry *entry=0;
812
813         // first look into map of cached objects
814         if(fCache && query.GetFirstRun() == fRun)
815                 entry = (AliCDBEntry*) fEntryCache.GetValue(query.GetPath());
816
817         if(entry) {
818                 AliDebug(2, Form("Object %s retrieved from cache !!",query.GetPath().Data()));
819                 return entry;
820         }
821
822         if(!fDefaultStorage) {
823                 AliError("No storage set!");
824                 return NULL;
825         }
826         // Entry is not in cache -> retrieve it from CDB and cache it!!
827         AliCDBStorage *aStorage=0;
828         AliCDBParam *aPar=SelectSpecificStorage(query.GetPath());
829 //      Bool_t usedDefStorage=kTRUE;
830
831         if(aPar) {
832                 aStorage=GetStorage(aPar);
833                 TString str = aPar->GetURI();
834                 AliDebug(2,Form("Looking into storage: %s",str.Data()));
835 //              usedDefStorage=kFALSE;
836
837         } else {
838                 aStorage=GetDefaultStorage();
839                 AliDebug(2,"Looking into default storage");
840         }
841
842         entry = aStorage->Get(query);
843
844         if(entry && fCache && (query.GetFirstRun() == fRun)){
845                 CacheEntry(query.GetPath(), entry);
846         }
847
848         if(entry && !fIds->Contains(&entry->GetId())){
849                 fIds->Add(entry->GetId().Clone());
850         }
851
852
853         return entry;
854
855 }
856
857 //_____________________________________________________________________________
858 const char* AliCDBManager::GetURI(const char* path) {
859 // return the URI of the storage where to look for path
860
861         if(!IsDefaultStorageSet()) return 0;
862         
863         AliCDBParam *aPar=SelectSpecificStorage(path);
864
865         if(aPar) {
866                 return aPar->GetURI().Data();
867
868         } else {
869                 return GetDefaultStorage()->GetURI().Data();
870         }
871         
872         return 0;
873 }
874
875 //_____________________________________________________________________________
876 AliCDBId* AliCDBManager::GetId(const AliCDBPath& path, Int_t runNumber,
877         Int_t version, Int_t subVersion) {
878 // get the AliCDBId of the valid object from the database (does not retrieve the object)
879 // User must delete returned object!
880
881         if(runNumber < 0){
882                 // RunNumber is not specified. Try with fRun
883                 if (fRun < 0){
884                         AliError("Run number neither specified in query nor set in AliCDBManager! Use AliCDBManager::SetRun.");
885                         return NULL;
886                 }
887                 runNumber = fRun;
888         }
889
890         return GetId(AliCDBId(path, runNumber, runNumber, version, subVersion));
891 }
892
893 //_____________________________________________________________________________
894 AliCDBId* AliCDBManager::GetId(const AliCDBPath& path,
895         const AliCDBRunRange& runRange, Int_t version,
896         Int_t subVersion) {
897 // get the AliCDBId of the valid object from the database (does not retrieve the object)
898 // User must delete returned object!
899
900         return GetId(AliCDBId(path, runRange, version, subVersion));
901 }
902
903 //_____________________________________________________________________________
904 AliCDBId* AliCDBManager::GetId(const AliCDBId& query) {
905 // get the AliCDBId of the valid object from the database (does not retrieve the object)
906 // User must delete returned object!
907
908         if(!fDefaultStorage) {
909                 AliError("No storage set!");
910                 return NULL;
911         }
912
913         // check if query's path and runRange are valid
914         // query is invalid also if version is not specified and subversion is!
915         if (!query.IsValid()) {
916                 AliError(Form("Invalid query: %s", query.ToString().Data()));
917                 return NULL;
918         }
919         
920         // query is not specified if path contains wildcard or run range= [-1,-1]
921         if (!query.IsSpecified()) {
922                 AliError(Form("Unspecified query: %s",
923                                 query.ToString().Data()));
924                 return NULL;
925         }
926
927         if(fCache && query.GetFirstRun() != fRun)
928                 AliWarning("Run number explicitly set in query: CDB cache temporarily disabled!");
929
930         AliCDBEntry* entry = 0;
931
932         // first look into map of cached objects
933         if(fCache && query.GetFirstRun() == fRun)
934                 entry = (AliCDBEntry*) fEntryCache.GetValue(query.GetPath());
935
936         if(entry) {
937                 AliDebug(2, Form("Object %s retrieved from cache !!",query.GetPath().Data()));
938                 return dynamic_cast<AliCDBId*> (entry->GetId().Clone());
939         }
940
941         // Entry is not in cache -> retrieve it from CDB and cache it!!
942         AliCDBStorage *aStorage=0;
943         AliCDBParam *aPar=SelectSpecificStorage(query.GetPath());
944
945         if(aPar) {
946                 aStorage=GetStorage(aPar);
947                 TString str = aPar->GetURI();
948                 AliDebug(2,Form("Looking into storage: %s",str.Data()));
949                 
950         } else {
951                 aStorage=GetDefaultStorage();
952                 AliDebug(2,"Looking into default storage");
953         }
954
955         return aStorage->GetId(query);
956
957 }
958
959 //_____________________________________________________________________________
960 TList* AliCDBManager::GetAll(const AliCDBPath& path, Int_t runNumber,
961         Int_t version, Int_t subVersion) {
962 // get multiple AliCDBEntry objects from the database
963
964         if(runNumber < 0){
965                 // RunNumber is not specified. Try with fRun
966                 if (fRun < 0){
967                         AliError("Run number neither specified in query nor set in AliCDBManager! Use AliCDBManager::SetRun.");
968                         return NULL;
969                 }
970                 runNumber = fRun;
971         }
972
973         return GetAll(AliCDBId(path, runNumber, runNumber, version,     
974                         subVersion));
975 }
976
977 //_____________________________________________________________________________
978 TList* AliCDBManager::GetAll(const AliCDBPath& path,
979         const AliCDBRunRange& runRange, Int_t version, Int_t subVersion) {
980 // get multiple AliCDBEntry objects from the database
981
982         return GetAll(AliCDBId(path, runRange, version, subVersion));
983 }
984
985 //_____________________________________________________________________________
986 TList* AliCDBManager::GetAll(const AliCDBId& query) {
987 // get multiple AliCDBEntry objects from the database
988 // Warning: this method works correctly only for queries of the type "Detector/*"
989 //              and not for more specific queries e.g. "Detector/Calib/*" !
990 // Warning #2: Entries are cached, but GetAll will keep on retrieving objects from OCDB!
991 //              To get an object from cache use Get() function
992
993         if(!fDefaultStorage) {
994                 AliError("No storage set!");
995                 return NULL;
996         }
997
998         if (!query.IsValid()) {
999                 AliError(Form("Invalid query: %s", query.ToString().Data()));
1000                 return NULL;
1001         }
1002
1003         if((fSpecificStorages.GetEntries()>0) && query.GetPath().BeginsWith('*')){
1004                 // if specific storages are active a query with "*" is ambiguous
1005                 AliError("Query too generic in this context!");
1006                 return NULL;
1007         }
1008
1009         if (query.IsAnyRange()) {
1010                 AliError(Form("Unspecified run or runrange: %s",
1011                                 query.ToString().Data()));
1012                 return NULL;
1013         }
1014
1015         if(fLock && query.GetFirstRun() != fRun)
1016                 AliFatal("Lock is ON: cannot use different run number than the internal one!");
1017         
1018         AliCDBParam *aPar=SelectSpecificStorage(query.GetPath());
1019
1020         AliCDBStorage *aStorage;
1021         if(aPar) {
1022                 aStorage=GetStorage(aPar);
1023                 AliDebug(2,Form("Looking into storage: %s", aPar->GetURI().Data()));
1024
1025         } else {
1026                 aStorage=GetDefaultStorage();
1027                 AliDebug(2,"Looking into default storage");
1028         }
1029
1030         TList *result = 0;
1031         if(aStorage) result = aStorage->GetAll(query);
1032         if(!result) return 0;
1033
1034        // loop on result to check whether entries should be re-queried with specific storages
1035         if(fSpecificStorages.GetEntries()>0 && ! (fSpecificStorages.GetEntries() == 1 && aPar)) {
1036                 AliInfo("Now look into all other specific storages...");
1037
1038                 TIter iter(result);
1039                 AliCDBEntry* chkEntry=0;
1040
1041                 while((chkEntry = dynamic_cast<AliCDBEntry*> (iter.Next()))){
1042                         AliCDBId& chkId = chkEntry->GetId();
1043                         AliDebug(2, Form("Checking id %s ", chkId.GetPath().Data()));
1044                         AliCDBParam *chkPar=SelectSpecificStorage(chkId.GetPath());
1045                         if (!chkPar || aPar == chkPar) continue;
1046                         AliCDBStorage *chkStorage = GetStorage(chkPar);
1047                         AliDebug(2, Form("Found specific storage! %s", chkPar->GetURI().Data()));
1048
1049                         AliCDBEntry *newEntry=0;
1050                         chkId.SetRunRange(query.GetFirstRun(), query.GetLastRun());
1051                         chkId.SetVersion(query.GetVersion());
1052                         chkId.SetSubVersion(query.GetSubVersion());
1053
1054                         if(chkStorage) newEntry = chkStorage->Get(chkId);
1055                         if(!newEntry) continue;
1056
1057                         // object is found in specific storage: replace entry in the result list!
1058                         chkEntry->SetOwner(1);
1059                         delete result->Remove(chkEntry);
1060                         result->AddFirst(newEntry);
1061                 }
1062
1063                 Int_t nEntries = result->GetEntries();
1064                 AliInfo("After look into other specific storages, result list is:");
1065                 for(int i=0; i<nEntries;i++){
1066                         AliCDBEntry *entry = (AliCDBEntry*) result->At(i);
1067                         AliInfo(Form("%s",entry->GetId().ToString().Data()));
1068                 }
1069         }
1070
1071         // caching entries
1072         TIter iter(result);
1073         AliCDBEntry* entry=0;
1074         while((entry = dynamic_cast<AliCDBEntry*> (iter.Next()))){
1075
1076                 if(!fIds->Contains(&entry->GetId())){
1077                         fIds->Add(entry->GetId().Clone());
1078                 }
1079                 if(fCache && (query.GetFirstRun() == fRun)){
1080                         CacheEntry(entry->GetId().GetPath(), entry);
1081                 }
1082         }
1083
1084
1085         return result;
1086 }
1087
1088 //_____________________________________________________________________________
1089 Bool_t AliCDBManager::Put(TObject* object, const AliCDBId& id, AliCDBMetaData* metaData, const DataType type){
1090 // store an AliCDBEntry object into the database
1091
1092         if (object==0x0) {
1093                 AliError("Null Entry! No storage will be done!");
1094                 return kFALSE;
1095         } 
1096
1097         AliCDBEntry anEntry(object, id, metaData);
1098         return Put(&anEntry, type);
1099
1100 }
1101
1102
1103 //_____________________________________________________________________________
1104 Bool_t AliCDBManager::Put(AliCDBEntry* entry, DataType type){
1105 // store an AliCDBEntry object into the database
1106
1107         if(type == kPrivate && !fDefaultStorage) {
1108                 AliError("No storage set!");
1109                 return kFALSE;
1110         }
1111
1112         if (!entry){
1113                 AliError("No entry!");
1114                 return kFALSE;
1115         }
1116
1117         if (entry->GetObject()==0x0){
1118                 AliError("No valid object in CDB entry!");
1119                 return kFALSE;
1120         }
1121
1122         if (!entry->GetId().IsValid()) {
1123                 AliError(Form("Invalid entry ID: %s", 
1124                         entry->GetId().ToString().Data()));
1125                 return kFALSE;
1126         }       
1127
1128         if (!entry->GetId().IsSpecified()) {
1129                 AliError(Form("Unspecified entry ID: %s", 
1130                         entry->GetId().ToString().Data()));
1131                 return kFALSE;
1132         }
1133
1134         AliCDBId id = entry->GetId();
1135         AliCDBParam *aPar = SelectSpecificStorage(id.GetPath());
1136
1137         AliCDBStorage *aStorage=0;
1138         
1139         if(aPar) {
1140                 aStorage=GetStorage(aPar);
1141         } else {
1142                 switch(type){
1143                         case kCondition:
1144                                 aStorage = GetStorage(fCondParam);
1145                                 break;
1146                         case kReference:
1147                                 aStorage = GetStorage(fRefParam);
1148                                 break;
1149                         case kPrivate:
1150                                 aStorage = GetDefaultStorage();
1151                                 break;
1152                 }
1153         }
1154
1155         AliDebug(2,Form("Storing object into storage: %s", aStorage->GetURI().Data()));
1156
1157         Bool_t result = aStorage->Put(entry, type);
1158
1159         if(fRun >= 0) QueryCDB();
1160
1161         return result;
1162
1163
1164 }
1165
1166 //_____________________________________________________________________________
1167 void AliCDBManager::CacheEntry(const char* path, AliCDBEntry* entry)
1168 {
1169 // cache AliCDBEntry. Cache is valid until run number is changed.
1170
1171         AliCDBEntry *chkEntry = dynamic_cast<AliCDBEntry*> (fEntryCache.GetValue(path));
1172
1173         if(chkEntry) {
1174                 AliDebug(2, Form("Object %s already in cache !!", path));
1175                 return;
1176         } else {
1177                 AliDebug(2,Form("Caching entry %s", path));
1178         }
1179
1180         fEntryCache.Add(new TObjString(path), entry);
1181         AliDebug(2,Form("Cache entries: %d", fEntryCache.GetEntries()));
1182
1183 }
1184
1185 //_____________________________________________________________________________
1186 void AliCDBManager::Print(Option_t* /*option*/) const
1187 {
1188 // Print list of active storages and their URIs
1189
1190         TString output=Form("Run number = %d; ",fRun);
1191         output += "Cache is ";
1192         if(!fCache) output += "NOT ";
1193         output += Form("ACTIVE; Number of active storages: %d\n",fActiveStorages.GetEntries());
1194
1195         if(fDefaultStorage) {
1196                 output += Form("\t*** Default Storage URI: \"%s\"\n",fDefaultStorage->GetURI().Data());
1197 //              AliInfo(output.Data());
1198         }
1199         if(fSpecificStorages.GetEntries()>0) {
1200                 TIter iter(fSpecificStorages.GetTable());
1201                 TPair *aPair=0;
1202                 Int_t i=1;
1203                 while((aPair = (TPair*) iter.Next())){
1204                         output += Form("\t*** Specific storage %d: Path \"%s\" -> URI \"%s\"\n",
1205                                 i++, ((TObjString*) aPair->Key())->GetName(),
1206                                 ((AliCDBParam*) aPair->Value())->GetURI().Data());
1207                 }
1208         }
1209         if(fDrainStorage) {
1210                 output += Form("*** Drain Storage URI: %s\n",fDrainStorage->GetURI().Data());
1211         }
1212         AliInfo(output.Data());
1213 }
1214
1215 //_____________________________________________________________________________
1216 void AliCDBManager::SetRun(Int_t run)
1217 {
1218 // Sets current run number.
1219 // When the run number changes the caching is cleared.
1220         
1221         if(fRun == run)
1222                 return;
1223   
1224         if(fLock && fRun >= 0) {
1225                 AliFatal("Lock is ON, cannot reset run number!");
1226         }       
1227                 
1228         fRun = run;
1229         if(fRaw){
1230                 // here the LHCPeriod xml file is parsed; the string containing the correct period is returned; the default storage is set
1231                 if (fStartRunLHCPeriod <= run && fEndRunLHCPeriod >= run){
1232                         AliInfo("LHCPeriod alien folder for current run already in memory");
1233                 }else{
1234                         SetDefaultStorageFromRun(run);
1235                         if(fEntryCache.GetEntries()!=0) ClearCache();
1236                         return;
1237                 }
1238         }
1239         ClearCache();
1240         QueryCDB();
1241 }
1242
1243 //_____________________________________________________________________________
1244 void AliCDBManager::ClearCache(){
1245 // clear AliCDBEntry cache
1246
1247         AliDebug(2, Form("Cache entries to be deleted: %d",fEntryCache.GetEntries()));
1248         
1249         /*
1250         // To clean entries one by one
1251         TIter iter(fEntryCache.GetTable());
1252         TPair* pair=0;
1253         while((pair= dynamic_cast<TPair*> (iter.Next()))){
1254         
1255                 TObjString* key = dynamic_cast<TObjString*> (pair->Key());
1256                 AliCDBEntry* entry = dynamic_cast<AliCDBEntry*> (pair->Value());
1257                 AliDebug(2, Form("Deleting entry: %s", key->GetName()));
1258                 if (entry) delete entry;
1259                 delete fEntryCache.Remove(key);
1260         }
1261         */
1262         fEntryCache.DeleteAll();
1263         AliDebug(2, Form("After deleting - Cache entries: %d",fEntryCache.GetEntries()));
1264 }
1265
1266 //_____________________________________________________________________________
1267 void AliCDBManager::UnloadFromCache(const char* path){
1268 // unload cached object
1269 // that is remove the entry from the cache and the id from the list of ids
1270 //
1271         if(!fActiveStorages.GetEntries()) {
1272                 AliDebug(2, Form("No active storages. Object \"%s\" is not unloaded from cache", path));
1273                 return;
1274         }
1275
1276         AliCDBPath queryPath(path);
1277         if(!queryPath.IsValid()) return;
1278
1279         if(!queryPath.IsWildcard()) { // path is not wildcard, get it directly from the cache and unload it!
1280                 if(fEntryCache.Contains(path)){
1281                         AliDebug(2, Form("Unloading object \"%s\" from cache and from list of ids", path));
1282                         TObjString pathStr(path);
1283                         delete fEntryCache.Remove(&pathStr);
1284                         TIter iter(fIds);
1285                         AliCDBId *id = 0;
1286                         while((id = dynamic_cast<AliCDBId*> (iter.Next()))){
1287                             if(queryPath.Comprises(id->GetPath()))
1288                                 delete fIds->Remove(id);
1289                         }
1290                 } else {
1291                   AliError(Form("Cache does not contain object \"%s\"!", path));
1292                 }
1293                 AliDebug(2, Form("Cache entries: %d",fEntryCache.GetEntries()));
1294                 return;
1295         }
1296
1297         // path is wildcard: loop on the cache and unload all comprised objects!
1298         TIter iter(fEntryCache.GetTable());
1299         TPair* pair = 0;
1300         Int_t removed=0;
1301
1302         while((pair = dynamic_cast<TPair*> (iter.Next()))){
1303                 AliCDBPath entryPath = pair->Key()->GetName();
1304                 if(queryPath.Comprises(entryPath)) {
1305                         AliDebug(2, Form("Unloading object \"%s\" from cache and from list of ids", entryPath.GetPath().Data()));
1306                         TObjString pathStr(entryPath.GetPath());
1307                         delete fEntryCache.Remove(&pathStr);
1308
1309                         TIter iterids(fIds);
1310                         AliCDBId *anId = 0;
1311                         while((anId = dynamic_cast<AliCDBId*> (iterids.Next()))){
1312                             AliCDBPath aPath = anId->GetPath();
1313                             TString aPathStr = aPath.GetPath();
1314                             if(queryPath.Comprises(aPath)) {
1315                                 delete fIds->Remove(anId);
1316                                 removed++;
1317                             }
1318                         }
1319                 }
1320         }
1321         AliDebug(2,Form("Cache entries and ids removed: %d   Remaining: %d",removed,fEntryCache.GetEntries()));
1322 }
1323
1324 //_____________________________________________________________________________
1325 void AliCDBManager::DestroyActiveStorages() {
1326 // delete list of active storages
1327
1328         fActiveStorages.DeleteAll();
1329         fSpecificStorages.DeleteAll();
1330 }
1331
1332 //_____________________________________________________________________________
1333 void AliCDBManager::DestroyActiveStorage(AliCDBStorage* /*storage*/) {
1334 // destroys active storage
1335
1336 /*
1337         TIter iter(fActiveStorages.GetTable());
1338         TPair* aPair;
1339         while ((aPair = (TPair*) iter.Next())) {
1340                 if(storage == (AliCDBStorage*) aPair->Value())
1341                         delete fActiveStorages.Remove(aPair->Key());
1342                         storage->Delete(); storage=0x0;
1343         }
1344 */
1345
1346 }
1347
1348 //_____________________________________________________________________________
1349 void AliCDBManager::QueryCDB() {
1350 // query default and specific storages for files valid for fRun. Every storage loads the Ids into its list.
1351
1352         if (fRun < 0){
1353                 AliError("Run number not yet set! Use AliCDBManager::SetRun.");
1354         return;
1355         }
1356         if (!fDefaultStorage){
1357                 AliError("Default storage is not set! Use AliCDBManager::SetDefaultStorage");
1358         return;
1359         }
1360         if(fDefaultStorage->GetType() == "alien"){
1361                 fDefaultStorage->QueryCDB(fRun);
1362         } else {
1363                 AliDebug(2,"Skipping query for valid files, it used only in grid...");
1364         }
1365
1366         TIter iter(&fSpecificStorages);
1367         TObjString *aCalibType=0;
1368         AliCDBParam* aPar=0;
1369         while((aCalibType = dynamic_cast<TObjString*> (iter.Next()))){
1370                 aPar = (AliCDBParam*) fSpecificStorages.GetValue(aCalibType);
1371                 if(aPar) {
1372                         AliDebug(2,Form("Querying specific storage %s",aCalibType->GetName()));
1373                         AliCDBStorage *aStorage = GetStorage(aPar);
1374                         if(aStorage->GetType() == "alien"){
1375                                 aStorage->QueryCDB(fRun,aCalibType->GetName());
1376                         } else {
1377                                 AliDebug(2,
1378                                         "Skipping query for valid files, it is used only in grid...");
1379                         }
1380                 }
1381         }
1382 }
1383
1384 //______________________________________________________________________________________________
1385 const char* AliCDBManager::GetDataTypeName(DataType type)
1386 {
1387   // returns the name (string) of the data type
1388
1389       switch (type){
1390             case kCondition: return "Conditions";
1391             case kReference: return "Reference";
1392             case kPrivate: return "Private";
1393      }
1394      return 0;
1395
1396 }
1397
1398 //______________________________________________________________________________________________
1399 void AliCDBManager::InitShortLived()
1400 {
1401   // Init the list of short-lived objects
1402   // currently disabled
1403
1404         fShortLived=0x0;
1405
1406 //      fShortLived = new TList();
1407 //      fShortLived->SetOwner(1);
1408 //
1409 //      fShortLived->Add(new TObjString("EMCAL/Calib/Data"));
1410 // 
1411 //      fShortLived->Add(new TObjString("HMPID/Calib/Nmean"));
1412 //      fShortLived->Add(new TObjString("HMPID/Calib/Qthre"));
1413 // 
1414 //      fShortLived->Add(new TObjString("ITS/Calib/CalibSPD"));
1415 // 
1416 //      fShortLived->Add(new TObjString("MUON/Calib/Gains"));
1417 //      fShortLived->Add(new TObjString("MUON/Calib/HV"));
1418 //      fShortLived->Add(new TObjString("MUON/Calib/Pedestals"));
1419 // 
1420 //      fShortLived->Add(new TObjString("PHOS/Calib/CpvGainPedestals"));
1421 //      fShortLived->Add(new TObjString("PHOS/Calib/EmcGainPedestals"));
1422 // 
1423 //      fShortLived->Add(new TObjString("PMD/Calib/Data"));
1424 // 
1425 //      fShortLived->Add(new TObjString("TRD/Calib/ChamberGainFactor"));
1426 //      fShortLived->Add(new TObjString("TRD/Calib/LocalGainFactor"));
1427 //      fShortLived->Add(new TObjString("TRD/Calib/ChamberT0"));
1428 //      fShortLived->Add(new TObjString("TRD/Calib/LocalT0"));
1429 //      fShortLived->Add(new TObjString("TRD/Calib/ChamberVdrift"));
1430 //      fShortLived->Add(new TObjString("TRD/Calib/LocalVdrift"));
1431 // 
1432 //      fShortLived->Add(new TObjString("ZDC/Calib/Data"));
1433
1434 }
1435
1436 //______________________________________________________________________________________________
1437 Bool_t AliCDBManager::IsShortLived(const char* path)
1438 {
1439   // returns the name (string) of the data type
1440
1441         if(!fShortLived) return kFALSE;
1442
1443         AliCDBPath aPath(path);
1444         if(!aPath.IsValid()){
1445                 AliError(Form("Not a valid path: %s", path));
1446                 return kFALSE;
1447         }
1448
1449         return fShortLived->Contains(path);
1450
1451 }
1452
1453 //______________________________________________________________________________________________
1454 ULong_t AliCDBManager::SetLock(Bool_t lock, ULong_t key){
1455   // To lock/unlock user must provide the key. A new key is provided after
1456   // each successful lock. User should always backup the returned key and
1457   // use it on next access.
1458   if (fLock == lock) return 0;  // nothing to be done
1459   if (lock) {
1460     // User wants to lock - check his identity
1461     if (fKey) {
1462       // Lock has a user - check his key
1463       if (fKey != key) {
1464         AliFatal("Wrong key provided to lock CDB. Please remove CDB lock access from your code !");
1465         return 0;
1466       }  
1467     }  
1468     // Provide new key 
1469     fKey = gSystem->Now();
1470     fLock = kTRUE;
1471     return fKey;
1472   }
1473   // User wants to unlock - check the provided key
1474   if (key != fKey) {
1475     AliFatal("Lock is ON: wrong key provided");
1476     return 0;
1477   }  
1478   fLock = kFALSE;
1479   return key;  
1480 }
1481
1482 ///////////////////////////////////////////////////////////
1483 // AliCDBManager Parameter class                         //
1484 // interface to specific AliCDBParameter class           //
1485 // (AliCDBGridParam, AliCDBLocalParam, AliCDBDumpParam)  //
1486 ///////////////////////////////////////////////////////////
1487
1488 AliCDBParam::AliCDBParam():
1489   fType(),
1490   fURI()
1491 {
1492 // constructor
1493
1494 }
1495
1496 //_____________________________________________________________________________
1497 AliCDBParam::~AliCDBParam() {
1498 // destructor
1499
1500 }
1501