]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - STEER/CDB/AliCDBManager.cxx
Merge branch 'master' of https://git.cern.ch/reps/AliRoot
[u/mrichter/AliRoot.git] / STEER / CDB / AliCDBManager.cxx
index d485595d49ba9abcd6dc51e0db2aee79ccc7b7c5..011220b3513e461ae86cd778e793adc0771f05a1 100644 (file)
@@ -18,7 +18,6 @@
 //   e-mail: Alberto.Colla@cern.ch
 //-------------------------------------------------------------------------
 
-#include <stdlib.h>
 #include <fstream>
 
 #include "AliCDBManager.h"
 #include "TMessage.h"
 #include "TObject.h"
 #include "TRegexp.h"
- // STD
-using namespace std;
-#include <iostream>
-#include <algorithm>
-#include <sstream>
-#include <stdexcept>
-#include <functional>
 
 ClassImp(AliCDBParam)
 
@@ -111,10 +103,12 @@ void AliCDBManager::InitFromCache(TMap *entryCache, Int_t run) {
 }
 
 //_____________________________________________________________________________
-void  AliCDBManager::DumpToSnapshotFile(const char* snapshotFileName, Bool_t singleKeys){
+void  AliCDBManager::DumpToSnapshotFile(const char* snapshotFileName, Bool_t singleKeys) const {
 //
-// dump the entries map and the ids list to
-// the output file
+// If singleKeys is true, dump the entries map and the ids list to the snapshot file
+// (provided mostly for historical reasons, the file is then read with InitFromSnapshot),
+// otherwise write to file each AliCDBEntry separately (the is the preferred way, the file
+// is then read with SetSnapshotMode).
 
   // open the file
   TFile *f = TFile::Open(snapshotFileName,"RECREATE");
@@ -127,15 +121,11 @@ void  AliCDBManager::DumpToSnapshotFile(const char* snapshotFileName, Bool_t sin
   AliInfo(Form("Dumping entriesList with %d entries!\n", fIds->GetEntries()));
 
   f->cd();
-  f->WriteObject(&fEntryCache,"CDBentriesMap");
-  f->WriteObject(fIds,"CDBidsList");
-
-  if(!singleKeys){
+  if(singleKeys){
+    f->WriteObject(&fEntryCache,"CDBentriesMap");
+    f->WriteObject(fIds,"CDBidsList");
+  }else{
     // We write the entries one by one named by their calibration path
-    /*
-       fEntryCache.Write("CDBentriesMap");
-       fIds->Write("CDBidsList");
-       */
     TIter iter(fEntryCache.GetTable());
     TPair* pair = 0;
     while((pair = dynamic_cast<TPair*> (iter.Next()))){
@@ -150,8 +140,28 @@ void  AliCDBManager::DumpToSnapshotFile(const char* snapshotFileName, Bool_t sin
   }
   f->Close();
   delete f;
+}
+
+//_____________________________________________________________________________
+void  AliCDBManager::DumpToLightSnapshotFile(const char* lightSnapshotFileName) const {
+// The light snapshot does not contain the CDB objects (AliCDBEntries) but
+// only the information identifying them, that is the map of storages and
+// the list of AliCDBIds, as in the UserInfo of AliESDs.root
+
+  // open the file
+  TFile *f = TFile::Open(lightSnapshotFileName,"RECREATE");
+  if (!f || f->IsZombie()){
+    AliError(Form("Cannot open file %s",lightSnapshotFileName));
+    return;
+  }
 
-  //exit(0);
+  AliInfo(Form("Dumping map of storages with %d entries!\n", fStorageMap->GetEntries()));
+  AliInfo(Form("Dumping entriesList with %d entries!\n", fIds->GetEntries()));
+  f->WriteObject(fStorageMap,"cdbStoragesMap");
+  f->WriteObject(fIds,"CDBidsList");
+
+  f->Close();
+  delete f;
 }
 
 //_____________________________________________________________________________
@@ -310,6 +320,7 @@ AliCDBManager::AliCDBManager():
   fLock(kFALSE),
   fSnapshotMode(kFALSE),
   fSnapshotFile(0),
+  fOCDBUploadMode(kFALSE),
   fRaw(kFALSE),
   fCvmfsOcdb(""),
   fStartRunLHCPeriod(-1),
@@ -399,7 +410,6 @@ AliCDBParam* AliCDBManager::CreateParameter(const char* dbString) const {
 
   AliCDBStorageFactory* factory=0;
   while ((factory = (AliCDBStorageFactory*) iter.Next())) {
-    //AliCDBParam* param = factory->CreateParameter(dbString);
     AliCDBParam* param = factory->CreateParameter(uriString);
     if(param) return param;
   }
@@ -451,15 +461,20 @@ void AliCDBManager::AlienToCvmfsUri(TString& uriString) const {
 
 //_____________________________________________________________________________
 AliCDBStorage* AliCDBManager::GetStorage(const char* dbString) {
-// get storage object from URI string
+// Get the CDB storage corresponding to the URI string passed as argument
+// If "raw://" is passed, get the storage for the raw OCDB for the current run (fRun)
 
-  // Check environment variable OCDB_PATH. It is set in case the user (job) expects
-  // to find in the local /cvmfs the required OCDB tag. The OCDB_PATH variable
-  // contains the path to the directory in /cvmfs/ which is an AliRoot tag based snapshot
-  // of the AliEn file catalogue. The directory has to contain:
-  // 1) catalogue/20??.list.gz gzipped text files listing the OCDB files (seen by that AliRoot tag)
-  // 2) bin/getOCDBperRun.sh   (shell+awk) script extracting from 1) the list
-  // of valid files for the given run.
+  TString uriString(dbString);
+  if (uriString.EqualTo("raw://")) {
+    if (!fLHCPeriod.IsNull() && !fLHCPeriod.IsWhitespace()) {
+      return GetDefaultStorage();
+    } else {
+      TString lhcPeriod("");
+      Int_t startRun = -1, endRun = -1;
+      GetLHCPeriodAgainstAlienFile(fRun, lhcPeriod, startRun, endRun);
+      return GetStorage(lhcPeriod.Data());
+    }
+  }
 
   AliCDBParam* param = CreateParameter(dbString);
   if (!param) {
@@ -567,6 +582,20 @@ Bool_t AliCDBManager::Drain(AliCDBEntry *entry) {
   return fDrainStorage->Put(entry);
 }
 
+//____________________________________________________________________________
+Bool_t AliCDBManager::SetOCDBUploadMode() {
+// Set the framework in official upload mode. This tells the framework to upload
+// objects to cvmfs after they have been uploaded to AliEn OCDBs.
+// It return false if the executable to upload to cvmfs is not found.
+
+  TString cvmfsUploadExecutable("$HOME/bin/ocdb-cvmfs");
+  gSystem->ExpandPathName(cvmfsUploadExecutable);
+  if ( gSystem->AccessPathName(cvmfsUploadExecutable) )
+    return kFALSE;
+  fOCDBUploadMode = kTRUE;
+  return kTRUE;
+}
+
 //____________________________________________________________________________
 void AliCDBManager::SetDefaultStorage(const char* storageUri) {
 // sets default storage from URI string
@@ -692,6 +721,14 @@ void AliCDBManager::SetDefaultStorage(const char* mcString, const char* simType)
 
 //_____________________________________________________________________________
 void AliCDBManager::ValidateCvmfsCase() const {
+  // The OCDB_PATH variable contains the path to the directory in /cvmfs/ which is
+  // an AliRoot tag based snapshot of the AliEn file catalogue (e.g. 
+  // /cvmfs/alice.cern.ch/x86_64-2.6-gnu-4.1.2/Packages/OCDB/v5-05-76-AN).
+  // The directory has to contain:
+  // 1) <data|MC>/20??.list.gz gzipped text files listing the OCDB files (seen by that AliRoot tag)
+  // 2) bin/getOCDBFilesPerRun.sh   (shell+awk) script extracting from 1) the list
+  //    of valid files for the given run.
+
     if (! fCvmfsOcdb.BeginsWith("/cvmfs"))  //!!!! to be commented out for testing
       AliFatal(Form("OCDB_PATH set to an invalid path: %s", fCvmfsOcdb.Data()));
 
@@ -721,85 +758,102 @@ void AliCDBManager::SetDefaultStorageFromRun(Int_t run) {
     }
   }    
 
-  // fRaw and cvmfs case
-  if (! fCvmfsOcdb.IsNull()) {
-    // we don't want to connect to AliEn just to set the uri from the runnumber
-    // for that we use the script getUriFromYear.sh in the cvmfs AliRoot package
-    // check that we find the two scripts we need
-    TString getYearScript(fCvmfsOcdb);
-    getYearScript = getYearScript.Strip(TString::kTrailing, '/');
-    getYearScript.Append("/bin/getUriFromYear.sh");
-    if (gSystem->AccessPathName(getYearScript))
-      AliFatal(Form("Cannot find valid script: %s", getYearScript.Data()));
-    TString inoutFile(gSystem->WorkingDirectory());
-    inoutFile += "/uri_range_";
-    inoutFile += TString::Itoa(fRun,10);
-    TString command(getYearScript);
-    command += ' ';
-    command += TString::Itoa(fRun,10);
-    command += Form(" > %s", inoutFile.Data());
-    AliDebug(3, Form("Running command: \"%s\"",command.Data()));
-    Int_t result = gSystem->Exec(command.Data());
-    if(result != 0) {
-      AliFatal(Form("Was not able to execute \"%s\"", command.Data()));
-    }
+  TString lhcPeriod("");
+  Int_t startRun = 0, endRun = 0;
+  if (! fCvmfsOcdb.IsNull()) { // fRaw and cvmfs case: set LHC period from cvmfs file
+    GetLHCPeriodAgainstCvmfsFile(run, lhcPeriod, startRun, endRun);
+  } else {                     // fRaw: set LHC period from AliEn XML file
+    GetLHCPeriodAgainstAlienFile(run, lhcPeriod, startRun, endRun);
+  }
 
-    // now read the file with the uri and first and last run
-    std::ifstream file(inoutFile.Data());
-    if (!file.is_open()) {
-      AliFatal(Form("Error opening file \"%s\"!", inoutFile.Data()));
-    }
-    TString lhcPeriod;
-    TObjArray* oStringsArray = 0;
-    while (lhcPeriod.ReadLine(file)){
-      oStringsArray = lhcPeriod.Tokenize(' ');
-    }
-    TObjString *oStrUri = dynamic_cast<TObjString*> (oStringsArray->At(0));
-    TObjString *oStrFirst = dynamic_cast<TObjString*> (oStringsArray->At(1));
-    TString firstRun = oStrFirst->GetString();
-    TObjString *oStrLast = dynamic_cast<TObjString*> (oStringsArray->At(2));
-    TString lastRun = oStrLast->GetString();
+  fLHCPeriod = lhcPeriod;
+  fStartRunLHCPeriod = startRun;
+  fEndRunLHCPeriod = endRun;
 
-    fLHCPeriod = oStrUri->GetString();
-    fStartRunLHCPeriod = firstRun.Atoi();
-    fEndRunLHCPeriod = lastRun.Atoi();
+  SetDefaultStorage(fLHCPeriod.Data());
+  if(!fDefaultStorage) AliFatal(Form("%s storage not there! Please check!",fLHCPeriod.Data()));
 
-    file.close();
+}
 
-  } else { // if not cvmfs case, "plain" AliEn case
-    // retrieve XML file from alien
+//_____________________________________________________________________________
+void AliCDBManager::GetLHCPeriodAgainstAlienFile(Int_t run, TString& lhcPeriod, Int_t& startRun, Int_t& endRun) {
+// set LHC period (year + first, last run) comparing run number and AliEn XML file
+// retrieve XML file from alien
+  if(!gGrid) {
+    TGrid::Connect("alien://","");
     if(!gGrid) {
-      TGrid::Connect("alien://","");
-      if(!gGrid) {
-        AliError("Connection to alien failed!");
-        return;
-      }
-    }
-    TUUID uuid;
-    TString rndname = "/tmp/";
-    rndname += "OCDBFolderXML.";
-    rndname += uuid.AsString();
-    rndname += ".xml";
-    AliDebug(2, Form("file to be copied = %s", fgkOCDBFolderXMLfile.Data()));
-    if (!TFile::Cp(fgkOCDBFolderXMLfile.Data(), rndname.Data())) {
-      AliFatal(Form("Cannot make a local copy of OCDBFolder xml file in %s",rndname.Data()));
+      AliError("Connection to alien failed!");
+      return;
     }
-    AliCDBHandler* saxcdb = new AliCDBHandler();
-    saxcdb->SetRun(run);
-    TSAXParser *saxParser = new TSAXParser();
-    saxParser->ConnectToHandler("AliCDBHandler", saxcdb);
-    saxParser->ParseFile(rndname.Data());
-    AliInfo(Form(" LHC folder = %s", saxcdb->GetOCDBFolder().Data()));
-    AliInfo(Form(" LHC period start run = %d", saxcdb->GetStartRunRange()));
-    AliInfo(Form(" LHC period end run = %d", saxcdb->GetEndRunRange()));
-    fLHCPeriod = saxcdb->GetOCDBFolder();
-    fStartRunLHCPeriod = saxcdb->GetStartRunRange();
-    fEndRunLHCPeriod = saxcdb->GetEndRunRange();
   }
+  TUUID uuid;
+  TString rndname = "/tmp/";
+  rndname += "OCDBFolderXML.";
+  rndname += uuid.AsString();
+  rndname += ".xml";
+  AliDebug(2, Form("file to be copied = %s", fgkOCDBFolderXMLfile.Data()));
+  if (!TFile::Cp(fgkOCDBFolderXMLfile.Data(), rndname.Data())) {
+    AliFatal(Form("Cannot make a local copy of OCDBFolder xml file in %s",rndname.Data()));
+  }
+  AliCDBHandler* saxcdb = new AliCDBHandler();
+  saxcdb->SetRun(run);
+  TSAXParser *saxParser = new TSAXParser();
+  saxParser->ConnectToHandler("AliCDBHandler", saxcdb);
+  saxParser->ParseFile(rndname.Data());
+  AliInfo(Form(" LHC folder = %s", saxcdb->GetOCDBFolder().Data()));
+  AliInfo(Form(" LHC period start run = %d", saxcdb->GetStartRunRange()));
+  AliInfo(Form(" LHC period end run = %d", saxcdb->GetEndRunRange()));
+  lhcPeriod = saxcdb->GetOCDBFolder();
+  startRun = saxcdb->GetStartRunRange();
+  endRun = saxcdb->GetEndRunRange();
+}
 
-  SetDefaultStorage(fLHCPeriod.Data());
-  if(!fDefaultStorage) AliFatal(Form("%s storage not there! Please check!",fLHCPeriod.Data()));
-
+//_____________________________________________________________________________
+void AliCDBManager::GetLHCPeriodAgainstCvmfsFile(Int_t run, TString& lhcPeriod, Int_t& startRun, Int_t& endRun) {
+// set LHC period (year + first, last run) comparing run number and CVMFS file
+// We don't want to connect to AliEn just to set the uri from the runnumber
+// for that we use the script getUriFromYear.sh in the cvmfs AliRoot package
+
+  TString getYearScript(fCvmfsOcdb);
+  getYearScript = getYearScript.Strip(TString::kTrailing, '/');
+  getYearScript.Append("/bin/getUriFromYear.sh");
+  if (gSystem->AccessPathName(getYearScript))
+    AliFatal(Form("Cannot find valid script: %s", getYearScript.Data()));
+  TString inoutFile(gSystem->WorkingDirectory());
+  inoutFile += "/uri_range_";
+  inoutFile += TString::Itoa(run,10);
+  TString command(getYearScript);
+  command += ' ';
+  command += TString::Itoa(run,10);
+  command += Form(" > %s", inoutFile.Data());
+  AliDebug(3, Form("Running command: \"%s\"",command.Data()));
+  Int_t result = gSystem->Exec(command.Data());
+  if(result != 0) {
+    AliFatal(Form("Was not able to execute \"%s\"", command.Data()));
+  }
+
+  // now read the file with the uri and first and last run
+  std::ifstream file(inoutFile.Data());
+  if (!file.is_open()) {
+    AliFatal(Form("Error opening file \"%s\"!", inoutFile.Data()));
+  }
+  TString line;
+  TObjArray* oStringsArray = 0;
+  while (line.ReadLine(file)){
+    oStringsArray = line.Tokenize(' ');
+  }
+  TObjString *oStrUri = dynamic_cast<TObjString*> (oStringsArray->At(0));
+  TObjString *oStrFirst = dynamic_cast<TObjString*> (oStringsArray->At(1));
+  TString firstRun = oStrFirst->GetString();
+  TObjString *oStrLast = dynamic_cast<TObjString*> (oStringsArray->At(2));
+  TString lastRun = oStrLast->GetString();
+
+  lhcPeriod = oStrUri->GetString();
+  startRun = firstRun.Atoi();
+  endRun = lastRun.Atoi();
+
+  file.close();
 }
 
 //_____________________________________________________________________________
@@ -941,14 +995,11 @@ AliCDBEntry* AliCDBManager::Get(const AliCDBPath& path,
 }
 
 //_____________________________________________________________________________
-AliCDBEntry* AliCDBManager::Get(const AliCDBId& query, Bool_t forceCaching, Bool_t doCatch) {
+AliCDBEntry* AliCDBManager::Get(const AliCDBId& query, Bool_t forceCaching) {
 // get an AliCDBEntry object from the database
 
   // check if query's path and runRange are valid
   // query is invalid also if version is not specified and subversion is!
-  try {
-     
-  
   if (!query.IsValid()) {
     AliError(Form("Invalid query: %s", query.ToString().Data()));
     return NULL;
@@ -1023,16 +1074,8 @@ AliCDBEntry* AliCDBManager::Get(const AliCDBId& query, Bool_t forceCaching, Bool
   if(entry && !fIds->Contains(&entry->GetId())){
     fIds->Add(entry->GetId().Clone());
   }
+
   return entry;
-  }catch(const exception &e){
-    cerr << "OCDB retrieval failed!" << endl;
-    cerr << "Detailes: " << e.what() << endl;
-    if (!doCatch) {
-      throw std::runtime_error(e.what());
-    }
-    return 0;
-  }
-  return 0;
 }
 
 //_____________________________________________________________________________
@@ -1516,7 +1559,7 @@ void AliCDBManager::SetRun(Int_t run) {
     if (fStartRunLHCPeriod <= run && fEndRunLHCPeriod >= run){
       AliInfo("LHCPeriod alien folder for current run already in memory");
     }else{
-      SetDefaultStorageFromRun(run);
+      SetDefaultStorageFromRun(fRun);
       if(fEntryCache.GetEntries()!=0) ClearCache();
       return;
     }
@@ -1853,3 +1896,26 @@ AliCDBParam::~AliCDBParam() {
 
 }
 
+void AliCDBManager::ExtractBaseFolder(TString& url)
+{
+  // TBD RS
+  // remove everything but the url - 
+  // Exact copy of the AliReconstuction::Rectify.... (to be removed)
+  // 
+  //
+  TString sbs;
+  if (!(sbs=url("\\?User=[^?]*")).IsNull())                url.ReplaceAll(sbs,"");
+  if (!(sbs=url("\\?DBFolder=[^?]*")).IsNull())            url.ReplaceAll("?DB","");
+  if (!(sbs=url("\\?SE=[^?]*")).IsNull())                  url.ReplaceAll(sbs,"");
+  if (!(sbs=url("\\?CacheFolder=[^?]*")).IsNull())         url.ReplaceAll(sbs,"");
+  if (!(sbs=url("\\?OperateDisconnected=[^?]*")).IsNull()) url.ReplaceAll(sbs,"");
+  if (!(sbs=url("\\?CacheSize=[^?]*")).IsNull())           url.ReplaceAll(sbs,"");  
+  if (!(sbs=url("\\?CleanupInterval=[^?]*")).IsNull())     url.ReplaceAll(sbs,"");  
+  Bool_t slash=kFALSE,space=kFALSE;
+  while ( (slash=url.EndsWith("/")) || (space=url.EndsWith(" ")) ) {
+    if (slash) url = url.Strip(TString::kTrailing,'/');
+    if (space) url = url.Strip(TString::kTrailing,' ');
+  }
+  //url.ToLower();
+  //
+}