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 **************************************************************************/
21 #include "AliCDBStorage.h"
22 #include "AliCDBGrid.h"
24 #include "AliCDBEntry.h"
27 ClassImp(AliCDBStorage)
29 //_____________________________________________________________________________
30 AliCDBStorage::AliCDBStorage():
43 fValidFileIds.SetOwner(1);
44 fSelections.SetOwner(1);
47 //_____________________________________________________________________________
48 AliCDBStorage::~AliCDBStorage() {
51 RemoveAllSelections();
52 fValidFileIds.Clear();
53 delete fMetaDataFilter;
57 //_____________________________________________________________________________
58 void AliCDBStorage::GetSelection(/*const*/ AliCDBId* id) {
59 // return required version and subversion from the list of selection criteria
61 TIter iter(&fSelections);
64 // loop on the list of selection criteria
65 while ((aSelection = (AliCDBId*) iter.Next())) {
66 // check if selection element contains id's path and run (range)
67 if (aSelection->Comprises(*id)) {
68 AliDebug(2,Form("Using selection criterion: %s ", aSelection->ToString().Data()));
69 // return required version and subversion
71 id->SetVersion(aSelection->GetVersion());
72 id->SetSubVersion(aSelection->GetSubVersion());
77 // no valid element is found in the list of selection criteria -> return
78 AliDebug(2,"Looking for objects with most recent version");
82 //_____________________________________________________________________________
83 void AliCDBStorage::ReadSelectionFromFile(const char *fileName){
84 // read selection criteria list from file
86 RemoveAllSelections();
88 TList *list = GetIdListFromFile(fileName);
92 Int_t nId = list->GetEntries();
96 for(int i=nId-1;i>=0;i--){
97 key = (TKey*) list->At(i);
98 id = (AliCDBId*) key->ReadObj();
99 if(id) AddSelection(*id);
102 AliInfo(Form("Selection criteria list filled with %d entries",fSelections.GetEntries()));
103 PrintSelectionList();
107 //_____________________________________________________________________________
108 void AliCDBStorage::AddSelection(const AliCDBId& selection) {
109 // add a selection criterion
111 AliCDBPath path = selection.GetPath();
112 if(!path.IsValid()) return;
114 TIter iter(&fSelections);
115 const AliCDBId *anId;
116 while((anId = (AliCDBId*) iter.Next())){
117 if(selection.Comprises(*anId)){
118 AliWarning("This selection is more general than a previous one and will hide it!");
119 AliWarning(Form("%s", (anId->ToString()).Data()));
120 fSelections.AddBefore(anId, new AliCDBId(selection));
125 fSelections.AddFirst(new AliCDBId(selection));
128 //_____________________________________________________________________________
129 void AliCDBStorage::AddSelection(const AliCDBPath& path,
130 const AliCDBRunRange& runRange, Int_t version, Int_t subVersion){
131 // add a selection criterion
133 AddSelection(AliCDBId(path, runRange, version, subVersion));
136 //_____________________________________________________________________________
137 void AliCDBStorage::AddSelection(const AliCDBPath& path,
138 Int_t firstRun, Int_t lastRun, Int_t version, Int_t subVersion){
139 // add a selection criterion
141 AddSelection(AliCDBId(path, firstRun, lastRun, version, subVersion));
144 //_____________________________________________________________________________
145 void AliCDBStorage::RemoveSelection(const AliCDBId& selection) {
146 // remove a selection criterion
148 TIter iter(&fSelections);
149 AliCDBId* aSelection;
151 while ((aSelection = (AliCDBId*) iter.Next())) {
152 if (selection.Comprises(*aSelection)) {
153 fSelections.Remove(aSelection);
158 //_____________________________________________________________________________
159 void AliCDBStorage::RemoveSelection(const AliCDBPath& path,
160 const AliCDBRunRange& runRange){
161 // remove a selection criterion
163 RemoveSelection(AliCDBId(path, runRange, -1, -1));
166 //_____________________________________________________________________________
167 void AliCDBStorage::RemoveSelection(const AliCDBPath& path,
168 Int_t firstRun, Int_t lastRun){
169 // remove a selection criterion
171 RemoveSelection(AliCDBId(path, firstRun, lastRun, -1, -1));
174 //_____________________________________________________________________________
175 void AliCDBStorage::RemoveSelection(int position){
176 // remove a selection criterion from its position in the list
178 delete fSelections.RemoveAt(position);
181 //_____________________________________________________________________________
182 void AliCDBStorage::RemoveAllSelections(){
183 // remove all selection criteria
188 //_____________________________________________________________________________
189 void AliCDBStorage::PrintSelectionList(){
190 // prints the list of selection criteria
192 TIter iter(&fSelections);
193 AliCDBId* aSelection;
195 // loop on the list of selection criteria
197 while ((aSelection = (AliCDBId*) iter.Next())) {
198 AliInfo(Form("index %d -> selection: %s",index++, aSelection->ToString().Data()));
203 //_____________________________________________________________________________
204 AliCDBEntry* AliCDBStorage::Get(const AliCDBId& query) {
205 // get an AliCDBEntry object from the database
207 // check if query's path and runRange are valid
208 // query is invalid also if version is not specified and subversion is!
209 if (!query.IsValid()) {
210 AliError(Form("Invalid query: %s", query.ToString().Data()));
214 // query is not specified if path contains wildcard or runrange = [-1,-1]
215 if (!query.IsSpecified()) {
216 AliError(Form("Unspecified query: %s",
217 query.ToString().Data()));
221 // This is needed otherwise TH1 objects (histos, TTree's) are lost when file is closed!
222 Bool_t oldStatus = TH1::AddDirectoryStatus();
223 TH1::AddDirectory(kFALSE);
225 AliCDBEntry* entry = GetEntry(query);
227 if (oldStatus != kFALSE)
228 TH1::AddDirectory(kTRUE);
231 // this is to make the SHUTTLE output lighter
232 if(!(query.GetPath().Contains("SHUTTLE/STATUS")))
233 AliDebug(2, Form("CDB object retrieved: %s", entry->GetId().ToString().Data()));
235 // this is to make the SHUTTLE output lighter
236 if(!(query.GetPath().Contains("SHUTTLE/STATUS")))
237 AliInfo(Form("No valid CDB object found! request was: %s", query.ToString().Data()));
240 // if drain storage is set, drain entry into drain storage
241 if(entry && (AliCDBManager::Instance())->IsDrainSet())
242 AliCDBManager::Instance()->Drain(entry);
247 //_____________________________________________________________________________
248 AliCDBEntry* AliCDBStorage::Get(const AliCDBPath& path, Int_t runNumber,
249 Int_t version, Int_t subVersion) {
250 // get an AliCDBEntry object from the database
252 return Get(AliCDBId(path, runNumber, runNumber, version, subVersion));
255 //_____________________________________________________________________________
256 AliCDBEntry* AliCDBStorage::Get(const AliCDBPath& path,
257 const AliCDBRunRange& runRange, Int_t version,
259 // get an AliCDBEntry object from the database
261 return Get(AliCDBId(path, runRange, version, subVersion));
264 //_____________________________________________________________________________
265 TList* AliCDBStorage::GetAll(const AliCDBId& query) {
266 // get multiple AliCDBEntry objects from the database
269 if (!query.IsValid()) {
270 AliError(Form("Invalid query: %s", query.ToString().Data()));
274 if (query.IsAnyRange()) {
275 AliError(Form("Unspecified run or runrange: %s",
276 query.ToString().Data()));
280 // This is needed otherwise TH1 objects (histos, TTree's) are lost when file is closed!
281 Bool_t oldStatus = TH1::AddDirectoryStatus();
282 TH1::AddDirectory(kFALSE);
284 TList *result = GetEntries(query);
286 if (oldStatus != kFALSE)
287 TH1::AddDirectory(kTRUE);
289 Int_t nEntries = result->GetEntries();
291 AliInfo(Form("%d objects retrieved. Request was: %s",
292 nEntries, query.ToString().Data()));
293 for(int i=0; i<nEntries;i++){
294 AliCDBEntry *entry = (AliCDBEntry*) result->At(i);
295 AliInfo(Form("%s",entry->GetId().ToString().Data()));
299 // if drain storage is set, drain entries into drain storage
300 if((AliCDBManager::Instance())->IsDrainSet()){
301 for(int i = 0; i<result->GetEntries(); i++){
302 AliCDBEntry* entry = (AliCDBEntry*) result->At(i);
303 AliCDBManager::Instance()->Drain(entry);
311 //_____________________________________________________________________________
312 TList* AliCDBStorage::GetAll(const AliCDBPath& path, Int_t runNumber,
313 Int_t version, Int_t subVersion) {
314 // get multiple AliCDBEntry objects from the database
316 return GetAll(AliCDBId(path, runNumber, runNumber, version,
320 //_____________________________________________________________________________
321 TList* AliCDBStorage::GetAll(const AliCDBPath& path,
322 const AliCDBRunRange& runRange, Int_t version, Int_t subVersion) {
323 // get multiple AliCDBEntry objects from the database
325 return GetAll(AliCDBId(path, runRange, version, subVersion));
328 //_____________________________________________________________________________
329 AliCDBId* AliCDBStorage::GetId(const AliCDBId& query) {
330 // get the Id of the valid object from the database (does not open the file)
332 // check if query's path and runRange are valid
333 // query is invalid also if version is not specified and subversion is!
334 if (!query.IsValid()) {
335 AliError(Form("Invalid query: %s", query.ToString().Data()));
339 // query is not specified if path contains wildcard or runrange = [-1,-1]
340 if (!query.IsSpecified()) {
341 AliError(Form("Unspecified query: %s",
342 query.ToString().Data()));
346 AliCDBId* id = GetEntryId(query);
351 //_____________________________________________________________________________
352 AliCDBId* AliCDBStorage::GetId(const AliCDBPath& path, Int_t runNumber,
353 Int_t version, Int_t subVersion) {
354 // get the Id of the valid object from the database (does not open the file)
356 return GetId(AliCDBId(path, runNumber, runNumber, version, subVersion));
359 //_____________________________________________________________________________
360 AliCDBId* AliCDBStorage::GetId(const AliCDBPath& path,
361 const AliCDBRunRange& runRange, Int_t version,
363 // get the Id of the valid object from the database (does not open the file)
365 return GetId(AliCDBId(path, runRange, version, subVersion));
368 //_____________________________________________________________________________
369 Bool_t AliCDBStorage::Put(TObject* object, AliCDBId& id, AliCDBMetaData* metaData, AliCDBManager::DataType type) {
370 // store an AliCDBEntry object into the database
373 AliError("Null Entry! No storage will be done!");
377 AliCDBEntry anEntry(object, id, metaData);
379 return Put(&anEntry, type);
382 //_____________________________________________________________________________
383 Bool_t AliCDBStorage::Put(AliCDBEntry* entry, AliCDBManager::DataType type) {
384 // store an AliCDBEntry object into the database
387 AliError("No entry!");
391 if (entry->GetObject()==0x0){
392 AliError("No valid object in CDB entry!");
396 if (!entry->GetId().IsValid()) {
397 AliError(Form("Invalid entry ID: %s",
398 entry->GetId().ToString().Data()));
402 if (!entry->GetId().IsSpecified()) {
403 AliError(Form("Unspecified entry ID: %s",
404 entry->GetId().ToString().Data()));
408 AliCDBManager::DataType expectedType = GetDataType();
410 if(expectedType != AliCDBManager::kPrivate && type != expectedType) {
411 AliError(Form("It is forbidden to store %s data into a folder of type %s!",
412 AliCDBManager::GetDataTypeName(type),
413 AliCDBManager::GetDataTypeName(expectedType)));
417 return PutEntry(entry);
420 //_____________________________________________________________________________
421 void AliCDBStorage::QueryCDB(Int_t run, const char* pathFilter,
422 Int_t version, AliCDBMetaData* md){
423 // query CDB for files valid for given run, and fill list fValidFileIds
424 // Actual query is done in virtual function QueryValidFiles()
428 fPathFilter = pathFilter;
429 if(!fPathFilter.IsValid()) {
430 AliError(Form("Filter not valid: %s", pathFilter));
437 AliInfo(Form("Querying files valid for run %d and path \"%s\" into CDB storage \"%s://%s\"",
438 fRun, pathFilter, fType.Data(), fBaseFolder.Data()));
440 // Clear fValidFileIds list (it cannot be filled twice!
441 AliDebug(2, "Clearing list of CDB Id's previously loaded");
442 fValidFileIds.Clear();
444 if(fMetaDataFilter) {delete fMetaDataFilter; fMetaDataFilter=0;}
445 if(md) fMetaDataFilter = dynamic_cast<AliCDBMetaData*> (md->Clone());
448 AliCDBId queryId(pathFilter,run,run,version);
450 AliInfo(Form("%d valid files found!", fValidFileIds.GetEntriesFast()));
454 //_____________________________________________________________________________
455 void AliCDBStorage::PrintQueryCDB(){
456 // print parameters used to load list of CDB Id's (fRun, fPathFilter, fVersion)
458 AliCDBId paramId(fPathFilter, fRun, fRun, fVersion);
459 AliInfo(Form("**** QueryCDB Parameters **** \n\t<%s>\n",
460 paramId.ToString().Data()));
462 if(fMetaDataFilter) fMetaDataFilter->PrintMetaData();
465 TString message = "**** Id's of valid objects found *****\n";
466 TIter iter(&fValidFileIds);
469 // loop on the list of selection criteria
470 while ((anId = dynamic_cast<AliCDBId*>(iter.Next()))) {
471 message += Form("\t%s\n", anId->ToString().Data());
473 message += Form("\n\tTotal: %d objects found\n", fValidFileIds.GetEntriesFast());
474 AliInfo(Form("%s", message.Data()));
477 //_____________________________________________________________________________
478 AliCDBManager::DataType AliCDBStorage::GetDataType() const {
479 // returns the type of the data that should be stored into this storage:
480 // kConditions: conditions data; kReference: reference data; kPrivate: private (user-defined) data type
482 if(GetType() != "alien") return AliCDBManager::kPrivate;
484 TString condFolder = ((AliCDBGridParam*) AliCDBManager::Instance()->GetCondParam())->GetDBFolder();
485 TString refFolder = ((AliCDBGridParam*) AliCDBManager::Instance()->GetRefParam())->GetDBFolder();
487 if(GetBaseFolder().Contains(condFolder)) return AliCDBManager::kCondition;
488 if(GetBaseFolder().Contains(refFolder)) return AliCDBManager::kReference;
490 return AliCDBManager::kPrivate;
493 //_____________________________________________________________________________
494 void AliCDBStorage::LoadTreeFromFile(AliCDBEntry *entry) const {
495 // Checks whether entry contains a TTree and in case loads it into memory
497 TObject *obj = (TObject*) entry->GetObject();
499 AliError("Cannot retrieve the object:");
500 entry->PrintMetaData();
504 if (!strcmp(obj->ClassName(),TTree::Class_Name())) {
506 AliWarning("Entry contains a TTree! Loading baskets...");
508 TTree* tree = dynamic_cast<TTree*> (obj);
513 tree->SetDirectory(0);
515 else if (!strcmp(obj->ClassName(),TNtuple::Class_Name())){
517 AliWarning("Entry contains a TNtuple! Loading baskets...");
519 TNtuple* ntu = dynamic_cast<TNtuple*> (obj);
524 ntu->SetDirectory(0);
530 // //_____________________________________________________________________________
531 // void AliCDBStorage::SetTreeToFile(AliCDBEntry *entry, TFile* file) const {
532 // // Checks whether entry contains a TTree and in case assigns it to memory
534 // AliCDBMetaData *md = dynamic_cast<AliCDBMetaData*> (entry->GetMetaData());
536 // TString objStr = md->GetObjectClassName();
537 // if(objStr != "TTree") return;
538 // AliWarning("Entry contains a TTree! Setting file...");
540 // TTree* tree = dynamic_cast<TTree*> (entry->GetObject());
544 // // tree->SetDirectory(file);
545 // tree->SetDirectory(0);