]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/CDB/AliCDBManager.cxx
dff2255dfae10dce2886bff450eeb118a5d35e22
[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 <fstream>
22
23 #include "AliCDBManager.h"
24 #include "AliCDBStorage.h"
25 #include "AliLog.h"
26 #include "AliCDBDump.h"
27 #include "AliCDBLocal.h"
28 #include "AliCDBGrid.h"
29 #include "AliCDBEntry.h"
30 #include "AliCDBHandler.h"
31
32 #include <TObjString.h>
33 #include <TSAXParser.h>
34 #include <TFile.h>
35 #include <TKey.h>
36 #include <TUUID.h>
37 #include <TGrid.h>
38 #include "TMessage.h"
39 #include "TObject.h"
40 #include "TRegexp.h"
41
42 ClassImp(AliCDBParam)
43
44 ClassImp(AliCDBManager)
45
46 //TODO OCDB and Reference folder should not be fully hardcoded but built from run number (or year/LHC period)
47 TString AliCDBManager::fgkCondUri("alien://folder=/alice/cern.ch/user/a/aliprod/testCDB/CDB?user=aliprod");
48 TString AliCDBManager::fgkRefUri("alien://folder=/alice/cern.ch/user/a/aliprod/testCDB/Reference?user=aliprod");
49 TString AliCDBManager::fgkMCIdealStorage("alien://folder=/alice/simulation/2008/v4-15-Release/Ideal");
50 TString AliCDBManager::fgkMCFullStorage("alien://folder=/alice/simulation/2008/v4-15-Release/Full");
51 TString AliCDBManager::fgkMCResidualStorage("alien://folder=/alice/simulation/2008/v4-15-Release/Residual");
52 TString AliCDBManager::fgkOCDBFolderXMLfile("alien:///alice/data/OCDBFoldervsRunRange.xml");
53 AliCDBManager* AliCDBManager::fgInstance = 0x0;
54
55 //_____________________________________________________________________________
56 AliCDBManager* AliCDBManager::Instance(TMap *entryCache, Int_t run) {
57 // returns AliCDBManager instance (singleton)
58
59   if (!fgInstance) {
60     fgInstance = new AliCDBManager();
61     if (!entryCache)
62       fgInstance->Init();
63     else
64       fgInstance->InitFromCache(entryCache,run);
65   }
66
67   return fgInstance;
68 }
69
70 //_____________________________________________________________________________
71 void AliCDBManager::Init() {
72 // factory registering
73
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   }
83
84   InitShortLived();
85 }
86
87 //_____________________________________________________________________________
88 void 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);
101   entryCache->SetOwnerKeyValue(kFALSE,kFALSE);
102   AliInfo(Form("%d cache entries have been loaded",fEntryCache.GetEntries()));
103 }
104
105 //_____________________________________________________________________________
106 void  AliCDBManager::DumpToSnapshotFile(const char* snapshotFileName, Bool_t singleKeys) const {
107 //
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).
112
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   }
119
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();
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     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());
139     }
140   }
141   f->Close();
142   delete f;
143 }
144
145 //_____________________________________________________________________________
146 void  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
167 //_____________________________________________________________________________
168 Bool_t AliCDBManager::InitFromSnapshot(const char* snapshotFileName, Bool_t overwrite){
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
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       }
188     }
189   }
190
191   TFile *f = TFile::Open(snapshotFileName);
192   if (!f || f->IsZombie()){
193     AliError(Form("Cannot open file %s",snapshotFileName));
194     return kFALSE;
195   }
196
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   }
224
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       }
245     }
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       }
259     }
260
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       }
277     }
278   }
279
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()));
286
287   f->Close();
288   delete f;
289
290   return kTRUE;
291 }
292
293 //_____________________________________________________________________________
294 void AliCDBManager::Destroy() {
295 // delete ALCDBManager instance and active storages
296
297   if (fgInstance) {
298     //fgInstance->Delete();
299     delete fgInstance;
300     fgInstance = 0x0;
301   }
302 }
303
304 //_____________________________________________________________________________
305 AliCDBManager::AliCDBManager():
306   TObject(),
307   fFactories(),
308   fActiveStorages(),
309   fSpecificStorages(),
310   fEntryCache(),
311   fIds(0),
312   fStorageMap(0),
313   fShortLived(0),
314   fDefaultStorage(NULL),
315   fDrainStorage(NULL),
316   fCondParam(0),
317   fRefParam(0),
318   fRun(-1),
319   fCache(kTRUE),
320   fLock(kFALSE),
321   fSnapshotMode(kFALSE),
322   fSnapshotFile(0),
323   fOCDBUploadMode(kFALSE),
324   fRaw(kFALSE),
325   fCvmfsOcdb(""),
326   fStartRunLHCPeriod(-1),
327   fEndRunLHCPeriod(-1),
328   fLHCPeriod(""),
329   fKey(0)
330 {
331   // default constuctor
332   fFactories.SetOwner(1);
333   fActiveStorages.SetOwner(1);
334   fSpecificStorages.SetOwner(1);
335   fEntryCache.SetName("CDBEntryCache");
336   fEntryCache.SetOwnerKeyValue(kTRUE,kTRUE);
337
338   fStorageMap = new TMap();
339   fStorageMap->SetOwner(1);
340   fIds = new TList();
341   fIds->SetOwner(1);
342 }
343
344 //_____________________________________________________________________________
345 AliCDBManager::~AliCDBManager() {
346 // destructor
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   }
363 }
364
365 //_____________________________________________________________________________
366 void AliCDBManager::PutActiveStorage(AliCDBParam* param, AliCDBStorage* storage){
367 // put a storage object into the list of active storages
368
369   fActiveStorages.Add(param, storage);
370   AliDebug(1, Form("Active storages: %d", fActiveStorages.GetEntries()));
371 }
372
373 //_____________________________________________________________________________
374 void AliCDBManager::RegisterFactory(AliCDBStorageFactory* factory) {
375 // add a storage factory to the list of registerd factories
376
377   if (!fFactories.Contains(factory)) {
378     fFactories.Add(factory);
379   }
380 }
381
382 //_____________________________________________________________________________
383 Bool_t AliCDBManager::HasStorage(const char* dbString) const {
384 // check if dbString is a URI valid for one of the registered factories
385
386   TIter iter(&fFactories);
387
388   AliCDBStorageFactory* factory=0;
389   while ((factory = (AliCDBStorageFactory*) iter.Next())) {
390
391     if (factory->Validate(dbString)) {
392       return kTRUE;
393     }
394   }
395
396   return kFALSE;
397 }
398
399 //_____________________________________________________________________________
400 AliCDBParam* AliCDBManager::CreateParameter(const char* dbString) const {
401 // create AliCDBParam object from URI string
402
403   TString uriString(dbString);
404
405   if ( !fCvmfsOcdb.IsNull() && uriString.BeginsWith("alien://")) {
406     AlienToCvmfsUri(uriString);
407   }
408
409   TIter iter(&fFactories);
410
411   AliCDBStorageFactory* factory=0;
412   while ((factory = (AliCDBStorageFactory*) iter.Next())) {
413     AliCDBParam* param = factory->CreateParameter(uriString);
414     if(param) return param;
415   }
416
417   return NULL;
418 }
419
420 //_____________________________________________________________________________
421 void AliCDBManager::AlienToCvmfsUri(TString& uriString) const {
422 // convert alien storage uri to local:///cvmfs storage uri (called when OCDB_PATH is set)
423
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() ){
444         entryValue.Replace(0, 6, "/cvmfs/alice-ocdb.cern.ch/calibration");
445         //entryValue.Replace(entryValue.Length()-4, entryValue.Length(), "");
446       } else if ( !mcFolder.IsNull() ){
447         entryValue.Replace(0,36,"/cvmfs/alice-ocdb.cern.ch/calibration/MC");
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;
460 }
461
462 //_____________________________________________________________________________
463 AliCDBStorage* AliCDBManager::GetStorage(const char* dbString) {
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)
466
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   }
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);
486
487   delete param;
488   return aStorage;
489 }
490
491 //_____________________________________________________________________________
492 AliCDBStorage* AliCDBManager::GetStorage(const AliCDBParam* param) {
493 // get storage object from AliCDBParam object
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);
523       }
524       return aStorage;
525     }
526   }
527
528   AliError(Form("Failed to activate requested storage! Check URI: %s", param->GetURI().Data()));
529
530   return NULL;
531 }
532
533 //_____________________________________________________________________________
534 AliCDBStorage* AliCDBManager::GetActiveStorage(const AliCDBParam* param) {
535 // get a storage object from the list of active storages
536
537   return dynamic_cast<AliCDBStorage*> (fActiveStorages.GetValue(param));
538 }
539
540 //_____________________________________________________________________________
541 TList* AliCDBManager::GetActiveStorages() {
542 // return list of active storages
543 // user has responsibility to delete returned object
544
545   TList* result = new TList();
546
547   TIter iter(fActiveStorages.GetTable());
548   TPair* aPair=0;
549   while ((aPair = (TPair*) iter.Next())) {
550     result->Add(aPair->Value());
551   }
552
553   return result;
554 }
555
556 //_____________________________________________________________________________
557 void AliCDBManager::SetDrain(const char* dbString) {
558 // set drain storage from URI string
559
560   fDrainStorage = GetStorage(dbString); 
561 }
562
563 //_____________________________________________________________________________
564 void AliCDBManager::SetDrain(const AliCDBParam* param) {
565 // set drain storage from AliCDBParam
566
567   fDrainStorage = GetStorage(param);
568 }
569
570 //_____________________________________________________________________________
571 void AliCDBManager::SetDrain(AliCDBStorage* storage) {
572 // set drain storage from another active storage
573
574   fDrainStorage = storage;
575 }
576
577 //_____________________________________________________________________________
578 Bool_t AliCDBManager::Drain(AliCDBEntry *entry) {
579 // drain retrieved object to drain storage
580
581   AliDebug(2, "Draining into drain storage...");
582   return fDrainStorage->Put(entry);
583 }
584
585 //____________________________________________________________________________
586 Bool_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
599 //____________________________________________________________________________
600 void AliCDBManager::SetDefaultStorage(const char* storageUri) {
601 // sets default storage from URI string
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()));
634 }
635
636 //_____________________________________________________________________________
637 void AliCDBManager::SetDefaultStorage(const AliCDBParam* param) {
638 // set default storage from AliCDBParam object
639
640   AliCDBStorage* bckStorage = fDefaultStorage;
641
642   fDefaultStorage = GetStorage(param);
643
644   if(!fDefaultStorage) return;
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(AliCDBStorage* storage) {
659 // set default storage from another active storage
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()));
687 }
688
689 //_____________________________________________________________________________
690 void AliCDBManager::SetDefaultStorage(const char* mcString, const char* simType) {
691 // sets default storage for MC data
692 // mcString MUST be "MC",
693 // simType can be "Ideal","Residual","Full"
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 //_____________________________________________________________________________
723 void AliCDBManager::ValidateCvmfsCase() const {
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
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()));
747 }
748
749 //_____________________________________________________________________________
750 void AliCDBManager::SetDefaultStorageFromRun(Int_t run) {
751 // set default storage from the run number - to be used only with raw data      
752
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
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   }
768
769   fLHCPeriod = lhcPeriod;
770   fStartRunLHCPeriod = startRun;
771   fEndRunLHCPeriod = endRun;
772
773   SetDefaultStorage(fLHCPeriod.Data());
774   if(!fDefaultStorage) AliFatal(Form("%s storage not there! Please check!",fLHCPeriod.Data()));
775
776 }
777
778 //_____________________________________________________________________________
779 void 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://","");
785     if(!gGrid) {
786       AliError("Connection to alien failed!");
787       return;
788     }
789   }
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 }
811
812 //_____________________________________________________________________________
813 void 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();
857 }
858
859 //_____________________________________________________________________________
860 void AliCDBManager::UnsetDefaultStorage() {
861 // Unset default storage
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;
879 }
880
881 //_____________________________________________________________________________
882 void AliCDBManager::SetSpecificStorage(const char* calibType, const char* dbString, Int_t version, Int_t subVersion) {
883 // sets storage specific for detector or calibration type (works with AliCDBManager::Get(...))
884
885   AliCDBParam *aPar = CreateParameter(dbString);
886   if(!aPar) return;
887   SetSpecificStorage(calibType, aPar, version, subVersion);
888   delete aPar;
889 }
890
891 //_____________________________________________________________________________
892 void AliCDBManager::SetSpecificStorage(const char* calibType, const AliCDBParam* param, Int_t version, Int_t subVersion) {
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://");
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)
899 // Specific version/subversion is set in the uniqueid of the TObjString used as key
900
901   if(!fDefaultStorage && !fRaw) {
902     AliError("Please activate a default storage first!");
903     return;
904   }
905
906   AliCDBPath aPath(calibType);
907   if(!aPath.IsValid()){
908     AliError(Form("Not a valid path: %s", calibType));
909     return;
910   }
911
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;
922
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);
928
929   if(fStorageMap->Contains(objCalibType)){
930     delete fStorageMap->Remove(objCalibType);
931   }
932   fStorageMap->Add(objCalibType->Clone(), new TObjString(param->GetURI()));
933
934 }
935
936 //_____________________________________________________________________________
937 AliCDBStorage* AliCDBManager::GetSpecificStorage(const char* calibType) {
938 // get storage specific for detector or calibration type
939
940   AliCDBPath calibPath(calibType);
941   if(!calibPath.IsValid()) return NULL;
942
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   }
950
951 }
952
953 //_____________________________________________________________________________
954 AliCDBParam* AliCDBManager::SelectSpecificStorage(const TString& path) {
955 // select storage valid for path from the list of specific storages
956
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;
973 }
974
975 //_____________________________________________________________________________
976 AliCDBEntry* AliCDBManager::Get(const AliCDBPath& path, Int_t runNumber,
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   }
988
989   return Get(AliCDBId(path, runNumber, runNumber, version, subVersion));
990 }
991
992 //_____________________________________________________________________________
993 AliCDBEntry* AliCDBManager::Get(const AliCDBPath& path,
994     const AliCDBRunRange& runRange, Int_t version,
995     Int_t subVersion) {
996   // get an AliCDBEntry object from the database!
997
998   return Get(AliCDBId(path, runRange, version, subVersion));
999 }
1000
1001 //_____________________________________________________________________________
1002 AliCDBEntry* AliCDBManager::Get(const AliCDBId& queryId, Bool_t forceCaching) {
1003 // get an AliCDBEntry object from the database
1004
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()));
1009     return NULL;
1010   }
1011
1012   // queryId is not specified if path contains wildcard or run range= [-1,-1]
1013   if (!queryId.IsSpecified()) {
1014     AliError(Form("Unspecified query: %s",
1015           queryId.ToString().Data()));
1016     return NULL;
1017   }
1018
1019   if(fLock && !(fRun >= queryId.GetFirstRun() && fRun <= queryId.GetLastRun()))
1020     AliFatal("Lock is ON: cannot use different run number than the internal one!");
1021
1022   if(fCache && !(fRun >= queryId.GetFirstRun() && fRun <= queryId.GetLastRun()))
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
1028   if(fCache && queryId.GetFirstRun() == fRun)
1029     entry = (AliCDBEntry*) fEntryCache.GetValue(queryId.GetPath());
1030   if(entry) {
1031     AliDebug(2, Form("Object %s retrieved from cache !!",queryId.GetPath().Data()));
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
1037   AliCDBParam *aPar=SelectSpecificStorage(queryId.GetPath());
1038   if(!aPar){
1039     if(fSnapshotMode && queryId.GetFirstRun() == fRun)
1040     {
1041       entry = GetEntryFromSnapshot(queryId.GetPath());
1042       if(entry) {
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);
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
1062   Int_t version = 0, subVersion = 0;
1063   AliCDBStorage *aStorage=0;
1064   if(aPar) {
1065     aStorage=GetStorage(aPar);
1066     TString str = aPar->GetURI();
1067     UInt_t uId = aPar->GetUniqueID();
1068     version = Int_t(uId&0xffff) - 1;
1069     subVersion = Int_t(uId>>16) - 1;
1070     AliDebug(2,Form("Looking into storage: %s",str.Data()));
1071   } else {
1072     aStorage=GetDefaultStorage();
1073     AliDebug(2,"Looking into default storage");
1074   }
1075
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);
1086
1087   if(entry && fCache && (queryId.GetFirstRun()==fRun || forceCaching)){
1088     CacheEntry(queryId.GetPath(), entry);
1089   }
1090
1091   if(entry && !fIds->Contains(&entry->GetId())){
1092     fIds->Add(entry->GetId().Clone());
1093   }
1094
1095   return entry;
1096 }
1097
1098 //_____________________________________________________________________________
1099 AliCDBEntry* AliCDBManager::GetEntryFromSnapshot(const char* path) {
1100 // get the entry from the open snapshot file
1101
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   }
1113
1114   return entry;
1115 }
1116
1117 //_____________________________________________________________________________
1118 Bool_t AliCDBManager::SetSnapshotMode(const char* snapshotFileName) {
1119 // set the manager in snapshot mode
1120
1121   if(!fCache){
1122     AliError("Cannot set the CDB manage in snapshot mode if the cache is not active!");
1123     return kFALSE;
1124   }
1125
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       }
1135     }
1136   }
1137
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;
1147
1148 }
1149
1150 //_____________________________________________________________________________
1151 const char* AliCDBManager::GetURI(const char* path) {
1152 // return the URI of the storage where to look for path
1153
1154   if(!IsDefaultStorageSet()) return 0;
1155
1156   AliCDBParam *aPar=SelectSpecificStorage(path);
1157
1158   if(aPar) {
1159     return aPar->GetURI().Data();
1160
1161   } else {
1162     return GetDefaultStorage()->GetURI().Data();
1163   }
1164
1165   return 0;
1166 }
1167
1168 //_____________________________________________________________________________
1169 Int_t AliCDBManager::GetStartRunLHCPeriod(){
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;
1176 }
1177
1178 //_____________________________________________________________________________
1179 Int_t AliCDBManager::GetEndRunLHCPeriod(){
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;
1186 }
1187
1188 //_____________________________________________________________________________
1189 TString AliCDBManager::GetLHCPeriod(){
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;
1195 }
1196
1197 //_____________________________________________________________________________
1198 AliCDBId* AliCDBManager::GetId(const AliCDBPath& path, Int_t runNumber,
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   }
1211
1212   return GetId(AliCDBId(path, runNumber, runNumber, version, subVersion));
1213 }
1214
1215 //_____________________________________________________________________________
1216 AliCDBId* AliCDBManager::GetId(const AliCDBPath& path,
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!
1221
1222   return GetId(AliCDBId(path, runRange, version, subVersion));
1223 }
1224
1225 //_____________________________________________________________________________
1226 AliCDBId* 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
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);
1278
1279 }
1280
1281 //_____________________________________________________________________________
1282 TList* AliCDBManager::GetAll(const AliCDBPath& path, Int_t runNumber,
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   }
1294
1295   return GetAll(AliCDBId(path, runNumber, runNumber, version,   
1296         subVersion));
1297 }
1298
1299 //_____________________________________________________________________________
1300 TList* AliCDBManager::GetAll(const AliCDBPath& path,
1301     const AliCDBRunRange& runRange, Int_t version, Int_t subVersion) {
1302   // get multiple AliCDBEntry objects from the database
1303
1304   return GetAll(AliCDBId(path, runRange, version, subVersion));
1305 }
1306
1307 //_____________________________________________________________________________
1308 TList* AliCDBManager::GetAll(const AliCDBId& query) {
1309 // get multiple AliCDBEntry objects from the database
1310 // Warning: this method works correctly only for queries of the type "Detector/*"
1311 //              and not for more specific queries e.g. "Detector/Calib/*" !
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
1314
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;
1408 }
1409
1410 //_____________________________________________________________________________
1411 Bool_t AliCDBManager::Put(TObject* object, const AliCDBId& id, AliCDBMetaData* metaData, const char* mirrors, DataType type){
1412 // store an AliCDBEntry object into the database
1413
1414   if (object==0x0) {
1415     AliError("Null Entry! No storage will be done!");
1416     return kFALSE;
1417   }
1418
1419   AliCDBEntry anEntry(object, id, metaData);
1420   return Put(&anEntry, mirrors, type);
1421
1422 }
1423
1424
1425 //_____________________________________________________________________________
1426 Bool_t AliCDBManager::Put(AliCDBEntry* entry, const char* mirrors, DataType type){
1427 // store an AliCDBEntry object into the database
1428
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;
1489
1490
1491 }
1492
1493 //_____________________________________________________________________________
1494 void AliCDBManager::SetMirrorSEs(const char* mirrors) {
1495 // set mirror Storage Elements for the default storage, if it is of type "alien"
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);
1501 }
1502
1503 //_____________________________________________________________________________
1504 const char* AliCDBManager::GetMirrorSEs() const {
1505 // get mirror Storage Elements for the default storage, if it is of type "alien"
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();
1511 }
1512
1513 //_____________________________________________________________________________
1514 void AliCDBManager::CacheEntry(const char* path, AliCDBEntry* entry) {
1515 // cache AliCDBEntry. Cache is valid until run number is changed.
1516
1517   AliCDBEntry *chkEntry = dynamic_cast<AliCDBEntry*> (fEntryCache.GetValue(path));
1518
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   }
1525
1526   fEntryCache.Add(new TObjString(path), entry);
1527   AliDebug(2,Form("Cache entries: %d", fEntryCache.GetEntries()));
1528
1529 }
1530
1531 //_____________________________________________________________________________
1532 void AliCDBManager::Print(Option_t* /*option*/) const {
1533 // Print list of active storages and their URIs
1534
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());
1558 }
1559
1560 //_____________________________________________________________________________
1561 void AliCDBManager::SetRun(Int_t run) {
1562 // Sets current run number.
1563 // When the run number changes the caching is cleared.
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{
1579       SetDefaultStorageFromRun(fRun);
1580       if(fEntryCache.GetEntries()!=0) ClearCache();
1581       return;
1582     }
1583   }
1584   ClearCache();
1585   QueryCDB();
1586 }
1587
1588 //_____________________________________________________________________________
1589 void AliCDBManager::ClearCache(){
1590 // clear AliCDBEntry cache
1591
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()));
1609 }
1610
1611 //_____________________________________________________________________________
1612 void AliCDBManager::UnloadFromCache(const char* path){
1613 // unload cached object
1614 // that is remove the entry from the cache and the id from the list of ids
1615 //
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()));
1673 }
1674
1675 //_____________________________________________________________________________
1676 void AliCDBManager::DestroyActiveStorages() {
1677 // delete list of active storages
1678
1679   fActiveStorages.DeleteAll();
1680   fSpecificStorages.DeleteAll();
1681 }
1682
1683 //_____________________________________________________________________________
1684 void AliCDBManager::DestroyActiveStorage(AliCDBStorage* /*storage*/) {
1685 // destroys active storage
1686
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      */
1696
1697 }
1698
1699 //_____________________________________________________________________________
1700 void AliCDBManager::QueryCDB() {
1701 // query default and specific storages for files valid for fRun. Every storage loads the Ids into its list.
1702
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);
1725       if(aStorage->GetType() == "alien" || aStorage->GetType() == "local"){
1726         aStorage->QueryCDB(fRun, aCalibType->GetName());
1727       } else {
1728         AliDebug(2,
1729             "Skipping query for valid files, it is used only in grid...");
1730       }
1731     }
1732   }
1733 }
1734
1735 //______________________________________________________________________________________________
1736 const char* AliCDBManager::GetDataTypeName(DataType type) {
1737 // returns the name (string) of the data type
1738
1739   switch (type){
1740     case kCondition: return "Conditions";
1741     case kReference: return "Reference";
1742     case kPrivate: return "Private";
1743   }
1744   return 0;
1745
1746 }
1747
1748 //______________________________________________________________________________________________
1749 Bool_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   }
1771
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   }
1782
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   }
1809
1810   Printf("The CDB objects are the same in the two files.");
1811   return kTRUE;
1812 }
1813
1814 //______________________________________________________________________________________________
1815 void 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"));
1848
1849 }
1850
1851 //______________________________________________________________________________________________
1852 Bool_t AliCDBManager::IsShortLived(const char* path) {
1853 // returns the name (string) of the data type
1854
1855   if(!fShortLived) return kFALSE;
1856
1857   AliCDBPath aPath(path);
1858   if(!aPath.IsValid()){
1859     AliError(Form("Not a valid path: %s", path));
1860     return kFALSE;
1861   }
1862
1863   return fShortLived->Contains(path);
1864
1865 }
1866
1867 //______________________________________________________________________________________________
1868 ULong64_t AliCDBManager::SetLock(Bool_t lock, ULong64_t key){
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.
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;
1880       }
1881     }
1882     // Provide new key
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;
1891   }
1892   fLock = kFALSE;
1893   return key;
1894 }
1895
1896 ///////////////////////////////////////////////////////////
1897 // AliCDBManager Parameter class                         //
1898 // interface to specific AliCDBParameter class           //
1899 // (AliCDBGridParam, AliCDBLocalParam, AliCDBDumpParam)  //
1900 ///////////////////////////////////////////////////////////
1901
1902 AliCDBParam::AliCDBParam():
1903   fType(),
1904   fURI()
1905 {
1906   // constructor
1907
1908 }
1909
1910 //_____________________________________________________________________________
1911 AliCDBParam::~AliCDBParam() {
1912   // destructor
1913
1914 }
1915
1916 void 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 }