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