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 //-------------------------------------------------------------------------
21 #include "AliCDBManager.h"
22 #include "AliCDBStorage.h"
24 #include "AliCDBDump.h"
25 #include "AliCDBLocal.h"
26 #include "AliCDBGrid.h"
27 #include "AliCDBEntry.h"
28 #include "AliCDBHandler.h"
30 #include <TObjString.h>
31 #include <TSAXParser.h>
39 ClassImp(AliCDBManager)
41 //TODO OCDB and Reference folder should not be fully hardcoded but built from run number (or year/LHC period)
42 TString AliCDBManager::fgkCondUri("alien://folder=/alice/cern.ch/user/a/aliprod/testCDB/CDB?user=aliprod");
43 TString AliCDBManager::fgkRefUri("alien://folder=/alice/cern.ch/user/a/aliprod/testCDB/Reference?user=aliprod");
44 TString AliCDBManager::fgkMCIdealStorage("alien://folder=/alice/simulation/2008/v4-15-Release/Ideal");
45 TString AliCDBManager::fgkMCFullStorage("alien://folder=/alice/simulation/2008/v4-15-Release/Full");
46 TString AliCDBManager::fgkMCResidualStorage("alien://folder=/alice/simulation/2008/v4-15-Release/Residual");
47 TString AliCDBManager::fgkOCDBFolderXMLfile("alien:///alice/data/OCDBFoldervsRunRange.xml");
48 AliCDBManager* AliCDBManager::fgInstance = 0x0;
50 //_____________________________________________________________________________
51 AliCDBManager* AliCDBManager::Instance(TMap *entryCache, Int_t run)
53 // returns AliCDBManager instance (singleton)
56 fgInstance = new AliCDBManager();
60 fgInstance->InitFromCache(entryCache,run);
66 //_____________________________________________________________________________
67 void AliCDBManager::Init() {
68 // factory registering
70 RegisterFactory(new AliCDBDumpFactory());
71 RegisterFactory(new AliCDBLocalFactory());
72 // AliCDBGridFactory is registered only if AliEn libraries are enabled in Root
73 if(!gSystem->Exec("root-config --has-alien |grep yes 2>&1 > /dev/null")){ // returns 0 if yes
74 AliInfo("AliEn classes enabled in Root. AliCDBGrid factory registered.");
75 RegisterFactory(new AliCDBGridFactory());
76 fCondParam = CreateParameter(fgkCondUri);
77 fRefParam = CreateParameter(fgkRefUri);
83 //_____________________________________________________________________________
84 void AliCDBManager::InitFromCache(TMap *entryCache, Int_t run) {
85 // initialize manager from existing cache
86 // used on the slaves in case of parallel reconstruction
89 TIter iter(entryCache->GetTable());
92 while((pair = dynamic_cast<TPair*> (iter.Next()))){
93 fEntryCache.Add(pair->Key(),pair->Value());
95 // fEntry is the new owner of the cache
96 fEntryCache.SetOwnerKeyValue(kTRUE,kTRUE);
97 entryCache->SetOwnerKeyValue(kFALSE,kFALSE);
98 AliInfo(Form("%d cache entries have been loaded",fEntryCache.GetEntries()));
101 //_____________________________________________________________________________
102 void AliCDBManager::DumpToSnapshotFile(const char* snapshotFileName){
104 // dump the entries map and the ids list to
108 TFile *f = TFile::Open(snapshotFileName,"RECREATE");
109 if (!f || f->IsZombie()){
110 AliError(Form("Cannot open file %s",snapshotFileName));
114 Printf("\ndumping entriesMap (entries'cache) with %d entries!\n", fEntryCache.GetEntries());
115 Printf("\ndumping entriesList with %d entries!\n", fIds->GetEntries());
119 f->WriteObject(&fEntryCache,"CDBentriesMap");
120 f->WriteObject(fIds,"CDBidsList");
128 //_____________________________________________________________________________
129 Bool_t AliCDBManager::InitFromSnapshot(const char* snapshotFileName){
130 // initialize manager from a CDB snapshot, that is add the entries
131 // to the entries map and the ids to the ids list taking them from
132 // the map and the list found in the input file
135 TString snapshotFile(snapshotFileName);
136 if(snapshotFile.BeginsWith("alien://")){
138 TGrid::Connect("alien://","");
140 AliError("Connection to alien failed!");
146 TFile *f = TFile::Open(snapshotFileName);
147 if (!f || f->IsZombie()){
148 AliError(Form("Cannot open file %s",snapshotFileName));
152 // retrieve entries map
153 TMap *entriesMap = 0;
154 TIter next(f->GetListOfKeys());
156 while ((key = (TKey*)next())) {
157 if (strcmp(key->GetClassName(),"TMap") != 0) continue;
158 entriesMap = (TMap*)key->ReadObj();
161 if (!entriesMap || entriesMap->GetEntries()==0){
162 AliError("Cannot get valid map of CDB entries from snapshot file");
168 TIter nextKey(f->GetListOfKeys());
170 while ((keyN = (TKey*)nextKey())) {
171 if (strcmp(keyN->GetClassName(),"TList") != 0) continue;
172 idsList = (TList*)keyN->ReadObj();
175 if (!idsList || idsList->GetEntries()==0){
176 AliError("Cannot get valid list of CDB entries from snapshot file");
180 TIter iterObj(entriesMap->GetTable());
183 while((pair = dynamic_cast<TPair*> (iterObj.Next()))){
184 fEntryCache.Add(pair->Key(),pair->Value());
186 // fEntry is the new owner of the cache
187 fEntryCache.SetOwnerKeyValue(kTRUE,kTRUE);
188 entriesMap->SetOwnerKeyValue(kFALSE,kFALSE);
189 AliInfo(Form("%d cache entries have been loaded",fEntryCache.GetEntries()));
191 TIter iterId(idsList);
194 while((id = dynamic_cast<AliCDBId*> (iterId.Next()))){
197 fIds->SetOwner(kTRUE);
198 idsList->SetOwner(kFALSE);
201 AliInfo(Form("%d cache ids loaded. Total number of ids is %d",idsList->GetEntries(),fIds->GetEntries()));
206 //_____________________________________________________________________________
207 void AliCDBManager::Destroy() {
208 // delete ALCDBManager instance and active storages
211 //fgInstance->Delete();
217 //_____________________________________________________________________________
218 AliCDBManager::AliCDBManager():
227 fDefaultStorage(NULL),
235 fStartRunLHCPeriod(-1),
236 fEndRunLHCPeriod(-1),
240 // default constuctor
241 fFactories.SetOwner(1);
242 fActiveStorages.SetOwner(1);
243 fSpecificStorages.SetOwner(1);
244 fEntryCache.SetName("CDBEntryCache");
245 fEntryCache.SetOwnerKeyValue(kTRUE,kTRUE);
247 fStorageMap = new TMap();
248 fStorageMap->SetOwner(1);
253 //_____________________________________________________________________________
254 AliCDBManager::~AliCDBManager() {
257 DestroyActiveStorages();
260 fDefaultStorage = 0x0;
261 delete fStorageMap; fStorageMap = 0;
262 delete fIds; fIds = 0;
265 delete fShortLived; fShortLived = 0x0;
268 //_____________________________________________________________________________
269 void AliCDBManager::PutActiveStorage(AliCDBParam* param, AliCDBStorage* storage){
270 // put a storage object into the list of active storages
272 fActiveStorages.Add(param, storage);
273 AliDebug(1, Form("Active storages: %d", fActiveStorages.GetEntries()));
276 //_____________________________________________________________________________
277 void AliCDBManager::RegisterFactory(AliCDBStorageFactory* factory) {
278 // add a storage factory to the list of registerd factories
280 if (!fFactories.Contains(factory)) {
281 fFactories.Add(factory);
285 //_____________________________________________________________________________
286 Bool_t AliCDBManager::HasStorage(const char* dbString) const {
287 // check if dbString is a URI valid for one of the registered factories
289 TIter iter(&fFactories);
291 AliCDBStorageFactory* factory=0;
292 while ((factory = (AliCDBStorageFactory*) iter.Next())) {
294 if (factory->Validate(dbString)) {
302 //_____________________________________________________________________________
303 AliCDBParam* AliCDBManager::CreateParameter(const char* dbString) const {
304 // create AliCDBParam object from URI string
306 TIter iter(&fFactories);
308 AliCDBStorageFactory* factory=0;
309 while ((factory = (AliCDBStorageFactory*) iter.Next())) {
310 AliCDBParam* param = factory->CreateParameter(dbString);
311 if(param) return param;
317 //_____________________________________________________________________________
318 AliCDBStorage* AliCDBManager::GetStorage(const char* dbString) {
319 // get storage object from URI string
321 AliCDBParam* param = CreateParameter(dbString);
323 AliError(Form("Failed to activate requested storage! Check URI: %s", dbString));
327 AliCDBStorage* aStorage = GetStorage(param);
333 //_____________________________________________________________________________
334 AliCDBStorage* AliCDBManager::GetStorage(const AliCDBParam* param) {
335 // get storage object from AliCDBParam object
337 // if the list of active storages already contains
338 // the requested storage, return it
339 AliCDBStorage* aStorage = GetActiveStorage(param);
344 // if lock is ON, cannot activate more storages!
346 if (fDefaultStorage) {
347 AliFatal("Lock is ON, and default storage is already set: "
348 "cannot reset it or activate more storages!");
352 TIter iter(&fFactories);
354 AliCDBStorageFactory* factory=0;
356 // loop on the list of registered factories
357 while ((factory = (AliCDBStorageFactory*) iter.Next())) {
359 // each factory tries to create its storage from the parameter
360 aStorage = factory->Create(param);
362 PutActiveStorage(param->CloneParam(), aStorage);
363 aStorage->SetURI(param->GetURI());
365 if(aStorage->GetType() == "alien"){
366 aStorage->QueryCDB(fRun);
369 "Skipping query for valid files, it is used only in grid...");
376 AliError(Form("Failed to activate requested storage! Check URI: %s", param->GetURI().Data()));
381 //_____________________________________________________________________________
382 AliCDBStorage* AliCDBManager::GetActiveStorage(const AliCDBParam* param) {
383 // get a storage object from the list of active storages
385 return dynamic_cast<AliCDBStorage*> (fActiveStorages.GetValue(param));
388 //_____________________________________________________________________________
389 TList* AliCDBManager::GetActiveStorages() {
390 // return list of active storages
391 // user has responsibility to delete returned object
393 TList* result = new TList();
395 TIter iter(fActiveStorages.GetTable());
397 while ((aPair = (TPair*) iter.Next())) {
398 result->Add(aPair->Value());
404 //_____________________________________________________________________________
405 void AliCDBManager::SetDrain(const char* dbString) {
406 // set drain storage from URI string
408 fDrainStorage = GetStorage(dbString);
411 //_____________________________________________________________________________
412 void AliCDBManager::SetDrain(const AliCDBParam* param) {
413 // set drain storage from AliCDBParam
415 fDrainStorage = GetStorage(param);
418 //_____________________________________________________________________________
419 void AliCDBManager::SetDrain(AliCDBStorage* storage) {
420 // set drain storage from another active storage
422 fDrainStorage = storage;
425 //_____________________________________________________________________________
426 Bool_t AliCDBManager::Drain(AliCDBEntry *entry) {
427 // drain retrieved object to drain storage
429 AliDebug(2, "Draining into drain storage...");
430 return fDrainStorage->Put(entry);
433 //____________________________________________________________________________
434 void AliCDBManager::SetDefaultStorage(const char* dbString) {
435 // sets default storage from URI string
437 // checking whether we are in the raw case
438 TString dbStringTemp(dbString);
439 if (dbStringTemp == "raw://")
442 AliInfo("Setting the run-number will set the corresponding OCDB for raw data reconstruction.");
443 AliInfo("Connecting to the grid...");
445 TGrid::Connect("alien://","");
447 AliError("Connection to alien failed!");
454 AliCDBStorage* bckStorage = fDefaultStorage;
456 fDefaultStorage = GetStorage(dbString);
458 if(!fDefaultStorage) return;
460 if(bckStorage && (fDefaultStorage != bckStorage)){
461 AliWarning("Existing default storage replaced: clearing cache!");
465 if (fStorageMap->Contains("default")) {
466 delete fStorageMap->Remove(((TPair*)fStorageMap->FindObject("default"))->Key());
468 fStorageMap->Add(new TObjString("default"), new TObjString(fDefaultStorage->GetURI()));
470 //_____________________________________________________________________________
471 void AliCDBManager::SetDefaultStorage(const AliCDBParam* param) {
472 // set default storage from AliCDBParam object
474 AliCDBStorage* bckStorage = fDefaultStorage;
476 fDefaultStorage = GetStorage(param);
478 if(!fDefaultStorage) return;
480 if(bckStorage && (fDefaultStorage != bckStorage)){
481 AliWarning("Existing default storage replaced: clearing cache!");
485 if (fStorageMap->Contains("default")) {
486 delete fStorageMap->Remove(((TPair*)fStorageMap->FindObject("default"))->Key());
488 fStorageMap->Add(new TObjString("default"), new TObjString(fDefaultStorage->GetURI()));
491 //_____________________________________________________________________________
492 void AliCDBManager::SetDefaultStorage(AliCDBStorage* storage) {
493 // set default storage from another active storage
495 // if lock is ON, cannot activate more storages!
497 if (fDefaultStorage) {
498 AliFatal("Lock is ON, and default storage is already set: "
499 "cannot reset it or activate more storages!");
504 UnsetDefaultStorage();
508 AliCDBStorage* bckStorage = fDefaultStorage;
510 fDefaultStorage = storage;
512 if(bckStorage && (fDefaultStorage != bckStorage)){
513 AliWarning("Existing default storage replaced: clearing cache!");
517 if (fStorageMap->Contains("default")) {
518 delete fStorageMap->Remove(((TPair*)fStorageMap->FindObject("default"))->Key());
520 fStorageMap->Add(new TObjString("default"), new TObjString(fDefaultStorage->GetURI()));
523 //_____________________________________________________________________________
524 void AliCDBManager::SetDefaultStorage(const char* mcString, const char* simType) {
525 // sets default storage for MC data
526 // mcString MUST be "MC",
527 // simType can be "Ideal","Residual","Full"
529 TString strmcString(mcString);
530 TString strsimType(simType);
532 if (strmcString != "MC"){
533 AliFatal("Method requires first string to be MC!");
536 if (strsimType == "Ideal"){
537 dbString = fgkMCIdealStorage;
539 else if (strsimType == "Full"){
540 dbString = fgkMCFullStorage;
542 else if (strsimType == "Residual"){
543 dbString = fgkMCResidualStorage;
546 AliFatal("Error in setting the storage for MC data, second argument MUST be either \"Ideal\" or \"Full\" or \"Residual\".");
549 SetDefaultStorage(dbString.Data());
550 if(!fDefaultStorage) AliFatal(Form("%s storage not there! Please check!",fLHCPeriod.Data()));
553 //_____________________________________________________________________________
554 void AliCDBManager::SetDefaultStorageFromRun(Int_t run) {
555 // set default storage from the run number - to be used only with raw data
557 // if lock is ON, cannot activate more storages!
559 if (fDefaultStorage) {
560 AliFatal("Lock is ON, and default storage is already set: "
561 "cannot activate default storage from run number");
565 // retrieve XML file from alien
567 TGrid::Connect("alien://","");
569 AliError("Connection to alien failed!");
574 TString rndname = "/tmp/";
575 rndname += "OCDBFolderXML.";
576 rndname += uuid.AsString();
578 AliDebug(2, Form("file to be copied = %s", fgkOCDBFolderXMLfile.Data()));
579 if (!TFile::Cp(fgkOCDBFolderXMLfile.Data(), rndname.Data())) {
580 AliFatal(Form("Cannot make a local copy of OCDBFolder xml file in %s",rndname.Data()));
582 AliCDBHandler* saxcdb = new AliCDBHandler();
584 TSAXParser *saxParser = new TSAXParser();
585 saxParser->ConnectToHandler("AliCDBHandler", saxcdb);
586 saxParser->ParseFile(rndname.Data());
587 AliInfo(Form(" LHC folder = %s", saxcdb->GetOCDBFolder().Data()));
588 AliInfo(Form(" LHC period start run = %d", saxcdb->GetStartRunRange()));
589 AliInfo(Form(" LHC period end run = %d", saxcdb->GetEndRunRange()));
590 fLHCPeriod = saxcdb->GetOCDBFolder();
591 fStartRunLHCPeriod = saxcdb->GetStartRunRange();
592 fEndRunLHCPeriod = saxcdb->GetEndRunRange();
594 SetDefaultStorage(fLHCPeriod.Data());
595 if(!fDefaultStorage) AliFatal(Form("%s storage not there! Please check!",fLHCPeriod.Data()));
599 //_____________________________________________________________________________
600 void AliCDBManager::UnsetDefaultStorage() {
601 // Unset default storage
603 // if lock is ON, action is forbidden!
605 if (fDefaultStorage) {
606 AliFatal("Lock is ON: cannot unset default storage!");
610 if (fDefaultStorage) {
611 AliWarning("Clearing cache!");
615 fRun = fStartRunLHCPeriod = fEndRunLHCPeriod = -1;
618 fDefaultStorage = 0x0;
621 //_____________________________________________________________________________
622 void AliCDBManager::SetSpecificStorage(const char* calibType, const char* dbString) {
623 // sets storage specific for detector or calibration type (works with AliCDBManager::Get(...))
625 AliCDBParam *aPar = CreateParameter(dbString);
627 SetSpecificStorage(calibType, aPar);
631 //_____________________________________________________________________________
632 void AliCDBManager::SetSpecificStorage(const char* calibType, const AliCDBParam* param) {
633 // sets storage specific for detector or calibration type (works with AliCDBManager::Get(...))
634 // Default storage should be defined prior to any specific storages, e.g.:
635 // AliCDBManager::instance()->SetDefaultStorage("alien://");
636 // AliCDBManager::instance()->SetSpecificStorage("TPC/*","local://DB_TPC");
637 // AliCDBManager::instance()->SetSpecificStorage("*/Align/*","local://DB_TPCAlign");
638 // calibType must be a valid CDB path! (3 level folder structure)
641 if(!fDefaultStorage && !fRaw) {
642 AliError("Please activate a default storage first!");
647 AliCDBPath aPath(calibType);
648 if(!aPath.IsValid()){
649 AliError(Form("Not a valid path: %s", calibType));
653 TObjString *objCalibType = new TObjString(aPath.GetPath());
654 if(fSpecificStorages.Contains(objCalibType)){
655 AliWarning(Form("Storage \"%s\" already activated! It will be replaced by the new one",
657 AliCDBParam *checkPar = dynamic_cast<AliCDBParam*> (fSpecificStorages.GetValue(calibType));
658 if(checkPar) delete checkPar;
659 delete fSpecificStorages.Remove(objCalibType);
661 AliCDBStorage *aStorage = GetStorage(param);
662 if(!aStorage) return;
664 fSpecificStorages.Add(objCalibType, param->CloneParam());
666 if(fStorageMap->Contains(objCalibType)){
667 delete fStorageMap->Remove(objCalibType);
669 fStorageMap->Add(objCalibType->Clone(), new TObjString(param->GetURI()));
673 //_____________________________________________________________________________
674 AliCDBStorage* AliCDBManager::GetSpecificStorage(const char* calibType) {
675 // get storage specific for detector or calibration type
677 AliCDBPath calibPath(calibType);
678 if(!calibPath.IsValid()) return NULL;
680 AliCDBParam *checkPar = (AliCDBParam*) fSpecificStorages.GetValue(calibPath.GetPath());
682 AliError(Form("%s storage not found!", calibType));
685 return GetStorage(checkPar);
690 //_____________________________________________________________________________
691 AliCDBParam* AliCDBManager::SelectSpecificStorage(const TString& path) {
692 // select storage valid for path from the list of specific storages
694 AliCDBPath aPath(path);
695 if(!aPath.IsValid()) return NULL;
697 TIter iter(&fSpecificStorages);
698 TObjString *aCalibType=0;
699 AliCDBPath tmpPath("null/null/null");
701 while((aCalibType = (TObjString*) iter.Next())){
702 AliCDBPath calibTypePath(aCalibType->GetName());
703 if(calibTypePath.Comprises(aPath)) {
704 if(calibTypePath.Comprises(tmpPath)) continue;
705 aPar = (AliCDBParam*) fSpecificStorages.GetValue(aCalibType);
706 tmpPath.SetPath(calibTypePath.GetPath());
712 //_____________________________________________________________________________
713 AliCDBEntry* AliCDBManager::Get(const AliCDBPath& path, Int_t runNumber,
714 Int_t version, Int_t subVersion) {
715 // get an AliCDBEntry object from the database
718 // RunNumber is not specified. Try with fRun
720 AliError("Run number neither specified in query nor set in AliCDBManager! Use AliCDBManager::SetRun.");
726 return Get(AliCDBId(path, runNumber, runNumber, version, subVersion));
729 //_____________________________________________________________________________
730 AliCDBEntry* AliCDBManager::Get(const AliCDBPath& path,
731 const AliCDBRunRange& runRange, Int_t version,
733 // get an AliCDBEntry object from the database!
735 return Get(AliCDBId(path, runRange, version, subVersion));
738 //_____________________________________________________________________________
739 AliCDBEntry* AliCDBManager::Get(const AliCDBId& query) {
740 // get an AliCDBEntry object from the database
742 // check if query's path and runRange are valid
743 // query is invalid also if version is not specified and subversion is!
744 if (!query.IsValid()) {
745 AliError(Form("Invalid query: %s", query.ToString().Data()));
749 // query is not specified if path contains wildcard or run range= [-1,-1]
750 if (!query.IsSpecified()) {
751 AliError(Form("Unspecified query: %s",
752 query.ToString().Data()));
756 if(fLock && !(fRun >= query.GetFirstRun() && fRun <= query.GetLastRun()))
757 AliFatal("Lock is ON: cannot use different run number than the internal one!");
759 if(fCache && !(fRun >= query.GetFirstRun() && fRun <= query.GetLastRun()))
760 AliWarning("Run number explicitly set in query: CDB cache temporarily disabled!");
762 AliCDBEntry *entry=0;
764 // first look into map of cached objects
765 if(fCache && query.GetFirstRun() == fRun)
766 entry = (AliCDBEntry*) fEntryCache.GetValue(query.GetPath());
769 AliDebug(2, Form("Object %s retrieved from cache !!",query.GetPath().Data()));
773 if(!fDefaultStorage) {
774 AliError("No storage set!");
777 // Entry is not in cache -> retrieve it from CDB and cache it!!
778 AliCDBStorage *aStorage=0;
779 AliCDBParam *aPar=SelectSpecificStorage(query.GetPath());
780 // Bool_t usedDefStorage=kTRUE;
783 aStorage=GetStorage(aPar);
784 TString str = aPar->GetURI();
785 AliDebug(2,Form("Looking into storage: %s",str.Data()));
786 // usedDefStorage=kFALSE;
789 aStorage=GetDefaultStorage();
790 AliDebug(2,"Looking into default storage");
793 entry = aStorage->Get(query);
795 if(entry && fCache && (query.GetFirstRun() == fRun)){
796 CacheEntry(query.GetPath(), entry);
799 if(entry && !fIds->Contains(&entry->GetId())){
800 fIds->Add(entry->GetId().Clone());
808 //_____________________________________________________________________________
809 const char* AliCDBManager::GetURI(const char* path) {
810 // return the URI of the storage where to look for path
812 if(!IsDefaultStorageSet()) return 0;
814 AliCDBParam *aPar=SelectSpecificStorage(path);
817 return aPar->GetURI().Data();
820 return GetDefaultStorage()->GetURI().Data();
826 //_____________________________________________________________________________
827 AliCDBId* AliCDBManager::GetId(const AliCDBPath& path, Int_t runNumber,
828 Int_t version, Int_t subVersion) {
829 // get the AliCDBId of the valid object from the database (does not retrieve the object)
830 // User must delete returned object!
833 // RunNumber is not specified. Try with fRun
835 AliError("Run number neither specified in query nor set in AliCDBManager! Use AliCDBManager::SetRun.");
841 return GetId(AliCDBId(path, runNumber, runNumber, version, subVersion));
844 //_____________________________________________________________________________
845 AliCDBId* AliCDBManager::GetId(const AliCDBPath& path,
846 const AliCDBRunRange& runRange, Int_t version,
848 // get the AliCDBId of the valid object from the database (does not retrieve the object)
849 // User must delete returned object!
851 return GetId(AliCDBId(path, runRange, version, subVersion));
854 //_____________________________________________________________________________
855 AliCDBId* AliCDBManager::GetId(const AliCDBId& query) {
856 // get the AliCDBId of the valid object from the database (does not retrieve the object)
857 // User must delete returned object!
859 if(!fDefaultStorage) {
860 AliError("No storage set!");
864 // check if query's path and runRange are valid
865 // query is invalid also if version is not specified and subversion is!
866 if (!query.IsValid()) {
867 AliError(Form("Invalid query: %s", query.ToString().Data()));
871 // query is not specified if path contains wildcard or run range= [-1,-1]
872 if (!query.IsSpecified()) {
873 AliError(Form("Unspecified query: %s",
874 query.ToString().Data()));
878 if(fCache && query.GetFirstRun() != fRun)
879 AliWarning("Run number explicitly set in query: CDB cache temporarily disabled!");
881 AliCDBEntry* entry = 0;
883 // first look into map of cached objects
884 if(fCache && query.GetFirstRun() == fRun)
885 entry = (AliCDBEntry*) fEntryCache.GetValue(query.GetPath());
888 AliDebug(2, Form("Object %s retrieved from cache !!",query.GetPath().Data()));
889 return dynamic_cast<AliCDBId*> (entry->GetId().Clone());
892 // Entry is not in cache -> retrieve it from CDB and cache it!!
893 AliCDBStorage *aStorage=0;
894 AliCDBParam *aPar=SelectSpecificStorage(query.GetPath());
897 aStorage=GetStorage(aPar);
898 TString str = aPar->GetURI();
899 AliDebug(2,Form("Looking into storage: %s",str.Data()));
902 aStorage=GetDefaultStorage();
903 AliDebug(2,"Looking into default storage");
906 return aStorage->GetId(query);
910 //_____________________________________________________________________________
911 TList* AliCDBManager::GetAll(const AliCDBPath& path, Int_t runNumber,
912 Int_t version, Int_t subVersion) {
913 // get multiple AliCDBEntry objects from the database
916 // RunNumber is not specified. Try with fRun
918 AliError("Run number neither specified in query nor set in AliCDBManager! Use AliCDBManager::SetRun.");
924 return GetAll(AliCDBId(path, runNumber, runNumber, version,
928 //_____________________________________________________________________________
929 TList* AliCDBManager::GetAll(const AliCDBPath& path,
930 const AliCDBRunRange& runRange, Int_t version, Int_t subVersion) {
931 // get multiple AliCDBEntry objects from the database
933 return GetAll(AliCDBId(path, runRange, version, subVersion));
936 //_____________________________________________________________________________
937 TList* AliCDBManager::GetAll(const AliCDBId& query) {
938 // get multiple AliCDBEntry objects from the database
939 // Warning: this method works correctly only for queries of the type "Detector/*"
940 // and not for more specific queries e.g. "Detector/Calib/*" !
941 // Warning #2: Entries are cached, but GetAll will keep on retrieving objects from OCDB!
942 // To get an object from cache use Get() function
944 if(!fDefaultStorage) {
945 AliError("No storage set!");
949 if (!query.IsValid()) {
950 AliError(Form("Invalid query: %s", query.ToString().Data()));
954 if((fSpecificStorages.GetEntries()>0) && query.GetPath().BeginsWith('*')){
955 // if specific storages are active a query with "*" is ambiguous
956 AliError("Query too generic in this context!");
960 if (query.IsAnyRange()) {
961 AliError(Form("Unspecified run or runrange: %s",
962 query.ToString().Data()));
966 if(fLock && query.GetFirstRun() != fRun)
967 AliFatal("Lock is ON: cannot use different run number than the internal one!");
969 AliCDBParam *aPar=SelectSpecificStorage(query.GetPath());
971 AliCDBStorage *aStorage;
973 aStorage=GetStorage(aPar);
974 AliDebug(2,Form("Looking into storage: %s", aPar->GetURI().Data()));
977 aStorage=GetDefaultStorage();
978 AliDebug(2,"Looking into default storage");
982 if(aStorage) result = aStorage->GetAll(query);
983 if(!result) return 0;
985 // loop on result to check whether entries should be re-queried with specific storages
986 if(fSpecificStorages.GetEntries()>0 && ! (fSpecificStorages.GetEntries() == 1 && aPar)) {
987 AliInfo("Now look into all other specific storages...");
990 AliCDBEntry* chkEntry=0;
992 while((chkEntry = dynamic_cast<AliCDBEntry*> (iter.Next()))){
993 AliCDBId& chkId = chkEntry->GetId();
994 AliDebug(2, Form("Checking id %s ", chkId.GetPath().Data()));
995 AliCDBParam *chkPar=SelectSpecificStorage(chkId.GetPath());
996 if (!chkPar || aPar == chkPar) continue;
997 AliCDBStorage *chkStorage = GetStorage(chkPar);
998 AliDebug(2, Form("Found specific storage! %s", chkPar->GetURI().Data()));
1000 AliCDBEntry *newEntry=0;
1001 chkId.SetRunRange(query.GetFirstRun(), query.GetLastRun());
1002 chkId.SetVersion(query.GetVersion());
1003 chkId.SetSubVersion(query.GetSubVersion());
1005 if(chkStorage) newEntry = chkStorage->Get(chkId);
1006 if(!newEntry) continue;
1008 // object is found in specific storage: replace entry in the result list!
1009 chkEntry->SetOwner(1);
1010 delete result->Remove(chkEntry);
1011 result->AddFirst(newEntry);
1014 Int_t nEntries = result->GetEntries();
1015 AliInfo("After look into other specific storages, result list is:");
1016 for(int i=0; i<nEntries;i++){
1017 AliCDBEntry *entry = (AliCDBEntry*) result->At(i);
1018 AliInfo(Form("%s",entry->GetId().ToString().Data()));
1024 AliCDBEntry* entry=0;
1025 while((entry = dynamic_cast<AliCDBEntry*> (iter.Next()))){
1027 if(!fIds->Contains(&entry->GetId())){
1028 fIds->Add(entry->GetId().Clone());
1030 if(fCache && (query.GetFirstRun() == fRun)){
1031 CacheEntry(entry->GetId().GetPath(), entry);
1039 //_____________________________________________________________________________
1040 Bool_t AliCDBManager::Put(TObject* object, const AliCDBId& id, AliCDBMetaData* metaData, const DataType type){
1041 // store an AliCDBEntry object into the database
1044 AliError("Null Entry! No storage will be done!");
1048 AliCDBEntry anEntry(object, id, metaData);
1049 return Put(&anEntry, type);
1054 //_____________________________________________________________________________
1055 Bool_t AliCDBManager::Put(AliCDBEntry* entry, DataType type){
1056 // store an AliCDBEntry object into the database
1058 if(type == kPrivate && !fDefaultStorage) {
1059 AliError("No storage set!");
1064 AliError("No entry!");
1068 if (entry->GetObject()==0x0){
1069 AliError("No valid object in CDB entry!");
1073 if (!entry->GetId().IsValid()) {
1074 AliError(Form("Invalid entry ID: %s",
1075 entry->GetId().ToString().Data()));
1079 if (!entry->GetId().IsSpecified()) {
1080 AliError(Form("Unspecified entry ID: %s",
1081 entry->GetId().ToString().Data()));
1085 AliCDBId id = entry->GetId();
1086 AliCDBParam *aPar = SelectSpecificStorage(id.GetPath());
1088 AliCDBStorage *aStorage=0;
1091 aStorage=GetStorage(aPar);
1095 aStorage = GetStorage(fCondParam);
1098 aStorage = GetStorage(fRefParam);
1101 aStorage = GetDefaultStorage();
1106 AliDebug(2,Form("Storing object into storage: %s", aStorage->GetURI().Data()));
1108 Bool_t result = aStorage->Put(entry, type);
1110 if(fRun >= 0) QueryCDB();
1117 //_____________________________________________________________________________
1118 void AliCDBManager::CacheEntry(const char* path, AliCDBEntry* entry)
1120 // cache AliCDBEntry. Cache is valid until run number is changed.
1122 AliCDBEntry *chkEntry = dynamic_cast<AliCDBEntry*> (fEntryCache.GetValue(path));
1125 AliDebug(2, Form("Object %s already in cache !!", path));
1128 AliDebug(2,Form("Caching entry %s", path));
1131 fEntryCache.Add(new TObjString(path), entry);
1132 AliDebug(2,Form("Cache entries: %d", fEntryCache.GetEntries()));
1136 //_____________________________________________________________________________
1137 void AliCDBManager::Print(Option_t* /*option*/) const
1139 // Print list of active storages and their URIs
1141 TString output=Form("Run number = %d; ",fRun);
1142 output += "Cache is ";
1143 if(!fCache) output += "NOT ";
1144 output += Form("ACTIVE; Number of active storages: %d\n",fActiveStorages.GetEntries());
1146 if(fDefaultStorage) {
1147 output += Form("\t*** Default Storage URI: \"%s\"\n",fDefaultStorage->GetURI().Data());
1148 // AliInfo(output.Data());
1150 if(fSpecificStorages.GetEntries()>0) {
1151 TIter iter(fSpecificStorages.GetTable());
1154 while((aPair = (TPair*) iter.Next())){
1155 output += Form("\t*** Specific storage %d: Path \"%s\" -> URI \"%s\"\n",
1156 i++, ((TObjString*) aPair->Key())->GetName(),
1157 ((AliCDBParam*) aPair->Value())->GetURI().Data());
1161 output += Form("*** Drain Storage URI: %s\n",fDrainStorage->GetURI().Data());
1163 AliInfo(output.Data());
1166 //_____________________________________________________________________________
1167 void AliCDBManager::SetRun(Int_t run)
1169 // Sets current run number.
1170 // When the run number changes the caching is cleared.
1175 if(fLock && fRun >= 0) {
1176 AliFatal("Lock is ON, cannot reset run number!");
1181 // here the LHCPeriod xml file is parsed; the string containing the correct period is returned; the default storage is set
1182 if (fStartRunLHCPeriod <= run && fEndRunLHCPeriod >= run){
1183 AliInfo("LHCPeriod alien folder for current run already in memory");
1185 SetDefaultStorageFromRun(run);
1186 if(fEntryCache.GetEntries()!=0) ClearCache();
1194 //_____________________________________________________________________________
1195 void AliCDBManager::ClearCache(){
1196 // clear AliCDBEntry cache
1198 AliDebug(2, Form("Cache entries to be deleted: %d",fEntryCache.GetEntries()));
1201 // To clean entries one by one
1202 TIter iter(fEntryCache.GetTable());
1204 while((pair= dynamic_cast<TPair*> (iter.Next()))){
1206 TObjString* key = dynamic_cast<TObjString*> (pair->Key());
1207 AliCDBEntry* entry = dynamic_cast<AliCDBEntry*> (pair->Value());
1208 AliDebug(2, Form("Deleting entry: %s", key->GetName()));
1209 if (entry) delete entry;
1210 delete fEntryCache.Remove(key);
1213 fEntryCache.DeleteAll();
1214 AliDebug(2, Form("After deleting - Cache entries: %d",fEntryCache.GetEntries()));
1217 //_____________________________________________________________________________
1218 void AliCDBManager::UnloadFromCache(const char* path){
1219 // unload cached object
1221 if(!fActiveStorages.GetEntries()) {
1222 AliDebug(2, Form("No active storages. Object \"%s\" is not unloaded from cache", path));
1226 AliCDBPath queryPath(path);
1227 if(!queryPath.IsValid()) return;
1229 if(!queryPath.IsWildcard()) { // path is not wildcard, get it directly from the cache and unload it!
1230 if(fEntryCache.Contains(path)){
1231 AliDebug(2, Form("Unloading object \"%s\" from cache", path));
1232 TObjString pathStr(path);
1233 delete fEntryCache.Remove(&pathStr);
1235 AliErrorF("Cache does not contain object \"%s\"!", path);
1237 AliDebugF(2, "Cache entries: %d",fEntryCache.GetEntries());
1241 // path is wildcard: loop on the cache and unload all comprised objects!
1242 TIter iter(fEntryCache.GetTable());
1245 while((pair = dynamic_cast<TPair*> (iter.Next()))){
1246 AliCDBPath entryPath = pair->Key()->GetName();
1247 if(queryPath.Comprises(entryPath)) {
1248 AliDebug(2, Form("Unloading object \"%s\" from cache", entryPath.GetPath().Data()));
1249 TObjString pathStr(entryPath.GetPath().Data());
1250 delete fEntryCache.Remove(&pathStr);
1253 AliDebug(2,Form("Cache entries: %d",fEntryCache.GetEntries()));
1256 //_____________________________________________________________________________
1257 void AliCDBManager::DestroyActiveStorages() {
1258 // delete list of active storages
1260 fActiveStorages.DeleteAll();
1261 fSpecificStorages.DeleteAll();
1264 //_____________________________________________________________________________
1265 void AliCDBManager::DestroyActiveStorage(AliCDBStorage* /*storage*/) {
1266 // destroys active storage
1269 TIter iter(fActiveStorages.GetTable());
1271 while ((aPair = (TPair*) iter.Next())) {
1272 if(storage == (AliCDBStorage*) aPair->Value())
1273 delete fActiveStorages.Remove(aPair->Key());
1274 storage->Delete(); storage=0x0;
1280 //_____________________________________________________________________________
1281 void AliCDBManager::QueryCDB() {
1282 // query default and specific storages for files valid for fRun. Every storage loads the Ids into its list.
1285 AliError("Run number not yet set! Use AliCDBManager::SetRun.");
1288 if (!fDefaultStorage){
1289 AliError("Default storage is not set! Use AliCDBManager::SetDefaultStorage");
1292 if(fDefaultStorage->GetType() == "alien"){
1293 fDefaultStorage->QueryCDB(fRun);
1295 AliDebug(2,"Skipping query for valid files, it used only in grid...");
1298 TIter iter(&fSpecificStorages);
1299 TObjString *aCalibType=0;
1300 AliCDBParam* aPar=0;
1301 while((aCalibType = dynamic_cast<TObjString*> (iter.Next()))){
1302 aPar = (AliCDBParam*) fSpecificStorages.GetValue(aCalibType);
1304 AliDebug(2,Form("Querying specific storage %s",aCalibType->GetName()));
1305 AliCDBStorage *aStorage = GetStorage(aPar);
1306 if(aStorage->GetType() == "alien"){
1307 aStorage->QueryCDB(fRun,aCalibType->GetName());
1310 "Skipping query for valid files, it is used only in grid...");
1316 //______________________________________________________________________________________________
1317 const char* AliCDBManager::GetDataTypeName(DataType type)
1319 // returns the name (string) of the data type
1322 case kCondition: return "Conditions";
1323 case kReference: return "Reference";
1324 case kPrivate: return "Private";
1330 //______________________________________________________________________________________________
1331 void AliCDBManager::InitShortLived()
1333 // Init the list of short-lived objects
1334 // currently disabled
1338 // fShortLived = new TList();
1339 // fShortLived->SetOwner(1);
1341 // fShortLived->Add(new TObjString("EMCAL/Calib/Data"));
1343 // fShortLived->Add(new TObjString("HMPID/Calib/Nmean"));
1344 // fShortLived->Add(new TObjString("HMPID/Calib/Qthre"));
1346 // fShortLived->Add(new TObjString("ITS/Calib/CalibSPD"));
1348 // fShortLived->Add(new TObjString("MUON/Calib/Gains"));
1349 // fShortLived->Add(new TObjString("MUON/Calib/HV"));
1350 // fShortLived->Add(new TObjString("MUON/Calib/Pedestals"));
1352 // fShortLived->Add(new TObjString("PHOS/Calib/CpvGainPedestals"));
1353 // fShortLived->Add(new TObjString("PHOS/Calib/EmcGainPedestals"));
1355 // fShortLived->Add(new TObjString("PMD/Calib/Data"));
1357 // fShortLived->Add(new TObjString("TRD/Calib/ChamberGainFactor"));
1358 // fShortLived->Add(new TObjString("TRD/Calib/LocalGainFactor"));
1359 // fShortLived->Add(new TObjString("TRD/Calib/ChamberT0"));
1360 // fShortLived->Add(new TObjString("TRD/Calib/LocalT0"));
1361 // fShortLived->Add(new TObjString("TRD/Calib/ChamberVdrift"));
1362 // fShortLived->Add(new TObjString("TRD/Calib/LocalVdrift"));
1364 // fShortLived->Add(new TObjString("ZDC/Calib/Data"));
1368 //______________________________________________________________________________________________
1369 Bool_t AliCDBManager::IsShortLived(const char* path)
1371 // returns the name (string) of the data type
1373 if(!fShortLived) return kFALSE;
1375 AliCDBPath aPath(path);
1376 if(!aPath.IsValid()){
1377 AliError(Form("Not a valid path: %s", path));
1381 return fShortLived->Contains(path);
1385 //______________________________________________________________________________________________
1386 ULong_t AliCDBManager::SetLock(Bool_t lock, ULong_t key){
1387 // To lock/unlock user must provide the key. A new key is provided after
1388 // each successful lock. User should always backup the returned key and
1389 // use it on next access.
1390 if (fLock == lock) return 0; // nothing to be done
1392 // User wants to lock - check his identity
1394 // Lock has a user - check his key
1396 AliFatal("Wrong key provided to lock CDB. Please remove CDB lock access from your code !");
1401 fKey = gSystem->Now();
1405 // User wants to unlock - check the provided key
1407 AliFatal("Lock is ON: wrong key provided");
1414 ///////////////////////////////////////////////////////////
1415 // AliCDBManager Parameter class //
1416 // interface to specific AliCDBParameter class //
1417 // (AliCDBGridParam, AliCDBLocalParam, AliCDBDumpParam) //
1418 ///////////////////////////////////////////////////////////
1420 AliCDBParam::AliCDBParam():
1428 //_____________________________________________________________________________
1429 AliCDBParam::~AliCDBParam() {