]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - STEER/AliCDBManager.cxx
Correct loadpoint in ReorderKine used fro TFluka.
[u/mrichter/AliRoot.git] / STEER / AliCDBManager.cxx
index fb9fb84ecfb150108794fc294acef3337c957a3a..e0fd144c669a1c06c293df60f512dbc9d57343d4 100644 (file)
 #include "AliCDBDump.h"
 #include "AliCDBLocal.h"
 #include "AliCDBGrid.h"
-//#include "AliCDBEntry.h"
+#include "AliCDBEntry.h"
+#include "AliCDBMetaData.h"
 
-//#include <TObjString.h>
+#include <TObjString.h>
 #include <TSystem.h>
 
 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) {
@@ -57,13 +62,18 @@ 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);
        }
+
+       InitShortLived();
 }
 //_____________________________________________________________________________
 void AliCDBManager::Destroy() {
 // delete ALCDBManager instance and active storages
 
        if (fgInstance) {
+               //fgInstance->Delete();
                delete fgInstance;
                fgInstance = 0x0;
        }
@@ -71,26 +81,39 @@ void AliCDBManager::Destroy() {
 
 //_____________________________________________________________________________
 AliCDBManager::AliCDBManager():
-       fDefaultStorage(NULL),
-       fDrainStorage(NULL)
+  TObject(),
+  fCondParam(0),
+  fRefParam(0),
+  fFactories(),
+  fActiveStorages(),
+  fSpecificStorages(),
+  fDefaultStorage(NULL),
+  fRemoteStorage(NULL),
+  fDrainStorage(NULL),
+  fEntryCache(),
+  fCache(kTRUE),
+  fRun(-1),
+  fShortLived(0)
 {
 // default constuctor
-       fFactories.SetOwner();
+       fFactories.SetOwner(1);
+       fActiveStorages.SetOwner(1);
+       fSpecificStorages.SetOwner(1);
+       fEntryCache.SetOwner(1);
 }
 
 //_____________________________________________________________________________
 AliCDBManager::~AliCDBManager() {
 // destructor
+       ClearCache();
        DestroyActiveStorages();
+       fFactories.Delete();
        fDrainStorage = 0x0;
        fDefaultStorage = 0x0;
-}
-
-//_____________________________________________________________________________
-AliCDBStorage* AliCDBManager::GetActiveStorage(const AliCDBParam* param) {
-// get a storage object from the list of active storages 
-
-        return (AliCDBStorage*) fActiveStorages.GetValue(param);
+       fRemoteStorage = 0x0;
+       delete fCondParam;
+       delete fRefParam;
+       delete fShortLived; fShortLived = 0x0;
 }
 
 //_____________________________________________________________________________
@@ -116,12 +139,12 @@ Bool_t AliCDBManager::HasStorage(const char* dbString) const {
 
        TIter iter(&fFactories);
 
-       AliCDBStorageFactory* factory;
+       AliCDBStorageFactory* factory=0;
        while ((factory = (AliCDBStorageFactory*) iter.Next())) {
 
                if (factory->Validate(dbString)) {
                        return kTRUE;
-               }       
+               }
        }
 
        return kFALSE;
@@ -133,13 +156,10 @@ AliCDBParam* AliCDBManager::CreateParameter(const char* dbString) const {
 
        TIter iter(&fFactories);
 
-        AliCDBStorageFactory* factory;
+        AliCDBStorageFactory* factory=0;
         while ((factory = (AliCDBStorageFactory*) iter.Next())) {
-
                AliCDBParam* param = factory->CreateParameter(dbString);
-               if (param) {
-                       return param;
-               }
+               if(param) return param;
         }
 
         return NULL;
@@ -151,13 +171,13 @@ AliCDBStorage* AliCDBManager::GetStorage(const char* dbString) {
        
        AliCDBParam* param = CreateParameter(dbString);
        if (!param) {
+               AliError(Form("Failed to activate requested storage! Check URI: %s", dbString));
                return NULL;
-       }       
+       }
 
        AliCDBStorage* aStorage = GetStorage(param);
 
        delete param;
-       
        return aStorage;
 }
 
@@ -165,7 +185,7 @@ AliCDBStorage* AliCDBManager::GetStorage(const char* dbString) {
 AliCDBStorage* AliCDBManager::GetStorage(const AliCDBParam* param) {
 // get storage object from AliCDBParam object
 
-       // if the list of active storages already contains 
+       // if the list of active storages already contains
        // the requested storage, return it
        AliCDBStorage* aStorage = GetActiveStorage(param);
        if (aStorage) {
@@ -174,8 +194,8 @@ AliCDBStorage* AliCDBManager::GetStorage(const AliCDBParam* param) {
 
        TIter iter(&fFactories);
 
-        AliCDBStorageFactory* factory;
-        
+        AliCDBStorageFactory* factory=0;
+
        // loop on the list of registered factories
        while ((factory = (AliCDBStorageFactory*) iter.Next())) {
 
@@ -183,26 +203,40 @@ AliCDBStorage* AliCDBManager::GetStorage(const AliCDBParam* param) {
                aStorage = factory->Create(param);
                if (aStorage) {
                        PutActiveStorage(param->CloneParam(), aStorage);
-                       // if default storage is not set, set to this storage
-                       if(!fDefaultStorage){
-                               fDefaultStorage=aStorage;
-                               AliInfo(Form("Default storage set to: %s",(param->GetURI()).Data()));
+                       aStorage->SetURI(param->GetURI());
+                       if(fRun >= 0) {
+                               if(aStorage->GetType() == "alien"){
+                                       aStorage->QueryCDB(fRun);
+                               } else {
+                                       AliDebug(2,
+                                               "Skipping query for valid files, it is used only in grid...");
+                               }
                        }
                        return aStorage;
                }
         }
 
+       AliError(Form("Failed to activate requested storage! Check URI: %s", param->GetURI().Data()));
+
         return NULL;
 }
 
+//_____________________________________________________________________________
+AliCDBStorage* AliCDBManager::GetActiveStorage(const AliCDBParam* param) {
+// get a storage object from the list of active storages
+
+        return dynamic_cast<AliCDBStorage*> (fActiveStorages.GetValue(param));
+}
+
 //_____________________________________________________________________________
 TList* AliCDBManager::GetActiveStorages() {
 // return list of active storages
+// user has responsibility to delete returned object
 
        TList* result = new TList();
 
-       TIter iter(fActiveStorages.GetTable()); 
-       TPair* aPair;
+       TIter iter(fActiveStorages.GetTable());
+       TPair* aPair=0;
        while ((aPair = (TPair*) iter.Next())) {
                result->Add(aPair->Value());
        }
@@ -220,7 +254,7 @@ void AliCDBManager::SetDrain(const char* dbString) {
 //_____________________________________________________________________________
 void AliCDBManager::SetDrain(const AliCDBParam* param) {
 // set drain storage from AliCDBParam
-       
+
        fDrainStorage = GetStorage(param);
 }
 
@@ -235,7 +269,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);
 }
 
@@ -243,29 +277,637 @@ Bool_t AliCDBManager::Drain(AliCDBEntry *entry) {
 void AliCDBManager::SetDefaultStorage(const char* dbString) {
 // sets default storage from URI string
 
-       fDefaultStorage = GetStorage(dbString); 
+       AliInfo(Form("Setting Default storage to: %s",dbString));
+       AliCDBStorage* bckStorage = fDefaultStorage;
+
+       fDefaultStorage = GetStorage(dbString);
+
+       if(bckStorage && (fDefaultStorage != bckStorage)){
+               AliWarning("Existing default storage replaced: clearing cache!");
+               ClearCache();
+       }
 }
 
 //_____________________________________________________________________________
 void AliCDBManager::SetDefaultStorage(const AliCDBParam* param) {
 // set default storage from AliCDBParam object
-       
-       fDrainStorage = GetStorage(param);
+
+       AliCDBStorage* bckStorage = fDefaultStorage;
+
+       fDefaultStorage = GetStorage(param);
+
+       if(bckStorage && (fDefaultStorage != bckStorage)){
+               AliWarning("Existing default storage replaced: clearing cache!");
+               ClearCache();
+       }
 }
 
 //_____________________________________________________________________________
 void AliCDBManager::SetDefaultStorage(AliCDBStorage* storage) {
 // set default storage from another active storage
-       
+
+       AliCDBStorage* bckStorage = fDefaultStorage;
+
        fDefaultStorage = storage;
+
+       if(bckStorage && (fDefaultStorage != bckStorage)){
+               AliWarning("Existing default storage replaced: clearing cache!");
+               ClearCache();
+       }
+}
+//_____________________________________________________________________________
+void AliCDBManager::SetRemoteStorage(const char* dbString) {
+// sets remote storage from URI string
+// Remote storage is queried if it is activated and if object was not found in default storage
+
+       AliInfo(Form("Setting remote storage to: %s",dbString));
+       fRemoteStorage = GetStorage(dbString);
+}
+
+//_____________________________________________________________________________
+void AliCDBManager::SetRemoteStorage(const AliCDBParam* param) {
+// set remote storage from AliCDBParam object
+// Remote storage is queried if it is activated and if object was not found in default storage
+
+       fRemoteStorage = GetStorage(param);
+}
+
+//_____________________________________________________________________________
+void AliCDBManager::SetRemoteStorage(AliCDBStorage* storage) {
+// set remote storage from another active storage
+// Remote storage is queried if it is activated and if object was not found in default storage
+
+       fRemoteStorage = storage;
+}
+
+//_____________________________________________________________________________
+void AliCDBManager::SetSpecificStorage(const char* calibType, const char* dbString) {
+// sets storage specific for detector or calibration type (works with AliCDBManager::Get(...))
+
+       AliCDBParam *aPar = CreateParameter(dbString);
+       if(!aPar) return;
+       SetSpecificStorage(calibType, aPar);
+       delete aPar;
+}
+
+//_____________________________________________________________________________
+void AliCDBManager::SetSpecificStorage(const char* calibType, AliCDBParam* param) {
+// sets storage specific for detector or calibration type (works with AliCDBManager::Get(...))
+// Default storage should be defined prior to any specific storages, e.g.:
+// AliCDBManager::instance()->SetDefaultStorage("alien://");
+// AliCDBManager::instance()->SetSpecificStorage("TPC/*","local://DB_TPC");
+// AliCDBManager::instance()->SetSpecificStorage("*/Align/*","local://DB_TPCAlign");
+// calibType must be a valid CDB path! (3 level folder structure)
+
+       if(!fDefaultStorage) {
+               AliError("Please activate a default storage first!");
+               return;
+       }
+
+       AliCDBPath aPath(calibType);
+       if(!aPath.IsValid()){
+               AliError(Form("Not a valid path: %s", calibType));
+               return;
+       }
+
+       TObjString *objCalibType = new TObjString(aPath.GetPath());
+       if(fSpecificStorages.Contains(objCalibType)){
+               AliWarning(Form("Storage \"%s\" already activated! It will be replaced by the new one",
+                                       calibType));
+               AliCDBParam *checkPar = dynamic_cast<AliCDBParam*> (fSpecificStorages.GetValue(calibType));
+               if(checkPar) delete checkPar;
+               delete fSpecificStorages.Remove(objCalibType);
+       }
+       GetStorage(param);
+
+       fSpecificStorages.Add(objCalibType, param->CloneParam());
+}
+
+//_____________________________________________________________________________
+AliCDBStorage* AliCDBManager::GetSpecificStorage(const char* calibType) {
+// get storage specific for detector or calibration type 
+
+       AliCDBPath calibPath(calibType);
+       if(!calibPath.IsValid()) return NULL;
+
+       AliCDBParam *checkPar = (AliCDBParam*) fSpecificStorages.GetValue(calibPath.GetPath());
+       if(!checkPar){
+               AliError(Form("%s storage not found!", calibType));
+               return NULL;
+       } else {
+               return GetStorage(checkPar);
+       }
+
+}
+
+//_____________________________________________________________________________
+AliCDBParam* AliCDBManager::SelectSpecificStorage(const TString& path) {
+// select storage valid for path from the list of specific storages
+
+       AliCDBPath aPath(path);
+       if(!aPath.IsValid()) return NULL;
+
+       TIter iter(&fSpecificStorages);
+       TObjString *aCalibType=0;
+       AliCDBPath tmpPath("null/null/null");
+       AliCDBParam* aPar=0;
+       while((aCalibType = (TObjString*) iter.Next())){
+               AliCDBPath calibTypePath(aCalibType->GetName());
+               if(calibTypePath.Comprises(aPath)) {
+                       if(calibTypePath.Comprises(tmpPath)) continue;
+                       aPar = (AliCDBParam*) fSpecificStorages.GetValue(aCalibType);
+                       tmpPath.SetPath(calibTypePath.GetPath());
+               }
+       }
+       return aPar;
+}
+
+//_____________________________________________________________________________
+AliCDBEntry* AliCDBManager::Get(const AliCDBPath& path, Int_t runNumber,
+       Int_t version, Int_t subVersion) {
+// get an AliCDBEntry object from the database
+
+       if(runNumber < 0){
+               // RunNumber is not specified. Try with fRun
+               if (fRun < 0){
+                       AliError("Run number neither specified in query nor set in AliCDBManager! Use AliCDBManager::SetRun.");
+                       return NULL;
+               }
+               runNumber = fRun;
+       }
+
+       return Get(AliCDBId(path, runNumber, runNumber, version, subVersion));
+}
+
+//_____________________________________________________________________________
+AliCDBEntry* AliCDBManager::Get(const AliCDBPath& path,
+       const AliCDBRunRange& runRange, Int_t version,
+       Int_t subVersion) {
+// get an AliCDBEntry object from the database!
+
+       return Get(AliCDBId(path, runRange, version, subVersion));
+}
+
+//_____________________________________________________________________________
+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()) {
+               AliError(Form("Invalid query: %s", query.ToString().Data()));
+               return NULL;
+       }
+       
+       // query is not specified if path contains wildcard or run range= [-1,-1]
+       if (!query.IsSpecified()) {
+               AliError(Form("Unspecified query: %s",
+                               query.ToString().Data()));
+                return NULL;
+       }
+
+       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) {
+               AliDebug(2, Form("Object %s retrieved from cache !!",query.GetPath().Data()));
+               return entry;
+       }
+
+       // Entry is not in cache -> retrieve it from CDB and cache it!!
+       AliCDBStorage *aStorage=0;
+       AliCDBParam *aPar=SelectSpecificStorage(query.GetPath());
+       Bool_t usedDefStorage=kTRUE;
+
+       if(aPar) {
+               aStorage=GetStorage(aPar);
+               TString str = aPar->GetURI();
+               AliDebug(2,Form("Looking into storage: %s",str.Data()));
+               usedDefStorage=kFALSE;
+
+       } else {
+               aStorage=GetDefaultStorage();
+               AliDebug(2,"Looking into default storage");
+       }
+
+       entry = aStorage->Get(query);
+
+       if (!entry && usedDefStorage && IsRemoteStorageSet()) {
+               AliWarning(Form("Object not found in default storage: Looking into remote storage!"));
+               entry = fRemoteStorage->Get(query);
+       }
+
+       if(entry && fCache && (query.GetFirstRun() == fRun)){
+               CacheEntry(query.GetPath(), entry);
+       }
+
+       return entry;
+
+}
+
+//_____________________________________________________________________________
+AliCDBId* AliCDBManager::GetId(const AliCDBPath& path, Int_t runNumber,
+       Int_t version, Int_t subVersion) {
+// get the AliCDBId of the valid object from the database (does not retrieve the object)
+// User must delete returned object!
+
+       if(runNumber < 0){
+               // RunNumber is not specified. Try with fRun
+               if (fRun < 0){
+                       AliError("Run number neither specified in query nor set in AliCDBManager! Use AliCDBManager::SetRun.");
+                       return NULL;
+               }
+               runNumber = fRun;
+       }
+
+       return GetId(AliCDBId(path, runNumber, runNumber, version, subVersion));
+}
+
+//_____________________________________________________________________________
+AliCDBId* AliCDBManager::GetId(const AliCDBPath& path,
+       const AliCDBRunRange& runRange, Int_t version,
+       Int_t subVersion) {
+// get the AliCDBId of the valid object from the database (does not retrieve the object)
+// User must delete returned object!
+
+       return GetId(AliCDBId(path, runRange, version, subVersion));
+}
+
+//_____________________________________________________________________________
+AliCDBId* AliCDBManager::GetId(const AliCDBId& query) {
+// get the AliCDBId of the valid object from the database (does not retrieve the object)
+// User must delete returned object!
+
+       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()) {
+               AliError(Form("Invalid query: %s", query.ToString().Data()));
+               return NULL;
+       }
+       
+       // query is not specified if path contains wildcard or run range= [-1,-1]
+       if (!query.IsSpecified()) {
+               AliError(Form("Unspecified query: %s",
+                               query.ToString().Data()));
+                return NULL;
+       }
+
+       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) {
+               AliDebug(2, Form("Object %s retrieved from cache !!",query.GetPath().Data()));
+               return dynamic_cast<AliCDBId*> (entry->GetId().Clone());
+       }
+
+       // Entry is not in cache -> retrieve it from CDB and cache it!!
+       AliCDBStorage *aStorage=0;
+       AliCDBParam *aPar=SelectSpecificStorage(query.GetPath());
+
+       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");
+       }
+
+       return aStorage->GetId(query);
+
+}
+
+//_____________________________________________________________________________
+TList* AliCDBManager::GetAll(const AliCDBPath& path, Int_t runNumber,
+       Int_t version, Int_t subVersion) {
+// get multiple AliCDBEntry objects from the database
+
+       if(runNumber < 0){
+               // RunNumber is not specified. Try with fRun
+               if (fRun < 0){
+                       AliError("Run number neither specified in query nor set in AliCDBManager! Use AliCDBManager::SetRun.");
+                       return NULL;
+               }
+               runNumber = fRun;
+       }
+
+       return GetAll(AliCDBId(path, runNumber, runNumber, version,     
+                       subVersion));
+}
+
+//_____________________________________________________________________________
+TList* AliCDBManager::GetAll(const AliCDBPath& path,
+       const AliCDBRunRange& runRange, Int_t version, Int_t subVersion) {
+// get multiple AliCDBEntry objects from the database
+
+       return GetAll(AliCDBId(path, runRange, version, subVersion));
+}
+
+//_____________________________________________________________________________
+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!");
+               return NULL;
+       }
+
+       if (!query.IsValid()) {
+                AliError(Form("Invalid query: %s", query.ToString().Data()));
+                return NULL;
+        }
+
+       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;
+       }
+
+       if (query.IsAnyRange()) {
+               AliError(Form("Unspecified run or runrange: %s",
+                               query.ToString().Data()));
+               return NULL;
+       }
+
+       AliCDBParam *aPar=SelectSpecificStorage(query.GetPath());
+
+       AliCDBStorage *aStorage;
+       if(aPar) {
+               aStorage=GetStorage(aPar);
+               AliDebug(2,Form("Looking into storage: %s", aPar->GetURI().Data()));
+
+       } else {
+               aStorage=GetDefaultStorage();
+               AliDebug(2,"Looking into default storage");
+       }
+
+       TList *result = 0;
+       if(aStorage) result = aStorage->GetAll(query);
+       if(!result) return 0;
+
+       // loop on result to check whether entries should be re-queried with specific storages
+       if(fSpecificStorages.GetEntries()>0 && ! (fSpecificStorages.GetEntries() == 1 && aPar)) {
+               AliInfo("Now look into all other specific storages...");
+
+               TIter iter(result);
+               AliCDBEntry* chkEntry=0;
+
+               while((chkEntry = dynamic_cast<AliCDBEntry*> (iter.Next()))){
+                       AliCDBId& chkId = chkEntry->GetId();
+                       AliDebug(2, Form("Checking id %s ", chkId.GetPath().Data()));
+                       AliCDBParam *chkPar=SelectSpecificStorage(chkId.GetPath());
+                       if (!chkPar || aPar == chkPar) continue;
+                       AliCDBStorage *chkStorage = GetStorage(chkPar);
+                       AliDebug(2, Form("Found specific storage! %s", chkPar->GetURI().Data()));
+
+                       AliCDBEntry *newEntry=0;
+                       chkId.SetRunRange(query.GetFirstRun(), query.GetLastRun());
+                       chkId.SetVersion(query.GetVersion());
+                       chkId.SetSubVersion(query.GetSubVersion());
+
+                       if(chkStorage) newEntry = chkStorage->Get(chkId);
+                       if(!newEntry) continue;
+
+                       // object is found in specific storage: replace entry in the result list!
+                       chkEntry->SetOwner(1);
+                       delete result->Remove(chkEntry);
+                       result->AddFirst(newEntry);
+               }
+
+               Int_t nEntries = result->GetEntries();
+               AliInfo("After look into other specific storages, result list is:");
+               for(int i=0; i<nEntries;i++){
+                       AliCDBEntry *entry = (AliCDBEntry*) result->At(i);
+                       AliInfo(Form("%s",entry->GetId().ToString().Data()));
+               }
+       }
+
+       // caching entries
+       if(fCache && (query.GetFirstRun() == fRun)){
+
+               TIter iter(result);
+               AliCDBEntry* entry=0;
+               while((entry = dynamic_cast<AliCDBEntry*> (iter.Next()))){
+                       CacheEntry(entry->GetId().GetPath(), entry);
+               }
+       }
+
+       return result;
+}
+
+//_____________________________________________________________________________
+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, type);
+
+}
+
+
+//_____________________________________________________________________________
+Bool_t AliCDBManager::Put(AliCDBEntry* entry, DataType type){
+// store an AliCDBEntry object into the database
+
+       if(type == kPrivate && !fDefaultStorage) {
+               AliError("No storage set!");
+               return kFALSE;
+       }
+
+       if (!entry){
+               AliError("No entry!");
+               return kFALSE;
+       }
+
+       if (!entry->GetId().IsValid()) {
+               AliError(Form("Invalid entry ID: %s", 
+                       entry->GetId().ToString().Data()));
+               return kFALSE;
+       }       
+
+       if (!entry->GetId().IsSpecified()) {
+               AliError(Form("Unspecified entry ID: %s", 
+                       entry->GetId().ToString().Data()));
+               return kFALSE;
+       }
+
+       AliCDBId id = entry->GetId();
+       AliCDBParam *aPar = SelectSpecificStorage(id.GetPath());
+
+       AliCDBStorage *aStorage=0;
+       
+       if(aPar) {
+               aStorage=GetStorage(aPar);
+       } else {
+               switch(type){
+                       case kCondition:
+                               aStorage = GetStorage(fCondParam);
+                               break;
+                       case kReference:
+                               aStorage = GetStorage(fRefParam);
+                               break;
+                       case kPrivate:
+                               aStorage = GetDefaultStorage();
+                               break;
+               }
+       }
+
+       AliDebug(2,Form("Storing object into storage: %s", aStorage->GetURI().Data()));
+
+       Bool_t result = aStorage->Put(entry, type);
+
+       if(fRun >= 0) QueryCDB();
+
+       return result;
+
+
+}
+
+//_____________________________________________________________________________
+void AliCDBManager::CacheEntry(const char* path, AliCDBEntry* entry)
+{
+// cache AliCDBEntry. Cache is valid until run number is changed.
+
+       AliCDBEntry *chkEntry = dynamic_cast<AliCDBEntry*> (fEntryCache.GetValue(path));
+
+       if(chkEntry) {
+               AliDebug(2, Form("Object %s already in cache !!", path));
+               return;
+       } else {
+               AliDebug(2,Form("Caching entry %s", path));
+       }
+
+       fEntryCache.Add(new TObjString(path), entry);
+       AliDebug(2,Form("Cache entries: %d", fEntryCache.GetEntries()));
+
+}
+
+//_____________________________________________________________________________
+void AliCDBManager::Print(Option_t* /*option*/) const
+{
+// Print list of active storages and their URIs
+
+       TString output=Form("Run number = %d; ",fRun);
+       output += "Cache is ";
+       if(!fCache) output += "NOT ";
+       output += Form("ACTIVE; Number of active storages: %d\n",fActiveStorages.GetEntries());
+
+       if(fDefaultStorage) {
+               output += Form("\t*** Default Storage URI: \"%s\"\n",fDefaultStorage->GetURI().Data());
+//             AliInfo(output.Data());
+       }
+       if(fSpecificStorages.GetEntries()>0) {
+               TIter iter(fSpecificStorages.GetTable());
+               TPair *aPair=0;
+               Int_t i=1;
+               while((aPair = (TPair*) iter.Next())){
+                       output += Form("\t*** Specific storage %d: Path \"%s\" -> URI \"%s\"\n",
+                               i++, ((TObjString*) aPair->Key())->GetName(),
+                               ((AliCDBParam*) aPair->Value())->GetURI().Data());
+               }
+       }
+       if(fDrainStorage) {
+               output += Form("*** Drain Storage URI: %s\n",fDrainStorage->GetURI().Data());
+       }
+       AliInfo(output.Data());
+}
+
+//_____________________________________________________________________________
+void AliCDBManager::SetRun(Int_t run)
+{
+// Sets current run number.
+// When the run number changes the caching is cleared.
+  
+       if (fRun == run)
+               return;
+  
+       fRun = run;
+       ClearCache();
+       QueryCDB();
+}
+
+//_____________________________________________________________________________
+void AliCDBManager::ClearCache(){
+// clear AliCDBEntry cache
+
+       AliDebug(2,Form("Clearing cache!"));
+       fEntryCache.DeleteAll();
+       AliDebug(2,Form("Cache entries: %d",fEntryCache.GetEntries()));
+
 }
 
