Changes related to the removal of the deprecated AliCDBGrid::GetLatestVersion:
[u/mrichter/AliRoot.git] / STEER / CDB / AliCDBStorage.cxx
CommitLineData
2c8628dd 1/**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3 * *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
6 * *
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 **************************************************************************/
15
b05400be 16#include <TKey.h>
02c4845e 17#include <TH1.h>
145f45a2 18#include <TTree.h>
ed343d5e 19#include <TNtuple.h>
145f45a2 20#include <TFile.h>
fe913d8f 21#include "AliCDBStorage.h"
b8ec52f6 22#include "AliCDBGrid.h"
2c8628dd 23
9e1ceb13 24#include "AliCDBEntry.h"
25#include "AliLog.h"
2c8628dd 26
fe913d8f 27ClassImp(AliCDBStorage)
2c8628dd 28
9e1ceb13 29//_____________________________________________________________________________
62032124 30AliCDBStorage::AliCDBStorage():
31fValidFileIds(),
32fRun(-1),
33fPathFilter(),
34fVersion(-1),
35fMetaDataFilter(0),
36fSelections(),
37fURI(),
38fType(),
89004e35 39fBaseFolder(),
40fNretry(0),
5bfe3965 41fInitRetrySeconds(0)
62032124 42{
9e1ceb13 43// constructor
44
62032124 45 fValidFileIds.SetOwner(1);
9e1ceb13 46 fSelections.SetOwner(1);
47}
2c8628dd 48
9e1ceb13 49//_____________________________________________________________________________
50AliCDBStorage::~AliCDBStorage() {
51// destructor
2c8628dd 52
62032124 53 RemoveAllSelections();
54 fValidFileIds.Clear();
55 delete fMetaDataFilter;
56
9e1ceb13 57}
2c8628dd 58
59//_____________________________________________________________________________
4b5e0dce 60void AliCDBStorage::GetSelection(/*const*/ AliCDBId* id) {
0f678e35 61// return required version and subversion from the list of selection criteria
9e1ceb13 62
63 TIter iter(&fSelections);
64 AliCDBId* aSelection;
65
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)
4b5e0dce 69 if (aSelection->Comprises(*id)) {
b05400be 70 AliDebug(2,Form("Using selection criterion: %s ", aSelection->ToString().Data()));
9e1ceb13 71 // return required version and subversion
4b5e0dce 72
73 id->SetVersion(aSelection->GetVersion());
74 id->SetSubVersion(aSelection->GetSubVersion());
75 return;
9e1ceb13 76 }
77 }
78
79 // no valid element is found in the list of selection criteria -> return
b05400be 80 AliDebug(2,"Looking for objects with most recent version");
4b5e0dce 81 return;
2c8628dd 82}
83
b05400be 84//_____________________________________________________________________________
85void AliCDBStorage::ReadSelectionFromFile(const char *fileName){
86// read selection criteria list from file
87
88 RemoveAllSelections();
89
90 TList *list = GetIdListFromFile(fileName);
91 if(!list) return;
92
93 list->SetOwner();
94 Int_t nId = list->GetEntries();
95 AliCDBId *id;
96 TKey *key;
97
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);
102 }
103 delete list;
104 AliInfo(Form("Selection criteria list filled with %d entries",fSelections.GetEntries()));
105 PrintSelectionList();
106
107}
108
2c8628dd 109//_____________________________________________________________________________
9e1ceb13 110void AliCDBStorage::AddSelection(const AliCDBId& selection) {
111// add a selection criterion
112
113 AliCDBPath path = selection.GetPath();
114 if(!path.IsValid()) return;
115
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));
123 return;
124 }
125
126 }
127 fSelections.AddFirst(new AliCDBId(selection));
128}
2c8628dd 129
130//_____________________________________________________________________________
62032124 131void AliCDBStorage::AddSelection(const AliCDBPath& path,
9e1ceb13 132 const AliCDBRunRange& runRange, Int_t version, Int_t subVersion){
133// add a selection criterion
2c8628dd 134
9e1ceb13 135 AddSelection(AliCDBId(path, runRange, version, subVersion));
2c8628dd 136}
137
9e1ceb13 138//_____________________________________________________________________________
139void AliCDBStorage::AddSelection(const AliCDBPath& path,
140 Int_t firstRun, Int_t lastRun, Int_t version, Int_t subVersion){
141// add a selection criterion
142
143 AddSelection(AliCDBId(path, firstRun, lastRun, version, subVersion));
144}
2c8628dd 145
9e1ceb13 146//_____________________________________________________________________________
147void AliCDBStorage::RemoveSelection(const AliCDBId& selection) {
148// remove a selection criterion
2c8628dd 149
9e1ceb13 150 TIter iter(&fSelections);
151 AliCDBId* aSelection;
2c8628dd 152
9e1ceb13 153 while ((aSelection = (AliCDBId*) iter.Next())) {
154 if (selection.Comprises(*aSelection)) {
155 fSelections.Remove(aSelection);
156 }
157 }
158}
2c8628dd 159
9e1ceb13 160//_____________________________________________________________________________
161void AliCDBStorage::RemoveSelection(const AliCDBPath& path,
162 const AliCDBRunRange& runRange){
163// remove a selection criterion
2c8628dd 164
9e1ceb13 165 RemoveSelection(AliCDBId(path, runRange, -1, -1));
166}
f05209ee 167
9e1ceb13 168//_____________________________________________________________________________
169void AliCDBStorage::RemoveSelection(const AliCDBPath& path,
170 Int_t firstRun, Int_t lastRun){
171// remove a selection criterion
f05209ee 172
9e1ceb13 173 RemoveSelection(AliCDBId(path, firstRun, lastRun, -1, -1));
174}
f05209ee 175
9e1ceb13 176//_____________________________________________________________________________
eb0b1051 177void AliCDBStorage::RemoveSelection(int position){
9e1ceb13 178// remove a selection criterion from its position in the list
2c8628dd 179
62032124 180 delete fSelections.RemoveAt(position);
2c8628dd 181}
182
2c8628dd 183//_____________________________________________________________________________
9e1ceb13 184void AliCDBStorage::RemoveAllSelections(){
185// remove all selection criteria
2c8628dd 186
62032124 187 fSelections.Clear();
9e1ceb13 188}
fe913d8f 189
9e1ceb13 190//_____________________________________________________________________________
191void AliCDBStorage::PrintSelectionList(){
192// prints the list of selection criteria
193
194 TIter iter(&fSelections);
195 AliCDBId* aSelection;
62032124 196
9e1ceb13 197 // loop on the list of selection criteria
198 int index=0;
199 while ((aSelection = (AliCDBId*) iter.Next())) {
200 AliInfo(Form("index %d -> selection: %s",index++, aSelection->ToString().Data()));
201 }
fe913d8f 202
9e1ceb13 203}
fe913d8f 204
9e1ceb13 205//_____________________________________________________________________________
0f678e35 206AliCDBEntry* AliCDBStorage::Get(const AliCDBId& query) {
9e1ceb13 207// get an AliCDBEntry object from the database
4667c116 208
9e1ceb13 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()));
213 return NULL;
214 }
215
4667c116 216 // query is not specified if path contains wildcard or runrange = [-1,-1]
9e1ceb13 217 if (!query.IsSpecified()) {
4667c116 218 AliError(Form("Unspecified query: %s",
9e1ceb13 219 query.ToString().Data()));
220 return NULL;
221 }
62032124 222
02c4845e 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);
9e1ceb13 226
227 AliCDBEntry* entry = GetEntry(query);
02c4845e 228
229 if (oldStatus != kFALSE)
230 TH1::AddDirectory(kTRUE);
0f678e35 231
9e1ceb13 232 // if drain storage is set, drain entry into drain storage
233 if(entry && (AliCDBManager::Instance())->IsDrainSet())
234 AliCDBManager::Instance()->Drain(entry);
0f678e35 235
9e1ceb13 236 return entry;
2c8628dd 237}
238
239//_____________________________________________________________________________
4667c116 240AliCDBEntry* AliCDBStorage::Get(const AliCDBPath& path, Int_t runNumber,
9e1ceb13 241 Int_t version, Int_t subVersion) {
242// get an AliCDBEntry object from the database
2c8628dd 243
9e1ceb13 244 return Get(AliCDBId(path, runNumber, runNumber, version, subVersion));
2c8628dd 245}
246
2c8628dd 247//_____________________________________________________________________________
4667c116 248AliCDBEntry* AliCDBStorage::Get(const AliCDBPath& path,
9e1ceb13 249 const AliCDBRunRange& runRange, Int_t version,
250 Int_t subVersion) {
251// get an AliCDBEntry object from the database
2c8628dd 252
9e1ceb13 253 return Get(AliCDBId(path, runRange, version, subVersion));
2c8628dd 254}
255
2c8628dd 256//_____________________________________________________________________________
9e1ceb13 257TList* AliCDBStorage::GetAll(const AliCDBId& query) {
258// get multiple AliCDBEntry objects from the database
259
260
261 if (!query.IsValid()) {
262 AliError(Form("Invalid query: %s", query.ToString().Data()));
263 return NULL;
264 }
265
266 if (query.IsAnyRange()) {
267 AliError(Form("Unspecified run or runrange: %s",
268 query.ToString().Data()));
269 return NULL;
270 }
271
02c4845e 272 // This is needed otherwise TH1 objects (histos, TTree's) are lost when file is closed!
273 Bool_t oldStatus = TH1::AddDirectoryStatus();
274 TH1::AddDirectory(kFALSE);
275
9e1ceb13 276 TList *result = GetEntries(query);
277
02c4845e 278 if (oldStatus != kFALSE)
279 TH1::AddDirectory(kTRUE);
280
9e1ceb13 281 Int_t nEntries = result->GetEntries();
b8ec52f6 282
283 AliInfo(Form("%d objects retrieved. Request was: %s",
284 nEntries, query.ToString().Data()));
285 for(int i=0; i<nEntries;i++){
286 AliCDBEntry *entry = (AliCDBEntry*) result->At(i);
287 AliInfo(Form("%s",entry->GetId().ToString().Data()));
9e1ceb13 288 }
289
b8ec52f6 290
9e1ceb13 291 // if drain storage is set, drain entries into drain storage
292 if((AliCDBManager::Instance())->IsDrainSet()){
293 for(int i = 0; i<result->GetEntries(); i++){
294 AliCDBEntry* entry = (AliCDBEntry*) result->At(i);
295 AliCDBManager::Instance()->Drain(entry);
296 }
297 }
298
299
300 return result;
301}
2c8628dd 302
9e1ceb13 303//_____________________________________________________________________________
304TList* AliCDBStorage::GetAll(const AliCDBPath& path, Int_t runNumber,
305 Int_t version, Int_t subVersion) {
306// get multiple AliCDBEntry objects from the database
2c8628dd 307
9e1ceb13 308 return GetAll(AliCDBId(path, runNumber, runNumber, version,
309 subVersion));
2c8628dd 310}
311
2c8628dd 312//_____________________________________________________________________________
9e1ceb13 313TList* AliCDBStorage::GetAll(const AliCDBPath& path,
314 const AliCDBRunRange& runRange, Int_t version, Int_t subVersion) {
315// get multiple AliCDBEntry objects from the database
2c8628dd 316
9e1ceb13 317 return GetAll(AliCDBId(path, runRange, version, subVersion));
2c8628dd 318}
f05209ee 319
4667c116 320//_____________________________________________________________________________
321AliCDBId* AliCDBStorage::GetId(const AliCDBId& query) {
322// get the Id of the valid object from the database (does not open the file)
323
324 // check if query's path and runRange are valid
325 // query is invalid also if version is not specified and subversion is!
326 if (!query.IsValid()) {
327 AliError(Form("Invalid query: %s", query.ToString().Data()));
328 return NULL;
329 }
330
331 // query is not specified if path contains wildcard or runrange = [-1,-1]
332 if (!query.IsSpecified()) {
333 AliError(Form("Unspecified query: %s",
334 query.ToString().Data()));
335 return NULL;
336 }
337
338 AliCDBId* id = GetEntryId(query);
339
340 return id;
341}
342
343//_____________________________________________________________________________
344AliCDBId* AliCDBStorage::GetId(const AliCDBPath& path, Int_t runNumber,
345 Int_t version, Int_t subVersion) {
346// get the Id of the valid object from the database (does not open the file)
347
348 return GetId(AliCDBId(path, runNumber, runNumber, version, subVersion));
349}
350
351//_____________________________________________________________________________
352AliCDBId* AliCDBStorage::GetId(const AliCDBPath& path,
353 const AliCDBRunRange& runRange, Int_t version,
354 Int_t subVersion) {
355// get the Id of the valid object from the database (does not open the file)
356
357 return GetId(AliCDBId(path, runRange, version, subVersion));
358}
f05209ee 359
360//_____________________________________________________________________________
c5941d67 361Bool_t AliCDBStorage::Put(TObject* object, AliCDBId& id, AliCDBMetaData* metaData, const char* mirrors, AliCDBManager::DataType type) {
02c4845e 362// store an AliCDBEntry object into the database
9e1ceb13 363
968cfa08 364 if (object==0x0) {
365 AliError("Null Entry! No storage will be done!");
366 return kFALSE;
367 }
368
9e1ceb13 369 AliCDBEntry anEntry(object, id, metaData);
f05209ee 370
c5941d67 371 return Put(&anEntry, mirrors, type);
b8ec52f6 372}
f05209ee 373
9e1ceb13 374//_____________________________________________________________________________
06eeadea 375Bool_t AliCDBStorage::Put(AliCDBEntry* entry, const char* mirrors, AliCDBManager::DataType type) {
02c4845e 376// store an AliCDBEntry object into the database
9e1ceb13 377
02c4845e 378 if (!entry){
379 AliError("No entry!");
380 return kFALSE;
381 }
382
968cfa08 383 if (entry->GetObject()==0x0){
384 AliError("No valid object in CDB entry!");
385 return kFALSE;
386 }
387
9e1ceb13 388 if (!entry->GetId().IsValid()) {
62032124 389 AliError(Form("Invalid entry ID: %s",
9e1ceb13 390 entry->GetId().ToString().Data()));
391 return kFALSE;
392 }
393
394 if (!entry->GetId().IsSpecified()) {
62032124 395 AliError(Form("Unspecified entry ID: %s",
9e1ceb13 396 entry->GetId().ToString().Data()));
397 return kFALSE;
398 }
399
b8ec52f6 400 AliCDBManager::DataType expectedType = GetDataType();
401
402 if(expectedType != AliCDBManager::kPrivate && type != expectedType) {
403 AliError(Form("It is forbidden to store %s data into a folder of type %s!",
404 AliCDBManager::GetDataTypeName(type),
405 AliCDBManager::GetDataTypeName(expectedType)));
406 return 0;
407 }
408
c5941d67 409
410 TString strMirrors(mirrors);
411 if(!strMirrors.IsNull() && !strMirrors.IsWhitespace())
412 return PutEntry(entry, mirrors);
413 else
414 return PutEntry(entry);
f05209ee 415}
fe913d8f 416
62032124 417//_____________________________________________________________________________
c3a7b59a 418void AliCDBStorage::QueryCDB(Int_t run, const char* pathFilter,
62032124 419 Int_t version, AliCDBMetaData* md){
420// query CDB for files valid for given run, and fill list fValidFileIds
421// Actual query is done in virtual function QueryValidFiles()
422
423 fRun = run;
424
425 fPathFilter = pathFilter;
426 if(!fPathFilter.IsValid()) {
427 AliError(Form("Filter not valid: %s", pathFilter));
428 fPathFilter = "*";
429 return;
430 }
431
432 fVersion = version;
433
6dc56e97 434 AliInfo(Form("Querying files valid for run %d and path \"%s\" into CDB storage \"%s://%s\"",
435 fRun, pathFilter, fType.Data(), fBaseFolder.Data()));
436
62032124 437 // Clear fValidFileIds list (it cannot be filled twice!
438 AliDebug(2, "Clearing list of CDB Id's previously loaded");
439 fValidFileIds.Clear();
440
441 if(fMetaDataFilter) {delete fMetaDataFilter; fMetaDataFilter=0;}
442 if(md) fMetaDataFilter = dynamic_cast<AliCDBMetaData*> (md->Clone());
443
444 QueryValidFiles();
445
6dc56e97 446 AliInfo(Form("%d valid files found!", fValidFileIds.GetEntriesFast()));
62032124 447
448}
449
450//_____________________________________________________________________________
451void AliCDBStorage::PrintQueryCDB(){
452// print parameters used to load list of CDB Id's (fRun, fPathFilter, fVersion)
453
c3a7b59a 454 AliCDBId paramId(fPathFilter, fRun, fRun, fVersion);
455 AliInfo(Form("**** QueryCDB Parameters **** \n\t<%s>\n",
456 paramId.ToString().Data()));
457
458 if(fMetaDataFilter) fMetaDataFilter->PrintMetaData();
62032124 459
62032124 460
c3a7b59a 461 TString message = "**** Id's of valid objects found *****\n";
62032124 462 TIter iter(&fValidFileIds);
463 AliCDBId* anId=0;
464
465 // loop on the list of selection criteria
466 while ((anId = dynamic_cast<AliCDBId*>(iter.Next()))) {
c3a7b59a 467 message += Form("\t%s\n", anId->ToString().Data());
62032124 468 }
6dc56e97 469 message += Form("\n\tTotal: %d objects found\n", fValidFileIds.GetEntriesFast());
c3a7b59a 470 AliInfo(Form("%s", message.Data()));
62032124 471}
472
b8ec52f6 473//_____________________________________________________________________________
474AliCDBManager::DataType AliCDBStorage::GetDataType() const {
475// returns the type of the data that should be stored into this storage:
476// kConditions: conditions data; kReference: reference data; kPrivate: private (user-defined) data type
477
478 if(GetType() != "alien") return AliCDBManager::kPrivate;
479
480 TString condFolder = ((AliCDBGridParam*) AliCDBManager::Instance()->GetCondParam())->GetDBFolder();
481 TString refFolder = ((AliCDBGridParam*) AliCDBManager::Instance()->GetRefParam())->GetDBFolder();
482
483 if(GetBaseFolder().Contains(condFolder)) return AliCDBManager::kCondition;
484 if(GetBaseFolder().Contains(refFolder)) return AliCDBManager::kReference;
485
486 return AliCDBManager::kPrivate;
487}
145f45a2 488
c5941d67 489//_____________________________________________________________________________
490void AliCDBStorage::SetMirrorSEs(const char* mirrors) {
491// if the current storage is not of "alien" type, just issue a warning
7cca01eb 492// AliCDBGrid implements its own SetMirrorSEs method, classes for other storage types do not
c5941d67 493
494 TString storageType = GetType();
495 if(storageType != "alien"){
7cca01eb 496 AliWarning(Form("The current storage is of type \"%s\". Setting of SEs to \"%s\" skipped!",storageType.Data(),mirrors));
c5941d67 497 return;
498 }
499 AliError("We should never get here!! AliCDBGrid must have masked this virtual method!");
500 return;
501}
502
503//_____________________________________________________________________________
504const char* AliCDBStorage::GetMirrorSEs() const {
505// if the current storage is not of "alien" type, just issue a warning
7cca01eb 506// AliCDBGrid implements its own GetMirrorSEs method, classes for other storage types do not
c5941d67 507
508 TString storageType = GetType();
509 if(storageType != "alien"){
510 AliWarning(Form("The current storage is of type \"%s\" and cannot handle SEs. Returning empty string!",storageType.Data()));
511 return "";
512 }
513 AliError("We should never get here!! AliCDBGrid must have masked this virtual method!");
514 return "";
515}
516
145f45a2 517//_____________________________________________________________________________
518void AliCDBStorage::LoadTreeFromFile(AliCDBEntry *entry) const {
519// Checks whether entry contains a TTree and in case loads it into memory
520
ed343d5e 521 TObject *obj = (TObject*) entry->GetObject();
0a9e4cc8 522 if (!obj) {
523 AliError("Cannot retrieve the object:");
524 entry->PrintMetaData();
525 return;
526 }
145f45a2 527
ed343d5e 528 if (!strcmp(obj->ClassName(),TTree::Class_Name())) {
145f45a2 529
ed343d5e 530 AliWarning("Entry contains a TTree! Loading baskets...");
145f45a2 531
ed343d5e 532 TTree* tree = dynamic_cast<TTree*> (obj);
145f45a2 533
ed343d5e 534 if(!tree) return;
145f45a2 535
ed343d5e 536 tree->LoadBaskets();
537 tree->SetDirectory(0);
538 }
539 else if (!strcmp(obj->ClassName(),TNtuple::Class_Name())){
145f45a2 540
ed343d5e 541 AliWarning("Entry contains a TNtuple! Loading baskets...");
145f45a2 542
ed343d5e 543 TNtuple* ntu = dynamic_cast<TNtuple*> (obj);
145f45a2 544
ed343d5e 545 if(!ntu) return;
145f45a2 546
ed343d5e 547 ntu->LoadBaskets();
548 ntu->SetDirectory(0);
549 }
145f45a2 550
551 return;
552}
ed343d5e 553
554// //_____________________________________________________________________________
555// void AliCDBStorage::SetTreeToFile(AliCDBEntry *entry, TFile* file) const {
556// // Checks whether entry contains a TTree and in case assigns it to memory
557//
558// AliCDBMetaData *md = dynamic_cast<AliCDBMetaData*> (entry->GetMetaData());
559// if(!md) return;
560// TString objStr = md->GetObjectClassName();
561// if(objStr != "TTree") return;
562// AliWarning("Entry contains a TTree! Setting file...");
563//
564// TTree* tree = dynamic_cast<TTree*> (entry->GetObject());
565//
566// if(!tree) return;
567//
568// // tree->SetDirectory(file);
569// tree->SetDirectory(0);
570//
571// return;
572// }