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 **************************************************************************/
16 /////////////////////////////////////////////////////////////////////////////////////////////////
19 // access class to a DataBase in a local storage //
21 /////////////////////////////////////////////////////////////////////////////////////////////////
28 #include <TObjString.h>
33 #include "AliCDBLocal.h"
34 #include "AliCDBEntry.h"
40 //_____________________________________________________________________________
41 AliCDBLocal::AliCDBLocal(const char* baseDir):
42 fBaseDirectory(baseDir)
46 AliDebug(1, Form("fBaseDirectory = %s",fBaseDirectory.Data()));
48 // check baseDire: trying to cd to baseDir; if it does not exist, create it
49 void* dir = gSystem->OpenDirectory(baseDir);
51 if (gSystem->mkdir(baseDir, kTRUE)) {
52 AliError(Form("Can't open directory <%s>!", baseDir)); //!!!!!!!! to be commented out for testing
56 AliDebug(2,Form("Folder <%s> found",fBaseDirectory.Data()));
57 gSystem->FreeDirectory(dir);
60 fBaseFolder = fBaseDirectory;
63 //_____________________________________________________________________________
64 AliCDBLocal::~AliCDBLocal() {
70 //_____________________________________________________________________________
71 Bool_t AliCDBLocal::FilenameToId(const char* filename, AliCDBRunRange& runRange,
72 Int_t& version, Int_t& subVersion) {
73 // build AliCDBId from filename numbers
77 // valid filename: Run#firstRun_#lastRun_v#version_s#subVersion.root
78 TRegexp keyPattern("^Run[0-9]+_[0-9]+_v[0-9]+_s[0-9]+.root$");
79 keyPattern.Index(filename, &mSize);
81 AliDebug(2, Form("Bad filename <%s>.", filename));
85 TString idString(filename);
86 idString.Resize(idString.Length() - sizeof(".root") + 1);
88 TObjArray* strArray = (TObjArray*) idString.Tokenize("_");
90 TString firstRunString(((TObjString*) strArray->At(0))->GetString());
91 runRange.SetFirstRun(atoi(firstRunString.Data() + 3));
92 runRange.SetLastRun(atoi(((TObjString*) strArray->At(1))->GetString()));
94 TString verString(((TObjString*) strArray->At(2))->GetString());
95 version = atoi(verString.Data() + 1);
97 TString subVerString(((TObjString*) strArray->At(3))->GetString());
98 subVersion = atoi(subVerString.Data() + 1);
106 //_____________________________________________________________________________
107 Bool_t AliCDBLocal::IdToFilename(const AliCDBId& id, TString& filename) const {
108 // build file name from AliCDBId data (run range, version, subVersion)
110 AliDebug(1, Form("fBaseDirectory = %s",fBaseDirectory.Data()));
112 if (!id.GetAliCDBRunRange().IsValid()) {
113 AliDebug(2,Form("Invalid run range <%d, %d>.",
114 id.GetFirstRun(), id.GetLastRun()));
118 if (id.GetVersion() < 0) {
119 AliDebug(2,Form("Invalid version <%d>.", id.GetVersion()));
123 if (id.GetSubVersion() < 0) {
124 AliDebug(2,Form("Invalid subversion <%d>.", id.GetSubVersion()));
128 filename = Form("Run%d_%d_v%d_s%d.root", id.GetFirstRun(), id.GetLastRun(),
129 id.GetVersion(), id.GetSubVersion());
131 filename.Prepend(fBaseDirectory +'/' + id.GetPath() + '/');
136 //_____________________________________________________________________________
137 Bool_t AliCDBLocal::PrepareId(AliCDBId& id) {
138 // prepare id (version, subVersion) of the object that will be stored (called by PutEntry)
140 TString dirName = Form("%s/%s", fBaseDirectory.Data(), id.GetPath().Data());
142 // go to the path; if directory does not exist, create it
143 void* dirPtr = gSystem->OpenDirectory(dirName);
145 gSystem->mkdir(dirName, kTRUE);
146 dirPtr = gSystem->OpenDirectory(dirName);
149 AliError(Form("Can't create directory <%s>!",
155 const char* filename;
156 AliCDBRunRange aRunRange; // the runRange got from filename
157 AliCDBRunRange lastRunRange(-1,-1); // highest runRange found
158 Int_t aVersion, aSubVersion; // the version subVersion got from filename
159 Int_t lastVersion = 0, lastSubVersion = -1; // highest version and subVersion found
161 if (!id.HasVersion()) { // version not specified: look for highest version & subVersion
163 while ((filename = gSystem->GetDirEntry(dirPtr))) { // loop on the files
165 TString aString(filename);
166 if (aString == "." || aString == "..") continue;
168 if (!FilenameToId(filename, aRunRange, aVersion,
171 "Bad filename <%s>! I'll skip it.",
176 if (!aRunRange.Overlaps(id.GetAliCDBRunRange())) continue;
177 if(aVersion < lastVersion) continue;
178 if(aVersion > lastVersion) lastSubVersion = -1;
179 if(aSubVersion < lastSubVersion) continue;
180 lastVersion = aVersion;
181 lastSubVersion = aSubVersion;
182 lastRunRange = aRunRange;
185 id.SetVersion(lastVersion);
186 id.SetSubVersion(lastSubVersion + 1);
188 } else { // version specified, look for highest subVersion only
190 while ((filename = gSystem->GetDirEntry(dirPtr))) { // loop on the files
192 TString aString(filename);
193 if (aString == "." || aString == "..") {
197 if (!FilenameToId(filename, aRunRange, aVersion,
200 "Bad filename <%s>!I'll skip it.",
205 if (aRunRange.Overlaps(id.GetAliCDBRunRange())
206 && aVersion == id.GetVersion()
207 && aSubVersion > lastSubVersion) {
208 lastSubVersion = aSubVersion;
209 lastRunRange = aRunRange;
214 id.SetSubVersion(lastSubVersion + 1);
217 gSystem->FreeDirectory(dirPtr);
219 TString lastStorage = id.GetLastStorage();
220 if(lastStorage.Contains(TString("grid"), TString::kIgnoreCase) &&
221 id.GetSubVersion() > 0 ){
222 AliError(Form("Grid to Local Storage error! local object with version v%d_s%d found:",id.GetVersion(), id.GetSubVersion()-1));
223 AliError(Form("This object has been already transferred from Grid (check v%d_s0)!",id.GetVersion()));
227 if(lastStorage.Contains(TString("new"), TString::kIgnoreCase) &&
228 id.GetSubVersion() > 0 ){
229 AliDebug(2, Form("A NEW object is being stored with version v%d_s%d",
230 id.GetVersion(),id.GetSubVersion()));
231 AliDebug(2, Form("and it will hide previously stored object with v%d_s%d!",
232 id.GetVersion(),id.GetSubVersion()-1));
235 if(!lastRunRange.IsAnyRange() && !(lastRunRange.IsEqual(& id.GetAliCDBRunRange())))
236 AliWarning(Form("Run range modified w.r.t. previous version (Run%d_%d_v%d_s%d)",
237 lastRunRange.GetFirstRun(), lastRunRange.GetLastRun(),
238 id.GetVersion(), id.GetSubVersion()-1));
243 //_____________________________________________________________________________
244 AliCDBId* AliCDBLocal::GetId(const AliCDBId& query) {
245 // look for filename matching query (called by GetEntryId)
247 // if querying for fRun and not specifying a version, look in the fValidFileIds list
248 if(query.GetFirstRun() == fRun && !query.HasVersion()) {
249 // get id from fValidFileIds
250 TIter iter(&fValidFileIds);
255 while((anIdPtr = dynamic_cast<AliCDBId*> (iter.Next()))){
256 if(anIdPtr->GetPath() != query.GetPath()) continue;
262 // otherwise browse in the local filesystem CDB storage
263 TString dirName = Form("%s/%s", fBaseDirectory.Data(), query.GetPath().Data());
265 void* dirPtr = gSystem->OpenDirectory(dirName);
267 AliDebug(2,Form("Directory <%s> not found", (query.GetPath()).Data()));
268 AliDebug(2,Form("in DB folder %s", fBaseDirectory.Data()));
272 const char* filename;
273 AliCDBId *result = new AliCDBId();
274 result->SetPath(query.GetPath());
276 AliCDBRunRange aRunRange; // the runRange got from filename
277 Int_t aVersion, aSubVersion; // the version and subVersion got from filename
279 if (!query.HasVersion()) { // neither version and subversion specified -> look for highest version and subVersion
281 while ((filename = gSystem->GetDirEntry(dirPtr))) { // loop on files
283 TString aString(filename);
284 if (aString.BeginsWith('.')) continue;
286 if (!FilenameToId(filename, aRunRange, aVersion, aSubVersion)) continue;
287 // aRunRange, aVersion, aSubVersion filled from filename
289 if (!aRunRange.Comprises(query.GetAliCDBRunRange())) continue;
290 // aRunRange contains requested run!
292 AliDebug(1,Form("Filename %s matches\n",filename));
294 if (result->GetVersion() < aVersion) {
295 result->SetVersion(aVersion);
296 result->SetSubVersion(aSubVersion);
299 aRunRange.GetFirstRun());
301 aRunRange.GetLastRun());
303 } else if (result->GetVersion() == aVersion
304 && result->GetSubVersion()
307 result->SetSubVersion(aSubVersion);
310 aRunRange.GetFirstRun());
312 aRunRange.GetLastRun());
313 } else if (result->GetVersion() == aVersion
314 && result->GetSubVersion() == aSubVersion){
315 AliError(Form("More than one object valid for run %d, version %d_%d!",
316 query.GetFirstRun(), aVersion, aSubVersion));
317 gSystem->FreeDirectory(dirPtr);
323 } else if (!query.HasSubVersion()) { // version specified but not subversion -> look for highest subVersion
325 result->SetVersion(query.GetVersion());
327 while ((filename = gSystem->GetDirEntry(dirPtr))) { // loop on files
329 TString aString(filename);
330 if (aString.BeginsWith('.')) continue;
332 if (!FilenameToId(filename, aRunRange, aVersion, aSubVersion)) continue;
333 // aRunRange, aVersion, aSubVersion filled from filename
335 if (!aRunRange.Comprises(query.GetAliCDBRunRange())) continue;
336 // aRunRange contains requested run!
338 if(query.GetVersion() != aVersion) continue;
339 // aVersion is requested version!
341 if(result->GetSubVersion() == aSubVersion){
342 AliError(Form("More than one object valid for run %d, version %d_%d!",
343 query.GetFirstRun(), aVersion, aSubVersion));
344 gSystem->FreeDirectory(dirPtr);
348 if( result->GetSubVersion() < aSubVersion) {
350 result->SetSubVersion(aSubVersion);
353 aRunRange.GetFirstRun());
355 aRunRange.GetLastRun());
359 } else { // both version and subversion specified
361 //AliCDBId dataId(queryId.GetAliCDBPath(), -1, -1, -1, -1);
363 while ((filename = gSystem->GetDirEntry(dirPtr))) { // loop on files
365 TString aString(filename);
366 if (aString.BeginsWith('.')) continue;
368 if (!FilenameToId(filename, aRunRange, aVersion, aSubVersion)) continue;
369 // aRunRange, aVersion, aSubVersion filled from filename
371 if (!aRunRange.Comprises(query.GetAliCDBRunRange())) continue;
372 // aRunRange contains requested run!
374 if(query.GetVersion() != aVersion || query.GetSubVersion() != aSubVersion) continue;
375 // aVersion and aSubVersion are requested version and subVersion!
377 if(result->GetVersion() == aVersion && result->GetSubVersion() == aSubVersion){
378 AliError(Form("More than one object valid for run %d, version %d_%d!",
379 query.GetFirstRun(), aVersion, aSubVersion));
380 gSystem->FreeDirectory(dirPtr);
384 result->SetVersion(aVersion);
385 result->SetSubVersion(aSubVersion);
386 result->SetFirstRun(aRunRange.GetFirstRun());
387 result->SetLastRun(aRunRange.GetLastRun());
392 gSystem->FreeDirectory(dirPtr);
397 //_____________________________________________________________________________
398 AliCDBEntry* AliCDBLocal::GetEntry(const AliCDBId& queryId) {
399 // get AliCDBEntry from the storage (the CDB file matching the query is
400 // selected by GetEntryId and the contained AliCDBid is passed here)
402 AliCDBId* dataId = GetEntryId(queryId);
404 TString errMessage(TString::Format("No valid CDB object found! request was: %s", queryId.ToString().Data()));
405 if (!dataId || !dataId->IsSpecified()){
406 AliError(Form("The data ID is undefined!"));
407 throw std::runtime_error(errMessage.Data());
412 if (!IdToFilename(*dataId, filename)) {
413 AliError(Form("Bad data ID encountered!"));
415 throw std::runtime_error(errMessage.Data());
419 TFile file(filename, "READ"); // open file
420 if (!file.IsOpen()) {
421 AliError(Form("Can't open file <%s>!", filename.Data()));
423 throw std::runtime_error(errMessage.Data());
427 // get the only AliCDBEntry object from the file
428 // the object in the file is an AliCDBEntry entry named "AliCDBEntry"
430 AliCDBEntry* anEntry = dynamic_cast<AliCDBEntry*> (file.Get("AliCDBEntry"));
432 AliError(Form("Bad storage data: No AliCDBEntry in file!"));
435 throw std::runtime_error(errMessage.Data());
439 AliCDBId& entryId = anEntry->GetId();
441 // The object's Id are not reset during storage
442 // If object's Id runRange or version do not match with filename,
443 // it means that someone renamed file by hand. In this case a warning msg is issued.
445 anEntry-> SetLastStorage("local");
447 if(!entryId.IsEqual(dataId)){
448 AliWarning(Form("Mismatch between file name and object's Id!"));
449 AliWarning(Form("File name: %s", dataId->ToString().Data()));
450 AliWarning(Form("Object's Id: %s", entryId.ToString().Data()));
453 // Check whether entry contains a TTree. In case load the tree in memory!
454 LoadTreeFromFile(anEntry);
456 // close file, return retrieved entry
463 //_____________________________________________________________________________
464 AliCDBId* AliCDBLocal::GetEntryId(const AliCDBId& queryId) {
465 // get AliCDBId from the storage
466 // Via GetId, select the CDB file matching the query and return
467 // the contained AliCDBId
469 AliCDBId* dataId = 0;
471 // look for a filename matching query requests (path, runRange, version, subVersion)
472 if (!queryId.HasVersion()) {
473 // if version is not specified, first check the selection criteria list
474 AliCDBId selectedId(queryId);
475 GetSelection(&selectedId);
476 dataId = GetId(selectedId);
478 dataId = GetId(queryId);
481 if (dataId && !dataId->IsSpecified()) {
489 //_____________________________________________________________________________
490 void AliCDBLocal::GetEntriesForLevel0(const char* level0,
491 const AliCDBId& queryId, TList* result) {
492 // multiple request (AliCDBStorage::GetAll)
494 TString level0Dir = Form("%s/%s", fBaseDirectory.Data(), level0);
496 void* level0DirPtr = gSystem->OpenDirectory(level0Dir);
498 AliDebug(2,Form("Can't open level0 directory <%s>!",
505 while ((level1 = gSystem->GetDirEntry(level0DirPtr))) {
507 TString level1Str(level1);
508 // skip directories starting with a dot (".svn" and similar in old svn working copies)
509 if (level1Str.BeginsWith('.')) {
513 TString fullPath = Form("%s/%s",level0Dir.Data(), level1);
515 Int_t res=gSystem->GetPathInfo(fullPath.Data(), 0, (Long64_t*) 0, &flag, 0);
518 AliDebug(2, Form("Error reading entry %s !",level1Str.Data()));
521 if(!(flag&2)) continue; // bit 1 of flag = directory!
523 if (queryId.GetAliCDBPath().Level1Comprises(level1)) {
524 GetEntriesForLevel1(level0, level1, queryId, result);
528 gSystem->FreeDirectory(level0DirPtr);
531 //_____________________________________________________________________________
532 void AliCDBLocal::GetEntriesForLevel1(const char* level0, const char* level1,
533 const AliCDBId& queryId, TList* result) {
534 // multiple request (AliCDBStorage::GetAll)
536 TString level1Dir = Form("%s/%s/%s", fBaseDirectory.Data(), level0,level1);
538 void* level1DirPtr = gSystem->OpenDirectory(level1Dir);
540 AliDebug(2,Form("Can't open level1 directory <%s>!",
547 while ((level2 = gSystem->GetDirEntry(level1DirPtr))) {
549 TString level2Str(level2);
550 // skip directories starting with a dot (".svn" and similar in old svn working copies)
551 if (level2Str.BeginsWith('.')) {
555 TString fullPath = Form("%s/%s",level1Dir.Data(), level2);
557 Int_t res=gSystem->GetPathInfo(fullPath.Data(), 0, (Long64_t*) 0, &flag, 0);
560 AliDebug(2, Form("Error reading entry %s !",level2Str.Data()));
563 if(!(flag&2)) continue; // skip if not a directory
565 if (queryId.GetAliCDBPath().Level2Comprises(level2)) {
567 AliCDBPath entryPath(level0, level1, level2);
569 AliCDBId entryId(entryPath, queryId.GetAliCDBRunRange(),
570 queryId.GetVersion(), queryId.GetSubVersion());
572 // check filenames to see if any includes queryId.GetAliCDBRunRange()
573 void* level2DirPtr = gSystem->OpenDirectory(fullPath);
575 AliDebug(2,Form("Can't open level2 directory <%s>!", fullPath.Data()));
580 while ((level3 = gSystem->GetDirEntry(level2DirPtr))) {
581 TString fileName(level3);
582 TString fullFileName = Form("%s/%s", fullPath.Data(), level3);
584 Int_t file_res=gSystem->GetPathInfo(fullFileName.Data(), 0, (Long64_t*) 0, &file_flag, 0);
587 AliDebug(2, Form("Error reading entry %s !",level2Str.Data()));
591 continue; // it is not a regular file!
593 // skip if result already contains an entry for this path
594 Bool_t alreadyLoaded = kFALSE;
595 Int_t nEntries = result->GetEntries();
596 for(int i=0; i<nEntries; i++){
597 AliCDBEntry *lEntry = (AliCDBEntry*) result->At(i);
598 AliCDBId lId = lEntry->GetId();
599 TString lPath = lId.GetPath();
600 if(lPath.EqualTo(entryPath.GetPath())){
601 alreadyLoaded = kTRUE;
605 if (alreadyLoaded) continue;
607 //skip filenames not matching the regex below
608 TRegexp re("^Run[0-9]+_[0-9]+_");
609 if(!fileName.Contains(re))
611 //extract first- and last-run. This allows to avoid quering for a calibration path
612 // if we did not find a filename with interesting run-range.
613 TString firstRunStr = fileName(3,fileName.First('_')-3);
614 TString lastRunStr = fileName(fileName.First('_')+1,fileName.Length()-fileName.First('_')-2);
615 Int_t firstRun = firstRunStr.Atoi();
616 Int_t lastRun = lastRunStr.Atoi();
617 AliCDBRunRange rr(firstRun,lastRun);
619 AliCDBEntry* anEntry = 0;
620 if (rr.Comprises(queryId.GetAliCDBRunRange()))
621 anEntry = GetEntry(entryId);
623 result->Add(anEntry);
628 gSystem->FreeDirectory(level1DirPtr);
631 //_____________________________________________________________________________
632 TList* AliCDBLocal::GetEntries(const AliCDBId& queryId) {
633 // multiple request (AliCDBStorage::GetAll)
635 TList* result = new TList();
638 // if querying for fRun and not specifying a version, look in the fValidFileIds list
639 if(queryId.GetFirstRun() == fRun && !queryId.HasVersion()) {
640 // get id from fValidFileIds
641 TIter *iter = new TIter(&fValidFileIds);
642 TObjArray selectedIds;
643 selectedIds.SetOwner(1);
645 // loop on list of valid Ids to select the right version to get.
646 // According to query and to the selection criteria list, version can be the highest or exact
649 AliCDBPath queryPath = queryId.GetAliCDBPath();
650 while((anIdPtr = dynamic_cast<AliCDBId*> (iter->Next()))){
651 AliCDBPath thisCDBPath = anIdPtr->GetAliCDBPath();
652 if(!(queryPath.Comprises(thisCDBPath))) continue;
654 AliCDBId thisId(*anIdPtr);
655 dataId = GetId(thisId);
656 if(dataId) selectedIds.Add(dataId);
661 // selectedIds contains the Ids of the files matching all requests of query!
662 // All the objects are now ready to be retrieved
663 iter = new TIter(&selectedIds);
664 while((anIdPtr = dynamic_cast<AliCDBId*> (iter->Next()))){
665 AliCDBEntry* anEntry = GetEntry(*anIdPtr);
666 if(anEntry) result->Add(anEntry);
672 void* storageDirPtr = gSystem->OpenDirectory(fBaseDirectory);
673 if (!storageDirPtr) {
674 AliDebug(2,Form("Can't open storage directory <%s>",
675 fBaseDirectory.Data()));
681 while ((level0 = gSystem->GetDirEntry(storageDirPtr))) {
683 TString level0Str(level0);
684 // skip directories starting with a dot (".svn" and similar in old svn working copies)
685 if (level0Str.BeginsWith('.')) {
689 TString fullPath = Form("%s/%s",fBaseDirectory.Data(), level0);
691 Int_t res=gSystem->GetPathInfo(fullPath.Data(), 0, (Long64_t*) 0, &flag, 0);
694 AliDebug(2, Form("Error reading entry %s !",level0Str.Data()));
698 if(!(flag&2)) continue; // bit 1 of flag = directory!
700 if (queryId.GetAliCDBPath().Level0Comprises(level0)) {
701 GetEntriesForLevel0(level0, queryId, result);
705 gSystem->FreeDirectory(storageDirPtr);
710 //_____________________________________________________________________________
711 Bool_t AliCDBLocal::PutEntry(AliCDBEntry* entry, const char* mirrors) {
712 // put an AliCDBEntry object into the database
714 AliCDBId& id = entry->GetId();
716 // set version and subVersion for the entry to be stored
717 if (!PrepareId(id)) return kFALSE;
720 // build filename from entry's id
722 if (!IdToFilename(id, filename)) {
724 AliDebug(2,Form("Bad ID encountered! Subnormal error!"));
728 TString mirrorsString(mirrors);
729 if(!mirrorsString.IsNull())
730 AliWarning("AliCDBLocal storage cannot take mirror SEs into account. They will be ignored.");
733 TFile file(filename, "CREATE");
734 if (!file.IsOpen()) {
735 AliError(Form("Can't open file <%s>!", filename.Data()));
739 //SetTreeToFile(entry, &file);
741 entry->SetVersion(id.GetVersion());
742 entry->SetSubVersion(id.GetSubVersion());
744 // write object (key name: "AliCDBEntry")
745 Bool_t result = file.WriteTObject(entry, "AliCDBEntry");
746 if (!result) AliDebug(2,Form("Can't write entry to file: %s", filename.Data()));
750 if(!(id.GetPath().Contains("SHUTTLE/STATUS")))
751 AliInfo(Form("CDB object stored into file %s",filename.Data()));
757 //_____________________________________________________________________________
758 TList* AliCDBLocal::GetIdListFromFile(const char* fileName){
760 TString fullFileName(fileName);
761 fullFileName.Prepend(fBaseDirectory+'/');
762 TFile *file = TFile::Open(fullFileName);
764 AliError(Form("Can't open selection file <%s>!", fullFileName.Data()));
769 TList *list = new TList();
777 keycycle = "AliCDBId;";
780 id = (AliCDBId*) file->Get(keycycle);
784 file->Close(); delete file; file=0;
788 //_____________________________________________________________________________
789 Bool_t AliCDBLocal::Contains(const char* path) const{
790 // check for path in storage's fBaseDirectory
792 TString dirName = Form("%s/%s", fBaseDirectory.Data(), path);
793 Bool_t result=kFALSE;
795 void* dirPtr = gSystem->OpenDirectory(dirName);
796 if (dirPtr) result=kTRUE;
797 gSystem->FreeDirectory(dirPtr);
802 //_____________________________________________________________________________
803 void AliCDBLocal::QueryValidFiles() {
804 // Query the CDB for files valid for AliCDBStorage::fRun.
805 // Fills list fValidFileIds with AliCDBId objects extracted from CDB files
806 // present in the local storage.
807 // If fVersion was not set, fValidFileIds is filled with highest versions.
808 // In the CVMFS case, the fValidFileIds is filled from the file containing
809 // the filepaths corresponding to the highest versions for the give OCDB tag
810 // by launching the script which extracts the last versions for the given run.
813 if(fVersion != -1) AliWarning ("Version parameter is not used by local storage query!");
814 if(fMetaDataFilter) {
815 AliWarning ("CDB meta data parameters are not used by local storage query!");
816 delete fMetaDataFilter; fMetaDataFilter=0;
819 // Check if in CVMFS case
820 TString cvmfsOcdbTag(gSystem->Getenv("OCDB_PATH"));
821 if (!cvmfsOcdbTag.IsNull()) {
822 QueryValidCVMFSFiles(cvmfsOcdbTag);
826 void* storageDirPtr = gSystem->OpenDirectory(fBaseDirectory);
829 while ((level0 = gSystem->GetDirEntry(storageDirPtr))) {
831 TString level0Str(level0);
832 if (level0Str.BeginsWith(".")) {
836 if (fPathFilter.Level0Comprises(level0)) {
837 TString level0Dir = Form("%s/%s",fBaseDirectory.Data(),level0);
838 void* level0DirPtr = gSystem->OpenDirectory(level0Dir);
840 while ((level1 = gSystem->GetDirEntry(level0DirPtr))) {
842 TString level1Str(level1);
843 if (level1Str.BeginsWith(".")) {
847 if (fPathFilter.Level1Comprises(level1)) {
848 TString level1Dir = Form("%s/%s/%s",
849 fBaseDirectory.Data(),level0,level1);
851 void* level1DirPtr = gSystem->OpenDirectory(level1Dir);
853 while ((level2 = gSystem->GetDirEntry(level1DirPtr))) {
855 TString level2Str(level2);
856 if (level2Str.BeginsWith(".")) {
860 if (fPathFilter.Level2Comprises(level2)) {
861 TString dirName = Form("%s/%s/%s/%s", fBaseDirectory.Data(), level0, level1, level2);
863 void* dirPtr = gSystem->OpenDirectory(dirName);
865 const char* filename;
867 AliCDBRunRange aRunRange; // the runRange got from filename
868 Int_t aVersion, aSubVersion; // the version and subVersion got from filename
869 Int_t highestV=-1, highestSubV=-1; // the highest version and subVersion for this calibration type
871 while ((filename = gSystem->GetDirEntry(dirPtr))) { // loop on files
873 TString aString(filename);
874 if (aString.BeginsWith(".")) continue;
876 if (!FilenameToId(filename, aRunRange, aVersion, aSubVersion)) {
880 AliCDBRunRange runrg(fRun, fRun);
881 if (!aRunRange.Comprises(runrg)) continue;
883 // check to keep the highest version/subversion (in case of more than one)
884 if (aVersion > highestV) {
886 highestSubV = aSubVersion;
888 if (aSubVersion > highestSubV) {
889 highestSubV = aSubVersion;
894 AliCDBPath validPath(level0, level1, level2);
895 AliCDBId validId(validPath, aRunRange, highestV, highestSubV);
896 fValidFileIds.AddLast(validId.Clone());
899 gSystem->FreeDirectory(dirPtr);
902 gSystem->FreeDirectory(level1DirPtr);
905 gSystem->FreeDirectory(level0DirPtr);
908 gSystem->FreeDirectory(storageDirPtr);
912 //_____________________________________________________________________________
913 void AliCDBLocal::QueryValidCVMFSFiles(TString& cvmfsOcdbTag) {
914 // Called in the CVMFS case to fill the fValidFileIds from the file containing
915 // the filepaths corresponding to the highest versions for the given OCDB tag
916 // by launching the script which extracts the last versions for the given run.
919 TString command = cvmfsOcdbTag;
920 AliDebug(3, Form("Getting valid files from CVMFS-OCDB tag \"%s\"", cvmfsOcdbTag.Data()));
921 // CVMFS-OCDB tag. This is the file $OCDB_PATH/catalogue/20??.list.gz
922 // containing all CDB file paths (for the given AR tag)
923 cvmfsOcdbTag.Strip(TString::kTrailing, '/');
924 cvmfsOcdbTag.Append("/");
925 gSystem->ExpandPathName(cvmfsOcdbTag);
926 if ( gSystem->AccessPathName(cvmfsOcdbTag) )
927 AliFatal(Form("cvmfs OCDB set to an invalid path: %s", cvmfsOcdbTag.Data()));
929 // The file containing the list of valid files for the current run has to be generated
930 // by running the (shell+awk) script on the CVMFS OCDB tag file.
932 // the script in cvmfs to extract CDB filepaths for the given run has the following fullpath
933 // w.r.t. $OCDB_PATH: bin/OCDBperRun.sh
934 command = command.Strip(TString::kTrailing, '/');
935 command.Append("/bin/getOCDBFilesPerRun.sh ");
936 command += cvmfsOcdbTag;
937 // from URI define the last two levels of the path of the cvmfs ocdb tag (e.g. data/2012.list.gz)
938 TString uri(GetURI());
939 uri.Remove(TString::kTrailing, '/');
940 TObjArray * osArr = uri.Tokenize('/');
941 TObjString* mcdata_os = dynamic_cast<TObjString*>(osArr->At(osArr->GetEntries()-3));
942 TObjString* yeartype_os = 0;
943 TString mcdata = mcdata_os->GetString();
944 if( mcdata == TString("data")) {
945 yeartype_os = dynamic_cast<TObjString*>(osArr->At(osArr->GetEntries()-2));
947 mcdata_os = dynamic_cast<TObjString*>(osArr->At(osArr->GetEntries()-2));
948 yeartype_os = dynamic_cast<TObjString*>(osArr->At(osArr->GetEntries()-1));
950 mcdata = mcdata_os->GetString();
951 TString yeartype = yeartype_os->GetString();
955 command += ".list.gz cvmfs ";
956 command += TString::Itoa(fRun,10);
958 command += TString::Itoa(fRun,10);
960 TString runValidFile(gSystem->WorkingDirectory());
962 runValidFile += mcdata;
964 runValidFile += yeartype;
966 runValidFile += TString::Itoa(fRun,10);
967 command += runValidFile;
968 AliDebug(3, Form("Running command: \"%s\"",command.Data()));
969 Int_t result = gSystem->Exec(command.Data());
971 AliError(Form("Was not able to execute \"%s\"", command.Data()));
974 // We expect the file with valid paths for this run to be generated in the current directory
975 // and to be named as the CVMFS OCDB tag, without .gz, with '_runnumber' appended
976 // Fill fValidFileIds from file
977 ifstream *file = new ifstream(runValidFile.Data());
979 AliFatal(Form("Error opening file \"%s\"!", runValidFile.Data()));
984 while (filepath.ReadLine(*file)) {
985 // skip line in case it is not a root file path
986 if(! filepath.EndsWith(".root")) {
989 //extract three-level path and basename
990 TObjArray *tokens = filepath.Tokenize('/');
991 if (tokens->GetEntries() < 5) {
992 AliError(Form("\"%s\" is not a valid cvmfs path for an OCDB object", filepath.Data()));
995 TObjString *baseNameOstr = (TObjString*) tokens->At(tokens->GetEntries()-1);
996 TString baseName(baseNameOstr->String());
997 TObjString *l0oStr = (TObjString*) tokens->At(tokens->GetEntries()-4);
998 TObjString *l1oStr = (TObjString*) tokens->At(tokens->GetEntries()-3);
999 TObjString *l2oStr = (TObjString*) tokens->At(tokens->GetEntries()-2);
1000 TString l0(l0oStr->String());
1001 TString l1(l1oStr->String());
1002 TString l2(l2oStr->String());
1003 TString threeLevels = l0 + '/' + l1 + '/' + l2;
1005 AliCDBPath validPath(threeLevels);
1006 //use basename and three-level path to create Id
1007 AliCDBRunRange aRunRange; // the runRange got from filename
1008 Int_t aVersion, aSubVersion; // the version and subVersion got from filename
1009 if ( !FilenameToId(baseName, aRunRange, aVersion, aSubVersion) )
1010 AliError( Form("Could not create a valid CDB id from path: \"%s\"", filepath.Data()) );
1012 AliCDBRunRange runrg(fRun,fRun);
1013 if (!aRunRange.Comprises(runrg)) continue; // should never happen (would mean awk script wrong output)
1014 // aRunRange contains requested run!
1015 AliCDBId validId(validPath,aRunRange,aVersion,aSubVersion);
1016 fValidFileIds.AddLast(validId.Clone());
1026 /////////////////////////////////////////////////////////////////////////////////////////////////
1028 // AliCDBLocal factory //
1030 /////////////////////////////////////////////////////////////////////////////////////////////////
1032 ClassImp(AliCDBLocalFactory)
1034 //_____________________________________________________________________________
1035 Bool_t AliCDBLocalFactory::Validate(const char* dbString) {
1036 // check if the string is valid local URI
1038 TRegexp dbPatternLocal("^local://.+$");
1040 return (TString(dbString).Contains(dbPatternLocal) || TString(dbString).BeginsWith("snapshot://folder="));
1043 //_____________________________________________________________________________
1044 AliCDBParam* AliCDBLocalFactory::CreateParameter(const char* dbString) {
1045 // create AliCDBLocalParam class from the URI string
1047 if (!Validate(dbString)) {
1051 TString checkSS(dbString);
1052 if(checkSS.BeginsWith("snapshot://"))
1054 TString snapshotPath("OCDB");
1055 snapshotPath.Prepend(TString(gSystem->WorkingDirectory()) + '/');
1056 checkSS.Remove(0,checkSS.First(':')+3);
1057 return new AliCDBLocalParam(snapshotPath,checkSS);
1060 // if the string argument is not a snapshot URI, than it is a plain local URI
1061 TString pathname(dbString + sizeof("local://") - 1);
1063 if(gSystem->ExpandPathName(pathname))
1066 if (pathname[0] != '/') {
1067 pathname.Prepend(TString(gSystem->WorkingDirectory()) + '/');
1069 //pathname.Prepend("local://");
1071 return new AliCDBLocalParam(pathname);
1074 //_____________________________________________________________________________
1075 AliCDBStorage* AliCDBLocalFactory::Create(const AliCDBParam* param) {
1076 // create AliCDBLocal storage instance from parameters
1078 if (AliCDBLocalParam::Class() == param->IsA()) {
1080 const AliCDBLocalParam* localParam =
1081 (const AliCDBLocalParam*) param;
1083 return new AliCDBLocal(localParam->GetPath());
1089 //_____________________________________________________________________________
1090 void AliCDBLocal::SetRetry(Int_t /* nretry */, Int_t /* initsec */) {
1092 // Function to set the exponential retry for putting entries in the OCDB
1094 AliInfo("This function sets the exponential retry for putting entries in the OCDB - to be used ONLY for AliCDBGrid --> returning without doing anything");
1100 /////////////////////////////////////////////////////////////////////////////////////////////////
1102 // AliCDBLocal Parameter class // //
1104 /////////////////////////////////////////////////////////////////////////////////////////////////
1106 ClassImp(AliCDBLocalParam)
1108 //_____________________________________________________________________________
1109 AliCDBLocalParam::AliCDBLocalParam():
1113 // default constructor
1117 //_____________________________________________________________________________
1118 AliCDBLocalParam::AliCDBLocalParam(const char* dbPath):
1125 SetURI(TString("local://") + dbPath);
1128 //_____________________________________________________________________________
1129 AliCDBLocalParam::AliCDBLocalParam(const char* dbPath, const char* uri):
1136 SetURI(TString("alien://") + uri);
1139 //_____________________________________________________________________________
1140 AliCDBLocalParam::~AliCDBLocalParam() {
1145 //_____________________________________________________________________________
1146 AliCDBParam* AliCDBLocalParam::CloneParam() const {
1149 return new AliCDBLocalParam(fDBPath);
1152 //_____________________________________________________________________________
1153 ULong_t AliCDBLocalParam::Hash() const {
1154 // return Hash function
1156 return fDBPath.Hash();
1159 //_____________________________________________________________________________
1160 Bool_t AliCDBLocalParam::IsEqual(const TObject* obj) const {
1161 // check if this object is equal to AliCDBParam obj
1167 if (AliCDBLocalParam::Class() != obj->IsA()) {
1171 AliCDBLocalParam* other = (AliCDBLocalParam*) obj;
1173 return fDBPath == other->fDBPath;