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