]> git.uio.no Git - u/mrichter/AliRoot.git/blame - STEER/AliCDBManager.cxx
GetEvent() - do not skip the event if the current event is the same, because by defau...
[u/mrichter/AliRoot.git] / STEER / AliCDBManager.cxx
CommitLineData
9e1ceb13 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 **************************************************************************/
fdf65bb5 15//-------------------------------------------------------------------------
16// Implementation of AliCDBManager and AliCDBParam classe
17// Author: Alberto Colla
18// e-mail: Alberto.Colla@cern.ch
19//-------------------------------------------------------------------------
9e1ceb13 20
21#include "AliCDBManager.h"
22#include "AliCDBStorage.h"
23#include "AliLog.h"
24#include "AliCDBDump.h"
25#include "AliCDBLocal.h"
26#include "AliCDBGrid.h"
02c4845e 27#include "AliCDBEntry.h"
28#include "AliCDBMetaData.h"
9e1ceb13 29
02c4845e 30#include <TObjString.h>
9e1ceb13 31#include <TSystem.h>
32
33ClassImp(AliCDBParam)
34
35ClassImp(AliCDBManager)
36
37AliCDBManager* AliCDBManager::fgInstance = 0x0;
38
39//_____________________________________________________________________________
40AliCDBManager* AliCDBManager::Instance() {
41// returns AliCDBManager instance (singleton)
42
43 if (!fgInstance) {
44 fgInstance = new AliCDBManager();
45 fgInstance->Init();
46 }
47
48 return fgInstance;
49}
50
51//_____________________________________________________________________________
52void AliCDBManager::Init() {
53// factory registering
54
55 RegisterFactory(new AliCDBDumpFactory());
56 RegisterFactory(new AliCDBLocalFactory());
57 // AliCDBGridFactory is registered only if AliEn libraries are enabled in Root
58 if(!gSystem->Exec("root-config --has-alien |grep yes 2>&1 > /dev/null")){ // returns 0 if yes
59 AliInfo("AliEn classes enabled in Root. AliCDBGrid factory registered.");
60 RegisterFactory(new AliCDBGridFactory());
61 }
62}
63//_____________________________________________________________________________
64void AliCDBManager::Destroy() {
65// delete ALCDBManager instance and active storages
66
67 if (fgInstance) {
4b5e0dce 68 //fgInstance->Delete();
9e1ceb13 69 delete fgInstance;
70 fgInstance = 0x0;
71 }
72}
73
74//_____________________________________________________________________________
75AliCDBManager::AliCDBManager():
76 fDefaultStorage(NULL),
4b5e0dce 77 fDrainStorage(NULL),
6a63c2e2 78 fCache(kTRUE),
81b958cb 79 fRun(-1)
9e1ceb13 80{
81// default constuctor
4b5e0dce 82 fFactories.SetOwner(1);
83 fEntryCache.SetOwner(1);
9e1ceb13 84}
85
86//_____________________________________________________________________________
87AliCDBManager::~AliCDBManager() {
88// destructor
4b5e0dce 89 ClearCache();
9e1ceb13 90 DestroyActiveStorages();
b05400be 91 fDrainStorage = 0x0;
92 fDefaultStorage = 0x0;
9e1ceb13 93}
94
95//_____________________________________________________________________________
96AliCDBStorage* AliCDBManager::GetActiveStorage(const AliCDBParam* param) {
97// get a storage object from the list of active storages
98
99 return (AliCDBStorage*) fActiveStorages.GetValue(param);
100}
101
102//_____________________________________________________________________________
103void AliCDBManager::PutActiveStorage(AliCDBParam* param, AliCDBStorage* storage){
104// put a storage object into the list of active storages
105
106 fActiveStorages.Add(param, storage);
107 AliDebug(1, Form("Active storages: %d", fActiveStorages.GetEntries()));
108}
109
110//_____________________________________________________________________________
111void AliCDBManager::RegisterFactory(AliCDBStorageFactory* factory) {
112// add a storage factory to the list of registerd factories
113
114 if (!fFactories.Contains(factory)) {
115 fFactories.Add(factory);
116 }
117}
118
119//_____________________________________________________________________________
fdf65bb5 120Bool_t AliCDBManager::HasStorage(const char* dbString) const {
9e1ceb13 121// check if dbString is a URI valid for one of the registered factories
122
123 TIter iter(&fFactories);
124
125 AliCDBStorageFactory* factory;
126 while ((factory = (AliCDBStorageFactory*) iter.Next())) {
127
128 if (factory->Validate(dbString)) {
129 return kTRUE;
130 }
131 }
132
133 return kFALSE;
134}
135
136//_____________________________________________________________________________
fdf65bb5 137AliCDBParam* AliCDBManager::CreateParameter(const char* dbString) const {
9e1ceb13 138// create AliCDBParam object from URI string
139
140 TIter iter(&fFactories);
141
142 AliCDBStorageFactory* factory;
143 while ((factory = (AliCDBStorageFactory*) iter.Next())) {
144
145 AliCDBParam* param = factory->CreateParameter(dbString);
146 if (param) {
147 return param;
148 }
149 }
150
151 return NULL;
152}
153
154//_____________________________________________________________________________
155AliCDBStorage* AliCDBManager::GetStorage(const char* dbString) {
156// get storage object from URI string
157
158 AliCDBParam* param = CreateParameter(dbString);
159 if (!param) {
160 return NULL;
161 }
162
163 AliCDBStorage* aStorage = GetStorage(param);
164
165 delete param;
166
167 return aStorage;
168}
169
170//_____________________________________________________________________________
171AliCDBStorage* AliCDBManager::GetStorage(const AliCDBParam* param) {
172// get storage object from AliCDBParam object
173
174 // if the list of active storages already contains
175 // the requested storage, return it
176 AliCDBStorage* aStorage = GetActiveStorage(param);
177 if (aStorage) {
178 return aStorage;
179 }
180
181 TIter iter(&fFactories);
182
183 AliCDBStorageFactory* factory;
184
185 // loop on the list of registered factories
186 while ((factory = (AliCDBStorageFactory*) iter.Next())) {
187
188 // each factory tries to create its storage from the parameter
189 aStorage = factory->Create(param);
190 if (aStorage) {
191 PutActiveStorage(param->CloneParam(), aStorage);
192 // if default storage is not set, set to this storage
193 if(!fDefaultStorage){
194 fDefaultStorage=aStorage;
195 AliInfo(Form("Default storage set to: %s",(param->GetURI()).Data()));
196 }
197 return aStorage;
198 }
199 }
200
201 return NULL;
202}
203
204//_____________________________________________________________________________
205TList* AliCDBManager::GetActiveStorages() {
206// return list of active storages
207
208 TList* result = new TList();
209
210 TIter iter(fActiveStorages.GetTable());
211 TPair* aPair;
212 while ((aPair = (TPair*) iter.Next())) {
213 result->Add(aPair->Value());
214 }
215
216 return result;
217}
218
219//_____________________________________________________________________________
220void AliCDBManager::SetDrain(const char* dbString) {
221// set drain storage from URI string
222
223 fDrainStorage = GetStorage(dbString);
224}
225
226//_____________________________________________________________________________
227void AliCDBManager::SetDrain(const AliCDBParam* param) {
228// set drain storage from AliCDBParam
229
230 fDrainStorage = GetStorage(param);
231}
232
233//_____________________________________________________________________________
234void AliCDBManager::SetDrain(AliCDBStorage* storage) {
235// set drain storage from another active storage
236
237 fDrainStorage = storage;
238}
239
240//_____________________________________________________________________________
241Bool_t AliCDBManager::Drain(AliCDBEntry *entry) {
242// drain retrieved object to drain storage
243
244 AliInfo("Draining into drain storage...");
245 return fDrainStorage->Put(entry);
246}
247
9e1ceb13 248//_____________________________________________________________________________
249void AliCDBManager::SetDefaultStorage(const char* dbString) {
250// sets default storage from URI string
251
9e1ceb13 252 fDefaultStorage = GetStorage(dbString);
253}
254
255//_____________________________________________________________________________
256void AliCDBManager::SetDefaultStorage(const AliCDBParam* param) {
257// set default storage from AliCDBParam object
258
9e1ceb13 259 fDrainStorage = GetStorage(param);
260}
261
262//_____________________________________________________________________________
263void AliCDBManager::SetDefaultStorage(AliCDBStorage* storage) {
264// set default storage from another active storage
265
9e1ceb13 266 fDefaultStorage = storage;
267}
268
02c4845e 269//_____________________________________________________________________________
024cf675 270void AliCDBManager::SetSpecificStorage(const char* calibType, const char* dbString) {
271// sets storage specific for detector or calibration type (works with AliCDBManager::Get(...))
02c4845e 272
273 AliCDBParam *aPar = CreateParameter(dbString);
274 if(!aPar) return;
024cf675 275 SetSpecificStorage(calibType, aPar);
02c4845e 276 delete aPar;
277}
278
279//_____________________________________________________________________________
024cf675 280void AliCDBManager::SetSpecificStorage(const char* calibType, AliCDBParam* param) {
281// sets storage specific for detector or calibration type (works with AliCDBManager::Get(...))
282// Default storage should be defined prior to any specific storages, e.g.:
283// AliCDBManager::instance()->SetDefaultStorage("alien://");
284// AliCDBManager::instance()->SetSpecificStorage("TPC","local://DB_TPC");
285// AliCDBManager::instance()->SetSpecificStorage("RICH/Align","local://DB_TPCAlign");
02c4845e 286
287 if(!fDefaultStorage) {
288 AliError("Please activate a default storage first!");
289 return;
290 }
291
024cf675 292 TObjString *objCalibType = new TObjString(calibType);
293 if(fSpecificStorages.Contains(objCalibType)){
294 AliWarning(Form("%s storage already activated! It will be replaced by the new one",
295 calibType));
296 fSpecificStorages.Remove(objCalibType);
02c4845e 297 }
298 GetStorage(param);
024cf675 299 fSpecificStorages.Add(objCalibType, param->CloneParam());
02c4845e 300}
301
302//_____________________________________________________________________________
024cf675 303AliCDBStorage* AliCDBManager::GetSpecificStorage(const char* calibType) {
304// get storage specific for detector or calibration type
4b5e0dce 305
024cf675 306 AliCDBParam *checkPar = (AliCDBParam*) fSpecificStorages.GetValue(calibType);
02c4845e 307 if(!checkPar){
024cf675 308 AliError(Form("%s storage not found!",calibType));
02c4845e 309 return NULL;
310 } else {
311 return GetStorage(checkPar);
312 }
313
314}
315
024cf675 316//_____________________________________________________________________________
317AliCDBParam* AliCDBManager::SelectSpecificStorage(const TString& path) {
318// select storage valid for path from the list of specific storages
319
320 TIter iter(&fSpecificStorages);
321 TObjString *aCalibType;
322 AliCDBParam* aPar=0;
323 while((aCalibType = (TObjString*) iter.Next())){
324 if(path.Contains(aCalibType->String(), TString::kIgnoreCase)) {
325 aPar = (AliCDBParam*) fSpecificStorages.GetValue(aCalibType);
326 break;
327 }
328 }
329 return aPar;
330}
331
02c4845e 332//_____________________________________________________________________________
333AliCDBEntry* AliCDBManager::Get(const AliCDBPath& path, Int_t runNumber,
334 Int_t version, Int_t subVersion) {
335// get an AliCDBEntry object from the database
336
024cf675 337 if(runNumber < 0){
338 // RunNumber is not specified. Try with fRun
339 if (fRun < 0){
340 AliError("Run number neither specified in query nor set in AliCDBManager! Use AliCDBManager::SetRun.");
341 return NULL;
342 }
343 runNumber = fRun;
344 }
345
02c4845e 346 return Get(AliCDBId(path, runNumber, runNumber, version, subVersion));
347}
348
349//_____________________________________________________________________________
350AliCDBEntry* AliCDBManager::Get(const AliCDBPath& path,
351 const AliCDBRunRange& runRange, Int_t version,
352 Int_t subVersion) {
353// get an AliCDBEntry object from the database!
354
355 return Get(AliCDBId(path, runRange, version, subVersion));
356}
357
358//_____________________________________________________________________________
359AliCDBEntry* AliCDBManager::Get(const AliCDBId& query) {
360// get an AliCDBEntry object from the database
361
362 if(!fDefaultStorage) {
363 AliError("No storage set!");
364 return NULL;
365 }
366
367 // check if query's path and runRange are valid
368 // query is invalid also if version is not specified and subversion is!
369 if (!query.IsValid()) {
370 AliError(Form("Invalid query: %s", query.ToString().Data()));
371 return NULL;
372 }
024cf675 373
374 // query is not specified if path contains wildcard or run range= [-1,-1]
375 if (!query.IsSpecified()) {
02c4845e 376 AliError(Form("Unspecified query: %s",
377 query.ToString().Data()));
378 return NULL;
379 }
380
024cf675 381 if(fCache && query.GetFirstRun() != fRun)
382 AliWarning("Run number explicitly set in query: CDB cache temporarily disabled!");
383
384
385 AliCDBEntry *entry=0;
386
024cf675 387 // first look into map of cached objects
388 if(query.GetFirstRun() == fRun)
389 entry = (AliCDBEntry*) fEntryCache.GetValue(query.GetPath());
390
391 if(entry) {
6a63c2e2 392 AliDebug(2,Form("Object %s retrieved from cache !!",query.GetPath().Data()));
024cf675 393 return entry;
394 }
395
396 // Entry is not in cache -> retrieve it from CDB and cache it!!
397 AliCDBStorage *aStorage=0;
398 AliCDBParam *aPar=SelectSpecificStorage(query.GetPath());
02c4845e 399
400 if(aPar) {
401 aStorage=GetStorage(aPar);
402 TString str = aPar->GetURI();
403 AliDebug(2,Form("Looking into storage: %s",str.Data()));
404
405 } else {
406 aStorage=GetDefaultStorage();
407 AliDebug(2,"Looking into default storage");
408 }
409
024cf675 410 entry = aStorage->Get(query);
411 if (!entry) return NULL;
412
413 if(fCache && (query.GetFirstRun() == fRun)){
6a63c2e2 414 AliDebug(2,Form("Caching entry %s !",query.GetPath().Data()));
024cf675 415 CacheEntry(query.GetPath(), entry);
416 }
417
418 return entry;
419
02c4845e 420}
421
422//_____________________________________________________________________________
423TList* AliCDBManager::GetAll(const AliCDBPath& path, Int_t runNumber,
424 Int_t version, Int_t subVersion) {
425// get multiple AliCDBEntry objects from the database
426
427 return GetAll(AliCDBId(path, runNumber, runNumber, version,
428 subVersion));
429}
430
431//_____________________________________________________________________________
432TList* AliCDBManager::GetAll(const AliCDBPath& path,
433 const AliCDBRunRange& runRange, Int_t version, Int_t subVersion) {
434// get multiple AliCDBEntry objects from the database
435
436 return GetAll(AliCDBId(path, runRange, version, subVersion));
437}
438
439//_____________________________________________________________________________
440TList* AliCDBManager::GetAll(const AliCDBId& query) {
441// get multiple AliCDBEntry objects from the database
024cf675 442// Warning: this method works correctly only for queries of the type "Detector/*"
443// and not for more specific queries e.g. "Detector/Calib/*" !
02c4845e 444
445 if(!fDefaultStorage) {
446 AliError("No storage set!");
447 return NULL;
448 }
449
450 if (!query.IsValid()) {
451 AliError(Form("Invalid query: %s", query.ToString().Data()));
452 return NULL;
453 }
454
455 if(query.GetPath().BeginsWith('*')){
456 AliError("Query too generic in this context!");
457 return NULL;
458 }
459
460 if (query.IsAnyRange()) {
461 AliError(Form("Unspecified run or runrange: %s",
462 query.ToString().Data()));
463 return NULL;
464 }
465
466 TObjString objStrLev0(query.GetLevel0());
467 AliCDBParam *aPar = (AliCDBParam*) fSpecificStorages.GetValue(&objStrLev0);
024cf675 468
469 AliCDBStorage *aStorage;
02c4845e 470 if(aPar) {
471 aStorage=GetStorage(aPar);
472 TString str = aPar->GetURI();
473 AliDebug(2,Form("Looking into storage: %s",str.Data()));
474
475 } else {
476 aStorage=GetDefaultStorage();
477 AliDebug(2,"Looking into default storage");
478 }
479
480 TList *result = aStorage->GetAll(query);
481
482 return result;
483}
484
485//_____________________________________________________________________________
486Bool_t AliCDBManager::Put(TObject* object, AliCDBId& id, AliCDBMetaData* metaData){
487// store an AliCDBEntry object into the database
488
489 AliCDBEntry anEntry(object, id, metaData);
490 return Put(&anEntry);
491
492}
493
494
495//_____________________________________________________________________________
496Bool_t AliCDBManager::Put(AliCDBEntry* entry){
497// store an AliCDBEntry object into the database
498
499 if(!fDefaultStorage) {
500 AliError("No storage set!");
501 return kFALSE;
502 }
503
504 if (!entry){
505 AliError("No entry!");
506 return kFALSE;
507 }
508
509 if (!entry->GetId().IsValid()) {
510 AliError(Form("Invalid entry ID: %s",
511 entry->GetId().ToString().Data()));
512 return kFALSE;
513 }
514
515 if (!entry->GetId().IsSpecified()) {
516 AliError(Form("Unspecified entry ID: %s",
517 entry->GetId().ToString().Data()));
518 return kFALSE;
519 }
520
521 AliCDBId id = entry->GetId();
024cf675 522 AliCDBParam *aPar = SelectSpecificStorage(id.GetPath());
523
02c4845e 524 AliCDBStorage *aStorage;
525
526 if(aPar) {
527 aStorage=GetStorage(aPar);
528 TString str = aPar->GetURI();
529 AliDebug(2,Form("Storing object into storage: %s",str.Data()));
530
531 } else {
532 aStorage=GetDefaultStorage();
533 AliDebug(2,"Storing object into default storage");
534 }
535
536 return aStorage->Put(entry);
537
538
539}
9e1ceb13 540
4b5e0dce 541//_____________________________________________________________________________
542void AliCDBManager::CacheEntry(const char* path, AliCDBEntry* entry)
543{
544// cache AliCDBEntry. Cache is valid until run number is changed.
545
546 AliDebug(2,Form("Filling cache with entry %s",path));
547 fEntryCache.Add(new TObjString(path), entry);
548 AliDebug(2,Form("Cache entries: %d",fEntryCache.GetEntries()));
549
550}
551
552//_____________________________________________________________________________
553void AliCDBManager::SetRun(Long64_t run)
554{
4b5e0dce 555// Sets current run number.
556// When the run number changes the caching is cleared.
4b5e0dce 557
558 if (fRun == run)
559 return;
560
561 fRun = run;
562 ClearCache();
563}
564
565//_____________________________________________________________________________
566void AliCDBManager::ClearCache(){
567// clear AliCDBEntry cache
568
569 AliDebug(2,Form("Clearing cache!"));
570 fEntryCache.DeleteAll();
571 AliDebug(2,Form("Cache entries: %d",fEntryCache.GetEntries()));
572
573}
574
9e1ceb13 575//_____________________________________________________________________________
576void AliCDBManager::DestroyActiveStorages() {
577// delete list of active storages
578
579 fActiveStorages.DeleteAll();
02c4845e 580 fSpecificStorages.DeleteAll();
9e1ceb13 581}
582
583//_____________________________________________________________________________
584void AliCDBManager::DestroyActiveStorage(AliCDBStorage* /*storage*/) {
b05400be 585// destroys active storage
586
587/*
588 TIter iter(fActiveStorages.GetTable());
589 TPair* aPair;
590 while ((aPair = (TPair*) iter.Next())) {
591 if(storage == (AliCDBStorage*) aPair->Value())
592 delete fActiveStorages.Remove(aPair->Key());
593 storage->Delete(); storage=0x0;
594 }
595*/
9e1ceb13 596
597}
598
599///////////////////////////////////////////////////////////
600// AliCDBManager Parameter class //
601// interface to specific AliCDBParameter class //
602// (AliCDBGridParam, AliCDBLocalParam, AliCDBDumpParam) //
603///////////////////////////////////////////////////////////
604
605AliCDBParam::AliCDBParam() {
606// constructor
607
608}
609
610//_____________________________________________________________________________
611AliCDBParam::~AliCDBParam() {
612// destructor
613
614}
615