]> git.uio.no Git - u/mrichter/AliRoot.git/blame - STEER/CDB/AliCDBManager.cxx
Let GetAll be aware of specific version
[u/mrichter/AliRoot.git] / STEER / CDB / 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
5078a13f 17// Author: Alberto Colla
fdf65bb5 18// e-mail: Alberto.Colla@cern.ch
19//-------------------------------------------------------------------------
9e1ceb13 20
5078a13f 21#include <fstream>
c11cdcf2 22
9e1ceb13 23#include "AliCDBManager.h"
24#include "AliCDBStorage.h"
25#include "AliLog.h"
26#include "AliCDBDump.h"
27#include "AliCDBLocal.h"
28#include "AliCDBGrid.h"
02c4845e 29#include "AliCDBEntry.h"
7de84b33 30#include "AliCDBHandler.h"
9e1ceb13 31
02c4845e 32#include <TObjString.h>
7de84b33 33#include <TSAXParser.h>
34#include <TFile.h>
647d2bb8 35#include <TKey.h>
7de84b33 36#include <TUUID.h>
27cac7e3 37#include <TGrid.h>
a37a1dfc 38#include "TMessage.h"
39#include "TObject.h"
5078a13f 40#include "TRegexp.h"
9e1ceb13 41
42ClassImp(AliCDBParam)
43
44ClassImp(AliCDBManager)
45
b8ec52f6 46//TODO OCDB and Reference folder should not be fully hardcoded but built from run number (or year/LHC period)
47TString AliCDBManager::fgkCondUri("alien://folder=/alice/cern.ch/user/a/aliprod/testCDB/CDB?user=aliprod");
48TString AliCDBManager::fgkRefUri("alien://folder=/alice/cern.ch/user/a/aliprod/testCDB/Reference?user=aliprod");
7de84b33 49TString AliCDBManager::fgkMCIdealStorage("alien://folder=/alice/simulation/2008/v4-15-Release/Ideal");
50TString AliCDBManager::fgkMCFullStorage("alien://folder=/alice/simulation/2008/v4-15-Release/Full");
51TString AliCDBManager::fgkMCResidualStorage("alien://folder=/alice/simulation/2008/v4-15-Release/Residual");
52TString AliCDBManager::fgkOCDBFolderXMLfile("alien:///alice/data/OCDBFoldervsRunRange.xml");
9e1ceb13 53AliCDBManager* AliCDBManager::fgInstance = 0x0;
54
55//_____________________________________________________________________________
5078a13f 56AliCDBManager* AliCDBManager::Instance(TMap *entryCache, Int_t run) {
9e1ceb13 57// returns AliCDBManager instance (singleton)
58
5078a13f 59 if (!fgInstance) {
60 fgInstance = new AliCDBManager();
61 if (!entryCache)
62 fgInstance->Init();
63 else
64 fgInstance->InitFromCache(entryCache,run);
65 }
9e1ceb13 66
5078a13f 67 return fgInstance;
9e1ceb13 68}
69
70//_____________________________________________________________________________
71void AliCDBManager::Init() {
72// factory registering
73
5078a13f 74 RegisterFactory(new AliCDBDumpFactory());
75 RegisterFactory(new AliCDBLocalFactory());
76 // AliCDBGridFactory is registered only if AliEn libraries are enabled in Root
77 if(!gSystem->Exec("root-config --has-alien 2>/dev/null |grep yes 2>&1 > /dev/null")){ // returns 0 if yes
78 AliInfo("AliEn classes enabled in Root. AliCDBGrid factory registered.");
79 RegisterFactory(new AliCDBGridFactory());
80 fCondParam = CreateParameter(fgkCondUri);
81 fRefParam = CreateParameter(fgkRefUri);
82 }
4667c116 83
5078a13f 84 InitShortLived();
9e1ceb13 85}
d1982b43 86
87//_____________________________________________________________________________
88void AliCDBManager::InitFromCache(TMap *entryCache, Int_t run) {
89// initialize manager from existing cache
90// used on the slaves in case of parallel reconstruction
91 SetRun(run);
92
93 TIter iter(entryCache->GetTable());
94 TPair* pair = 0;
95
96 while((pair = dynamic_cast<TPair*> (iter.Next()))){
97 fEntryCache.Add(pair->Key(),pair->Value());
98 }
99 // fEntry is the new owner of the cache
100 fEntryCache.SetOwnerKeyValue(kTRUE,kTRUE);
77d2efce 101 entryCache->SetOwnerKeyValue(kFALSE,kFALSE);
d1982b43 102 AliInfo(Form("%d cache entries have been loaded",fEntryCache.GetEntries()));
103}
104
7ec0d3df 105//_____________________________________________________________________________
18d58637 106void AliCDBManager::DumpToSnapshotFile(const char* snapshotFileName, Bool_t singleKeys) const {
5078a13f 107//
e973a743 108// If singleKeys is true, dump the entries map and the ids list to the snapshot file
109// (provided mostly for historical reasons, the file is then read with InitFromSnapshot),
110// otherwise write to file each AliCDBEntry separately (the is the preferred way, the file
111// is then read with SetSnapshotMode).
7ec0d3df 112
5078a13f 113 // open the file
114 TFile *f = TFile::Open(snapshotFileName,"RECREATE");
115 if (!f || f->IsZombie()){
116 AliError(Form("Cannot open file %s",snapshotFileName));
117 return;
118 }
7ec0d3df 119
5078a13f 120 AliInfo(Form("Dumping entriesMap (entries'cache) with %d entries!\n", fEntryCache.GetEntries()));
121 AliInfo(Form("Dumping entriesList with %d entries!\n", fIds->GetEntries()));
122
123 f->cd();
e973a743 124 if(singleKeys){
18d58637 125 f->WriteObject(&fEntryCache,"CDBentriesMap");
126 f->WriteObject(fIds,"CDBidsList");
e973a743 127 }else{
5078a13f 128 // We write the entries one by one named by their calibration path
5078a13f 129 TIter iter(fEntryCache.GetTable());
130 TPair* pair = 0;
131 while((pair = dynamic_cast<TPair*> (iter.Next()))){
132 TObjString *os = dynamic_cast<TObjString*>(pair->Key());
133 if (!os) continue;
134 TString path = os->GetString();
135 AliCDBEntry *entry = dynamic_cast<AliCDBEntry*>(pair->Value());
136 if (!entry) continue;
137 path.ReplaceAll("/","*");
138 entry->Write(path.Data());
c022b397 139 }
5078a13f 140 }
141 f->Close();
142 delete f;
7ec0d3df 143}
144
18d58637 145//_____________________________________________________________________________
146void AliCDBManager::DumpToLightSnapshotFile(const char* lightSnapshotFileName) const {
147// The light snapshot does not contain the CDB objects (AliCDBEntries) but
148// only the information identifying them, that is the map of storages and
149// the list of AliCDBIds, as in the UserInfo of AliESDs.root
150
151 // open the file
152 TFile *f = TFile::Open(lightSnapshotFileName,"RECREATE");
153 if (!f || f->IsZombie()){
154 AliError(Form("Cannot open file %s",lightSnapshotFileName));
155 return;
156 }
157
158 AliInfo(Form("Dumping map of storages with %d entries!\n", fStorageMap->GetEntries()));
159 AliInfo(Form("Dumping entriesList with %d entries!\n", fIds->GetEntries()));
160 f->WriteObject(fStorageMap,"cdbStoragesMap");
161 f->WriteObject(fIds,"CDBidsList");
162
163 f->Close();
164 delete f;
165}
166
647d2bb8 167//_____________________________________________________________________________
b0e8cda6 168Bool_t AliCDBManager::InitFromSnapshot(const char* snapshotFileName, Bool_t overwrite){
647d2bb8 169// initialize manager from a CDB snapshot, that is add the entries
170// to the entries map and the ids to the ids list taking them from
171// the map and the list found in the input file
172
5078a13f 173 // if the manager is locked it cannot initialize from a snapshot
174 if(fLock) {
175 AliError("Being locked I cannot initialize from the snapshot!");
176 return kFALSE;
177 }
178
179 // open the file
180 TString snapshotFile(snapshotFileName);
181 if(snapshotFile.BeginsWith("alien://")){
182 if(!gGrid) {
183 TGrid::Connect("alien://","");
184 if(!gGrid) {
185 AliError("Connection to alien failed!");
186 return kFALSE;
187 }
647d2bb8 188 }
5078a13f 189 }
647d2bb8 190
5078a13f 191 TFile *f = TFile::Open(snapshotFileName);
192 if (!f || f->IsZombie()){
193 AliError(Form("Cannot open file %s",snapshotFileName));
194 return kFALSE;
195 }
647d2bb8 196
5078a13f 197 // retrieve entries' map from snapshot file
198 TMap *entriesMap = 0;
199 TIter next(f->GetListOfKeys());
200 TKey *key;
201 while ((key = (TKey*)next())) {
202 if (strcmp(key->GetClassName(),"TMap") != 0) continue;
203 entriesMap = (TMap*)key->ReadObj();
204 break;
205 }
206 if (!entriesMap || entriesMap->GetEntries()==0){
207 AliError("Cannot get valid map of CDB entries from snapshot file");
208 return kFALSE;
209 }
210
211 // retrieve ids' list from snapshot file
212 TList *idsList = 0;
213 TIter nextKey(f->GetListOfKeys());
214 TKey *keyN;
215 while ((keyN = (TKey*)nextKey())) {
216 if (strcmp(keyN->GetClassName(),"TList") != 0) continue;
217 idsList = (TList*)keyN->ReadObj();
218 break;
219 }
220 if (!idsList || idsList->GetEntries()==0){
221 AliError("Cannot get valid list of CDB entries from snapshot file");
222 return kFALSE;
223 }
647d2bb8 224
5078a13f 225 // Add each (entry,id) from the snapshot to the memory: entry to the cache, id to the list of ids.
226 // If "overwrite" is false: add the entry to the cache and its id to the list of ids
227 // only if neither of them is already there.
228 // If "overwrite" is true: write the snapshot entry,id in any case. If something
229 // was already there for that calibration type, remove it and issue a warning
230 TIter iterObj(entriesMap->GetTable());
231 TPair* pair = 0;
232 Int_t nAdded=0;
233 while((pair = dynamic_cast<TPair*> (iterObj.Next()))){
234 TObjString* os = (TObjString*) pair->Key();
235 TString path = os->GetString();
236 TIter iterId(idsList);
237 AliCDBId* id=0;
238 AliCDBId* correspondingId=0;
239 while((id = dynamic_cast<AliCDBId*> (iterId.Next()))){
240 TString idpath(id->GetPath());
241 if(idpath==path){
242 correspondingId=id;
243 break;
244 }
647d2bb8 245 }
5078a13f 246 if(!correspondingId){
247 AliError(Form("id for \"%s\" not found in the snapshot (while entry was). This entry is skipped!",path.Data()));
248 break;
249 }
250 Bool_t cached = fEntryCache.Contains(path.Data());
251 Bool_t registeredId = kFALSE;
252 TIter iter(fIds);
253 AliCDBId *idT = 0;
254 while((idT = dynamic_cast<AliCDBId*> (iter.Next()))){
255 if(idT->GetPath()==path){
256 registeredId = kTRUE;
257 break;
258 }
647d2bb8 259 }
260
5078a13f 261 if(overwrite){
262 if(cached || registeredId){
263 AliWarning(Form("An entry was already cached for \"%s\". Removing it before caching from snapshot",path.Data()));
264 UnloadFromCache(path.Data());
265 }
266 fEntryCache.Add(pair->Key(),pair->Value());
267 fIds->Add(id);
268 nAdded++;
269 }else{
270 if(cached || registeredId){
271 AliWarning(Form("An entry was already cached for \"%s\". Not adding this object from snapshot",path.Data()));
272 }else{
273 fEntryCache.Add(pair->Key(),pair->Value());
274 fIds->Add(id);
275 nAdded++;
276 }
647d2bb8 277 }
5078a13f 278 }
b0e8cda6 279
5078a13f 280 // fEntry is the new owner of the cache
281 fEntryCache.SetOwnerKeyValue(kTRUE,kTRUE);
282 entriesMap->SetOwnerKeyValue(kFALSE,kFALSE);
283 fIds->SetOwner(kTRUE);
284 idsList->SetOwner(kFALSE);
285 AliInfo(Form("%d new (entry,id) cached. Total number %d",nAdded,fEntryCache.GetEntries()));
b0e8cda6 286
5078a13f 287 f->Close();
288 delete f;
647d2bb8 289
5078a13f 290 return kTRUE;
647d2bb8 291}
292
9e1ceb13 293//_____________________________________________________________________________
294void AliCDBManager::Destroy() {
295// delete ALCDBManager instance and active storages
296
5078a13f 297 if (fgInstance) {
298 //fgInstance->Delete();
299 delete fgInstance;
300 fgInstance = 0x0;
301 }
9e1ceb13 302}
303
304//_____________________________________________________________________________
305AliCDBManager::AliCDBManager():
fe12e09c 306 TObject(),
307 fFactories(),
308 fActiveStorages(),
309 fSpecificStorages(),
b21e3194 310 fEntryCache(),
311 fIds(0),
312 fStorageMap(0),
313 fShortLived(0),
fe12e09c 314 fDefaultStorage(NULL),
315 fDrainStorage(NULL),
b21e3194 316 fCondParam(0),
317 fRefParam(0),
4667c116 318 fRun(-1),
1f341624 319 fCache(kTRUE),
7de84b33 320 fLock(kFALSE),
c022b397 321 fSnapshotMode(kFALSE),
322 fSnapshotFile(0),
67522875 323 fOCDBUploadMode(kFALSE),
7de84b33 324 fRaw(kFALSE),
5078a13f 325 fCvmfsOcdb(""),
7de84b33 326 fStartRunLHCPeriod(-1),
327 fEndRunLHCPeriod(-1),
a6f59240 328 fLHCPeriod(""),
329 fKey(0)
9e1ceb13 330{
5078a13f 331 // default constuctor
332 fFactories.SetOwner(1);
333 fActiveStorages.SetOwner(1);
334 fSpecificStorages.SetOwner(1);
335 fEntryCache.SetName("CDBEntryCache");
336 fEntryCache.SetOwnerKeyValue(kTRUE,kTRUE);
b21e3194 337
5078a13f 338 fStorageMap = new TMap();
339 fStorageMap->SetOwner(1);
340 fIds = new TList();
341 fIds->SetOwner(1);
9e1ceb13 342}
343
344//_____________________________________________________________________________
345AliCDBManager::~AliCDBManager() {
346// destructor
5078a13f 347 ClearCache();
348 DestroyActiveStorages();
349 fFactories.Delete();
350 fDrainStorage = 0x0;
351 fDefaultStorage = 0x0;
352 delete fStorageMap; fStorageMap = 0;
353 delete fIds; fIds = 0;
354 delete fCondParam;
355 delete fRefParam;
356 delete fShortLived; fShortLived = 0x0;
357 //fSnapshotCache = 0;
358 //fSnapshotIdsList = 0;
359 if(fSnapshotMode){
360 fSnapshotFile->Close();
361 fSnapshotFile = 0;
362 }
9e1ceb13 363}
364
9e1ceb13 365//_____________________________________________________________________________
366void AliCDBManager::PutActiveStorage(AliCDBParam* param, AliCDBStorage* storage){
367// put a storage object into the list of active storages
368
5078a13f 369 fActiveStorages.Add(param, storage);
370 AliDebug(1, Form("Active storages: %d", fActiveStorages.GetEntries()));
9e1ceb13 371}
372
373//_____________________________________________________________________________
374void AliCDBManager::RegisterFactory(AliCDBStorageFactory* factory) {
375// add a storage factory to the list of registerd factories
5078a13f 376
377 if (!fFactories.Contains(factory)) {
378 fFactories.Add(factory);
379 }
9e1ceb13 380}
381
382//_____________________________________________________________________________
fdf65bb5 383Bool_t AliCDBManager::HasStorage(const char* dbString) const {
b21e3194 384// check if dbString is a URI valid for one of the registered factories
9e1ceb13 385
5078a13f 386 TIter iter(&fFactories);
9e1ceb13 387
5078a13f 388 AliCDBStorageFactory* factory=0;
389 while ((factory = (AliCDBStorageFactory*) iter.Next())) {
9e1ceb13 390
5078a13f 391 if (factory->Validate(dbString)) {
392 return kTRUE;
393 }
394 }
9e1ceb13 395
5078a13f 396 return kFALSE;
9e1ceb13 397}
398
399//_____________________________________________________________________________
fdf65bb5 400AliCDBParam* AliCDBManager::CreateParameter(const char* dbString) const {
9e1ceb13 401// create AliCDBParam object from URI string
402
5078a13f 403 TString uriString(dbString);
404
4dcc40d2 405 if ( !fCvmfsOcdb.IsNull() && uriString.BeginsWith("alien://")) {
5078a13f 406 AlienToCvmfsUri(uriString);
407 }
408
409 TIter iter(&fFactories);
7de84b33 410
5078a13f 411 AliCDBStorageFactory* factory=0;
412 while ((factory = (AliCDBStorageFactory*) iter.Next())) {
5078a13f 413 AliCDBParam* param = factory->CreateParameter(uriString);
414 if(param) return param;
415 }
416
417 return NULL;
418}
9e1ceb13 419
5078a13f 420//_____________________________________________________________________________
421void AliCDBManager::AlienToCvmfsUri(TString& uriString) const {
422// convert alien storage uri to local:///cvmfs storage uri (called when OCDB_PATH is set)
423
5078a13f 424 TObjArray *arr = uriString.Tokenize('?');
425 TIter iter(arr);
426 TObjString *str = 0;
427 TString entryKey = "";
428 TString entryValue = "";
429 TString newUriString = "";
430 while((str = (TObjString*) iter.Next())){
431 TString entry(str->String());
432 Int_t indeq = entry.Index('=');
433 entryKey = entry(0, indeq+1);
434 entryValue = entry(indeq+1, entry.Length()-indeq);
435
436 if ( entryKey.Contains("folder", TString::kIgnoreCase) )
437 {
438
439 TRegexp re_RawFolder("^/alice/data/20[0-9]+/OCDB");
440 TRegexp re_MCFolder("^/alice/simulation/2008/v4-15-Release");
441 TString rawFolder = entryValue(re_RawFolder);
442 TString mcFolder = entryValue(re_MCFolder);
443 if ( !rawFolder.IsNull() ){
37286873 444 entryValue.Replace(0, 6, "/cvmfs/alice-ocdb.cern.ch/calibration");
7ab8651a 445 //entryValue.Replace(entryValue.Length()-4, entryValue.Length(), "");
5078a13f 446 } else if ( !mcFolder.IsNull() ){
37286873 447 entryValue.Replace(0,36,"/cvmfs/alice-ocdb.cern.ch/calibration/MC");
5078a13f 448 } else {
449 AliFatal(Form("Environment variable for cvmfs OCDB folder set for an invalid OCDB storage:\n %s", entryValue.Data()));
450 }
451 } else {
452 newUriString += entryKey;
453 }
454 newUriString += entryValue;
455 newUriString += '?';
456 }
457 newUriString.Prepend("local://");
458 newUriString.Remove(TString::kTrailing, '?');
459 uriString = newUriString;
9e1ceb13 460}
461
462//_____________________________________________________________________________
463AliCDBStorage* AliCDBManager::GetStorage(const char* dbString) {
112ba4b4 464// Get the CDB storage corresponding to the URI string passed as argument
465// If "raw://" is passed, get the storage for the raw OCDB for the current run (fRun)
9e1ceb13 466
112ba4b4 467 TString uriString(dbString);
468 if (uriString.EqualTo("raw://")) {
469 if (!fLHCPeriod.IsNull() && !fLHCPeriod.IsWhitespace()) {
470 return GetDefaultStorage();
471 } else {
472 TString lhcPeriod("");
473 Int_t startRun = -1, endRun = -1;
474 GetLHCPeriodAgainstAlienFile(fRun, lhcPeriod, startRun, endRun);
475 return GetStorage(lhcPeriod.Data());
476 }
477 }
5078a13f 478
479 AliCDBParam* param = CreateParameter(dbString);
480 if (!param) {
481 AliError(Form("Failed to activate requested storage! Check URI: %s", dbString));
482 return NULL;
483 }
484
485 AliCDBStorage* aStorage = GetStorage(param);
9e1ceb13 486
5078a13f 487 delete param;
488 return aStorage;
9e1ceb13 489}
490
491//_____________________________________________________________________________
492AliCDBStorage* AliCDBManager::GetStorage(const AliCDBParam* param) {
493// get storage object from AliCDBParam object
5078a13f 494
495 // if the list of active storages already contains
496 // the requested storage, return it
497 AliCDBStorage* aStorage = GetActiveStorage(param);
498 if (aStorage) {
499 return aStorage;
500 }
501
502 // if lock is ON, cannot activate more storages!
503 if(fLock) {
504 if (fDefaultStorage) {
505 AliFatal("Lock is ON, and default storage is already set: "
506 "cannot reset it or activate more storages!");
507 }
508 }
509
510 // loop on the list of registered factories
511 TIter iter(&fFactories);
512 AliCDBStorageFactory* factory=0;
513 while ((factory = (AliCDBStorageFactory*) iter.Next())) {
514
515 // each factory tries to create its storage from the parameter
516 aStorage = factory->Create(param);
517 if (aStorage) {
518 PutActiveStorage(param->CloneParam(), aStorage);
519 aStorage->SetURI(param->GetURI());
520 if(fRun >= 0) {
521 if( aStorage->GetType() == "alien" || aStorage->GetType() == "local" )
522 aStorage->QueryCDB(fRun);
7ab8651a 523 }
78f84046 524 return aStorage;
7ab8651a 525 }
5078a13f 526 }
5078a13f 527
7ab8651a 528 AliError(Form("Failed to activate requested storage! Check URI: %s", param->GetURI().Data()));
5078a13f 529
7ab8651a 530 return NULL;
9e1ceb13 531}
532
62032124 533//_____________________________________________________________________________
534AliCDBStorage* AliCDBManager::GetActiveStorage(const AliCDBParam* param) {
535// get a storage object from the list of active storages
536
5078a13f 537 return dynamic_cast<AliCDBStorage*> (fActiveStorages.GetValue(param));
62032124 538}
539
9e1ceb13 540//_____________________________________________________________________________
541TList* AliCDBManager::GetActiveStorages() {
542// return list of active storages
62032124 543// user has responsibility to delete returned object
9e1ceb13 544
5078a13f 545 TList* result = new TList();
9e1ceb13 546
5078a13f 547 TIter iter(fActiveStorages.GetTable());
548 TPair* aPair=0;
549 while ((aPair = (TPair*) iter.Next())) {
550 result->Add(aPair->Value());
551 }
9e1ceb13 552
5078a13f 553 return result;
9e1ceb13 554}
555
556//_____________________________________________________________________________
557void AliCDBManager::SetDrain(const char* dbString) {
558// set drain storage from URI string
559
5078a13f 560 fDrainStorage = GetStorage(dbString);
9e1ceb13 561}
562
563//_____________________________________________________________________________
564void AliCDBManager::SetDrain(const AliCDBParam* param) {
565// set drain storage from AliCDBParam
b8ec52f6 566
5078a13f 567 fDrainStorage = GetStorage(param);
9e1ceb13 568}
569
570//_____________________________________________________________________________
571void AliCDBManager::SetDrain(AliCDBStorage* storage) {
572// set drain storage from another active storage
5078a13f 573
574 fDrainStorage = storage;
9e1ceb13 575}
576
577//_____________________________________________________________________________
578Bool_t AliCDBManager::Drain(AliCDBEntry *entry) {
579// drain retrieved object to drain storage
580
5078a13f 581 AliDebug(2, "Draining into drain storage...");
582 return fDrainStorage->Put(entry);
9e1ceb13 583}
584
67522875 585//____________________________________________________________________________
586Bool_t AliCDBManager::SetOCDBUploadMode() {
587// Set the framework in official upload mode. This tells the framework to upload
588// objects to cvmfs after they have been uploaded to AliEn OCDBs.
589// It return false if the executable to upload to cvmfs is not found.
590
591 TString cvmfsUploadExecutable("$HOME/bin/ocdb-cvmfs");
592 gSystem->ExpandPathName(cvmfsUploadExecutable);
593 if ( gSystem->AccessPathName(cvmfsUploadExecutable) )
594 return kFALSE;
595 fOCDBUploadMode = kTRUE;
596 return kTRUE;
597}
598
7de84b33 599//____________________________________________________________________________
5078a13f 600void AliCDBManager::SetDefaultStorage(const char* storageUri) {
9e1ceb13 601// sets default storage from URI string
5078a13f 602
603 // if in the cvmfs case (triggered by environment variable) check for path validity
604 // and modify Uri if it is "raw://"
605 TString cvmfsOcdb(gSystem->Getenv("OCDB_PATH"));
606 if (! cvmfsOcdb.IsNull()){
607 fCvmfsOcdb = cvmfsOcdb;
608 ValidateCvmfsCase();
609 }
610
611 // checking whether we are in the raw case
612 TString uriTemp(storageUri);
613 if (uriTemp == "raw://") {
614 fRaw = kTRUE; // read then by SetRun to check if the method has to be called again with expanded uri
615 AliInfo("Setting the run-number will set the corresponding OCDB for raw data reconstruction.");
616 return;
617 }
618
619 AliCDBStorage* bckStorage = fDefaultStorage;
620
621 fDefaultStorage = GetStorage(storageUri);
622
623 if(!fDefaultStorage) return;
624
625 if(bckStorage && (fDefaultStorage != bckStorage)){
626 AliWarning("Existing default storage replaced: clearing cache!");
627 ClearCache();
628 }
629
630 if (fStorageMap->Contains("default")) {
631 delete fStorageMap->Remove(((TPair*)fStorageMap->FindObject("default"))->Key());
632 }
633 fStorageMap->Add(new TObjString("default"), new TObjString(fDefaultStorage->GetURI()));
9e1ceb13 634}
5078a13f 635
9e1ceb13 636//_____________________________________________________________________________
637void AliCDBManager::SetDefaultStorage(const AliCDBParam* param) {
638// set default storage from AliCDBParam object
4954c5cd 639
5078a13f 640 AliCDBStorage* bckStorage = fDefaultStorage;
4954c5cd 641
5078a13f 642 fDefaultStorage = GetStorage(param);
95c0e132 643
5078a13f 644 if(!fDefaultStorage) return;
b21e3194 645
5078a13f 646 if(bckStorage && (fDefaultStorage != bckStorage)){
647 AliWarning("Existing default storage replaced: clearing cache!");
648 ClearCache();
649 }
650
651 if (fStorageMap->Contains("default")) {
652 delete fStorageMap->Remove(((TPair*)fStorageMap->FindObject("default"))->Key());
653 }
654 fStorageMap->Add(new TObjString("default"), new TObjString(fDefaultStorage->GetURI()));
9e1ceb13 655}
656
657//_____________________________________________________________________________
658void AliCDBManager::SetDefaultStorage(AliCDBStorage* storage) {
659// set default storage from another active storage
5078a13f 660
661 // if lock is ON, cannot activate more storages!
662 if(fLock) {
663 if (fDefaultStorage) {
664 AliFatal("Lock is ON, and default storage is already set: "
665 "cannot reset it or activate more storages!");
666 }
667 }
668
669 if (!storage) {
670 UnsetDefaultStorage();
671 return;
672 }
673
674 AliCDBStorage* bckStorage = fDefaultStorage;
675
676 fDefaultStorage = storage;
677
678 if(bckStorage && (fDefaultStorage != bckStorage)){
679 AliWarning("Existing default storage replaced: clearing cache!");
680 ClearCache();
681 }
682
683 if (fStorageMap->Contains("default")) {
684 delete fStorageMap->Remove(((TPair*)fStorageMap->FindObject("default"))->Key());
685 }
686 fStorageMap->Add(new TObjString("default"), new TObjString(fDefaultStorage->GetURI()));
9e1ceb13 687}
4667c116 688
7de84b33 689//_____________________________________________________________________________
690void AliCDBManager::SetDefaultStorage(const char* mcString, const char* simType) {
691// sets default storage for MC data
5078a13f 692// mcString MUST be "MC",
7de84b33 693// simType can be "Ideal","Residual","Full"
5078a13f 694
695 TString strmcString(mcString);
696 TString strsimType(simType);
697 TString dbString;
698 if (strmcString != "MC"){
699 AliFatal("Method requires first string to be MC!");
700 }
701 else {
702 if (strsimType == "Ideal"){
703 dbString = fgkMCIdealStorage;
704 }
705 else if (strsimType == "Full"){
706 dbString = fgkMCFullStorage;
707 }
708 else if (strsimType == "Residual"){
709 dbString = fgkMCResidualStorage;
710 }
711 else {
712 AliFatal("Error in setting the storage for MC data, second argument MUST be either \"Ideal\" or \"Full\" or \"Residual\".");
713 }
714
715 SetDefaultStorage(dbString.Data());
716 fStartRunLHCPeriod=0;
717 fEndRunLHCPeriod=AliCDBRunRange::Infinity();
718 if(!fDefaultStorage) AliFatal(Form("%s storage not there! Please check!",dbString.Data()));
719 }
720}
721
722//_____________________________________________________________________________
723void AliCDBManager::ValidateCvmfsCase() const {
112ba4b4 724 // The OCDB_PATH variable contains the path to the directory in /cvmfs/ which is
725 // an AliRoot tag based snapshot of the AliEn file catalogue (e.g.
726 // /cvmfs/alice.cern.ch/x86_64-2.6-gnu-4.1.2/Packages/OCDB/v5-05-76-AN).
727 // The directory has to contain:
728 // 1) <data|MC>/20??.list.gz gzipped text files listing the OCDB files (seen by that AliRoot tag)
729 // 2) bin/getOCDBFilesPerRun.sh (shell+awk) script extracting from 1) the list
730 // of valid files for the given run.
731
5078a13f 732 if (! fCvmfsOcdb.BeginsWith("/cvmfs")) //!!!! to be commented out for testing
733 AliFatal(Form("OCDB_PATH set to an invalid path: %s", fCvmfsOcdb.Data()));
734
735 TString cvmfsUri(fCvmfsOcdb);
736 gSystem->ExpandPathName(cvmfsUri);
737 if (gSystem->AccessPathName(cvmfsUri))
738 AliFatal(Form("OCDB_PATH set to an invalid path: %s", cvmfsUri.Data()));
739
740 // check that we find the two scripts we need
741
742 AliDebug(3, "OCDB_PATH envvar is set. Changing OCDB storage from alien:// to local:///cvmfs type.");
743 cvmfsUri = cvmfsUri.Strip(TString::kTrailing, '/');
744 cvmfsUri.Append("/bin/getOCDBFilesPerRun.sh");
745 if (gSystem->AccessPathName(cvmfsUri))
746 AliFatal(Form("Cannot find valid script: %s", cvmfsUri.Data()));
7de84b33 747}
5078a13f 748
7de84b33 749//_____________________________________________________________________________
750void AliCDBManager::SetDefaultStorageFromRun(Int_t run) {
751// set default storage from the run number - to be used only with raw data
752
5078a13f 753 // if lock is ON, cannot activate more storages!
754 if(fLock) {
755 if (fDefaultStorage) {
756 AliFatal("Lock is ON, and default storage is already set: "
757 "cannot activate default storage from run number");
758 }
759 }
760
112ba4b4 761 TString lhcPeriod("");
762 Int_t startRun = 0, endRun = 0;
763 if (! fCvmfsOcdb.IsNull()) { // fRaw and cvmfs case: set LHC period from cvmfs file
764 GetLHCPeriodAgainstCvmfsFile(run, lhcPeriod, startRun, endRun);
765 } else { // fRaw: set LHC period from AliEn XML file
766 GetLHCPeriodAgainstAlienFile(run, lhcPeriod, startRun, endRun);
767 }
5078a13f 768
112ba4b4 769 fLHCPeriod = lhcPeriod;
770 fStartRunLHCPeriod = startRun;
771 fEndRunLHCPeriod = endRun;
5078a13f 772
112ba4b4 773 SetDefaultStorage(fLHCPeriod.Data());
774 if(!fDefaultStorage) AliFatal(Form("%s storage not there! Please check!",fLHCPeriod.Data()));
5078a13f 775
112ba4b4 776}
5078a13f 777
112ba4b4 778//_____________________________________________________________________________
779void AliCDBManager::GetLHCPeriodAgainstAlienFile(Int_t run, TString& lhcPeriod, Int_t& startRun, Int_t& endRun) {
780// set LHC period (year + first, last run) comparing run number and AliEn XML file
781
782// retrieve XML file from alien
783 if(!gGrid) {
784 TGrid::Connect("alien://","");
5078a13f 785 if(!gGrid) {
112ba4b4 786 AliError("Connection to alien failed!");
787 return;
5078a13f 788 }
5078a13f 789 }
112ba4b4 790 TUUID uuid;
ec0ce6e5 791 TString rndname = gSystem->TempDirectory(); // "/tmp/";
792 rndname += "/";
112ba4b4 793 rndname += "OCDBFolderXML.";
794 rndname += uuid.AsString();
795 rndname += ".xml";
796 AliDebug(2, Form("file to be copied = %s", fgkOCDBFolderXMLfile.Data()));
797 if (!TFile::Cp(fgkOCDBFolderXMLfile.Data(), rndname.Data())) {
798 AliFatal(Form("Cannot make a local copy of OCDBFolder xml file in %s",rndname.Data()));
799 }
800 AliCDBHandler* saxcdb = new AliCDBHandler();
801 saxcdb->SetRun(run);
802 TSAXParser *saxParser = new TSAXParser();
803 saxParser->ConnectToHandler("AliCDBHandler", saxcdb);
804 saxParser->ParseFile(rndname.Data());
805 AliInfo(Form(" LHC folder = %s", saxcdb->GetOCDBFolder().Data()));
806 AliInfo(Form(" LHC period start run = %d", saxcdb->GetStartRunRange()));
807 AliInfo(Form(" LHC period end run = %d", saxcdb->GetEndRunRange()));
808 lhcPeriod = saxcdb->GetOCDBFolder();
809 startRun = saxcdb->GetStartRunRange();
810 endRun = saxcdb->GetEndRunRange();
811}
5078a13f 812
112ba4b4 813//_____________________________________________________________________________
814void AliCDBManager::GetLHCPeriodAgainstCvmfsFile(Int_t run, TString& lhcPeriod, Int_t& startRun, Int_t& endRun) {
815// set LHC period (year + first, last run) comparing run number and CVMFS file
816// We don't want to connect to AliEn just to set the uri from the runnumber
817// for that we use the script getUriFromYear.sh in the cvmfs AliRoot package
818
819 TString getYearScript(fCvmfsOcdb);
820 getYearScript = getYearScript.Strip(TString::kTrailing, '/');
821 getYearScript.Append("/bin/getUriFromYear.sh");
822 if (gSystem->AccessPathName(getYearScript))
823 AliFatal(Form("Cannot find valid script: %s", getYearScript.Data()));
824 TString inoutFile(gSystem->WorkingDirectory());
825 inoutFile += "/uri_range_";
826 inoutFile += TString::Itoa(run,10);
827 TString command(getYearScript);
828 command += ' ';
829 command += TString::Itoa(run,10);
830 command += Form(" > %s", inoutFile.Data());
831 AliDebug(3, Form("Running command: \"%s\"",command.Data()));
832 Int_t result = gSystem->Exec(command.Data());
833 if(result != 0) {
834 AliFatal(Form("Was not able to execute \"%s\"", command.Data()));
835 }
836
837 // now read the file with the uri and first and last run
838 std::ifstream file(inoutFile.Data());
839 if (!file.is_open()) {
840 AliFatal(Form("Error opening file \"%s\"!", inoutFile.Data()));
841 }
842 TString line;
843 TObjArray* oStringsArray = 0;
844 while (line.ReadLine(file)){
845 oStringsArray = line.Tokenize(' ');
846 }
847 TObjString *oStrUri = dynamic_cast<TObjString*> (oStringsArray->At(0));
848 TObjString *oStrFirst = dynamic_cast<TObjString*> (oStringsArray->At(1));
849 TString firstRun = oStrFirst->GetString();
850 TObjString *oStrLast = dynamic_cast<TObjString*> (oStringsArray->At(2));
851 TString lastRun = oStrLast->GetString();
852
853 lhcPeriod = oStrUri->GetString();
854 startRun = firstRun.Atoi();
855 endRun = lastRun.Atoi();
856
857 file.close();
7de84b33 858}
859
4667c116 860//_____________________________________________________________________________
1f341624 861void AliCDBManager::UnsetDefaultStorage() {
862// Unset default storage
5078a13f 863
864 // if lock is ON, action is forbidden!
865 if(fLock) {
866 if (fDefaultStorage) {
867 AliFatal("Lock is ON: cannot unset default storage!");
868 }
869 }
870
871 if (fDefaultStorage) {
872 AliWarning("Clearing cache!");
873 ClearCache();
874 }
875
876 fRun = fStartRunLHCPeriod = fEndRunLHCPeriod = -1;
877 fRaw = kFALSE;
878
879 fDefaultStorage = 0x0;
4667c116 880}
9e1ceb13 881
02c4845e 882//_____________________________________________________________________________
d8734d06 883void AliCDBManager::SetSpecificStorage(const char* calibType, const char* dbString, Int_t version, Int_t subVersion) {
024cf675 884// sets storage specific for detector or calibration type (works with AliCDBManager::Get(...))
02c4845e 885
5078a13f 886 AliCDBParam *aPar = CreateParameter(dbString);
887 if(!aPar) return;
d8734d06 888 SetSpecificStorage(calibType, aPar, version, subVersion);
5078a13f 889 delete aPar;
02c4845e 890}
891
892//_____________________________________________________________________________
d8734d06 893void AliCDBManager::SetSpecificStorage(const char* calibType, const AliCDBParam* param, Int_t version, Int_t subVersion) {
024cf675 894// sets storage specific for detector or calibration type (works with AliCDBManager::Get(...))
895// Default storage should be defined prior to any specific storages, e.g.:
896// AliCDBManager::instance()->SetDefaultStorage("alien://");
c3a7b59a 897// AliCDBManager::instance()->SetSpecificStorage("TPC/*","local://DB_TPC");
898// AliCDBManager::instance()->SetSpecificStorage("*/Align/*","local://DB_TPCAlign");
899// calibType must be a valid CDB path! (3 level folder structure)
7737f97c 900// Specific version/subversion is set in the uniqueid of the AliCDBParam value stored in the
901// specific storages map
7de84b33 902
5078a13f 903 if(!fDefaultStorage && !fRaw) {
904 AliError("Please activate a default storage first!");
905 return;
906 }
4005d0b5 907
5078a13f 908 AliCDBPath aPath(calibType);
909 if(!aPath.IsValid()){
910 AliError(Form("Not a valid path: %s", calibType));
911 return;
912 }
62032124 913
5078a13f 914 TObjString *objCalibType = new TObjString(aPath.GetPath());
915 if(fSpecificStorages.Contains(objCalibType)){
916 AliWarning(Form("Storage \"%s\" already activated! It will be replaced by the new one",
917 calibType));
918 AliCDBParam *checkPar = dynamic_cast<AliCDBParam*> (fSpecificStorages.GetValue(calibType));
919 if(checkPar) delete checkPar;
920 delete fSpecificStorages.Remove(objCalibType);
921 }
922 AliCDBStorage *aStorage = GetStorage(param);
923 if(!aStorage) return;
96b7636d 924
7737f97c 925 // Set the unique id of the AliCDBParam stored in the map to store specific version/subversion
d8734d06 926 UInt_t uId = ((subVersion+1)<<16) + (version+1);
927 AliCDBParam *specificParam = param->CloneParam();
928 specificParam->SetUniqueID(uId);
929 fSpecificStorages.Add(objCalibType, specificParam);
b21e3194 930
5078a13f 931 if(fStorageMap->Contains(objCalibType)){
932 delete fStorageMap->Remove(objCalibType);
933 }
934 fStorageMap->Add(objCalibType->Clone(), new TObjString(param->GetURI()));
b21e3194 935
02c4845e 936}
937
938//_____________________________________________________________________________
024cf675 939AliCDBStorage* AliCDBManager::GetSpecificStorage(const char* calibType) {
5078a13f 940// get storage specific for detector or calibration type
4b5e0dce 941
5078a13f 942 AliCDBPath calibPath(calibType);
943 if(!calibPath.IsValid()) return NULL;
96b7636d 944
5078a13f 945 AliCDBParam *checkPar = (AliCDBParam*) fSpecificStorages.GetValue(calibPath.GetPath());
946 if(!checkPar){
947 AliError(Form("%s storage not found!", calibType));
948 return NULL;
949 } else {
950 return GetStorage(checkPar);
951 }
96b7636d 952
02c4845e 953}
954
024cf675 955//_____________________________________________________________________________
956AliCDBParam* AliCDBManager::SelectSpecificStorage(const TString& path) {
82096dfc 957// select storage valid for path from the list of specific storages
024cf675 958
5078a13f 959 AliCDBPath aPath(path);
960 if(!aPath.IsValid()) return NULL;
961
962 TIter iter(&fSpecificStorages);
963 TObjString *aCalibType=0;
964 AliCDBPath tmpPath("null/null/null");
965 AliCDBParam* aPar=0;
966 while((aCalibType = (TObjString*) iter.Next())){
967 AliCDBPath calibTypePath(aCalibType->GetName());
968 if(calibTypePath.Comprises(aPath)) {
969 if(calibTypePath.Comprises(tmpPath)) continue;
970 aPar = (AliCDBParam*) fSpecificStorages.GetValue(aCalibType);
971 tmpPath.SetPath(calibTypePath.GetPath());
972 }
973 }
974 return aPar;
024cf675 975}
976
02c4845e 977//_____________________________________________________________________________
4667c116 978AliCDBEntry* AliCDBManager::Get(const AliCDBPath& path, Int_t runNumber,
5078a13f 979 Int_t version, Int_t subVersion) {
980 // get an AliCDBEntry object from the database
981
982 if(runNumber < 0){
983 // RunNumber is not specified. Try with fRun
984 if (fRun < 0){
985 AliError("Run number neither specified in query nor set in AliCDBManager! Use AliCDBManager::SetRun.");
986 return NULL;
987 }
988 runNumber = fRun;
989 }
024cf675 990
5078a13f 991 return Get(AliCDBId(path, runNumber, runNumber, version, subVersion));
02c4845e 992}
993
994//_____________________________________________________________________________
4667c116 995AliCDBEntry* AliCDBManager::Get(const AliCDBPath& path,
5078a13f 996 const AliCDBRunRange& runRange, Int_t version,
997 Int_t subVersion) {
998 // get an AliCDBEntry object from the database!
02c4845e 999
5078a13f 1000 return Get(AliCDBId(path, runRange, version, subVersion));
02c4845e 1001}
1002
1003//_____________________________________________________________________________
d8734d06 1004AliCDBEntry* AliCDBManager::Get(const AliCDBId& queryId, Bool_t forceCaching) {
02c4845e 1005// get an AliCDBEntry object from the database
5078a13f 1006
d8734d06 1007 // check if queryId's path and runRange are valid
1008 // queryId is invalid also if version is not specified and subversion is!
1009 if (!queryId.IsValid()) {
1010 AliError(Form("Invalid query: %s", queryId.ToString().Data()));
5078a13f 1011 return NULL;
1012 }
1013
d8734d06 1014 // queryId is not specified if path contains wildcard or run range= [-1,-1]
1015 if (!queryId.IsSpecified()) {
5078a13f 1016 AliError(Form("Unspecified query: %s",
d8734d06 1017 queryId.ToString().Data()));
5078a13f 1018 return NULL;
1019 }
1020
d8734d06 1021 if(fLock && !(fRun >= queryId.GetFirstRun() && fRun <= queryId.GetLastRun()))
5078a13f 1022 AliFatal("Lock is ON: cannot use different run number than the internal one!");
1023
d8734d06 1024 if(fCache && !(fRun >= queryId.GetFirstRun() && fRun <= queryId.GetLastRun()))
5078a13f 1025 AliWarning("Run number explicitly set in query: CDB cache temporarily disabled!");
1026
1027 AliCDBEntry *entry=0;
1028
1029 // first look into map of cached objects
d8734d06 1030 if(fCache && queryId.GetFirstRun() == fRun)
1031 entry = (AliCDBEntry*) fEntryCache.GetValue(queryId.GetPath());
5078a13f 1032 if(entry) {
d8734d06 1033 AliDebug(2, Form("Object %s retrieved from cache !!",queryId.GetPath().Data()));
5078a13f 1034 return entry;
1035 }
1036
1037 // if snapshot flag is set, try getting from the snapshot
1038 // but in the case a specific storage is specified for this path
d8734d06 1039 AliCDBParam *aPar=SelectSpecificStorage(queryId.GetPath());
5078a13f 1040 if(!aPar){
d8734d06 1041 if(fSnapshotMode && queryId.GetFirstRun() == fRun)
5078a13f 1042 {
d8734d06 1043 entry = GetEntryFromSnapshot(queryId.GetPath());
5078a13f 1044 if(entry) {
d8734d06 1045 AliInfo(Form("Object \"%s\" retrieved from the snapshot.",queryId.GetPath().Data()));
1046 if(queryId.GetFirstRun() == fRun) // no need to check fCache, fSnapshotMode not possible otherwise
1047 CacheEntry(queryId.GetPath(), entry);
5078a13f 1048
1049 if(!fIds->Contains(&entry->GetId()))
1050 fIds->Add(entry->GetId().Clone());
1051
1052 return entry;
1053 }
1054 }
1055 }
1056
1057 // Entry is not in cache (and, in case we are in snapshot mode, not in the snapshot either)
1058 // => retrieve it from the storage and cache it!!
1059 if(!fDefaultStorage) {
1060 AliError("No storage set!");
1061 return NULL;
1062 }
1063
a9355e77 1064 Int_t version = -1, subVersion = -1;
5078a13f 1065 AliCDBStorage *aStorage=0;
1066 if(aPar) {
1067 aStorage=GetStorage(aPar);
1068 TString str = aPar->GetURI();
d8734d06 1069 UInt_t uId = aPar->GetUniqueID();
1070 version = Int_t(uId&0xffff) - 1;
1071 subVersion = Int_t(uId>>16) - 1;
5078a13f 1072 AliDebug(2,Form("Looking into storage: %s",str.Data()));
1073 } else {
1074 aStorage=GetDefaultStorage();
1075 AliDebug(2,"Looking into default storage");
1076 }
1077
d8734d06 1078 AliCDBId finalQueryId(queryId);
1079 if(version >= 0) {
1080 AliDebug(2,Form("Specific version set to: %d", version));
1081 finalQueryId.SetVersion(version);
1082 }
1083 if(subVersion >= 0) {
1084 AliDebug(2,Form("Specific subversion set to: %d", subVersion));
1085 finalQueryId.SetSubVersion(subVersion);
1086 }
1087 entry = aStorage->Get(finalQueryId);
5078a13f 1088
d8734d06 1089 if(entry && fCache && (queryId.GetFirstRun()==fRun || forceCaching)){
1090 CacheEntry(queryId.GetPath(), entry);
5078a13f 1091 }
1092
1093 if(entry && !fIds->Contains(&entry->GetId())){
1094 fIds->Add(entry->GetId().Clone());
1095 }
e973a743 1096
5078a13f 1097 return entry;
4667c116 1098}
1099
c022b397 1100//_____________________________________________________________________________
1101AliCDBEntry* AliCDBManager::GetEntryFromSnapshot(const char* path) {
5078a13f 1102// get the entry from the open snapshot file
c022b397 1103
5078a13f 1104 TString sPath(path);
1105 sPath.ReplaceAll("/","*");
1106 if(!fSnapshotFile){
1107 AliError("No snapshot file is open!");
1108 return 0;
1109 }
1110 AliCDBEntry *entry = dynamic_cast<AliCDBEntry*>(fSnapshotFile->Get(sPath.Data()));
1111 if(!entry){
1112 AliDebug(2,Form("Cannot get a CDB entry for \"%s\" from snapshot file",path));
1113 return 0;
1114 }
c022b397 1115
5078a13f 1116 return entry;
c022b397 1117}
1118
1119//_____________________________________________________________________________
1120Bool_t AliCDBManager::SetSnapshotMode(const char* snapshotFileName) {
1121// set the manager in snapshot mode
c022b397 1122
5078a13f 1123 if(!fCache){
1124 AliError("Cannot set the CDB manage in snapshot mode if the cache is not active!");
1125 return kFALSE;
1126 }
c022b397 1127
5078a13f 1128 //open snapshot file
1129 TString snapshotFile(snapshotFileName);
1130 if(snapshotFile.BeginsWith("alien://")){
1131 if(!gGrid) {
1132 TGrid::Connect("alien://","");
1133 if(!gGrid) {
1134 AliError("Connection to alien failed!");
1135 return kFALSE;
1136 }
c022b397 1137 }
5078a13f 1138 }
c022b397 1139
5078a13f 1140 fSnapshotFile = TFile::Open(snapshotFileName);
1141 if (!fSnapshotFile || fSnapshotFile->IsZombie()){
1142 AliError(Form("Cannot open file %s",snapshotFileName));
1143 return kFALSE;
1144 }
1145
1146 AliInfo("The CDB manager is set in snapshot mode!");
1147 fSnapshotMode = kTRUE;
1148 return kTRUE;
c022b397 1149
1150}
1151
0f40a831 1152//_____________________________________________________________________________
1153const char* AliCDBManager::GetURI(const char* path) {
1154// return the URI of the storage where to look for path
1155
5078a13f 1156 if(!IsDefaultStorageSet()) return 0;
1157
1158 AliCDBParam *aPar=SelectSpecificStorage(path);
0f40a831 1159
5078a13f 1160 if(aPar) {
1161 return aPar->GetURI().Data();
0f40a831 1162
5078a13f 1163 } else {
1164 return GetDefaultStorage()->GetURI().Data();
1165 }
1166
1167 return 0;
0f40a831 1168}
1169
b7df0cc5 1170//_____________________________________________________________________________
1171Int_t AliCDBManager::GetStartRunLHCPeriod(){
5078a13f 1172// get the first run of validity
1173// for the current period
1174// if set
1175 if(fStartRunLHCPeriod==-1)
1176 AliWarning("Run-range not yet set for the current LHC period.");
1177 return fStartRunLHCPeriod;
b7df0cc5 1178}
1179
1180//_____________________________________________________________________________
1181Int_t AliCDBManager::GetEndRunLHCPeriod(){
5078a13f 1182// get the last run of validity
1183// for the current period
1184// if set
1185 if(fEndRunLHCPeriod==-1)
1186 AliWarning("Run-range not yet set for the current LHC period.");
1187 return fEndRunLHCPeriod;
b7df0cc5 1188}
1189
1190//_____________________________________________________________________________
1191TString AliCDBManager::GetLHCPeriod(){
5078a13f 1192// get the current LHC period string
1193//
1194 if(fLHCPeriod.IsWhitespace() || fLHCPeriod.IsNull())
1195 AliWarning("LHC period (OCDB folder) not yet set");
1196 return fLHCPeriod;
b7df0cc5 1197}
1198
4667c116 1199//_____________________________________________________________________________
1200AliCDBId* AliCDBManager::GetId(const AliCDBPath& path, Int_t runNumber,
5078a13f 1201 Int_t version, Int_t subVersion) {
1202 // get the AliCDBId of the valid object from the database (does not retrieve the object)
1203 // User must delete returned object!
1204
1205 if(runNumber < 0){
1206 // RunNumber is not specified. Try with fRun
1207 if (fRun < 0){
1208 AliError("Run number neither specified in query nor set in AliCDBManager! Use AliCDBManager::SetRun.");
1209 return NULL;
1210 }
1211 runNumber = fRun;
1212 }
4667c116 1213
5078a13f 1214 return GetId(AliCDBId(path, runNumber, runNumber, version, subVersion));
4667c116 1215}
1216
1217//_____________________________________________________________________________
1218AliCDBId* AliCDBManager::GetId(const AliCDBPath& path,
5078a13f 1219 const AliCDBRunRange& runRange, Int_t version,
1220 Int_t subVersion) {
1221 // get the AliCDBId of the valid object from the database (does not retrieve the object)
1222 // User must delete returned object!
4667c116 1223
5078a13f 1224 return GetId(AliCDBId(path, runRange, version, subVersion));
4667c116 1225}
1226
1227//_____________________________________________________________________________
1228AliCDBId* AliCDBManager::GetId(const AliCDBId& query) {
1229// get the AliCDBId of the valid object from the database (does not retrieve the object)
1230// User must delete returned object!
1231
5078a13f 1232 if(!fDefaultStorage) {
1233 AliError("No storage set!");
1234 return NULL;
1235 }
1236
1237 // check if query's path and runRange are valid
1238 // query is invalid also if version is not specified and subversion is!
1239 if (!query.IsValid()) {
1240 AliError(Form("Invalid query: %s", query.ToString().Data()));
1241 return NULL;
1242 }
1243
1244 // query is not specified if path contains wildcard or run range= [-1,-1]
1245 if (!query.IsSpecified()) {
1246 AliError(Form("Unspecified query: %s",
1247 query.ToString().Data()));
1248 return NULL;
1249 }
1250
1251 if(fCache && query.GetFirstRun() != fRun)
1252 AliWarning("Run number explicitly set in query: CDB cache temporarily disabled!");
1253
1254 AliCDBEntry* entry = 0;
1255
1256 // first look into map of cached objects
1257 if(fCache && query.GetFirstRun() == fRun)
1258 entry = (AliCDBEntry*) fEntryCache.GetValue(query.GetPath());
1259
1260 if(entry) {
1261 AliDebug(2, Form("Object %s retrieved from cache !!",query.GetPath().Data()));
1262 return dynamic_cast<AliCDBId*> (entry->GetId().Clone());
1263 }
1264
1265 // Entry is not in cache -> retrieve it from CDB and cache it!!
1266 AliCDBStorage *aStorage=0;
1267 AliCDBParam *aPar=SelectSpecificStorage(query.GetPath());
1268
1269 if(aPar) {
1270 aStorage=GetStorage(aPar);
1271 TString str = aPar->GetURI();
1272 AliDebug(2,Form("Looking into storage: %s",str.Data()));
1273
1274 } else {
1275 aStorage=GetDefaultStorage();
1276 AliDebug(2,"Looking into default storage");
1277 }
1278
1279 return aStorage->GetId(query);
4667c116 1280
02c4845e 1281}
1282
1283//_____________________________________________________________________________
62032124 1284TList* AliCDBManager::GetAll(const AliCDBPath& path, Int_t runNumber,
5078a13f 1285 Int_t version, Int_t subVersion) {
1286 // get multiple AliCDBEntry objects from the database
1287
1288 if(runNumber < 0){
1289 // RunNumber is not specified. Try with fRun
1290 if (fRun < 0){
1291 AliError("Run number neither specified in query nor set in AliCDBManager! Use AliCDBManager::SetRun.");
1292 return NULL;
1293 }
1294 runNumber = fRun;
1295 }
afd8dbf4 1296
5078a13f 1297 return GetAll(AliCDBId(path, runNumber, runNumber, version,
1298 subVersion));
02c4845e 1299}
1300
1301//_____________________________________________________________________________
8e245d15 1302TList* AliCDBManager::GetAll(const AliCDBPath& path,
5078a13f 1303 const AliCDBRunRange& runRange, Int_t version, Int_t subVersion) {
1304 // get multiple AliCDBEntry objects from the database
02c4845e 1305
5078a13f 1306 return GetAll(AliCDBId(path, runRange, version, subVersion));
02c4845e 1307}
1308
1309//_____________________________________________________________________________
1310TList* AliCDBManager::GetAll(const AliCDBId& query) {
1311// get multiple AliCDBEntry objects from the database
024cf675 1312// Warning: this method works correctly only for queries of the type "Detector/*"
1313// and not for more specific queries e.g. "Detector/Calib/*" !
8e245d15 1314// Warning #2: Entries are cached, but GetAll will keep on retrieving objects from OCDB!
1315// To get an object from cache use Get() function
02c4845e 1316
5078a13f 1317 if(!fDefaultStorage) {
1318 AliError("No storage set!");
1319 return NULL;
1320 }
1321
1322 if (!query.IsValid()) {
1323 AliError(Form("Invalid query: %s", query.ToString().Data()));
1324 return NULL;
1325 }
1326
1327 if((fSpecificStorages.GetEntries()>0) && query.GetPath().BeginsWith('*')){
1328 // if specific storages are active a query with "*" is ambiguous
1329 AliError("Query too generic in this context!");
1330 return NULL;
1331 }
1332
1333 if (query.IsAnyRange()) {
1334 AliError(Form("Unspecified run or runrange: %s",
1335 query.ToString().Data()));
1336 return NULL;
1337 }
1338
1339 if(fLock && query.GetFirstRun() != fRun)
1340 AliFatal("Lock is ON: cannot use different run number than the internal one!");
1341
bae7166c 1342 AliCDBParam *aPar = SelectSpecificStorage(query.GetPath());
5078a13f 1343
1344 AliCDBStorage *aStorage;
1345 if(aPar) {
1346 aStorage=GetStorage(aPar);
1347 AliDebug(2,Form("Looking into storage: %s", aPar->GetURI().Data()));
1348
1349 } else {
1350 aStorage=GetDefaultStorage();
1351 AliDebug(2,Form("Looking into default storage: %s", aStorage->GetURI().Data()));
1352 }
1353
1354 TList *result = 0;
1355 if(aStorage) result = aStorage->GetAll(query);
1356 if(!result) return 0;
1357
1358 // loop on result to check whether entries should be re-queried with specific storages
1359 if(fSpecificStorages.GetEntries()>0 && ! (fSpecificStorages.GetEntries() == 1 && aPar)) {
1360 AliInfo("Now look into all other specific storages...");
1361
1362 TIter iter(result);
1363 AliCDBEntry* chkEntry=0;
1364
1365 while((chkEntry = dynamic_cast<AliCDBEntry*> (iter.Next()))){
1366 AliCDBId& chkId = chkEntry->GetId();
1367 AliDebug(2, Form("Checking id %s ", chkId.GetPath().Data()));
1368 AliCDBParam *chkPar=SelectSpecificStorage(chkId.GetPath());
1369 if (!chkPar || aPar == chkPar) continue;
1370 AliCDBStorage *chkStorage = GetStorage(chkPar);
1371 AliDebug(2, Form("Found specific storage! %s", chkPar->GetURI().Data()));
1372
5078a13f 1373 chkId.SetRunRange(query.GetFirstRun(), query.GetLastRun());
bae7166c 1374 UInt_t uId = chkPar->GetUniqueID();
1375 Int_t version = -1, subVersion = -1;
1376 version = Int_t(uId&0xffff) - 1;
1377 subVersion = Int_t(uId>>16) - 1;
1378 if(version!=-1){
1379 chkId.SetVersion(version);
1380 }else{
1381 chkId.SetVersion(query.GetVersion());
1382 }
1383 if(subVersion!=-1){
1384 chkId.SetSubVersion(subVersion);
1385 }else{
1386 chkId.SetSubVersion(query.GetSubVersion());
1387 }
5078a13f 1388
bae7166c 1389 AliCDBEntry *newEntry=0;
5078a13f 1390 if(chkStorage) newEntry = chkStorage->Get(chkId);
1391 if(!newEntry) continue;
1392
1393 // object is found in specific storage: replace entry in the result list!
1394 chkEntry->SetOwner(1);
1395 delete result->Remove(chkEntry);
1396 result->AddFirst(newEntry);
1397 }
1398
1399 Int_t nEntries = result->GetEntries();
1400 AliInfo("After look into other specific storages, result list is:");
1401 for(int i=0; i<nEntries;i++){
1402 AliCDBEntry *entry = (AliCDBEntry*) result->At(i);
1403 AliInfo(Form("%s",entry->GetId().ToString().Data()));
1404 }
1405 }
1406
1407 // caching entries
1408 TIter iter(result);
1409 AliCDBEntry* entry=0;
1410 while((entry = dynamic_cast<AliCDBEntry*> (iter.Next()))){
1411
1412 if(!fIds->Contains(&entry->GetId())){
1413 fIds->Add(entry->GetId().Clone());
1414 }
1415 if(fCache && (query.GetFirstRun() == fRun)){
1416 CacheEntry(entry->GetId().GetPath(), entry);
1417 }
1418 }
1419
1420
1421 return result;
02c4845e 1422}
1423
1424//_____________________________________________________________________________
b1bfa962 1425Bool_t AliCDBManager::Put(TObject* object, const AliCDBId& id, AliCDBMetaData* metaData, const char* mirrors, DataType type){
02c4845e 1426// store an AliCDBEntry object into the database
1427
5078a13f 1428 if (object==0x0) {
1429 AliError("Null Entry! No storage will be done!");
1430 return kFALSE;
1431 }
968cfa08 1432
5078a13f 1433 AliCDBEntry anEntry(object, id, metaData);
1434 return Put(&anEntry, mirrors, type);
02c4845e 1435
1436}
1437
1438
1439//_____________________________________________________________________________
06eeadea 1440Bool_t AliCDBManager::Put(AliCDBEntry* entry, const char* mirrors, DataType type){
02c4845e 1441// store an AliCDBEntry object into the database
1442
5078a13f 1443 if(type == kPrivate && !fDefaultStorage) {
1444 AliError("No storage set!");
1445 return kFALSE;
1446 }
1447
1448 if (!entry){
1449 AliError("No entry!");
1450 return kFALSE;
1451 }
1452
1453 if (entry->GetObject()==0x0){
1454 AliError("No valid object in CDB entry!");
1455 return kFALSE;
1456 }
1457
1458 if (!entry->GetId().IsValid()) {
1459 AliError(Form("Invalid entry ID: %s",
1460 entry->GetId().ToString().Data()));
1461 return kFALSE;
1462 }
1463
1464 if (!entry->GetId().IsSpecified()) {
1465 AliError(Form("Unspecified entry ID: %s",
1466 entry->GetId().ToString().Data()));
1467 return kFALSE;
1468 }
1469
1470 AliCDBId id = entry->GetId();
1471 AliCDBParam *aPar = SelectSpecificStorage(id.GetPath());
1472
1473 AliCDBStorage *aStorage=0;
1474
1475 if(aPar) {
1476 aStorage=GetStorage(aPar);
1477 } else {
1478 switch(type){
1479 case kCondition:
1480 aStorage = GetStorage(fCondParam);
1481 break;
1482 case kReference:
1483 aStorage = GetStorage(fRefParam);
1484 break;
1485 case kPrivate:
1486 aStorage = GetDefaultStorage();
1487 break;
1488 }
1489 }
1490
1491 AliDebug(2,Form("Storing object into storage: %s", aStorage->GetURI().Data()));
1492
1493 TString strMirrors(mirrors);
1494 Bool_t result = kFALSE;
1495 if(!strMirrors.IsNull() && !strMirrors.IsWhitespace())
1496 result = aStorage->Put(entry, mirrors, type);
1497 else
1498 result = aStorage->Put(entry, "", type);
1499
1500 if(fRun >= 0) QueryCDB();
1501
1502 return result;
02c4845e 1503
1504
1505}
9e1ceb13 1506
c5941d67 1507//_____________________________________________________________________________
5078a13f 1508void AliCDBManager::SetMirrorSEs(const char* mirrors) {
c5941d67 1509// set mirror Storage Elements for the default storage, if it is of type "alien"
5078a13f 1510 if(fDefaultStorage->GetType() != "alien"){
1511 AliInfo("The default storage is not of type \"alien\". Settings for Storage Elements are not taken into account!");
1512 return;
1513 }
1514 fDefaultStorage->SetMirrorSEs(mirrors);
c5941d67 1515}
1516
1517//_____________________________________________________________________________
1518const char* AliCDBManager::GetMirrorSEs() const {
1519// get mirror Storage Elements for the default storage, if it is of type "alien"
5078a13f 1520 if(fDefaultStorage->GetType() != "alien"){
1521 AliInfo("The default storage is not of type \"alien\". Settings for Storage Elements are not taken into account!");
1522 return "";
1523 }
1524 return fDefaultStorage->GetMirrorSEs();
c5941d67 1525}
1526
4b5e0dce 1527//_____________________________________________________________________________
5078a13f 1528void AliCDBManager::CacheEntry(const char* path, AliCDBEntry* entry) {
4b5e0dce 1529// cache AliCDBEntry. Cache is valid until run number is changed.
1530
5078a13f 1531 AliCDBEntry *chkEntry = dynamic_cast<AliCDBEntry*> (fEntryCache.GetValue(path));
96b7636d 1532
5078a13f 1533 if(chkEntry) {
1534 AliDebug(2, Form("Object %s already in cache !!", path));
1535 return;
1536 } else {
1537 AliDebug(2,Form("Caching entry %s", path));
1538 }
96b7636d 1539
5078a13f 1540 fEntryCache.Add(new TObjString(path), entry);
1541 AliDebug(2,Form("Cache entries: %d", fEntryCache.GetEntries()));
4b5e0dce 1542
1543}
1544
917a098b 1545//_____________________________________________________________________________
5078a13f 1546void AliCDBManager::Print(Option_t* /*option*/) const {
917a098b 1547// Print list of active storages and their URIs
917a098b 1548
5078a13f 1549 TString output=Form("Run number = %d; ",fRun);
1550 output += "Cache is ";
1551 if(!fCache) output += "NOT ";
1552 output += Form("ACTIVE; Number of active storages: %d\n",fActiveStorages.GetEntries());
1553
1554 if(fDefaultStorage) {
1555 output += Form("\t*** Default Storage URI: \"%s\"\n",fDefaultStorage->GetURI().Data());
1556 // AliInfo(output.Data());
1557 }
1558 if(fSpecificStorages.GetEntries()>0) {
1559 TIter iter(fSpecificStorages.GetTable());
1560 TPair *aPair=0;
1561 Int_t i=1;
1562 while((aPair = (TPair*) iter.Next())){
1563 output += Form("\t*** Specific storage %d: Path \"%s\" -> URI \"%s\"\n",
1564 i++, ((TObjString*) aPair->Key())->GetName(),
1565 ((AliCDBParam*) aPair->Value())->GetURI().Data());
1566 }
1567 }
1568 if(fDrainStorage) {
1569 output += Form("*** Drain Storage URI: %s\n",fDrainStorage->GetURI().Data());
1570 }
1571 AliInfo(output.Data());
917a098b 1572}
1573
4b5e0dce 1574//_____________________________________________________________________________
5078a13f 1575void AliCDBManager::SetRun(Int_t run) {
917a098b 1576// Sets current run number.
4b5e0dce 1577// When the run number changes the caching is cleared.
5078a13f 1578
1579 if(fRun == run)
1580 return;
1581
1582 if(fLock && fRun >= 0) {
1583 AliFatal("Lock is ON, cannot reset run number!");
1584 }
1585
1586 fRun = run;
1587
1588 if (fRaw) {
1589 // here the LHCPeriod xml file is parsed; the string containing the correct period is returned; the default storage is set
1590 if (fStartRunLHCPeriod <= run && fEndRunLHCPeriod >= run){
1591 AliInfo("LHCPeriod alien folder for current run already in memory");
1592 }else{
112ba4b4 1593 SetDefaultStorageFromRun(fRun);
5078a13f 1594 if(fEntryCache.GetEntries()!=0) ClearCache();
1595 return;
1596 }
1597 }
1598 ClearCache();
1599 QueryCDB();
4b5e0dce 1600}
1601
1602//_____________________________________________________________________________
1603void AliCDBManager::ClearCache(){
1604// clear AliCDBEntry cache
1605
5078a13f 1606 AliDebug(2, Form("Cache entries to be deleted: %d",fEntryCache.GetEntries()));
1607
1608 /*
1609 // To clean entries one by one
1610 TIter iter(fEntryCache.GetTable());
1611 TPair* pair=0;
1612 while((pair= dynamic_cast<TPair*> (iter.Next()))){
1613
1614 TObjString* key = dynamic_cast<TObjString*> (pair->Key());
1615 AliCDBEntry* entry = dynamic_cast<AliCDBEntry*> (pair->Value());
1616 AliDebug(2, Form("Deleting entry: %s", key->GetName()));
1617 if (entry) delete entry;
1618 delete fEntryCache.Remove(key);
1619 }
1620 */
1621 fEntryCache.DeleteAll();
1622 AliDebug(2, Form("After deleting - Cache entries: %d",fEntryCache.GetEntries()));
4b5e0dce 1623}
1624
4005d0b5 1625//_____________________________________________________________________________
1626void AliCDBManager::UnloadFromCache(const char* path){
1627// unload cached object
b0e8cda6 1628// that is remove the entry from the cache and the id from the list of ids
1629//
5078a13f 1630 if(!fActiveStorages.GetEntries()) {
1631 AliDebug(2, Form("No active storages. Object \"%s\" is not unloaded from cache", path));
1632 return;
1633 }
1634
1635 AliCDBPath queryPath(path);
1636 if(!queryPath.IsValid()) return;
1637
1638 if(!queryPath.IsWildcard()) { // path is not wildcard, get it directly from the cache and unload it!
1639 if(fEntryCache.Contains(path)){
1640 AliDebug(2, Form("Unloading object \"%s\" from cache and from list of ids", path));
1641 TObjString pathStr(path);
1642 delete fEntryCache.Remove(&pathStr);
1643 // we do not remove from the list of Id's (it's not very coherent but we leave the
1644 // id for the benefit of the userinfo)
1645 /*
1646 TIter iter(fIds);
1647 AliCDBId *id = 0;
1648 while((id = dynamic_cast<AliCDBId*> (iter.Next()))){
1649 if(queryPath.Comprises(id->GetPath()))
1650 delete fIds->Remove(id);
1651 }*/
1652 } else {
1653 AliWarning(Form("Cache does not contain object \"%s\"!", path));
1654 }
1655 AliDebug(2, Form("Cache entries: %d",fEntryCache.GetEntries()));
1656 return;
1657 }
1658
1659 // path is wildcard: loop on the cache and unload all comprised objects!
1660 TIter iter(fEntryCache.GetTable());
1661 TPair* pair = 0;
1662 Int_t removed=0;
1663
1664 while((pair = dynamic_cast<TPair*> (iter.Next()))){
1665 AliCDBPath entryPath = pair->Key()->GetName();
1666 if(queryPath.Comprises(entryPath)) {
1667 AliDebug(2, Form("Unloading object \"%s\" from cache and from list of ids", entryPath.GetPath().Data()));
1668 TObjString pathStr(entryPath.GetPath());
1669 delete fEntryCache.Remove(&pathStr);
1670 removed++;
1671
1672 // we do not remove from the list of Id's (it's not very coherent but we leave the
1673 // id for the benefit of the userinfo)
1674 /*
1675 TIter iterids(fIds);
1676 AliCDBId *anId = 0;
1677 while((anId = dynamic_cast<AliCDBId*> (iterids.Next()))){
1678 AliCDBPath aPath = anId->GetPath();
1679 TString aPathStr = aPath.GetPath();
1680 if(queryPath.Comprises(aPath)) {
1681 delete fIds->Remove(anId);
1682 }
1683 }*/
1684 }
1685 }
1686 AliDebug(2,Form("Cache entries and ids removed: %d Remaining: %d",removed,fEntryCache.GetEntries()));
4005d0b5 1687}
1688
9e1ceb13 1689//_____________________________________________________________________________
1690void AliCDBManager::DestroyActiveStorages() {
1691// delete list of active storages
1692
5078a13f 1693 fActiveStorages.DeleteAll();
1694 fSpecificStorages.DeleteAll();
9e1ceb13 1695}
1696
1697//_____________________________________________________________________________
1698void AliCDBManager::DestroyActiveStorage(AliCDBStorage* /*storage*/) {
b05400be 1699// destroys active storage
1700
5078a13f 1701 /*
1702 TIter iter(fActiveStorages.GetTable());
1703 TPair* aPair;
1704 while ((aPair = (TPair*) iter.Next())) {
1705 if(storage == (AliCDBStorage*) aPair->Value())
1706 delete fActiveStorages.Remove(aPair->Key());
1707 storage->Delete(); storage=0x0;
1708 }
1709 */
9e1ceb13 1710
1711}
1712
62032124 1713//_____________________________________________________________________________
1714void AliCDBManager::QueryCDB() {
1715// query default and specific storages for files valid for fRun. Every storage loads the Ids into its list.
1716
5078a13f 1717 if (fRun < 0){
1718 AliError("Run number not yet set! Use AliCDBManager::SetRun.");
1719 return;
1720 }
1721 if (!fDefaultStorage){
1722 AliError("Default storage is not set! Use AliCDBManager::SetDefaultStorage");
1723 return;
1724 }
1725 if(fDefaultStorage->GetType() == "alien" || fDefaultStorage->GetType() == "local"){
1726 fDefaultStorage->QueryCDB(fRun);
1727 //} else {
1728 // AliDebug(2,"Skipping query for valid files, it used only in grid...");
1729 }
1730
1731 TIter iter(&fSpecificStorages);
1732 TObjString *aCalibType=0;
1733 AliCDBParam* aPar=0;
1734 while((aCalibType = dynamic_cast<TObjString*> (iter.Next()))){
1735 aPar = (AliCDBParam*) fSpecificStorages.GetValue(aCalibType);
1736 if(aPar) {
1737 AliDebug(2,Form("Querying specific storage %s",aCalibType->GetName()));
1738 AliCDBStorage *aStorage = GetStorage(aPar);
7ab8651a 1739 if(aStorage->GetType() == "alien" || aStorage->GetType() == "local"){
003e9909 1740 aStorage->QueryCDB(fRun, aCalibType->GetName());
5078a13f 1741 } else {
1742 AliDebug(2,
1743 "Skipping query for valid files, it is used only in grid...");
1744 }
1745 }
1746 }
62032124 1747}
1748
b8ec52f6 1749//______________________________________________________________________________________________
5078a13f 1750const char* AliCDBManager::GetDataTypeName(DataType type) {
1751// returns the name (string) of the data type
b8ec52f6 1752
5078a13f 1753 switch (type){
1754 case kCondition: return "Conditions";
1755 case kReference: return "Reference";
1756 case kPrivate: return "Private";
1757 }
1758 return 0;
b8ec52f6 1759
1760}
62032124 1761
a37a1dfc 1762//______________________________________________________________________________________________
5078a13f 1763Bool_t AliCDBManager::DiffObjects(const char *cdbFile1, const char *cdbFile2) const {
1764// Compare byte-by-byte the objects contained in the CDB entry in two different files,
1765// whose name is passed as input
1766// Return value:
1767// kTRUE - in case the content of the OCDB object (persistent part) is exactly the same
1768// kFALSE - otherwise
1769
1770 TString f1Str(cdbFile1);
1771 TString f2Str(cdbFile2);
1772 if (!gGrid && ( f1Str.BeginsWith("alien://") || f2Str.BeginsWith("alien://") ))
1773 TGrid::Connect("alien://");
1774
1775 TFile * f1 = TFile::Open(cdbFile1);
1776 if (!f1){
1777 Printf("Cannot open file \"%s\"",cdbFile1);
1778 return kFALSE;
1779 }
1780 TFile * f2 = TFile::Open(cdbFile2);
1781 if (!f2){
1782 Printf("Cannot open file \"%s\"",cdbFile2);
1783 return kFALSE;
1784 }
a37a1dfc 1785
5078a13f 1786 AliCDBEntry * entry1 = (AliCDBEntry*)f1->Get("AliCDBEntry");
1787 if (!entry1){
1788 Printf("Cannot get CDB entry from file \"%s\"",cdbFile1);
1789 return kFALSE;
1790 }
1791 AliCDBEntry * entry2 = (AliCDBEntry*)f2->Get("AliCDBEntry");
1792 if (!entry2){
1793 Printf("Cannot get CDB entry from file \"%s\"",cdbFile2);
1794 return kFALSE;
1795 }
a37a1dfc 1796
5078a13f 1797 // stream the two objects in the buffer of two TMessages
1798 TObject* object1 = entry1->GetObject();
1799 TObject* object2 = entry2->GetObject();
1800 TMessage * file1 = new TMessage(TBuffer::kWrite);
1801 file1->WriteObject(object1);
1802 Int_t size1 = file1->Length();
1803 TMessage * file2 = new TMessage(TBuffer::kWrite);
1804 file2->WriteObject(object2);
1805 Int_t size2 = file2->Length();
1806 if (size1!=size2){
1807 Printf("Problem 2: OCDB entry of different size (%d,%d)",size1,size2);
1808 return kFALSE;
1809 }
1810
1811 // if the two buffers have the same size, check that they are the same byte-by-byte
1812 Int_t countDiff=0;
1813 char* buf1 = file1->Buffer();
1814 char* buf2 = file2->Buffer();
1815 //for (Int_t i=0; i<size1; i++) if (file1->Buffer()[i]!=file2->Buffer()[i]) countDiff++;
1816 for(Int_t i=0; i<size1; i++)
1817 if (buf1[i]!=buf2[i]) countDiff++;
1818
1819 if (countDiff>0){
1820 Printf("The CDB objects differ by %d bytes.", countDiff);
1821 return kFALSE;
1822 }
a37a1dfc 1823
5078a13f 1824 Printf("The CDB objects are the same in the two files.");
1825 return kTRUE;
a37a1dfc 1826}
1827
4667c116 1828//______________________________________________________________________________________________
5078a13f 1829void AliCDBManager::InitShortLived() {
1830// Init the list of short-lived objects
1831// currently disabled
1832
1833 fShortLived=0x0;
1834
1835 // fShortLived = new TList();
1836 // fShortLived->SetOwner(1);
1837 //
1838 // fShortLived->Add(new TObjString("EMCAL/Calib/Data"));
1839 //
1840 // fShortLived->Add(new TObjString("HMPID/Calib/Nmean"));
1841 // fShortLived->Add(new TObjString("HMPID/Calib/Qthre"));
1842 //
1843 // fShortLived->Add(new TObjString("ITS/Calib/CalibSPD"));
1844 //
1845 // fShortLived->Add(new TObjString("MUON/Calib/Gains"));
1846 // fShortLived->Add(new TObjString("MUON/Calib/HV"));
1847 // fShortLived->Add(new TObjString("MUON/Calib/Pedestals"));
1848 //
1849 // fShortLived->Add(new TObjString("PHOS/Calib/CpvGainPedestals"));
1850 // fShortLived->Add(new TObjString("PHOS/Calib/EmcGainPedestals"));
1851 //
1852 // fShortLived->Add(new TObjString("PMD/Calib/Data"));
1853 //
1854 // fShortLived->Add(new TObjString("TRD/Calib/ChamberGainFactor"));
1855 // fShortLived->Add(new TObjString("TRD/Calib/LocalGainFactor"));
1856 // fShortLived->Add(new TObjString("TRD/Calib/ChamberT0"));
1857 // fShortLived->Add(new TObjString("TRD/Calib/LocalT0"));
1858 // fShortLived->Add(new TObjString("TRD/Calib/ChamberVdrift"));
1859 // fShortLived->Add(new TObjString("TRD/Calib/LocalVdrift"));
1860 //
1861 // fShortLived->Add(new TObjString("ZDC/Calib/Data"));
4667c116 1862
1863}
1864
1865//______________________________________________________________________________________________
5078a13f 1866Bool_t AliCDBManager::IsShortLived(const char* path) {
1867// returns the name (string) of the data type
4667c116 1868
5078a13f 1869 if(!fShortLived) return kFALSE;
4667c116 1870
5078a13f 1871 AliCDBPath aPath(path);
1872 if(!aPath.IsValid()){
1873 AliError(Form("Not a valid path: %s", path));
1874 return kFALSE;
1875 }
4667c116 1876
5078a13f 1877 return fShortLived->Contains(path);
4667c116 1878
1879}
1880
1f341624 1881//______________________________________________________________________________________________
096d8e90 1882ULong64_t AliCDBManager::SetLock(Bool_t lock, ULong64_t key){
5078a13f 1883// To lock/unlock user must provide the key. A new key is provided after
1884// each successful lock. User should always backup the returned key and
1885// use it on next access.
a6f59240 1886 if (fLock == lock) return 0; // nothing to be done
1887 if (lock) {
1888 // User wants to lock - check his identity
1889 if (fKey) {
1890 // Lock has a user - check his key
1891 if (fKey != key) {
1892 AliFatal("Wrong key provided to lock CDB. Please remove CDB lock access from your code !");
1893 return 0;
5078a13f 1894 }
1895 }
1896 // Provide new key
a6f59240 1897 fKey = gSystem->Now();
1898 fLock = kTRUE;
1899 return fKey;
1900 }
1901 // User wants to unlock - check the provided key
1902 if (key != fKey) {
1903 AliFatal("Lock is ON: wrong key provided");
1904 return 0;
5078a13f 1905 }
a6f59240 1906 fLock = kFALSE;
5078a13f 1907 return key;
1f341624 1908}
1909
9e1ceb13 1910///////////////////////////////////////////////////////////
1911// AliCDBManager Parameter class //
1912// interface to specific AliCDBParameter class //
1913// (AliCDBGridParam, AliCDBLocalParam, AliCDBDumpParam) //
1914///////////////////////////////////////////////////////////
1915
62032124 1916AliCDBParam::AliCDBParam():
fe12e09c 1917 fType(),
1918 fURI()
62032124 1919{
5078a13f 1920 // constructor
9e1ceb13 1921
1922}
1923
1924//_____________________________________________________________________________
1925AliCDBParam::~AliCDBParam() {
5078a13f 1926 // destructor
9e1ceb13 1927
1928}
1929
c612d6cd 1930void AliCDBManager::ExtractBaseFolder(TString& url)
1931{
1932 // TBD RS
1933 // remove everything but the url -
1934 // Exact copy of the AliReconstuction::Rectify.... (to be removed)
1935 //
1936 //
1937 TString sbs;
1938 if (!(sbs=url("\\?User=[^?]*")).IsNull()) url.ReplaceAll(sbs,"");
1939 if (!(sbs=url("\\?DBFolder=[^?]*")).IsNull()) url.ReplaceAll("?DB","");
1940 if (!(sbs=url("\\?SE=[^?]*")).IsNull()) url.ReplaceAll(sbs,"");
1941 if (!(sbs=url("\\?CacheFolder=[^?]*")).IsNull()) url.ReplaceAll(sbs,"");
1942 if (!(sbs=url("\\?OperateDisconnected=[^?]*")).IsNull()) url.ReplaceAll(sbs,"");
1943 if (!(sbs=url("\\?CacheSize=[^?]*")).IsNull()) url.ReplaceAll(sbs,"");
1944 if (!(sbs=url("\\?CleanupInterval=[^?]*")).IsNull()) url.ReplaceAll(sbs,"");
1945 Bool_t slash=kFALSE,space=kFALSE;
1946 while ( (slash=url.EndsWith("/")) || (space=url.EndsWith(" ")) ) {
1947 if (slash) url = url.Strip(TString::kTrailing,'/');
1948 if (space) url = url.Strip(TString::kTrailing,' ');
1949 }
1950 //url.ToLower();
1951 //
1952}