]> git.uio.no Git - u/mrichter/AliRoot.git/blame - STEER/CDB/AliCDBStorage.cxx
ZDC automatic scripts updates (Marco Leoncino) + updates in QA config
[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():
5078a13f 31 fValidFileIds(),
32 fRun(-1),
33 fPathFilter(),
34 fVersion(-1),
35 fMetaDataFilter(0),
36 fSelections(),
37 fURI(),
38 fType(),
39 fBaseFolder(),
40 fNretry(0),
41 fInitRetrySeconds(0)
62032124 42{
5078a13f 43 // constructor
9e1ceb13 44
5078a13f 45 fValidFileIds.SetOwner(1);
46 fSelections.SetOwner(1);
9e1ceb13 47}
2c8628dd 48
9e1ceb13 49//_____________________________________________________________________________
50AliCDBStorage::~AliCDBStorage() {
51// destructor
2c8628dd 52
5078a13f 53 RemoveAllSelections();
54 fValidFileIds.Clear();
55 delete fMetaDataFilter;
62032124 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
5078a13f 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)
69 if (aSelection->Comprises(*id)) {
70 AliDebug(2,Form("Using selection criterion: %s ", aSelection->ToString().Data()));
71 // return required version and subversion
72
73 id->SetVersion(aSelection->GetVersion());
74 id->SetSubVersion(aSelection->GetSubVersion());
75 return;
76 }
77 }
78
79 // no valid element is found in the list of selection criteria -> return
80 AliDebug(2,"Looking for objects with most recent version");
81 return;
2c8628dd 82}
83
b05400be 84//_____________________________________________________________________________
85void AliCDBStorage::ReadSelectionFromFile(const char *fileName){
86// read selection criteria list from file
5078a13f 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
b05400be 107}
108
2c8628dd 109//_____________________________________________________________________________
9e1ceb13 110void AliCDBStorage::AddSelection(const AliCDBId& selection) {
111// add a selection criterion
112
5078a13f 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));
9e1ceb13 128}
2c8628dd 129
130//_____________________________________________________________________________
62032124 131void AliCDBStorage::AddSelection(const AliCDBPath& path,
5078a13f 132 const AliCDBRunRange& runRange, Int_t version, Int_t subVersion){
133 // add a selection criterion
2c8628dd 134
5078a13f 135 AddSelection(AliCDBId(path, runRange, version, subVersion));
2c8628dd 136}
137
9e1ceb13 138//_____________________________________________________________________________
139void AliCDBStorage::AddSelection(const AliCDBPath& path,
5078a13f 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));
9e1ceb13 144}
2c8628dd 145
9e1ceb13 146//_____________________________________________________________________________
147void AliCDBStorage::RemoveSelection(const AliCDBId& selection) {
148// remove a selection criterion
2c8628dd 149
5078a13f 150 TIter iter(&fSelections);
151 AliCDBId* aSelection;
2c8628dd 152
5078a13f 153 while ((aSelection = (AliCDBId*) iter.Next())) {
154 if (selection.Comprises(*aSelection)) {
155 fSelections.Remove(aSelection);
156 }
157 }
9e1ceb13 158}
2c8628dd 159
9e1ceb13 160//_____________________________________________________________________________
161void AliCDBStorage::RemoveSelection(const AliCDBPath& path,
5078a13f 162 const AliCDBRunRange& runRange){
163 // remove a selection criterion
2c8628dd 164
5078a13f 165 RemoveSelection(AliCDBId(path, runRange, -1, -1));
9e1ceb13 166}
f05209ee 167
9e1ceb13 168//_____________________________________________________________________________
169void AliCDBStorage::RemoveSelection(const AliCDBPath& path,
5078a13f 170 Int_t firstRun, Int_t lastRun){
171 // remove a selection criterion
f05209ee 172
5078a13f 173 RemoveSelection(AliCDBId(path, firstRun, lastRun, -1, -1));
9e1ceb13 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
5078a13f 180 delete fSelections.RemoveAt(position);
2c8628dd 181}
182
2c8628dd 183//_____________________________________________________________________________
9e1ceb13 184void AliCDBStorage::RemoveAllSelections(){
185// remove all selection criteria
2c8628dd 186
5078a13f 187 fSelections.Clear();
9e1ceb13 188}
fe913d8f 189
9e1ceb13 190//_____________________________________________________________________________
191void AliCDBStorage::PrintSelectionList(){
192// prints the list of selection criteria
193
5078a13f 194 TIter iter(&fSelections);
195 AliCDBId* aSelection;
62032124 196
5078a13f 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
5078a13f 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 }
9e1ceb13 215
5078a13f 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()));
220 return NULL;
221 }
62032124 222
5078a13f 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
5078a13f 227 AliCDBEntry* entry = GetEntry(query);
02c4845e 228
5078a13f 229 if (oldStatus != kFALSE)
230 TH1::AddDirectory(kTRUE);
0f678e35 231
5078a13f 232 // if drain storage is set, drain entry into drain storage
233 if(entry && (AliCDBManager::Instance())->IsDrainSet())
234 AliCDBManager::Instance()->Drain(entry);
0f678e35 235
5078a13f 236 return entry;
2c8628dd 237}
238
239//_____________________________________________________________________________
4667c116 240AliCDBEntry* AliCDBStorage::Get(const AliCDBPath& path, Int_t runNumber,
5078a13f 241 Int_t version, Int_t subVersion) {
242 // get an AliCDBEntry object from the database
2c8628dd 243
5078a13f 244 return Get(AliCDBId(path, runNumber, runNumber, version, subVersion));
2c8628dd 245}
246
2c8628dd 247//_____________________________________________________________________________
4667c116 248AliCDBEntry* AliCDBStorage::Get(const AliCDBPath& path,
5078a13f 249 const AliCDBRunRange& runRange, Int_t version,
250 Int_t subVersion) {
251 // get an AliCDBEntry object from the database
2c8628dd 252
5078a13f 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
5078a13f 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
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);
9e1ceb13 275
5078a13f 276 TList *result = GetEntries(query);
02c4845e 277
5078a13f 278 if (oldStatus != kFALSE)
279 TH1::AddDirectory(kTRUE);
9e1ceb13 280
5078a13f 281 Int_t nEntries = result->GetEntries();
02c4845e 282
5078a13f 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()));
288 }
b8ec52f6 289
9e1ceb13 290
5078a13f 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 }
b8ec52f6 298
9e1ceb13 299
5078a13f 300 return result;
9e1ceb13 301}
2c8628dd 302
9e1ceb13 303//_____________________________________________________________________________
304TList* AliCDBStorage::GetAll(const AliCDBPath& path, Int_t runNumber,
5078a13f 305 Int_t version, Int_t subVersion) {
306 // get multiple AliCDBEntry objects from the database
2c8628dd 307
5078a13f 308 return GetAll(AliCDBId(path, runNumber, runNumber, version,
309 subVersion));
2c8628dd 310}
311
2c8628dd 312//_____________________________________________________________________________
9e1ceb13 313TList* AliCDBStorage::GetAll(const AliCDBPath& path,
5078a13f 314 const AliCDBRunRange& runRange, Int_t version, Int_t subVersion) {
315 // get multiple AliCDBEntry objects from the database
2c8628dd 316
5078a13f 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
5078a13f 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 }
4667c116 330
5078a13f 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 }
4667c116 337
5078a13f 338 AliCDBId* id = GetEntryId(query);
4667c116 339
5078a13f 340 return id;
4667c116 341}
342
343//_____________________________________________________________________________
344AliCDBId* AliCDBStorage::GetId(const AliCDBPath& path, Int_t runNumber,
5078a13f 345 Int_t version, Int_t subVersion) {
346 // get the Id of the valid object from the database (does not open the file)
4667c116 347
5078a13f 348 return GetId(AliCDBId(path, runNumber, runNumber, version, subVersion));
4667c116 349}
350
351//_____________________________________________________________________________
352AliCDBId* AliCDBStorage::GetId(const AliCDBPath& path,
5078a13f 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)
4667c116 356
5078a13f 357 return GetId(AliCDBId(path, runRange, version, subVersion));
4667c116 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
968cfa08 363
5078a13f 364 if (object==0x0) {
365 AliError("Null Entry! No storage will be done!");
366 return kFALSE;
367 }
368
369 AliCDBEntry anEntry(object, id, metaData);
f05209ee 370
5078a13f 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
5078a13f 378 if (!entry){
379 AliError("No entry!");
380 return kFALSE;
381 }
382
383 if (entry->GetObject()==0x0){
384 AliError("No valid object in CDB entry!");
385 return kFALSE;
386 }
387
388 if (!entry->GetId().IsValid()) {
389 AliError(Form("Invalid entry ID: %s",
390 entry->GetId().ToString().Data()));
391 return kFALSE;
392 }
393
394 if (!entry->GetId().IsSpecified()) {
395 AliError(Form("Unspecified entry ID: %s",
396 entry->GetId().ToString().Data()));
397 return kFALSE;
398 }
399
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
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,
5078a13f 419 Int_t version, AliCDBMetaData* md){
62032124 420// query CDB for files valid for given run, and fill list fValidFileIds
421// Actual query is done in virtual function QueryValidFiles()
5078a13f 422// If version is not specified, the query will fill fValidFileIds
423// with highest versions
62032124 424
5078a13f 425 fRun = run;
62032124 426
5078a13f 427 fPathFilter = pathFilter;
428 if(!fPathFilter.IsValid()) {
429 AliError(Form("Filter not valid: %s", pathFilter));
430 fPathFilter = "*";
431 return;
432 }
62032124 433
5078a13f 434 fVersion = version;
62032124 435
5078a13f 436 AliInfo(Form("Querying files valid for run %d and path \"%s\" into CDB storage \"%s://%s\"",
437 fRun, pathFilter, fType.Data(), fBaseFolder.Data()));
6dc56e97 438
003e9909 439 // In fValidFileIds, clear id for the same 3level path, if any
440 AliDebug(3, Form("Clearing list of CDB Id's previously loaded for path \"%s\"", pathFilter));
441 AliCDBPath filter(pathFilter);
78f84046 442 for (Int_t i=fValidFileIds.GetEntries()-1; i>=0; --i) {
003e9909 443 AliCDBId *rmMe = dynamic_cast<AliCDBId*>(fValidFileIds.At(i));
444 AliCDBPath rmPath = rmMe->GetAliCDBPath();
445 if (filter.Comprises(rmPath)) {
446 AliDebug(3,Form("Removing id \"%s\" matching: \"%s\"", rmPath.GetPath().Data(), pathFilter));
447 delete fValidFileIds.RemoveAt(i);
448 }
449 }
62032124 450
5078a13f 451 if(fMetaDataFilter) {delete fMetaDataFilter; fMetaDataFilter=0;}
452 if(md) fMetaDataFilter = dynamic_cast<AliCDBMetaData*> (md->Clone());
62032124 453
5078a13f 454 QueryValidFiles();
62032124 455
003e9909 456 AliInfo(Form("%d valid files found!", fValidFileIds.GetEntries()));
62032124 457
458}
459
460//_____________________________________________________________________________
461void AliCDBStorage::PrintQueryCDB(){
462// print parameters used to load list of CDB Id's (fRun, fPathFilter, fVersion)
463
5078a13f 464 AliCDBId paramId(fPathFilter, fRun, fRun, fVersion);
465 AliInfo(Form("**** QueryCDB Parameters **** \n\t<%s>\n",
466 paramId.ToString().Data()));
c3a7b59a 467
5078a13f 468 if(fMetaDataFilter) fMetaDataFilter->PrintMetaData();
62032124 469
62032124 470
5078a13f 471 TString message = "**** Id's of valid objects found *****\n";
472 TIter iter(&fValidFileIds);
473 AliCDBId* anId=0;
62032124 474
5078a13f 475 // loop on the list of selection criteria
476 while ((anId = dynamic_cast<AliCDBId*>(iter.Next()))) {
477 message += Form("\t%s\n", anId->ToString().Data());
478 }
479 message += Form("\n\tTotal: %d objects found\n", fValidFileIds.GetEntriesFast());
480 AliInfo(Form("%s", message.Data()));
62032124 481}
482
b8ec52f6 483//_____________________________________________________________________________
484AliCDBManager::DataType AliCDBStorage::GetDataType() const {
485// returns the type of the data that should be stored into this storage:
486// kConditions: conditions data; kReference: reference data; kPrivate: private (user-defined) data type
487
5078a13f 488 if(GetType() != "alien") return AliCDBManager::kPrivate;
b8ec52f6 489
5078a13f 490 TString condFolder = ((AliCDBGridParam*) AliCDBManager::Instance()->GetCondParam())->GetDBFolder();
491 TString refFolder = ((AliCDBGridParam*) AliCDBManager::Instance()->GetRefParam())->GetDBFolder();
b8ec52f6 492
5078a13f 493 if(GetBaseFolder().Contains(condFolder)) return AliCDBManager::kCondition;
494 if(GetBaseFolder().Contains(refFolder)) return AliCDBManager::kReference;
b8ec52f6 495
5078a13f 496 return AliCDBManager::kPrivate;
b8ec52f6 497}
145f45a2 498
c5941d67 499//_____________________________________________________________________________
500void AliCDBStorage::SetMirrorSEs(const char* mirrors) {
501// if the current storage is not of "alien" type, just issue a warning
7cca01eb 502// AliCDBGrid implements its own SetMirrorSEs method, classes for other storage types do not
c5941d67 503
5078a13f 504 TString storageType = GetType();
505 if(storageType != "alien"){
506 AliWarning(Form("The current storage is of type \"%s\". Setting of SEs to \"%s\" skipped!",storageType.Data(),mirrors));
507 return;
508 }
509 AliError("We should never get here!! AliCDBGrid must have masked this virtual method!");
510 return;
c5941d67 511}
512
513//_____________________________________________________________________________
514const char* AliCDBStorage::GetMirrorSEs() const {
515// if the current storage is not of "alien" type, just issue a warning
7cca01eb 516// AliCDBGrid implements its own GetMirrorSEs method, classes for other storage types do not
c5941d67 517
5078a13f 518 TString storageType = GetType();
519 if(storageType != "alien"){
520 AliWarning(Form("The current storage is of type \"%s\" and cannot handle SEs. Returning empty string!",storageType.Data()));
521 return "";
522 }
523 AliError("We should never get here!! AliCDBGrid must have masked this virtual method!");
524 return "";
c5941d67 525}
526
145f45a2 527//_____________________________________________________________________________
528void AliCDBStorage::LoadTreeFromFile(AliCDBEntry *entry) const {
529// Checks whether entry contains a TTree and in case loads it into memory
530
5078a13f 531 TObject *obj = (TObject*) entry->GetObject();
532 if (!obj) {
533 AliError("Cannot retrieve the object:");
534 entry->PrintMetaData();
535 return;
536 }
145f45a2 537
5078a13f 538 if (!strcmp(obj->ClassName(),TTree::Class_Name())) {
145f45a2 539
5078a13f 540 AliWarning("Entry contains a TTree! Loading baskets...");
145f45a2 541
5078a13f 542 TTree* tree = dynamic_cast<TTree*> (obj);
145f45a2 543
5078a13f 544 if(!tree) return;
145f45a2 545
5078a13f 546 tree->LoadBaskets();
547 tree->SetDirectory(0);
548 }
549 else if (!strcmp(obj->ClassName(),TNtuple::Class_Name())){
145f45a2 550
5078a13f 551 AliWarning("Entry contains a TNtuple! Loading baskets...");
145f45a2 552
5078a13f 553 TNtuple* ntu = dynamic_cast<TNtuple*> (obj);
145f45a2 554
5078a13f 555 if(!ntu) return;
145f45a2 556
5078a13f 557 ntu->LoadBaskets();
558 ntu->SetDirectory(0);
559 }
145f45a2 560
5078a13f 561 return;
145f45a2 562}
ed343d5e 563
564// //_____________________________________________________________________________
565// void AliCDBStorage::SetTreeToFile(AliCDBEntry *entry, TFile* file) const {
566// // Checks whether entry contains a TTree and in case assigns it to memory
567//
568// AliCDBMetaData *md = dynamic_cast<AliCDBMetaData*> (entry->GetMetaData());
569// if(!md) return;
570// TString objStr = md->GetObjectClassName();
571// if(objStr != "TTree") return;
572// AliWarning("Entry contains a TTree! Setting file...");
573//
574// TTree* tree = dynamic_cast<TTree*> (entry->GetObject());
575//
576// if(!tree) return;
577//
578// // tree->SetDirectory(file);
579// tree->SetDirectory(0);
580//
581// return;
582// }