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():
45 fValidFileIds.SetOwner(1);
46 fSelections.SetOwner(1);
49 //_____________________________________________________________________________
50 AliCDBStorage::~AliCDBStorage() {
53 RemoveAllSelections();
54 fValidFileIds.Clear();
55 delete fMetaDataFilter;
59 //_____________________________________________________________________________
60 void AliCDBStorage::GetSelection(/*const*/ AliCDBId* id) {
61 // return required version and subversion from the list of selection criteria
63 TIter iter(&fSelections);
66 // loop on the list of selection criteria
67 while ((aSelection = (AliCDBId*) iter.Next())) {
68 // check if selection element contains id's path and run (range)
69 if (aSelection->Comprises(*id)) {
70 AliDebug(2,Form("Using selection criterion: %s ", aSelection->ToString().Data()));
71 // return required version and subversion
73 id->SetVersion(aSelection->GetVersion());
74 id->SetSubVersion(aSelection->GetSubVersion());
79 // no valid element is found in the list of selection criteria -> return
80 AliDebug(2,"Looking for objects with most recent version");
84 //_____________________________________________________________________________
85 void AliCDBStorage::ReadSelectionFromFile(const char *fileName){
86 // read selection criteria list from file
88 RemoveAllSelections();
90 TList *list = GetIdListFromFile(fileName);
94 Int_t nId = list->GetEntries();
98 for(int i=nId-1;i>=0;i--){
99 key = (TKey*) list->At(i);
100 id = (AliCDBId*) key->ReadObj();
101 if(id) AddSelection(*id);
104 AliInfo(Form("Selection criteria list filled with %d entries",fSelections.GetEntries()));
105 PrintSelectionList();
109 //_____________________________________________________________________________
110 void AliCDBStorage::AddSelection(const AliCDBId& selection) {
111 // add a selection criterion
113 AliCDBPath path = selection.GetPath();
114 if(!path.IsValid()) return;
116 TIter iter(&fSelections);
117 const AliCDBId *anId;
118 while((anId = (AliCDBId*) iter.Next())){
119 if(selection.Comprises(*anId)){
120 AliWarning("This selection is more general than a previous one and will hide it!");
121 AliWarning(Form("%s", (anId->ToString()).Data()));
122 fSelections.AddBefore(anId, new AliCDBId(selection));
127 fSelections.AddFirst(new AliCDBId(selection));
130 //_____________________________________________________________________________
131 void AliCDBStorage::AddSelection(const AliCDBPath& path,
132 const AliCDBRunRange& runRange, Int_t version, Int_t subVersion){
133 // add a selection criterion
135 AddSelection(AliCDBId(path, runRange, version, subVersion));
138 //_____________________________________________________________________________
139 void AliCDBStorage::AddSelection(const AliCDBPath& path,
140 Int_t firstRun, Int_t lastRun, Int_t version, Int_t subVersion){
141 // add a selection criterion
143 AddSelection(AliCDBId(path, firstRun, lastRun, version, subVersion));
146 //_____________________________________________________________________________
147 void AliCDBStorage::RemoveSelection(const AliCDBId& selection) {
148 // remove a selection criterion
150 TIter iter(&fSelections);
151 AliCDBId* aSelection;
153 while ((aSelection = (AliCDBId*) iter.Next())) {
154 if (selection.Comprises(*aSelection)) {
155 fSelections.Remove(aSelection);
160 //_____________________________________________________________________________
161 void AliCDBStorage::RemoveSelection(const AliCDBPath& path,
162 const AliCDBRunRange& runRange){
163 // remove a selection criterion
165 RemoveSelection(AliCDBId(path, runRange, -1, -1));
168 //_____________________________________________________________________________
169 void AliCDBStorage::RemoveSelection(const AliCDBPath& path,
170 Int_t firstRun, Int_t lastRun){
171 // remove a selection criterion
173 RemoveSelection(AliCDBId(path, firstRun, lastRun, -1, -1));
176 //_____________________________________________________________________________
177 void AliCDBStorage::RemoveSelection(int position){
178 // remove a selection criterion from its position in the list
180 delete fSelections.RemoveAt(position);
183 //_____________________________________________________________________________
184 void AliCDBStorage::RemoveAllSelections(){
185 // remove all selection criteria
190 //_____________________________________________________________________________
191 void AliCDBStorage::PrintSelectionList(){
192 // prints the list of selection criteria
194 TIter iter(&fSelections);
195 AliCDBId* aSelection;
197 // loop on the list of selection criteria
199 while ((aSelection = (AliCDBId*) iter.Next())) {
200 AliInfo(Form("index %d -> selection: %s",index++, aSelection->ToString().Data()));
205 //_____________________________________________________________________________
206 AliCDBEntry* AliCDBStorage::Get(const AliCDBId& query) {
207 // get an AliCDBEntry object from the database
209 // check if query's path and runRange are valid
210 // query is invalid also if version is not specified and subversion is!
211 if (!query.IsValid()) {
212 AliError(Form("Invalid query: %s", query.ToString().Data()));
216 // query is not specified if path contains wildcard or runrange = [-1,-1]
217 if (!query.IsSpecified()) {
218 AliError(Form("Unspecified query: %s",
219 query.ToString().Data()));
223 // This is needed otherwise TH1 objects (histos, TTree's) are lost when file is closed!
224 Bool_t oldStatus = TH1::AddDirectoryStatus();
225 TH1::AddDirectory(kFALSE);
227 AliCDBEntry* entry = GetEntry(query);
229 if (oldStatus != kFALSE)
230 TH1::AddDirectory(kTRUE);
233 // this is to make the SHUTTLE output lighter
234 if(!(query.GetPath().Contains("SHUTTLE/STATUS")))
235 AliDebug(2, Form("CDB object retrieved: %s", entry->GetId().ToString().Data()));
237 // this is to make the SHUTTLE output lighter
238 if(!(query.GetPath().Contains("SHUTTLE/STATUS"))){
240 if(!(query.GetPath().Contains("SHUTTLE"))){
241 AliFatal(Form("No valid CDB object found! request was: %s", query.ToString().Data()));
244 AliInfo(Form("No valid CDB object found! request was: %s", query.ToString().Data()));
249 // if drain storage is set, drain entry into drain storage
250 if(entry && (AliCDBManager::Instance())->IsDrainSet())
251 AliCDBManager::Instance()->Drain(entry);
256 //_____________________________________________________________________________
257 AliCDBEntry* AliCDBStorage::Get(const AliCDBPath& path, Int_t runNumber,
258 Int_t version, Int_t subVersion) {
259 // get an AliCDBEntry object from the database
261 return Get(AliCDBId(path, runNumber, runNumber, version, subVersion));
264 //_____________________________________________________________________________
265 AliCDBEntry* AliCDBStorage::Get(const AliCDBPath& path,
266 const AliCDBRunRange& runRange, Int_t version,
268 // get an AliCDBEntry object from the database
270 return Get(AliCDBId(path, runRange, version, subVersion));
273 //_____________________________________________________________________________
274 TList* AliCDBStorage::GetAll(const AliCDBId& query) {
275 // get multiple AliCDBEntry objects from the database
278 if (!query.IsValid()) {
279 AliError(Form("Invalid query: %s", query.ToString().Data()));
283 if (query.IsAnyRange()) {
284 AliError(Form("Unspecified run or runrange: %s",
285 query.ToString().Data()));
289 // This is needed otherwise TH1 objects (histos, TTree's) are lost when file is closed!
290 Bool_t oldStatus = TH1::AddDirectoryStatus();
291 TH1::AddDirectory(kFALSE);
293 TList *result = GetEntries(query);
295 if (oldStatus != kFALSE)
296 TH1::AddDirectory(kTRUE);
298 Int_t nEntries = result->GetEntries();
300 AliInfo(Form("%d objects retrieved. Request was: %s",
301 nEntries, query.ToString().Data()));
302 for(int i=0; i<nEntries;i++){
303 AliCDBEntry *entry = (AliCDBEntry*) result->At(i);
304 AliInfo(Form("%s",entry->GetId().ToString().Data()));
308 // if drain storage is set, drain entries into drain storage
309 if((AliCDBManager::Instance())->IsDrainSet()){
310 for(int i = 0; i<result->GetEntries(); i++){
311 AliCDBEntry* entry = (AliCDBEntry*) result->At(i);
312 AliCDBManager::Instance()->Drain(entry);
320 //_____________________________________________________________________________
321 TList* AliCDBStorage::GetAll(const AliCDBPath& path, Int_t runNumber,
322 Int_t version, Int_t subVersion) {
323 // get multiple AliCDBEntry objects from the database
325 return GetAll(AliCDBId(path, runNumber, runNumber, version,
329 //_____________________________________________________________________________
330 TList* AliCDBStorage::GetAll(const AliCDBPath& path,
331 const AliCDBRunRange& runRange, Int_t version, Int_t subVersion) {
332 // get multiple AliCDBEntry objects from the database
334 return GetAll(AliCDBId(path, runRange, version, subVersion));
337 //_____________________________________________________________________________
338 AliCDBId* AliCDBStorage::GetId(const AliCDBId& query) {
339 // get the Id of the valid object from the database (does not open the file)
341 // check if query's path and runRange are valid
342 // query is invalid also if version is not specified and subversion is!
343 if (!query.IsValid()) {
344 AliError(Form("Invalid query: %s", query.ToString().Data()));
348 // query is not specified if path contains wildcard or runrange = [-1,-1]
349 if (!query.IsSpecified()) {
350 AliError(Form("Unspecified query: %s",
351 query.ToString().Data()));
355 AliCDBId* id = GetEntryId(query);
360 //_____________________________________________________________________________
361 AliCDBId* AliCDBStorage::GetId(const AliCDBPath& path, Int_t runNumber,
362 Int_t version, Int_t subVersion) {
363 // get the Id of the valid object from the database (does not open the file)
365 return GetId(AliCDBId(path, runNumber, runNumber, version, subVersion));
368 //_____________________________________________________________________________
369 AliCDBId* AliCDBStorage::GetId(const AliCDBPath& path,
370 const AliCDBRunRange& runRange, Int_t version,
372 // get the Id of the valid object from the database (does not open the file)
374 return GetId(AliCDBId(path, runRange, version, subVersion));
377 //_____________________________________________________________________________
378 Bool_t AliCDBStorage::Put(TObject* object, AliCDBId& id, AliCDBMetaData* metaData, const char* mirrors, AliCDBManager::DataType type) {
379 // store an AliCDBEntry object into the database
382 AliError("Null Entry! No storage will be done!");
386 AliCDBEntry anEntry(object, id, metaData);
388 return Put(&anEntry, mirrors, type);
391 //_____________________________________________________________________________
392 Bool_t AliCDBStorage::Put(AliCDBEntry* entry, const char* mirrors, AliCDBManager::DataType type) {
393 // store an AliCDBEntry object into the database
396 AliError("No entry!");
400 if (entry->GetObject()==0x0){
401 AliError("No valid object in CDB entry!");
405 if (!entry->GetId().IsValid()) {
406 AliError(Form("Invalid entry ID: %s",
407 entry->GetId().ToString().Data()));
411 if (!entry->GetId().IsSpecified()) {
412 AliError(Form("Unspecified entry ID: %s",
413 entry->GetId().ToString().Data()));
417 AliCDBManager::DataType expectedType = GetDataType();
419 if(expectedType != AliCDBManager::kPrivate && type != expectedType) {
420 AliError(Form("It is forbidden to store %s data into a folder of type %s!",
421 AliCDBManager::GetDataTypeName(type),
422 AliCDBManager::GetDataTypeName(expectedType)));
427 TString strMirrors(mirrors);
428 if(!strMirrors.IsNull() && !strMirrors.IsWhitespace())
429 return PutEntry(entry, mirrors);
431 return PutEntry(entry);
434 //_____________________________________________________________________________
435 void AliCDBStorage::QueryCDB(Int_t run, const char* pathFilter,
436 Int_t version, AliCDBMetaData* md){
437 // query CDB for files valid for given run, and fill list fValidFileIds
438 // Actual query is done in virtual function QueryValidFiles()
442 fPathFilter = pathFilter;
443 if(!fPathFilter.IsValid()) {
444 AliError(Form("Filter not valid: %s", pathFilter));
451 AliInfo(Form("Querying files valid for run %d and path \"%s\" into CDB storage \"%s://%s\"",
452 fRun, pathFilter, fType.Data(), fBaseFolder.Data()));
454 // Clear fValidFileIds list (it cannot be filled twice!
455 AliDebug(2, "Clearing list of CDB Id's previously loaded");
456 fValidFileIds.Clear();
458 if(fMetaDataFilter) {delete fMetaDataFilter; fMetaDataFilter=0;}
459 if(md) fMetaDataFilter = dynamic_cast<AliCDBMetaData*> (md->Clone());
463 AliInfo(Form("%d valid files found!", fValidFileIds.GetEntriesFast()));
467 //_____________________________________________________________________________
468 void AliCDBStorage::PrintQueryCDB(){
469 // print parameters used to load list of CDB Id's (fRun, fPathFilter, fVersion)
471 AliCDBId paramId(fPathFilter, fRun, fRun, fVersion);
472 AliInfo(Form("**** QueryCDB Parameters **** \n\t<%s>\n",
473 paramId.ToString().Data()));
475 if(fMetaDataFilter) fMetaDataFilter->PrintMetaData();
478 TString message = "**** Id's of valid objects found *****\n";
479 TIter iter(&fValidFileIds);
482 // loop on the list of selection criteria
483 while ((anId = dynamic_cast<AliCDBId*>(iter.Next()))) {
484 message += Form("\t%s\n", anId->ToString().Data());
486 message += Form("\n\tTotal: %d objects found\n", fValidFileIds.GetEntriesFast());
487 AliInfo(Form("%s", message.Data()));
490 //_____________________________________________________________________________
491 AliCDBManager::DataType AliCDBStorage::GetDataType() const {
492 // returns the type of the data that should be stored into this storage:
493 // kConditions: conditions data; kReference: reference data; kPrivate: private (user-defined) data type
495 if(GetType() != "alien") return AliCDBManager::kPrivate;
497 TString condFolder = ((AliCDBGridParam*) AliCDBManager::Instance()->GetCondParam())->GetDBFolder();
498 TString refFolder = ((AliCDBGridParam*) AliCDBManager::Instance()->GetRefParam())->GetDBFolder();
500 if(GetBaseFolder().Contains(condFolder)) return AliCDBManager::kCondition;
501 if(GetBaseFolder().Contains(refFolder)) return AliCDBManager::kReference;
503 return AliCDBManager::kPrivate;
506 //_____________________________________________________________________________
507 void AliCDBStorage::SetMirrorSEs(const char* mirrors) {
508 // if the current storage is not of "alien" type, just issue a warning
509 // AliCDBGrid implements its own SetMirrorSEs method, classes for other storage types do not
511 TString storageType = GetType();
512 if(storageType != "alien"){
513 AliWarning(Form("The current storage is of type \"%s\". Setting of SEs to \"%s\" skipped!",storageType.Data(),mirrors));
516 AliError("We should never get here!! AliCDBGrid must have masked this virtual method!");
520 //_____________________________________________________________________________
521 const char* AliCDBStorage::GetMirrorSEs() const {
522 // if the current storage is not of "alien" type, just issue a warning
523 // AliCDBGrid implements its own GetMirrorSEs method, classes for other storage types do not
525 TString storageType = GetType();
526 if(storageType != "alien"){
527 AliWarning(Form("The current storage is of type \"%s\" and cannot handle SEs. Returning empty string!",storageType.Data()));
530 AliError("We should never get here!! AliCDBGrid must have masked this virtual method!");
534 //_____________________________________________________________________________
535 void AliCDBStorage::LoadTreeFromFile(AliCDBEntry *entry) const {
536 // Checks whether entry contains a TTree and in case loads it into memory
538 TObject *obj = (TObject*) entry->GetObject();
540 AliError("Cannot retrieve the object:");
541 entry->PrintMetaData();
545 if (!strcmp(obj->ClassName(),TTree::Class_Name())) {
547 AliWarning("Entry contains a TTree! Loading baskets...");
549 TTree* tree = dynamic_cast<TTree*> (obj);
554 tree->SetDirectory(0);
556 else if (!strcmp(obj->ClassName(),TNtuple::Class_Name())){
558 AliWarning("Entry contains a TNtuple! Loading baskets...");
560 TNtuple* ntu = dynamic_cast<TNtuple*> (obj);
565 ntu->SetDirectory(0);
571 // //_____________________________________________________________________________
572 // void AliCDBStorage::SetTreeToFile(AliCDBEntry *entry, TFile* file) const {
573 // // Checks whether entry contains a TTree and in case assigns it to memory
575 // AliCDBMetaData *md = dynamic_cast<AliCDBMetaData*> (entry->GetMetaData());
577 // TString objStr = md->GetObjectClassName();
578 // if(objStr != "TTree") return;
579 // AliWarning("Entry contains a TTree! Setting file...");
581 // TTree* tree = dynamic_cast<TTree*> (entry->GetObject());
585 // // tree->SetDirectory(file);
586 // tree->SetDirectory(0);