]> git.uio.no Git - u/mrichter/AliRoot.git/blame - STEER/CDB/AliCDBStorage.cxx
Adding the possibility to specify SEs for mirroring when putting an object on AliEn
[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 (entry) {
62032124 233 // this is to make the SHUTTLE output lighter
234 if(!(query.GetPath().Contains("SHUTTLE/STATUS")))
b8ec52f6 235 AliDebug(2, Form("CDB object retrieved: %s", entry->GetId().ToString().Data()));
9e1ceb13 236 } else {
62032124 237 // this is to make the SHUTTLE output lighter
f6c857dc 238 if(!(query.GetPath().Contains("SHUTTLE/STATUS"))){
239
240 if(!(query.GetPath().Contains("SHUTTLE"))){
241 AliFatal(Form("No valid CDB object found! request was: %s", query.ToString().Data()));
242 }
243 else {
244 AliInfo(Form("No valid CDB object found! request was: %s", query.ToString().Data()));
245 }
b6102d21 246 }
9e1ceb13 247 }
0f678e35 248
9e1ceb13 249 // if drain storage is set, drain entry into drain storage
250 if(entry && (AliCDBManager::Instance())->IsDrainSet())
251 AliCDBManager::Instance()->Drain(entry);
0f678e35 252
9e1ceb13 253 return entry;
2c8628dd 254}
255
256//_____________________________________________________________________________
4667c116 257AliCDBEntry* AliCDBStorage::Get(const AliCDBPath& path, Int_t runNumber,
9e1ceb13 258 Int_t version, Int_t subVersion) {
259// get an AliCDBEntry object from the database
2c8628dd 260
9e1ceb13 261 return Get(AliCDBId(path, runNumber, runNumber, version, subVersion));
2c8628dd 262}
263
2c8628dd 264//_____________________________________________________________________________
4667c116 265AliCDBEntry* AliCDBStorage::Get(const AliCDBPath& path,
9e1ceb13 266 const AliCDBRunRange& runRange, Int_t version,
267 Int_t subVersion) {
268// get an AliCDBEntry object from the database
2c8628dd 269
9e1ceb13 270 return Get(AliCDBId(path, runRange, version, subVersion));
2c8628dd 271}
272
2c8628dd 273//_____________________________________________________________________________
9e1ceb13 274TList* AliCDBStorage::GetAll(const AliCDBId& query) {
275// get multiple AliCDBEntry objects from the database
276
277
278 if (!query.IsValid()) {
279 AliError(Form("Invalid query: %s", query.ToString().Data()));
280 return NULL;
281 }
282
283 if (query.IsAnyRange()) {
284 AliError(Form("Unspecified run or runrange: %s",
285 query.ToString().Data()));
286 return NULL;
287 }
288
02c4845e 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);
292
9e1ceb13 293 TList *result = GetEntries(query);
294
02c4845e 295 if (oldStatus != kFALSE)
296 TH1::AddDirectory(kTRUE);
297
9e1ceb13 298 Int_t nEntries = result->GetEntries();
b8ec52f6 299
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()));
9e1ceb13 305 }
306
b8ec52f6 307
9e1ceb13 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);
313 }
314 }
315
316
317 return result;
318}
2c8628dd 319
9e1ceb13 320//_____________________________________________________________________________
321TList* AliCDBStorage::GetAll(const AliCDBPath& path, Int_t runNumber,
322 Int_t version, Int_t subVersion) {
323// get multiple AliCDBEntry objects from the database
2c8628dd 324
9e1ceb13 325 return GetAll(AliCDBId(path, runNumber, runNumber, version,
326 subVersion));
2c8628dd 327}
328
2c8628dd 329//_____________________________________________________________________________
9e1ceb13 330TList* AliCDBStorage::GetAll(const AliCDBPath& path,
331 const AliCDBRunRange& runRange, Int_t version, Int_t subVersion) {
332// get multiple AliCDBEntry objects from the database
2c8628dd 333
9e1ceb13 334 return GetAll(AliCDBId(path, runRange, version, subVersion));
2c8628dd 335}
f05209ee 336
4667c116 337//_____________________________________________________________________________
338AliCDBId* AliCDBStorage::GetId(const AliCDBId& query) {
339// get the Id of the valid object from the database (does not open the file)
340
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()));
345 return NULL;
346 }
347
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()));
352 return NULL;
353 }
354
355 AliCDBId* id = GetEntryId(query);
356
357 return id;
358}
359
360//_____________________________________________________________________________
361AliCDBId* 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)
364
365 return GetId(AliCDBId(path, runNumber, runNumber, version, subVersion));
366}
367
368//_____________________________________________________________________________
369AliCDBId* AliCDBStorage::GetId(const AliCDBPath& path,
370 const AliCDBRunRange& runRange, Int_t version,
371 Int_t subVersion) {
372// get the Id of the valid object from the database (does not open the file)
373
374 return GetId(AliCDBId(path, runRange, version, subVersion));
375}
f05209ee 376
377//_____________________________________________________________________________
b8ec52f6 378Bool_t AliCDBStorage::Put(TObject* object, AliCDBId& id, AliCDBMetaData* metaData, AliCDBManager::DataType type) {
02c4845e 379// store an AliCDBEntry object into the database
9e1ceb13 380
968cfa08 381 if (object==0x0) {
382 AliError("Null Entry! No storage will be done!");
383 return kFALSE;
384 }
385
9e1ceb13 386 AliCDBEntry anEntry(object, id, metaData);
f05209ee 387
06eeadea 388 return Put(&anEntry, "", type);
b8ec52f6 389}
f05209ee 390
9e1ceb13 391//_____________________________________________________________________________
06eeadea 392Bool_t AliCDBStorage::Put(AliCDBEntry* entry, const char* mirrors, AliCDBManager::DataType type) {
02c4845e 393// store an AliCDBEntry object into the database
9e1ceb13 394
02c4845e 395 if (!entry){
396 AliError("No entry!");
397 return kFALSE;
398 }
399
968cfa08 400 if (entry->GetObject()==0x0){
401 AliError("No valid object in CDB entry!");
402 return kFALSE;
403 }
404
9e1ceb13 405 if (!entry->GetId().IsValid()) {
62032124 406 AliError(Form("Invalid entry ID: %s",
9e1ceb13 407 entry->GetId().ToString().Data()));
408 return kFALSE;
409 }
410
411 if (!entry->GetId().IsSpecified()) {
62032124 412 AliError(Form("Unspecified entry ID: %s",
9e1ceb13 413 entry->GetId().ToString().Data()));
414 return kFALSE;
415 }
416
b8ec52f6 417 AliCDBManager::DataType expectedType = GetDataType();
418
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)));
423 return 0;
424 }
425
06eeadea 426 return PutEntry(entry,mirrors);
f05209ee 427}
fe913d8f 428
62032124 429//_____________________________________________________________________________
c3a7b59a 430void AliCDBStorage::QueryCDB(Int_t run, const char* pathFilter,
62032124 431 Int_t version, AliCDBMetaData* md){
432// query CDB for files valid for given run, and fill list fValidFileIds
433// Actual query is done in virtual function QueryValidFiles()
434
435 fRun = run;
436
437 fPathFilter = pathFilter;
438 if(!fPathFilter.IsValid()) {
439 AliError(Form("Filter not valid: %s", pathFilter));
440 fPathFilter = "*";
441 return;
442 }
443
444 fVersion = version;
445
6dc56e97 446 AliInfo(Form("Querying files valid for run %d and path \"%s\" into CDB storage \"%s://%s\"",
447 fRun, pathFilter, fType.Data(), fBaseFolder.Data()));
448
62032124 449 // Clear fValidFileIds list (it cannot be filled twice!
450 AliDebug(2, "Clearing list of CDB Id's previously loaded");
451 fValidFileIds.Clear();
452
453 if(fMetaDataFilter) {delete fMetaDataFilter; fMetaDataFilter=0;}
454 if(md) fMetaDataFilter = dynamic_cast<AliCDBMetaData*> (md->Clone());
455
456 QueryValidFiles();
457
6dc56e97 458 AliInfo(Form("%d valid files found!", fValidFileIds.GetEntriesFast()));
62032124 459
460}
461
462//_____________________________________________________________________________
463void AliCDBStorage::PrintQueryCDB(){
464// print parameters used to load list of CDB Id's (fRun, fPathFilter, fVersion)
465
c3a7b59a 466 AliCDBId paramId(fPathFilter, fRun, fRun, fVersion);
467 AliInfo(Form("**** QueryCDB Parameters **** \n\t<%s>\n",
468 paramId.ToString().Data()));
469
470 if(fMetaDataFilter) fMetaDataFilter->PrintMetaData();
62032124 471
62032124 472
c3a7b59a 473 TString message = "**** Id's of valid objects found *****\n";
62032124 474 TIter iter(&fValidFileIds);
475 AliCDBId* anId=0;
476
477 // loop on the list of selection criteria
478 while ((anId = dynamic_cast<AliCDBId*>(iter.Next()))) {
c3a7b59a 479 message += Form("\t%s\n", anId->ToString().Data());
62032124 480 }
6dc56e97 481 message += Form("\n\tTotal: %d objects found\n", fValidFileIds.GetEntriesFast());
c3a7b59a 482 AliInfo(Form("%s", message.Data()));
62032124 483}
484
b8ec52f6 485//_____________________________________________________________________________
486AliCDBManager::DataType AliCDBStorage::GetDataType() const {
487// returns the type of the data that should be stored into this storage:
488// kConditions: conditions data; kReference: reference data; kPrivate: private (user-defined) data type
489
490 if(GetType() != "alien") return AliCDBManager::kPrivate;
491
492 TString condFolder = ((AliCDBGridParam*) AliCDBManager::Instance()->GetCondParam())->GetDBFolder();
493 TString refFolder = ((AliCDBGridParam*) AliCDBManager::Instance()->GetRefParam())->GetDBFolder();
494
495 if(GetBaseFolder().Contains(condFolder)) return AliCDBManager::kCondition;
496 if(GetBaseFolder().Contains(refFolder)) return AliCDBManager::kReference;
497
498 return AliCDBManager::kPrivate;
499}
145f45a2 500
501//_____________________________________________________________________________
502void AliCDBStorage::LoadTreeFromFile(AliCDBEntry *entry) const {
503// Checks whether entry contains a TTree and in case loads it into memory
504
ed343d5e 505 TObject *obj = (TObject*) entry->GetObject();
0a9e4cc8 506 if (!obj) {
507 AliError("Cannot retrieve the object:");
508 entry->PrintMetaData();
509 return;
510 }
145f45a2 511
ed343d5e 512 if (!strcmp(obj->ClassName(),TTree::Class_Name())) {
145f45a2 513
ed343d5e 514 AliWarning("Entry contains a TTree! Loading baskets...");
145f45a2 515
ed343d5e 516 TTree* tree = dynamic_cast<TTree*> (obj);
145f45a2 517
ed343d5e 518 if(!tree) return;
145f45a2 519
ed343d5e 520 tree->LoadBaskets();
521 tree->SetDirectory(0);
522 }
523 else if (!strcmp(obj->ClassName(),TNtuple::Class_Name())){
145f45a2 524
ed343d5e 525 AliWarning("Entry contains a TNtuple! Loading baskets...");
145f45a2 526
ed343d5e 527 TNtuple* ntu = dynamic_cast<TNtuple*> (obj);
145f45a2 528
ed343d5e 529 if(!ntu) return;
145f45a2 530
ed343d5e 531 ntu->LoadBaskets();
532 ntu->SetDirectory(0);
533 }
145f45a2 534
535 return;
536}
ed343d5e 537
538// //_____________________________________________________________________________
539// void AliCDBStorage::SetTreeToFile(AliCDBEntry *entry, TFile* file) const {
540// // Checks whether entry contains a TTree and in case assigns it to memory
541//
542// AliCDBMetaData *md = dynamic_cast<AliCDBMetaData*> (entry->GetMetaData());
543// if(!md) return;
544// TString objStr = md->GetObjectClassName();
545// if(objStr != "TTree") return;
546// AliWarning("Entry contains a TTree! Setting file...");
547//
548// TTree* tree = dynamic_cast<TTree*> (entry->GetObject());
549//
550// if(!tree) return;
551//
552// // tree->SetDirectory(file);
553// tree->SetDirectory(0);
554//
555// return;
556// }