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