]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - STEER/AliCDBManager.cxx
Container classes for the PHOS reconstruction parameters.
[u/mrichter/AliRoot.git] / STEER / AliCDBManager.cxx
index 2a7cfb58326b3e806906ae4c426871981f511143..1b33c45421b597a9778a4a036546b6b1da6527a3 100644 (file)
@@ -34,10 +34,14 @@ ClassImp(AliCDBParam)
 
 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");
 AliCDBManager* AliCDBManager::fgInstance = 0x0;
 
 //_____________________________________________________________________________
-AliCDBManager* AliCDBManager::Instance() {
+AliCDBManager* AliCDBManager::Instance()
+{
 // returns AliCDBManager instance (singleton)
 
        if (!fgInstance) {
@@ -58,6 +62,8 @@ void AliCDBManager::Init() {
        if(!gSystem->Exec("root-config --has-alien |grep yes 2>&1 > /dev/null")){ // returns 0 if yes
                AliInfo("AliEn classes enabled in Root. AliCDBGrid factory registered.");
                RegisterFactory(new AliCDBGridFactory());
+               fCondParam = CreateParameter(fgkCondUri);
+               fRefParam = CreateParameter(fgkRefUri);
        }
 }
 //_____________________________________________________________________________
@@ -74,6 +80,8 @@ void AliCDBManager::Destroy() {
 //_____________________________________________________________________________
 AliCDBManager::AliCDBManager():
   TObject(),
+  fCondParam(0),
+  fRefParam(0),
   fFactories(),
   fActiveStorages(),
   fSpecificStorages(),
@@ -98,6 +106,8 @@ AliCDBManager::~AliCDBManager() {
        fFactories.Delete();
        fDrainStorage = 0x0;
        fDefaultStorage = 0x0;
+       delete fCondParam;
+       delete fRefParam;
 }
 
 //_____________________________________________________________________________
@@ -128,7 +138,7 @@ Bool_t AliCDBManager::HasStorage(const char* dbString) const {
 
                if (factory->Validate(dbString)) {
                        return kTRUE;
-               }       
+               }
        }
 
        return kFALSE;
@@ -188,7 +198,7 @@ AliCDBStorage* AliCDBManager::GetStorage(const AliCDBParam* param) {
                if (aStorage) {
                        PutActiveStorage(param->CloneParam(), aStorage);
                        aStorage->SetURI(param->GetURI());
-                       if(fRun > 0) {
+                       if(fRun >= 0) {
                                aStorage->QueryCDB(fRun);
                        }
                        return aStorage;
@@ -233,7 +243,7 @@ void AliCDBManager::SetDrain(const char* dbString) {
 //_____________________________________________________________________________
 void AliCDBManager::SetDrain(const AliCDBParam* param) {
 // set drain storage from AliCDBParam
-       
+
        fDrainStorage = GetStorage(param);
 }
 
@@ -248,7 +258,7 @@ void AliCDBManager::SetDrain(AliCDBStorage* storage) {
 Bool_t AliCDBManager::Drain(AliCDBEntry *entry) {
 // drain retrieved object to drain storage
 
-       AliInfo("Draining into drain storage...");
+       AliDebug(2, "Draining into drain storage...");
        return fDrainStorage->Put(entry);
 }
 
@@ -264,7 +274,7 @@ void AliCDBManager::SetDefaultStorage(const char* dbString) {
 void AliCDBManager::SetDefaultStorage(const AliCDBParam* param) {
 // set default storage from AliCDBParam object
        
-       fDrainStorage = GetStorage(param);
+       fDefaultStorage = GetStorage(param);
 }
 
 //_____________________________________________________________________________
@@ -297,7 +307,7 @@ void AliCDBManager::SetSpecificStorage(const char* calibType, AliCDBParam* param
                AliError("Please activate a default storage first!");
                return;
        }
-       
+
        AliCDBPath aPath(calibType);
        if(!aPath.IsValid()){
                AliError(Form("Not a valid path: %s", calibType));
@@ -308,7 +318,9 @@ void AliCDBManager::SetSpecificStorage(const char* calibType, AliCDBParam* param
        if(fSpecificStorages.Contains(objCalibType)){
                AliWarning(Form("Storage \"%s\" already activated! It will be replaced by the new one",
                                        calibType));
-               fSpecificStorages.Remove(objCalibType);
+               AliCDBParam *checkPar = dynamic_cast<AliCDBParam*> (fSpecificStorages.GetValue(calibType));
+               if(checkPar) delete checkPar;
+               delete fSpecificStorages.Remove(objCalibType);
        }
        GetStorage(param);
        fSpecificStorages.Add(objCalibType, param->CloneParam());
@@ -318,9 +330,12 @@ void AliCDBManager::SetSpecificStorage(const char* calibType, AliCDBParam* param
 AliCDBStorage* AliCDBManager::GetSpecificStorage(const char* calibType) {
 // get storage specific for detector or calibration type 
 
-       AliCDBParam *checkPar = (AliCDBParam*) fSpecificStorages.GetValue(calibType);
+       AliCDBPath calibPath(calibType);
+       if(!calibPath.IsValid()) return NULL;
+       
+       AliCDBParam *checkPar = (AliCDBParam*) fSpecificStorages.GetValue(calibPath.GetPath());
        if(!checkPar){
-               AliError(Form("%s storage not found!",calibType));
+               AliError(Form("%s storage not found!", calibType));
                return NULL;
        } else {
                return GetStorage(checkPar);
@@ -333,10 +348,7 @@ AliCDBParam* AliCDBManager::SelectSpecificStorage(const TString& path) {
 // select storage valid for path from the list of specific storages
 
        AliCDBPath aPath(path);
-       if(!aPath.IsValid()){
-               AliError(Form("Not a valid path: %s", path.Data()));
-               return NULL;
-       }
+       if(!aPath.IsValid()) return NULL;
 
        TIter iter(&fSpecificStorages);
        TObjString *aCalibType=0;
@@ -400,18 +412,18 @@ AliCDBEntry* AliCDBManager::Get(const AliCDBId& query) {
                 return NULL;
        }
 
-       if(fCache && query.GetFirstRun() != fRun) 
+       if(fCache && query.GetFirstRun() != fRun)
                AliWarning("Run number explicitly set in query: CDB cache temporarily disabled!");
 
-       
+
        AliCDBEntry *entry=0;
-       
+
        // first look into map of cached objects
        if(fCache && query.GetFirstRun() == fRun)
                entry = (AliCDBEntry*) fEntryCache.GetValue(query.GetPath());
 
        if(entry) {
-               AliInfo(Form("Object %s retrieved from cache !!",query.GetPath().Data()));
+               AliDebug(2, Form("Object %s retrieved from cache !!",query.GetPath().Data()));
                return entry;
        }
        
@@ -436,7 +448,7 @@ AliCDBEntry* AliCDBManager::Get(const AliCDBId& query) {
                AliDebug(2,Form("Caching entry %s !",query.GetPath().Data()));
                CacheEntry(query.GetPath(), entry);
        }
-  
+
        return entry;
                
 }
@@ -460,7 +472,7 @@ TList* AliCDBManager::GetAll(const AliCDBPath& path, Int_t runNumber,
 }
 
 //_____________________________________________________________________________
-TList* AliCDBManager::GetAll(const AliCDBPath& path, 
+TList* AliCDBManager::GetAll(const AliCDBPath& path,
        const AliCDBRunRange& runRange, Int_t version, Int_t subVersion) {
 // get multiple AliCDBEntry objects from the database
 
@@ -472,6 +484,8 @@ TList* AliCDBManager::GetAll(const AliCDBId& query) {
 // get multiple AliCDBEntry objects from the database
 // Warning: this method works correctly only for queries of the type "Detector/*"
 //             and not for more specific queries e.g. "Detector/Calib/*" !
+// Warning #2: Entries are cached, but GetAll will keep on retrieving objects from OCDB!
+//             To get an object from cache use Get() function
 
        if(!fDefaultStorage) {
                AliError("No storage set!");
@@ -486,49 +500,63 @@ TList* AliCDBManager::GetAll(const AliCDBId& query) {
        if((fSpecificStorages.GetEntries()>0) && query.GetPath().BeginsWith('*')){
                 // if specific storages are active a query with "*" is ambiguous
                AliError("Query too generic in this context!");
-                return NULL;           
+                return NULL;
        }
 
        if (query.IsAnyRange()) {
                AliError(Form("Unspecified run or runrange: %s",
-                               query.ToString().Data()));      
+                               query.ToString().Data()));
                return NULL;
-       }       
-        
+       }
+
        TObjString objStrLev0(query.GetLevel0());
-       AliCDBParam *aPar = (AliCDBParam*) fSpecificStorages.GetValue(&objStrLev0);
+       //AliCDBParam *aPar = (AliCDBParam*) fSpecificStorages.GetValue(&objStrLev0);
+       AliCDBParam *aPar=SelectSpecificStorage(query.GetPath());
 
-       AliCDBStorage *aStorage;        
+       AliCDBStorage *aStorage;
        if(aPar) {
                aStorage=GetStorage(aPar);
                TString str = aPar->GetURI();
                AliDebug(2,Form("Looking into storage: %s",str.Data()));
-               
+
        } else {
                aStorage=GetDefaultStorage();
-               AliDebug(2,"Looking into default storage");     
+               AliDebug(2,"Looking into default storage");
        }
 
        TList *result = aStorage->GetAll(query);
+       if(!result) return 0;
 
-        return result;
+       // caching entries
+       if(fCache && (query.GetFirstRun() == fRun)){
+
+               TIter iter(result);
+               AliCDBEntry* entry=0;
+               while((entry = dynamic_cast<AliCDBEntry*> (iter.Next()))){
+                       const AliCDBId& anId = entry->GetId();
+                       AliDebug(2,Form("Caching entry %s !", anId.GetPath().Data()));
+                       CacheEntry(anId.GetPath(), entry);
+               }
+       }
+
+       return result;
 }
 
 //_____________________________________________________________________________
-Bool_t AliCDBManager::Put(TObject* object, AliCDBId& id,  AliCDBMetaData* metaData){
+Bool_t AliCDBManager::Put(TObject* object, AliCDBId& id,  AliCDBMetaData* metaData, DataType type){
 // store an AliCDBEntry object into the database
 
        AliCDBEntry anEntry(object, id, metaData);
-       return Put(&anEntry);
+       return Put(&anEntry, type);
 
 }
 
 
 //_____________________________________________________________________________
-Bool_t AliCDBManager::Put(AliCDBEntry* entry){
+Bool_t AliCDBManager::Put(AliCDBEntry* entry, DataType type){
 // store an AliCDBEntry object into the database
 
-       if(!fDefaultStorage) {
+       if(type == kPrivate && !fDefaultStorage) {
                AliError("No storage set!");
                return kFALSE;
        }
@@ -553,19 +581,31 @@ Bool_t AliCDBManager::Put(AliCDBEntry* entry){
        AliCDBId id = entry->GetId();
        AliCDBParam *aPar = SelectSpecificStorage(id.GetPath());
 
-       AliCDBStorage *aStorage;
+       AliCDBStorage *aStorage=0;
        
        if(aPar) {
                aStorage=GetStorage(aPar);
-               TString str = aPar->GetURI();
-               AliDebug(2,Form("Storing object into storage: %s",str.Data()));
-               
        } else {
-               aStorage=GetDefaultStorage();
-               AliDebug(2,"Storing object into default storage");      
+               switch(type){
+                       case kCondition:
+                               aStorage = GetStorage(fCondParam);
+                               break;
+                       case kReference:
+                               aStorage = GetStorage(fRefParam);
+                               break;
+                       case kPrivate:
+                               aStorage = GetDefaultStorage();
+                               break;
+               }
        }
 
-       return aStorage->Put(entry);
+       AliDebug(2,Form("Storing object into storage: %s", aStorage->GetURI().Data()));
+
+       Bool_t result = aStorage->Put(entry, type);
+
+       if(fRun >= 0) QueryCDB();
+
+       return result;
 
 
 }
@@ -585,38 +625,30 @@ void AliCDBManager::CacheEntry(const char* path, AliCDBEntry* entry)
 void AliCDBManager::Print(Option_t* /*option*/) const
 {
 // Print list of active storages and their URIs
-       AliInfo(Form("Run number: %d\n",fRun));
 
-       TString output;
-       output = "Cache is ";
+       TString output=Form("Run number = %d; ",fRun);
+       output += "Cache is ";
        if(!fCache) output += "NOT ";
-       output += "ACTIVE\n";
-       AliInfo(output.Data());
+       output += Form("ACTIVE; Number of active storages: %d\n",fActiveStorages.GetEntries());
 
        if(fDefaultStorage) {
-               AliInfo("*** Default Storage: ***");
-               output = Form("%s\n",fDefaultStorage->GetURI().Data());
-               AliInfo(output.Data());
+               output += Form("\t*** Default Storage URI: \"%s\"\n",fDefaultStorage->GetURI().Data());
+//             AliInfo(output.Data());
        }
        if(fSpecificStorages.GetEntries()>0) {
-               AliInfo("*** Specific Storages: ***");
                TIter iter(fSpecificStorages.GetTable());
                TPair *aPair=0;
+               Int_t i=1;
                while((aPair = (TPair*) iter.Next())){
-                       output = Form("Key: %s - Storage: %s",
-                               ((TObjString*) aPair->Key())->GetName(),
+                       output += Form("\t*** Specific storage %d: Path \"%s\" -> URI \"%s\"\n",
+                               i++, ((TObjString*) aPair->Key())->GetName(),
                                ((AliCDBParam*) aPair->Value())->GetURI().Data());
-                       AliInfo(output.Data());
                }
-               printf("\n");
        }
        if(fDrainStorage) {
-               AliInfo("*** Drain Storage: ***");
-               output = Form("%s\n",fDrainStorage->GetURI().Data());
-               AliInfo(output.Data());
+               output += Form("*** Drain Storage URI: %s\n",fDrainStorage->GetURI().Data());
        }
-       AliInfo(Form("Total number of active storages: %d",fActiveStorages.GetEntries()));
-
+       AliInfo(output.Data());
 }
 
 //_____________________________________________________________________________
@@ -643,6 +675,44 @@ void AliCDBManager::ClearCache(){
 
 }
 
+//_____________________________________________________________________________
+void AliCDBManager::UnloadFromCache(const char* path){
+// unload cached object
+
+       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));
+                       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()));
+               return;
+       }
+
+       // path is wildcard: loop on the cache and unload all comprised objects!
+       TIter iter(fEntryCache.GetTable());
+       TPair* pair = 0;
+
+       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()));
+                       TObjString pathStr(entryPath.GetPath().Data());
+                       AliCDBEntry *entry = dynamic_cast<AliCDBEntry*> (fEntryCache.GetValue(&pathStr));
+                       if(entry) delete entry;
+                       delete fEntryCache.Remove(&pathStr);
+               }
+       }
+       AliDebug(2,Form("Cache entries: %d",fEntryCache.GetEntries()));
+}
+
 //_____________________________________________________________________________
 void AliCDBManager::DestroyActiveStorages() {
 // delete list of active storages
@@ -663,7 +733,7 @@ void AliCDBManager::DestroyActiveStorage(AliCDBStorage* /*storage*/) {
                        delete fActiveStorages.Remove(aPair->Key());
                        storage->Delete(); storage=0x0;
        }
-*/     
+*/
 
 }
 
@@ -675,9 +745,15 @@ void AliCDBManager::QueryCDB() {
                AliError("Run number not yet set! Use AliCDBManager::SetRun.");
        return;
        }
-
-       AliInfo(Form("Querying default and specific storages for files valid for run %d", fRun));
-       fDefaultStorage->QueryCDB(fRun);
+       if (!fDefaultStorage){
+               AliError("Default storage is not set! Use AliCDBManager::SetDefaultStorage");
+       return;
+       }
+       if(fDefaultStorage->GetType() == "alien"){
+               fDefaultStorage->QueryCDB(fRun);
+       } else {
+               AliDebug(2,"Skipping query for valid files, it used only in grid...");
+       }
 
        TIter iter(&fSpecificStorages);
        TObjString *aCalibType=0;
@@ -686,13 +762,30 @@ void AliCDBManager::QueryCDB() {
                aPar = (AliCDBParam*) fSpecificStorages.GetValue(aCalibType);
                if(aPar) {
                        AliDebug(2,Form("Querying specific storage %s",aCalibType->GetName()));
-                       GetStorage(aPar)->QueryCDB(fRun,aCalibType->GetName());
-
+                       AliCDBStorage *aStorage = GetStorage(aPar);
+                       if(aStorage->GetType() == "alien"){
+                               aStorage->QueryCDB(fRun,aCalibType->GetName());
+                       } else {
+                               AliDebug(2,
+                                       "Skipping query for valid files, it is used only in grid...");
+                       }
                }
        }
-
 }
 
+//______________________________________________________________________________________________
+const char* AliCDBManager::GetDataTypeName(DataType type)
+{
+  // returns the name (string) of the data type
+
+      switch (type){
+           case kCondition: return "Conditions";
+           case kReference: return "Reference";
+           case kPrivate: return "Private";
+     }
+     return 0;
+
+}
 
 ///////////////////////////////////////////////////////////
 // AliCDBManager Parameter class                         //