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>
44 ClassImp(AliCDBManager)
46 //TODO OCDB and Reference folder should not be fully hardcoded but built from run number (or year/LHC period)
47 TString AliCDBManager::fgkCondUri("alien://folder=/alice/cern.ch/user/a/aliprod/testCDB/CDB?user=aliprod");
48 TString AliCDBManager::fgkRefUri("alien://folder=/alice/cern.ch/user/a/aliprod/testCDB/Reference?user=aliprod");
49 TString AliCDBManager::fgkMCIdealStorage("alien://folder=/alice/simulation/2008/v4-15-Release/Ideal");
50 TString AliCDBManager::fgkMCFullStorage("alien://folder=/alice/simulation/2008/v4-15-Release/Full");
51 TString AliCDBManager::fgkMCResidualStorage("alien://folder=/alice/simulation/2008/v4-15-Release/Residual");
52 TString AliCDBManager::fgkOCDBFolderXMLfile("alien:///alice/data/OCDBFoldervsRunRange.xml");
53 AliCDBManager* AliCDBManager::fgInstance = 0x0;
55 //_____________________________________________________________________________
56 AliCDBManager* AliCDBManager::Instance(TMap *entryCache, Int_t run) {
57 // returns AliCDBManager instance (singleton)
60 fgInstance = new AliCDBManager();
64 fgInstance->InitFromCache(entryCache,run);
70 //_____________________________________________________________________________
71 void AliCDBManager::Init() {
72 // factory registering
74 RegisterFactory(new AliCDBDumpFactory());
75 RegisterFactory(new AliCDBLocalFactory());
76 // AliCDBGridFactory is registered only if AliEn libraries are enabled in Root
77 if(!gSystem->Exec("root-config --has-alien 2>/dev/null |grep yes 2>&1 > /dev/null")){ // returns 0 if yes
78 AliInfo("AliEn classes enabled in Root. AliCDBGrid factory registered.");
79 RegisterFactory(new AliCDBGridFactory());
80 fCondParam = CreateParameter(fgkCondUri);
81 fRefParam = CreateParameter(fgkRefUri);
87 //_____________________________________________________________________________
88 void AliCDBManager::InitFromCache(TMap *entryCache, Int_t run) {
89 // initialize manager from existing cache
90 // used on the slaves in case of parallel reconstruction
93 TIter iter(entryCache->GetTable());
96 while((pair = dynamic_cast<TPair*> (iter.Next()))){
97 fEntryCache.Add(pair->Key(),pair->Value());
99 // fEntry is the new owner of the cache
100 fEntryCache.SetOwnerKeyValue(kTRUE,kTRUE);
101 entryCache->SetOwnerKeyValue(kFALSE,kFALSE);
102 AliInfo(Form("%d cache entries have been loaded",fEntryCache.GetEntries()));
105 //_____________________________________________________________________________
106 void AliCDBManager::DumpToSnapshotFile(const char* snapshotFileName, Bool_t singleKeys) const {
108 // If singleKeys is true, dump the entries map and the ids list to the snapshot file
109 // (provided mostly for historical reasons, the file is then read with InitFromSnapshot),
110 // otherwise write to file each AliCDBEntry separately (the is the preferred way, the file
111 // is then read with SetSnapshotMode).
114 TFile *f = TFile::Open(snapshotFileName,"RECREATE");
115 if (!f || f->IsZombie()){
116 AliError(Form("Cannot open file %s",snapshotFileName));
120 AliInfo(Form("Dumping entriesMap (entries'cache) with %d entries!\n", fEntryCache.GetEntries()));
121 AliInfo(Form("Dumping entriesList with %d entries!\n", fIds->GetEntries()));
125 f->WriteObject(&fEntryCache,"CDBentriesMap");
126 f->WriteObject(fIds,"CDBidsList");
128 // We write the entries one by one named by their calibration path
129 TIter iter(fEntryCache.GetTable());
131 while((pair = dynamic_cast<TPair*> (iter.Next()))){
132 TObjString *os = dynamic_cast<TObjString*>(pair->Key());
134 TString path = os->GetString();
135 AliCDBEntry *entry = dynamic_cast<AliCDBEntry*>(pair->Value());
136 if (!entry) continue;
137 path.ReplaceAll("/","*");
138 entry->Write(path.Data());
145 //_____________________________________________________________________________
146 void AliCDBManager::DumpToLightSnapshotFile(const char* lightSnapshotFileName) const {
147 // The light snapshot does not contain the CDB objects (AliCDBEntries) but
148 // only the information identifying them, that is the map of storages and
149 // the list of AliCDBIds, as in the UserInfo of AliESDs.root
152 TFile *f = TFile::Open(lightSnapshotFileName,"RECREATE");
153 if (!f || f->IsZombie()){
154 AliError(Form("Cannot open file %s",lightSnapshotFileName));
158 AliInfo(Form("Dumping map of storages with %d entries!\n", fStorageMap->GetEntries()));
159 AliInfo(Form("Dumping entriesList with %d entries!\n", fIds->GetEntries()));
160 f->WriteObject(fStorageMap,"cdbStoragesMap");
161 f->WriteObject(fIds,"CDBidsList");
167 //_____________________________________________________________________________
168 Bool_t AliCDBManager::InitFromSnapshot(const char* snapshotFileName, Bool_t overwrite){
169 // initialize manager from a CDB snapshot, that is add the entries
170 // to the entries map and the ids to the ids list taking them from
171 // the map and the list found in the input file
173 // if the manager is locked it cannot initialize from a snapshot
175 AliError("Being locked I cannot initialize from the snapshot!");
180 TString snapshotFile(snapshotFileName);
181 if(snapshotFile.BeginsWith("alien://")){
183 TGrid::Connect("alien://","");
185 AliError("Connection to alien failed!");
191 TFile *f = TFile::Open(snapshotFileName);
192 if (!f || f->IsZombie()){
193 AliError(Form("Cannot open file %s",snapshotFileName));
197 // retrieve entries' map from snapshot file
198 TMap *entriesMap = 0;
199 TIter next(f->GetListOfKeys());
201 while ((key = (TKey*)next())) {
202 if (strcmp(key->GetClassName(),"TMap") != 0) continue;
203 entriesMap = (TMap*)key->ReadObj();
206 if (!entriesMap || entriesMap->GetEntries()==0){
207 AliError("Cannot get valid map of CDB entries from snapshot file");
211 // retrieve ids' list from snapshot file
213 TIter nextKey(f->GetListOfKeys());
215 while ((keyN = (TKey*)nextKey())) {
216 if (strcmp(keyN->GetClassName(),"TList") != 0) continue;
217 idsList = (TList*)keyN->ReadObj();
220 if (!idsList || idsList->GetEntries()==0){
221 AliError("Cannot get valid list of CDB entries from snapshot file");
225 // Add each (entry,id) from the snapshot to the memory: entry to the cache, id to the list of ids.
226 // If "overwrite" is false: add the entry to the cache and its id to the list of ids
227 // only if neither of them is already there.
228 // If "overwrite" is true: write the snapshot entry,id in any case. If something
229 // was already there for that calibration type, remove it and issue a warning
230 TIter iterObj(entriesMap->GetTable());
233 while((pair = dynamic_cast<TPair*> (iterObj.Next()))){
234 TObjString* os = (TObjString*) pair->Key();
235 TString path = os->GetString();
236 TIter iterId(idsList);
238 AliCDBId* correspondingId=0;
239 while((id = dynamic_cast<AliCDBId*> (iterId.Next()))){
240 TString idpath(id->GetPath());
246 if(!correspondingId){
247 AliError(Form("id for \"%s\" not found in the snapshot (while entry was). This entry is skipped!",path.Data()));
250 Bool_t cached = fEntryCache.Contains(path.Data());
251 Bool_t registeredId = kFALSE;
254 while((idT = dynamic_cast<AliCDBId*> (iter.Next()))){
255 if(idT->GetPath()==path){
256 registeredId = kTRUE;
262 if(cached || registeredId){
263 AliWarning(Form("An entry was already cached for \"%s\". Removing it before caching from snapshot",path.Data()));
264 UnloadFromCache(path.Data());
266 fEntryCache.Add(pair->Key(),pair->Value());
270 if(cached || registeredId){
271 AliWarning(Form("An entry was already cached for \"%s\". Not adding this object from snapshot",path.Data()));
273 fEntryCache.Add(pair->Key(),pair->Value());
280 // fEntry is the new owner of the cache
281 fEntryCache.SetOwnerKeyValue(kTRUE,kTRUE);
282 entriesMap->SetOwnerKeyValue(kFALSE,kFALSE);
283 fIds->SetOwner(kTRUE);
284 idsList->SetOwner(kFALSE);
285 AliInfo(Form("%d new (entry,id) cached. Total number %d",nAdded,fEntryCache.GetEntries()));
293 //_____________________________________________________________________________
294 void AliCDBManager::Destroy() {
295 // delete ALCDBManager instance and active storages
298 //fgInstance->Delete();
304 //_____________________________________________________________________________
305 AliCDBManager::AliCDBManager():
314 fDefaultStorage(NULL),
321 fSnapshotMode(kFALSE),
323 fOCDBUploadMode(kFALSE),
326 fStartRunLHCPeriod(-1),
327 fEndRunLHCPeriod(-1),
331 // default constuctor
332 fFactories.SetOwner(1);
333 fActiveStorages.SetOwner(1);
334 fSpecificStorages.SetOwner(1);
335 fEntryCache.SetName("CDBEntryCache");
336 fEntryCache.SetOwnerKeyValue(kTRUE,kTRUE);
338 fStorageMap = new TMap();
339 fStorageMap->SetOwner(1);
344 //_____________________________________________________________________________
345 AliCDBManager::~AliCDBManager() {
348 DestroyActiveStorages();
351 fDefaultStorage = 0x0;
352 delete fStorageMap; fStorageMap = 0;
353 delete fIds; fIds = 0;
356 delete fShortLived; fShortLived = 0x0;
357 //fSnapshotCache = 0;
358 //fSnapshotIdsList = 0;
360 fSnapshotFile->Close();
365 //_____________________________________________________________________________
366 void AliCDBManager::PutActiveStorage(AliCDBParam* param, AliCDBStorage* storage){
367 // put a storage object into the list of active storages
369 fActiveStorages.Add(param, storage);
370 AliDebug(1, Form("Active storages: %d", fActiveStorages.GetEntries()));
373 //_____________________________________________________________________________
374 void AliCDBManager::RegisterFactory(AliCDBStorageFactory* factory) {
375 // add a storage factory to the list of registerd factories
377 if (!fFactories.Contains(factory)) {
378 fFactories.Add(factory);
382 //_____________________________________________________________________________
383 Bool_t AliCDBManager::HasStorage(const char* dbString) const {
384 // check if dbString is a URI valid for one of the registered factories
386 TIter iter(&fFactories);
388 AliCDBStorageFactory* factory=0;
389 while ((factory = (AliCDBStorageFactory*) iter.Next())) {
391 if (factory->Validate(dbString)) {
399 //_____________________________________________________________________________
400 AliCDBParam* AliCDBManager::CreateParameter(const char* dbString) const {
401 // create AliCDBParam object from URI string
403 TString uriString(dbString);
405 if ( !fCvmfsOcdb.IsNull() && uriString.BeginsWith("alien://")) {
406 AlienToCvmfsUri(uriString);
409 TIter iter(&fFactories);
411 AliCDBStorageFactory* factory=0;
412 while ((factory = (AliCDBStorageFactory*) iter.Next())) {
413 AliCDBParam* param = factory->CreateParameter(uriString);
414 if(param) return param;
420 //_____________________________________________________________________________
421 void AliCDBManager::AlienToCvmfsUri(TString& uriString) const {
422 // convert alien storage uri to local:///cvmfs storage uri (called when OCDB_PATH is set)
424 TObjArray *arr = uriString.Tokenize('?');
427 TString entryKey = "";
428 TString entryValue = "";
429 TString newUriString = "";
430 while((str = (TObjString*) iter.Next())){
431 TString entry(str->String());
432 Int_t indeq = entry.Index('=');
433 entryKey = entry(0, indeq+1);
434 entryValue = entry(indeq+1, entry.Length()-indeq);
436 if ( entryKey.Contains("folder", TString::kIgnoreCase) )
439 TRegexp re_RawFolder("^/alice/data/20[0-9]+/OCDB");
440 TRegexp re_MCFolder("^/alice/simulation/2008/v4-15-Release");
441 TString rawFolder = entryValue(re_RawFolder);
442 TString mcFolder = entryValue(re_MCFolder);
443 if ( !rawFolder.IsNull() ){
444 entryValue.Replace(0, 6, "/cvmfs/alice-ocdb.cern.ch/calibration");
445 //entryValue.Replace(entryValue.Length()-4, entryValue.Length(), "");
446 } else if ( !mcFolder.IsNull() ){
447 entryValue.Replace(0,36,"/cvmfs/alice-ocdb.cern.ch/calibration/MC");
449 AliFatal(Form("Environment variable for cvmfs OCDB folder set for an invalid OCDB storage:\n %s", entryValue.Data()));
452 newUriString += entryKey;
454 newUriString += entryValue;
457 newUriString.Prepend("local://");
458 newUriString.Remove(TString::kTrailing, '?');
459 uriString = newUriString;
462 //_____________________________________________________________________________
463 AliCDBStorage* AliCDBManager::GetStorage(const char* dbString) {
464 // Get the CDB storage corresponding to the URI string passed as argument
465 // If "raw://" is passed, get the storage for the raw OCDB for the current run (fRun)
467 TString uriString(dbString);
468 if (uriString.EqualTo("raw://")) {
469 if (!fLHCPeriod.IsNull() && !fLHCPeriod.IsWhitespace()) {
470 return GetDefaultStorage();
472 TString lhcPeriod("");
473 Int_t startRun = -1, endRun = -1;
474 GetLHCPeriodAgainstAlienFile(fRun, lhcPeriod, startRun, endRun);
475 return GetStorage(lhcPeriod.Data());
479 AliCDBParam* param = CreateParameter(dbString);
481 AliError(Form("Failed to activate requested storage! Check URI: %s", dbString));
485 AliCDBStorage* aStorage = GetStorage(param);
491 //_____________________________________________________________________________
492 AliCDBStorage* AliCDBManager::GetStorage(const AliCDBParam* param) {
493 // get storage object from AliCDBParam object
495 // if the list of active storages already contains
496 // the requested storage, return it
497 AliCDBStorage* aStorage = GetActiveStorage(param);
502 // if lock is ON, cannot activate more storages!
504 if (fDefaultStorage) {
505 AliFatal("Lock is ON, and default storage is already set: "
506 "cannot reset it or activate more storages!");
510 // loop on the list of registered factories
511 TIter iter(&fFactories);
512 AliCDBStorageFactory* factory=0;
513 while ((factory = (AliCDBStorageFactory*) iter.Next())) {
515 // each factory tries to create its storage from the parameter
516 aStorage = factory->Create(param);
518 PutActiveStorage(param->CloneParam(), aStorage);
519 aStorage->SetURI(param->GetURI());
521 if( aStorage->GetType() == "alien" || aStorage->GetType() == "local" )
522 aStorage->QueryCDB(fRun);
528 AliError(Form("Failed to activate requested storage! Check URI: %s", param->GetURI().Data()));
533 //_____________________________________________________________________________
534 AliCDBStorage* AliCDBManager::GetActiveStorage(const AliCDBParam* param) {
535 // get a storage object from the list of active storages
537 return dynamic_cast<AliCDBStorage*> (fActiveStorages.GetValue(param));
540 //_____________________________________________________________________________
541 TList* AliCDBManager::GetActiveStorages() {
542 // return list of active storages
543 // user has responsibility to delete returned object
545 TList* result = new TList();
547 TIter iter(fActiveStorages.GetTable());
549 while ((aPair = (TPair*) iter.Next())) {
550 result->Add(aPair->Value());
556 //_____________________________________________________________________________
557 void AliCDBManager::SetDrain(const char* dbString) {
558 // set drain storage from URI string
560 fDrainStorage = GetStorage(dbString);
563 //_____________________________________________________________________________
564 void AliCDBManager::SetDrain(const AliCDBParam* param) {
565 // set drain storage from AliCDBParam
567 fDrainStorage = GetStorage(param);
570 //_____________________________________________________________________________
571 void AliCDBManager::SetDrain(AliCDBStorage* storage) {
572 // set drain storage from another active storage
574 fDrainStorage = storage;
577 //_____________________________________________________________________________
578 Bool_t AliCDBManager::Drain(AliCDBEntry *entry) {
579 // drain retrieved object to drain storage
581 AliDebug(2, "Draining into drain storage...");
582 return fDrainStorage->Put(entry);
585 //____________________________________________________________________________
586 Bool_t AliCDBManager::SetOCDBUploadMode() {
587 // Set the framework in official upload mode. This tells the framework to upload
588 // objects to cvmfs after they have been uploaded to AliEn OCDBs.
589 // It return false if the executable to upload to cvmfs is not found.
591 TString cvmfsUploadExecutable("$HOME/bin/ocdb-cvmfs");
592 gSystem->ExpandPathName(cvmfsUploadExecutable);
593 if ( gSystem->AccessPathName(cvmfsUploadExecutable) )
595 fOCDBUploadMode = kTRUE;
599 //____________________________________________________________________________
600 void AliCDBManager::SetDefaultStorage(const char* storageUri) {
601 // sets default storage from URI string
603 // if in the cvmfs case (triggered by environment variable) check for path validity
604 // and modify Uri if it is "raw://"
605 TString cvmfsOcdb(gSystem->Getenv("OCDB_PATH"));
606 if (! cvmfsOcdb.IsNull()){
607 fCvmfsOcdb = cvmfsOcdb;
611 // checking whether we are in the raw case
612 TString uriTemp(storageUri);
613 if (uriTemp == "raw://") {
614 fRaw = kTRUE; // read then by SetRun to check if the method has to be called again with expanded uri
615 AliInfo("Setting the run-number will set the corresponding OCDB for raw data reconstruction.");
619 AliCDBStorage* bckStorage = fDefaultStorage;
621 fDefaultStorage = GetStorage(storageUri);
623 if(!fDefaultStorage) return;
625 if(bckStorage && (fDefaultStorage != bckStorage)){
626 AliWarning("Existing default storage replaced: clearing cache!");
630 if (fStorageMap->Contains("default")) {
631 delete fStorageMap->Remove(((TPair*)fStorageMap->FindObject("default"))->Key());
633 fStorageMap->Add(new TObjString("default"), new TObjString(fDefaultStorage->GetURI()));
636 //_____________________________________________________________________________
637 void AliCDBManager::SetDefaultStorage(const AliCDBParam* param) {
638 // set default storage from AliCDBParam object
640 AliCDBStorage* bckStorage = fDefaultStorage;
642 fDefaultStorage = GetStorage(param);
644 if(!fDefaultStorage) return;
646 if(bckStorage && (fDefaultStorage != bckStorage)){
647 AliWarning("Existing default storage replaced: clearing cache!");
651 if (fStorageMap->Contains("default")) {
652 delete fStorageMap->Remove(((TPair*)fStorageMap->FindObject("default"))->Key());
654 fStorageMap->Add(new TObjString("default"), new TObjString(fDefaultStorage->GetURI()));
657 //_____________________________________________________________________________
658 void AliCDBManager::SetDefaultStorage(AliCDBStorage* storage) {
659 // set default storage from another active storage
661 // if lock is ON, cannot activate more storages!
663 if (fDefaultStorage) {
664 AliFatal("Lock is ON, and default storage is already set: "
665 "cannot reset it or activate more storages!");
670 UnsetDefaultStorage();
674 AliCDBStorage* bckStorage = fDefaultStorage;
676 fDefaultStorage = storage;
678 if(bckStorage && (fDefaultStorage != bckStorage)){
679 AliWarning("Existing default storage replaced: clearing cache!");
683 if (fStorageMap->Contains("default")) {
684 delete fStorageMap->Remove(((TPair*)fStorageMap->FindObject("default"))->Key());
686 fStorageMap->Add(new TObjString("default"), new TObjString(fDefaultStorage->GetURI()));
689 //_____________________________________________________________________________
690 void AliCDBManager::SetDefaultStorage(const char* mcString, const char* simType) {
691 // sets default storage for MC data
692 // mcString MUST be "MC",
693 // simType can be "Ideal","Residual","Full"
695 TString strmcString(mcString);
696 TString strsimType(simType);
698 if (strmcString != "MC"){
699 AliFatal("Method requires first string to be MC!");
702 if (strsimType == "Ideal"){
703 dbString = fgkMCIdealStorage;
705 else if (strsimType == "Full"){
706 dbString = fgkMCFullStorage;
708 else if (strsimType == "Residual"){
709 dbString = fgkMCResidualStorage;
712 AliFatal("Error in setting the storage for MC data, second argument MUST be either \"Ideal\" or \"Full\" or \"Residual\".");
715 SetDefaultStorage(dbString.Data());
716 fStartRunLHCPeriod=0;
717 fEndRunLHCPeriod=AliCDBRunRange::Infinity();
718 if(!fDefaultStorage) AliFatal(Form("%s storage not there! Please check!",dbString.Data()));
722 //_____________________________________________________________________________
723 void AliCDBManager::ValidateCvmfsCase() const {
724 // The OCDB_PATH variable contains the path to the directory in /cvmfs/ which is
725 // an AliRoot tag based snapshot of the AliEn file catalogue (e.g.
726 // /cvmfs/alice.cern.ch/x86_64-2.6-gnu-4.1.2/Packages/OCDB/v5-05-76-AN).
727 // The directory has to contain:
728 // 1) <data|MC>/20??.list.gz gzipped text files listing the OCDB files (seen by that AliRoot tag)
729 // 2) bin/getOCDBFilesPerRun.sh (shell+awk) script extracting from 1) the list
730 // of valid files for the given run.
732 if (! fCvmfsOcdb.BeginsWith("/cvmfs")) //!!!! to be commented out for testing
733 AliFatal(Form("OCDB_PATH set to an invalid path: %s", fCvmfsOcdb.Data()));
735 TString cvmfsUri(fCvmfsOcdb);
736 gSystem->ExpandPathName(cvmfsUri);
737 if (gSystem->AccessPathName(cvmfsUri))
738 AliFatal(Form("OCDB_PATH set to an invalid path: %s", cvmfsUri.Data()));
740 // check that we find the two scripts we need
742 AliDebug(3, "OCDB_PATH envvar is set. Changing OCDB storage from alien:// to local:///cvmfs type.");
743 cvmfsUri = cvmfsUri.Strip(TString::kTrailing, '/');
744 cvmfsUri.Append("/bin/getOCDBFilesPerRun.sh");
745 if (gSystem->AccessPathName(cvmfsUri))
746 AliFatal(Form("Cannot find valid script: %s", cvmfsUri.Data()));
749 //_____________________________________________________________________________
750 void AliCDBManager::SetDefaultStorageFromRun(Int_t run) {
751 // set default storage from the run number - to be used only with raw data
753 // if lock is ON, cannot activate more storages!
755 if (fDefaultStorage) {
756 AliFatal("Lock is ON, and default storage is already set: "
757 "cannot activate default storage from run number");
761 TString lhcPeriod("");
762 Int_t startRun = 0, endRun = 0;
763 if (! fCvmfsOcdb.IsNull()) { // fRaw and cvmfs case: set LHC period from cvmfs file
764 GetLHCPeriodAgainstCvmfsFile(run, lhcPeriod, startRun, endRun);
765 } else { // fRaw: set LHC period from AliEn XML file
766 GetLHCPeriodAgainstAlienFile(run, lhcPeriod, startRun, endRun);
769 fLHCPeriod = lhcPeriod;
770 fStartRunLHCPeriod = startRun;
771 fEndRunLHCPeriod = endRun;
773 SetDefaultStorage(fLHCPeriod.Data());
774 if(!fDefaultStorage) AliFatal(Form("%s storage not there! Please check!",fLHCPeriod.Data()));
778 //_____________________________________________________________________________
779 void AliCDBManager::GetLHCPeriodAgainstAlienFile(Int_t run, TString& lhcPeriod, Int_t& startRun, Int_t& endRun) {
780 // set LHC period (year + first, last run) comparing run number and AliEn XML file
782 // retrieve XML file from alien
784 TGrid::Connect("alien://","");
786 AliError("Connection to alien failed!");
791 TString rndname = "/tmp/";
792 rndname += "OCDBFolderXML.";
793 rndname += uuid.AsString();
795 AliDebug(2, Form("file to be copied = %s", fgkOCDBFolderXMLfile.Data()));
796 if (!TFile::Cp(fgkOCDBFolderXMLfile.Data(), rndname.Data())) {
797 AliFatal(Form("Cannot make a local copy of OCDBFolder xml file in %s",rndname.Data()));
799 AliCDBHandler* saxcdb = new AliCDBHandler();
801 TSAXParser *saxParser = new TSAXParser();
802 saxParser->ConnectToHandler("AliCDBHandler", saxcdb);
803 saxParser->ParseFile(rndname.Data());
804 AliInfo(Form(" LHC folder = %s", saxcdb->GetOCDBFolder().Data()));
805 AliInfo(Form(" LHC period start run = %d", saxcdb->GetStartRunRange()));
806 AliInfo(Form(" LHC period end run = %d", saxcdb->GetEndRunRange()));
807 lhcPeriod = saxcdb->GetOCDBFolder();
808 startRun = saxcdb->GetStartRunRange();
809 endRun = saxcdb->GetEndRunRange();
812 //_____________________________________________________________________________
813 void AliCDBManager::GetLHCPeriodAgainstCvmfsFile(Int_t run, TString& lhcPeriod, Int_t& startRun, Int_t& endRun) {
814 // set LHC period (year + first, last run) comparing run number and CVMFS file
815 // We don't want to connect to AliEn just to set the uri from the runnumber
816 // for that we use the script getUriFromYear.sh in the cvmfs AliRoot package
818 TString getYearScript(fCvmfsOcdb);
819 getYearScript = getYearScript.Strip(TString::kTrailing, '/');
820 getYearScript.Append("/bin/getUriFromYear.sh");
821 if (gSystem->AccessPathName(getYearScript))
822 AliFatal(Form("Cannot find valid script: %s", getYearScript.Data()));
823 TString inoutFile(gSystem->WorkingDirectory());
824 inoutFile += "/uri_range_";
825 inoutFile += TString::Itoa(run,10);
826 TString command(getYearScript);
828 command += TString::Itoa(run,10);
829 command += Form(" > %s", inoutFile.Data());
830 AliDebug(3, Form("Running command: \"%s\"",command.Data()));
831 Int_t result = gSystem->Exec(command.Data());
833 AliFatal(Form("Was not able to execute \"%s\"", command.Data()));
836 // now read the file with the uri and first and last run
837 std::ifstream file(inoutFile.Data());
838 if (!file.is_open()) {
839 AliFatal(Form("Error opening file \"%s\"!", inoutFile.Data()));
842 TObjArray* oStringsArray = 0;
843 while (line.ReadLine(file)){
844 oStringsArray = line.Tokenize(' ');
846 TObjString *oStrUri = dynamic_cast<TObjString*> (oStringsArray->At(0));
847 TObjString *oStrFirst = dynamic_cast<TObjString*> (oStringsArray->At(1));
848 TString firstRun = oStrFirst->GetString();
849 TObjString *oStrLast = dynamic_cast<TObjString*> (oStringsArray->At(2));
850 TString lastRun = oStrLast->GetString();
852 lhcPeriod = oStrUri->GetString();
853 startRun = firstRun.Atoi();
854 endRun = lastRun.Atoi();
859 //_____________________________________________________________________________
860 void AliCDBManager::UnsetDefaultStorage() {
861 // Unset default storage
863 // if lock is ON, action is forbidden!
865 if (fDefaultStorage) {
866 AliFatal("Lock is ON: cannot unset default storage!");
870 if (fDefaultStorage) {
871 AliWarning("Clearing cache!");
875 fRun = fStartRunLHCPeriod = fEndRunLHCPeriod = -1;
878 fDefaultStorage = 0x0;
881 //_____________________________________________________________________________
882 void AliCDBManager::SetSpecificStorage(const char* calibType, const char* dbString, Int_t version, Int_t subVersion) {
883 // sets storage specific for detector or calibration type (works with AliCDBManager::Get(...))
885 AliCDBParam *aPar = CreateParameter(dbString);
887 SetSpecificStorage(calibType, aPar, version, subVersion);
891 //_____________________________________________________________________________
892 void AliCDBManager::SetSpecificStorage(const char* calibType, const AliCDBParam* param, Int_t version, Int_t subVersion) {
893 // sets storage specific for detector or calibration type (works with AliCDBManager::Get(...))
894 // Default storage should be defined prior to any specific storages, e.g.:
895 // AliCDBManager::instance()->SetDefaultStorage("alien://");
896 // AliCDBManager::instance()->SetSpecificStorage("TPC/*","local://DB_TPC");
897 // AliCDBManager::instance()->SetSpecificStorage("*/Align/*","local://DB_TPCAlign");
898 // calibType must be a valid CDB path! (3 level folder structure)
899 // Specific version/subversion is set in the uniqueid of the TObjString used as key
901 if(!fDefaultStorage && !fRaw) {
902 AliError("Please activate a default storage first!");
906 AliCDBPath aPath(calibType);
907 if(!aPath.IsValid()){
908 AliError(Form("Not a valid path: %s", calibType));
912 TObjString *objCalibType = new TObjString(aPath.GetPath());
913 if(fSpecificStorages.Contains(objCalibType)){
914 AliWarning(Form("Storage \"%s\" already activated! It will be replaced by the new one",
916 AliCDBParam *checkPar = dynamic_cast<AliCDBParam*> (fSpecificStorages.GetValue(calibType));
917 if(checkPar) delete checkPar;
918 delete fSpecificStorages.Remove(objCalibType);
920 AliCDBStorage *aStorage = GetStorage(param);
921 if(!aStorage) return;
923 // Set the unique id of the TObjString used as key to store specific version/subversion
924 UInt_t uId = ((subVersion+1)<<16) + (version+1);
925 AliCDBParam *specificParam = param->CloneParam();
926 specificParam->SetUniqueID(uId);
927 fSpecificStorages.Add(objCalibType, specificParam);
929 if(fStorageMap->Contains(objCalibType)){
930 delete fStorageMap->Remove(objCalibType);
932 fStorageMap->Add(objCalibType->Clone(), new TObjString(param->GetURI()));
936 //_____________________________________________________________________________
937 AliCDBStorage* AliCDBManager::GetSpecificStorage(const char* calibType) {
938 // get storage specific for detector or calibration type
940 AliCDBPath calibPath(calibType);
941 if(!calibPath.IsValid()) return NULL;
943 AliCDBParam *checkPar = (AliCDBParam*) fSpecificStorages.GetValue(calibPath.GetPath());
945 AliError(Form("%s storage not found!", calibType));
948 return GetStorage(checkPar);
953 //_____________________________________________________________________________
954 AliCDBParam* AliCDBManager::SelectSpecificStorage(const TString& path) {
955 // select storage valid for path from the list of specific storages
957 AliCDBPath aPath(path);
958 if(!aPath.IsValid()) return NULL;
960 TIter iter(&fSpecificStorages);
961 TObjString *aCalibType=0;
962 AliCDBPath tmpPath("null/null/null");
964 while((aCalibType = (TObjString*) iter.Next())){
965 AliCDBPath calibTypePath(aCalibType->GetName());
966 if(calibTypePath.Comprises(aPath)) {
967 if(calibTypePath.Comprises(tmpPath)) continue;
968 aPar = (AliCDBParam*) fSpecificStorages.GetValue(aCalibType);
969 tmpPath.SetPath(calibTypePath.GetPath());
975 //_____________________________________________________________________________
976 AliCDBEntry* AliCDBManager::Get(const AliCDBPath& path, Int_t runNumber,
977 Int_t version, Int_t subVersion) {
978 // get an AliCDBEntry object from the database
981 // RunNumber is not specified. Try with fRun
983 AliError("Run number neither specified in query nor set in AliCDBManager! Use AliCDBManager::SetRun.");
989 return Get(AliCDBId(path, runNumber, runNumber, version, subVersion));
992 //_____________________________________________________________________________
993 AliCDBEntry* AliCDBManager::Get(const AliCDBPath& path,
994 const AliCDBRunRange& runRange, Int_t version,
996 // get an AliCDBEntry object from the database!
998 return Get(AliCDBId(path, runRange, version, subVersion));
1001 //_____________________________________________________________________________
1002 AliCDBEntry* AliCDBManager::Get(const AliCDBId& queryId, Bool_t forceCaching) {
1003 // get an AliCDBEntry object from the database
1005 // check if queryId's path and runRange are valid
1006 // queryId is invalid also if version is not specified and subversion is!
1007 if (!queryId.IsValid()) {
1008 AliError(Form("Invalid query: %s", queryId.ToString().Data()));
1012 // queryId is not specified if path contains wildcard or run range= [-1,-1]
1013 if (!queryId.IsSpecified()) {
1014 AliError(Form("Unspecified query: %s",
1015 queryId.ToString().Data()));
1019 if(fLock && !(fRun >= queryId.GetFirstRun() && fRun <= queryId.GetLastRun()))
1020 AliFatal("Lock is ON: cannot use different run number than the internal one!");
1022 if(fCache && !(fRun >= queryId.GetFirstRun() && fRun <= queryId.GetLastRun()))
1023 AliWarning("Run number explicitly set in query: CDB cache temporarily disabled!");
1025 AliCDBEntry *entry=0;
1027 // first look into map of cached objects
1028 if(fCache && queryId.GetFirstRun() == fRun)
1029 entry = (AliCDBEntry*) fEntryCache.GetValue(queryId.GetPath());
1031 AliDebug(2, Form("Object %s retrieved from cache !!",queryId.GetPath().Data()));
1035 // if snapshot flag is set, try getting from the snapshot
1036 // but in the case a specific storage is specified for this path
1037 AliCDBParam *aPar=SelectSpecificStorage(queryId.GetPath());
1039 if(fSnapshotMode && queryId.GetFirstRun() == fRun)
1041 entry = GetEntryFromSnapshot(queryId.GetPath());
1043 AliInfo(Form("Object \"%s\" retrieved from the snapshot.",queryId.GetPath().Data()));
1044 if(queryId.GetFirstRun() == fRun) // no need to check fCache, fSnapshotMode not possible otherwise
1045 CacheEntry(queryId.GetPath(), entry);
1047 if(!fIds->Contains(&entry->GetId()))
1048 fIds->Add(entry->GetId().Clone());
1055 // Entry is not in cache (and, in case we are in snapshot mode, not in the snapshot either)
1056 // => retrieve it from the storage and cache it!!
1057 if(!fDefaultStorage) {
1058 AliError("No storage set!");
1062 Int_t version = 0, subVersion = 0;
1063 AliCDBStorage *aStorage=0;
1065 aStorage=GetStorage(aPar);
1066 TString str = aPar->GetURI();
1067 UInt_t uId = aPar->GetUniqueID();
1068 version = Int_t(uId&0xffff) - 1;
1069 subVersion = Int_t(uId>>16) - 1;
1070 AliDebug(2,Form("Looking into storage: %s",str.Data()));
1072 aStorage=GetDefaultStorage();
1073 AliDebug(2,"Looking into default storage");
1076 AliCDBId finalQueryId(queryId);
1078 AliDebug(2,Form("Specific version set to: %d", version));
1079 finalQueryId.SetVersion(version);
1081 if(subVersion >= 0) {
1082 AliDebug(2,Form("Specific subversion set to: %d", subVersion));
1083 finalQueryId.SetSubVersion(subVersion);
1085 entry = aStorage->Get(finalQueryId);
1087 if(entry && fCache && (queryId.GetFirstRun()==fRun || forceCaching)){
1088 CacheEntry(queryId.GetPath(), entry);
1091 if(entry && !fIds->Contains(&entry->GetId())){
1092 fIds->Add(entry->GetId().Clone());
1098 //_____________________________________________________________________________
1099 AliCDBEntry* AliCDBManager::GetEntryFromSnapshot(const char* path) {
1100 // get the entry from the open snapshot file
1102 TString sPath(path);
1103 sPath.ReplaceAll("/","*");
1105 AliError("No snapshot file is open!");
1108 AliCDBEntry *entry = dynamic_cast<AliCDBEntry*>(fSnapshotFile->Get(sPath.Data()));
1110 AliDebug(2,Form("Cannot get a CDB entry for \"%s\" from snapshot file",path));
1117 //_____________________________________________________________________________
1118 Bool_t AliCDBManager::SetSnapshotMode(const char* snapshotFileName) {
1119 // set the manager in snapshot mode
1122 AliError("Cannot set the CDB manage in snapshot mode if the cache is not active!");
1126 //open snapshot file
1127 TString snapshotFile(snapshotFileName);
1128 if(snapshotFile.BeginsWith("alien://")){
1130 TGrid::Connect("alien://","");
1132 AliError("Connection to alien failed!");
1138 fSnapshotFile = TFile::Open(snapshotFileName);
1139 if (!fSnapshotFile || fSnapshotFile->IsZombie()){
1140 AliError(Form("Cannot open file %s",snapshotFileName));
1144 AliInfo("The CDB manager is set in snapshot mode!");
1145 fSnapshotMode = kTRUE;
1150 //_____________________________________________________________________________
1151 const char* AliCDBManager::GetURI(const char* path) {
1152 // return the URI of the storage where to look for path
1154 if(!IsDefaultStorageSet()) return 0;
1156 AliCDBParam *aPar=SelectSpecificStorage(path);
1159 return aPar->GetURI().Data();
1162 return GetDefaultStorage()->GetURI().Data();
1168 //_____________________________________________________________________________
1169 Int_t AliCDBManager::GetStartRunLHCPeriod(){
1170 // get the first run of validity
1171 // for the current period
1173 if(fStartRunLHCPeriod==-1)
1174 AliWarning("Run-range not yet set for the current LHC period.");
1175 return fStartRunLHCPeriod;
1178 //_____________________________________________________________________________
1179 Int_t AliCDBManager::GetEndRunLHCPeriod(){
1180 // get the last run of validity
1181 // for the current period
1183 if(fEndRunLHCPeriod==-1)
1184 AliWarning("Run-range not yet set for the current LHC period.");
1185 return fEndRunLHCPeriod;
1188 //_____________________________________________________________________________
1189 TString AliCDBManager::GetLHCPeriod(){
1190 // get the current LHC period string
1192 if(fLHCPeriod.IsWhitespace() || fLHCPeriod.IsNull())
1193 AliWarning("LHC period (OCDB folder) not yet set");
1197 //_____________________________________________________________________________
1198 AliCDBId* AliCDBManager::GetId(const AliCDBPath& path, Int_t runNumber,
1199 Int_t version, Int_t subVersion) {
1200 // get the AliCDBId of the valid object from the database (does not retrieve the object)
1201 // User must delete returned object!
1204 // RunNumber is not specified. Try with fRun
1206 AliError("Run number neither specified in query nor set in AliCDBManager! Use AliCDBManager::SetRun.");
1212 return GetId(AliCDBId(path, runNumber, runNumber, version, subVersion));
1215 //_____________________________________________________________________________
1216 AliCDBId* AliCDBManager::GetId(const AliCDBPath& path,
1217 const AliCDBRunRange& runRange, Int_t version,
1219 // get the AliCDBId of the valid object from the database (does not retrieve the object)
1220 // User must delete returned object!
1222 return GetId(AliCDBId(path, runRange, version, subVersion));
1225 //_____________________________________________________________________________
1226 AliCDBId* AliCDBManager::GetId(const AliCDBId& query) {
1227 // get the AliCDBId of the valid object from the database (does not retrieve the object)
1228 // User must delete returned object!
1230 if(!fDefaultStorage) {
1231 AliError("No storage set!");
1235 // check if query's path and runRange are valid
1236 // query is invalid also if version is not specified and subversion is!
1237 if (!query.IsValid()) {
1238 AliError(Form("Invalid query: %s", query.ToString().Data()));
1242 // query is not specified if path contains wildcard or run range= [-1,-1]
1243 if (!query.IsSpecified()) {
1244 AliError(Form("Unspecified query: %s",
1245 query.ToString().Data()));
1249 if(fCache && query.GetFirstRun() != fRun)
1250 AliWarning("Run number explicitly set in query: CDB cache temporarily disabled!");
1252 AliCDBEntry* entry = 0;
1254 // first look into map of cached objects
1255 if(fCache && query.GetFirstRun() == fRun)
1256 entry = (AliCDBEntry*) fEntryCache.GetValue(query.GetPath());
1259 AliDebug(2, Form("Object %s retrieved from cache !!",query.GetPath().Data()));
1260 return dynamic_cast<AliCDBId*> (entry->GetId().Clone());
1263 // Entry is not in cache -> retrieve it from CDB and cache it!!
1264 AliCDBStorage *aStorage=0;
1265 AliCDBParam *aPar=SelectSpecificStorage(query.GetPath());
1268 aStorage=GetStorage(aPar);
1269 TString str = aPar->GetURI();
1270 AliDebug(2,Form("Looking into storage: %s",str.Data()));
1273 aStorage=GetDefaultStorage();
1274 AliDebug(2,"Looking into default storage");
1277 return aStorage->GetId(query);
1281 //_____________________________________________________________________________
1282 TList* AliCDBManager::GetAll(const AliCDBPath& path, Int_t runNumber,
1283 Int_t version, Int_t subVersion) {
1284 // get multiple AliCDBEntry objects from the database
1287 // RunNumber is not specified. Try with fRun
1289 AliError("Run number neither specified in query nor set in AliCDBManager! Use AliCDBManager::SetRun.");
1295 return GetAll(AliCDBId(path, runNumber, runNumber, version,
1299 //_____________________________________________________________________________
1300 TList* AliCDBManager::GetAll(const AliCDBPath& path,
1301 const AliCDBRunRange& runRange, Int_t version, Int_t subVersion) {
1302 // get multiple AliCDBEntry objects from the database
1304 return GetAll(AliCDBId(path, runRange, version, subVersion));
1307 //_____________________________________________________________________________
1308 TList* AliCDBManager::GetAll(const AliCDBId& query) {
1309 // get multiple AliCDBEntry objects from the database
1310 // Warning: this method works correctly only for queries of the type "Detector/*"
1311 // and not for more specific queries e.g. "Detector/Calib/*" !
1312 // Warning #2: Entries are cached, but GetAll will keep on retrieving objects from OCDB!
1313 // To get an object from cache use Get() function
1315 if(!fDefaultStorage) {
1316 AliError("No storage set!");
1320 if (!query.IsValid()) {
1321 AliError(Form("Invalid query: %s", query.ToString().Data()));
1325 if((fSpecificStorages.GetEntries()>0) && query.GetPath().BeginsWith('*')){
1326 // if specific storages are active a query with "*" is ambiguous
1327 AliError("Query too generic in this context!");
1331 if (query.IsAnyRange()) {
1332 AliError(Form("Unspecified run or runrange: %s",
1333 query.ToString().Data()));
1337 if(fLock && query.GetFirstRun() != fRun)
1338 AliFatal("Lock is ON: cannot use different run number than the internal one!");
1340 AliCDBParam *aPar=SelectSpecificStorage(query.GetPath());
1342 AliCDBStorage *aStorage;
1344 aStorage=GetStorage(aPar);
1345 AliDebug(2,Form("Looking into storage: %s", aPar->GetURI().Data()));
1348 aStorage=GetDefaultStorage();
1349 AliDebug(2,Form("Looking into default storage: %s", aStorage->GetURI().Data()));
1353 if(aStorage) result = aStorage->GetAll(query);
1354 if(!result) return 0;
1356 // loop on result to check whether entries should be re-queried with specific storages
1357 if(fSpecificStorages.GetEntries()>0 && ! (fSpecificStorages.GetEntries() == 1 && aPar)) {
1358 AliInfo("Now look into all other specific storages...");
1361 AliCDBEntry* chkEntry=0;
1363 while((chkEntry = dynamic_cast<AliCDBEntry*> (iter.Next()))){
1364 AliCDBId& chkId = chkEntry->GetId();
1365 AliDebug(2, Form("Checking id %s ", chkId.GetPath().Data()));
1366 AliCDBParam *chkPar=SelectSpecificStorage(chkId.GetPath());
1367 if (!chkPar || aPar == chkPar) continue;
1368 AliCDBStorage *chkStorage = GetStorage(chkPar);
1369 AliDebug(2, Form("Found specific storage! %s", chkPar->GetURI().Data()));
1371 AliCDBEntry *newEntry=0;
1372 chkId.SetRunRange(query.GetFirstRun(), query.GetLastRun());
1373 chkId.SetVersion(query.GetVersion());
1374 chkId.SetSubVersion(query.GetSubVersion());
1376 if(chkStorage) newEntry = chkStorage->Get(chkId);
1377 if(!newEntry) continue;
1379 // object is found in specific storage: replace entry in the result list!
1380 chkEntry->SetOwner(1);
1381 delete result->Remove(chkEntry);
1382 result->AddFirst(newEntry);
1385 Int_t nEntries = result->GetEntries();
1386 AliInfo("After look into other specific storages, result list is:");
1387 for(int i=0; i<nEntries;i++){
1388 AliCDBEntry *entry = (AliCDBEntry*) result->At(i);
1389 AliInfo(Form("%s",entry->GetId().ToString().Data()));
1395 AliCDBEntry* entry=0;
1396 while((entry = dynamic_cast<AliCDBEntry*> (iter.Next()))){
1398 if(!fIds->Contains(&entry->GetId())){
1399 fIds->Add(entry->GetId().Clone());
1401 if(fCache && (query.GetFirstRun() == fRun)){
1402 CacheEntry(entry->GetId().GetPath(), entry);
1410 //_____________________________________________________________________________
1411 Bool_t AliCDBManager::Put(TObject* object, const AliCDBId& id, AliCDBMetaData* metaData, const char* mirrors, DataType type){
1412 // store an AliCDBEntry object into the database
1415 AliError("Null Entry! No storage will be done!");
1419 AliCDBEntry anEntry(object, id, metaData);
1420 return Put(&anEntry, mirrors, type);
1425 //_____________________________________________________________________________
1426 Bool_t AliCDBManager::Put(AliCDBEntry* entry, const char* mirrors, DataType type){
1427 // store an AliCDBEntry object into the database
1429 if(type == kPrivate && !fDefaultStorage) {
1430 AliError("No storage set!");
1435 AliError("No entry!");
1439 if (entry->GetObject()==0x0){
1440 AliError("No valid object in CDB entry!");
1444 if (!entry->GetId().IsValid()) {
1445 AliError(Form("Invalid entry ID: %s",
1446 entry->GetId().ToString().Data()));
1450 if (!entry->GetId().IsSpecified()) {
1451 AliError(Form("Unspecified entry ID: %s",
1452 entry->GetId().ToString().Data()));
1456 AliCDBId id = entry->GetId();
1457 AliCDBParam *aPar = SelectSpecificStorage(id.GetPath());
1459 AliCDBStorage *aStorage=0;
1462 aStorage=GetStorage(aPar);
1466 aStorage = GetStorage(fCondParam);
1469 aStorage = GetStorage(fRefParam);
1472 aStorage = GetDefaultStorage();
1477 AliDebug(2,Form("Storing object into storage: %s", aStorage->GetURI().Data()));
1479 TString strMirrors(mirrors);
1480 Bool_t result = kFALSE;
1481 if(!strMirrors.IsNull() && !strMirrors.IsWhitespace())
1482 result = aStorage->Put(entry, mirrors, type);
1484 result = aStorage->Put(entry, "", type);
1486 if(fRun >= 0) QueryCDB();
1493 //_____________________________________________________________________________
1494 void AliCDBManager::SetMirrorSEs(const char* mirrors) {
1495 // set mirror Storage Elements for the default storage, if it is of type "alien"
1496 if(fDefaultStorage->GetType() != "alien"){
1497 AliInfo("The default storage is not of type \"alien\". Settings for Storage Elements are not taken into account!");
1500 fDefaultStorage->SetMirrorSEs(mirrors);
1503 //_____________________________________________________________________________
1504 const char* AliCDBManager::GetMirrorSEs() const {
1505 // get mirror Storage Elements for the default storage, if it is of type "alien"
1506 if(fDefaultStorage->GetType() != "alien"){
1507 AliInfo("The default storage is not of type \"alien\". Settings for Storage Elements are not taken into account!");
1510 return fDefaultStorage->GetMirrorSEs();
1513 //_____________________________________________________________________________
1514 void AliCDBManager::CacheEntry(const char* path, AliCDBEntry* entry) {
1515 // cache AliCDBEntry. Cache is valid until run number is changed.
1517 AliCDBEntry *chkEntry = dynamic_cast<AliCDBEntry*> (fEntryCache.GetValue(path));
1520 AliDebug(2, Form("Object %s already in cache !!", path));
1523 AliDebug(2,Form("Caching entry %s", path));
1526 fEntryCache.Add(new TObjString(path), entry);
1527 AliDebug(2,Form("Cache entries: %d", fEntryCache.GetEntries()));
1531 //_____________________________________________________________________________
1532 void AliCDBManager::Print(Option_t* /*option*/) const {
1533 // Print list of active storages and their URIs
1535 TString output=Form("Run number = %d; ",fRun);
1536 output += "Cache is ";
1537 if(!fCache) output += "NOT ";
1538 output += Form("ACTIVE; Number of active storages: %d\n",fActiveStorages.GetEntries());
1540 if(fDefaultStorage) {
1541 output += Form("\t*** Default Storage URI: \"%s\"\n",fDefaultStorage->GetURI().Data());
1542 // AliInfo(output.Data());
1544 if(fSpecificStorages.GetEntries()>0) {
1545 TIter iter(fSpecificStorages.GetTable());
1548 while((aPair = (TPair*) iter.Next())){
1549 output += Form("\t*** Specific storage %d: Path \"%s\" -> URI \"%s\"\n",
1550 i++, ((TObjString*) aPair->Key())->GetName(),
1551 ((AliCDBParam*) aPair->Value())->GetURI().Data());
1555 output += Form("*** Drain Storage URI: %s\n",fDrainStorage->GetURI().Data());
1557 AliInfo(output.Data());
1560 //_____________________________________________________________________________
1561 void AliCDBManager::SetRun(Int_t run) {
1562 // Sets current run number.
1563 // When the run number changes the caching is cleared.
1568 if(fLock && fRun >= 0) {
1569 AliFatal("Lock is ON, cannot reset run number!");
1575 // here the LHCPeriod xml file is parsed; the string containing the correct period is returned; the default storage is set
1576 if (fStartRunLHCPeriod <= run && fEndRunLHCPeriod >= run){
1577 AliInfo("LHCPeriod alien folder for current run already in memory");
1579 SetDefaultStorageFromRun(fRun);
1580 if(fEntryCache.GetEntries()!=0) ClearCache();
1588 //_____________________________________________________________________________
1589 void AliCDBManager::ClearCache(){
1590 // clear AliCDBEntry cache
1592 AliDebug(2, Form("Cache entries to be deleted: %d",fEntryCache.GetEntries()));
1595 // To clean entries one by one
1596 TIter iter(fEntryCache.GetTable());
1598 while((pair= dynamic_cast<TPair*> (iter.Next()))){
1600 TObjString* key = dynamic_cast<TObjString*> (pair->Key());
1601 AliCDBEntry* entry = dynamic_cast<AliCDBEntry*> (pair->Value());
1602 AliDebug(2, Form("Deleting entry: %s", key->GetName()));
1603 if (entry) delete entry;
1604 delete fEntryCache.Remove(key);
1607 fEntryCache.DeleteAll();
1608 AliDebug(2, Form("After deleting - Cache entries: %d",fEntryCache.GetEntries()));
1611 //_____________________________________________________________________________
1612 void AliCDBManager::UnloadFromCache(const char* path){
1613 // unload cached object
1614 // that is remove the entry from the cache and the id from the list of ids
1616 if(!fActiveStorages.GetEntries()) {
1617 AliDebug(2, Form("No active storages. Object \"%s\" is not unloaded from cache", path));
1621 AliCDBPath queryPath(path);
1622 if(!queryPath.IsValid()) return;
1624 if(!queryPath.IsWildcard()) { // path is not wildcard, get it directly from the cache and unload it!
1625 if(fEntryCache.Contains(path)){
1626 AliDebug(2, Form("Unloading object \"%s\" from cache and from list of ids", path));
1627 TObjString pathStr(path);
1628 delete fEntryCache.Remove(&pathStr);
1629 // we do not remove from the list of Id's (it's not very coherent but we leave the
1630 // id for the benefit of the userinfo)
1634 while((id = dynamic_cast<AliCDBId*> (iter.Next()))){
1635 if(queryPath.Comprises(id->GetPath()))
1636 delete fIds->Remove(id);
1639 AliWarning(Form("Cache does not contain object \"%s\"!", path));
1641 AliDebug(2, Form("Cache entries: %d",fEntryCache.GetEntries()));
1645 // path is wildcard: loop on the cache and unload all comprised objects!
1646 TIter iter(fEntryCache.GetTable());
1650 while((pair = dynamic_cast<TPair*> (iter.Next()))){
1651 AliCDBPath entryPath = pair->Key()->GetName();
1652 if(queryPath.Comprises(entryPath)) {
1653 AliDebug(2, Form("Unloading object \"%s\" from cache and from list of ids", entryPath.GetPath().Data()));
1654 TObjString pathStr(entryPath.GetPath());
1655 delete fEntryCache.Remove(&pathStr);
1658 // we do not remove from the list of Id's (it's not very coherent but we leave the
1659 // id for the benefit of the userinfo)
1661 TIter iterids(fIds);
1663 while((anId = dynamic_cast<AliCDBId*> (iterids.Next()))){
1664 AliCDBPath aPath = anId->GetPath();
1665 TString aPathStr = aPath.GetPath();
1666 if(queryPath.Comprises(aPath)) {
1667 delete fIds->Remove(anId);
1672 AliDebug(2,Form("Cache entries and ids removed: %d Remaining: %d",removed,fEntryCache.GetEntries()));
1675 //_____________________________________________________________________________
1676 void AliCDBManager::DestroyActiveStorages() {
1677 // delete list of active storages
1679 fActiveStorages.DeleteAll();
1680 fSpecificStorages.DeleteAll();
1683 //_____________________________________________________________________________
1684 void AliCDBManager::DestroyActiveStorage(AliCDBStorage* /*storage*/) {
1685 // destroys active storage
1688 TIter iter(fActiveStorages.GetTable());
1690 while ((aPair = (TPair*) iter.Next())) {
1691 if(storage == (AliCDBStorage*) aPair->Value())
1692 delete fActiveStorages.Remove(aPair->Key());
1693 storage->Delete(); storage=0x0;
1699 //_____________________________________________________________________________
1700 void AliCDBManager::QueryCDB() {
1701 // query default and specific storages for files valid for fRun. Every storage loads the Ids into its list.
1704 AliError("Run number not yet set! Use AliCDBManager::SetRun.");
1707 if (!fDefaultStorage){
1708 AliError("Default storage is not set! Use AliCDBManager::SetDefaultStorage");
1711 if(fDefaultStorage->GetType() == "alien" || fDefaultStorage->GetType() == "local"){
1712 fDefaultStorage->QueryCDB(fRun);
1714 // AliDebug(2,"Skipping query for valid files, it used only in grid...");
1717 TIter iter(&fSpecificStorages);
1718 TObjString *aCalibType=0;
1719 AliCDBParam* aPar=0;
1720 while((aCalibType = dynamic_cast<TObjString*> (iter.Next()))){
1721 aPar = (AliCDBParam*) fSpecificStorages.GetValue(aCalibType);
1723 AliDebug(2,Form("Querying specific storage %s",aCalibType->GetName()));
1724 AliCDBStorage *aStorage = GetStorage(aPar);
1725 if(aStorage->GetType() == "alien" || aStorage->GetType() == "local"){
1726 aStorage->QueryCDB(fRun, aCalibType->GetName());
1729 "Skipping query for valid files, it is used only in grid...");
1735 //______________________________________________________________________________________________
1736 const char* AliCDBManager::GetDataTypeName(DataType type) {
1737 // returns the name (string) of the data type
1740 case kCondition: return "Conditions";
1741 case kReference: return "Reference";
1742 case kPrivate: return "Private";
1748 //______________________________________________________________________________________________
1749 Bool_t AliCDBManager::DiffObjects(const char *cdbFile1, const char *cdbFile2) const {
1750 // Compare byte-by-byte the objects contained in the CDB entry in two different files,
1751 // whose name is passed as input
1753 // kTRUE - in case the content of the OCDB object (persistent part) is exactly the same
1754 // kFALSE - otherwise
1756 TString f1Str(cdbFile1);
1757 TString f2Str(cdbFile2);
1758 if (!gGrid && ( f1Str.BeginsWith("alien://") || f2Str.BeginsWith("alien://") ))
1759 TGrid::Connect("alien://");
1761 TFile * f1 = TFile::Open(cdbFile1);
1763 Printf("Cannot open file \"%s\"",cdbFile1);
1766 TFile * f2 = TFile::Open(cdbFile2);
1768 Printf("Cannot open file \"%s\"",cdbFile2);
1772 AliCDBEntry * entry1 = (AliCDBEntry*)f1->Get("AliCDBEntry");
1774 Printf("Cannot get CDB entry from file \"%s\"",cdbFile1);
1777 AliCDBEntry * entry2 = (AliCDBEntry*)f2->Get("AliCDBEntry");
1779 Printf("Cannot get CDB entry from file \"%s\"",cdbFile2);
1783 // stream the two objects in the buffer of two TMessages
1784 TObject* object1 = entry1->GetObject();
1785 TObject* object2 = entry2->GetObject();
1786 TMessage * file1 = new TMessage(TBuffer::kWrite);
1787 file1->WriteObject(object1);
1788 Int_t size1 = file1->Length();
1789 TMessage * file2 = new TMessage(TBuffer::kWrite);
1790 file2->WriteObject(object2);
1791 Int_t size2 = file2->Length();
1793 Printf("Problem 2: OCDB entry of different size (%d,%d)",size1,size2);
1797 // if the two buffers have the same size, check that they are the same byte-by-byte
1799 char* buf1 = file1->Buffer();
1800 char* buf2 = file2->Buffer();
1801 //for (Int_t i=0; i<size1; i++) if (file1->Buffer()[i]!=file2->Buffer()[i]) countDiff++;
1802 for(Int_t i=0; i<size1; i++)
1803 if (buf1[i]!=buf2[i]) countDiff++;
1806 Printf("The CDB objects differ by %d bytes.", countDiff);
1810 Printf("The CDB objects are the same in the two files.");
1814 //______________________________________________________________________________________________
1815 void AliCDBManager::InitShortLived() {
1816 // Init the list of short-lived objects
1817 // currently disabled
1821 // fShortLived = new TList();
1822 // fShortLived->SetOwner(1);
1824 // fShortLived->Add(new TObjString("EMCAL/Calib/Data"));
1826 // fShortLived->Add(new TObjString("HMPID/Calib/Nmean"));
1827 // fShortLived->Add(new TObjString("HMPID/Calib/Qthre"));
1829 // fShortLived->Add(new TObjString("ITS/Calib/CalibSPD"));
1831 // fShortLived->Add(new TObjString("MUON/Calib/Gains"));
1832 // fShortLived->Add(new TObjString("MUON/Calib/HV"));
1833 // fShortLived->Add(new TObjString("MUON/Calib/Pedestals"));
1835 // fShortLived->Add(new TObjString("PHOS/Calib/CpvGainPedestals"));
1836 // fShortLived->Add(new TObjString("PHOS/Calib/EmcGainPedestals"));
1838 // fShortLived->Add(new TObjString("PMD/Calib/Data"));
1840 // fShortLived->Add(new TObjString("TRD/Calib/ChamberGainFactor"));
1841 // fShortLived->Add(new TObjString("TRD/Calib/LocalGainFactor"));
1842 // fShortLived->Add(new TObjString("TRD/Calib/ChamberT0"));
1843 // fShortLived->Add(new TObjString("TRD/Calib/LocalT0"));
1844 // fShortLived->Add(new TObjString("TRD/Calib/ChamberVdrift"));
1845 // fShortLived->Add(new TObjString("TRD/Calib/LocalVdrift"));
1847 // fShortLived->Add(new TObjString("ZDC/Calib/Data"));
1851 //______________________________________________________________________________________________
1852 Bool_t AliCDBManager::IsShortLived(const char* path) {
1853 // returns the name (string) of the data type
1855 if(!fShortLived) return kFALSE;
1857 AliCDBPath aPath(path);
1858 if(!aPath.IsValid()){
1859 AliError(Form("Not a valid path: %s", path));
1863 return fShortLived->Contains(path);
1867 //______________________________________________________________________________________________
1868 ULong64_t AliCDBManager::SetLock(Bool_t lock, ULong64_t key){
1869 // To lock/unlock user must provide the key. A new key is provided after
1870 // each successful lock. User should always backup the returned key and
1871 // use it on next access.
1872 if (fLock == lock) return 0; // nothing to be done
1874 // User wants to lock - check his identity
1876 // Lock has a user - check his key
1878 AliFatal("Wrong key provided to lock CDB. Please remove CDB lock access from your code !");
1883 fKey = gSystem->Now();
1887 // User wants to unlock - check the provided key
1889 AliFatal("Lock is ON: wrong key provided");
1896 ///////////////////////////////////////////////////////////
1897 // AliCDBManager Parameter class //
1898 // interface to specific AliCDBParameter class //
1899 // (AliCDBGridParam, AliCDBLocalParam, AliCDBDumpParam) //
1900 ///////////////////////////////////////////////////////////
1902 AliCDBParam::AliCDBParam():
1910 //_____________________________________________________________________________
1911 AliCDBParam::~AliCDBParam() {
1916 void AliCDBManager::ExtractBaseFolder(TString& url)
1919 // remove everything but the url -
1920 // Exact copy of the AliReconstuction::Rectify.... (to be removed)
1924 if (!(sbs=url("\\?User=[^?]*")).IsNull()) url.ReplaceAll(sbs,"");
1925 if (!(sbs=url("\\?DBFolder=[^?]*")).IsNull()) url.ReplaceAll("?DB","");
1926 if (!(sbs=url("\\?SE=[^?]*")).IsNull()) url.ReplaceAll(sbs,"");
1927 if (!(sbs=url("\\?CacheFolder=[^?]*")).IsNull()) url.ReplaceAll(sbs,"");
1928 if (!(sbs=url("\\?OperateDisconnected=[^?]*")).IsNull()) url.ReplaceAll(sbs,"");
1929 if (!(sbs=url("\\?CacheSize=[^?]*")).IsNull()) url.ReplaceAll(sbs,"");
1930 if (!(sbs=url("\\?CleanupInterval=[^?]*")).IsNull()) url.ReplaceAll(sbs,"");
1931 Bool_t slash=kFALSE,space=kFALSE;
1932 while ( (slash=url.EndsWith("/")) || (space=url.EndsWith(" ")) ) {
1933 if (slash) url = url.Strip(TString::kTrailing,'/');
1934 if (space) url = url.Strip(TString::kTrailing,' ');