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