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
135 // if the manager is locked it cannot initialize from a snapshot
137 AliError("Being locked I cannot initialize from the snapshot!");
142 TString snapshotFile(snapshotFileName);
143 if(snapshotFile.BeginsWith("alien://")){
145 TGrid::Connect("alien://","");
147 AliError("Connection to alien failed!");
153 TFile *f = TFile::Open(snapshotFileName);
154 if (!f || f->IsZombie()){
155 AliError(Form("Cannot open file %s",snapshotFileName));
159 // retrieve entries' map from snapshot file
160 TMap *entriesMap = 0;
161 TIter next(f->GetListOfKeys());
163 while ((key = (TKey*)next())) {
164 if (strcmp(key->GetClassName(),"TMap") != 0) continue;
165 entriesMap = (TMap*)key->ReadObj();
168 if (!entriesMap || entriesMap->GetEntries()==0){
169 AliError("Cannot get valid map of CDB entries from snapshot file");
173 // retrieve ids' list from snapshot file
175 TIter nextKey(f->GetListOfKeys());
177 while ((keyN = (TKey*)nextKey())) {
178 if (strcmp(keyN->GetClassName(),"TList") != 0) continue;
179 idsList = (TList*)keyN->ReadObj();
182 if (!idsList || idsList->GetEntries()==0){
183 AliError("Cannot get valid list of CDB entries from snapshot file");
187 // Add each (entry,id) from the snapshot to the memory: entry to the cache, id to the list of ids.
188 // If "overwrite" is false: add the entry to the cache and its id to the list of ids
189 // only if neither of them is already there.
190 // If "overwrite" is true: write the snapshot entry,id in any case. If something
191 // was already there for that calibration type, remove it and issue a warning
192 TIter iterObj(entriesMap->GetTable());
195 while((pair = dynamic_cast<TPair*> (iterObj.Next()))){
196 TObjString* os = (TObjString*) pair->Key();
197 TString path = os->GetString();
198 TIter iterId(idsList);
200 AliCDBId* correspondingId=0;
201 while((id = dynamic_cast<AliCDBId*> (iterId.Next()))){
202 TString idpath(id->GetPath());
208 if(!correspondingId){
209 AliError(Form("id for \"%s\" not found in the snapshot (while entry was). This entry is skipped!",path.Data()));
212 Bool_t cached = fEntryCache.Contains(path.Data());
213 Bool_t registeredId = kFALSE;
216 while((idT = dynamic_cast<AliCDBId*> (iter.Next()))){
217 if(idT->GetPath()==path){
218 registeredId = kTRUE;
224 if(cached || registeredId){
225 AliWarning(Form("An entry was already cached for \"%s\". Removing it before caching from snapshot",path.Data()));
226 UnloadFromCache(path.Data());
228 fEntryCache.Add(pair->Key(),pair->Value());
232 if(cached || registeredId){
233 AliWarning(Form("An entry was already cached for \"%s\". Not adding this object from snapshot",path.Data()));
235 fEntryCache.Add(pair->Key(),pair->Value());
242 // fEntry is the new owner of the cache
243 fEntryCache.SetOwnerKeyValue(kTRUE,kTRUE);
244 entriesMap->SetOwnerKeyValue(kFALSE,kFALSE);
245 fIds->SetOwner(kTRUE);
246 idsList->SetOwner(kFALSE);
247 AliInfo(Form("%d new (entry,id) cached. Total number %d",nAdded,fEntryCache.GetEntries()));
255 //_____________________________________________________________________________
256 void AliCDBManager::Destroy() {
257 // delete ALCDBManager instance and active storages
260 //fgInstance->Delete();
266 //_____________________________________________________________________________
267 AliCDBManager::AliCDBManager():
276 fDefaultStorage(NULL),
284 fStartRunLHCPeriod(-1),
285 fEndRunLHCPeriod(-1),
289 // default constuctor
290 fFactories.SetOwner(1);
291 fActiveStorages.SetOwner(1);
292 fSpecificStorages.SetOwner(1);
293 fEntryCache.SetName("CDBEntryCache");
294 fEntryCache.SetOwnerKeyValue(kTRUE,kTRUE);
296 fStorageMap = new TMap();
297 fStorageMap->SetOwner(1);
302 //_____________________________________________________________________________
303 AliCDBManager::~AliCDBManager() {
306 DestroyActiveStorages();
309 fDefaultStorage = 0x0;
310 delete fStorageMap; fStorageMap = 0;
311 delete fIds; fIds = 0;
314 delete fShortLived; fShortLived = 0x0;
317 //_____________________________________________________________________________
318 void AliCDBManager::PutActiveStorage(AliCDBParam* param, AliCDBStorage* storage){
319 // put a storage object into the list of active storages
321 fActiveStorages.Add(param, storage);
322 AliDebug(1, Form("Active storages: %d", fActiveStorages.GetEntries()));
325 //_____________________________________________________________________________
326 void AliCDBManager::RegisterFactory(AliCDBStorageFactory* factory) {
327 // add a storage factory to the list of registerd factories
329 if (!fFactories.Contains(factory)) {
330 fFactories.Add(factory);
334 //_____________________________________________________________________________
335 Bool_t AliCDBManager::HasStorage(const char* dbString) const {
336 // check if dbString is a URI valid for one of the registered factories
338 TIter iter(&fFactories);
340 AliCDBStorageFactory* factory=0;
341 while ((factory = (AliCDBStorageFactory*) iter.Next())) {
343 if (factory->Validate(dbString)) {
351 //_____________________________________________________________________________
352 AliCDBParam* AliCDBManager::CreateParameter(const char* dbString) const {
353 // create AliCDBParam object from URI string
355 TIter iter(&fFactories);
357 AliCDBStorageFactory* factory=0;
358 while ((factory = (AliCDBStorageFactory*) iter.Next())) {
359 AliCDBParam* param = factory->CreateParameter(dbString);
360 if(param) return param;
366 //_____________________________________________________________________________
367 AliCDBStorage* AliCDBManager::GetStorage(const char* dbString) {
368 // get storage object from URI string
370 AliCDBParam* param = CreateParameter(dbString);
372 AliError(Form("Failed to activate requested storage! Check URI: %s", dbString));
376 AliCDBStorage* aStorage = GetStorage(param);
382 //_____________________________________________________________________________
383 AliCDBStorage* AliCDBManager::GetStorage(const AliCDBParam* param) {
384 // get storage object from AliCDBParam object
386 // if the list of active storages already contains
387 // the requested storage, return it
388 AliCDBStorage* aStorage = GetActiveStorage(param);
393 // if lock is ON, cannot activate more storages!
395 if (fDefaultStorage) {
396 AliFatal("Lock is ON, and default storage is already set: "
397 "cannot reset it or activate more storages!");
401 TIter iter(&fFactories);
403 AliCDBStorageFactory* factory=0;
405 // loop on the list of registered factories
406 while ((factory = (AliCDBStorageFactory*) iter.Next())) {
408 // each factory tries to create its storage from the parameter
409 aStorage = factory->Create(param);
411 PutActiveStorage(param->CloneParam(), aStorage);
412 aStorage->SetURI(param->GetURI());
414 if(aStorage->GetType() == "alien"){
415 aStorage->QueryCDB(fRun);
418 "Skipping query for valid files, it is used only in grid...");
425 AliError(Form("Failed to activate requested storage! Check URI: %s", param->GetURI().Data()));
430 //_____________________________________________________________________________
431 AliCDBStorage* AliCDBManager::GetActiveStorage(const AliCDBParam* param) {
432 // get a storage object from the list of active storages
434 return dynamic_cast<AliCDBStorage*> (fActiveStorages.GetValue(param));
437 //_____________________________________________________________________________
438 TList* AliCDBManager::GetActiveStorages() {
439 // return list of active storages
440 // user has responsibility to delete returned object
442 TList* result = new TList();
444 TIter iter(fActiveStorages.GetTable());
446 while ((aPair = (TPair*) iter.Next())) {
447 result->Add(aPair->Value());
453 //_____________________________________________________________________________
454 void AliCDBManager::SetDrain(const char* dbString) {
455 // set drain storage from URI string
457 fDrainStorage = GetStorage(dbString);
460 //_____________________________________________________________________________
461 void AliCDBManager::SetDrain(const AliCDBParam* param) {
462 // set drain storage from AliCDBParam
464 fDrainStorage = GetStorage(param);
467 //_____________________________________________________________________________
468 void AliCDBManager::SetDrain(AliCDBStorage* storage) {
469 // set drain storage from another active storage
471 fDrainStorage = storage;
474 //_____________________________________________________________________________
475 Bool_t AliCDBManager::Drain(AliCDBEntry *entry) {
476 // drain retrieved object to drain storage
478 AliDebug(2, "Draining into drain storage...");
479 return fDrainStorage->Put(entry);
482 //____________________________________________________________________________
483 void AliCDBManager::SetDefaultStorage(const char* dbString) {
484 // sets default storage from URI string
486 // checking whether we are in the raw case
487 TString dbStringTemp(dbString);
488 if (dbStringTemp == "raw://")
491 AliInfo("Setting the run-number will set the corresponding OCDB for raw data reconstruction.");
492 AliInfo("Connecting to the grid...");
494 TGrid::Connect("alien://","");
496 AliError("Connection to alien failed!");
503 AliCDBStorage* bckStorage = fDefaultStorage;
505 fDefaultStorage = GetStorage(dbString);
507 if(!fDefaultStorage) return;
509 if(bckStorage && (fDefaultStorage != bckStorage)){
510 AliWarning("Existing default storage replaced: clearing cache!");
514 if (fStorageMap->Contains("default")) {
515 delete fStorageMap->Remove(((TPair*)fStorageMap->FindObject("default"))->Key());
517 fStorageMap->Add(new TObjString("default"), new TObjString(fDefaultStorage->GetURI()));
519 //_____________________________________________________________________________
520 void AliCDBManager::SetDefaultStorage(const AliCDBParam* param) {
521 // set default storage from AliCDBParam object
523 AliCDBStorage* bckStorage = fDefaultStorage;
525 fDefaultStorage = GetStorage(param);
527 if(!fDefaultStorage) return;
529 if(bckStorage && (fDefaultStorage != bckStorage)){
530 AliWarning("Existing default storage replaced: clearing cache!");
534 if (fStorageMap->Contains("default")) {
535 delete fStorageMap->Remove(((TPair*)fStorageMap->FindObject("default"))->Key());
537 fStorageMap->Add(new TObjString("default"), new TObjString(fDefaultStorage->GetURI()));
540 //_____________________________________________________________________________
541 void AliCDBManager::SetDefaultStorage(AliCDBStorage* storage) {
542 // set default storage from another active storage
544 // if lock is ON, cannot activate more storages!
546 if (fDefaultStorage) {
547 AliFatal("Lock is ON, and default storage is already set: "
548 "cannot reset it or activate more storages!");
553 UnsetDefaultStorage();
557 AliCDBStorage* bckStorage = fDefaultStorage;
559 fDefaultStorage = storage;
561 if(bckStorage && (fDefaultStorage != bckStorage)){
562 AliWarning("Existing default storage replaced: clearing cache!");
566 if (fStorageMap->Contains("default")) {
567 delete fStorageMap->Remove(((TPair*)fStorageMap->FindObject("default"))->Key());
569 fStorageMap->Add(new TObjString("default"), new TObjString(fDefaultStorage->GetURI()));
572 //_____________________________________________________________________________
573 void AliCDBManager::SetDefaultStorage(const char* mcString, const char* simType) {
574 // sets default storage for MC data
575 // mcString MUST be "MC",
576 // simType can be "Ideal","Residual","Full"
578 TString strmcString(mcString);
579 TString strsimType(simType);
581 if (strmcString != "MC"){
582 AliFatal("Method requires first string to be MC!");
585 if (strsimType == "Ideal"){
586 dbString = fgkMCIdealStorage;
588 else if (strsimType == "Full"){
589 dbString = fgkMCFullStorage;
591 else if (strsimType == "Residual"){
592 dbString = fgkMCResidualStorage;
595 AliFatal("Error in setting the storage for MC data, second argument MUST be either \"Ideal\" or \"Full\" or \"Residual\".");
598 SetDefaultStorage(dbString.Data());
599 if(!fDefaultStorage) AliFatal(Form("%s storage not there! Please check!",fLHCPeriod.Data()));
602 //_____________________________________________________________________________
603 void AliCDBManager::SetDefaultStorageFromRun(Int_t run) {
604 // set default storage from the run number - to be used only with raw data
606 // if lock is ON, cannot activate more storages!
608 if (fDefaultStorage) {
609 AliFatal("Lock is ON, and default storage is already set: "
610 "cannot activate default storage from run number");
614 // retrieve XML file from alien
616 TGrid::Connect("alien://","");
618 AliError("Connection to alien failed!");
623 TString rndname = "/tmp/";
624 rndname += "OCDBFolderXML.";
625 rndname += uuid.AsString();
627 AliDebug(2, Form("file to be copied = %s", fgkOCDBFolderXMLfile.Data()));
628 if (!TFile::Cp(fgkOCDBFolderXMLfile.Data(), rndname.Data())) {
629 AliFatal(Form("Cannot make a local copy of OCDBFolder xml file in %s",rndname.Data()));
631 AliCDBHandler* saxcdb = new AliCDBHandler();
633 TSAXParser *saxParser = new TSAXParser();
634 saxParser->ConnectToHandler("AliCDBHandler", saxcdb);
635 saxParser->ParseFile(rndname.Data());
636 AliInfo(Form(" LHC folder = %s", saxcdb->GetOCDBFolder().Data()));
637 AliInfo(Form(" LHC period start run = %d", saxcdb->GetStartRunRange()));
638 AliInfo(Form(" LHC period end run = %d", saxcdb->GetEndRunRange()));
639 fLHCPeriod = saxcdb->GetOCDBFolder();
640 fStartRunLHCPeriod = saxcdb->GetStartRunRange();
641 fEndRunLHCPeriod = saxcdb->GetEndRunRange();
643 SetDefaultStorage(fLHCPeriod.Data());
644 if(!fDefaultStorage) AliFatal(Form("%s storage not there! Please check!",fLHCPeriod.Data()));
648 //_____________________________________________________________________________
649 void AliCDBManager::UnsetDefaultStorage() {
650 // Unset default storage
652 // if lock is ON, action is forbidden!
654 if (fDefaultStorage) {
655 AliFatal("Lock is ON: cannot unset default storage!");
659 if (fDefaultStorage) {
660 AliWarning("Clearing cache!");
664 fRun = fStartRunLHCPeriod = fEndRunLHCPeriod = -1;
667 fDefaultStorage = 0x0;
670 //_____________________________________________________________________________
671 void AliCDBManager::SetSpecificStorage(const char* calibType, const char* dbString) {
672 // sets storage specific for detector or calibration type (works with AliCDBManager::Get(...))
674 AliCDBParam *aPar = CreateParameter(dbString);
676 SetSpecificStorage(calibType, aPar);
680 //_____________________________________________________________________________
681 void AliCDBManager::SetSpecificStorage(const char* calibType, const AliCDBParam* param) {
682 // sets storage specific for detector or calibration type (works with AliCDBManager::Get(...))
683 // Default storage should be defined prior to any specific storages, e.g.:
684 // AliCDBManager::instance()->SetDefaultStorage("alien://");
685 // AliCDBManager::instance()->SetSpecificStorage("TPC/*","local://DB_TPC");
686 // AliCDBManager::instance()->SetSpecificStorage("*/Align/*","local://DB_TPCAlign");
687 // calibType must be a valid CDB path! (3 level folder structure)
690 if(!fDefaultStorage && !fRaw) {
691 AliError("Please activate a default storage first!");
696 AliCDBPath aPath(calibType);
697 if(!aPath.IsValid()){
698 AliError(Form("Not a valid path: %s", calibType));
702 TObjString *objCalibType = new TObjString(aPath.GetPath());
703 if(fSpecificStorages.Contains(objCalibType)){
704 AliWarning(Form("Storage \"%s\" already activated! It will be replaced by the new one",
706 AliCDBParam *checkPar = dynamic_cast<AliCDBParam*> (fSpecificStorages.GetValue(calibType));
707 if(checkPar) delete checkPar;
708 delete fSpecificStorages.Remove(objCalibType);
710 AliCDBStorage *aStorage = GetStorage(param);
711 if(!aStorage) return;
713 fSpecificStorages.Add(objCalibType, param->CloneParam());
715 if(fStorageMap->Contains(objCalibType)){
716 delete fStorageMap->Remove(objCalibType);
718 fStorageMap->Add(objCalibType->Clone(), new TObjString(param->GetURI()));
722 //_____________________________________________________________________________
723 AliCDBStorage* AliCDBManager::GetSpecificStorage(const char* calibType) {
724 // get storage specific for detector or calibration type
726 AliCDBPath calibPath(calibType);
727 if(!calibPath.IsValid()) return NULL;
729 AliCDBParam *checkPar = (AliCDBParam*) fSpecificStorages.GetValue(calibPath.GetPath());
731 AliError(Form("%s storage not found!", calibType));
734 return GetStorage(checkPar);
739 //_____________________________________________________________________________
740 AliCDBParam* AliCDBManager::SelectSpecificStorage(const TString& path) {
741 // select storage valid for path from the list of specific storages
743 AliCDBPath aPath(path);
744 if(!aPath.IsValid()) return NULL;
746 TIter iter(&fSpecificStorages);
747 TObjString *aCalibType=0;
748 AliCDBPath tmpPath("null/null/null");
750 while((aCalibType = (TObjString*) iter.Next())){
751 AliCDBPath calibTypePath(aCalibType->GetName());
752 if(calibTypePath.Comprises(aPath)) {
753 if(calibTypePath.Comprises(tmpPath)) continue;
754 aPar = (AliCDBParam*) fSpecificStorages.GetValue(aCalibType);
755 tmpPath.SetPath(calibTypePath.GetPath());
761 //_____________________________________________________________________________
762 AliCDBEntry* AliCDBManager::Get(const AliCDBPath& path, Int_t runNumber,
763 Int_t version, Int_t subVersion) {
764 // get an AliCDBEntry object from the database
767 // RunNumber is not specified. Try with fRun
769 AliError("Run number neither specified in query nor set in AliCDBManager! Use AliCDBManager::SetRun.");
775 return Get(AliCDBId(path, runNumber, runNumber, version, subVersion));
778 //_____________________________________________________________________________
779 AliCDBEntry* AliCDBManager::Get(const AliCDBPath& path,
780 const AliCDBRunRange& runRange, Int_t version,
782 // get an AliCDBEntry object from the database!
784 return Get(AliCDBId(path, runRange, version, subVersion));
787 //_____________________________________________________________________________
788 AliCDBEntry* AliCDBManager::Get(const AliCDBId& query) {
789 // get an AliCDBEntry object from the database
791 // check if query's path and runRange are valid
792 // query is invalid also if version is not specified and subversion is!
793 if (!query.IsValid()) {
794 AliError(Form("Invalid query: %s", query.ToString().Data()));
798 // query is not specified if path contains wildcard or run range= [-1,-1]
799 if (!query.IsSpecified()) {
800 AliError(Form("Unspecified query: %s",
801 query.ToString().Data()));
805 if(fLock && !(fRun >= query.GetFirstRun() && fRun <= query.GetLastRun()))
806 AliFatal("Lock is ON: cannot use different run number than the internal one!");
808 if(fCache && !(fRun >= query.GetFirstRun() && fRun <= query.GetLastRun()))
809 AliWarning("Run number explicitly set in query: CDB cache temporarily disabled!");
811 AliCDBEntry *entry=0;
813 // first look into map of cached objects
814 if(fCache && query.GetFirstRun() == fRun)
815 entry = (AliCDBEntry*) fEntryCache.GetValue(query.GetPath());
818 AliDebug(2, Form("Object %s retrieved from cache !!",query.GetPath().Data()));
822 if(!fDefaultStorage) {
823 AliError("No storage set!");
826 // Entry is not in cache -> retrieve it from CDB and cache it!!
827 AliCDBStorage *aStorage=0;
828 AliCDBParam *aPar=SelectSpecificStorage(query.GetPath());
829 // Bool_t usedDefStorage=kTRUE;
832 aStorage=GetStorage(aPar);
833 TString str = aPar->GetURI();
834 AliDebug(2,Form("Looking into storage: %s",str.Data()));
835 // usedDefStorage=kFALSE;
838 aStorage=GetDefaultStorage();
839 AliDebug(2,"Looking into default storage");
842 entry = aStorage->Get(query);
844 if(entry && fCache && (query.GetFirstRun() == fRun)){
845 CacheEntry(query.GetPath(), entry);
848 if(entry && !fIds->Contains(&entry->GetId())){
849 fIds->Add(entry->GetId().Clone());
857 //_____________________________________________________________________________
858 const char* AliCDBManager::GetURI(const char* path) {
859 // return the URI of the storage where to look for path
861 if(!IsDefaultStorageSet()) return 0;
863 AliCDBParam *aPar=SelectSpecificStorage(path);
866 return aPar->GetURI().Data();
869 return GetDefaultStorage()->GetURI().Data();
875 //_____________________________________________________________________________
876 AliCDBId* AliCDBManager::GetId(const AliCDBPath& path, Int_t runNumber,
877 Int_t version, Int_t subVersion) {
878 // get the AliCDBId of the valid object from the database (does not retrieve the object)
879 // User must delete returned object!
882 // RunNumber is not specified. Try with fRun
884 AliError("Run number neither specified in query nor set in AliCDBManager! Use AliCDBManager::SetRun.");
890 return GetId(AliCDBId(path, runNumber, runNumber, version, subVersion));
893 //_____________________________________________________________________________
894 AliCDBId* AliCDBManager::GetId(const AliCDBPath& path,
895 const AliCDBRunRange& runRange, Int_t version,
897 // get the AliCDBId of the valid object from the database (does not retrieve the object)
898 // User must delete returned object!
900 return GetId(AliCDBId(path, runRange, version, subVersion));
903 //_____________________________________________________________________________
904 AliCDBId* AliCDBManager::GetId(const AliCDBId& query) {
905 // get the AliCDBId of the valid object from the database (does not retrieve the object)
906 // User must delete returned object!
908 if(!fDefaultStorage) {
909 AliError("No storage set!");
913 // check if query's path and runRange are valid
914 // query is invalid also if version is not specified and subversion is!
915 if (!query.IsValid()) {
916 AliError(Form("Invalid query: %s", query.ToString().Data()));
920 // query is not specified if path contains wildcard or run range= [-1,-1]
921 if (!query.IsSpecified()) {
922 AliError(Form("Unspecified query: %s",
923 query.ToString().Data()));
927 if(fCache && query.GetFirstRun() != fRun)
928 AliWarning("Run number explicitly set in query: CDB cache temporarily disabled!");
930 AliCDBEntry* entry = 0;
932 // first look into map of cached objects
933 if(fCache && query.GetFirstRun() == fRun)
934 entry = (AliCDBEntry*) fEntryCache.GetValue(query.GetPath());
937 AliDebug(2, Form("Object %s retrieved from cache !!",query.GetPath().Data()));
938 return dynamic_cast<AliCDBId*> (entry->GetId().Clone());
941 // Entry is not in cache -> retrieve it from CDB and cache it!!
942 AliCDBStorage *aStorage=0;
943 AliCDBParam *aPar=SelectSpecificStorage(query.GetPath());
946 aStorage=GetStorage(aPar);
947 TString str = aPar->GetURI();
948 AliDebug(2,Form("Looking into storage: %s",str.Data()));
951 aStorage=GetDefaultStorage();
952 AliDebug(2,"Looking into default storage");
955 return aStorage->GetId(query);
959 //_____________________________________________________________________________
960 TList* AliCDBManager::GetAll(const AliCDBPath& path, Int_t runNumber,
961 Int_t version, Int_t subVersion) {
962 // get multiple AliCDBEntry objects from the database
965 // RunNumber is not specified. Try with fRun
967 AliError("Run number neither specified in query nor set in AliCDBManager! Use AliCDBManager::SetRun.");
973 return GetAll(AliCDBId(path, runNumber, runNumber, version,
977 //_____________________________________________________________________________
978 TList* AliCDBManager::GetAll(const AliCDBPath& path,
979 const AliCDBRunRange& runRange, Int_t version, Int_t subVersion) {
980 // get multiple AliCDBEntry objects from the database
982 return GetAll(AliCDBId(path, runRange, version, subVersion));
985 //_____________________________________________________________________________
986 TList* AliCDBManager::GetAll(const AliCDBId& query) {
987 // get multiple AliCDBEntry objects from the database
988 // Warning: this method works correctly only for queries of the type "Detector/*"
989 // and not for more specific queries e.g. "Detector/Calib/*" !
990 // Warning #2: Entries are cached, but GetAll will keep on retrieving objects from OCDB!
991 // To get an object from cache use Get() function
993 if(!fDefaultStorage) {
994 AliError("No storage set!");
998 if (!query.IsValid()) {
999 AliError(Form("Invalid query: %s", query.ToString().Data()));
1003 if((fSpecificStorages.GetEntries()>0) && query.GetPath().BeginsWith('*')){
1004 // if specific storages are active a query with "*" is ambiguous
1005 AliError("Query too generic in this context!");
1009 if (query.IsAnyRange()) {
1010 AliError(Form("Unspecified run or runrange: %s",
1011 query.ToString().Data()));
1015 if(fLock && query.GetFirstRun() != fRun)
1016 AliFatal("Lock is ON: cannot use different run number than the internal one!");
1018 AliCDBParam *aPar=SelectSpecificStorage(query.GetPath());
1020 AliCDBStorage *aStorage;
1022 aStorage=GetStorage(aPar);
1023 AliDebug(2,Form("Looking into storage: %s", aPar->GetURI().Data()));
1026 aStorage=GetDefaultStorage();
1027 AliDebug(2,"Looking into default storage");
1031 if(aStorage) result = aStorage->GetAll(query);
1032 if(!result) return 0;
1034 // loop on result to check whether entries should be re-queried with specific storages
1035 if(fSpecificStorages.GetEntries()>0 && ! (fSpecificStorages.GetEntries() == 1 && aPar)) {
1036 AliInfo("Now look into all other specific storages...");
1039 AliCDBEntry* chkEntry=0;
1041 while((chkEntry = dynamic_cast<AliCDBEntry*> (iter.Next()))){
1042 AliCDBId& chkId = chkEntry->GetId();
1043 AliDebug(2, Form("Checking id %s ", chkId.GetPath().Data()));
1044 AliCDBParam *chkPar=SelectSpecificStorage(chkId.GetPath());
1045 if (!chkPar || aPar == chkPar) continue;
1046 AliCDBStorage *chkStorage = GetStorage(chkPar);
1047 AliDebug(2, Form("Found specific storage! %s", chkPar->GetURI().Data()));
1049 AliCDBEntry *newEntry=0;
1050 chkId.SetRunRange(query.GetFirstRun(), query.GetLastRun());
1051 chkId.SetVersion(query.GetVersion());
1052 chkId.SetSubVersion(query.GetSubVersion());
1054 if(chkStorage) newEntry = chkStorage->Get(chkId);
1055 if(!newEntry) continue;
1057 // object is found in specific storage: replace entry in the result list!
1058 chkEntry->SetOwner(1);
1059 delete result->Remove(chkEntry);
1060 result->AddFirst(newEntry);
1063 Int_t nEntries = result->GetEntries();
1064 AliInfo("After look into other specific storages, result list is:");
1065 for(int i=0; i<nEntries;i++){
1066 AliCDBEntry *entry = (AliCDBEntry*) result->At(i);
1067 AliInfo(Form("%s",entry->GetId().ToString().Data()));
1073 AliCDBEntry* entry=0;
1074 while((entry = dynamic_cast<AliCDBEntry*> (iter.Next()))){
1076 if(!fIds->Contains(&entry->GetId())){
1077 fIds->Add(entry->GetId().Clone());
1079 if(fCache && (query.GetFirstRun() == fRun)){
1080 CacheEntry(entry->GetId().GetPath(), entry);
1088 //_____________________________________________________________________________
1089 Bool_t AliCDBManager::Put(TObject* object, const AliCDBId& id, AliCDBMetaData* metaData, const DataType type){
1090 // store an AliCDBEntry object into the database
1093 AliError("Null Entry! No storage will be done!");
1097 AliCDBEntry anEntry(object, id, metaData);
1098 return Put(&anEntry, type);
1103 //_____________________________________________________________________________
1104 Bool_t AliCDBManager::Put(AliCDBEntry* entry, DataType type){
1105 // store an AliCDBEntry object into the database
1107 if(type == kPrivate && !fDefaultStorage) {
1108 AliError("No storage set!");
1113 AliError("No entry!");
1117 if (entry->GetObject()==0x0){
1118 AliError("No valid object in CDB entry!");
1122 if (!entry->GetId().IsValid()) {
1123 AliError(Form("Invalid entry ID: %s",
1124 entry->GetId().ToString().Data()));
1128 if (!entry->GetId().IsSpecified()) {
1129 AliError(Form("Unspecified entry ID: %s",
1130 entry->GetId().ToString().Data()));
1134 AliCDBId id = entry->GetId();
1135 AliCDBParam *aPar = SelectSpecificStorage(id.GetPath());
1137 AliCDBStorage *aStorage=0;
1140 aStorage=GetStorage(aPar);
1144 aStorage = GetStorage(fCondParam);
1147 aStorage = GetStorage(fRefParam);
1150 aStorage = GetDefaultStorage();
1155 AliDebug(2,Form("Storing object into storage: %s", aStorage->GetURI().Data()));
1157 Bool_t result = aStorage->Put(entry, type);
1159 if(fRun >= 0) QueryCDB();
1166 //_____________________________________________________________________________
1167 void AliCDBManager::CacheEntry(const char* path, AliCDBEntry* entry)
1169 // cache AliCDBEntry. Cache is valid until run number is changed.
1171 AliCDBEntry *chkEntry = dynamic_cast<AliCDBEntry*> (fEntryCache.GetValue(path));
1174 AliDebug(2, Form("Object %s already in cache !!", path));
1177 AliDebug(2,Form("Caching entry %s", path));
1180 fEntryCache.Add(new TObjString(path), entry);
1181 AliDebug(2,Form("Cache entries: %d", fEntryCache.GetEntries()));
1185 //_____________________________________________________________________________
1186 void AliCDBManager::Print(Option_t* /*option*/) const
1188 // Print list of active storages and their URIs
1190 TString output=Form("Run number = %d; ",fRun);
1191 output += "Cache is ";
1192 if(!fCache) output += "NOT ";
1193 output += Form("ACTIVE; Number of active storages: %d\n",fActiveStorages.GetEntries());
1195 if(fDefaultStorage) {
1196 output += Form("\t*** Default Storage URI: \"%s\"\n",fDefaultStorage->GetURI().Data());
1197 // AliInfo(output.Data());
1199 if(fSpecificStorages.GetEntries()>0) {
1200 TIter iter(fSpecificStorages.GetTable());
1203 while((aPair = (TPair*) iter.Next())){
1204 output += Form("\t*** Specific storage %d: Path \"%s\" -> URI \"%s\"\n",
1205 i++, ((TObjString*) aPair->Key())->GetName(),
1206 ((AliCDBParam*) aPair->Value())->GetURI().Data());
1210 output += Form("*** Drain Storage URI: %s\n",fDrainStorage->GetURI().Data());
1212 AliInfo(output.Data());
1215 //_____________________________________________________________________________
1216 void AliCDBManager::SetRun(Int_t run)
1218 // Sets current run number.
1219 // When the run number changes the caching is cleared.
1224 if(fLock && fRun >= 0) {
1225 AliFatal("Lock is ON, cannot reset run number!");
1230 // here the LHCPeriod xml file is parsed; the string containing the correct period is returned; the default storage is set
1231 if (fStartRunLHCPeriod <= run && fEndRunLHCPeriod >= run){
1232 AliInfo("LHCPeriod alien folder for current run already in memory");
1234 SetDefaultStorageFromRun(run);
1235 if(fEntryCache.GetEntries()!=0) ClearCache();
1243 //_____________________________________________________________________________
1244 void AliCDBManager::ClearCache(){
1245 // clear AliCDBEntry cache
1247 AliDebug(2, Form("Cache entries to be deleted: %d",fEntryCache.GetEntries()));
1250 // To clean entries one by one
1251 TIter iter(fEntryCache.GetTable());
1253 while((pair= dynamic_cast<TPair*> (iter.Next()))){
1255 TObjString* key = dynamic_cast<TObjString*> (pair->Key());
1256 AliCDBEntry* entry = dynamic_cast<AliCDBEntry*> (pair->Value());
1257 AliDebug(2, Form("Deleting entry: %s", key->GetName()));
1258 if (entry) delete entry;
1259 delete fEntryCache.Remove(key);
1262 fEntryCache.DeleteAll();
1263 AliDebug(2, Form("After deleting - Cache entries: %d",fEntryCache.GetEntries()));
1266 //_____________________________________________________________________________
1267 void AliCDBManager::UnloadFromCache(const char* path){
1268 // unload cached object
1269 // that is remove the entry from the cache and the id from the list of ids
1271 if(!fActiveStorages.GetEntries()) {
1272 AliDebug(2, Form("No active storages. Object \"%s\" is not unloaded from cache", path));
1276 AliCDBPath queryPath(path);
1277 if(!queryPath.IsValid()) return;
1279 if(!queryPath.IsWildcard()) { // path is not wildcard, get it directly from the cache and unload it!
1280 if(fEntryCache.Contains(path)){
1281 AliDebug(2, Form("Unloading object \"%s\" from cache and from list of ids", path));
1282 TObjString pathStr(path);
1283 delete fEntryCache.Remove(&pathStr);
1286 while((id = dynamic_cast<AliCDBId*> (iter.Next()))){
1287 if(queryPath.Comprises(id->GetPath()))
1288 delete fIds->Remove(id);
1291 AliError(Form("Cache does not contain object \"%s\"!", path));
1293 AliDebug(2, Form("Cache entries: %d",fEntryCache.GetEntries()));
1297 // path is wildcard: loop on the cache and unload all comprised objects!
1298 TIter iter(fEntryCache.GetTable());
1302 while((pair = dynamic_cast<TPair*> (iter.Next()))){
1303 AliCDBPath entryPath = pair->Key()->GetName();
1304 if(queryPath.Comprises(entryPath)) {
1305 AliDebug(2, Form("Unloading object \"%s\" from cache and from list of ids", entryPath.GetPath().Data()));
1306 TObjString pathStr(entryPath.GetPath());
1307 delete fEntryCache.Remove(&pathStr);
1309 TIter iterids(fIds);
1311 while((anId = dynamic_cast<AliCDBId*> (iterids.Next()))){
1312 AliCDBPath aPath = anId->GetPath();
1313 TString aPathStr = aPath.GetPath();
1314 if(queryPath.Comprises(aPath)) {
1315 delete fIds->Remove(anId);
1321 AliDebug(2,Form("Cache entries and ids removed: %d Remaining: %d",removed,fEntryCache.GetEntries()));
1324 //_____________________________________________________________________________
1325 void AliCDBManager::DestroyActiveStorages() {
1326 // delete list of active storages
1328 fActiveStorages.DeleteAll();
1329 fSpecificStorages.DeleteAll();
1332 //_____________________________________________________________________________
1333 void AliCDBManager::DestroyActiveStorage(AliCDBStorage* /*storage*/) {
1334 // destroys active storage
1337 TIter iter(fActiveStorages.GetTable());
1339 while ((aPair = (TPair*) iter.Next())) {
1340 if(storage == (AliCDBStorage*) aPair->Value())
1341 delete fActiveStorages.Remove(aPair->Key());
1342 storage->Delete(); storage=0x0;
1348 //_____________________________________________________________________________
1349 void AliCDBManager::QueryCDB() {
1350 // query default and specific storages for files valid for fRun. Every storage loads the Ids into its list.
1353 AliError("Run number not yet set! Use AliCDBManager::SetRun.");
1356 if (!fDefaultStorage){
1357 AliError("Default storage is not set! Use AliCDBManager::SetDefaultStorage");
1360 if(fDefaultStorage->GetType() == "alien"){
1361 fDefaultStorage->QueryCDB(fRun);
1363 AliDebug(2,"Skipping query for valid files, it used only in grid...");
1366 TIter iter(&fSpecificStorages);
1367 TObjString *aCalibType=0;
1368 AliCDBParam* aPar=0;
1369 while((aCalibType = dynamic_cast<TObjString*> (iter.Next()))){
1370 aPar = (AliCDBParam*) fSpecificStorages.GetValue(aCalibType);
1372 AliDebug(2,Form("Querying specific storage %s",aCalibType->GetName()));
1373 AliCDBStorage *aStorage = GetStorage(aPar);
1374 if(aStorage->GetType() == "alien"){
1375 aStorage->QueryCDB(fRun,aCalibType->GetName());
1378 "Skipping query for valid files, it is used only in grid...");
1384 //______________________________________________________________________________________________
1385 const char* AliCDBManager::GetDataTypeName(DataType type)
1387 // returns the name (string) of the data type
1390 case kCondition: return "Conditions";
1391 case kReference: return "Reference";
1392 case kPrivate: return "Private";
1398 //______________________________________________________________________________________________
1399 void AliCDBManager::InitShortLived()
1401 // Init the list of short-lived objects
1402 // currently disabled
1406 // fShortLived = new TList();
1407 // fShortLived->SetOwner(1);
1409 // fShortLived->Add(new TObjString("EMCAL/Calib/Data"));
1411 // fShortLived->Add(new TObjString("HMPID/Calib/Nmean"));
1412 // fShortLived->Add(new TObjString("HMPID/Calib/Qthre"));
1414 // fShortLived->Add(new TObjString("ITS/Calib/CalibSPD"));
1416 // fShortLived->Add(new TObjString("MUON/Calib/Gains"));
1417 // fShortLived->Add(new TObjString("MUON/Calib/HV"));
1418 // fShortLived->Add(new TObjString("MUON/Calib/Pedestals"));
1420 // fShortLived->Add(new TObjString("PHOS/Calib/CpvGainPedestals"));
1421 // fShortLived->Add(new TObjString("PHOS/Calib/EmcGainPedestals"));
1423 // fShortLived->Add(new TObjString("PMD/Calib/Data"));
1425 // fShortLived->Add(new TObjString("TRD/Calib/ChamberGainFactor"));
1426 // fShortLived->Add(new TObjString("TRD/Calib/LocalGainFactor"));
1427 // fShortLived->Add(new TObjString("TRD/Calib/ChamberT0"));
1428 // fShortLived->Add(new TObjString("TRD/Calib/LocalT0"));
1429 // fShortLived->Add(new TObjString("TRD/Calib/ChamberVdrift"));
1430 // fShortLived->Add(new TObjString("TRD/Calib/LocalVdrift"));
1432 // fShortLived->Add(new TObjString("ZDC/Calib/Data"));
1436 //______________________________________________________________________________________________
1437 Bool_t AliCDBManager::IsShortLived(const char* path)
1439 // returns the name (string) of the data type
1441 if(!fShortLived) return kFALSE;
1443 AliCDBPath aPath(path);
1444 if(!aPath.IsValid()){
1445 AliError(Form("Not a valid path: %s", path));
1449 return fShortLived->Contains(path);
1453 //______________________________________________________________________________________________
1454 ULong_t AliCDBManager::SetLock(Bool_t lock, ULong_t key){
1455 // To lock/unlock user must provide the key. A new key is provided after
1456 // each successful lock. User should always backup the returned key and
1457 // use it on next access.
1458 if (fLock == lock) return 0; // nothing to be done
1460 // User wants to lock - check his identity
1462 // Lock has a user - check his key
1464 AliFatal("Wrong key provided to lock CDB. Please remove CDB lock access from your code !");
1469 fKey = gSystem->Now();
1473 // User wants to unlock - check the provided key
1475 AliFatal("Lock is ON: wrong key provided");
1482 ///////////////////////////////////////////////////////////
1483 // AliCDBManager Parameter class //
1484 // interface to specific AliCDBParameter class //
1485 // (AliCDBGridParam, AliCDBLocalParam, AliCDBDumpParam) //
1486 ///////////////////////////////////////////////////////////
1488 AliCDBParam::AliCDBParam():
1496 //_____________________________________________________________________________
1497 AliCDBParam::~AliCDBParam() {