]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - STEER/AliCDBManager.cxx
Bug corrected.
[u/mrichter/AliRoot.git] / STEER / AliCDBManager.cxx
index cd4c52d6e3d5d59c6fcd982ce1aff6789f2e4a5f..06b412fea9391e8260f4cf9348fcb1092f8c488b 100644 (file)
 #include "AliCDBLocal.h"
 #include "AliCDBGrid.h"
 #include "AliCDBEntry.h"
-#include "AliCDBMetaData.h"
+#include "AliCDBHandler.h"
 
 #include <TObjString.h>
-#include <TSystem.h>
+#include <TSAXParser.h>
+#include <TFile.h>
+#include <TUUID.h>
+#include <TGrid.h>
 
 ClassImp(AliCDBParam)
 
@@ -37,16 +40,23 @@ ClassImp(AliCDBManager)
 //TODO OCDB and Reference folder should not be fully hardcoded but built from run number (or year/LHC period)
 TString AliCDBManager::fgkCondUri("alien://folder=/alice/cern.ch/user/a/aliprod/testCDB/CDB?user=aliprod");
 TString AliCDBManager::fgkRefUri("alien://folder=/alice/cern.ch/user/a/aliprod/testCDB/Reference?user=aliprod");
+TString AliCDBManager::fgkMCIdealStorage("alien://folder=/alice/simulation/2008/v4-15-Release/Ideal");
+TString AliCDBManager::fgkMCFullStorage("alien://folder=/alice/simulation/2008/v4-15-Release/Full");
+TString AliCDBManager::fgkMCResidualStorage("alien://folder=/alice/simulation/2008/v4-15-Release/Residual");
+TString AliCDBManager::fgkOCDBFolderXMLfile("alien:///alice/data/OCDBFoldervsRunRange.xml");
 AliCDBManager* AliCDBManager::fgInstance = 0x0;
 
 //_____________________________________________________________________________
