1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
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 //-------------------------------------------------------------------------
23 #include "AliCDBManager.h"
24 #include "AliCDBStorage.h"
26 #include "AliCDBDump.h"
27 #include "AliCDBLocal.h"
28 #include "AliCDBGrid.h"
29 #include "AliCDBEntry.h"
30 #include "AliCDBHandler.h"
32 #include <TObjString.h>
33 #include <TSAXParser.h>
41 ClassImp(AliCDBManager)
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;
52 //_____________________________________________________________________________
53 AliCDBManager* AliCDBManager::Instance(TMap *entryCache, Int_t run)
55 // returns AliCDBManager instance (singleton)
58 fgInstance = new AliCDBManager();
62 fgInstance->InitFromCache(entryCache,run);
68 //_____________________________________________________________________________
69 void AliCDBManager::Init() {
70 // factory registering
72 RegisterFactory(new AliCDBDumpFactory());
73 RegisterFactory(new AliCDBLocalFactory());
74 // AliCDBGridFactory is registered only if AliEn libraries are enabled in Root
75 if(!gSystem->Exec("root-config --has-alien |grep yes 2>&1 > /dev/null")){ // returns 0 if yes
76 AliInfo("AliEn classes enabled in Root. AliCDBGrid factory registered.");
77 RegisterFactory(new AliCDBGridFactory());
78 fCondParam = CreateParameter(fgkCondUri);
79 fRefParam = CreateParameter(fgkRefUri);
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
91 TIter iter(entryCache->GetTable());
94 while((pair = dynamic_cast<TPair*> (iter.Next()))){
95 fEntryCache.Add(pair->Key(),pair->Value());
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()));
103 //_____________________________________________________________________________
104 void AliCDBManager::DumpToSnapshotFile(const char* snapshotFileName, Bool_t singleKeys){
106 // dump the entries map and the ids list to
110 TFile *f = TFile::Open(snapshotFileName,"RECREATE");
111 if (!f || f->IsZombie()){
112 AliError(Form("Cannot open file %s",snapshotFileName));
116 Printf("\ndumping entriesMap (entries'cache) with %d entries!\n", fEntryCache.GetEntries());
117 Printf("\ndumping entriesList with %d entries!\n", fIds->GetEntries());
122 f->WriteObject(&fEntryCache,"CDBentriesMap");
123 f->WriteObject(fIds,"CDBidsList");
125 // We write the entries one by one named by their calibration path
127 fEntryCache.Write("CDBentriesMap");
128 fIds->Write("CDBidsList");
130 TIter iter(fEntryCache.GetTable());
132 while((pair = dynamic_cast<TPair*> (iter.Next()))){
133 TObjString *os = dynamic_cast<TObjString*>(pair->Key());
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());
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
154 // if the manager is locked it cannot initialize from a snapshot
156 AliError("Being locked I cannot initialize from the snapshot!");
161 TString snapshotFile(snapshotFileName);
162 if(snapshotFile.BeginsWith("alien://")){
164 TGrid::Connect("alien://","");
166 AliError("Connection to alien failed!");
172 TFile *f = TFile::Open(snapshotFileName);
173 if (!f || f->IsZombie()){
174 AliError(Form("Cannot open file %s",snapshotFileName));
178 // retrieve entries' map from snapshot file
179 TMap *entriesMap = 0;
180 TIter next(f->GetListOfKeys());
182 while ((key = (TKey*)next())) {
183 if (strcmp(key->GetClassName(),"TMap") != 0) continue;
184 entriesMap = (TMap*)key->ReadObj();
187 if (!entriesMap || entriesMap->GetEntries()==0){
188 AliError("Cannot get valid map of CDB entries from snapshot file");
192 // retrieve ids' list from snapshot file
194 TIter nextKey(f->GetListOfKeys());
196 while ((keyN = (TKey*)nextKey())) {
197 if (strcmp(keyN->GetClassName(),"TList") != 0) continue;
198 idsList = (TList*)keyN->ReadObj();
201 if (!idsList || idsList->GetEntries()==0){
202 AliError("Cannot get valid list of CDB entries from snapshot file");
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());
214 while((pair = dynamic_cast<TPair*> (iterObj.Next()))){
215 TObjString* os = (TObjString*) pair->Key();
216 TString path = os->GetString();
217 TIter iterId(idsList);
219 AliCDBId* correspondingId=0;
220 while((id = dynamic_cast<AliCDBId*> (iterId.Next()))){
221 TString idpath(id->GetPath());
227 if(!correspondingId){
228 AliError(Form("id for \"%s\" not found in the snapshot (while entry was). This entry is skipped!",path.Data()));
231 Bool_t cached = fEntryCache.Contains(path.Data());
232 Bool_t registeredId = kFALSE;
235 while((idT = dynamic_cast<AliCDBId*> (iter.Next()))){
236 if(idT->GetPath()==path){
237 registeredId = kTRUE;
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());
247 fEntryCache.Add(pair->Key(),pair->Value());
251 if(cached || registeredId){
252 AliWarning(Form("An entry was already cached for \"%s\". Not adding this object from snapshot",path.Data()));
254 fEntryCache.Add(pair->Key(),pair->Value());
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()));
274 //_____________________________________________________________________________
275 void AliCDBManager::Destroy() {
276 // delete ALCDBManager instance and active storages
279 //fgInstance->Delete();
285 //_____________________________________________________________________________
286 AliCDBManager::AliCDBManager():
295 fDefaultStorage(NULL),
302 fSnapshotMode(kFALSE),
304 // fSnapshotCache(0),
305 // fSnapshotIdsList(0),
307 fStartRunLHCPeriod(-1),
308 fEndRunLHCPeriod(-1),
312 // default constuctor
313 fFactories.SetOwner(1);
314 fActiveStorages.SetOwner(1);
315 fSpecificStorages.SetOwner(1);
316 fEntryCache.SetName("CDBEntryCache");
317 fEntryCache.SetOwnerKeyValue(kTRUE,kTRUE);
319 fStorageMap = new TMap();
320 fStorageMap->SetOwner(1);
325 //_____________________________________________________________________________
326 AliCDBManager::~AliCDBManager() {
329 DestroyActiveStorages();
332 fDefaultStorage = 0x0;
333 delete fStorageMap; fStorageMap = 0;
334 delete fIds; fIds = 0;
337 delete fShortLived; fShortLived = 0x0;
338 //fSnapshotCache = 0;
339 //fSnapshotIdsList = 0;
341 fSnapshotFile->Close();
346 //_____________________________________________________________________________
347 void AliCDBManager::PutActiveStorage(AliCDBParam* param, AliCDBStorage* storage){
348 // put a storage object into the list of active storages
350 fActiveStorages.Add(param, storage);
351 AliDebug(1, Form("Active storages: %d", fActiveStorages.GetEntries()));
354 //_____________________________________________________________________________
355 void AliCDBManager::RegisterFactory(AliCDBStorageFactory* factory) {
356 // add a storage factory to the list of registerd factories
358 if (!fFactories.Contains(factory)) {
359 fFactories.Add(factory);
363 //_____________________________________________________________________________
364 Bool_t AliCDBManager::HasStorage(const char* dbString) const {
365 // check if dbString is a URI valid for one of the registered factories
367 TIter iter(&fFactories);
369 AliCDBStorageFactory* factory=0;
370 while ((factory = (AliCDBStorageFactory*) iter.Next())) {
372 if (factory->Validate(dbString)) {
380 //_____________________________________________________________________________
381 AliCDBParam* AliCDBManager::CreateParameter(const char* dbString) const {
382 // create AliCDBParam object from URI string
384 TIter iter(&fFactories);
386 AliCDBStorageFactory* factory=0;
387 while ((factory = (AliCDBStorageFactory*) iter.Next())) {
388 AliCDBParam* param = factory->CreateParameter(dbString);
389 if(param) return param;
395 //_____________________________________________________________________________
396 AliCDBStorage* AliCDBManager::GetStorage(const char* dbString) {
397 // get storage object from URI string
399 AliCDBParam* param = CreateParameter(dbString);
401 AliError(Form("Failed to activate requested storage! Check URI: %s", dbString));
405 AliCDBStorage* aStorage = GetStorage(param);
411 //_____________________________________________________________________________
412 AliCDBStorage* AliCDBManager::GetStorage(const AliCDBParam* param) {
413 // get storage object from AliCDBParam object
415 // if the list of active storages already contains
416 // the requested storage, return it
417 AliCDBStorage* aStorage = GetActiveStorage(param);
422 // if lock is ON, cannot activate more storages!
424 if (fDefaultStorage) {
425 AliFatal("Lock is ON, and default storage is already set: "
426 "cannot reset it or activate more storages!");
430 TIter iter(&fFactories);
432 AliCDBStorageFactory* factory=0;
434 // loop on the list of registered factories
435 while ((factory = (AliCDBStorageFactory*) iter.Next())) {
437 // each factory tries to create its storage from the parameter
438 aStorage = factory->Create(param);
440 PutActiveStorage(param->CloneParam(), aStorage);
441 aStorage->SetURI(param->GetURI());
443 if(aStorage->GetType() == "alien"){
444 aStorage->QueryCDB(fRun);
447 "Skipping query for valid files, it is used only in grid...");
454 AliError(Form("Failed to activate requested storage! Check URI: %s", param->GetURI().Data()));
459 //_____________________________________________________________________________
460 AliCDBStorage* AliCDBManager::GetActiveStorage(const AliCDBParam* param) {
461 // get a storage object from the list of active storages
463 return dynamic_cast<AliCDBStorage*> (fActiveStorages.GetValue(param));
466 //_____________________________________________________________________________
467 TList* AliCDBManager::GetActiveStorages() {
468 // return list of active storages
469 // user has responsibility to delete returned object
471 TList* result = new TList();
473 TIter iter(fActiveStorages.GetTable());
475 while ((aPair = (TPair*) iter.Next())) {
476 result->Add(aPair->Value());
482 //_____________________________________________________________________________
483 void AliCDBManager::SetDrain(const char* dbString) {
484 // set drain storage from URI string
486 fDrainStorage = GetStorage(dbString);
489 //_____________________________________________________________________________
490 void AliCDBManager::SetDrain(const AliCDBParam* param) {
491 // set drain storage from AliCDBParam
493 fDrainStorage = GetStorage(param);
496 //_____________________________________________________________________________
497 void AliCDBManager::SetDrain(AliCDBStorage* storage) {
498 // set drain storage from another active storage
500 fDrainStorage = storage;
503 //_____________________________________________________________________________
504 Bool_t AliCDBManager::Drain(AliCDBEntry *entry) {
505 // drain retrieved object to drain storage
507 AliDebug(2, "Draining into drain storage...");
508 return fDrainStorage->Put(entry);
511 //____________________________________________________________________________
512 void AliCDBManager::SetDefaultStorage(const char* dbString) {
513 // sets default storage from URI string
515 // checking whether we are in the raw case
516 TString dbStringTemp(dbString);
517 if (dbStringTemp == "raw://")
520 AliInfo("Setting the run-number will set the corresponding OCDB for raw data reconstruction.");
521 AliInfo("Connecting to the grid...");
523 TGrid::Connect("alien://","");
525 AliError("Connection to alien failed!");
532 AliCDBStorage* bckStorage = fDefaultStorage;
534 fDefaultStorage = GetStorage(dbString);
536 if(!fDefaultStorage) return;
538 if(bckStorage && (fDefaultStorage != bckStorage)){
539 AliWarning("Existing default storage replaced: clearing cache!");
543 if (fStorageMap->Contains("default")) {
544 delete fStorageMap->Remove(((TPair*)fStorageMap->FindObject("default"))->Key());
546 fStorageMap->Add(new TObjString("default"), new TObjString(fDefaultStorage->GetURI()));
548 //_____________________________________________________________________________
549 void AliCDBManager::SetDefaultStorage(const AliCDBParam* param) {
550 // set default storage from AliCDBParam object
552 AliCDBStorage* bckStorage = fDefaultStorage;
554 fDefaultStorage = GetStorage(param);
556 if(!fDefaultStorage) return;
558 if(bckStorage && (fDefaultStorage != bckStorage)){
559 AliWarning("Existing default storage replaced: clearing cache!");
563 if (fStorageMap->Contains("default")) {
564 delete fStorageMap->Remove(((TPair*)fStorageMap->FindObject("default"))->Key());
566 fStorageMap->Add(new TObjString("default"), new TObjString(fDefaultStorage->GetURI()));
569 //_____________________________________________________________________________
570 void AliCDBManager::SetDefaultStorage(AliCDBStorage* storage) {
571 // set default storage from another active storage
573 // if lock is ON, cannot activate more storages!
575 if (fDefaultStorage) {
576 AliFatal("Lock is ON, and default storage is already set: "
577 "cannot reset it or activate more storages!");
582 UnsetDefaultStorage();
586 AliCDBStorage* bckStorage = fDefaultStorage;
588 fDefaultStorage = storage;
590 if(bckStorage && (fDefaultStorage != bckStorage)){
591 AliWarning("Existing default storage replaced: clearing cache!");
595 if (fStorageMap->Contains("default")) {
596 delete fStorageMap->Remove(((TPair*)fStorageMap->FindObject("default"))->Key());
598 fStorageMap->Add(new TObjString("default"), new TObjString(fDefaultStorage->GetURI()));
601 //_____________________________________________________________________________
602 void AliCDBManager::SetDefaultStorage(const char* mcString, const char* simType) {
603 // sets default storage for MC data
604 // mcString MUST be "MC",
605 // simType can be "Ideal","Residual","Full"
607 TString strmcString(mcString);
608 TString strsimType(simType);
610 if (strmcString != "MC"){
611 AliFatal("Method requires first string to be MC!");
614 if (strsimType == "Ideal"){
615 dbString = fgkMCIdealStorage;
617 else if (strsimType == "Full"){
618 dbString = fgkMCFullStorage;
620 else if (strsimType == "Residual"){
621 dbString = fgkMCResidualStorage;
624 AliFatal("Error in setting the storage for MC data, second argument MUST be either \"Ideal\" or \"Full\" or \"Residual\".");
627 SetDefaultStorage(dbString.Data());
628 if(!fDefaultStorage) AliFatal(Form("%s storage not there! Please check!",fLHCPeriod.Data()));
631 //_____________________________________________________________________________
632 void AliCDBManager::SetDefaultStorageFromRun(Int_t run) {
633 // set default storage from the run number - to be used only with raw data
635 // if lock is ON, cannot activate more storages!
637 if (fDefaultStorage) {
638 AliFatal("Lock is ON, and default storage is already set: "
639 "cannot activate default storage from run number");
643 // retrieve XML file from alien
645 TGrid::Connect("alien://","");
647 AliError("Connection to alien failed!");
652 TString rndname = "/tmp/";
653 rndname += "OCDBFolderXML.";
654 rndname += uuid.AsString();
656 AliDebug(2, Form("file to be copied = %s", fgkOCDBFolderXMLfile.Data()));
657 if (!TFile::Cp(fgkOCDBFolderXMLfile.Data(), rndname.Data())) {
658 AliFatal(Form("Cannot make a local copy of OCDBFolder xml file in %s",rndname.Data()));
660 AliCDBHandler* saxcdb = new AliCDBHandler();
662 TSAXParser *saxParser = new TSAXParser();
663 saxParser->ConnectToHandler("AliCDBHandler", saxcdb);
664 saxParser->ParseFile(rndname.Data());
665 AliInfo(Form(" LHC folder = %s", saxcdb->GetOCDBFolder().Data()));
666 AliInfo(Form(" LHC period start run = %d", saxcdb->GetStartRunRange()));
667 AliInfo(Form(" LHC period end run = %d", saxcdb->GetEndRunRange()));
668 fLHCPeriod = saxcdb->GetOCDBFolder();
669 fStartRunLHCPeriod = saxcdb->GetStartRunRange();
670 fEndRunLHCPeriod = saxcdb->GetEndRunRange();
672 SetDefaultStorage(fLHCPeriod.Data());
673 if(!fDefaultStorage) AliFatal(Form("%s storage not there! Please check!",fLHCPeriod.Data()));
677 //_____________________________________________________________________________
678 void AliCDBManager::UnsetDefaultStorage() {
679 // Unset default storage
681 // if lock is ON, action is forbidden!
683 if (fDefaultStorage) {
684 AliFatal("Lock is ON: cannot unset default storage!");
688 if (fDefaultStorage) {
689 AliWarning("Clearing cache!");
693 fRun = fStartRunLHCPeriod = fEndRunLHCPeriod = -1;
696 fDefaultStorage = 0x0;
699 //_____________________________________________________________________________
700 void AliCDBManager::SetSpecificStorage(const char* calibType, const char* dbString) {
701 // sets storage specific for detector or calibration type (works with AliCDBManager::Get(...))
703 AliCDBParam *aPar = CreateParameter(dbString);
705 SetSpecificStorage(calibType, aPar);
709 //_____________________________________________________________________________
710 void AliCDBManager::SetSpecificStorage(const char* calibType, const AliCDBParam* param) {
711 // sets storage specific for detector or calibration type (works with AliCDBManager::Get(...))
712 // Default storage should be defined prior to any specific storages, e.g.:
713 // AliCDBManager::instance()->SetDefaultStorage("alien://");
714 // AliCDBManager::instance()->SetSpecificStorage("TPC/*","local://DB_TPC");
715 // AliCDBManager::instance()->SetSpecificStorage("*/Align/*","local://DB_TPCAlign");
716 // calibType must be a valid CDB path! (3 level folder structure)
719 if(!fDefaultStorage && !fRaw) {
720 AliError("Please activate a default storage first!");
725 AliCDBPath aPath(calibType);
726 if(!aPath.IsValid()){
727 AliError(Form("Not a valid path: %s", calibType));
731 TObjString *objCalibType = new TObjString(aPath.GetPath());
732 if(fSpecificStorages.Contains(objCalibType)){
733 AliWarning(Form("Storage \"%s\" already activated! It will be replaced by the new one",
735 AliCDBParam *checkPar = dynamic_cast<AliCDBParam*> (fSpecificStorages.GetValue(calibType));
736 if(checkPar) delete checkPar;
737 delete fSpecificStorages.Remove(objCalibType);
739 AliCDBStorage *aStorage = GetStorage(param);
740 if(!aStorage) return;
742 fSpecificStorages.Add(objCalibType, param->CloneParam());
744 if(fStorageMap->Contains(objCalibType)){
745 delete fStorageMap->Remove(objCalibType);
747 fStorageMap->Add(objCalibType->Clone(), new TObjString(param->GetURI()));
751 //_____________________________________________________________________________
752 AliCDBStorage* AliCDBManager::GetSpecificStorage(const char* calibType) {
753 // get storage specific for detector or calibration type
755 AliCDBPath calibPath(calibType);
756 if(!calibPath.IsValid()) return NULL;
758 AliCDBParam *checkPar = (AliCDBParam*) fSpecificStorages.GetValue(calibPath.GetPath());
760 AliError(Form("%s storage not found!", calibType));
763 return GetStorage(checkPar);
768 //_____________________________________________________________________________
769 AliCDBParam* AliCDBManager::SelectSpecificStorage(const TString& path) {
770 // select storage valid for path from the list of specific storages
772 AliCDBPath aPath(path);
773 if(!aPath.IsValid()) return NULL;
775 TIter iter(&fSpecificStorages);
776 TObjString *aCalibType=0;
777 AliCDBPath tmpPath("null/null/null");
779 while((aCalibType = (TObjString*) iter.Next())){
780 AliCDBPath calibTypePath(aCalibType->GetName());
781 if(calibTypePath.Comprises(aPath)) {
782 if(calibTypePath.Comprises(tmpPath)) continue;
783 aPar = (AliCDBParam*) fSpecificStorages.GetValue(aCalibType);
784 tmpPath.SetPath(calibTypePath.GetPath());
790 //_____________________________________________________________________________
791 AliCDBEntry* AliCDBManager::Get(const AliCDBPath& path, Int_t runNumber,
792 Int_t version, Int_t subVersion) {
793 // get an AliCDBEntry object from the database
796 // RunNumber is not specified. Try with fRun
798 AliError("Run number neither specified in query nor set in AliCDBManager! Use AliCDBManager::SetRun.");
804 return Get(AliCDBId(path, runNumber, runNumber, version, subVersion));
807 //_____________________________________________________________________________
808 AliCDBEntry* AliCDBManager::Get(const AliCDBPath& path,
809 const AliCDBRunRange& runRange, Int_t version,
811 // get an AliCDBEntry object from the database!
813 return Get(AliCDBId(path, runRange, version, subVersion));
816 //_____________________________________________________________________________
817 AliCDBEntry* AliCDBManager::Get(const AliCDBId& query) {
818 // get an AliCDBEntry object from the database
820 // check if query's path and runRange are valid
821 // query is invalid also if version is not specified and subversion is!
822 if (!query.IsValid()) {
823 AliError(Form("Invalid query: %s", query.ToString().Data()));
827 // query is not specified if path contains wildcard or run range= [-1,-1]
828 if (!query.IsSpecified()) {
829 AliError(Form("Unspecified query: %s",
830 query.ToString().Data()));
834 if(fLock && !(fRun >= query.GetFirstRun() && fRun <= query.GetLastRun()))
835 AliFatal("Lock is ON: cannot use different run number than the internal one!");
837 if(fCache && !(fRun >= query.GetFirstRun() && fRun <= query.GetLastRun()))
838 AliWarning("Run number explicitly set in query: CDB cache temporarily disabled!");
840 AliCDBEntry *entry=0;
842 // first look into map of cached objects
843 if(fCache && query.GetFirstRun() == fRun)
844 entry = (AliCDBEntry*) fEntryCache.GetValue(query.GetPath());
846 AliDebug(2, Form("Object %s retrieved from cache !!",query.GetPath().Data()));
850 // if snapshot flag is set, try getting from the snapshot
851 if(fSnapshotMode && query.GetFirstRun() == fRun)
852 // entry = (AliCDBEntry*) fSnapshotCache->GetValue(query.GetPath()); // not possible,
853 // all the map would be charged in memory from the snapshot anyway.
854 entry = GetEntryFromSnapshot(query.GetPath());
856 AliDebug(2, 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);
860 if(!fIds->Contains(&entry->GetId()))
861 fIds->Add(entry->GetId().Clone());
866 // Entry is not in cache (and, in case we are in snapshot mode, not in the snapshot either)
867 // => retrieve it from the storage and cache it!!
868 if(!fDefaultStorage) {
869 AliError("No storage set!");
872 AliCDBStorage *aStorage=0;
873 AliCDBParam *aPar=SelectSpecificStorage(query.GetPath());
876 aStorage=GetStorage(aPar);
877 TString str = aPar->GetURI();
878 AliDebug(2,Form("Looking into storage: %s",str.Data()));
880 aStorage=GetDefaultStorage();
881 AliDebug(2,"Looking into default storage");
884 entry = aStorage->Get(query);
886 if(entry && fCache && (query.GetFirstRun() == fRun)){
887 CacheEntry(query.GetPath(), entry);
890 if(entry && !fIds->Contains(&entry->GetId())){
891 fIds->Add(entry->GetId().Clone());
899 //_____________________________________________________________________________
900 AliCDBEntry* AliCDBManager::GetEntryFromSnapshot(const char* path) {
901 // get the entry from the open snapshot file
904 sPath.ReplaceAll("/","*");
905 AliCDBEntry *entry = dynamic_cast<AliCDBEntry*>(fSnapshotFile->Get(sPath.Data()));
907 AliDebug(2,Form("Cannot get a CDB entry for \"%s\" from snapshot file",path));
914 //_____________________________________________________________________________
915 Bool_t AliCDBManager::SetSnapshotMode(const char* snapshotFileName) {
916 // set the manager in snapshot mode
919 AliError("Cannot set the CDB manage in snapshot mode if the cache is not active!");
924 TString snapshotFile(snapshotFileName);
925 if(snapshotFile.BeginsWith("alien://")){
927 TGrid::Connect("alien://","");
929 AliError("Connection to alien failed!");
935 fSnapshotFile = TFile::Open(snapshotFileName);
936 if (!fSnapshotFile || fSnapshotFile->IsZombie()){
937 AliError(Form("Cannot open file %s",snapshotFileName));
941 fSnapshotMode = kTRUE;
946 //_____________________________________________________________________________
947 const char* AliCDBManager::GetURI(const char* path) {
948 // return the URI of the storage where to look for path
950 if(!IsDefaultStorageSet()) return 0;
952 AliCDBParam *aPar=SelectSpecificStorage(path);
955 return aPar->GetURI().Data();
958 return GetDefaultStorage()->GetURI().Data();
964 //_____________________________________________________________________________
965 AliCDBId* AliCDBManager::GetId(const AliCDBPath& path, Int_t runNumber,
966 Int_t version, Int_t subVersion) {
967 // get the AliCDBId of the valid object from the database (does not retrieve the object)
968 // User must delete returned object!
971 // RunNumber is not specified. Try with fRun
973 AliError("Run number neither specified in query nor set in AliCDBManager! Use AliCDBManager::SetRun.");
979 return GetId(AliCDBId(path, runNumber, runNumber, version, subVersion));
982 //_____________________________________________________________________________
983 AliCDBId* AliCDBManager::GetId(const AliCDBPath& path,
984 const AliCDBRunRange& runRange, Int_t version,
986 // get the AliCDBId of the valid object from the database (does not retrieve the object)
987 // User must delete returned object!
989 return GetId(AliCDBId(path, runRange, version, subVersion));
992 //_____________________________________________________________________________
993 AliCDBId* AliCDBManager::GetId(const AliCDBId& query) {
994 // get the AliCDBId of the valid object from the database (does not retrieve the object)
995 // User must delete returned object!
997 if(!fDefaultStorage) {
998 AliError("No storage set!");
1002 // check if query's path and runRange are valid
1003 // query is invalid also if version is not specified and subversion is!
1004 if (!query.IsValid()) {
1005 AliError(Form("Invalid query: %s", query.ToString().Data()));
1009 // query is not specified if path contains wildcard or run range= [-1,-1]
1010 if (!query.IsSpecified()) {
1011 AliError(Form("Unspecified query: %s",
1012 query.ToString().Data()));
1016 if(fCache && query.GetFirstRun() != fRun)
1017 AliWarning("Run number explicitly set in query: CDB cache temporarily disabled!");
1019 AliCDBEntry* entry = 0;
1021 // first look into map of cached objects
1022 if(fCache && query.GetFirstRun() == fRun)
1023 entry = (AliCDBEntry*) fEntryCache.GetValue(query.GetPath());
1026 AliDebug(2, Form("Object %s retrieved from cache !!",query.GetPath().Data()));
1027 return dynamic_cast<AliCDBId*> (entry->GetId().Clone());
1030 // Entry is not in cache -> retrieve it from CDB and cache it!!
1031 AliCDBStorage *aStorage=0;
1032 AliCDBParam *aPar=SelectSpecificStorage(query.GetPath());
1035 aStorage=GetStorage(aPar);
1036 TString str = aPar->GetURI();
1037 AliDebug(2,Form("Looking into storage: %s",str.Data()));
1040 aStorage=GetDefaultStorage();
1041 AliDebug(2,"Looking into default storage");
1044 return aStorage->GetId(query);
1048 //_____________________________________________________________________________
1049 TList* AliCDBManager::GetAll(const AliCDBPath& path, Int_t runNumber,
1050 Int_t version, Int_t subVersion) {
1051 // get multiple AliCDBEntry objects from the database
1054 // RunNumber is not specified. Try with fRun
1056 AliError("Run number neither specified in query nor set in AliCDBManager! Use AliCDBManager::SetRun.");
1062 return GetAll(AliCDBId(path, runNumber, runNumber, version,
1066 //_____________________________________________________________________________
1067 TList* AliCDBManager::GetAll(const AliCDBPath& path,
1068 const AliCDBRunRange& runRange, Int_t version, Int_t subVersion) {
1069 // get multiple AliCDBEntry objects from the database
1071 return GetAll(AliCDBId(path, runRange, version, subVersion));
1074 //_____________________________________________________________________________
1075 TList* AliCDBManager::GetAll(const AliCDBId& query) {
1076 // get multiple AliCDBEntry objects from the database
1077 // Warning: this method works correctly only for queries of the type "Detector/*"
1078 // and not for more specific queries e.g. "Detector/Calib/*" !
1079 // Warning #2: Entries are cached, but GetAll will keep on retrieving objects from OCDB!
1080 // To get an object from cache use Get() function
1082 if(!fDefaultStorage) {
1083 AliError("No storage set!");
1087 if (!query.IsValid()) {
1088 AliError(Form("Invalid query: %s", query.ToString().Data()));
1092 if((fSpecificStorages.GetEntries()>0) && query.GetPath().BeginsWith('*')){
1093 // if specific storages are active a query with "*" is ambiguous
1094 AliError("Query too generic in this context!");
1098 if (query.IsAnyRange()) {
1099 AliError(Form("Unspecified run or runrange: %s",
1100 query.ToString().Data()));
1104 if(fLock && query.GetFirstRun() != fRun)
1105 AliFatal("Lock is ON: cannot use different run number than the internal one!");
1107 AliCDBParam *aPar=SelectSpecificStorage(query.GetPath());
1109 AliCDBStorage *aStorage;
1111 aStorage=GetStorage(aPar);
1112 AliDebug(2,Form("Looking into storage: %s", aPar->GetURI().Data()));
1115 aStorage=GetDefaultStorage();
1116 AliDebug(2,"Looking into default storage");
1120 if(aStorage) result = aStorage->GetAll(query);
1121 if(!result) return 0;
1123 // loop on result to check whether entries should be re-queried with specific storages
1124 if(fSpecificStorages.GetEntries()>0 && ! (fSpecificStorages.GetEntries() == 1 && aPar)) {
1125 AliInfo("Now look into all other specific storages...");
1128 AliCDBEntry* chkEntry=0;
1130 while((chkEntry = dynamic_cast<AliCDBEntry*> (iter.Next()))){
1131 AliCDBId& chkId = chkEntry->GetId();
1132 AliDebug(2, Form("Checking id %s ", chkId.GetPath().Data()));
1133 AliCDBParam *chkPar=SelectSpecificStorage(chkId.GetPath());
1134 if (!chkPar || aPar == chkPar) continue;
1135 AliCDBStorage *chkStorage = GetStorage(chkPar);
1136 AliDebug(2, Form("Found specific storage! %s", chkPar->GetURI().Data()));
1138 AliCDBEntry *newEntry=0;
1139 chkId.SetRunRange(query.GetFirstRun(), query.GetLastRun());
1140 chkId.SetVersion(query.GetVersion());
1141 chkId.SetSubVersion(query.GetSubVersion());
1143 if(chkStorage) newEntry = chkStorage->Get(chkId);
1144 if(!newEntry) continue;
1146 // object is found in specific storage: replace entry in the result list!
1147 chkEntry->SetOwner(1);
1148 delete result->Remove(chkEntry);
1149 result->AddFirst(newEntry);
1152 Int_t nEntries = result->GetEntries();
1153 AliInfo("After look into other specific storages, result list is:");
1154 for(int i=0; i<nEntries;i++){
1155 AliCDBEntry *entry = (AliCDBEntry*) result->At(i);
1156 AliInfo(Form("%s",entry->GetId().ToString().Data()));
1162 AliCDBEntry* entry=0;
1163 while((entry = dynamic_cast<AliCDBEntry*> (iter.Next()))){
1165 if(!fIds->Contains(&entry->GetId())){
1166 fIds->Add(entry->GetId().Clone());
1168 if(fCache && (query.GetFirstRun() == fRun)){
1169 CacheEntry(entry->GetId().GetPath(), entry);
1177 //_____________________________________________________________________________
1178 Bool_t AliCDBManager::Put(TObject* object, const AliCDBId& id, AliCDBMetaData* metaData, const DataType type){
1179 // store an AliCDBEntry object into the database
1182 AliError("Null Entry! No storage will be done!");
1186 AliCDBEntry anEntry(object, id, metaData);
1187 return Put(&anEntry, type);
1192 //_____________________________________________________________________________
1193 Bool_t AliCDBManager::Put(AliCDBEntry* entry, DataType type){
1194 // store an AliCDBEntry object into the database
1196 if(type == kPrivate && !fDefaultStorage) {
1197 AliError("No storage set!");
1202 AliError("No entry!");
1206 if (entry->GetObject()==0x0){
1207 AliError("No valid object in CDB entry!");
1211 if (!entry->GetId().IsValid()) {
1212 AliError(Form("Invalid entry ID: %s",
1213 entry->GetId().ToString().Data()));
1217 if (!entry->GetId().IsSpecified()) {
1218 AliError(Form("Unspecified entry ID: %s",
1219 entry->GetId().ToString().Data()));
1223 AliCDBId id = entry->GetId();
1224 AliCDBParam *aPar = SelectSpecificStorage(id.GetPath());
1226 AliCDBStorage *aStorage=0;
1229 aStorage=GetStorage(aPar);
1233 aStorage = GetStorage(fCondParam);
1236 aStorage = GetStorage(fRefParam);
1239 aStorage = GetDefaultStorage();
1244 AliDebug(2,Form("Storing object into storage: %s", aStorage->GetURI().Data()));
1246 Bool_t result = aStorage->Put(entry, type);
1248 if(fRun >= 0) QueryCDB();
1255 //_____________________________________________________________________________
1256 void AliCDBManager::CacheEntry(const char* path, AliCDBEntry* entry)
1258 // cache AliCDBEntry. Cache is valid until run number is changed.
1260 AliCDBEntry *chkEntry = dynamic_cast<AliCDBEntry*> (fEntryCache.GetValue(path));
1263 AliDebug(2, Form("Object %s already in cache !!", path));
1266 AliDebug(2,Form("Caching entry %s", path));
1269 fEntryCache.Add(new TObjString(path), entry);
1270 AliDebug(2,Form("Cache entries: %d", fEntryCache.GetEntries()));
1274 //_____________________________________________________________________________
1275 void AliCDBManager::Print(Option_t* /*option*/) const
1277 // Print list of active storages and their URIs
1279 TString output=Form("Run number = %d; ",fRun);
1280 output += "Cache is ";
1281 if(!fCache) output += "NOT ";
1282 output += Form("ACTIVE; Number of active storages: %d\n",fActiveStorages.GetEntries());
1284 if(fDefaultStorage) {
1285 output += Form("\t*** Default Storage URI: \"%s\"\n",fDefaultStorage->GetURI().Data());
1286 // AliInfo(output.Data());
1288 if(fSpecificStorages.GetEntries()>0) {
1289 TIter iter(fSpecificStorages.GetTable());
1292 while((aPair = (TPair*) iter.Next())){
1293 output += Form("\t*** Specific storage %d: Path \"%s\" -> URI \"%s\"\n",
1294 i++, ((TObjString*) aPair->Key())->GetName(),
1295 ((AliCDBParam*) aPair->Value())->GetURI().Data());
1299 output += Form("*** Drain Storage URI: %s\n",fDrainStorage->GetURI().Data());
1301 AliInfo(output.Data());
1304 //_____________________________________________________________________________
1305 void AliCDBManager::SetRun(Int_t run)
1307 // Sets current run number.
1308 // When the run number changes the caching is cleared.
1313 if(fLock && fRun >= 0) {
1314 AliFatal("Lock is ON, cannot reset run number!");
1319 // here the LHCPeriod xml file is parsed; the string containing the correct period is returned; the default storage is set
1320 if (fStartRunLHCPeriod <= run && fEndRunLHCPeriod >= run){
1321 AliInfo("LHCPeriod alien folder for current run already in memory");
1323 SetDefaultStorageFromRun(run);
1324 if(fEntryCache.GetEntries()!=0) ClearCache();
1332 //_____________________________________________________________________________
1333 void AliCDBManager::ClearCache(){
1334 // clear AliCDBEntry cache
1336 AliDebug(2, Form("Cache entries to be deleted: %d",fEntryCache.GetEntries()));
1339 // To clean entries one by one
1340 TIter iter(fEntryCache.GetTable());
1342 while((pair= dynamic_cast<TPair*> (iter.Next()))){
1344 TObjString* key = dynamic_cast<TObjString*> (pair->Key());
1345 AliCDBEntry* entry = dynamic_cast<AliCDBEntry*> (pair->Value());
1346 AliDebug(2, Form("Deleting entry: %s", key->GetName()));
1347 if (entry) delete entry;
1348 delete fEntryCache.Remove(key);
1351 fEntryCache.DeleteAll();
1352 AliDebug(2, Form("After deleting - Cache entries: %d",fEntryCache.GetEntries()));
1355 //_____________________________________________________________________________
1356 void AliCDBManager::UnloadFromCache(const char* path){
1357 // unload cached object
1358 // that is remove the entry from the cache and the id from the list of ids
1360 if(!fActiveStorages.GetEntries()) {
1361 AliDebug(2, Form("No active storages. Object \"%s\" is not unloaded from cache", path));
1365 AliCDBPath queryPath(path);
1366 if(!queryPath.IsValid()) return;
1368 if(!queryPath.IsWildcard()) { // path is not wildcard, get it directly from the cache and unload it!
1369 if(fEntryCache.Contains(path)){
1370 AliDebug(2, Form("Unloading object \"%s\" from cache and from list of ids", path));
1371 TObjString pathStr(path);
1372 delete fEntryCache.Remove(&pathStr);
1373 // we do not remove from the list of Id's (it's not very coherent but we leave the
1374 // id for the benefit of the userinfo)
1378 while((id = dynamic_cast<AliCDBId*> (iter.Next()))){
1379 if(queryPath.Comprises(id->GetPath()))
1380 delete fIds->Remove(id);
1383 AliError(Form("Cache does not contain object \"%s\"!", path));
1385 AliDebug(2, Form("Cache entries: %d",fEntryCache.GetEntries()));
1389 // path is wildcard: loop on the cache and unload all comprised objects!
1390 TIter iter(fEntryCache.GetTable());
1394 while((pair = dynamic_cast<TPair*> (iter.Next()))){
1395 AliCDBPath entryPath = pair->Key()->GetName();
1396 if(queryPath.Comprises(entryPath)) {
1397 AliDebug(2, Form("Unloading object \"%s\" from cache and from list of ids", entryPath.GetPath().Data()));
1398 TObjString pathStr(entryPath.GetPath());
1399 delete fEntryCache.Remove(&pathStr);
1402 // we do not remove from the list of Id's (it's not very coherent but we leave the
1403 // id for the benefit of the userinfo)
1405 TIter iterids(fIds);
1407 while((anId = dynamic_cast<AliCDBId*> (iterids.Next()))){
1408 AliCDBPath aPath = anId->GetPath();
1409 TString aPathStr = aPath.GetPath();
1410 if(queryPath.Comprises(aPath)) {
1411 delete fIds->Remove(anId);
1416 AliDebug(2,Form("Cache entries and ids removed: %d Remaining: %d",removed,fEntryCache.GetEntries()));
1419 //_____________________________________________________________________________
1420 void AliCDBManager::DestroyActiveStorages() {
1421 // delete list of active storages
1423 fActiveStorages.DeleteAll();
1424 fSpecificStorages.DeleteAll();
1427 //_____________________________________________________________________________
1428 void AliCDBManager::DestroyActiveStorage(AliCDBStorage* /*storage*/) {
1429 // destroys active storage
1432 TIter iter(fActiveStorages.GetTable());
1434 while ((aPair = (TPair*) iter.Next())) {
1435 if(storage == (AliCDBStorage*) aPair->Value())
1436 delete fActiveStorages.Remove(aPair->Key());
1437 storage->Delete(); storage=0x0;
1443 //_____________________________________________________________________________
1444 void AliCDBManager::QueryCDB() {
1445 // query default and specific storages for files valid for fRun. Every storage loads the Ids into its list.
1448 AliError("Run number not yet set! Use AliCDBManager::SetRun.");
1451 if (!fDefaultStorage){
1452 AliError("Default storage is not set! Use AliCDBManager::SetDefaultStorage");
1455 if(fDefaultStorage->GetType() == "alien"){
1456 fDefaultStorage->QueryCDB(fRun);
1458 AliDebug(2,"Skipping query for valid files, it used only in grid...");
1461 TIter iter(&fSpecificStorages);
1462 TObjString *aCalibType=0;
1463 AliCDBParam* aPar=0;
1464 while((aCalibType = dynamic_cast<TObjString*> (iter.Next()))){
1465 aPar = (AliCDBParam*) fSpecificStorages.GetValue(aCalibType);
1467 AliDebug(2,Form("Querying specific storage %s",aCalibType->GetName()));
1468 AliCDBStorage *aStorage = GetStorage(aPar);
1469 if(aStorage->GetType() == "alien"){
1470 aStorage->QueryCDB(fRun,aCalibType->GetName());
1473 "Skipping query for valid files, it is used only in grid...");
1479 //______________________________________________________________________________________________
1480 const char* AliCDBManager::GetDataTypeName(DataType type)
1482 // returns the name (string) of the data type
1485 case kCondition: return "Conditions";
1486 case kReference: return "Reference";
1487 case kPrivate: return "Private";
1493 //______________________________________________________________________________________________
1494 void AliCDBManager::InitShortLived()
1496 // Init the list of short-lived objects
1497 // currently disabled
1501 // fShortLived = new TList();
1502 // fShortLived->SetOwner(1);
1504 // fShortLived->Add(new TObjString("EMCAL/Calib/Data"));
1506 // fShortLived->Add(new TObjString("HMPID/Calib/Nmean"));
1507 // fShortLived->Add(new TObjString("HMPID/Calib/Qthre"));
1509 // fShortLived->Add(new TObjString("ITS/Calib/CalibSPD"));
1511 // fShortLived->Add(new TObjString("MUON/Calib/Gains"));
1512 // fShortLived->Add(new TObjString("MUON/Calib/HV"));
1513 // fShortLived->Add(new TObjString("MUON/Calib/Pedestals"));
1515 // fShortLived->Add(new TObjString("PHOS/Calib/CpvGainPedestals"));
1516 // fShortLived->Add(new TObjString("PHOS/Calib/EmcGainPedestals"));
1518 // fShortLived->Add(new TObjString("PMD/Calib/Data"));
1520 // fShortLived->Add(new TObjString("TRD/Calib/ChamberGainFactor"));
1521 // fShortLived->Add(new TObjString("TRD/Calib/LocalGainFactor"));
1522 // fShortLived->Add(new TObjString("TRD/Calib/ChamberT0"));
1523 // fShortLived->Add(new TObjString("TRD/Calib/LocalT0"));
1524 // fShortLived->Add(new TObjString("TRD/Calib/ChamberVdrift"));
1525 // fShortLived->Add(new TObjString("TRD/Calib/LocalVdrift"));
1527 // fShortLived->Add(new TObjString("ZDC/Calib/Data"));
1531 //______________________________________________________________________________________________
1532 Bool_t AliCDBManager::IsShortLived(const char* path)
1534 // returns the name (string) of the data type
1536 if(!fShortLived) return kFALSE;
1538 AliCDBPath aPath(path);
1539 if(!aPath.IsValid()){
1540 AliError(Form("Not a valid path: %s", path));
1544 return fShortLived->Contains(path);
1548 //______________________________________________________________________________________________
1549 ULong_t AliCDBManager::SetLock(Bool_t lock, ULong_t key){
1550 // To lock/unlock user must provide the key. A new key is provided after
1551 // each successful lock. User should always backup the returned key and
1552 // use it on next access.
1553 if (fLock == lock) return 0; // nothing to be done
1555 // User wants to lock - check his identity
1557 // Lock has a user - check his key
1559 AliFatal("Wrong key provided to lock CDB. Please remove CDB lock access from your code !");
1564 fKey = gSystem->Now();
1568 // User wants to unlock - check the provided key
1570 AliFatal("Lock is ON: wrong key provided");
1577 ///////////////////////////////////////////////////////////
1578 // AliCDBManager Parameter class //
1579 // interface to specific AliCDBParameter class //
1580 // (AliCDBGridParam, AliCDBLocalParam, AliCDBDumpParam) //
1581 ///////////////////////////////////////////////////////////
1583 AliCDBParam::AliCDBParam():
1591 //_____________________________________________________________________________
1592 AliCDBParam::~AliCDBParam() {