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){
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());
121 f->WriteObject(&fEntryCache,"CDBentriesMap");
122 f->WriteObject(fIds,"CDBidsList");
129 //_____________________________________________________________________________
130 Bool_t AliCDBManager::InitFromSnapshot(const char* snapshotFileName, Bool_t overwrite){
131 // initialize manager from a CDB snapshot, that is add the entries
132 // to the entries map and the ids to the ids list taking them from
133 // the map and the list found in the input file
136 TString snapshotFile(snapshotFileName);
137 if(snapshotFile.BeginsWith("alien://")){
139 TGrid::Connect("alien://","");
141 AliError("Connection to alien failed!");
147 TFile *f = TFile::Open(snapshotFileName);
148 if (!f || f->IsZombie()){
149 AliError(Form("Cannot open file %s",snapshotFileName));
153 // retrieve entries' map from snapshot file
154 TMap *entriesMap = 0;
155 TIter next(f->GetListOfKeys());
157 while ((key = (TKey*)next())) {
158 if (strcmp(key->GetClassName(),"TMap") != 0) continue;
159 entriesMap = (TMap*)key->ReadObj();
162 if (!entriesMap || entriesMap->GetEntries()==0){
163 AliError("Cannot get valid map of CDB entries from snapshot file");
167 // retrieve ids' list from snapshot file
169 TIter nextKey(f->GetListOfKeys());
171 while ((keyN = (TKey*)nextKey())) {
172 if (strcmp(keyN->GetClassName(),"TList") != 0) continue;
173 idsList = (TList*)keyN->ReadObj();
176 if (!idsList || idsList->GetEntries()==0){
177 AliError("Cannot get valid list of CDB entries from snapshot file");
181 // Add each (entry,id) from the snapshot to the memory: entry to the cache, id to the list of ids.
182 // If "overwrite" is false: add the entry to the cache and its id to the list of ids
183 // only if neither of them is already there.
184 // If "overwrite" is true: write the snapshot entry,id in any case. If something
185 // was already there for that calibration type, remove it and issue a warning
186 TIter iterObj(entriesMap->GetTable());
189 while((pair = dynamic_cast<TPair*> (iterObj.Next()))){
190 TObjString* os = (TObjString*) pair->Key();
191 TString path = os->GetString();
192 TIter iterId(idsList);
194 AliCDBId* correspondingId=0;
195 while((id = dynamic_cast<AliCDBId*> (iterId.Next()))){
196 TString idpath(id->GetPath());
202 if(!correspondingId){
203 AliError(Form("id for \"%s\" not found in the snapshot (while entry was). This entry is skipped!",path.Data()));
206 Bool_t cached = fEntryCache.Contains(path.Data());
207 Bool_t registeredId = kFALSE;
210 while((idT = dynamic_cast<AliCDBId*> (iter.Next()))){
211 if(idT->GetPath()==path){
212 registeredId = kTRUE;
218 if(cached || registeredId){
219 AliWarning(Form("An entry was already cached for \"%s\". Removing it before caching from snapshot",path.Data()));
220 UnloadFromCache(path.Data());
222 fEntryCache.Add(pair->Key(),pair->Value());
226 if(cached || registeredId){
227 AliWarning(Form("An entry was already cached for \"%s\". Not adding this object from snapshot",path.Data()));
229 fEntryCache.Add(pair->Key(),pair->Value());
236 // fEntry is the new owner of the cache
237 fEntryCache.SetOwnerKeyValue(kTRUE,kTRUE);
238 entriesMap->SetOwnerKeyValue(kFALSE,kFALSE);
239 fIds->SetOwner(kTRUE);
240 idsList->SetOwner(kFALSE);
241 AliInfo(Form("%d new (entry,id) cached. Total number %d",nAdded,fEntryCache.GetEntries()));
249 //_____________________________________________________________________________
250 void AliCDBManager::Destroy() {
251 // delete ALCDBManager instance and active storages
254 //fgInstance->Delete();
260 //_____________________________________________________________________________
261 AliCDBManager::AliCDBManager():
270 fDefaultStorage(NULL),
278 fStartRunLHCPeriod(-1),
279 fEndRunLHCPeriod(-1),
283 // default constuctor
284 fFactories.SetOwner(1);
285 fActiveStorages.SetOwner(1);
286 fSpecificStorages.SetOwner(1);
287 fEntryCache.SetName("CDBEntryCache");
288 fEntryCache.SetOwnerKeyValue(kTRUE,kTRUE);
290 fStorageMap = new TMap();
291 fStorageMap->SetOwner(1);
296 //_____________________________________________________________________________
297 AliCDBManager::~AliCDBManager() {
300 DestroyActiveStorages();
303 fDefaultStorage = 0x0;
304 delete fStorageMap; fStorageMap = 0;
305 delete fIds; fIds = 0;
308 delete fShortLived; fShortLived = 0x0;
311 //_____________________________________________________________________________
312 void AliCDBManager::PutActiveStorage(AliCDBParam* param, AliCDBStorage* storage){
313 // put a storage object into the list of active storages
315 fActiveStorages.Add(param, storage);
316 AliDebug(1, Form("Active storages: %d", fActiveStorages.GetEntries()));
319 //_____________________________________________________________________________
320 void AliCDBManager::RegisterFactory(AliCDBStorageFactory* factory) {
321 // add a storage factory to the list of registerd factories
323 if (!fFactories.Contains(factory)) {
324 fFactories.Add(factory);
328 //_____________________________________________________________________________
329 Bool_t AliCDBManager::HasStorage(const char* dbString) const {
330 // check if dbString is a URI valid for one of the registered factories
332 TIter iter(&fFactories);
334 AliCDBStorageFactory* factory=0;
335 while ((factory = (AliCDBStorageFactory*) iter.Next())) {
337 if (factory->Validate(dbString)) {
345 //_____________________________________________________________________________
346 AliCDBParam* AliCDBManager::CreateParameter(const char* dbString) const {
347 // create AliCDBParam object from URI string
349 TIter iter(&fFactories);
351 AliCDBStorageFactory* factory=0;
352 while ((factory = (AliCDBStorageFactory*) iter.Next())) {
353 AliCDBParam* param = factory->CreateParameter(dbString);
354 if(param) return param;
360 //_____________________________________________________________________________
361 AliCDBStorage* AliCDBManager::GetStorage(const char* dbString) {
362 // get storage object from URI string
364 AliCDBParam* param = CreateParameter(dbString);
366 AliError(Form("Failed to activate requested storage! Check URI: %s", dbString));
370 AliCDBStorage* aStorage = GetStorage(param);
376 //_____________________________________________________________________________
377 AliCDBStorage* AliCDBManager::GetStorage(const AliCDBParam* param) {
378 // get storage object from AliCDBParam object
380 // if the list of active storages already contains
381 // the requested storage, return it
382 AliCDBStorage* aStorage = GetActiveStorage(param);
387 // if lock is ON, cannot activate more storages!
389 if (fDefaultStorage) {
390 AliFatal("Lock is ON, and default storage is already set: "
391 "cannot reset it or activate more storages!");
395 TIter iter(&fFactories);
397 AliCDBStorageFactory* factory=0;
399 // loop on the list of registered factories
400 while ((factory = (AliCDBStorageFactory*) iter.Next())) {
402 // each factory tries to create its storage from the parameter
403 aStorage = factory->Create(param);
405 PutActiveStorage(param->CloneParam(), aStorage);
406 aStorage->SetURI(param->GetURI());
408 if(aStorage->GetType() == "alien"){
409 aStorage->QueryCDB(fRun);
412 "Skipping query for valid files, it is used only in grid...");
419 AliError(Form("Failed to activate requested storage! Check URI: %s", param->GetURI().Data()));
424 //_____________________________________________________________________________
425 AliCDBStorage* AliCDBManager::GetActiveStorage(const AliCDBParam* param) {
426 // get a storage object from the list of active storages
428 return dynamic_cast<AliCDBStorage*> (fActiveStorages.GetValue(param));
431 //_____________________________________________________________________________
432 TList* AliCDBManager::GetActiveStorages() {
433 // return list of active storages
434 // user has responsibility to delete returned object
436 TList* result = new TList();
438 TIter iter(fActiveStorages.GetTable());
440 while ((aPair = (TPair*) iter.Next())) {
441 result->Add(aPair->Value());
447 //_____________________________________________________________________________
448 void AliCDBManager::SetDrain(const char* dbString) {
449 // set drain storage from URI string
451 fDrainStorage = GetStorage(dbString);
454 //_____________________________________________________________________________
455 void AliCDBManager::SetDrain(const AliCDBParam* param) {
456 // set drain storage from AliCDBParam
458 fDrainStorage = GetStorage(param);
461 //_____________________________________________________________________________
462 void AliCDBManager::SetDrain(AliCDBStorage* storage) {
463 // set drain storage from another active storage
465 fDrainStorage = storage;
468 //_____________________________________________________________________________
469 Bool_t AliCDBManager::Drain(AliCDBEntry *entry) {
470 // drain retrieved object to drain storage
472 AliDebug(2, "Draining into drain storage...");
473 return fDrainStorage->Put(entry);
476 //____________________________________________________________________________
477 void AliCDBManager::SetDefaultStorage(const char* dbString) {
478 // sets default storage from URI string
480 // checking whether we are in the raw case
481 TString dbStringTemp(dbString);
482 if (dbStringTemp == "raw://")
485 AliInfo("Setting the run-number will set the corresponding OCDB for raw data reconstruction.");
486 AliInfo("Connecting to the grid...");
488 TGrid::Connect("alien://","");
490 AliError("Connection to alien failed!");
497 AliCDBStorage* bckStorage = fDefaultStorage;
499 fDefaultStorage = GetStorage(dbString);
501 if(!fDefaultStorage) return;
503 if(bckStorage && (fDefaultStorage != bckStorage)){
504 AliWarning("Existing default storage replaced: clearing cache!");
508 if (fStorageMap->Contains("default")) {
509 delete fStorageMap->Remove(((TPair*)fStorageMap->FindObject("default"))->Key());
511 fStorageMap->Add(new TObjString("default"), new TObjString(fDefaultStorage->GetURI()));
513 //_____________________________________________________________________________
514 void AliCDBManager::SetDefaultStorage(const AliCDBParam* param) {
515 // set default storage from AliCDBParam object
517 AliCDBStorage* bckStorage = fDefaultStorage;
519 fDefaultStorage = GetStorage(param);
521 if(!fDefaultStorage) return;
523 if(bckStorage && (fDefaultStorage != bckStorage)){
524 AliWarning("Existing default storage replaced: clearing cache!");
528 if (fStorageMap->Contains("default")) {
529 delete fStorageMap->Remove(((TPair*)fStorageMap->FindObject("default"))->Key());
531 fStorageMap->Add(new TObjString("default"), new TObjString(fDefaultStorage->GetURI()));
534 //_____________________________________________________________________________
535 void AliCDBManager::SetDefaultStorage(AliCDBStorage* storage) {
536 // set default storage from another active storage
538 // if lock is ON, cannot activate more storages!
540 if (fDefaultStorage) {
541 AliFatal("Lock is ON, and default storage is already set: "
542 "cannot reset it or activate more storages!");
547 UnsetDefaultStorage();
551 AliCDBStorage* bckStorage = fDefaultStorage;
553 fDefaultStorage = storage;
555 if(bckStorage && (fDefaultStorage != bckStorage)){
556 AliWarning("Existing default storage replaced: clearing cache!");
560 if (fStorageMap->Contains("default")) {
561 delete fStorageMap->Remove(((TPair*)fStorageMap->FindObject("default"))->Key());
563 fStorageMap->Add(new TObjString("default"), new TObjString(fDefaultStorage->GetURI()));
566 //_____________________________________________________________________________
567 void AliCDBManager::SetDefaultStorage(const char* mcString, const char* simType) {
568 // sets default storage for MC data
569 // mcString MUST be "MC",
570 // simType can be "Ideal","Residual","Full"
572 TString strmcString(mcString);
573 TString strsimType(simType);
575 if (strmcString != "MC"){
576 AliFatal("Method requires first string to be MC!");
579 if (strsimType == "Ideal"){
580 dbString = fgkMCIdealStorage;
582 else if (strsimType == "Full"){
583 dbString = fgkMCFullStorage;
585 else if (strsimType == "Residual"){
586 dbString = fgkMCResidualStorage;
589 AliFatal("Error in setting the storage for MC data, second argument MUST be either \"Ideal\" or \"Full\" or \"Residual\".");
592 SetDefaultStorage(dbString.Data());
593 if(!fDefaultStorage) AliFatal(Form("%s storage not there! Please check!",fLHCPeriod.Data()));
596 //_____________________________________________________________________________
597 void AliCDBManager::SetDefaultStorageFromRun(Int_t run) {
598 // set default storage from the run number - to be used only with raw data
600 // if lock is ON, cannot activate more storages!
602 if (fDefaultStorage) {
603 AliFatal("Lock is ON, and default storage is already set: "
604 "cannot activate default storage from run number");
608 // retrieve XML file from alien
610 TGrid::Connect("alien://","");
612 AliError("Connection to alien failed!");
617 TString rndname = "/tmp/";
618 rndname += "OCDBFolderXML.";
619 rndname += uuid.AsString();
621 AliDebug(2, Form("file to be copied = %s", fgkOCDBFolderXMLfile.Data()));
622 if (!TFile::Cp(fgkOCDBFolderXMLfile.Data(), rndname.Data())) {
623 AliFatal(Form("Cannot make a local copy of OCDBFolder xml file in %s",rndname.Data()));
625 AliCDBHandler* saxcdb = new AliCDBHandler();
627 TSAXParser *saxParser = new TSAXParser();
628 saxParser->ConnectToHandler("AliCDBHandler", saxcdb);
629 saxParser->ParseFile(rndname.Data());
630 AliInfo(Form(" LHC folder = %s", saxcdb->GetOCDBFolder().Data()));
631 AliInfo(Form(" LHC period start run = %d", saxcdb->GetStartRunRange()));
632 AliInfo(Form(" LHC period end run = %d", saxcdb->GetEndRunRange()));
633 fLHCPeriod = saxcdb->GetOCDBFolder();
634 fStartRunLHCPeriod = saxcdb->GetStartRunRange();
635 fEndRunLHCPeriod = saxcdb->GetEndRunRange();
637 SetDefaultStorage(fLHCPeriod.Data());
638 if(!fDefaultStorage) AliFatal(Form("%s storage not there! Please check!",fLHCPeriod.Data()));
642 //_____________________________________________________________________________
643 void AliCDBManager::UnsetDefaultStorage() {
644 // Unset default storage
646 // if lock is ON, action is forbidden!
648 if (fDefaultStorage) {
649 AliFatal("Lock is ON: cannot unset default storage!");
653 if (fDefaultStorage) {
654 AliWarning("Clearing cache!");
658 fRun = fStartRunLHCPeriod = fEndRunLHCPeriod = -1;
661 fDefaultStorage = 0x0;
664 //_____________________________________________________________________________
665 void AliCDBManager::SetSpecificStorage(const char* calibType, const char* dbString) {
666 // sets storage specific for detector or calibration type (works with AliCDBManager::Get(...))
668 AliCDBParam *aPar = CreateParameter(dbString);
670 SetSpecificStorage(calibType, aPar);
674 //_____________________________________________________________________________
675 void AliCDBManager::SetSpecificStorage(const char* calibType, const AliCDBParam* param) {
676 // sets storage specific for detector or calibration type (works with AliCDBManager::Get(...))
677 // Default storage should be defined prior to any specific storages, e.g.:
678 // AliCDBManager::instance()->SetDefaultStorage("alien://");
679 // AliCDBManager::instance()->SetSpecificStorage("TPC/*","local://DB_TPC");
680 // AliCDBManager::instance()->SetSpecificStorage("*/Align/*","local://DB_TPCAlign");
681 // calibType must be a valid CDB path! (3 level folder structure)
684 if(!fDefaultStorage && !fRaw) {
685 AliError("Please activate a default storage first!");
690 AliCDBPath aPath(calibType);
691 if(!aPath.IsValid()){
692 AliError(Form("Not a valid path: %s", calibType));
696 TObjString *objCalibType = new TObjString(aPath.GetPath());
697 if(fSpecificStorages.Contains(objCalibType)){
698 AliWarning(Form("Storage \"%s\" already activated! It will be replaced by the new one",
700 AliCDBParam *checkPar = dynamic_cast<AliCDBParam*> (fSpecificStorages.GetValue(calibType));
701 if(checkPar) delete checkPar;
702 delete fSpecificStorages.Remove(objCalibType);
704 AliCDBStorage *aStorage = GetStorage(param);
705 if(!aStorage) return;
707 fSpecificStorages.Add(objCalibType, param->CloneParam());
709 if(fStorageMap->Contains(objCalibType)){
710 delete fStorageMap->Remove(objCalibType);
712 fStorageMap->Add(objCalibType->Clone(), new TObjString(param->GetURI()));
716 //_____________________________________________________________________________
717 AliCDBStorage* AliCDBManager::GetSpecificStorage(const char* calibType) {
718 // get storage specific for detector or calibration type
720 AliCDBPath calibPath(calibType);
721 if(!calibPath.IsValid()) return NULL;
723 AliCDBParam *checkPar = (AliCDBParam*) fSpecificStorages.GetValue(calibPath.GetPath());
725 AliError(Form("%s storage not found!", calibType));
728 return GetStorage(checkPar);
733 //_____________________________________________________________________________
734 AliCDBParam* AliCDBManager::SelectSpecificStorage(const TString& path) {
735 // select storage valid for path from the list of specific storages
737 AliCDBPath aPath(path);
738 if(!aPath.IsValid()) return NULL;
740 TIter iter(&fSpecificStorages);
741 TObjString *aCalibType=0;
742 AliCDBPath tmpPath("null/null/null");
744 while((aCalibType = (TObjString*) iter.Next())){
745 AliCDBPath calibTypePath(aCalibType->GetName());
746 if(calibTypePath.Comprises(aPath)) {
747 if(calibTypePath.Comprises(tmpPath)) continue;
748 aPar = (AliCDBParam*) fSpecificStorages.GetValue(aCalibType);
749 tmpPath.SetPath(calibTypePath.GetPath());
755 //_____________________________________________________________________________
756 AliCDBEntry* AliCDBManager::Get(const AliCDBPath& path, Int_t runNumber,
757 Int_t version, Int_t subVersion) {
758 // get an AliCDBEntry object from the database
761 // RunNumber is not specified. Try with fRun
763 AliError("Run number neither specified in query nor set in AliCDBManager! Use AliCDBManager::SetRun.");
769 return Get(AliCDBId(path, runNumber, runNumber, version, subVersion));
772 //_____________________________________________________________________________
773 AliCDBEntry* AliCDBManager::Get(const AliCDBPath& path,
774 const AliCDBRunRange& runRange, Int_t version,
776 // get an AliCDBEntry object from the database!
778 return Get(AliCDBId(path, runRange, version, subVersion));
781 //_____________________________________________________________________________
782 AliCDBEntry* AliCDBManager::Get(const AliCDBId& query) {
783 // get an AliCDBEntry object from the database
785 // check if query's path and runRange are valid
786 // query is invalid also if version is not specified and subversion is!
787 if (!query.IsValid()) {
788 AliError(Form("Invalid query: %s", query.ToString().Data()));
792 // query is not specified if path contains wildcard or run range= [-1,-1]
793 if (!query.IsSpecified()) {
794 AliError(Form("Unspecified query: %s",
795 query.ToString().Data()));
799 if(fLock && !(fRun >= query.GetFirstRun() && fRun <= query.GetLastRun()))
800 AliFatal("Lock is ON: cannot use different run number than the internal one!");
802 if(fCache && !(fRun >= query.GetFirstRun() && fRun <= query.GetLastRun()))
803 AliWarning("Run number explicitly set in query: CDB cache temporarily disabled!");
805 AliCDBEntry *entry=0;
807 // first look into map of cached objects
808 if(fCache && query.GetFirstRun() == fRun)
809 entry = (AliCDBEntry*) fEntryCache.GetValue(query.GetPath());
812 AliDebug(2, Form("Object %s retrieved from cache !!",query.GetPath().Data()));
816 if(!fDefaultStorage) {
817 AliError("No storage set!");
820 // Entry is not in cache -> retrieve it from CDB and cache it!!
821 AliCDBStorage *aStorage=0;
822 AliCDBParam *aPar=SelectSpecificStorage(query.GetPath());
823 // Bool_t usedDefStorage=kTRUE;
826 aStorage=GetStorage(aPar);
827 TString str = aPar->GetURI();
828 AliDebug(2,Form("Looking into storage: %s",str.Data()));
829 // usedDefStorage=kFALSE;
832 aStorage=GetDefaultStorage();
833 AliDebug(2,"Looking into default storage");
836 entry = aStorage->Get(query);
838 if(entry && fCache && (query.GetFirstRun() == fRun)){
839 CacheEntry(query.GetPath(), entry);
842 if(entry && !fIds->Contains(&entry->GetId())){
843 fIds->Add(entry->GetId().Clone());
851 //_____________________________________________________________________________
852 const char* AliCDBManager::GetURI(const char* path) {
853 // return the URI of the storage where to look for path
855 if(!IsDefaultStorageSet()) return 0;
857 AliCDBParam *aPar=SelectSpecificStorage(path);
860 return aPar->GetURI().Data();
863 return GetDefaultStorage()->GetURI().Data();
869 //_____________________________________________________________________________
870 AliCDBId* AliCDBManager::GetId(const AliCDBPath& path, Int_t runNumber,
871 Int_t version, Int_t subVersion) {
872 // get the AliCDBId of the valid object from the database (does not retrieve the object)
873 // User must delete returned object!
876 // RunNumber is not specified. Try with fRun
878 AliError("Run number neither specified in query nor set in AliCDBManager! Use AliCDBManager::SetRun.");
884 return GetId(AliCDBId(path, runNumber, runNumber, version, subVersion));
887 //_____________________________________________________________________________
888 AliCDBId* AliCDBManager::GetId(const AliCDBPath& path,
889 const AliCDBRunRange& runRange, Int_t version,
891 // get the AliCDBId of the valid object from the database (does not retrieve the object)
892 // User must delete returned object!
894 return GetId(AliCDBId(path, runRange, version, subVersion));
897 //_____________________________________________________________________________
898 AliCDBId* AliCDBManager::GetId(const AliCDBId& query) {
899 // get the AliCDBId of the valid object from the database (does not retrieve the object)
900 // User must delete returned object!
902 if(!fDefaultStorage) {
903 AliError("No storage set!");
907 // check if query's path and runRange are valid
908 // query is invalid also if version is not specified and subversion is!
909 if (!query.IsValid()) {
910 AliError(Form("Invalid query: %s", query.ToString().Data()));
914 // query is not specified if path contains wildcard or run range= [-1,-1]
915 if (!query.IsSpecified()) {
916 AliError(Form("Unspecified query: %s",
917 query.ToString().Data()));
921 if(fCache && query.GetFirstRun() != fRun)
922 AliWarning("Run number explicitly set in query: CDB cache temporarily disabled!");
924 AliCDBEntry* entry = 0;
926 // first look into map of cached objects
927 if(fCache && query.GetFirstRun() == fRun)
928 entry = (AliCDBEntry*) fEntryCache.GetValue(query.GetPath());
931 AliDebug(2, Form("Object %s retrieved from cache !!",query.GetPath().Data()));
932 return dynamic_cast<AliCDBId*> (entry->GetId().Clone());
935 // Entry is not in cache -> retrieve it from CDB and cache it!!
936 AliCDBStorage *aStorage=0;
937 AliCDBParam *aPar=SelectSpecificStorage(query.GetPath());
940 aStorage=GetStorage(aPar);
941 TString str = aPar->GetURI();
942 AliDebug(2,Form("Looking into storage: %s",str.Data()));
945 aStorage=GetDefaultStorage();
946 AliDebug(2,"Looking into default storage");
949 return aStorage->GetId(query);
953 //_____________________________________________________________________________
954 TList* AliCDBManager::GetAll(const AliCDBPath& path, Int_t runNumber,
955 Int_t version, Int_t subVersion) {
956 // get multiple AliCDBEntry objects from the database
959 // RunNumber is not specified. Try with fRun
961 AliError("Run number neither specified in query nor set in AliCDBManager! Use AliCDBManager::SetRun.");
967 return GetAll(AliCDBId(path, runNumber, runNumber, version,
971 //_____________________________________________________________________________
972 TList* AliCDBManager::GetAll(const AliCDBPath& path,
973 const AliCDBRunRange& runRange, Int_t version, Int_t subVersion) {
974 // get multiple AliCDBEntry objects from the database
976 return GetAll(AliCDBId(path, runRange, version, subVersion));
979 //_____________________________________________________________________________
980 TList* AliCDBManager::GetAll(const AliCDBId& query) {
981 // get multiple AliCDBEntry objects from the database
982 // Warning: this method works correctly only for queries of the type "Detector/*"
983 // and not for more specific queries e.g. "Detector/Calib/*" !
984 // Warning #2: Entries are cached, but GetAll will keep on retrieving objects from OCDB!
985 // To get an object from cache use Get() function
987 if(!fDefaultStorage) {
988 AliError("No storage set!");
992 if (!query.IsValid()) {
993 AliError(Form("Invalid query: %s", query.ToString().Data()));
997 if((fSpecificStorages.GetEntries()>0) && query.GetPath().BeginsWith('*')){
998 // if specific storages are active a query with "*" is ambiguous
999 AliError("Query too generic in this context!");
1003 if (query.IsAnyRange()) {
1004 AliError(Form("Unspecified run or runrange: %s",
1005 query.ToString().Data()));
1009 if(fLock && query.GetFirstRun() != fRun)
1010 AliFatal("Lock is ON: cannot use different run number than the internal one!");
1012 AliCDBParam *aPar=SelectSpecificStorage(query.GetPath());
1014 AliCDBStorage *aStorage;
1016 aStorage=GetStorage(aPar);
1017 AliDebug(2,Form("Looking into storage: %s", aPar->GetURI().Data()));
1020 aStorage=GetDefaultStorage();
1021 AliDebug(2,"Looking into default storage");
1025 if(aStorage) result = aStorage->GetAll(query);
1026 if(!result) return 0;
1028 // loop on result to check whether entries should be re-queried with specific storages
1029 if(fSpecificStorages.GetEntries()>0 && ! (fSpecificStorages.GetEntries() == 1 && aPar)) {
1030 AliInfo("Now look into all other specific storages...");
1033 AliCDBEntry* chkEntry=0;
1035 while((chkEntry = dynamic_cast<AliCDBEntry*> (iter.Next()))){
1036 AliCDBId& chkId = chkEntry->GetId();
1037 AliDebug(2, Form("Checking id %s ", chkId.GetPath().Data()));
1038 AliCDBParam *chkPar=SelectSpecificStorage(chkId.GetPath());
1039 if (!chkPar || aPar == chkPar) continue;
1040 AliCDBStorage *chkStorage = GetStorage(chkPar);
1041 AliDebug(2, Form("Found specific storage! %s", chkPar->GetURI().Data()));
1043 AliCDBEntry *newEntry=0;
1044 chkId.SetRunRange(query.GetFirstRun(), query.GetLastRun());
1045 chkId.SetVersion(query.GetVersion());
1046 chkId.SetSubVersion(query.GetSubVersion());
1048 if(chkStorage) newEntry = chkStorage->Get(chkId);
1049 if(!newEntry) continue;
1051 // object is found in specific storage: replace entry in the result list!
1052 chkEntry->SetOwner(1);
1053 delete result->Remove(chkEntry);
1054 result->AddFirst(newEntry);
1057 Int_t nEntries = result->GetEntries();
1058 AliInfo("After look into other specific storages, result list is:");
1059 for(int i=0; i<nEntries;i++){
1060 AliCDBEntry *entry = (AliCDBEntry*) result->At(i);
1061 AliInfo(Form("%s",entry->GetId().ToString().Data()));
1067 AliCDBEntry* entry=0;
1068 while((entry = dynamic_cast<AliCDBEntry*> (iter.Next()))){
1070 if(!fIds->Contains(&entry->GetId())){
1071 fIds->Add(entry->GetId().Clone());
1073 if(fCache && (query.GetFirstRun() == fRun)){
1074 CacheEntry(entry->GetId().GetPath(), entry);
1082 //_____________________________________________________________________________
1083 Bool_t AliCDBManager::Put(TObject* object, const AliCDBId& id, AliCDBMetaData* metaData, const DataType type){
1084 // store an AliCDBEntry object into the database
1087 AliError("Null Entry! No storage will be done!");
1091 AliCDBEntry anEntry(object, id, metaData);
1092 return Put(&anEntry, type);
1097 //_____________________________________________________________________________
1098 Bool_t AliCDBManager::Put(AliCDBEntry* entry, DataType type){
1099 // store an AliCDBEntry object into the database
1101 if(type == kPrivate && !fDefaultStorage) {
1102 AliError("No storage set!");
1107 AliError("No entry!");
1111 if (entry->GetObject()==0x0){
1112 AliError("No valid object in CDB entry!");
1116 if (!entry->GetId().IsValid()) {
1117 AliError(Form("Invalid entry ID: %s",
1118 entry->GetId().ToString().Data()));
1122 if (!entry->GetId().IsSpecified()) {
1123 AliError(Form("Unspecified entry ID: %s",
1124 entry->GetId().ToString().Data()));
1128 AliCDBId id = entry->GetId();
1129 AliCDBParam *aPar = SelectSpecificStorage(id.GetPath());
1131 AliCDBStorage *aStorage=0;
1134 aStorage=GetStorage(aPar);
1138 aStorage = GetStorage(fCondParam);
1141 aStorage = GetStorage(fRefParam);
1144 aStorage = GetDefaultStorage();
1149 AliDebug(2,Form("Storing object into storage: %s", aStorage->GetURI().Data()));
1151 Bool_t result = aStorage->Put(entry, type);
1153 if(fRun >= 0) QueryCDB();
1160 //_____________________________________________________________________________
1161 void AliCDBManager::CacheEntry(const char* path, AliCDBEntry* entry)
1163 // cache AliCDBEntry. Cache is valid until run number is changed.
1165 AliCDBEntry *chkEntry = dynamic_cast<AliCDBEntry*> (fEntryCache.GetValue(path));
1168 AliDebug(2, Form("Object %s already in cache !!", path));
1171 AliDebug(2,Form("Caching entry %s", path));
1174 fEntryCache.Add(new TObjString(path), entry);
1175 AliDebug(2,Form("Cache entries: %d", fEntryCache.GetEntries()));
1179 //_____________________________________________________________________________
1180 void AliCDBManager::Print(Option_t* /*option*/) const
1182 // Print list of active storages and their URIs
1184 TString output=Form("Run number = %d; ",fRun);
1185 output += "Cache is ";
1186 if(!fCache) output += "NOT ";
1187 output += Form("ACTIVE; Number of active storages: %d\n",fActiveStorages.GetEntries());
1189 if(fDefaultStorage) {
1190 output += Form("\t*** Default Storage URI: \"%s\"\n",fDefaultStorage->GetURI().Data());
1191 // AliInfo(output.Data());
1193 if(fSpecificStorages.GetEntries()>0) {
1194 TIter iter(fSpecificStorages.GetTable());
1197 while((aPair = (TPair*) iter.Next())){
1198 output += Form("\t*** Specific storage %d: Path \"%s\" -> URI \"%s\"\n",
1199 i++, ((TObjString*) aPair->Key())->GetName(),
1200 ((AliCDBParam*) aPair->Value())->GetURI().Data());
1204 output += Form("*** Drain Storage URI: %s\n",fDrainStorage->GetURI().Data());
1206 AliInfo(output.Data());
1209 //_____________________________________________________________________________
1210 void AliCDBManager::SetRun(Int_t run)
1212 // Sets current run number.
1213 // When the run number changes the caching is cleared.
1218 if(fLock && fRun >= 0) {
1219 AliFatal("Lock is ON, cannot reset run number!");
1224 // here the LHCPeriod xml file is parsed; the string containing the correct period is returned; the default storage is set
1225 if (fStartRunLHCPeriod <= run && fEndRunLHCPeriod >= run){
1226 AliInfo("LHCPeriod alien folder for current run already in memory");
1228 SetDefaultStorageFromRun(run);
1229 if(fEntryCache.GetEntries()!=0) ClearCache();
1237 //_____________________________________________________________________________
1238 void AliCDBManager::ClearCache(){
1239 // clear AliCDBEntry cache
1241 AliDebug(2, Form("Cache entries to be deleted: %d",fEntryCache.GetEntries()));
1244 // To clean entries one by one
1245 TIter iter(fEntryCache.GetTable());
1247 while((pair= dynamic_cast<TPair*> (iter.Next()))){
1249 TObjString* key = dynamic_cast<TObjString*> (pair->Key());
1250 AliCDBEntry* entry = dynamic_cast<AliCDBEntry*> (pair->Value());
1251 AliDebug(2, Form("Deleting entry: %s", key->GetName()));
1252 if (entry) delete entry;
1253 delete fEntryCache.Remove(key);
1256 fEntryCache.DeleteAll();
1257 AliDebug(2, Form("After deleting - Cache entries: %d",fEntryCache.GetEntries()));
1260 //_____________________________________________________________________________
1261 void AliCDBManager::UnloadFromCache(const char* path){
1262 // unload cached object
1263 // that is remove the entry from the cache and the id from the list of ids
1265 if(!fActiveStorages.GetEntries()) {
1266 AliDebug(2, Form("No active storages. Object \"%s\" is not unloaded from cache", path));
1270 AliCDBPath queryPath(path);
1271 if(!queryPath.IsValid()) return;
1273 if(!queryPath.IsWildcard()) { // path is not wildcard, get it directly from the cache and unload it!
1274 if(fEntryCache.Contains(path)){
1275 AliDebug(2, Form("Unloading object \"%s\" from cache and from list of ids", path));
1276 TObjString pathStr(path);
1277 delete fEntryCache.Remove(&pathStr);
1280 while((id = dynamic_cast<AliCDBId*> (iter.Next()))){
1281 if(queryPath.Comprises(id->GetPath()))
1282 delete fIds->Remove(id);
1285 AliError(Form("Cache does not contain object \"%s\"!", path));
1287 AliDebug(2, Form("Cache entries: %d",fEntryCache.GetEntries()));
1291 // path is wildcard: loop on the cache and unload all comprised objects!
1292 TIter iter(fEntryCache.GetTable());
1296 while((pair = dynamic_cast<TPair*> (iter.Next()))){
1297 AliCDBPath entryPath = pair->Key()->GetName();
1298 if(queryPath.Comprises(entryPath)) {
1299 AliDebug(2, Form("Unloading object \"%s\" from cache and from list of ids", entryPath.GetPath().Data()));
1300 TObjString pathStr(entryPath.GetPath());
1301 delete fEntryCache.Remove(&pathStr);
1303 TIter iterids(fIds);
1305 while((anId = dynamic_cast<AliCDBId*> (iterids.Next()))){
1306 AliCDBPath aPath = anId->GetPath();
1307 TString aPathStr = aPath.GetPath();
1308 if(queryPath.Comprises(aPath)) {
1309 delete fIds->Remove(anId);
1315 AliDebug(2,Form("Cache entries and ids removed: %d Remaining: %d",removed,fEntryCache.GetEntries()));
1318 //_____________________________________________________________________________
1319 void AliCDBManager::DestroyActiveStorages() {
1320 // delete list of active storages
1322 fActiveStorages.DeleteAll();
1323 fSpecificStorages.DeleteAll();
1326 //_____________________________________________________________________________
1327 void AliCDBManager::DestroyActiveStorage(AliCDBStorage* /*storage*/) {
1328 // destroys active storage
1331 TIter iter(fActiveStorages.GetTable());
1333 while ((aPair = (TPair*) iter.Next())) {
1334 if(storage == (AliCDBStorage*) aPair->Value())
1335 delete fActiveStorages.Remove(aPair->Key());
1336 storage->Delete(); storage=0x0;
1342 //_____________________________________________________________________________
1343 void AliCDBManager::QueryCDB() {
1344 // query default and specific storages for files valid for fRun. Every storage loads the Ids into its list.
1347 AliError("Run number not yet set! Use AliCDBManager::SetRun.");
1350 if (!fDefaultStorage){
1351 AliError("Default storage is not set! Use AliCDBManager::SetDefaultStorage");
1354 if(fDefaultStorage->GetType() == "alien"){
1355 fDefaultStorage->QueryCDB(fRun);
1357 AliDebug(2,"Skipping query for valid files, it used only in grid...");
1360 TIter iter(&fSpecificStorages);
1361 TObjString *aCalibType=0;
1362 AliCDBParam* aPar=0;
1363 while((aCalibType = dynamic_cast<TObjString*> (iter.Next()))){
1364 aPar = (AliCDBParam*) fSpecificStorages.GetValue(aCalibType);
1366 AliDebug(2,Form("Querying specific storage %s",aCalibType->GetName()));
1367 AliCDBStorage *aStorage = GetStorage(aPar);
1368 if(aStorage->GetType() == "alien"){
1369 aStorage->QueryCDB(fRun,aCalibType->GetName());
1372 "Skipping query for valid files, it is used only in grid...");
1378 //______________________________________________________________________________________________
1379 const char* AliCDBManager::GetDataTypeName(DataType type)
1381 // returns the name (string) of the data type
1384 case kCondition: return "Conditions";
1385 case kReference: return "Reference";
1386 case kPrivate: return "Private";
1392 //______________________________________________________________________________________________
1393 void AliCDBManager::InitShortLived()
1395 // Init the list of short-lived objects
1396 // currently disabled
1400 // fShortLived = new TList();
1401 // fShortLived->SetOwner(1);
1403 // fShortLived->Add(new TObjString("EMCAL/Calib/Data"));
1405 // fShortLived->Add(new TObjString("HMPID/Calib/Nmean"));
1406 // fShortLived->Add(new TObjString("HMPID/Calib/Qthre"));
1408 // fShortLived->Add(new TObjString("ITS/Calib/CalibSPD"));
1410 // fShortLived->Add(new TObjString("MUON/Calib/Gains"));
1411 // fShortLived->Add(new TObjString("MUON/Calib/HV"));
1412 // fShortLived->Add(new TObjString("MUON/Calib/Pedestals"));
1414 // fShortLived->Add(new TObjString("PHOS/Calib/CpvGainPedestals"));
1415 // fShortLived->Add(new TObjString("PHOS/Calib/EmcGainPedestals"));
1417 // fShortLived->Add(new TObjString("PMD/Calib/Data"));
1419 // fShortLived->Add(new TObjString("TRD/Calib/ChamberGainFactor"));
1420 // fShortLived->Add(new TObjString("TRD/Calib/LocalGainFactor"));
1421 // fShortLived->Add(new TObjString("TRD/Calib/ChamberT0"));
1422 // fShortLived->Add(new TObjString("TRD/Calib/LocalT0"));
1423 // fShortLived->Add(new TObjString("TRD/Calib/ChamberVdrift"));
1424 // fShortLived->Add(new TObjString("TRD/Calib/LocalVdrift"));
1426 // fShortLived->Add(new TObjString("ZDC/Calib/Data"));
1430 //______________________________________________________________________________________________
1431 Bool_t AliCDBManager::IsShortLived(const char* path)
1433 // returns the name (string) of the data type
1435 if(!fShortLived) return kFALSE;
1437 AliCDBPath aPath(path);
1438 if(!aPath.IsValid()){
1439 AliError(Form("Not a valid path: %s", path));
1443 return fShortLived->Contains(path);
1447 //______________________________________________________________________________________________
1448 ULong_t AliCDBManager::SetLock(Bool_t lock, ULong_t key){
1449 // To lock/unlock user must provide the key. A new key is provided after
1450 // each successful lock. User should always backup the returned key and
1451 // use it on next access.
1452 if (fLock == lock) return 0; // nothing to be done
1454 // User wants to lock - check his identity
1456 // Lock has a user - check his key
1458 AliFatal("Wrong key provided to lock CDB. Please remove CDB lock access from your code !");
1463 fKey = gSystem->Now();
1467 // User wants to unlock - check the provided key
1469 AliFatal("Lock is ON: wrong key provided");
1476 ///////////////////////////////////////////////////////////
1477 // AliCDBManager Parameter class //
1478 // interface to specific AliCDBParameter class //
1479 // (AliCDBGridParam, AliCDBLocalParam, AliCDBDumpParam) //
1480 ///////////////////////////////////////////////////////////
1482 AliCDBParam::AliCDBParam():
1490 //_____________________________________________________________________________
1491 AliCDBParam::~AliCDBParam() {