]> git.uio.no Git - u/mrichter/AliRoot.git/blame - STEER/AliCDBManager.cxx
Adding the number of fired chips in the ITS fast OR (Domenico, Massimo)
[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
b8ec52f6 37//TODO OCDB and Reference folder should not be fully hardcoded but built from run number (or year/LHC period)
38TString AliCDBManager::fgkCondUri("alien://folder=/alice/cern.ch/user/a/aliprod/testCDB/CDB?user=aliprod");
39TString AliCDBManager::fgkRefUri("alien://folder=/alice/cern.ch/user/a/aliprod/testCDB/Reference?user=aliprod");
9e1ceb13 40AliCDBManager* AliCDBManager::fgInstance = 0x0;
41
42//_____________________________________________________________________________
d1982b43 43AliCDBManager* AliCDBManager::Instance(TMap *entryCache, Int_t run)
b8ec52f6 44{
9e1ceb13 45// returns AliCDBManager instance (singleton)
46
47 if (!fgInstance) {
48 fgInstance = new AliCDBManager();
d1982b43 49 if (!entryCache)
50 fgInstance->Init();
51 else
52 fgInstance->InitFromCache(entryCache,run);
9e1ceb13 53 }
54
55 return fgInstance;
56}
57
58//_____________________________________________________________________________
59void AliCDBManager::Init() {
60// factory registering
61
62 RegisterFactory(new AliCDBDumpFactory());
63 RegisterFactory(new AliCDBLocalFactory());
64 // AliCDBGridFactory is registered only if AliEn libraries are enabled in Root
65 if(!gSystem->Exec("root-config --has-alien |grep yes 2>&1 > /dev/null")){ // returns 0 if yes
66 AliInfo("AliEn classes enabled in Root. AliCDBGrid factory registered.");
67 RegisterFactory(new AliCDBGridFactory());
b8ec52f6 68 fCondParam = CreateParameter(fgkCondUri);
69 fRefParam = CreateParameter(fgkRefUri);
9e1ceb13 70 }
4667c116 71
72 InitShortLived();
9e1ceb13 73}
d1982b43 74
75//_____________________________________________________________________________
76void AliCDBManager::InitFromCache(TMap *entryCache, Int_t run) {
77// initialize manager from existing cache
78// used on the slaves in case of parallel reconstruction
79 SetRun(run);
80
81 TIter iter(entryCache->GetTable());
82 TPair* pair = 0;
83
84 while((pair = dynamic_cast<TPair*> (iter.Next()))){
85 fEntryCache.Add(pair->Key(),pair->Value());
86 }
87 // fEntry is the new owner of the cache
88 fEntryCache.SetOwnerKeyValue(kTRUE,kTRUE);
89 AliInfo(Form("%d cache entries have been loaded",fEntryCache.GetEntries()));
90}
91
9e1ceb13 92//_____________________________________________________________________________
93void AliCDBManager::Destroy() {
94// delete ALCDBManager instance and active storages
95
96 if (fgInstance) {
4b5e0dce 97 //fgInstance->Delete();
9e1ceb13 98 delete fgInstance;
99 fgInstance = 0x0;
100 }
101}
102
103//_____________________________________________________________________________
104AliCDBManager::AliCDBManager():
fe12e09c 105 TObject(),
106 fFactories(),
107 fActiveStorages(),
108 fSpecificStorages(),
b21e3194 109 fEntryCache(),
110 fIds(0),
111 fStorageMap(0),
112 fShortLived(0),
fe12e09c 113 fDefaultStorage(NULL),
114 fDrainStorage(NULL),
b21e3194 115 fCondParam(0),
116 fRefParam(0),
4667c116 117 fRun(-1),
1f341624 118 fCache(kTRUE),
119 fLock(kFALSE)
9e1ceb13 120{
121// default constuctor
4b5e0dce 122 fFactories.SetOwner(1);
62032124 123 fActiveStorages.SetOwner(1);
124 fSpecificStorages.SetOwner(1);
d1982b43 125 fEntryCache.SetName("CDBEntryCache");
62032124 126 fEntryCache.SetOwner(1);
b21e3194 127
128 fStorageMap = new TMap();
129 fStorageMap->SetOwner(1);
130 fIds = new TList();
131 fIds->SetOwner(1);
9e1ceb13 132}
133
134//_____________________________________________________________________________
135AliCDBManager::~AliCDBManager() {
136// destructor
4b5e0dce 137 ClearCache();
9e1ceb13 138 DestroyActiveStorages();
62032124 139 fFactories.Delete();
b05400be 140 fDrainStorage = 0x0;
141 fDefaultStorage = 0x0;
b21e3194 142 delete fStorageMap; fStorageMap = 0;
143 delete fIds; fIds = 0;
b8ec52f6 144 delete fCondParam;
145 delete fRefParam;
4667c116 146 delete fShortLived; fShortLived = 0x0;
9e1ceb13 147}
148
9e1ceb13 149//_____________________________________________________________________________
150void AliCDBManager::PutActiveStorage(AliCDBParam* param, AliCDBStorage* storage){
151// put a storage object into the list of active storages
152
153 fActiveStorages.Add(param, storage);
154 AliDebug(1, Form("Active storages: %d", fActiveStorages.GetEntries()));
155}
156
157//_____________________________________________________________________________
158void AliCDBManager::RegisterFactory(AliCDBStorageFactory* factory) {
159// add a storage factory to the list of registerd factories
160
161 if (!fFactories.Contains(factory)) {
162 fFactories.Add(factory);
163 }
164}
165
166//_____________________________________________________________________________
fdf65bb5 167Bool_t AliCDBManager::HasStorage(const char* dbString) const {
b21e3194 168// check if dbString is a URI valid for one of the registered factories
9e1ceb13 169
170 TIter iter(&fFactories);
171
62032124 172 AliCDBStorageFactory* factory=0;
9e1ceb13 173 while ((factory = (AliCDBStorageFactory*) iter.Next())) {
174
175 if (factory->Validate(dbString)) {
176 return kTRUE;
4005d0b5 177 }
9e1ceb13 178 }
179
180 return kFALSE;
181}
182
183//_____________________________________________________________________________
fdf65bb5 184AliCDBParam* AliCDBManager::CreateParameter(const char* dbString) const {
9e1ceb13 185// create AliCDBParam object from URI string
186
187 TIter iter(&fFactories);
188
62032124 189 AliCDBStorageFactory* factory=0;
9e1ceb13 190 while ((factory = (AliCDBStorageFactory*) iter.Next())) {
9e1ceb13 191 AliCDBParam* param = factory->CreateParameter(dbString);
62032124 192 if(param) return param;
9e1ceb13 193 }
194
195 return NULL;
196}
197
198//_____________________________________________________________________________
199AliCDBStorage* AliCDBManager::GetStorage(const char* dbString) {
200// get storage object from URI string
1f341624 201
9e1ceb13 202 AliCDBParam* param = CreateParameter(dbString);
203 if (!param) {
c3a7b59a 204 AliError(Form("Failed to activate requested storage! Check URI: %s", dbString));
9e1ceb13 205 return NULL;
82096dfc 206 }
9e1ceb13 207
208 AliCDBStorage* aStorage = GetStorage(param);
209
210 delete param;
9e1ceb13 211 return aStorage;
212}
213
214//_____________________________________________________________________________
215AliCDBStorage* AliCDBManager::GetStorage(const AliCDBParam* param) {
216// get storage object from AliCDBParam object
1f341624 217
82096dfc 218 // if the list of active storages already contains
9e1ceb13 219 // the requested storage, return it
220 AliCDBStorage* aStorage = GetActiveStorage(param);
221 if (aStorage) {
222 return aStorage;
223 }
224
1f341624 225 // if lock is ON, cannot activate more storages!
226 if(fLock) {
227 if (fDefaultStorage) {
228 AliFatal("Lock is ON, and default storage is already set: "
229 "cannot reset it or activate more storages!");
230 }
231 }
232
9e1ceb13 233 TIter iter(&fFactories);
234
62032124 235 AliCDBStorageFactory* factory=0;
917a098b 236
9e1ceb13 237 // loop on the list of registered factories
238 while ((factory = (AliCDBStorageFactory*) iter.Next())) {
239
240 // each factory tries to create its storage from the parameter
241 aStorage = factory->Create(param);
242 if (aStorage) {
243 PutActiveStorage(param->CloneParam(), aStorage);
917a098b 244 aStorage->SetURI(param->GetURI());
6dc56e97 245 if(fRun >= 0) {
96b7636d 246 if(aStorage->GetType() == "alien"){
247 aStorage->QueryCDB(fRun);
248 } else {
249 AliDebug(2,
250 "Skipping query for valid files, it is used only in grid...");
251 }
c3a7b59a 252 }
9e1ceb13 253 return aStorage;
254 }
255 }
256
c3a7b59a 257 AliError(Form("Failed to activate requested storage! Check URI: %s", param->GetURI().Data()));
258
9e1ceb13 259 return NULL;
260}
261
62032124 262//_____________________________________________________________________________
263AliCDBStorage* AliCDBManager::GetActiveStorage(const AliCDBParam* param) {
264// get a storage object from the list of active storages
265
266 return dynamic_cast<AliCDBStorage*> (fActiveStorages.GetValue(param));
267}
268
9e1ceb13 269//_____________________________________________________________________________
270TList* AliCDBManager::GetActiveStorages() {
271// return list of active storages
62032124 272// user has responsibility to delete returned object
9e1ceb13 273
274 TList* result = new TList();
275
917a098b 276 TIter iter(fActiveStorages.GetTable());
62032124 277 TPair* aPair=0;
9e1ceb13 278 while ((aPair = (TPair*) iter.Next())) {
279 result->Add(aPair->Value());
280 }
281
282 return result;
283}
284
285//_____________________________________________________________________________
286void AliCDBManager::SetDrain(const char* dbString) {
287// set drain storage from URI string
288
289 fDrainStorage = GetStorage(dbString);
290}
291
292//_____________________________________________________________________________
293void AliCDBManager::SetDrain(const AliCDBParam* param) {
294// set drain storage from AliCDBParam
b8ec52f6 295
9e1ceb13 296 fDrainStorage = GetStorage(param);
297}
298
299//_____________________________________________________________________________
300void AliCDBManager::SetDrain(AliCDBStorage* storage) {
301// set drain storage from another active storage
302
303 fDrainStorage = storage;
304}
305
306//_____________________________________________________________________________
307Bool_t AliCDBManager::Drain(AliCDBEntry *entry) {
308// drain retrieved object to drain storage
309
b8ec52f6 310 AliDebug(2, "Draining into drain storage...");
9e1ceb13 311 return fDrainStorage->Put(entry);
312}
313
9e1ceb13 314//_____________________________________________________________________________
315void AliCDBManager::SetDefaultStorage(const char* dbString) {
316// sets default storage from URI string
1f341624 317
82096dfc 318 AliInfo(Form("Setting Default storage to: %s",dbString));
4954c5cd 319 AliCDBStorage* bckStorage = fDefaultStorage;
320
82096dfc 321 fDefaultStorage = GetStorage(dbString);
95c0e132 322
323 if(!fDefaultStorage) return;
4954c5cd 324
325 if(bckStorage && (fDefaultStorage != bckStorage)){
326 AliWarning("Existing default storage replaced: clearing cache!");
327 ClearCache();
328 }
b21e3194 329
330 if (fStorageMap->Contains("default")) {
331 delete fStorageMap->Remove(fStorageMap->GetValue("default"));
332 }
333 fStorageMap->Add(new TObjString("default"), new TObjString(fDefaultStorage->GetURI()));
9e1ceb13 334}
335
336//_____________________________________________________________________________
337void AliCDBManager::SetDefaultStorage(const AliCDBParam* param) {
338// set default storage from AliCDBParam object
1f341624 339
4954c5cd 340 AliCDBStorage* bckStorage = fDefaultStorage;
341
b8ec52f6 342 fDefaultStorage = GetStorage(param);
4954c5cd 343
95c0e132 344 if(!fDefaultStorage) return;
345
4954c5cd 346 if(bckStorage && (fDefaultStorage != bckStorage)){
347 AliWarning("Existing default storage replaced: clearing cache!");
348 ClearCache();
349 }
b21e3194 350
351 if (fStorageMap->Contains("default")) {
352 delete fStorageMap->Remove(fStorageMap->GetValue("default"));
353 }
354 fStorageMap->Add(new TObjString("default"), new TObjString(fDefaultStorage->GetURI()));
9e1ceb13 355}
356
357//_____________________________________________________________________________
358void AliCDBManager::SetDefaultStorage(AliCDBStorage* storage) {
359// set default storage from another active storage
1f341624 360
361 // if lock is ON, cannot activate more storages!
362 if(fLock) {
363 if (fDefaultStorage) {
364 AliFatal("Lock is ON, and default storage is already set: "
365 "cannot reset it or activate more storages!");
366 }
367 }
368
369 if (!storage) {
370 UnsetDefaultStorage();
371 return;
372 }
373
4954c5cd 374 AliCDBStorage* bckStorage = fDefaultStorage;
375
9e1ceb13 376 fDefaultStorage = storage;
4954c5cd 377
378 if(bckStorage && (fDefaultStorage != bckStorage)){
379 AliWarning("Existing default storage replaced: clearing cache!");
380 ClearCache();
381 }
b21e3194 382
383 if (fStorageMap->Contains("default")) {
384 delete fStorageMap->Remove(fStorageMap->GetValue("default"));
385 }
386 fStorageMap->Add(new TObjString("default"), new TObjString(fDefaultStorage->GetURI()));
9e1ceb13 387}
4667c116 388
389//_____________________________________________________________________________
1f341624 390void AliCDBManager::UnsetDefaultStorage() {
391// Unset default storage
392
393 // if lock is ON, action is forbidden!
394 if(fLock) {
395 if (fDefaultStorage) {
396 AliFatal("Lock is ON: cannot unset default storage!");
397 }
398 }
399
400 if (fDefaultStorage) {
401 AliWarning("Clearing cache!");
402 ClearCache();
403 }
404
405 fDefaultStorage = 0x0;
4667c116 406}
9e1ceb13 407
02c4845e 408//_____________________________________________________________________________
024cf675 409void AliCDBManager::SetSpecificStorage(const char* calibType, const char* dbString) {
410// sets storage specific for detector or calibration type (works with AliCDBManager::Get(...))
02c4845e 411
412 AliCDBParam *aPar = CreateParameter(dbString);
413 if(!aPar) return;
024cf675 414 SetSpecificStorage(calibType, aPar);
02c4845e 415 delete aPar;
416}
417
418//_____________________________________________________________________________
024cf675 419void AliCDBManager::SetSpecificStorage(const char* calibType, AliCDBParam* param) {
420// sets storage specific for detector or calibration type (works with AliCDBManager::Get(...))
421// Default storage should be defined prior to any specific storages, e.g.:
422// AliCDBManager::instance()->SetDefaultStorage("alien://");
c3a7b59a 423// AliCDBManager::instance()->SetSpecificStorage("TPC/*","local://DB_TPC");
424// AliCDBManager::instance()->SetSpecificStorage("*/Align/*","local://DB_TPCAlign");
425// calibType must be a valid CDB path! (3 level folder structure)
02c4845e 426
427 if(!fDefaultStorage) {
c3a7b59a 428 AliError("Please activate a default storage first!");
02c4845e 429 return;
430 }
4005d0b5 431
62032124 432 AliCDBPath aPath(calibType);
433 if(!aPath.IsValid()){
434 AliError(Form("Not a valid path: %s", calibType));
435 return;
436 }
437
438 TObjString *objCalibType = new TObjString(aPath.GetPath());
024cf675 439 if(fSpecificStorages.Contains(objCalibType)){
62032124 440 AliWarning(Form("Storage \"%s\" already activated! It will be replaced by the new one",
024cf675 441 calibType));
4005d0b5 442 AliCDBParam *checkPar = dynamic_cast<AliCDBParam*> (fSpecificStorages.GetValue(calibType));
443 if(checkPar) delete checkPar;
8e245d15 444 delete fSpecificStorages.Remove(objCalibType);
02c4845e 445 }
95c0e132 446 AliCDBStorage *aStorage = GetStorage(param);
447 if(!aStorage) return;
96b7636d 448
449 fSpecificStorages.Add(objCalibType, param->CloneParam());
b21e3194 450
451 if(fStorageMap->Contains(objCalibType)){
452 delete fStorageMap->Remove(objCalibType);
453 }
454 fStorageMap->Add(objCalibType->Clone(), new TObjString(param->GetURI()));
455
02c4845e 456}
457
458//_____________________________________________________________________________
024cf675 459AliCDBStorage* AliCDBManager::GetSpecificStorage(const char* calibType) {
460// get storage specific for detector or calibration type
4b5e0dce 461
9c7fb557 462 AliCDBPath calibPath(calibType);
463 if(!calibPath.IsValid()) return NULL;
96b7636d 464
9c7fb557 465 AliCDBParam *checkPar = (AliCDBParam*) fSpecificStorages.GetValue(calibPath.GetPath());
02c4845e 466 if(!checkPar){
b8ec52f6 467 AliError(Form("%s storage not found!", calibType));
02c4845e 468 return NULL;
469 } else {
470 return GetStorage(checkPar);
471 }
96b7636d 472
02c4845e 473}
474
024cf675 475//_____________________________________________________________________________
476AliCDBParam* AliCDBManager::SelectSpecificStorage(const TString& path) {
82096dfc 477// select storage valid for path from the list of specific storages
024cf675 478
62032124 479 AliCDBPath aPath(path);
4005d0b5 480 if(!aPath.IsValid()) return NULL;
62032124 481
024cf675 482 TIter iter(&fSpecificStorages);
62032124 483 TObjString *aCalibType=0;
96b7636d 484 AliCDBPath tmpPath("null/null/null");
024cf675 485 AliCDBParam* aPar=0;
486 while((aCalibType = (TObjString*) iter.Next())){
62032124 487 AliCDBPath calibTypePath(aCalibType->GetName());
488 if(calibTypePath.Comprises(aPath)) {
96b7636d 489 if(calibTypePath.Comprises(tmpPath)) continue;
024cf675 490 aPar = (AliCDBParam*) fSpecificStorages.GetValue(aCalibType);
96b7636d 491 tmpPath.SetPath(calibTypePath.GetPath());
024cf675 492 }
493 }
494 return aPar;
495}
496
02c4845e 497//_____________________________________________________________________________
4667c116 498AliCDBEntry* AliCDBManager::Get(const AliCDBPath& path, Int_t runNumber,
02c4845e 499 Int_t version, Int_t subVersion) {
500// get an AliCDBEntry object from the database
501
024cf675 502 if(runNumber < 0){
503 // RunNumber is not specified. Try with fRun
504 if (fRun < 0){
505 AliError("Run number neither specified in query nor set in AliCDBManager! Use AliCDBManager::SetRun.");
506 return NULL;
507 }
508 runNumber = fRun;
509 }
510
02c4845e 511 return Get(AliCDBId(path, runNumber, runNumber, version, subVersion));
512}
513
514//_____________________________________________________________________________
4667c116 515AliCDBEntry* AliCDBManager::Get(const AliCDBPath& path,
02c4845e 516 const AliCDBRunRange& runRange, Int_t version,
517 Int_t subVersion) {
518// get an AliCDBEntry object from the database!
519
520 return Get(AliCDBId(path, runRange, version, subVersion));
521}
522
523//_____________________________________________________________________________
4667c116 524AliCDBEntry* AliCDBManager::Get(const AliCDBId& query) {
02c4845e 525// get an AliCDBEntry object from the database
526
02c4845e 527 // check if query's path and runRange are valid
528 // query is invalid also if version is not specified and subversion is!
529 if (!query.IsValid()) {
530 AliError(Form("Invalid query: %s", query.ToString().Data()));
531 return NULL;
532 }
024cf675 533
534 // query is not specified if path contains wildcard or run range= [-1,-1]
535 if (!query.IsSpecified()) {
4667c116 536 AliError(Form("Unspecified query: %s",
02c4845e 537 query.ToString().Data()));
538 return NULL;
539 }
540
1f341624 541 if(fLock && query.GetFirstRun() != fRun)
542 AliFatal("Lock is ON: cannot use different run number than the internal one!");
543
544 if(fCache && query.GetFirstRun() != fRun)
024cf675 545 AliWarning("Run number explicitly set in query: CDB cache temporarily disabled!");
546
024cf675 547 AliCDBEntry *entry=0;
8e245d15 548
024cf675 549 // first look into map of cached objects
62032124 550 if(fCache && query.GetFirstRun() == fRun)
024cf675 551 entry = (AliCDBEntry*) fEntryCache.GetValue(query.GetPath());
552
553 if(entry) {
b8ec52f6 554 AliDebug(2, Form("Object %s retrieved from cache !!",query.GetPath().Data()));
024cf675 555 return entry;
556 }
96b7636d 557
d1982b43 558 if(!fDefaultStorage) {
559 AliError("No storage set!");
560 return NULL;
561 }
96b7636d 562 // Entry is not in cache -> retrieve it from CDB and cache it!!
024cf675 563 AliCDBStorage *aStorage=0;
564 AliCDBParam *aPar=SelectSpecificStorage(query.GetPath());
1f341624 565// Bool_t usedDefStorage=kTRUE;
62032124 566
02c4845e 567 if(aPar) {
568 aStorage=GetStorage(aPar);
569 TString str = aPar->GetURI();
570 AliDebug(2,Form("Looking into storage: %s",str.Data()));
1f341624 571// usedDefStorage=kFALSE;
4667c116 572
02c4845e 573 } else {
574 aStorage=GetDefaultStorage();
96b7636d 575 AliDebug(2,"Looking into default storage");
02c4845e 576 }
4667c116 577
024cf675 578 entry = aStorage->Get(query);
024cf675 579
4667c116 580 if(entry && fCache && (query.GetFirstRun() == fRun)){
024cf675 581 CacheEntry(query.GetPath(), entry);
582 }
8e245d15 583
b21e3194 584 if(entry && !fIds->Contains(&entry->GetId())){
585 fIds->Add(entry->GetId().Clone());
586 }
587
588
024cf675 589 return entry;
4667c116 590
591}
592
0f40a831 593//_____________________________________________________________________________
594const char* AliCDBManager::GetURI(const char* path) {
595// return the URI of the storage where to look for path
596
597 if(!IsDefaultStorageSet()) return 0;
598
599 AliCDBParam *aPar=SelectSpecificStorage(path);
600
601 if(aPar) {
602 return aPar->GetURI().Data();
603
604 } else {
605 return GetDefaultStorage()->GetURI().Data();
606 }
607
608 return 0;
609}
610
4667c116 611//_____________________________________________________________________________
612AliCDBId* AliCDBManager::GetId(const AliCDBPath& path, Int_t runNumber,
613 Int_t version, Int_t subVersion) {
614// get the AliCDBId of the valid object from the database (does not retrieve the object)
615// User must delete returned object!
616
617 if(runNumber < 0){
618 // RunNumber is not specified. Try with fRun
619 if (fRun < 0){
620 AliError("Run number neither specified in query nor set in AliCDBManager! Use AliCDBManager::SetRun.");
621 return NULL;
622 }
623 runNumber = fRun;
624 }
625
626 return GetId(AliCDBId(path, runNumber, runNumber, version, subVersion));
627}
628
629//_____________________________________________________________________________
630AliCDBId* AliCDBManager::GetId(const AliCDBPath& path,
631 const AliCDBRunRange& runRange, Int_t version,
632 Int_t subVersion) {
633// get the AliCDBId of the valid object from the database (does not retrieve the object)
634// User must delete returned object!
635
636 return GetId(AliCDBId(path, runRange, version, subVersion));
637}
638
639//_____________________________________________________________________________
640AliCDBId* AliCDBManager::GetId(const AliCDBId& query) {
641// get the AliCDBId of the valid object from the database (does not retrieve the object)
642// User must delete returned object!
643
644 if(!fDefaultStorage) {
645 AliError("No storage set!");
646 return NULL;
647 }
648
649 // check if query's path and runRange are valid
650 // query is invalid also if version is not specified and subversion is!
651 if (!query.IsValid()) {
652 AliError(Form("Invalid query: %s", query.ToString().Data()));
653 return NULL;
654 }
655
656 // query is not specified if path contains wildcard or run range= [-1,-1]
657 if (!query.IsSpecified()) {
658 AliError(Form("Unspecified query: %s",
659 query.ToString().Data()));
660 return NULL;
661 }
662
663 if(fCache && query.GetFirstRun() != fRun)
664 AliWarning("Run number explicitly set in query: CDB cache temporarily disabled!");
665
666 AliCDBEntry* entry = 0;
667
668 // first look into map of cached objects
669 if(fCache && query.GetFirstRun() == fRun)
670 entry = (AliCDBEntry*) fEntryCache.GetValue(query.GetPath());
671
672 if(entry) {
673 AliDebug(2, Form("Object %s retrieved from cache !!",query.GetPath().Data()));
674 return dynamic_cast<AliCDBId*> (entry->GetId().Clone());
675 }
676
677 // Entry is not in cache -> retrieve it from CDB and cache it!!
678 AliCDBStorage *aStorage=0;
679 AliCDBParam *aPar=SelectSpecificStorage(query.GetPath());
680
681 if(aPar) {
682 aStorage=GetStorage(aPar);
683 TString str = aPar->GetURI();
684 AliDebug(2,Form("Looking into storage: %s",str.Data()));
024cf675 685
4667c116 686 } else {
687 aStorage=GetDefaultStorage();
688 AliDebug(2,"Looking into default storage");
689 }
690
691 return aStorage->GetId(query);
692
02c4845e 693}
694
695//_____________________________________________________________________________
62032124 696TList* AliCDBManager::GetAll(const AliCDBPath& path, Int_t runNumber,
02c4845e 697 Int_t version, Int_t subVersion) {
698// get multiple AliCDBEntry objects from the database
699
afd8dbf4 700 if(runNumber < 0){
701 // RunNumber is not specified. Try with fRun
702 if (fRun < 0){
703 AliError("Run number neither specified in query nor set in AliCDBManager! Use AliCDBManager::SetRun.");
704 return NULL;
705 }
706 runNumber = fRun;
707 }
708
02c4845e 709 return GetAll(AliCDBId(path, runNumber, runNumber, version,
710 subVersion));
711}
712
713//_____________________________________________________________________________
8e245d15 714TList* AliCDBManager::GetAll(const AliCDBPath& path,
02c4845e 715 const AliCDBRunRange& runRange, Int_t version, Int_t subVersion) {
716// get multiple AliCDBEntry objects from the database
717
718 return GetAll(AliCDBId(path, runRange, version, subVersion));
719}
720
721//_____________________________________________________________________________
722TList* AliCDBManager::GetAll(const AliCDBId& query) {
723// get multiple AliCDBEntry objects from the database
024cf675 724// Warning: this method works correctly only for queries of the type "Detector/*"
725// and not for more specific queries e.g. "Detector/Calib/*" !
8e245d15 726// Warning #2: Entries are cached, but GetAll will keep on retrieving objects from OCDB!
727// To get an object from cache use Get() function
02c4845e 728
729 if(!fDefaultStorage) {
730 AliError("No storage set!");
731 return NULL;
732 }
733
734 if (!query.IsValid()) {
735 AliError(Form("Invalid query: %s", query.ToString().Data()));
736 return NULL;
737 }
738
afd8dbf4 739 if((fSpecificStorages.GetEntries()>0) && query.GetPath().BeginsWith('*')){
740 // if specific storages are active a query with "*" is ambiguous
741 AliError("Query too generic in this context!");
8e245d15 742 return NULL;
02c4845e 743 }
744
745 if (query.IsAnyRange()) {
746 AliError(Form("Unspecified run or runrange: %s",
8e245d15 747 query.ToString().Data()));
02c4845e 748 return NULL;
8e245d15 749 }
750
1f341624 751 if(fLock && query.GetFirstRun() != fRun)
752 AliFatal("Lock is ON: cannot use different run number than the internal one!");
753
6dc56e97 754 AliCDBParam *aPar=SelectSpecificStorage(query.GetPath());
024cf675 755
8e245d15 756 AliCDBStorage *aStorage;
02c4845e 757 if(aPar) {
758 aStorage=GetStorage(aPar);
96b7636d 759 AliDebug(2,Form("Looking into storage: %s", aPar->GetURI().Data()));
8e245d15 760
02c4845e 761 } else {
762 aStorage=GetDefaultStorage();
8e245d15 763 AliDebug(2,"Looking into default storage");
02c4845e 764 }
765
96b7636d 766 TList *result = 0;
767 if(aStorage) result = aStorage->GetAll(query);
8e245d15 768 if(!result) return 0;
769
96b7636d 770 // loop on result to check whether entries should be re-queried with specific storages
771 if(fSpecificStorages.GetEntries()>0 && ! (fSpecificStorages.GetEntries() == 1 && aPar)) {
772 AliInfo("Now look into all other specific storages...");
773
774 TIter iter(result);
775 AliCDBEntry* chkEntry=0;
776
777 while((chkEntry = dynamic_cast<AliCDBEntry*> (iter.Next()))){
778 AliCDBId& chkId = chkEntry->GetId();
779 AliDebug(2, Form("Checking id %s ", chkId.GetPath().Data()));
780 AliCDBParam *chkPar=SelectSpecificStorage(chkId.GetPath());
781 if (!chkPar || aPar == chkPar) continue;
782 AliCDBStorage *chkStorage = GetStorage(chkPar);
783 AliDebug(2, Form("Found specific storage! %s", chkPar->GetURI().Data()));
784
785 AliCDBEntry *newEntry=0;
786 chkId.SetRunRange(query.GetFirstRun(), query.GetLastRun());
787 chkId.SetVersion(query.GetVersion());
788 chkId.SetSubVersion(query.GetSubVersion());
789
790 if(chkStorage) newEntry = chkStorage->Get(chkId);
791 if(!newEntry) continue;
792
793 // object is found in specific storage: replace entry in the result list!
794 chkEntry->SetOwner(1);
795 delete result->Remove(chkEntry);
796 result->AddFirst(newEntry);
797 }
798
799 Int_t nEntries = result->GetEntries();
800 AliInfo("After look into other specific storages, result list is:");
801 for(int i=0; i<nEntries;i++){
802 AliCDBEntry *entry = (AliCDBEntry*) result->At(i);
803 AliInfo(Form("%s",entry->GetId().ToString().Data()));
804 }
805 }
806
8e245d15 807 // caching entries
b21e3194 808 TIter iter(result);
809 AliCDBEntry* entry=0;
810 while((entry = dynamic_cast<AliCDBEntry*> (iter.Next()))){
02c4845e 811
b21e3194 812 if(!fIds->Contains(&entry->GetId())){
813 fIds->Add(entry->GetId().Clone());
814 }
815 if(fCache && (query.GetFirstRun() == fRun)){
96b7636d 816 CacheEntry(entry->GetId().GetPath(), entry);
8e245d15 817 }
818 }
819
b21e3194 820
8e245d15 821 return result;
02c4845e 822}
823
824//_____________________________________________________________________________
b8ec52f6 825Bool_t AliCDBManager::Put(TObject* object, AliCDBId& id, AliCDBMetaData* metaData, DataType type){
02c4845e 826// store an AliCDBEntry object into the database
827
968cfa08 828 if (object==0x0) {
829 AliError("Null Entry! No storage will be done!");
830 return kFALSE;
831 }
832
02c4845e 833 AliCDBEntry anEntry(object, id, metaData);
b8ec52f6 834 return Put(&anEntry, type);
02c4845e 835
836}
837
838
839//_____________________________________________________________________________
b8ec52f6 840Bool_t AliCDBManager::Put(AliCDBEntry* entry, DataType type){
02c4845e 841// store an AliCDBEntry object into the database
842
b8ec52f6 843 if(type == kPrivate && !fDefaultStorage) {
02c4845e 844 AliError("No storage set!");
845 return kFALSE;
846 }
847
968cfa08 848 if (entry->GetObject()==0x0){
849 AliError("No valid object in CDB entry!");
850 return kFALSE;
851 }
852
02c4845e 853 if (!entry){
854 AliError("No entry!");
855 return kFALSE;
856 }
857
858 if (!entry->GetId().IsValid()) {
859 AliError(Form("Invalid entry ID: %s",
860 entry->GetId().ToString().Data()));
861 return kFALSE;
862 }
863
864 if (!entry->GetId().IsSpecified()) {
865 AliError(Form("Unspecified entry ID: %s",
866 entry->GetId().ToString().Data()));
867 return kFALSE;
868 }
869
870 AliCDBId id = entry->GetId();
024cf675 871 AliCDBParam *aPar = SelectSpecificStorage(id.GetPath());
872
4005d0b5 873 AliCDBStorage *aStorage=0;
02c4845e 874
875 if(aPar) {
876 aStorage=GetStorage(aPar);
02c4845e 877 } else {
b8ec52f6 878 switch(type){
879 case kCondition:
880 aStorage = GetStorage(fCondParam);
881 break;
882 case kReference:
883 aStorage = GetStorage(fRefParam);
884 break;
885 case kPrivate:
886 aStorage = GetDefaultStorage();
887 break;
888 }
02c4845e 889 }
890
b8ec52f6 891 AliDebug(2,Form("Storing object into storage: %s", aStorage->GetURI().Data()));
892
4005d0b5 893 Bool_t result = aStorage->Put(entry, type);
894
895 if(fRun >= 0) QueryCDB();
896
897 return result;
02c4845e 898
899
900}
9e1ceb13 901
4b5e0dce 902//_____________________________________________________________________________
903void AliCDBManager::CacheEntry(const char* path, AliCDBEntry* entry)
904{
905// cache AliCDBEntry. Cache is valid until run number is changed.
906
96b7636d 907 AliCDBEntry *chkEntry = dynamic_cast<AliCDBEntry*> (fEntryCache.GetValue(path));
908
909 if(chkEntry) {
910 AliDebug(2, Form("Object %s already in cache !!", path));
911 return;
912 } else {
913 AliDebug(2,Form("Caching entry %s", path));
914 }
915
4b5e0dce 916 fEntryCache.Add(new TObjString(path), entry);
96b7636d 917 AliDebug(2,Form("Cache entries: %d", fEntryCache.GetEntries()));
4b5e0dce 918
919}
920
917a098b 921//_____________________________________________________________________________
922void AliCDBManager::Print(Option_t* /*option*/) const
923{
924// Print list of active storages and their URIs
917a098b 925
b8ec52f6 926 TString output=Form("Run number = %d; ",fRun);
927 output += "Cache is ";
917a098b 928 if(!fCache) output += "NOT ";
b8ec52f6 929 output += Form("ACTIVE; Number of active storages: %d\n",fActiveStorages.GetEntries());
917a098b 930
931 if(fDefaultStorage) {
b8ec52f6 932 output += Form("\t*** Default Storage URI: \"%s\"\n",fDefaultStorage->GetURI().Data());
933// AliInfo(output.Data());
917a098b 934 }
935 if(fSpecificStorages.GetEntries()>0) {
917a098b 936 TIter iter(fSpecificStorages.GetTable());
62032124 937 TPair *aPair=0;
b8ec52f6 938 Int_t i=1;
917a098b 939 while((aPair = (TPair*) iter.Next())){
b8ec52f6 940 output += Form("\t*** Specific storage %d: Path \"%s\" -> URI \"%s\"\n",
941 i++, ((TObjString*) aPair->Key())->GetName(),
917a098b 942 ((AliCDBParam*) aPair->Value())->GetURI().Data());
917a098b 943 }
917a098b 944 }
945 if(fDrainStorage) {
b8ec52f6 946 output += Form("*** Drain Storage URI: %s\n",fDrainStorage->GetURI().Data());
917a098b 947 }
b8ec52f6 948 AliInfo(output.Data());
917a098b 949}
950
4b5e0dce 951//_____________________________________________________________________________
c3a7b59a 952void AliCDBManager::SetRun(Int_t run)
4b5e0dce 953{
917a098b 954// Sets current run number.
4b5e0dce 955// When the run number changes the caching is cleared.
1f341624 956
4b5e0dce 957 if (fRun == run)
958 return;
959
1f341624 960 if(fLock && fRun >= 0) {
961 AliFatal("Lock is ON, cannot reset run number!");
962 }
963
4b5e0dce 964 fRun = run;
965 ClearCache();
62032124 966 QueryCDB();
4b5e0dce 967}
968
969//_____________________________________________________________________________
970void AliCDBManager::ClearCache(){
971// clear AliCDBEntry cache
972
1f341624 973 AliDebug(2, Form("Cache entries to be deleted: %d",fEntryCache.GetEntries()));
974
975 /*
976 // To clean entries one by one
977 TIter iter(fEntryCache.GetTable());
978 TPair* pair=0;
979 while((pair= dynamic_cast<TPair*> (iter.Next()))){
980
981 TObjString* key = dynamic_cast<TObjString*> (pair->Key());
982 AliCDBEntry* entry = dynamic_cast<AliCDBEntry*> (pair->Value());
983 AliDebug(2, Form("Deleting entry: %s", key->GetName()));
984 if (entry) delete entry;
985 delete fEntryCache.Remove(key);
986 }
987 */
4b5e0dce 988 fEntryCache.DeleteAll();
1f341624 989 AliDebug(2, Form("After deleting - Cache entries: %d",fEntryCache.GetEntries()));
4b5e0dce 990}
991
4005d0b5 992//_____________________________________________________________________________
993void AliCDBManager::UnloadFromCache(const char* path){
994// unload cached object
995
996 AliCDBPath queryPath(path);
997 if(!queryPath.IsValid()) return;
998
999 if(!queryPath.IsWildcard()) { // path is not wildcard, get it directly from the cache and unload it!
1000 if(fEntryCache.Contains(path)){
1001 AliInfo(Form("Unloading object \"%s\" from cache", path));
1002 TObjString pathStr(path);
1003 AliCDBEntry *entry = dynamic_cast<AliCDBEntry*> (fEntryCache.GetValue(&pathStr));
1004 if(entry) delete entry;
1005 delete fEntryCache.Remove(&pathStr);
1006 } else {
1007 AliError(Form("Cache does not contain object \"%s\"!", path))
1008 }
1009 AliDebug(2,Form("Cache entries: %d",fEntryCache.GetEntries()));
1010 return;
1011 }
1012
1013 // path is wildcard: loop on the cache and unload all comprised objects!
1014 TIter iter(fEntryCache.GetTable());
1015 TPair* pair = 0;
1016
1017 while((pair = dynamic_cast<TPair*> (iter.Next()))){
1018 AliCDBPath entryPath = pair->Key()->GetName();
1019 if(queryPath.Comprises(entryPath)) {
1020 AliInfo(Form("Unloading object \"%s\" from cache", entryPath.GetPath().Data()));
1021 TObjString pathStr(entryPath.GetPath().Data());
1022 AliCDBEntry *entry = dynamic_cast<AliCDBEntry*> (fEntryCache.GetValue(&pathStr));
1023 if(entry) delete entry;
1024 delete fEntryCache.Remove(&pathStr);
1025 }
1026 }
1027 AliDebug(2,Form("Cache entries: %d",fEntryCache.GetEntries()));
1028}
1029
9e1ceb13 1030//_____________________________________________________________________________
1031void AliCDBManager::DestroyActiveStorages() {
1032// delete list of active storages
1033
1034 fActiveStorages.DeleteAll();
02c4845e 1035 fSpecificStorages.DeleteAll();
9e1ceb13 1036}
1037
1038//_____________________________________________________________________________
1039void AliCDBManager::DestroyActiveStorage(AliCDBStorage* /*storage*/) {
b05400be 1040// destroys active storage
1041
1042/*
917a098b 1043 TIter iter(fActiveStorages.GetTable());
b05400be 1044 TPair* aPair;
1045 while ((aPair = (TPair*) iter.Next())) {
1046 if(storage == (AliCDBStorage*) aPair->Value())
1047 delete fActiveStorages.Remove(aPair->Key());
1048 storage->Delete(); storage=0x0;
1049 }
6dc56e97 1050*/
9e1ceb13 1051
1052}
1053
62032124 1054//_____________________________________________________________________________
1055void AliCDBManager::QueryCDB() {
1056// query default and specific storages for files valid for fRun. Every storage loads the Ids into its list.
1057
1058 if (fRun < 0){
1059 AliError("Run number not yet set! Use AliCDBManager::SetRun.");
1060 return;
1061 }
6dc56e97 1062 if (!fDefaultStorage){
1063 AliError("Default storage is not set! Use AliCDBManager::SetDefaultStorage");
1064 return;
1065 }
1066 if(fDefaultStorage->GetType() == "alien"){
1067 fDefaultStorage->QueryCDB(fRun);
1068 } else {
1069 AliDebug(2,"Skipping query for valid files, it used only in grid...");
1070 }
62032124 1071
1072 TIter iter(&fSpecificStorages);
1073 TObjString *aCalibType=0;
1074 AliCDBParam* aPar=0;
1075 while((aCalibType = dynamic_cast<TObjString*> (iter.Next()))){
1076 aPar = (AliCDBParam*) fSpecificStorages.GetValue(aCalibType);
1077 if(aPar) {
c3a7b59a 1078 AliDebug(2,Form("Querying specific storage %s",aCalibType->GetName()));
6dc56e97 1079 AliCDBStorage *aStorage = GetStorage(aPar);
1080 if(aStorage->GetType() == "alien"){
1081 aStorage->QueryCDB(fRun,aCalibType->GetName());
1082 } else {
1083 AliDebug(2,
1084 "Skipping query for valid files, it is used only in grid...");
1085 }
62032124 1086 }
1087 }
62032124 1088}
1089
b8ec52f6 1090//______________________________________________________________________________________________
1091const char* AliCDBManager::GetDataTypeName(DataType type)
1092{
1093 // returns the name (string) of the data type
1094
1095 switch (type){
1096 case kCondition: return "Conditions";
1097 case kReference: return "Reference";
1098 case kPrivate: return "Private";
1099 }
1100 return 0;
1101
1102}
62032124 1103
4667c116 1104//______________________________________________________________________________________________
1105void AliCDBManager::InitShortLived()
1106{
1107 // Init the list of short-lived objects
1108 // currently disabled
1109
1110 fShortLived=0x0;
1111
1112// fShortLived = new TList();
1113// fShortLived->SetOwner(1);
1114//
1115// fShortLived->Add(new TObjString("EMCAL/Calib/Data"));
1116//
1117// fShortLived->Add(new TObjString("HMPID/Calib/Nmean"));
1118// fShortLived->Add(new TObjString("HMPID/Calib/Qthre"));
1119//
1120// fShortLived->Add(new TObjString("ITS/Calib/CalibSPD"));
1121//
1122// fShortLived->Add(new TObjString("MUON/Calib/Gains"));
1123// fShortLived->Add(new TObjString("MUON/Calib/HV"));
1124// fShortLived->Add(new TObjString("MUON/Calib/Pedestals"));
1125//
1126// fShortLived->Add(new TObjString("PHOS/Calib/CpvGainPedestals"));
1127// fShortLived->Add(new TObjString("PHOS/Calib/EmcGainPedestals"));
1128//
1129// fShortLived->Add(new TObjString("PMD/Calib/Data"));
1130//
1131// fShortLived->Add(new TObjString("TRD/Calib/ChamberGainFactor"));
1132// fShortLived->Add(new TObjString("TRD/Calib/LocalGainFactor"));
1133// fShortLived->Add(new TObjString("TRD/Calib/ChamberT0"));
1134// fShortLived->Add(new TObjString("TRD/Calib/LocalT0"));
1135// fShortLived->Add(new TObjString("TRD/Calib/ChamberVdrift"));
1136// fShortLived->Add(new TObjString("TRD/Calib/LocalVdrift"));
1137//
1138// fShortLived->Add(new TObjString("ZDC/Calib/Data"));
1139
1140}
1141
1142//______________________________________________________________________________________________
1143Bool_t AliCDBManager::IsShortLived(const char* path)
1144{
1145 // returns the name (string) of the data type
1146
1147 if(!fShortLived) return kFALSE;
1148
1149 AliCDBPath aPath(path);
1150 if(!aPath.IsValid()){
1151 AliError(Form("Not a valid path: %s", path));
1152 return kFALSE;
1153 }
1154
1155 return fShortLived->Contains(path);
1156
1157}
1158
1f341624 1159//______________________________________________________________________________________________
1160void AliCDBManager::SetLock(Bool_t lock){
1161
1162 if(fLock == kTRUE && lock == kFALSE) {
1163 AliFatal("Lock is ON: cannot reset it!");
1164 }
1165
1166 fLock=lock;
1167}
1168
9e1ceb13 1169///////////////////////////////////////////////////////////
1170// AliCDBManager Parameter class //
1171// interface to specific AliCDBParameter class //
1172// (AliCDBGridParam, AliCDBLocalParam, AliCDBDumpParam) //
1173///////////////////////////////////////////////////////////
1174
62032124 1175AliCDBParam::AliCDBParam():
fe12e09c 1176 fType(),
1177 fURI()
62032124 1178{
9e1ceb13 1179// constructor
1180
1181}
1182
1183//_____________________________________________________________________________
1184AliCDBParam::~AliCDBParam() {
1185// destructor
1186
1187}
1188