-AliCDBManager* AliCDBManager::Instance()
+AliCDBManager* AliCDBManager::Instance(TMap *entryCache, Int_t run)
 {
 // returns AliCDBManager instance (singleton)
 
        if (!fgInstance) {
                fgInstance = new AliCDBManager();
-               fgInstance->Init();
+               if (!entryCache)
+                 fgInstance->Init();
+               else
+                 fgInstance->InitFromCache(entryCache,run);
        }
 
        return fgInstance;
@@ -68,6 +78,25 @@ void AliCDBManager::Init() {
 
        InitShortLived();
 }
+
+//_____________________________________________________________________________
+void AliCDBManager::InitFromCache(TMap *entryCache, Int_t run) {
+// initialize manager from existing cache
+// used on the slaves in case of parallel reconstruction
+  SetRun(run);
+
+  TIter iter(entryCache->GetTable());
+  TPair* pair = 0;
+
+  while((pair = dynamic_cast<TPair*> (iter.Next()))){
+    fEntryCache.Add(pair->Key(),pair->Value());
+  }
+  // fEntry is the new owner of the cache
+  fEntryCache.SetOwnerKeyValue(kTRUE,kTRUE);
+  entryCache->SetOwnerKeyValue(kFALSE,kFALSE);
+  AliInfo(Form("%d cache entries have been loaded",fEntryCache.GetEntries()));
+}
+
 //_____________________________________________________________________________
 void AliCDBManager::Destroy() {
 // delete ALCDBManager instance and active storages
@@ -95,13 +124,19 @@ AliCDBManager::AliCDBManager():
   fRefParam(0),
   fRun(-1),
   fCache(kTRUE),
-  fLock(kFALSE)
+  fLock(kFALSE),
+  fRaw(kFALSE),
+  fStartRunLHCPeriod(-1),
+  fEndRunLHCPeriod(-1),
+  fLHCPeriod(""),
+  fKey(0)
 {
 // default constuctor
        fFactories.SetOwner(1);
        fActiveStorages.SetOwner(1);
        fSpecificStorages.SetOwner(1);
-       fEntryCache.SetOwner(1);
+       fEntryCache.SetName("CDBEntryCache");
+       fEntryCache.SetOwnerKeyValue(kTRUE,kTRUE);
 
        fStorageMap = new TMap();
        fStorageMap->SetOwner(1);
@@ -289,26 +324,43 @@ Bool_t AliCDBManager::Drain(AliCDBEntry *entry) {
        return fDrainStorage->Put(entry);
 }
 
-//_____________________________________________________________________________
+//____________________________________________________________________________
 void AliCDBManager::SetDefaultStorage(const char* dbString) {
 // sets default storage from URI string
        
-       AliInfo(Form("Setting Default storage to: %s",dbString));
-       AliCDBStorage* bckStorage = fDefaultStorage;
+       // checking whether we are in the raw case
+       TString dbStringTemp(dbString);
+       if (dbStringTemp == "raw://")
+       {
+               fRaw = kTRUE;
+               AliInfo("Setting the run-number will set the corresponding OCDB for raw data reconstruction.");
+               AliInfo("Connecting to the grid...");
+               if(!gGrid) {
+                       TGrid::Connect("alien://","");
+                       if(!gGrid) {
+                               AliError("Connection to alien failed!");
+                               return;
+                       }
+               }
+               return;
+       }
 
+       AliCDBStorage* bckStorage = fDefaultStorage;
+       
        fDefaultStorage = GetStorage(dbString);
-
+       
+       if(!fDefaultStorage) return;
+       
        if(bckStorage && (fDefaultStorage != bckStorage)){
                AliWarning("Existing default storage replaced: clearing cache!");
                ClearCache();
        }
-
+       
        if (fStorageMap->Contains("default")) {
                delete fStorageMap->Remove(fStorageMap->GetValue("default"));
        }
        fStorageMap->Add(new TObjString("default"), new TObjString(fDefaultStorage->GetURI()));
 }
-
 //_____________________________________________________________________________
 void AliCDBManager::SetDefaultStorage(const AliCDBParam* param) {
 // set default storage from AliCDBParam object
@@ -317,6 +369,8 @@ void AliCDBManager::SetDefaultStorage(const AliCDBParam* param) {
 
        fDefaultStorage = GetStorage(param);
 
+       if(!fDefaultStorage) return;
+
        if(bckStorage && (fDefaultStorage != bckStorage)){
                AliWarning("Existing default storage replaced: clearing cache!");
                ClearCache();
@@ -360,6 +414,82 @@ void AliCDBManager::SetDefaultStorage(AliCDBStorage* storage) {
        fStorageMap->Add(new TObjString("default"), new TObjString(fDefaultStorage->GetURI()));
 }
 
+//_____________________________________________________________________________
+void AliCDBManager::SetDefaultStorage(const char* mcString, const char* simType) {
+// sets default storage for MC data
+// mcString MUST be "MC", 
+// simType can be "Ideal","Residual","Full"
+       
+       TString strmcString(mcString);
+       TString strsimType(simType);
+       TString dbString; 
+        if (strmcString != "MC"){
+               AliFatal("Method requires first string to be MC!");
+       }
+        else {
+               if (strsimType == "Ideal"){
+                       dbString = fgkMCIdealStorage;
+               }
+               else if (strsimType == "Full"){
+                       dbString = fgkMCFullStorage;
+               }
+               else if (strsimType == "Residual"){
+                       dbString = fgkMCResidualStorage;
+               }
+               else {
+                       AliFatal("Error in setting the storage for MC data, second argument MUST be either \"Ideal\" or \"Full\" or \"Residual\".");
+               }
+
+               SetDefaultStorage(dbString.Data());
+               if(!fDefaultStorage) AliFatal(Form("%s storage not there! Please check!",fLHCPeriod.Data()));
+       }
+}
+//_____________________________________________________________________________
+void AliCDBManager::SetDefaultStorageFromRun(Int_t run) {
+// set default storage from the run number - to be used only with raw data     
+
+       // if lock is ON, cannot activate more storages!
+       if(fLock) {
+               if (fDefaultStorage) {
+                       AliFatal("Lock is ON, and default storage is already set: "
+                               "cannot activate default storage from run number");
+               }
+       }       
+
+       // retrieve XML file from 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()));
+       }
+       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();
+
+       SetDefaultStorage(fLHCPeriod.Data());
+       if(!fDefaultStorage) AliFatal(Form("%s storage not there! Please check!",fLHCPeriod.Data()));
+
+}
+
 //_____________________________________________________________________________
 void AliCDBManager::UnsetDefaultStorage() {
 // Unset default storage
@@ -375,6 +505,9 @@ void AliCDBManager::UnsetDefaultStorage() {
                AliWarning("Clearing cache!");
                ClearCache();
        }
+
+       fRun = fStartRunLHCPeriod = fEndRunLHCPeriod = -1;
+       fRaw = kFALSE;
        
        fDefaultStorage = 0x0;
 }
@@ -398,11 +531,13 @@ void AliCDBManager::SetSpecificStorage(const char* calibType, AliCDBParam* param
 // AliCDBManager::instance()->SetSpecificStorage("*/Align/*","local://DB_TPCAlign");
 // calibType must be a valid CDB path! (3 level folder structure)
 
-       if(!fDefaultStorage) {
+
+       if(!fDefaultStorage && !fRaw) {
                AliError("Please activate a default storage first!");
                return;
        }
 
+
        AliCDBPath aPath(calibType);
        if(!aPath.IsValid()){
                AliError(Form("Not a valid path: %s", calibType));
@@ -417,7 +552,8 @@ void AliCDBManager::SetSpecificStorage(const char* calibType, AliCDBParam* param
                if(checkPar) delete checkPar;
                delete fSpecificStorages.Remove(objCalibType);
        }
-       GetStorage(param);
+       AliCDBStorage *aStorage = GetStorage(param);
+       if(!aStorage) return;
 
        fSpecificStorages.Add(objCalibType, param->CloneParam());
 
@@ -497,11 +633,6 @@ AliCDBEntry* AliCDBManager::Get(const AliCDBPath& path,
 AliCDBEntry* AliCDBManager::Get(const AliCDBId& query) {
 // get an AliCDBEntry object from the database
        
-       if(!fDefaultStorage) {
-               AliError("No storage set!");
-               return NULL;
-       }
-
        // check if query's path and runRange are valid
        // query is invalid also if version is not specified and subversion is!
        if (!query.IsValid()) {
@@ -516,10 +647,10 @@ AliCDBEntry* AliCDBManager::Get(const AliCDBId& query) {
                 return NULL;
        }
 
-       if(fLock && query.GetFirstRun() != fRun)
+       if(fLock && !(fRun >= query.GetFirstRun() && fRun <= query.GetLastRun())) 
                AliFatal("Lock is ON: cannot use different run number than the internal one!");
        
-       if(fCache && query.GetFirstRun() != fRun)
+       if(fCache && !(fRun >= query.GetFirstRun() && fRun <= query.GetLastRun())) 
                AliWarning("Run number explicitly set in query: CDB cache temporarily disabled!");
 
        AliCDBEntry *entry=0;
@@ -533,6 +664,10 @@ AliCDBEntry* AliCDBManager::Get(const AliCDBId& query) {
                return entry;
        }
 
+       if(!fDefaultStorage) {
+               AliError("No storage set!");
+               return NULL;
+       }
        // Entry is not in cache -> retrieve it from CDB and cache it!!
        AliCDBStorage *aStorage=0;
        AliCDBParam *aPar=SelectSpecificStorage(query.GetPath());
@@ -796,9 +931,14 @@ TList* AliCDBManager::GetAll(const AliCDBId& query) {
 }
 
 //_____________________________________________________________________________
-Bool_t AliCDBManager::Put(TObject* object, AliCDBId& id,  AliCDBMetaData* metaData, DataType type){
+Bool_t AliCDBManager::Put(TObject* object, AliCDBId& id, AliCDBMetaData* metaData, const DataType type){
 // store an AliCDBEntry object into the database
 
+       if (object==0x0) {
+               AliError("Null Entry! No storage will be done!");
+               return kFALSE;
+       } 
+
        AliCDBEntry anEntry(object, id, metaData);
        return Put(&anEntry, type);
 
@@ -819,6 +959,11 @@ Bool_t AliCDBManager::Put(AliCDBEntry* entry, DataType type){
                return kFALSE;
        }
 
+       if (entry->GetObject()==0x0){
+               AliError("No valid object in CDB entry!");
+               return kFALSE;
+       }
+
        if (!entry->GetId().IsValid()) {
                AliError(Form("Invalid entry ID: %s", 
                        entry->GetId().ToString().Data()));
@@ -918,7 +1063,7 @@ void AliCDBManager::SetRun(Int_t run)
 // Sets current run number.
 // When the run number changes the caching is cleared.
        
-       if (fRun == run)
+       if(fRun == run)
                return;
   
        if(fLock && fRun >= 0) {
@@ -926,6 +1071,16 @@ void AliCDBManager::SetRun(Int_t run)
        }       
                
        fRun = run;
+       if(fRaw){
+               // here the LHCPeriod xml file is parsed; the string containing the correct period is returned; the default storage is set
+               if (fStartRunLHCPeriod <= run && fEndRunLHCPeriod >= run){
+                       AliInfo("LHCPeriod alien folder for current run already in memory");
+               }else{
+                       SetDefaultStorageFromRun(run);
+                       if(fEntryCache.GetEntries()!=0) ClearCache();
+                       return;
+               }
+       }
        ClearCache();
        QueryCDB();
 }
@@ -957,20 +1112,23 @@ void AliCDBManager::ClearCache(){
 void AliCDBManager::UnloadFromCache(const char* path){
 // unload cached object
 
+       if(!fActiveStorages.GetEntries()) {
+               AliDebug(2, Form("No active storages. Object \"%s\" is not unloaded from cache", path));
+               return;
+       }
+
        AliCDBPath queryPath(path);
        if(!queryPath.IsValid()) return;
 
        if(!queryPath.IsWildcard()) { // path is not wildcard, get it directly from the cache and unload it!
                if(fEntryCache.Contains(path)){
-                       AliInfo(Form("Unloading object \"%s\" from cache", path));
+                       AliDebug(2, Form("Unloading object \"%s\" from cache", path));
                        TObjString pathStr(path);
-                       AliCDBEntry *entry = dynamic_cast<AliCDBEntry*> (fEntryCache.GetValue(&pathStr));
-                       if(entry) delete entry;
                        delete fEntryCache.Remove(&pathStr);
                } else {
                        AliError(Form("Cache does not contain object \"%s\"!", path))
                }
-               AliDebug(2,Form("Cache entries: %d",fEntryCache.GetEntries()));
+               AliDebug(2, Form("Cache entries: %d",fEntryCache.GetEntries()));
                return;
        }
 
@@ -981,10 +1139,8 @@ void AliCDBManager::UnloadFromCache(const char* path){
        while((pair = dynamic_cast<TPair*> (iter.Next()))){
                AliCDBPath entryPath = pair->Key()->GetName();
                if(queryPath.Comprises(entryPath)) {
-                       AliInfo(Form("Unloading object \"%s\" from cache", entryPath.GetPath().Data()));
+                       AliDebug(2, Form("Unloading object \"%s\" from cache", entryPath.GetPath().Data()));
                        TObjString pathStr(entryPath.GetPath().Data());
-                       AliCDBEntry *entry = dynamic_cast<AliCDBEntry*> (fEntryCache.GetValue(&pathStr));
-                       if(entry) delete entry;
                        delete fEntryCache.Remove(&pathStr);
                }
        }
@@ -1121,13 +1277,32 @@ Bool_t AliCDBManager::IsShortLived(const char* path)
 }
 
 //______________________________________________________________________________________________
-void AliCDBManager::SetLock(Bool_t lock){
-
-       if(fLock == kTRUE && lock == kFALSE) {
-               AliFatal("Lock is ON: cannot reset it!");
-       }
-       
-       fLock=lock;
+ULong_t AliCDBManager::SetLock(Bool_t lock, ULong_t key){
+  // To lock/unlock user must provide the key. A new key is provided after
+  // each successful lock. User should always backup the returned key and
+  // use it on next access.
+  if (fLock == lock) return 0;  // nothing to be done
+  if (lock) {
+    // User wants to lock - check his identity
+    if (fKey) {
+      // Lock has a user - check his key
+      if (fKey != key) {
+        AliFatal("Wrong key provided to lock CDB. Please remove CDB lock access from your code !");
+        return 0;
+      }  
+    }  
+    // Provide new key 
+    fKey = gSystem->Now();
+    fLock = kTRUE;
+    return fKey;
+  }
+  // User wants to unlock - check the provided key
+  if (key != fKey) {
+    AliFatal("Lock is ON: wrong key provided");
+    return 0;
+  }  
+  fLock = kFALSE;
+  return key;  
 }
 
 ///////////////////////////////////////////////////////////