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