]> git.uio.no Git - u/mrichter/AliRoot.git/blame - STEER/CDB/AliCDBManager.cxx
Add possibility to set specific (sub)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;
791 TString rndname = "/tmp/";
792 rndname += "OCDBFolderXML.";
793 rndname += uuid.AsString();
794 rndname += ".xml";
795 AliDebug(2, Form("file to be copied = %s", fgkOCDBFolderXMLfile.Data()));
796 if (!TFile::Cp(fgkOCDBFolderXMLfile.Data(), rndname.Data())) {
797 AliFatal(Form("Cannot make a local copy of OCDBFolder xml file in %s",rndname.Data()));
798 }
799 AliCDBHandler* saxcdb = new AliCDBHandler();
800 saxcdb->SetRun(run);
801 TSAXParser *saxParser = new TSAXParser();
802 saxParser->ConnectToHandler("AliCDBHandler", saxcdb);
803 saxParser->ParseFile(rndname.Data());
804 AliInfo(Form(" LHC folder = %s", saxcdb->GetOCDBFolder().Data()));
805 AliInfo(Form(" LHC period start run = %d", saxcdb->GetStartRunRange()));
806 AliInfo(Form(" LHC period end run = %d", saxcdb->GetEndRunRange()));
807 lhcPeriod = saxcdb->GetOCDBFolder();
808 startRun = saxcdb->GetStartRunRange();
809 endRun = saxcdb->GetEndRunRange();
810}
5078a13f 811
112ba4b4 812//_____________________________________________________________________________
813void AliCDBManager::GetLHCPeriodAgainstCvmfsFile(Int_t run, TString& lhcPeriod, Int_t& startRun, Int_t& endRun) {
814// set LHC period (year + first, last run) comparing run number and CVMFS file
815// We don't want to connect to AliEn just to set the uri from the runnumber
816// for that we use the script getUriFromYear.sh in the cvmfs AliRoot package
817
818 TString getYearScript(fCvmfsOcdb);
819 getYearScript = getYearScript.Strip(TString::kTrailing, '/');
820 getYearScript.Append("/bin/getUriFromYear.sh");
821 if (gSystem->AccessPathName(getYearScript))
822 AliFatal(Form("Cannot find valid script: %s", getYearScript.Data()));
823 TString inoutFile(gSystem->WorkingDirectory());
824 inoutFile += "/uri_range_";
825 inoutFile += TString::Itoa(run,10);
826 TString command(getYearScript);
827 command += ' ';
828 command += TString::Itoa(run,10);
829 command += Form(" > %s", inoutFile.Data());
830 AliDebug(3, Form("Running command: \"%s\"",command.Data()));
831 Int_t result = gSystem->Exec(command.Data());
832 if(result != 0) {
833 AliFatal(Form("Was not able to execute \"%s\"", command.Data()));
834 }
835
836 // now read the file with the uri and first and last run
837 std::ifstream file(inoutFile.Data());
838 if (!file.is_open()) {
839 AliFatal(Form("Error opening file \"%s\"!", inoutFile.Data()));
840 }
841 TString line;
842 TObjArray* oStringsArray = 0;
843 while (line.ReadLine(file)){
844 oStringsArray = line.Tokenize(' ');
845 }
846 TObjString *oStrUri = dynamic_cast<TObjString*> (oStringsArray->At(0));
847 TObjString *oStrFirst = dynamic_cast<TObjString*> (oStringsArray->At(1));
848 TString firstRun = oStrFirst->GetString();
849 TObjString *oStrLast = dynamic_cast<TObjString*> (oStringsArray->At(2));
850 TString lastRun = oStrLast->GetString();
851
852 lhcPeriod = oStrUri->GetString();
853 startRun = firstRun.Atoi();
854 endRun = lastRun.Atoi();
855
856 file.close();
7de84b33 857}
858
4667c116 859//_____________________________________________________________________________
1f341624 860void AliCDBManager::UnsetDefaultStorage() {
861// Unset default storage
5078a13f 862
863 // if lock is ON, action is forbidden!
864 if(fLock) {
865 if (fDefaultStorage) {
866 AliFatal("Lock is ON: cannot unset default storage!");
867 }
868 }
869
870 if (fDefaultStorage) {
871 AliWarning("Clearing cache!");
872 ClearCache();
873 }
874
875 fRun = fStartRunLHCPeriod = fEndRunLHCPeriod = -1;
876 fRaw = kFALSE;
877
878 fDefaultStorage = 0x0;
4667c116 879}
9e1ceb13 880
02c4845e 881//_____________________________________________________________________________
d8734d06 882void AliCDBManager::SetSpecificStorage(const char* calibType, const char* dbString, Int_t version, Int_t subVersion) {
024cf675 883// sets storage specific for detector or calibration type (works with AliCDBManager::Get(...))
02c4845e 884
5078a13f 885 AliCDBParam *aPar = CreateParameter(dbString);
886 if(!aPar) return;
d8734d06 887 SetSpecificStorage(calibType, aPar, version, subVersion);
5078a13f 888 delete aPar;
02c4845e 889}
890
891//_____________________________________________________________________________
d8734d06 892void AliCDBManager::SetSpecificStorage(const char* calibType, const AliCDBParam* param, Int_t version, Int_t subVersion) {
024cf675 893// sets storage specific for detector or calibration type (works with AliCDBManager::Get(...))
894// Default storage should be defined prior to any specific storages, e.g.:
895// AliCDBManager::instance()->SetDefaultStorage("alien://");
c3a7b59a 896// AliCDBManager::instance()->SetSpecificStorage("TPC/*","local://DB_TPC");
897// AliCDBManager::instance()->SetSpecificStorage("*/Align/*","local://DB_TPCAlign");
898// calibType must be a valid CDB path! (3 level folder structure)
d8734d06 899// Specific version/subversion is set in the uniqueid of the TObjString used as key
7de84b33 900
5078a13f 901 if(!fDefaultStorage && !fRaw) {
902 AliError("Please activate a default storage first!");
903 return;
904 }
4005d0b5 905
5078a13f 906 AliCDBPath aPath(calibType);
907 if(!aPath.IsValid()){
908 AliError(Form("Not a valid path: %s", calibType));
909 return;
910 }
62032124 911
5078a13f 912 TObjString *objCalibType = new TObjString(aPath.GetPath());
913 if(fSpecificStorages.Contains(objCalibType)){
914 AliWarning(Form("Storage \"%s\" already activated! It will be replaced by the new one",
915 calibType));
916 AliCDBParam *checkPar = dynamic_cast<AliCDBParam*> (fSpecificStorages.GetValue(calibType));
917 if(checkPar) delete checkPar;
918 delete fSpecificStorages.Remove(objCalibType);
919 }
920 AliCDBStorage *aStorage = GetStorage(param);
921 if(!aStorage) return;
96b7636d 922
d8734d06 923 // Set the unique id of the TObjString used as key to store specific version/subversion
924 UInt_t uId = ((subVersion+1)<<16) + (version+1);
925 AliCDBParam *specificParam = param->CloneParam();
926 specificParam->SetUniqueID(uId);
927 fSpecificStorages.Add(objCalibType, specificParam);
b21e3194 928
5078a13f 929 if(fStorageMap->Contains(objCalibType)){
930 delete fStorageMap->Remove(objCalibType);
931 }
932 fStorageMap->Add(objCalibType->Clone(), new TObjString(param->GetURI()));
b21e3194 933
02c4845e 934}
935
936//_____________________________________________________________________________
024cf675 937AliCDBStorage* AliCDBManager::GetSpecificStorage(const char* calibType) {
5078a13f 938// get storage specific for detector or calibration type
4b5e0dce 939
5078a13f 940 AliCDBPath calibPath(calibType);
941 if(!calibPath.IsValid()) return NULL;
96b7636d 942
5078a13f 943 AliCDBParam *checkPar = (AliCDBParam*) fSpecificStorages.GetValue(calibPath.GetPath());
944 if(!checkPar){
945 AliError(Form("%s storage not found!", calibType));
946 return NULL;
947 } else {
948 return GetStorage(checkPar);
949 }
96b7636d 950
02c4845e 951}
952
024cf675 953//_____________________________________________________________________________
954AliCDBParam* AliCDBManager::SelectSpecificStorage(const TString& path) {
82096dfc 955// select storage valid for path from the list of specific storages
024cf675 956
5078a13f 957 AliCDBPath aPath(path);
958 if(!aPath.IsValid()) return NULL;
959
960 TIter iter(&fSpecificStorages);
961 TObjString *aCalibType=0;
962 AliCDBPath tmpPath("null/null/null");
963 AliCDBParam* aPar=0;
964 while((aCalibType = (TObjString*) iter.Next())){
965 AliCDBPath calibTypePath(aCalibType->GetName());
966 if(calibTypePath.Comprises(aPath)) {
967 if(calibTypePath.Comprises(tmpPath)) continue;
968 aPar = (AliCDBParam*) fSpecificStorages.GetValue(aCalibType);
969 tmpPath.SetPath(calibTypePath.GetPath());
970 }
971 }
972 return aPar;
024cf675 973}
974
02c4845e 975//_____________________________________________________________________________
4667c116 976AliCDBEntry* AliCDBManager::Get(const AliCDBPath& path, Int_t runNumber,
5078a13f 977 Int_t version, Int_t subVersion) {
978 // get an AliCDBEntry object from the database
979
980 if(runNumber < 0){
981 // RunNumber is not specified. Try with fRun
982 if (fRun < 0){
983 AliError("Run number neither specified in query nor set in AliCDBManager! Use AliCDBManager::SetRun.");
984 return NULL;
985 }
986 runNumber = fRun;
987 }
024cf675 988
5078a13f 989 return Get(AliCDBId(path, runNumber, runNumber, version, subVersion));
02c4845e 990}
991
992//_____________________________________________________________________________
4667c116 993AliCDBEntry* AliCDBManager::Get(const AliCDBPath& path,
5078a13f 994 const AliCDBRunRange& runRange, Int_t version,
995 Int_t subVersion) {
996 // get an AliCDBEntry object from the database!
02c4845e 997
5078a13f 998 return Get(AliCDBId(path, runRange, version, subVersion));
02c4845e 999}
1000
1001//_____________________________________________________________________________
d8734d06 1002AliCDBEntry* AliCDBManager::Get(const AliCDBId& queryId, Bool_t forceCaching) {
02c4845e 1003// get an AliCDBEntry object from the database
5078a13f 1004
d8734d06 1005 // check if queryId's path and runRange are valid
1006 // queryId is invalid also if version is not specified and subversion is!
1007 if (!queryId.IsValid()) {
1008 AliError(Form("Invalid query: %s", queryId.ToString().Data()));
5078a13f 1009 return NULL;
1010 }
1011
d8734d06 1012 // queryId is not specified if path contains wildcard or run range= [-1,-1]
1013 if (!queryId.IsSpecified()) {
5078a13f 1014 AliError(Form("Unspecified query: %s",
d8734d06 1015 queryId.ToString().Data()));
5078a13f 1016 return NULL;
1017 }
1018
d8734d06 1019 if(fLock && !(fRun >= queryId.GetFirstRun() && fRun <= queryId.GetLastRun()))
5078a13f 1020 AliFatal("Lock is ON: cannot use different run number than the internal one!");
1021
d8734d06 1022 if(fCache && !(fRun >= queryId.GetFirstRun() && fRun <= queryId.GetLastRun()))
5078a13f 1023 AliWarning("Run number explicitly set in query: CDB cache temporarily disabled!");
1024
1025 AliCDBEntry *entry=0;
1026
1027 // first look into map of cached objects
d8734d06 1028 if(fCache && queryId.GetFirstRun() == fRun)
1029 entry = (AliCDBEntry*) fEntryCache.GetValue(queryId.GetPath());
5078a13f 1030 if(entry) {
d8734d06 1031 AliDebug(2, Form("Object %s retrieved from cache !!",queryId.GetPath().Data()));
5078a13f 1032 return entry;
1033 }
1034
1035 // if snapshot flag is set, try getting from the snapshot
1036 // but in the case a specific storage is specified for this path
d8734d06 1037 AliCDBParam *aPar=SelectSpecificStorage(queryId.GetPath());
5078a13f 1038 if(!aPar){
d8734d06 1039 if(fSnapshotMode && queryId.GetFirstRun() == fRun)
5078a13f 1040 {
d8734d06 1041 entry = GetEntryFromSnapshot(queryId.GetPath());
5078a13f 1042 if(entry) {
d8734d06 1043 AliInfo(Form("Object \"%s\" retrieved from the snapshot.",queryId.GetPath().Data()));
1044 if(queryId.GetFirstRun() == fRun) // no need to check fCache, fSnapshotMode not possible otherwise
1045 CacheEntry(queryId.GetPath(), entry);
5078a13f 1046
1047 if(!fIds->Contains(&entry->GetId()))
1048 fIds->Add(entry->GetId().Clone());
1049
1050 return entry;
1051 }
1052 }
1053 }
1054
1055 // Entry is not in cache (and, in case we are in snapshot mode, not in the snapshot either)
1056 // => retrieve it from the storage and cache it!!
1057 if(!fDefaultStorage) {
1058 AliError("No storage set!");
1059 return NULL;
1060 }
1061
d8734d06 1062 Int_t version = 0, subVersion = 0;
5078a13f 1063 AliCDBStorage *aStorage=0;
1064 if(aPar) {
1065 aStorage=GetStorage(aPar);
1066 TString str = aPar->GetURI();
d8734d06 1067 UInt_t uId = aPar->GetUniqueID();
1068 version = Int_t(uId&0xffff) - 1;
1069 subVersion = Int_t(uId>>16) - 1;
5078a13f 1070 AliDebug(2,Form("Looking into storage: %s",str.Data()));
1071 } else {
1072 aStorage=GetDefaultStorage();
1073 AliDebug(2,"Looking into default storage");
1074 }
1075
d8734d06 1076 AliCDBId finalQueryId(queryId);
1077 if(version >= 0) {
1078 AliDebug(2,Form("Specific version set to: %d", version));
1079 finalQueryId.SetVersion(version);
1080 }
1081 if(subVersion >= 0) {
1082 AliDebug(2,Form("Specific subversion set to: %d", subVersion));
1083 finalQueryId.SetSubVersion(subVersion);
1084 }
1085 entry = aStorage->Get(finalQueryId);
5078a13f 1086
d8734d06 1087 if(entry && fCache && (queryId.GetFirstRun()==fRun || forceCaching)){
1088 CacheEntry(queryId.GetPath(), entry);
5078a13f 1089 }
1090
1091 if(entry && !fIds->Contains(&entry->GetId())){
1092 fIds->Add(entry->GetId().Clone());
1093 }
e973a743 1094
5078a13f 1095 return entry;
4667c116 1096}
1097
c022b397 1098//_____________________________________________________________________________
1099AliCDBEntry* AliCDBManager::GetEntryFromSnapshot(const char* path) {
5078a13f 1100// get the entry from the open snapshot file
c022b397 1101
5078a13f 1102 TString sPath(path);
1103 sPath.ReplaceAll("/","*");
1104 if(!fSnapshotFile){
1105 AliError("No snapshot file is open!");
1106 return 0;
1107 }
1108 AliCDBEntry *entry = dynamic_cast<AliCDBEntry*>(fSnapshotFile->Get(sPath.Data()));
1109 if(!entry){
1110 AliDebug(2,Form("Cannot get a CDB entry for \"%s\" from snapshot file",path));
1111 return 0;
1112 }
c022b397 1113
5078a13f 1114 return entry;
c022b397 1115}
1116
1117//_____________________________________________________________________________
1118Bool_t AliCDBManager::SetSnapshotMode(const char* snapshotFileName) {
1119// set the manager in snapshot mode
c022b397 1120
5078a13f 1121 if(!fCache){
1122 AliError("Cannot set the CDB manage in snapshot mode if the cache is not active!");
1123 return kFALSE;
1124 }
c022b397 1125
5078a13f 1126 //open snapshot file
1127 TString snapshotFile(snapshotFileName);
1128 if(snapshotFile.BeginsWith("alien://")){
1129 if(!gGrid) {
1130 TGrid::Connect("alien://","");
1131 if(!gGrid) {
1132 AliError("Connection to alien failed!");
1133 return kFALSE;
1134 }
c022b397 1135 }
5078a13f 1136 }
c022b397 1137
5078a13f 1138 fSnapshotFile = TFile::Open(snapshotFileName);
1139 if (!fSnapshotFile || fSnapshotFile->IsZombie()){
1140 AliError(Form("Cannot open file %s",snapshotFileName));
1141 return kFALSE;
1142 }
1143
1144 AliInfo("The CDB manager is set in snapshot mode!");
1145 fSnapshotMode = kTRUE;
1146 return kTRUE;
c022b397 1147
1148}
1149
0f40a831 1150//_____________________________________________________________________________
1151const char* AliCDBManager::GetURI(const char* path) {
1152// return the URI of the storage where to look for path
1153
5078a13f 1154 if(!IsDefaultStorageSet()) return 0;
1155
1156 AliCDBParam *aPar=SelectSpecificStorage(path);
0f40a831 1157
5078a13f 1158 if(aPar) {
1159 return aPar->GetURI().Data();
0f40a831 1160
5078a13f 1161 } else {
1162 return GetDefaultStorage()->GetURI().Data();
1163 }
1164
1165 return 0;
0f40a831 1166}
1167
b7df0cc5 1168//_____________________________________________________________________________
1169Int_t AliCDBManager::GetStartRunLHCPeriod(){
5078a13f 1170// get the first run of validity
1171// for the current period
1172// if set
1173 if(fStartRunLHCPeriod==-1)
1174 AliWarning("Run-range not yet set for the current LHC period.");
1175 return fStartRunLHCPeriod;
b7df0cc5 1176}
1177
1178//_____________________________________________________________________________
1179Int_t AliCDBManager::GetEndRunLHCPeriod(){
5078a13f 1180// get the last run of validity
1181// for the current period
1182// if set
1183 if(fEndRunLHCPeriod==-1)
1184 AliWarning("Run-range not yet set for the current LHC period.");
1185 return fEndRunLHCPeriod;
b7df0cc5 1186}
1187
1188//_____________________________________________________________________________
1189TString AliCDBManager::GetLHCPeriod(){
5078a13f 1190// get the current LHC period string
1191//
1192 if(fLHCPeriod.IsWhitespace() || fLHCPeriod.IsNull())
1193 AliWarning("LHC period (OCDB folder) not yet set");
1194 return fLHCPeriod;
b7df0cc5 1195}
1196
4667c116 1197//_____________________________________________________________________________
1198AliCDBId* AliCDBManager::GetId(const AliCDBPath& path, Int_t runNumber,
5078a13f 1199 Int_t version, Int_t subVersion) {
1200 // get the AliCDBId of the valid object from the database (does not retrieve the object)
1201 // User must delete returned object!
1202
1203 if(runNumber < 0){
1204 // RunNumber is not specified. Try with fRun
1205 if (fRun < 0){
1206 AliError("Run number neither specified in query nor set in AliCDBManager! Use AliCDBManager::SetRun.");
1207 return NULL;
1208 }
1209 runNumber = fRun;
1210 }
4667c116 1211
5078a13f 1212 return GetId(AliCDBId(path, runNumber, runNumber, version, subVersion));
4667c116 1213}
1214
1215//_____________________________________________________________________________
1216AliCDBId* AliCDBManager::GetId(const AliCDBPath& path,
5078a13f 1217 const AliCDBRunRange& runRange, Int_t version,
1218 Int_t subVersion) {
1219 // get the AliCDBId of the valid object from the database (does not retrieve the object)
1220 // User must delete returned object!
4667c116 1221
5078a13f 1222 return GetId(AliCDBId(path, runRange, version, subVersion));
4667c116 1223}
1224
1225//_____________________________________________________________________________
1226AliCDBId* AliCDBManager::GetId(const AliCDBId& query) {
1227// get the AliCDBId of the valid object from the database (does not retrieve the object)
1228// User must delete returned object!
1229
5078a13f 1230 if(!fDefaultStorage) {
1231 AliError("No storage set!");
1232 return NULL;
1233 }
1234
1235 // check if query's path and runRange are valid
1236 // query is invalid also if version is not specified and subversion is!
1237 if (!query.IsValid()) {
1238 AliError(Form("Invalid query: %s", query.ToString().Data()));
1239 return NULL;
1240 }
1241
1242 // query is not specified if path contains wildcard or run range= [-1,-1]
1243 if (!query.IsSpecified()) {
1244 AliError(Form("Unspecified query: %s",
1245 query.ToString().Data()));
1246 return NULL;
1247 }
1248
1249 if(fCache && query.GetFirstRun() != fRun)
1250 AliWarning("Run number explicitly set in query: CDB cache temporarily disabled!");
1251
1252 AliCDBEntry* entry = 0;
1253
1254 // first look into map of cached objects
1255 if(fCache && query.GetFirstRun() == fRun)
1256 entry = (AliCDBEntry*) fEntryCache.GetValue(query.GetPath());
1257
1258 if(entry) {
1259 AliDebug(2, Form("Object %s retrieved from cache !!",query.GetPath().Data()));
1260 return dynamic_cast<AliCDBId*> (entry->GetId().Clone());
1261 }
1262
1263 // Entry is not in cache -> retrieve it from CDB and cache it!!
1264 AliCDBStorage *aStorage=0;
1265 AliCDBParam *aPar=SelectSpecificStorage(query.GetPath());
1266
1267 if(aPar) {
1268 aStorage=GetStorage(aPar);
1269 TString str = aPar->GetURI();
1270 AliDebug(2,Form("Looking into storage: %s",str.Data()));
1271
1272 } else {
1273 aStorage=GetDefaultStorage();
1274 AliDebug(2,"Looking into default storage");
1275 }
1276
1277 return aStorage->GetId(query);
4667c116 1278
02c4845e 1279}
1280
1281//_____________________________________________________________________________
62032124 1282TList* AliCDBManager::GetAll(const AliCDBPath& path, Int_t runNumber,
5078a13f 1283 Int_t version, Int_t subVersion) {
1284 // get multiple AliCDBEntry objects from the database
1285
1286 if(runNumber < 0){
1287 // RunNumber is not specified. Try with fRun
1288 if (fRun < 0){
1289 AliError("Run number neither specified in query nor set in AliCDBManager! Use AliCDBManager::SetRun.");
1290 return NULL;
1291 }
1292 runNumber = fRun;
1293 }
afd8dbf4 1294
5078a13f 1295 return GetAll(AliCDBId(path, runNumber, runNumber, version,
1296 subVersion));
02c4845e 1297}
1298
1299//_____________________________________________________________________________
8e245d15 1300TList* AliCDBManager::GetAll(const AliCDBPath& path,
5078a13f 1301 const AliCDBRunRange& runRange, Int_t version, Int_t subVersion) {
1302 // get multiple AliCDBEntry objects from the database
02c4845e 1303
5078a13f 1304 return GetAll(AliCDBId(path, runRange, version, subVersion));
02c4845e 1305}
1306
1307//_____________________________________________________________________________
1308TList* AliCDBManager::GetAll(const AliCDBId& query) {
1309// get multiple AliCDBEntry objects from the database
024cf675 1310// Warning: this method works correctly only for queries of the type "Detector/*"
1311// and not for more specific queries e.g. "Detector/Calib/*" !
8e245d15 1312// Warning #2: Entries are cached, but GetAll will keep on retrieving objects from OCDB!
1313// To get an object from cache use Get() function
02c4845e 1314
5078a13f 1315 if(!fDefaultStorage) {
1316 AliError("No storage set!");
1317 return NULL;
1318 }
1319
1320 if (!query.IsValid()) {
1321 AliError(Form("Invalid query: %s", query.ToString().Data()));
1322 return NULL;
1323 }
1324
1325 if((fSpecificStorages.GetEntries()>0) && query.GetPath().BeginsWith('*')){
1326 // if specific storages are active a query with "*" is ambiguous
1327 AliError("Query too generic in this context!");
1328 return NULL;
1329 }
1330
1331 if (query.IsAnyRange()) {
1332 AliError(Form("Unspecified run or runrange: %s",
1333 query.ToString().Data()));
1334 return NULL;
1335 }
1336
1337 if(fLock && query.GetFirstRun() != fRun)
1338 AliFatal("Lock is ON: cannot use different run number than the internal one!");
1339
1340 AliCDBParam *aPar=SelectSpecificStorage(query.GetPath());
1341
1342 AliCDBStorage *aStorage;
1343 if(aPar) {
1344 aStorage=GetStorage(aPar);
1345 AliDebug(2,Form("Looking into storage: %s", aPar->GetURI().Data()));
1346
1347 } else {
1348 aStorage=GetDefaultStorage();
1349 AliDebug(2,Form("Looking into default storage: %s", aStorage->GetURI().Data()));
1350 }
1351
1352 TList *result = 0;
1353 if(aStorage) result = aStorage->GetAll(query);
1354 if(!result) return 0;
1355
1356 // loop on result to check whether entries should be re-queried with specific storages
1357 if(fSpecificStorages.GetEntries()>0 && ! (fSpecificStorages.GetEntries() == 1 && aPar)) {
1358 AliInfo("Now look into all other specific storages...");
1359
1360 TIter iter(result);
1361 AliCDBEntry* chkEntry=0;
1362
1363 while((chkEntry = dynamic_cast<AliCDBEntry*> (iter.Next()))){
1364 AliCDBId& chkId = chkEntry->GetId();
1365 AliDebug(2, Form("Checking id %s ", chkId.GetPath().Data()));
1366 AliCDBParam *chkPar=SelectSpecificStorage(chkId.GetPath());
1367 if (!chkPar || aPar == chkPar) continue;
1368 AliCDBStorage *chkStorage = GetStorage(chkPar);
1369 AliDebug(2, Form("Found specific storage! %s", chkPar->GetURI().Data()));
1370
1371 AliCDBEntry *newEntry=0;
1372 chkId.SetRunRange(query.GetFirstRun(), query.GetLastRun());
1373 chkId.SetVersion(query.GetVersion());
1374 chkId.SetSubVersion(query.GetSubVersion());
1375
1376 if(chkStorage) newEntry = chkStorage->Get(chkId);
1377 if(!newEntry) continue;
1378
1379 // object is found in specific storage: replace entry in the result list!
1380 chkEntry->SetOwner(1);
1381 delete result->Remove(chkEntry);
1382 result->AddFirst(newEntry);
1383 }
1384
1385 Int_t nEntries = result->GetEntries();
1386 AliInfo("After look into other specific storages, result list is:");
1387 for(int i=0; i<nEntries;i++){
1388 AliCDBEntry *entry = (AliCDBEntry*) result->At(i);
1389 AliInfo(Form("%s",entry->GetId().ToString().Data()));
1390 }
1391 }
1392
1393 // caching entries
1394 TIter iter(result);
1395 AliCDBEntry* entry=0;
1396 while((entry = dynamic_cast<AliCDBEntry*> (iter.Next()))){
1397
1398 if(!fIds->Contains(&entry->GetId())){
1399 fIds->Add(entry->GetId().Clone());
1400 }
1401 if(fCache && (query.GetFirstRun() == fRun)){
1402 CacheEntry(entry->GetId().GetPath(), entry);
1403 }
1404 }
1405
1406
1407 return result;
02c4845e 1408}
1409
1410//_____________________________________________________________________________
b1bfa962 1411Bool_t AliCDBManager::Put(TObject* object, const AliCDBId& id, AliCDBMetaData* metaData, const char* mirrors, DataType type){
02c4845e 1412// store an AliCDBEntry object into the database
1413
5078a13f 1414 if (object==0x0) {
1415 AliError("Null Entry! No storage will be done!");
1416 return kFALSE;
1417 }
968cfa08 1418
5078a13f 1419 AliCDBEntry anEntry(object, id, metaData);
1420 return Put(&anEntry, mirrors, type);
02c4845e 1421
1422}
1423
1424
1425//_____________________________________________________________________________
06eeadea 1426Bool_t AliCDBManager::Put(AliCDBEntry* entry, const char* mirrors, DataType type){
02c4845e 1427// store an AliCDBEntry object into the database
1428
5078a13f 1429 if(type == kPrivate && !fDefaultStorage) {
1430 AliError("No storage set!");
1431 return kFALSE;
1432 }
1433
1434 if (!entry){
1435 AliError("No entry!");
1436 return kFALSE;
1437 }
1438
1439 if (entry->GetObject()==0x0){
1440 AliError("No valid object in CDB entry!");
1441 return kFALSE;
1442 }
1443
1444 if (!entry->GetId().IsValid()) {
1445 AliError(Form("Invalid entry ID: %s",
1446 entry->GetId().ToString().Data()));
1447 return kFALSE;
1448 }
1449
1450 if (!entry->GetId().IsSpecified()) {
1451 AliError(Form("Unspecified entry ID: %s",
1452 entry->GetId().ToString().Data()));
1453 return kFALSE;
1454 }
1455
1456 AliCDBId id = entry->GetId();
1457 AliCDBParam *aPar = SelectSpecificStorage(id.GetPath());
1458
1459 AliCDBStorage *aStorage=0;
1460
1461 if(aPar) {
1462 aStorage=GetStorage(aPar);
1463 } else {
1464 switch(type){
1465 case kCondition:
1466 aStorage = GetStorage(fCondParam);
1467 break;
1468 case kReference:
1469 aStorage = GetStorage(fRefParam);
1470 break;
1471 case kPrivate:
1472 aStorage = GetDefaultStorage();
1473 break;
1474 }
1475 }
1476
1477 AliDebug(2,Form("Storing object into storage: %s", aStorage->GetURI().Data()));
1478
1479 TString strMirrors(mirrors);
1480 Bool_t result = kFALSE;
1481 if(!strMirrors.IsNull() && !strMirrors.IsWhitespace())
1482 result = aStorage->Put(entry, mirrors, type);
1483 else
1484 result = aStorage->Put(entry, "", type);
1485
1486 if(fRun >= 0) QueryCDB();
1487
1488 return result;
02c4845e 1489
1490
1491}
9e1ceb13 1492
c5941d67 1493//_____________________________________________________________________________
5078a13f 1494void AliCDBManager::SetMirrorSEs(const char* mirrors) {
c5941d67 1495// set mirror Storage Elements for the default storage, if it is of type "alien"
5078a13f 1496 if(fDefaultStorage->GetType() != "alien"){
1497 AliInfo("The default storage is not of type \"alien\". Settings for Storage Elements are not taken into account!");
1498 return;
1499 }
1500 fDefaultStorage->SetMirrorSEs(mirrors);
c5941d67 1501}
1502
1503//_____________________________________________________________________________
1504const char* AliCDBManager::GetMirrorSEs() const {
1505// get mirror Storage Elements for the default storage, if it is of type "alien"
5078a13f 1506 if(fDefaultStorage->GetType() != "alien"){
1507 AliInfo("The default storage is not of type \"alien\". Settings for Storage Elements are not taken into account!");
1508 return "";
1509 }
1510 return fDefaultStorage->GetMirrorSEs();
c5941d67 1511}
1512
4b5e0dce 1513//_____________________________________________________________________________
5078a13f 1514void AliCDBManager::CacheEntry(const char* path, AliCDBEntry* entry) {
4b5e0dce 1515// cache AliCDBEntry. Cache is valid until run number is changed.
1516
5078a13f 1517 AliCDBEntry *chkEntry = dynamic_cast<AliCDBEntry*> (fEntryCache.GetValue(path));
96b7636d 1518
5078a13f 1519 if(chkEntry) {
1520 AliDebug(2, Form("Object %s already in cache !!", path));
1521 return;
1522 } else {
1523 AliDebug(2,Form("Caching entry %s", path));
1524 }
96b7636d 1525
5078a13f 1526 fEntryCache.Add(new TObjString(path), entry);
1527 AliDebug(2,Form("Cache entries: %d", fEntryCache.GetEntries()));
4b5e0dce 1528
1529}
1530
917a098b 1531//_____________________________________________________________________________
5078a13f 1532void AliCDBManager::Print(Option_t* /*option*/) const {
917a098b 1533// Print list of active storages and their URIs
917a098b 1534
5078a13f 1535 TString output=Form("Run number = %d; ",fRun);
1536 output += "Cache is ";
1537 if(!fCache) output += "NOT ";
1538 output += Form("ACTIVE; Number of active storages: %d\n",fActiveStorages.GetEntries());
1539
1540 if(fDefaultStorage) {
1541 output += Form("\t*** Default Storage URI: \"%s\"\n",fDefaultStorage->GetURI().Data());
1542 // AliInfo(output.Data());
1543 }
1544 if(fSpecificStorages.GetEntries()>0) {
1545 TIter iter(fSpecificStorages.GetTable());
1546 TPair *aPair=0;
1547 Int_t i=1;
1548 while((aPair = (TPair*) iter.Next())){
1549 output += Form("\t*** Specific storage %d: Path \"%s\" -> URI \"%s\"\n",
1550 i++, ((TObjString*) aPair->Key())->GetName(),
1551 ((AliCDBParam*) aPair->Value())->GetURI().Data());
1552 }
1553 }
1554 if(fDrainStorage) {
1555 output += Form("*** Drain Storage URI: %s\n",fDrainStorage->GetURI().Data());
1556 }
1557 AliInfo(output.Data());
917a098b 1558}
1559
4b5e0dce 1560//_____________________________________________________________________________
5078a13f 1561void AliCDBManager::SetRun(Int_t run) {
917a098b 1562// Sets current run number.
4b5e0dce 1563// When the run number changes the caching is cleared.
5078a13f 1564
1565 if(fRun == run)
1566 return;
1567
1568 if(fLock && fRun >= 0) {
1569 AliFatal("Lock is ON, cannot reset run number!");
1570 }
1571
1572 fRun = run;
1573
1574 if (fRaw) {
1575 // here the LHCPeriod xml file is parsed; the string containing the correct period is returned; the default storage is set
1576 if (fStartRunLHCPeriod <= run && fEndRunLHCPeriod >= run){
1577 AliInfo("LHCPeriod alien folder for current run already in memory");
1578 }else{
112ba4b4 1579 SetDefaultStorageFromRun(fRun);
5078a13f 1580 if(fEntryCache.GetEntries()!=0) ClearCache();
1581 return;
1582 }
1583 }
1584 ClearCache();
1585 QueryCDB();
4b5e0dce 1586}
1587
1588//_____________________________________________________________________________
1589void AliCDBManager::ClearCache(){
1590// clear AliCDBEntry cache
1591
5078a13f 1592 AliDebug(2, Form("Cache entries to be deleted: %d",fEntryCache.GetEntries()));
1593
1594 /*
1595 // To clean entries one by one
1596 TIter iter(fEntryCache.GetTable());
1597 TPair* pair=0;
1598 while((pair= dynamic_cast<TPair*> (iter.Next()))){
1599
1600 TObjString* key = dynamic_cast<TObjString*> (pair->Key());
1601 AliCDBEntry* entry = dynamic_cast<AliCDBEntry*> (pair->Value());
1602 AliDebug(2, Form("Deleting entry: %s", key->GetName()));
1603 if (entry) delete entry;
1604 delete fEntryCache.Remove(key);
1605 }
1606 */
1607 fEntryCache.DeleteAll();
1608 AliDebug(2, Form("After deleting - Cache entries: %d",fEntryCache.GetEntries()));
4b5e0dce 1609}
1610
4005d0b5 1611//_____________________________________________________________________________
1612void AliCDBManager::UnloadFromCache(const char* path){
1613// unload cached object
b0e8cda6 1614// that is remove the entry from the cache and the id from the list of ids
1615//
5078a13f 1616 if(!fActiveStorages.GetEntries()) {
1617 AliDebug(2, Form("No active storages. Object \"%s\" is not unloaded from cache", path));
1618 return;
1619 }
1620
1621 AliCDBPath queryPath(path);
1622 if(!queryPath.IsValid()) return;
1623
1624 if(!queryPath.IsWildcard()) { // path is not wildcard, get it directly from the cache and unload it!
1625 if(fEntryCache.Contains(path)){
1626 AliDebug(2, Form("Unloading object \"%s\" from cache and from list of ids", path));
1627 TObjString pathStr(path);
1628 delete fEntryCache.Remove(&pathStr);
1629 // we do not remove from the list of Id's (it's not very coherent but we leave the
1630 // id for the benefit of the userinfo)
1631 /*
1632 TIter iter(fIds);
1633 AliCDBId *id = 0;
1634 while((id = dynamic_cast<AliCDBId*> (iter.Next()))){
1635 if(queryPath.Comprises(id->GetPath()))
1636 delete fIds->Remove(id);
1637 }*/
1638 } else {
1639 AliWarning(Form("Cache does not contain object \"%s\"!", path));
1640 }
1641 AliDebug(2, Form("Cache entries: %d",fEntryCache.GetEntries()));
1642 return;
1643 }
1644
1645 // path is wildcard: loop on the cache and unload all comprised objects!
1646 TIter iter(fEntryCache.GetTable());
1647 TPair* pair = 0;
1648 Int_t removed=0;
1649
1650 while((pair = dynamic_cast<TPair*> (iter.Next()))){
1651 AliCDBPath entryPath = pair->Key()->GetName();
1652 if(queryPath.Comprises(entryPath)) {
1653 AliDebug(2, Form("Unloading object \"%s\" from cache and from list of ids", entryPath.GetPath().Data()));
1654 TObjString pathStr(entryPath.GetPath());
1655 delete fEntryCache.Remove(&pathStr);
1656 removed++;
1657
1658 // we do not remove from the list of Id's (it's not very coherent but we leave the
1659 // id for the benefit of the userinfo)
1660 /*
1661 TIter iterids(fIds);
1662 AliCDBId *anId = 0;
1663 while((anId = dynamic_cast<AliCDBId*> (iterids.Next()))){
1664 AliCDBPath aPath = anId->GetPath();
1665 TString aPathStr = aPath.GetPath();
1666 if(queryPath.Comprises(aPath)) {
1667 delete fIds->Remove(anId);
1668 }
1669 }*/
1670 }
1671 }
1672 AliDebug(2,Form("Cache entries and ids removed: %d Remaining: %d",removed,fEntryCache.GetEntries()));
4005d0b5 1673}
1674
9e1ceb13 1675//_____________________________________________________________________________
1676void AliCDBManager::DestroyActiveStorages() {
1677// delete list of active storages
1678
5078a13f 1679 fActiveStorages.DeleteAll();
1680 fSpecificStorages.DeleteAll();
9e1ceb13 1681}
1682
1683//_____________________________________________________________________________
1684void AliCDBManager::DestroyActiveStorage(AliCDBStorage* /*storage*/) {
b05400be 1685// destroys active storage
1686
5078a13f 1687 /*
1688 TIter iter(fActiveStorages.GetTable());
1689 TPair* aPair;
1690 while ((aPair = (TPair*) iter.Next())) {
1691 if(storage == (AliCDBStorage*) aPair->Value())
1692 delete fActiveStorages.Remove(aPair->Key());
1693 storage->Delete(); storage=0x0;
1694 }
1695 */
9e1ceb13 1696
1697}
1698
62032124 1699//_____________________________________________________________________________
1700void AliCDBManager::QueryCDB() {
1701// query default and specific storages for files valid for fRun. Every storage loads the Ids into its list.
1702
5078a13f 1703 if (fRun < 0){
1704 AliError("Run number not yet set! Use AliCDBManager::SetRun.");
1705 return;
1706 }
1707 if (!fDefaultStorage){
1708 AliError("Default storage is not set! Use AliCDBManager::SetDefaultStorage");
1709 return;
1710 }
1711 if(fDefaultStorage->GetType() == "alien" || fDefaultStorage->GetType() == "local"){
1712 fDefaultStorage->QueryCDB(fRun);
1713 //} else {
1714 // AliDebug(2,"Skipping query for valid files, it used only in grid...");
1715 }
1716
1717 TIter iter(&fSpecificStorages);
1718 TObjString *aCalibType=0;
1719 AliCDBParam* aPar=0;
1720 while((aCalibType = dynamic_cast<TObjString*> (iter.Next()))){
1721 aPar = (AliCDBParam*) fSpecificStorages.GetValue(aCalibType);
1722 if(aPar) {
1723 AliDebug(2,Form("Querying specific storage %s",aCalibType->GetName()));
1724 AliCDBStorage *aStorage = GetStorage(aPar);
7ab8651a 1725 if(aStorage->GetType() == "alien" || aStorage->GetType() == "local"){
003e9909 1726 aStorage->QueryCDB(fRun, aCalibType->GetName());
5078a13f 1727 } else {
1728 AliDebug(2,
1729 "Skipping query for valid files, it is used only in grid...");
1730 }
1731 }
1732 }
62032124 1733}
1734
b8ec52f6 1735//______________________________________________________________________________________________
5078a13f 1736const char* AliCDBManager::GetDataTypeName(DataType type) {
1737// returns the name (string) of the data type
b8ec52f6 1738
5078a13f 1739 switch (type){
1740 case kCondition: return "Conditions";
1741 case kReference: return "Reference";
1742 case kPrivate: return "Private";
1743 }
1744 return 0;
b8ec52f6 1745
1746}
62032124 1747
a37a1dfc 1748//______________________________________________________________________________________________
5078a13f 1749Bool_t AliCDBManager::DiffObjects(const char *cdbFile1, const char *cdbFile2) const {
1750// Compare byte-by-byte the objects contained in the CDB entry in two different files,
1751// whose name is passed as input
1752// Return value:
1753// kTRUE - in case the content of the OCDB object (persistent part) is exactly the same
1754// kFALSE - otherwise
1755
1756 TString f1Str(cdbFile1);
1757 TString f2Str(cdbFile2);
1758 if (!gGrid && ( f1Str.BeginsWith("alien://") || f2Str.BeginsWith("alien://") ))
1759 TGrid::Connect("alien://");
1760
1761 TFile * f1 = TFile::Open(cdbFile1);
1762 if (!f1){
1763 Printf("Cannot open file \"%s\"",cdbFile1);
1764 return kFALSE;
1765 }
1766 TFile * f2 = TFile::Open(cdbFile2);
1767 if (!f2){
1768 Printf("Cannot open file \"%s\"",cdbFile2);
1769 return kFALSE;
1770 }
a37a1dfc 1771
5078a13f 1772 AliCDBEntry * entry1 = (AliCDBEntry*)f1->Get("AliCDBEntry");
1773 if (!entry1){
1774 Printf("Cannot get CDB entry from file \"%s\"",cdbFile1);
1775 return kFALSE;
1776 }
1777 AliCDBEntry * entry2 = (AliCDBEntry*)f2->Get("AliCDBEntry");
1778 if (!entry2){
1779 Printf("Cannot get CDB entry from file \"%s\"",cdbFile2);
1780 return kFALSE;
1781 }
a37a1dfc 1782
5078a13f 1783 // stream the two objects in the buffer of two TMessages
1784 TObject* object1 = entry1->GetObject();
1785 TObject* object2 = entry2->GetObject();
1786 TMessage * file1 = new TMessage(TBuffer::kWrite);
1787 file1->WriteObject(object1);
1788 Int_t size1 = file1->Length();
1789 TMessage * file2 = new TMessage(TBuffer::kWrite);
1790 file2->WriteObject(object2);
1791 Int_t size2 = file2->Length();
1792 if (size1!=size2){
1793 Printf("Problem 2: OCDB entry of different size (%d,%d)",size1,size2);
1794 return kFALSE;
1795 }
1796
1797 // if the two buffers have the same size, check that they are the same byte-by-byte
1798 Int_t countDiff=0;
1799 char* buf1 = file1->Buffer();
1800 char* buf2 = file2->Buffer();
1801 //for (Int_t i=0; i<size1; i++) if (file1->Buffer()[i]!=file2->Buffer()[i]) countDiff++;
1802 for(Int_t i=0; i<size1; i++)
1803 if (buf1[i]!=buf2[i]) countDiff++;
1804
1805 if (countDiff>0){
1806 Printf("The CDB objects differ by %d bytes.", countDiff);
1807 return kFALSE;
1808 }
a37a1dfc 1809
5078a13f 1810 Printf("The CDB objects are the same in the two files.");
1811 return kTRUE;
a37a1dfc 1812}
1813
4667c116 1814//______________________________________________________________________________________________
5078a13f 1815void AliCDBManager::InitShortLived() {
1816// Init the list of short-lived objects
1817// currently disabled
1818
1819 fShortLived=0x0;
1820
1821 // fShortLived = new TList();
1822 // fShortLived->SetOwner(1);
1823 //
1824 // fShortLived->Add(new TObjString("EMCAL/Calib/Data"));
1825 //
1826 // fShortLived->Add(new TObjString("HMPID/Calib/Nmean"));
1827 // fShortLived->Add(new TObjString("HMPID/Calib/Qthre"));
1828 //
1829 // fShortLived->Add(new TObjString("ITS/Calib/CalibSPD"));
1830 //
1831 // fShortLived->Add(new TObjString("MUON/Calib/Gains"));
1832 // fShortLived->Add(new TObjString("MUON/Calib/HV"));
1833 // fShortLived->Add(new TObjString("MUON/Calib/Pedestals"));
1834 //
1835 // fShortLived->Add(new TObjString("PHOS/Calib/CpvGainPedestals"));
1836 // fShortLived->Add(new TObjString("PHOS/Calib/EmcGainPedestals"));
1837 //
1838 // fShortLived->Add(new TObjString("PMD/Calib/Data"));
1839 //
1840 // fShortLived->Add(new TObjString("TRD/Calib/ChamberGainFactor"));
1841 // fShortLived->Add(new TObjString("TRD/Calib/LocalGainFactor"));
1842 // fShortLived->Add(new TObjString("TRD/Calib/ChamberT0"));
1843 // fShortLived->Add(new TObjString("TRD/Calib/LocalT0"));
1844 // fShortLived->Add(new TObjString("TRD/Calib/ChamberVdrift"));
1845 // fShortLived->Add(new TObjString("TRD/Calib/LocalVdrift"));
1846 //
1847 // fShortLived->Add(new TObjString("ZDC/Calib/Data"));
4667c116 1848
1849}
1850
1851//______________________________________________________________________________________________
5078a13f 1852Bool_t AliCDBManager::IsShortLived(const char* path) {
1853// returns the name (string) of the data type
4667c116 1854
5078a13f 1855 if(!fShortLived) return kFALSE;
4667c116 1856
5078a13f 1857 AliCDBPath aPath(path);
1858 if(!aPath.IsValid()){
1859 AliError(Form("Not a valid path: %s", path));
1860 return kFALSE;
1861 }
4667c116 1862
5078a13f 1863 return fShortLived->Contains(path);
4667c116 1864
1865}
1866
1f341624 1867//______________________________________________________________________________________________
096d8e90 1868ULong64_t AliCDBManager::SetLock(Bool_t lock, ULong64_t key){
5078a13f 1869// To lock/unlock user must provide the key. A new key is provided after
1870// each successful lock. User should always backup the returned key and
1871// use it on next access.
a6f59240 1872 if (fLock == lock) return 0; // nothing to be done
1873 if (lock) {
1874 // User wants to lock - check his identity
1875 if (fKey) {
1876 // Lock has a user - check his key
1877 if (fKey != key) {
1878 AliFatal("Wrong key provided to lock CDB. Please remove CDB lock access from your code !");
1879 return 0;
5078a13f 1880 }
1881 }
1882 // Provide new key
a6f59240 1883 fKey = gSystem->Now();
1884 fLock = kTRUE;
1885 return fKey;
1886 }
1887 // User wants to unlock - check the provided key
1888 if (key != fKey) {
1889 AliFatal("Lock is ON: wrong key provided");
1890 return 0;
5078a13f 1891 }
a6f59240 1892 fLock = kFALSE;
5078a13f 1893 return key;
1f341624 1894}
1895
9e1ceb13 1896///////////////////////////////////////////////////////////
1897// AliCDBManager Parameter class //
1898// interface to specific AliCDBParameter class //
1899// (AliCDBGridParam, AliCDBLocalParam, AliCDBDumpParam) //
1900///////////////////////////////////////////////////////////
1901
62032124 1902AliCDBParam::AliCDBParam():
fe12e09c 1903 fType(),
1904 fURI()
62032124 1905{
5078a13f 1906 // constructor
9e1ceb13 1907
1908}
1909
1910//_____________________________________________________________________________
1911AliCDBParam::~AliCDBParam() {
5078a13f 1912 // destructor
9e1ceb13 1913
1914}
1915
c612d6cd 1916void AliCDBManager::ExtractBaseFolder(TString& url)
1917{
1918 // TBD RS
1919 // remove everything but the url -
1920 // Exact copy of the AliReconstuction::Rectify.... (to be removed)
1921 //
1922 //
1923 TString sbs;
1924 if (!(sbs=url("\\?User=[^?]*")).IsNull()) url.ReplaceAll(sbs,"");
1925 if (!(sbs=url("\\?DBFolder=[^?]*")).IsNull()) url.ReplaceAll("?DB","");
1926 if (!(sbs=url("\\?SE=[^?]*")).IsNull()) url.ReplaceAll(sbs,"");
1927 if (!(sbs=url("\\?CacheFolder=[^?]*")).IsNull()) url.ReplaceAll(sbs,"");
1928 if (!(sbs=url("\\?OperateDisconnected=[^?]*")).IsNull()) url.ReplaceAll(sbs,"");
1929 if (!(sbs=url("\\?CacheSize=[^?]*")).IsNull()) url.ReplaceAll(sbs,"");
1930 if (!(sbs=url("\\?CleanupInterval=[^?]*")).IsNull()) url.ReplaceAll(sbs,"");
1931 Bool_t slash=kFALSE,space=kFALSE;
1932 while ( (slash=url.EndsWith("/")) || (space=url.EndsWith(" ")) ) {
1933 if (slash) url = url.Strip(TString::kTrailing,'/');
1934 if (space) url = url.Strip(TString::kTrailing,' ');
1935 }
1936 //url.ToLower();
1937 //
1938}