* provided "as is" without express or implied warranty. *
**************************************************************************/
-/* $Id$ */
+#include <TKey.h>
+#include <TH1.h>
+#include <TTree.h>
+#include <TNtuple.h>
+#include <TFile.h>
+#include "AliCDBStorage.h"
+#include "AliCDBGrid.h"
-///////////////////////////////////////////////////////////////////////////////
-// //
-// base class for data base access classes //
-// //
-///////////////////////////////////////////////////////////////////////////////
+#include "AliCDBEntry.h"
+#include "AliLog.h"
+ClassImp(AliCDBStorage)
-#include <TFile.h>
-#include <TKey.h>
-#include <TROOT.h>
+//_____________________________________________________________________________
+AliCDBStorage::AliCDBStorage():
+fValidFileIds(),
+fRun(-1),
+fPathFilter(),
+fVersion(-1),
+fMetaDataFilter(0),
+fSelections(),
+fURI(),
+fType(),
+fBaseFolder()
+{
+// constructor
-#include "AliLog.h"
-#include "AliCDBEntry.h"
-#include "AliCDBStorage.h"
+ fValidFileIds.SetOwner(1);
+ fSelections.SetOwner(1);
+}
+//_____________________________________________________________________________
+AliCDBStorage::~AliCDBStorage() {
+// destructor
-ClassImp(AliCDBStorage)
+ RemoveAllSelections();
+ fValidFileIds.Clear();
+ delete fMetaDataFilter;
+}
-AliCDBStorage* AliCDBStorage::fgInstance = NULL;
+//_____________________________________________________________________________
+void AliCDBStorage::GetSelection(/*const*/ AliCDBId* id) {
+// return required version and subversion from the list of selection criteria
+
+ TIter iter(&fSelections);
+ AliCDBId* aSelection;
+
+ // loop on the list of selection criteria
+ while ((aSelection = (AliCDBId*) iter.Next())) {
+ // check if selection element contains id's path and run (range)
+ if (aSelection->Comprises(*id)) {
+ AliDebug(2,Form("Using selection criterion: %s ", aSelection->ToString().Data()));
+ // return required version and subversion
+
+ id->SetVersion(aSelection->GetVersion());
+ id->SetSubVersion(aSelection->GetSubVersion());
+ return;
+ }
+ }
+
+ // no valid element is found in the list of selection criteria -> return
+ AliDebug(2,"Looking for objects with most recent version");
+ return;
+}
+//_____________________________________________________________________________
+void AliCDBStorage::ReadSelectionFromFile(const char *fileName){
+// read selection criteria list from file
+
+ RemoveAllSelections();
+
+ TList *list = GetIdListFromFile(fileName);
+ if(!list) return;
+
+ list->SetOwner();
+ Int_t nId = list->GetEntries();
+ AliCDBId *id;
+ TKey *key;
+
+ for(int i=nId-1;i>=0;i--){
+ key = (TKey*) list->At(i);
+ id = (AliCDBId*) key->ReadObj();
+ if(id) AddSelection(*id);
+ }
+ delete list;
+ AliInfo(Form("Selection criteria list filled with %d entries",fSelections.GetEntries()));
+ PrintSelectionList();
+
+}
//_____________________________________________________________________________
-AliCDBStorage::AliCDBStorage() :
- TObject(),
- fSelection(),
- fEntries(),
- fDumpFile(NULL)
-{
-// default constructor
- if (fgInstance) delete fgInstance;
- fgInstance = this;
- fStorageMode=kDevelopment;
+void AliCDBStorage::AddSelection(const AliCDBId& selection) {
+// add a selection criterion
+
+ AliCDBPath path = selection.GetPath();
+ if(!path.IsValid()) return;
+
+ TIter iter(&fSelections);
+ const AliCDBId *anId;
+ while((anId = (AliCDBId*) iter.Next())){
+ if(selection.Comprises(*anId)){
+ AliWarning("This selection is more general than a previous one and will hide it!");
+ AliWarning(Form("%s", (anId->ToString()).Data()));
+ fSelections.AddBefore(anId, new AliCDBId(selection));
+ return;
+ }
+
+ }
+ fSelections.AddFirst(new AliCDBId(selection));
}
//_____________________________________________________________________________
-AliCDBStorage::~AliCDBStorage()
-{
-// destructor
- fSelection.Delete();
- fEntries.Delete();
- if (fDumpFile) {
- fDumpFile->Close();
- delete fDumpFile;
- }
- fgInstance = NULL;
+void AliCDBStorage::AddSelection(const AliCDBPath& path,
+ const AliCDBRunRange& runRange, Int_t version, Int_t subVersion){
+// add a selection criterion
+
+ AddSelection(AliCDBId(path, runRange, version, subVersion));
}
//_____________________________________________________________________________
-AliCDBStorage::AliCDBStorage(const AliCDBStorage& db) :
- TObject(db),
- fSelection(),
- fEntries(),
- fDumpFile(NULL)
-{
-// copy constructor
+void AliCDBStorage::AddSelection(const AliCDBPath& path,
+ Int_t firstRun, Int_t lastRun, Int_t version, Int_t subVersion){
+// add a selection criterion
+
+ AddSelection(AliCDBId(path, firstRun, lastRun, version, subVersion));
+}
+
+//_____________________________________________________________________________
+void AliCDBStorage::RemoveSelection(const AliCDBId& selection) {
+// remove a selection criterion
+
+ TIter iter(&fSelections);
+ AliCDBId* aSelection;
- AliFatal("not implemented");
+ while ((aSelection = (AliCDBId*) iter.Next())) {
+ if (selection.Comprises(*aSelection)) {
+ fSelections.Remove(aSelection);
+ }
+ }
}
//_____________________________________________________________________________
-AliCDBStorage& AliCDBStorage::operator = (const AliCDBStorage& /*db*/)
-{
-// assignment operator
+void AliCDBStorage::RemoveSelection(const AliCDBPath& path,
+ const AliCDBRunRange& runRange){
+// remove a selection criterion
- AliFatal("not implemented");
- return *this;
+ RemoveSelection(AliCDBId(path, runRange, -1, -1));
}
+//_____________________________________________________________________________
+void AliCDBStorage::RemoveSelection(const AliCDBPath& path,
+ Int_t firstRun, Int_t lastRun){
+// remove a selection criterion
+
+ RemoveSelection(AliCDBId(path, firstRun, lastRun, -1, -1));
+}
//_____________________________________________________________________________
-const TObject* AliCDBStorage::Get(const char* name, Int_t runNumber)
-{
-// get an object from the data base
-// (AliCDBStorage is NOT the owner of the returned object)
-// name must be in the form "Detector/DBType/DetSpecType"
-// es: "ZDC/Calib/Pedestals"
-
- AliCDBMetaDataSelect defaultMetaData;
- AliCDBMetaDataSelect* selectedMetaData = &defaultMetaData;
-
- // look for a meta data selection
- for (Int_t i = 0; i < fSelection.GetEntriesFast(); i++) {
- AliCDBMetaDataSelect* selection = (AliCDBMetaDataSelect*) fSelection[i];
- if (!selection) continue;
- if (selection->Matches(name, runNumber)) {
- selectedMetaData = selection;
- }
- }
-
- // get the entry
- AliCDBMetaDataSelect selMetaData(*selectedMetaData);
- selMetaData.SetName(name);
- AliCDBEntry* entry = GetEntry(selMetaData, runNumber);
- if (entry) {
- AliDebug(2, "got the entry:");
- ToAliDebug(2, entry->Dump());
- } else {
- AliDebug(2, Form("got no entry for %s", name));
- }
-
- // update array of current entries
- if (!entry) return NULL;
- TObject* oldEntry = fEntries.FindObject(entry->GetName());
- if (oldEntry) {
- delete fEntries.Remove(oldEntry);
- }
- fEntries.Add(entry);
-
- // Dump entry to a file (in the same way as AliCDBDump::PutEntry,
- // so that the file can be opened as a AliCDBDump!)
-
- if (fDumpFile) {
- fDumpFile->cd();
- TDirectory* saveDir = gDirectory;
-
- // go to or create the directory
- TString strname(name);
- while (strname.BeginsWith("/")) strname.Remove(0);
- TDirectory* dir = fDumpFile;
- Int_t index = -1;
- while ((index = strname.Index("/")) >= 0) {
- TString dirName(strname(0, index));
- if ((index > 0) && !dir->Get(dirName)) dir->mkdir(dirName);
- dir->cd(dirName);
- dir = gDirectory;
- strname.Remove(0, index+1);
- }
-
- entry->Write(strname);
- if (saveDir) saveDir->cd(); else gROOT->cd();
-
- }
-
- return (entry->GetObject())->Clone();
-
-}
-
-//_____________________________________________________________________________
-Bool_t AliCDBStorage::Put(const TObject* object,
- const AliCDBMetaData& metaData)
-{
-// put an object into the data base
-// (AliCDBStorage does not adopt the object)
-// location of where the object is stored is defined by
-// the AliCDBMetaData's name ("Detector/DBType/DetSpecType")
-// and run Range. Storage is handled by the PutEntry method
-// of the current AliCDBStorage instance.
+void AliCDBStorage::RemoveSelection(int position){
+// remove a selection criterion from its position in the list
+
+ delete fSelections.RemoveAt(position);
+}
+
+//_____________________________________________________________________________
+void AliCDBStorage::RemoveAllSelections(){
+// remove all selection criteria
+
+ fSelections.Clear();
+}
- if (!object) return kFALSE;
+//_____________________________________________________________________________
+void AliCDBStorage::PrintSelectionList(){
+// prints the list of selection criteria
- AliCDBEntry *entry= new AliCDBEntry(object, metaData);
+ TIter iter(&fSelections);
+ AliCDBId* aSelection;
- Bool_t result = PutEntry(entry);
-
- delete entry;
+ // loop on the list of selection criteria
+ int index=0;
+ while ((aSelection = (AliCDBId*) iter.Next())) {
+ AliInfo(Form("index %d -> selection: %s",index++, aSelection->ToString().Data()));
+ }
- return result;
}
//_____________________________________________________________________________
-Bool_t AliCDBStorage::PutEntry(AliCDBEntry* entry)
-{
-// put an object into the data base
-// Refer to the specific method of the current AliCDBStorage instance
-// (AliCDBDump, AliCDBLocalFile, AliCDBGrid)
+AliCDBEntry* AliCDBStorage::Get(const AliCDBId& query) {
+// 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!
+ if (!query.IsValid()) {
+ AliError(Form("Invalid query: %s", query.ToString().Data()));
+ return NULL;
+ }
+
+ // query is not specified if path contains wildcard or runrange = [-1,-1]
+ if (!query.IsSpecified()) {
+ AliError(Form("Unspecified query: %s",
+ query.ToString().Data()));
+ return NULL;
+ }
+
+ // This is needed otherwise TH1 objects (histos, TTree's) are lost when file is closed!
+ Bool_t oldStatus = TH1::AddDirectoryStatus();
+ TH1::AddDirectory(kFALSE);
+
+ AliCDBEntry* entry = GetEntry(query);
+
+ if (oldStatus != kFALSE)
+ TH1::AddDirectory(kTRUE);
+
+ if (entry) {
+ // this is to make the SHUTTLE output lighter
+ if(!(query.GetPath().Contains("SHUTTLE/STATUS")))
+ AliDebug(2, Form("CDB object retrieved: %s", entry->GetId().ToString().Data()));
+ } else {
+ // this is to make the SHUTTLE output lighter
+ if(!(query.GetPath().Contains("SHUTTLE/STATUS")))
+ AliInfo(Form("No valid CDB object found! request was: %s", query.ToString().Data()));
+ }
+
+ // if drain storage is set, drain entry into drain storage
+ if(entry && (AliCDBManager::Instance())->IsDrainSet())
+ AliCDBManager::Instance()->Drain(entry);
+
+ return entry;
+}
- if (!entry) return kFALSE;
- AliError(Form("This is a read only data base. "
- "The object %s was not inserted", entry->GetName()));
- return kFALSE;
+//_____________________________________________________________________________
+AliCDBEntry* AliCDBStorage::Get(const AliCDBPath& path, Int_t runNumber,
+ Int_t version, Int_t subVersion) {
+// get an AliCDBEntry object from the database
+
+ return Get(AliCDBId(path, runNumber, runNumber, version, subVersion));
}
+//_____________________________________________________________________________
+AliCDBEntry* AliCDBStorage::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));
+}
//_____________________________________________________________________________
-void AliCDBStorage::Select(const AliCDBMetaDataSelect& selMetaData)
-{
-// add some meta data selection criteria
+TList* AliCDBStorage::GetAll(const AliCDBId& query) {
+// get multiple AliCDBEntry objects from the database
+
+
+ if (!query.IsValid()) {
+ AliError(Form("Invalid query: %s", query.ToString().Data()));
+ return NULL;
+ }
+
+ if (query.IsAnyRange()) {
+ AliError(Form("Unspecified run or runrange: %s",
+ query.ToString().Data()));
+ return NULL;
+ }
+
+ // This is needed otherwise TH1 objects (histos, TTree's) are lost when file is closed!
+ Bool_t oldStatus = TH1::AddDirectoryStatus();
+ TH1::AddDirectory(kFALSE);
+
+ TList *result = GetEntries(query);
+
+ if (oldStatus != kFALSE)
+ TH1::AddDirectory(kTRUE);
+
+ Int_t nEntries = result->GetEntries();
+
+ AliInfo(Form("%d objects retrieved. Request was: %s",
+ nEntries, query.ToString().Data()));
+ for(int i=0; i<nEntries;i++){
+ AliCDBEntry *entry = (AliCDBEntry*) result->At(i);
+ AliInfo(Form("%s",entry->GetId().ToString().Data()));
+ }
+
+
+ // if drain storage is set, drain entries into drain storage
+ if((AliCDBManager::Instance())->IsDrainSet()){
+ for(int i = 0; i<result->GetEntries(); i++){
+ AliCDBEntry* entry = (AliCDBEntry*) result->At(i);
+ AliCDBManager::Instance()->Drain(entry);
+ }
+ }
+
+
+ return result;
+}
- fSelection.Add(new AliCDBMetaDataSelect(selMetaData));
+//_____________________________________________________________________________
+TList* AliCDBStorage::GetAll(const AliCDBPath& path, Int_t runNumber,
+ Int_t version, Int_t subVersion) {
+// get multiple AliCDBEntry objects from the database
+
+ return GetAll(AliCDBId(path, runNumber, runNumber, version,
+ subVersion));
}
+//_____________________________________________________________________________
+TList* AliCDBStorage::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));
+}
//_____________________________________________________________________________
-Bool_t AliCDBStorage::DumpToFile(const char* fileName)
-{
-// Dump entries retrieved from the data base to a file with the given name
+AliCDBId* AliCDBStorage::GetId(const AliCDBId& query) {
+// get the Id of the valid object from the database (does not open the file)
+
+ // 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 runrange = [-1,-1]
+ if (!query.IsSpecified()) {
+ AliError(Form("Unspecified query: %s",
+ query.ToString().Data()));
+ return NULL;
+ }
+
+ AliCDBId* id = GetEntryId(query);
+
+ return id;
+}
- if (fDumpFile) {
- fDumpFile->Close();
- delete fDumpFile;
- }
+//_____________________________________________________________________________
+AliCDBId* AliCDBStorage::GetId(const AliCDBPath& path, Int_t runNumber,
+ Int_t version, Int_t subVersion) {
+// get the Id of the valid object from the database (does not open the file)
- TDirectory* dir = gDirectory;
- fDumpFile = TFile::Open(fileName, "UPDATE");
- if (dir) dir->cd(); else gROOT->cd();
- if (!fDumpFile || !fDumpFile->IsOpen()) {
- AliError(Form("could not open file %s", fileName));
- delete fDumpFile;
- fDumpFile = NULL;
- return kFALSE;
- }
- return kTRUE;
+ return GetId(AliCDBId(path, runNumber, runNumber, version, subVersion));
}
+//_____________________________________________________________________________
+AliCDBId* AliCDBStorage::GetId(const AliCDBPath& path,
+ const AliCDBRunRange& runRange, Int_t version,
+ Int_t subVersion) {
+// get the Id of the valid object from the database (does not open the file)
+
+ return GetId(AliCDBId(path, runRange, version, subVersion));
+}
//_____________________________________________________________________________
-AliCDBStorage* AliCDBStorage::Instance()
-{
-// return the current instance of the DB (AliCDBDump, AliCDBLocalFile...)
-// Example of usage: after creating an istance of AliCDBStorage:
-// AliCDBStorage::Instance()->Get(...)
+Bool_t AliCDBStorage::Put(TObject* object, AliCDBId& id, AliCDBMetaData* metaData, AliCDBManager::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 fgInstance;
+ return Put(&anEntry, type);
}
+//_____________________________________________________________________________
+Bool_t AliCDBStorage::Put(AliCDBEntry* entry, AliCDBManager::DataType type) {
+// store an AliCDBEntry object into the database
+
+ if (!entry){
+ AliError("No entry!");
+ 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()));
+ return kFALSE;
+ }
+
+ if (!entry->GetId().IsSpecified()) {
+ AliError(Form("Unspecified entry ID: %s",
+ entry->GetId().ToString().Data()));
+ return kFALSE;
+ }
+
+ AliCDBManager::DataType expectedType = GetDataType();
+
+ if(expectedType != AliCDBManager::kPrivate && type != expectedType) {
+ AliError(Form("It is forbidden to store %s data into a folder of type %s!",
+ AliCDBManager::GetDataTypeName(type),
+ AliCDBManager::GetDataTypeName(expectedType)));
+ return 0;
+ }
+
+ // set object's class name into metaData!
+ entry->GetMetaData()->SetObjectClassName(entry->GetObject()->ClassName());
+
+ return PutEntry(entry);
+}
//_____________________________________________________________________________
-const AliCDBMetaData& AliCDBStorage::GetCDBMetaData(const char* name)
-{
-// Returns the object's metadata of the already retrieved object
-// (useful, for example, if you want to know the format of the object you have
-// retrieved)
+void AliCDBStorage::QueryCDB(Int_t run, const char* pathFilter,
+ Int_t version, AliCDBMetaData* md){
+// query CDB for files valid for given run, and fill list fValidFileIds
+// Actual query is done in virtual function QueryValidFiles()
+
+ fRun = run;
+
+ fPathFilter = pathFilter;
+ if(!fPathFilter.IsValid()) {
+ AliError(Form("Filter not valid: %s", pathFilter));
+ fPathFilter = "*";
+ return;
+ }
+
+ fVersion = version;
+
+ AliInfo(Form("Querying files valid for run %d and path \"%s\" into CDB storage \"%s://%s\"",
+ fRun, pathFilter, fType.Data(), fBaseFolder.Data()));
+
+ // Clear fValidFileIds list (it cannot be filled twice!
+ AliDebug(2, "Clearing list of CDB Id's previously loaded");
+ fValidFileIds.Clear();
+
+ if(fMetaDataFilter) {delete fMetaDataFilter; fMetaDataFilter=0;}
+ if(md) fMetaDataFilter = dynamic_cast<AliCDBMetaData*> (md->Clone());
+
+ QueryValidFiles();
+ AliCDBId queryId(pathFilter,run,run,version);
+
+ AliInfo(Form("%d valid files found!", fValidFileIds.GetEntriesFast()));
+
+}
+
+//_____________________________________________________________________________
+void AliCDBStorage::PrintQueryCDB(){
+// print parameters used to load list of CDB Id's (fRun, fPathFilter, fVersion)
+
+ AliCDBId paramId(fPathFilter, fRun, fRun, fVersion);
+ AliInfo(Form("**** QueryCDB Parameters **** \n\t<%s>\n",
+ paramId.ToString().Data()));
+
+ if(fMetaDataFilter) fMetaDataFilter->PrintMetaData();
+
+
+ TString message = "**** Id's of valid objects found *****\n";
+ TIter iter(&fValidFileIds);
+ AliCDBId* anId=0;
+
+ // loop on the list of selection criteria
+ while ((anId = dynamic_cast<AliCDBId*>(iter.Next()))) {
+ message += Form("\t%s\n", anId->ToString().Data());
+ }
+ message += Form("\n\tTotal: %d objects found\n", fValidFileIds.GetEntriesFast());
+ AliInfo(Form("%s", message.Data()));
+}
+
+//_____________________________________________________________________________
+AliCDBManager::DataType AliCDBStorage::GetDataType() const {
+// returns the type of the data that should be stored into this storage:
+// kConditions: conditions data; kReference: reference data; kPrivate: private (user-defined) data type
+
+ if(GetType() != "alien") return AliCDBManager::kPrivate;
+
+ TString condFolder = ((AliCDBGridParam*) AliCDBManager::Instance()->GetCondParam())->GetDBFolder();
+ TString refFolder = ((AliCDBGridParam*) AliCDBManager::Instance()->GetRefParam())->GetDBFolder();
+
+ if(GetBaseFolder().Contains(condFolder)) return AliCDBManager::kCondition;
+ if(GetBaseFolder().Contains(refFolder)) return AliCDBManager::kReference;
+
+ return AliCDBManager::kPrivate;
+}
+
+//_____________________________________________________________________________
+void AliCDBStorage::LoadTreeFromFile(AliCDBEntry *entry) const {
+// Checks whether entry contains a TTree and in case loads it into memory
+
+ TObject *obj = (TObject*) entry->GetObject();
+ if (!obj) {
+ AliError("Cannot retrieve the object:");
+ entry->PrintMetaData();
+ return;
+ }
+
+ if (!strcmp(obj->ClassName(),TTree::Class_Name())) {
+
+ AliWarning("Entry contains a TTree! Loading baskets...");
+
+ TTree* tree = dynamic_cast<TTree*> (obj);
+
+ if(!tree) return;
+
+ tree->LoadBaskets();
+ tree->SetDirectory(0);
+ }
+ else if (!strcmp(obj->ClassName(),TNtuple::Class_Name())){
+
+ AliWarning("Entry contains a TNtuple! Loading baskets...");
+
+ TNtuple* ntu = dynamic_cast<TNtuple*> (obj);
+
+ if(!ntu) return;
-AliCDBEntry *entry = (AliCDBEntry*) fEntries.FindObject(name);
- if(!entry){
- AliError(Form("Entry %s not found! You make me crash!",name));
- }
- return entry->GetCDBMetaData();
+ ntu->LoadBaskets();
+ ntu->SetDirectory(0);
+ }
+ return;
}
+// //_____________________________________________________________________________
+// void AliCDBStorage::SetTreeToFile(AliCDBEntry *entry, TFile* file) const {
+// // Checks whether entry contains a TTree and in case assigns it to memory
+//
+// AliCDBMetaData *md = dynamic_cast<AliCDBMetaData*> (entry->GetMetaData());
+// if(!md) return;
+// TString objStr = md->GetObjectClassName();
+// if(objStr != "TTree") return;
+// AliWarning("Entry contains a TTree! Setting file...");
+//
+// TTree* tree = dynamic_cast<TTree*> (entry->GetObject());
+//
+// if(!tree) return;
+//
+// // tree->SetDirectory(file);
+// tree->SetDirectory(0);
+//
+// return;
+// }