1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
7 * Permission to use, copy, modify and distribute this software and its *
8 * documentation strictly for non-commercial purposes is hereby granted *
9 * without fee, provided that the above copyright notice appears in all *
10 * copies and that both the copyright notice and this permission notice *
11 * appear in the supporting documentation. The authors make no claims *
12 * about the suitability of this software for any purpose. It is *
13 * provided "as is" without express or implied warranty. *
14 **************************************************************************/
20 #include "AliCDBStorage.h"
21 #include "AliCDBGrid.h"
23 #include "AliCDBEntry.h"
26 ClassImp(AliCDBStorage)
28 //_____________________________________________________________________________
29 AliCDBStorage::AliCDBStorage():
42 fValidFileIds.SetOwner(1);
43 fSelections.SetOwner(1);
46 //_____________________________________________________________________________
47 AliCDBStorage::~AliCDBStorage() {
50 RemoveAllSelections();
51 fValidFileIds.Clear();
52 delete fMetaDataFilter;
56 //_____________________________________________________________________________
57 void AliCDBStorage::GetSelection(/*const*/ AliCDBId* id) {
58 // return required version and subversion from the list of selection criteria
60 TIter iter(&fSelections);
63 // loop on the list of selection criteria
64 while ((aSelection = (AliCDBId*) iter.Next())) {
65 // check if selection element contains id's path and run (range)
66 if (aSelection->Comprises(*id)) {
67 AliDebug(2,Form("Using selection criterion: %s ", aSelection->ToString().Data()));
68 // return required version and subversion
70 id->SetVersion(aSelection->GetVersion());
71 id->SetSubVersion(aSelection->GetSubVersion());
76 // no valid element is found in the list of selection criteria -> return
77 AliDebug(2,"Looking for objects with most recent version");
81 //_____________________________________________________________________________
82 void AliCDBStorage::ReadSelectionFromFile(const char *fileName){
83 // read selection criteria list from file
85 RemoveAllSelections();
87 TList *list = GetIdListFromFile(fileName);
91 Int_t nId = list->GetEntries();
95 for(int i=nId-1;i>=0;i--){
96 key = (TKey*) list->At(i);
97 id = (AliCDBId*) key->ReadObj();
98 if(id) AddSelection(*id);
101 AliInfo(Form("Selection criteria list filled with %d entries",fSelections.GetEntries()));
102 PrintSelectionList();
106 //_____________________________________________________________________________
107 void AliCDBStorage::AddSelection(const AliCDBId& selection) {
108 // add a selection criterion
110 AliCDBPath path = selection.GetPath();
111 if(!path.IsValid()) return;
113 TIter iter(&fSelections);
114 const AliCDBId *anId;
115 while((anId = (AliCDBId*) iter.Next())){
116 if(selection.Comprises(*anId)){
117 AliWarning("This selection is more general than a previous one and will hide it!");
118 AliWarning(Form("%s", (anId->ToString()).Data()));
119 fSelections.AddBefore(anId, new AliCDBId(selection));
124 fSelections.AddFirst(new AliCDBId(selection));
127 //_____________________________________________________________________________
128 void AliCDBStorage::AddSelection(const AliCDBPath& path,
129 const AliCDBRunRange& runRange, Int_t version, Int_t subVersion){
130 // add a selection criterion
132 AddSelection(AliCDBId(path, runRange, version, subVersion));
135 //_____________________________________________________________________________
136 void AliCDBStorage::AddSelection(const AliCDBPath& path,
137 Int_t firstRun, Int_t lastRun, Int_t version, Int_t subVersion){
138 // add a selection criterion
140 AddSelection(AliCDBId(path, firstRun, lastRun, version, subVersion));
143 //_____________________________________________________________________________
144 void AliCDBStorage::RemoveSelection(const AliCDBId& selection) {
145 // remove a selection criterion
147 TIter iter(&fSelections);
148 AliCDBId* aSelection;
150 while ((aSelection = (AliCDBId*) iter.Next())) {
151 if (selection.Comprises(*aSelection)) {
152 fSelections.Remove(aSelection);
157 //_____________________________________________________________________________
158 void AliCDBStorage::RemoveSelection(const AliCDBPath& path,
159 const AliCDBRunRange& runRange){
160 // remove a selection criterion
162 RemoveSelection(AliCDBId(path, runRange, -1, -1));
165 //_____________________________________________________________________________
166 void AliCDBStorage::RemoveSelection(const AliCDBPath& path,
167 Int_t firstRun, Int_t lastRun){
168 // remove a selection criterion
170 RemoveSelection(AliCDBId(path, firstRun, lastRun, -1, -1));
173 //_____________________________________________________________________________
174 void AliCDBStorage::RemoveSelection(int position){
175 // remove a selection criterion from its position in the list
177 delete fSelections.RemoveAt(position);
180 //_____________________________________________________________________________
181 void AliCDBStorage::RemoveAllSelections(){
182 // remove all selection criteria
187 //_____________________________________________________________________________
188 void AliCDBStorage::PrintSelectionList(){
189 // prints the list of selection criteria
191 TIter iter(&fSelections);
192 AliCDBId* aSelection;
194 // loop on the list of selection criteria
196 while ((aSelection = (AliCDBId*) iter.Next())) {
197 AliInfo(Form("index %d -> selection: %s",index++, aSelection->ToString().Data()));
202 //_____________________________________________________________________________
203 AliCDBEntry* AliCDBStorage::Get(const AliCDBId& query) {
204 // get an AliCDBEntry object from the database
206 // check if query's path and runRange are valid
207 // query is invalid also if version is not specified and subversion is!
208 if (!query.IsValid()) {
209 AliError(Form("Invalid query: %s", query.ToString().Data()));
213 // query is not specified if path contains wildcard or runrange = [-1,-1]
214 if (!query.IsSpecified()) {
215 AliError(Form("Unspecified query: %s",
216 query.ToString().Data()));
220 // This is needed otherwise TH1 objects (histos, TTree's) are lost when file is closed!
221 Bool_t oldStatus = TH1::AddDirectoryStatus();
222 TH1::AddDirectory(kFALSE);
224 AliCDBEntry* entry = GetEntry(query);
226 if (oldStatus != kFALSE)
227 TH1::AddDirectory(kTRUE);
230 // this is to make the SHUTTLE output lighter
231 if(!(query.GetPath().Contains("SHUTTLE/STATUS")))
232 AliDebug(2, Form("CDB object retrieved: %s", entry->GetId().ToString().Data()));
234 // this is to make the SHUTTLE output lighter
235 if(!(query.GetPath().Contains("SHUTTLE/STATUS")))
236 AliInfo(Form("No valid CDB object found! request was: %s", query.ToString().Data()));
239 // if drain storage is set, drain entry into drain storage
240 if(entry && (AliCDBManager::Instance())->IsDrainSet())
241 AliCDBManager::Instance()->Drain(entry);
246 //_____________________________________________________________________________
247 AliCDBEntry* AliCDBStorage::Get(const AliCDBPath& path, Int_t runNumber,
248 Int_t version, Int_t subVersion) {
249 // get an AliCDBEntry object from the database
251 return Get(AliCDBId(path, runNumber, runNumber, version, subVersion));
254 //_____________________________________________________________________________
255 AliCDBEntry* AliCDBStorage::Get(const AliCDBPath& path,
256 const AliCDBRunRange& runRange, Int_t version,
258 // get an AliCDBEntry object from the database
260 return Get(AliCDBId(path, runRange, version, subVersion));
263 //_____________________________________________________________________________
264 TList* AliCDBStorage::GetAll(const AliCDBId& query) {
265 // get multiple AliCDBEntry objects from the database
268 if (!query.IsValid()) {
269 AliError(Form("Invalid query: %s", query.ToString().Data()));
273 if (query.IsAnyRange()) {
274 AliError(Form("Unspecified run or runrange: %s",
275 query.ToString().Data()));
279 // This is needed otherwise TH1 objects (histos, TTree's) are lost when file is closed!
280 Bool_t oldStatus = TH1::AddDirectoryStatus();
281 TH1::AddDirectory(kFALSE);
283 TList *result = GetEntries(query);
285 if (oldStatus != kFALSE)
286 TH1::AddDirectory(kTRUE);
288 Int_t nEntries = result->GetEntries();
290 AliInfo(Form("%d objects retrieved. Request was: %s",
291 nEntries, query.ToString().Data()));
292 for(int i=0; i<nEntries;i++){
293 AliCDBEntry *entry = (AliCDBEntry*) result->At(i);
294 AliInfo(Form("%s",entry->GetId().ToString().Data()));
298 // if drain storage is set, drain entries into drain storage
299 if((AliCDBManager::Instance())->IsDrainSet()){
300 for(int i = 0; i<result->GetEntries(); i++){
301 AliCDBEntry* entry = (AliCDBEntry*) result->At(i);
302 AliCDBManager::Instance()->Drain(entry);
310 //_____________________________________________________________________________
311 TList* AliCDBStorage::GetAll(const AliCDBPath& path, Int_t runNumber,
312 Int_t version, Int_t subVersion) {
313 // get multiple AliCDBEntry objects from the database
315 return GetAll(AliCDBId(path, runNumber, runNumber, version,
319 //_____________________________________________________________________________
320 TList* AliCDBStorage::GetAll(const AliCDBPath& path,
321 const AliCDBRunRange& runRange, Int_t version, Int_t subVersion) {
322 // get multiple AliCDBEntry objects from the database
324 return GetAll(AliCDBId(path, runRange, version, subVersion));
328 //_____________________________________________________________________________
329 Bool_t AliCDBStorage::Put(TObject* object, AliCDBId& id, AliCDBMetaData* metaData, AliCDBManager::DataType type) {
330 // store an AliCDBEntry object into the database
332 AliCDBEntry anEntry(object, id, metaData);
334 return Put(&anEntry, type);
337 //_____________________________________________________________________________
338 Bool_t AliCDBStorage::Put(AliCDBEntry* entry, AliCDBManager::DataType type) {
339 // store an AliCDBEntry object into the database
342 AliError("No entry!");
346 if (!entry->GetId().IsValid()) {
347 AliError(Form("Invalid entry ID: %s",
348 entry->GetId().ToString().Data()));
352 if (!entry->GetId().IsSpecified()) {
353 AliError(Form("Unspecified entry ID: %s",
354 entry->GetId().ToString().Data()));
358 AliCDBManager::DataType expectedType = GetDataType();
360 if(expectedType != AliCDBManager::kPrivate && type != expectedType) {
361 AliError(Form("It is forbidden to store %s data into a folder of type %s!",
362 AliCDBManager::GetDataTypeName(type),
363 AliCDBManager::GetDataTypeName(expectedType)));
367 // set object's class name into metaData!
368 entry->GetMetaData()->SetObjectClassName(entry->GetObject()->ClassName());
370 return PutEntry(entry);
373 //_____________________________________________________________________________
374 void AliCDBStorage::QueryCDB(Int_t run, const char* pathFilter,
375 Int_t version, AliCDBMetaData* md){
376 // query CDB for files valid for given run, and fill list fValidFileIds
377 // Actual query is done in virtual function QueryValidFiles()
381 fPathFilter = pathFilter;
382 if(!fPathFilter.IsValid()) {
383 AliError(Form("Filter not valid: %s", pathFilter));
390 AliInfo(Form("Querying files valid for run %d and path \"%s\" into CDB storage \"%s://%s\"",
391 fRun, pathFilter, fType.Data(), fBaseFolder.Data()));
393 // Clear fValidFileIds list (it cannot be filled twice!
394 AliDebug(2, "Clearing list of CDB Id's previously loaded");
395 fValidFileIds.Clear();
397 if(fMetaDataFilter) {delete fMetaDataFilter; fMetaDataFilter=0;}
398 if(md) fMetaDataFilter = dynamic_cast<AliCDBMetaData*> (md->Clone());
401 AliCDBId queryId(pathFilter,run,run,version);
403 AliInfo(Form("%d valid files found!", fValidFileIds.GetEntriesFast()));
407 //_____________________________________________________________________________
408 void AliCDBStorage::PrintQueryCDB(){
409 // print parameters used to load list of CDB Id's (fRun, fPathFilter, fVersion)
411 AliCDBId paramId(fPathFilter, fRun, fRun, fVersion);
412 AliInfo(Form("**** QueryCDB Parameters **** \n\t<%s>\n",
413 paramId.ToString().Data()));
415 if(fMetaDataFilter) fMetaDataFilter->PrintMetaData();
418 TString message = "**** Id's of valid objects found *****\n";
419 TIter iter(&fValidFileIds);
422 // loop on the list of selection criteria
423 while ((anId = dynamic_cast<AliCDBId*>(iter.Next()))) {
424 message += Form("\t%s\n", anId->ToString().Data());
426 message += Form("\n\tTotal: %d objects found\n", fValidFileIds.GetEntriesFast());
427 AliInfo(Form("%s", message.Data()));
430 //_____________________________________________________________________________
431 AliCDBManager::DataType AliCDBStorage::GetDataType() const {
432 // returns the type of the data that should be stored into this storage:
433 // kConditions: conditions data; kReference: reference data; kPrivate: private (user-defined) data type
435 if(GetType() != "alien") return AliCDBManager::kPrivate;
437 TString condFolder = ((AliCDBGridParam*) AliCDBManager::Instance()->GetCondParam())->GetDBFolder();
438 TString refFolder = ((AliCDBGridParam*) AliCDBManager::Instance()->GetRefParam())->GetDBFolder();
440 if(GetBaseFolder().Contains(condFolder)) return AliCDBManager::kCondition;
441 if(GetBaseFolder().Contains(refFolder)) return AliCDBManager::kReference;
443 return AliCDBManager::kPrivate;
446 //_____________________________________________________________________________
447 void AliCDBStorage::LoadTreeFromFile(AliCDBEntry *entry) const {
448 // Checks whether entry contains a TTree and in case loads it into memory
450 AliCDBMetaData *md = dynamic_cast<AliCDBMetaData*> (entry->GetMetaData());
452 TString objStr = md->GetObjectClassName();
453 if(objStr != "TTree") return;
454 AliWarning("Entry contains a TTree! Loading baskets...");
456 TTree* tree = dynamic_cast<TTree*> (entry->GetObject());
461 tree->SetDirectory(0);
466 //_____________________________________________________________________________
467 void AliCDBStorage::SetTreeToFile(AliCDBEntry *entry, TFile* file) const {
468 // Checks whether entry contains a TTree and in case assigns it to memory
470 AliCDBMetaData *md = dynamic_cast<AliCDBMetaData*> (entry->GetMetaData());
472 TString objStr = md->GetObjectClassName();
473 if(objStr != "TTree") return;
474 AliWarning("Entry contains a TTree! Setting file...");
476 TTree* tree = dynamic_cast<TTree*> (entry->GetObject());
480 tree->SetDirectory(file);