]> git.uio.no Git - u/mrichter/AliRoot.git/blame - STEER/AliCDBStorage.cxx
moved AliSelector, AliSelectorRL to STEER
[u/mrichter/AliRoot.git] / STEER / 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>
9e1ceb13 18#include "AliCDBManager.h"
fe913d8f 19#include "AliCDBStorage.h"
2c8628dd 20
9e1ceb13 21#include "AliCDBEntry.h"
22#include "AliLog.h"
2c8628dd 23
fe913d8f 24ClassImp(AliCDBStorage)
2c8628dd 25
9e1ceb13 26//_____________________________________________________________________________
62032124 27AliCDBStorage::AliCDBStorage():
28fValidFileIds(),
29fRun(-1),
30fPathFilter(),
31fVersion(-1),
32fMetaDataFilter(0),
33fSelections(),
34fURI(),
35fType(),
36fBaseFolder()
37{
9e1ceb13 38// constructor
39
62032124 40 fValidFileIds.SetOwner(1);
9e1ceb13 41 fSelections.SetOwner(1);
42}
2c8628dd 43
9e1ceb13 44//_____________________________________________________________________________
45AliCDBStorage::~AliCDBStorage() {
46// destructor
2c8628dd 47
62032124 48 RemoveAllSelections();
49 fValidFileIds.Clear();
50 delete fMetaDataFilter;
51
9e1ceb13 52}
2c8628dd 53
54//_____________________________________________________________________________
4b5e0dce 55void AliCDBStorage::GetSelection(/*const*/ AliCDBId* id) {
0f678e35 56// return required version and subversion from the list of selection criteria
9e1ceb13 57
58 TIter iter(&fSelections);
59 AliCDBId* aSelection;
60
61 // loop on the list of selection criteria
62 while ((aSelection = (AliCDBId*) iter.Next())) {
63 // check if selection element contains id's path and run (range)
4b5e0dce 64 if (aSelection->Comprises(*id)) {
b05400be 65 AliDebug(2,Form("Using selection criterion: %s ", aSelection->ToString().Data()));
9e1ceb13 66 // return required version and subversion
4b5e0dce 67
68 id->SetVersion(aSelection->GetVersion());
69 id->SetSubVersion(aSelection->GetSubVersion());
70 return;
9e1ceb13 71 }
72 }
73
74 // no valid element is found in the list of selection criteria -> return
b05400be 75 AliDebug(2,"Looking for objects with most recent version");
4b5e0dce 76 return;
2c8628dd 77}
78
b05400be 79//_____________________________________________________________________________
80void AliCDBStorage::ReadSelectionFromFile(const char *fileName){
81// read selection criteria list from file
82
83 RemoveAllSelections();
84
85 TList *list = GetIdListFromFile(fileName);
86 if(!list) return;
87
88 list->SetOwner();
89 Int_t nId = list->GetEntries();
90 AliCDBId *id;
91 TKey *key;
92
93 for(int i=nId-1;i>=0;i--){
94 key = (TKey*) list->At(i);
95 id = (AliCDBId*) key->ReadObj();
96 if(id) AddSelection(*id);
97 }
98 delete list;
99 AliInfo(Form("Selection criteria list filled with %d entries",fSelections.GetEntries()));
100 PrintSelectionList();
101
102}
103
2c8628dd 104//_____________________________________________________________________________
9e1ceb13 105void AliCDBStorage::AddSelection(const AliCDBId& selection) {
106// add a selection criterion
107
108 AliCDBPath path = selection.GetPath();
109 if(!path.IsValid()) return;
110
111 TIter iter(&fSelections);
112 const AliCDBId *anId;
113 while((anId = (AliCDBId*) iter.Next())){
114 if(selection.Comprises(*anId)){
115 AliWarning("This selection is more general than a previous one and will hide it!");
116 AliWarning(Form("%s", (anId->ToString()).Data()));
117 fSelections.AddBefore(anId, new AliCDBId(selection));
118 return;
119 }
120
121 }
122 fSelections.AddFirst(new AliCDBId(selection));
123}
2c8628dd 124
125//_____________________________________________________________________________
62032124 126void AliCDBStorage::AddSelection(const AliCDBPath& path,
9e1ceb13 127 const AliCDBRunRange& runRange, Int_t version, Int_t subVersion){
128// add a selection criterion
2c8628dd 129
9e1ceb13 130 AddSelection(AliCDBId(path, runRange, version, subVersion));
2c8628dd 131}
132
9e1ceb13 133//_____________________________________________________________________________
134void AliCDBStorage::AddSelection(const AliCDBPath& path,
135 Int_t firstRun, Int_t lastRun, Int_t version, Int_t subVersion){
136// add a selection criterion
137
138 AddSelection(AliCDBId(path, firstRun, lastRun, version, subVersion));
139}
2c8628dd 140
9e1ceb13 141//_____________________________________________________________________________
142void AliCDBStorage::RemoveSelection(const AliCDBId& selection) {
143// remove a selection criterion
2c8628dd 144
9e1ceb13 145 TIter iter(&fSelections);
146 AliCDBId* aSelection;
2c8628dd 147
9e1ceb13 148 while ((aSelection = (AliCDBId*) iter.Next())) {
149 if (selection.Comprises(*aSelection)) {
150 fSelections.Remove(aSelection);
151 }
152 }
153}
2c8628dd 154
9e1ceb13 155//_____________________________________________________________________________
156void AliCDBStorage::RemoveSelection(const AliCDBPath& path,
157 const AliCDBRunRange& runRange){
158// remove a selection criterion
2c8628dd 159
9e1ceb13 160 RemoveSelection(AliCDBId(path, runRange, -1, -1));
161}
f05209ee 162
9e1ceb13 163//_____________________________________________________________________________
164void AliCDBStorage::RemoveSelection(const AliCDBPath& path,
165 Int_t firstRun, Int_t lastRun){
166// remove a selection criterion
f05209ee 167
9e1ceb13 168 RemoveSelection(AliCDBId(path, firstRun, lastRun, -1, -1));
169}
f05209ee 170
9e1ceb13 171//_____________________________________________________________________________
eb0b1051 172void AliCDBStorage::RemoveSelection(int position){
9e1ceb13 173// remove a selection criterion from its position in the list
2c8628dd 174
62032124 175 delete fSelections.RemoveAt(position);
2c8628dd 176}
177
2c8628dd 178//_____________________________________________________________________________
9e1ceb13 179void AliCDBStorage::RemoveAllSelections(){
180// remove all selection criteria
2c8628dd 181
62032124 182 fSelections.Clear();
9e1ceb13 183}
fe913d8f 184
9e1ceb13 185//_____________________________________________________________________________
186void AliCDBStorage::PrintSelectionList(){
187// prints the list of selection criteria
188
189 TIter iter(&fSelections);
190 AliCDBId* aSelection;
62032124 191
9e1ceb13 192 // loop on the list of selection criteria
193 int index=0;
194 while ((aSelection = (AliCDBId*) iter.Next())) {
195 AliInfo(Form("index %d -> selection: %s",index++, aSelection->ToString().Data()));
196 }
fe913d8f 197
9e1ceb13 198}
fe913d8f 199
9e1ceb13 200//_____________________________________________________________________________
0f678e35 201AliCDBEntry* AliCDBStorage::Get(const AliCDBId& query) {
9e1ceb13 202// get an AliCDBEntry object from the database
203
204 // check if query's path and runRange are valid
205 // query is invalid also if version is not specified and subversion is!
206 if (!query.IsValid()) {
207 AliError(Form("Invalid query: %s", query.ToString().Data()));
208 return NULL;
209 }
210
211 // query is not specified if path contains wildcard or runrange = [-1,-1]
212 if (!query.IsSpecified()) {
213 AliError(Form("Unspecified query: %s",
214 query.ToString().Data()));
215 return NULL;
216 }
62032124 217
02c4845e 218 // This is needed otherwise TH1 objects (histos, TTree's) are lost when file is closed!
219 Bool_t oldStatus = TH1::AddDirectoryStatus();
220 TH1::AddDirectory(kFALSE);
9e1ceb13 221
222 AliCDBEntry* entry = GetEntry(query);
02c4845e 223
224 if (oldStatus != kFALSE)
225 TH1::AddDirectory(kTRUE);
0f678e35 226
9e1ceb13 227 if (entry) {
62032124 228 // this is to make the SHUTTLE output lighter
229 if(!(query.GetPath().Contains("SHUTTLE/STATUS")))
230 AliInfo(Form("CDB object retrieved: %s", entry->GetId().ToString().Data()));
9e1ceb13 231 } else {
62032124 232 // this is to make the SHUTTLE output lighter
0f678e35 233 if(!(query.GetPath().Contains("SHUTTLE/STATUS")))
c3a7b59a 234 AliInfo(Form("No valid CDB object found! request was: %s", query.ToString().Data()));
9e1ceb13 235 }
0f678e35 236
9e1ceb13 237 // if drain storage is set, drain entry into drain storage
238 if(entry && (AliCDBManager::Instance())->IsDrainSet())
239 AliCDBManager::Instance()->Drain(entry);
0f678e35 240
9e1ceb13 241 return entry;
2c8628dd 242}
243
244//_____________________________________________________________________________
9e1ceb13 245AliCDBEntry* AliCDBStorage::Get(const AliCDBPath& path, Int_t runNumber,
246 Int_t version, Int_t subVersion) {
247// get an AliCDBEntry object from the database
2c8628dd 248
9e1ceb13 249 return Get(AliCDBId(path, runNumber, runNumber, version, subVersion));
2c8628dd 250}
251
2c8628dd 252//_____________________________________________________________________________
9e1ceb13 253AliCDBEntry* AliCDBStorage::Get(const AliCDBPath& path,
254 const AliCDBRunRange& runRange, Int_t version,
255 Int_t subVersion) {
256// get an AliCDBEntry object from the database
2c8628dd 257
9e1ceb13 258 return Get(AliCDBId(path, runRange, version, subVersion));
2c8628dd 259}
260
2c8628dd 261//_____________________________________________________________________________
9e1ceb13 262TList* AliCDBStorage::GetAll(const AliCDBId& query) {
263// get multiple AliCDBEntry objects from the database
264
265
266 if (!query.IsValid()) {
267 AliError(Form("Invalid query: %s", query.ToString().Data()));
268 return NULL;
269 }
270
271 if (query.IsAnyRange()) {
272 AliError(Form("Unspecified run or runrange: %s",
273 query.ToString().Data()));
274 return NULL;
275 }
276
02c4845e 277 // This is needed otherwise TH1 objects (histos, TTree's) are lost when file is closed!
278 Bool_t oldStatus = TH1::AddDirectoryStatus();
279 TH1::AddDirectory(kFALSE);
280
9e1ceb13 281 TList *result = GetEntries(query);
282
02c4845e 283 if (oldStatus != kFALSE)
284 TH1::AddDirectory(kTRUE);
285
9e1ceb13 286 Int_t nEntries = result->GetEntries();
287 if (nEntries) {
62032124 288 AliInfo(Form("%d objects retrieved.",nEntries));
9e1ceb13 289 for(int i=0; i<nEntries;i++){
290 AliCDBEntry *entry = (AliCDBEntry*) result->At(i);
291 AliInfo(Form("%s",entry->GetId().ToString().Data()));
292
293 }
294 } else {
c3a7b59a 295 AliInfo(Form("No valid CDB object found! request was: %s", query.ToString().Data()));
9e1ceb13 296 }
297
298 // if drain storage is set, drain entries into drain storage
299 if((AliCDBManager::Instance())->IsDrainSet()){
300 for(int i = 0; i<result->GetEntries(); i++){
301 AliCDBEntry* entry = (AliCDBEntry*) result->At(i);
302 AliCDBManager::Instance()->Drain(entry);
303 }
304 }
305
306
307 return result;
308}
2c8628dd 309
9e1ceb13 310//_____________________________________________________________________________
311TList* AliCDBStorage::GetAll(const AliCDBPath& path, Int_t runNumber,
312 Int_t version, Int_t subVersion) {
313// get multiple AliCDBEntry objects from the database
2c8628dd 314
9e1ceb13 315 return GetAll(AliCDBId(path, runNumber, runNumber, version,
316 subVersion));
2c8628dd 317}
318
2c8628dd 319//_____________________________________________________________________________
9e1ceb13 320TList* AliCDBStorage::GetAll(const AliCDBPath& path,
321 const AliCDBRunRange& runRange, Int_t version, Int_t subVersion) {
322// get multiple AliCDBEntry objects from the database
2c8628dd 323
9e1ceb13 324 return GetAll(AliCDBId(path, runRange, version, subVersion));
2c8628dd 325}
f05209ee 326
327
328//_____________________________________________________________________________
9e1ceb13 329Bool_t AliCDBStorage::Put(TObject* object, AliCDBId& id, AliCDBMetaData* metaData) {
02c4845e 330// store an AliCDBEntry object into the database
9e1ceb13 331
332 AliCDBEntry anEntry(object, id, metaData);
f05209ee 333
9e1ceb13 334 return Put(&anEntry);
335}
f05209ee 336
9e1ceb13 337//_____________________________________________________________________________
338Bool_t AliCDBStorage::Put(AliCDBEntry* entry) {
02c4845e 339// store an AliCDBEntry object into the database
9e1ceb13 340
02c4845e 341 if (!entry){
342 AliError("No entry!");
343 return kFALSE;
344 }
345
9e1ceb13 346 if (!entry->GetId().IsValid()) {
62032124 347 AliError(Form("Invalid entry ID: %s",
9e1ceb13 348 entry->GetId().ToString().Data()));
349 return kFALSE;
350 }
351
352 if (!entry->GetId().IsSpecified()) {
62032124 353 AliError(Form("Unspecified entry ID: %s",
9e1ceb13 354 entry->GetId().ToString().Data()));
355 return kFALSE;
356 }
357
02c4845e 358 // set object's class name into metaData!
359 entry->GetMetaData()->SetObjectClassName(entry->GetObject()->ClassName());
360
9e1ceb13 361 return PutEntry(entry);
f05209ee 362}
fe913d8f 363
62032124 364//_____________________________________________________________________________
c3a7b59a 365void AliCDBStorage::QueryCDB(Int_t run, const char* pathFilter,
62032124 366 Int_t version, AliCDBMetaData* md){
367// query CDB for files valid for given run, and fill list fValidFileIds
368// Actual query is done in virtual function QueryValidFiles()
369
370 fRun = run;
371
372 fPathFilter = pathFilter;
373 if(!fPathFilter.IsValid()) {
374 AliError(Form("Filter not valid: %s", pathFilter));
375 fPathFilter = "*";
376 return;
377 }
378
379 fVersion = version;
380
6dc56e97 381 AliInfo(Form("Querying files valid for run %d and path \"%s\" into CDB storage \"%s://%s\"",
382 fRun, pathFilter, fType.Data(), fBaseFolder.Data()));
383
62032124 384 // Clear fValidFileIds list (it cannot be filled twice!
385 AliDebug(2, "Clearing list of CDB Id's previously loaded");
386 fValidFileIds.Clear();
387
388 if(fMetaDataFilter) {delete fMetaDataFilter; fMetaDataFilter=0;}
389 if(md) fMetaDataFilter = dynamic_cast<AliCDBMetaData*> (md->Clone());
390
391 QueryValidFiles();
c3a7b59a 392 AliCDBId queryId(pathFilter,run,run,version);
62032124 393
6dc56e97 394 AliInfo(Form("%d valid files found!", fValidFileIds.GetEntriesFast()));
62032124 395
396}
397
398//_____________________________________________________________________________
399void AliCDBStorage::PrintQueryCDB(){
400// print parameters used to load list of CDB Id's (fRun, fPathFilter, fVersion)
401
c3a7b59a 402 AliCDBId paramId(fPathFilter, fRun, fRun, fVersion);
403 AliInfo(Form("**** QueryCDB Parameters **** \n\t<%s>\n",
404 paramId.ToString().Data()));
405
406 if(fMetaDataFilter) fMetaDataFilter->PrintMetaData();
62032124 407
62032124 408
c3a7b59a 409 TString message = "**** Id's of valid objects found *****\n";
62032124 410 TIter iter(&fValidFileIds);
411 AliCDBId* anId=0;
412
413 // loop on the list of selection criteria
414 while ((anId = dynamic_cast<AliCDBId*>(iter.Next()))) {
c3a7b59a 415 message += Form("\t%s\n", anId->ToString().Data());
62032124 416 }
6dc56e97 417 message += Form("\n\tTotal: %d objects found\n", fValidFileIds.GetEntriesFast());
c3a7b59a 418 AliInfo(Form("%s", message.Data()));
62032124 419}
420