+//_____________________________________________________________________________
+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
 
        fActiveStorages.DeleteAll();
+       fSpecificStorages.DeleteAll();
 }
 
 //_____________________________________________________________________________
@@ -273,14 +915,119 @@ void AliCDBManager::DestroyActiveStorage(AliCDBStorage* /*storage*/) {
 // destroys active storage
 
 /*
-       TIter iter(fActiveStorages.GetTable()); 
+       TIter iter(fActiveStorages.GetTable());
        TPair* aPair;
        while ((aPair = (TPair*) iter.Next())) {
                if(storage == (AliCDBStorage*) aPair->Value())
                        delete fActiveStorages.Remove(aPair->Key());
                        storage->Delete(); storage=0x0;
        }
-*/     
+*/
+
+}
+
+//_____________________________________________________________________________
+void AliCDBManager::QueryCDB() {
+// query default and specific storages for files valid for fRun. Every storage loads the Ids into its list.
+
+       if (fRun < 0){
+               AliError("Run number not yet set! Use AliCDBManager::SetRun.");
+       return;
+       }
+       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;
+       AliCDBParam* aPar=0;
+       while((aCalibType = dynamic_cast<TObjString*> (iter.Next()))){
+               aPar = (AliCDBParam*) fSpecificStorages.GetValue(aCalibType);
+               if(aPar) {
+                       AliDebug(2,Form("Querying specific storage %s",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;
+
+}
+
+//______________________________________________________________________________________________
+void AliCDBManager::InitShortLived()
+{
+  // Init the list of short-lived objects
+  // currently disabled
+
+       fShortLived=0x0;
+
+//     fShortLived = new TList();
+//     fShortLived->SetOwner(1);
+//
+//     fShortLived->Add(new TObjString("EMCAL/Calib/Data"));
+// 
+//     fShortLived->Add(new TObjString("HMPID/Calib/Nmean"));
+//     fShortLived->Add(new TObjString("HMPID/Calib/Qthre"));
+// 
+//     fShortLived->Add(new TObjString("ITS/Calib/CalibSPD"));
+// 
+//     fShortLived->Add(new TObjString("MUON/Calib/Gains"));
+//     fShortLived->Add(new TObjString("MUON/Calib/HV"));
+//     fShortLived->Add(new TObjString("MUON/Calib/Pedestals"));
+// 
+//     fShortLived->Add(new TObjString("PHOS/Calib/CpvGainPedestals"));
+//     fShortLived->Add(new TObjString("PHOS/Calib/EmcGainPedestals"));
+// 
+//     fShortLived->Add(new TObjString("PMD/Calib/Data"));
+// 
+//     fShortLived->Add(new TObjString("TRD/Calib/ChamberGainFactor"));
+//     fShortLived->Add(new TObjString("TRD/Calib/LocalGainFactor"));
+//     fShortLived->Add(new TObjString("TRD/Calib/ChamberT0"));
+//     fShortLived->Add(new TObjString("TRD/Calib/LocalT0"));
+//     fShortLived->Add(new TObjString("TRD/Calib/ChamberVdrift"));
+//     fShortLived->Add(new TObjString("TRD/Calib/LocalVdrift"));
+// 
+//     fShortLived->Add(new TObjString("ZDC/Calib/Data"));
+
+}
+
+//______________________________________________________________________________________________
+Bool_t AliCDBManager::IsShortLived(const char* path)
+{
+  // returns the name (string) of the data type
+
+       if(!fShortLived) return kFALSE;
+
+       AliCDBPath aPath(path);
+       if(!aPath.IsValid()){
+               AliError(Form("Not a valid path: %s", path));
+               return kFALSE;
+       }
+
+       return fShortLived->Contains(path);
 
 }
 
@@ -290,7 +1037,10 @@ void AliCDBManager::DestroyActiveStorage(AliCDBStorage* /*storage*/) {
 // (AliCDBGridParam, AliCDBLocalParam, AliCDBDumpParam)  //
 ///////////////////////////////////////////////////////////
 
-AliCDBParam::AliCDBParam() {
+AliCDBParam::AliCDBParam():
+  fType(),
+  fURI()
+{
 // constructor
 
 }