]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliCDBLocal.cxx
Added functionality to get TTree's from the OCDB. The tree is loaded in memory
[u/mrichter/AliRoot.git] / STEER / AliCDBLocal.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 /////////////////////////////////////////////////////////////////////////////////////////////////
17 //                                                                                             //
18 // AliCDBLocal                                                                                 //
19 // access class to a DataBase in a local storage                                               //
20 //                                                                                             //
21 /////////////////////////////////////////////////////////////////////////////////////////////////
22
23 #include <TSystem.h>
24 #include <TObjString.h>
25 #include <TRegexp.h>
26 #include <TFile.h>
27 #include <TKey.h>
28
29 #include "AliCDBLocal.h"
30 #include "AliCDBEntry.h"
31 #include "AliLog.h"
32
33 ClassImp(AliCDBLocal)
34
35 //_____________________________________________________________________________
36 AliCDBLocal::AliCDBLocal(const char* baseDir):
37 fBaseDirectory(baseDir) 
38 {
39 // constructor
40
41         // check baseDire: trying to cd to baseDir; if it does not exist, create it
42         void* dir = gSystem->OpenDirectory(baseDir);
43         if (dir == NULL) {
44                 if (gSystem->mkdir(baseDir, kTRUE)) {
45                         AliError(Form("Can't open directory <%s>!", baseDir));
46                 }
47
48         } else {
49                 AliDebug(2,Form("Folder <%s> found",fBaseDirectory.Data()));
50                 gSystem->FreeDirectory(dir);
51         }
52         fType="local";
53         fBaseFolder = fBaseDirectory;
54 }
55
56 //_____________________________________________________________________________
57 AliCDBLocal::~AliCDBLocal() {
58 // destructor
59
60 }
61
62
63 //_____________________________________________________________________________
64 Bool_t AliCDBLocal::FilenameToId(const char* filename, AliCDBRunRange& runRange,
65         Int_t& version, Int_t& subVersion) {
66 // build AliCDBId from filename numbers
67
68
69         Ssiz_t mSize;
70
71         // valid filename: Run#firstRun_#lastRun_v#version_s#subVersion.root
72         TRegexp keyPattern("^Run[0-9]+_[0-9]+_v[0-9]+_s[0-9]+.root$");
73         keyPattern.Index(filename, &mSize);
74         if (!mSize) {
75                 AliDebug(2, Form("Bad filename <%s>.", filename));
76                 return kFALSE;
77         }
78
79         TString idString(filename);
80         idString.Resize(idString.Length() - sizeof(".root") + 1);
81
82         TObjArray* strArray = (TObjArray*) idString.Tokenize("_");
83
84         TString firstRunString(((TObjString*) strArray->At(0))->GetString());
85         runRange.SetFirstRun(atoi(firstRunString.Data() + 3));
86         runRange.SetLastRun(atoi(((TObjString*) strArray->At(1))->GetString()));
87         
88         TString verString(((TObjString*) strArray->At(2))->GetString());
89         version = atoi(verString.Data() + 1);
90
91         TString subVerString(((TObjString*) strArray->At(3))->GetString());
92         subVersion = atoi(subVerString.Data() + 1);
93
94         delete strArray;
95
96         return kTRUE;
97 }
98
99
100 //_____________________________________________________________________________
101 Bool_t AliCDBLocal::IdToFilename(const AliCDBId& id, TString& filename) const {
102 // build file name from AliCDBId data (run range, version, subVersion)
103
104         if (!id.GetAliCDBRunRange().IsValid()) {
105                 AliDebug(2,Form("Invalid run range <%d, %d>.", 
106                         id.GetFirstRun(), id.GetLastRun()));
107                 return kFALSE;
108         }
109
110         if (id.GetVersion() < 0) {
111                 AliDebug(2,Form("Invalid version <%d>.", id.GetVersion()));
112                 return kFALSE;
113         }
114
115         if (id.GetSubVersion() < 0) {
116                 AliDebug(2,Form("Invalid subversion <%s>.", id.GetSubVersion()));
117                 return kFALSE;
118         }
119  
120         filename = Form("Run%d_%d_v%d_s%d.root", id.GetFirstRun(), id.GetLastRun(),
121                                                  id.GetVersion(), id.GetSubVersion());
122
123         filename.Prepend(fBaseDirectory +'/' + id.GetPath() + '/');
124
125         return kTRUE;
126 }
127
128 //_____________________________________________________________________________
129 Bool_t AliCDBLocal::PrepareId(AliCDBId& id) {
130 // prepare id (version, subVersion) of the object that will be stored (called by PutEntry)
131
132         TString dirName = Form("%s/%s", fBaseDirectory.Data(), id.GetPath().Data());
133
134         // go to the path; if directory does not exist, create it
135         void* dirPtr = gSystem->OpenDirectory(dirName);
136         if (!dirPtr) {
137                 gSystem->mkdir(dirName, kTRUE);
138                 dirPtr = gSystem->OpenDirectory(dirName);
139
140                 if (!dirPtr) {
141                         AliError(Form("Can't create directory <%s>!", 
142                                         dirName.Data()));
143                         return kFALSE;
144                 }
145         }
146
147         const char* filename;
148         AliCDBRunRange aRunRange; // the runRange got from filename 
149         AliCDBRunRange lastRunRange(-1,-1); // highest runRange found
150         Int_t aVersion, aSubVersion; // the version subVersion got from filename
151         Int_t lastVersion = 0, lastSubVersion = -1; // highest version and subVersion found
152
153         if (!id.HasVersion()) { // version not specified: look for highest version & subVersion
154                                 
155                 while ((filename = gSystem->GetDirEntry(dirPtr))) { // loop on the files
156
157                         TString aString(filename);
158                         if (aString == "." || aString == "..") continue;
159         
160                         if (!FilenameToId(filename, aRunRange, aVersion, 
161                                 aSubVersion)) {
162                                 AliDebug(2,Form(
163                                         "Bad filename <%s>! I'll skip it.", 
164                                         filename));
165                                 continue;
166                         }
167                         
168                         if (!aRunRange.Overlaps(id.GetAliCDBRunRange())) continue;
169                         if(aVersion < lastVersion) continue;
170                         if(aVersion > lastVersion) lastSubVersion = -1;
171                         if(aSubVersion < lastSubVersion) continue;
172                         lastVersion = aVersion;
173                         lastSubVersion = aSubVersion;
174                         lastRunRange = aRunRange;
175                 }
176
177                 id.SetVersion(lastVersion);
178                 id.SetSubVersion(lastSubVersion + 1);
179
180         } else { // version specified, look for highest subVersion only
181                 
182                 while ((filename = gSystem->GetDirEntry(dirPtr))) { // loop on the files
183                         
184                         TString aString(filename);
185                         if (aString == "." || aString == "..") {
186                                 continue;
187                         }
188
189                         if (!FilenameToId(filename, aRunRange, aVersion, 
190                                 aSubVersion)) {
191                                 AliDebug(2,Form(
192                                         "Bad filename <%s>!I'll skip it.",
193                                         filename));     
194                                 continue;
195                         }
196
197                         if (aRunRange.Overlaps(id.GetAliCDBRunRange()) 
198                                 && aVersion == id.GetVersion()
199                                 && aSubVersion > lastSubVersion) {
200                                 lastSubVersion = aSubVersion;
201                                 lastRunRange = aRunRange;
202                         }
203         
204                 }
205                 
206                 id.SetSubVersion(lastSubVersion + 1);
207         }
208
209         gSystem->FreeDirectory(dirPtr);
210
211         TString lastStorage = id.GetLastStorage();
212         if(lastStorage.Contains(TString("grid"), TString::kIgnoreCase) &&
213            id.GetSubVersion() > 0 ){
214                 AliError(Form("Grid to Local Storage error! local object with version v%d_s%d found:",id.GetVersion(), id.GetSubVersion()-1));
215                 AliError(Form("This object has been already transferred from Grid (check v%d_s0)!",id.GetVersion()));
216                 return kFALSE;
217         }
218
219         if(lastStorage.Contains(TString("new"), TString::kIgnoreCase) &&
220            id.GetSubVersion() > 0 ){
221                 AliDebug(2, Form("A NEW object is being stored with version v%d_s%d",
222                                         id.GetVersion(),id.GetSubVersion()));
223                 AliDebug(2, Form("and it will hide previously stored object with v%d_s%d!",
224                                         id.GetVersion(),id.GetSubVersion()-1));
225         }
226
227         if(!lastRunRange.IsAnyRange() && !(lastRunRange.IsEqual(& id.GetAliCDBRunRange()))) 
228                 AliWarning(Form("Run range modified w.r.t. previous version (Run%d_%d_v%d_s%d)",
229                         lastRunRange.GetFirstRun(), lastRunRange.GetLastRun(), 
230                         id.GetVersion(), id.GetSubVersion()-1));
231
232         return kTRUE;
233 }
234
235 //_____________________________________________________________________________
236 Bool_t AliCDBLocal::GetId(const AliCDBId& query, AliCDBId& result) {
237 // look for filename matching query (called by GetEntry)
238
239         TString dirName = Form("%s/%s", fBaseDirectory.Data(), query.GetPath().Data());
240
241         void* dirPtr = gSystem->OpenDirectory(dirName);
242         if (!dirPtr) {
243                 AliDebug(2,Form("Directory <%s> not found", (query.GetPath()).Data()));
244                 AliDebug(2,Form("in DB folder %s", fBaseDirectory.Data()));
245                 return kFALSE;
246         }
247
248         const char* filename;
249
250         AliCDBRunRange aRunRange; // the runRange got from filename
251         Int_t aVersion, aSubVersion; // the version and subVersion got from filename
252
253         if (!query.HasVersion()) { // neither version and subversion specified -> look for highest version and subVersion
254
255                 while ((filename = gSystem->GetDirEntry(dirPtr))) { // loop on files
256
257                         TString aString(filename);
258                         if (aString == "." || aString == "..") continue;
259
260                         if (!FilenameToId(filename, aRunRange, aVersion, aSubVersion)) continue;
261                         // aRunRange, aVersion, aSubVersion filled from filename
262
263                         if (!aRunRange.Comprises(query.GetAliCDBRunRange())) continue;
264                         // aRunRange contains requested run!
265
266                         if (result.GetVersion() < aVersion) {
267                                 result.SetVersion(aVersion);
268                                 result.SetSubVersion(aSubVersion);
269
270                                 result.SetFirstRun(
271                                         aRunRange.GetFirstRun());
272                                 result.SetLastRun(
273                                         aRunRange.GetLastRun());
274
275                         } else if (result.GetVersion() == aVersion 
276                                 && result.GetSubVersion() 
277                                         < aSubVersion) {
278
279                                 result.SetSubVersion(aSubVersion);
280
281                                 result.SetFirstRun(
282                                         aRunRange.GetFirstRun());
283                                 result.SetLastRun(
284                                         aRunRange.GetLastRun());
285                         } else if (result.GetVersion() == aVersion
286                                 && result.GetSubVersion() == aSubVersion){
287                                 AliDebug(2,Form("More than one object valid for run %d, version %d_%d!", 
288                                         query.GetFirstRun(), aVersion, aSubVersion));
289                                 gSystem->FreeDirectory(dirPtr);
290                                 return kFALSE; 
291                                 }
292                 }
293                 
294         } else if (!query.HasSubVersion()) { // version specified but not subversion -> look for highest subVersion
295
296                 result.SetVersion(query.GetVersion());
297
298                 while ((filename = gSystem->GetDirEntry(dirPtr))) { // loop on files
299
300                         TString aString(filename);
301                         if (aString == "." || aString == "..") continue;
302
303                         if (!FilenameToId(filename, aRunRange, aVersion, aSubVersion)) continue;       
304                         // aRunRange, aVersion, aSubVersion filled from filename
305
306                         if (!aRunRange.Comprises(query.GetAliCDBRunRange())) continue; 
307                         // aRunRange contains requested run!
308                         
309                         if(query.GetVersion() != aVersion) continue;
310                         // aVersion is requested version!
311                         
312                         if(result.GetSubVersion() == aSubVersion){
313                                 AliDebug(2,Form("More than one object valid for run %d, version %d_%d!", 
314                                         query.GetFirstRun(), aVersion, aSubVersion));
315                                 gSystem->FreeDirectory(dirPtr);
316                                 return kFALSE; 
317                         }
318                         if( result.GetSubVersion() < aSubVersion) {
319
320                                 result.SetSubVersion(aSubVersion);
321
322                                 result.SetFirstRun(
323                                         aRunRange.GetFirstRun());
324                                 result.SetLastRun(
325                                         aRunRange.GetLastRun());
326                         } 
327                 }
328
329         } else { // both version and subversion specified
330
331                 while ((filename = gSystem->GetDirEntry(dirPtr))) { // loop on files
332
333                         TString aString(filename);
334                         if (aString == "." || aString == "..") continue;
335
336                         if (!FilenameToId(filename, aRunRange, aVersion, aSubVersion)) continue;
337                         // aRunRange, aVersion, aSubVersion filled from filename
338                         
339                         if (!aRunRange.Comprises(query.GetAliCDBRunRange())) continue;
340                         // aRunRange contains requested run!
341
342                         if(query.GetVersion() != aVersion || query.GetSubVersion() != aSubVersion) continue; 
343                         // aVersion and aSubVersion are requested version and subVersion!
344                         
345                         if(result.GetVersion() == aVersion && result.GetSubVersion() == aSubVersion){
346                                 AliDebug(2,Form("More than one object valid for run %d, version %d_%d!", 
347                                         query.GetFirstRun(), aVersion, aSubVersion));
348                                 gSystem->FreeDirectory(dirPtr);
349                                 return kFALSE; 
350                         }
351                         result.SetVersion(aVersion);
352                         result.SetSubVersion(aSubVersion);
353                         result.SetFirstRun(aRunRange.GetFirstRun());
354                         result.SetLastRun(aRunRange.GetLastRun());
355                         
356                 }
357         }
358
359         gSystem->FreeDirectory(dirPtr);
360
361         return kTRUE;
362 }
363
364 //_____________________________________________________________________________
365 AliCDBEntry* AliCDBLocal::GetEntry(const AliCDBId& queryId) {
366 // get AliCDBEntry from the database
367
368         AliCDBId dataId(queryId.GetAliCDBPath(), -1, -1, -1, -1);
369         Bool_t result;
370
371         // look for a filename matching query requests (path, runRange, version, subVersion)
372         if (!queryId.HasVersion()) {
373                 // if version is not specified, first check the selection criteria list
374                 AliCDBId selectedId(queryId);
375                 GetSelection(&selectedId);
376                 result = GetId(selectedId, dataId);
377         } else {
378                 result = GetId(queryId, dataId);
379         }
380
381         if (!result || !dataId.IsSpecified()) return NULL;
382
383         TString filename;
384         if (!IdToFilename(dataId, filename)) {
385
386                 AliDebug(2,Form("Bad data ID encountered! Subnormal error!"));
387                 return NULL;
388         }
389
390         TFile file(filename, "READ"); // open file
391         if (!file.IsOpen()) {
392                 AliDebug(2,Form("Can't open file <%s>!", filename.Data()));
393                 return NULL;
394         }
395
396         // get the only AliCDBEntry object from the file
397         // the object in the file is an AliCDBEntry entry named "AliCDBEntry"
398         
399         AliCDBEntry* anEntry = dynamic_cast<AliCDBEntry*> (file.Get("AliCDBEntry"));
400         if (!anEntry) {
401                 AliDebug(2,Form("Bad storage data: No AliCDBEntry in file!"));
402                 file.Close();
403                 return NULL;
404         }
405
406         AliCDBId entryId = anEntry->GetId();
407  
408         // The object's Id are not reset during storage
409         // If object's Id runRange or version do not match with filename,
410         // it means that someone renamed file by hand. In this case a warning msg is issued.
411
412         anEntry-> SetLastStorage("local");
413  
414         if(!entryId.IsEqual(&dataId)){
415                 AliWarning(Form("Mismatch between file name and object's Id!"));
416                 AliWarning(Form("File name: %s", dataId.ToString().Data()));
417                 AliWarning(Form("Object's Id: %s", entryId.ToString().Data()));
418         }
419
420         // Check whether entry contains a TTree. In case load the tree in memory!
421         LoadTreeFromFile(anEntry);
422
423         // close file, return retieved entry
424         file.Close();
425         return anEntry;
426 }
427
428 //_____________________________________________________________________________
429 void AliCDBLocal::GetEntriesForLevel0(const char* level0,
430         const AliCDBId& queryId, TList* result) {
431 // multiple request (AliCDBStorage::GetAll)
432
433         TString level0Dir = Form("%s/%s", fBaseDirectory.Data(), level0);
434
435         void* level0DirPtr = gSystem->OpenDirectory(level0Dir);
436         if (!level0DirPtr) {
437                 AliDebug(2,Form("Can't open level0 directory <%s>!",
438                         level0Dir.Data()));
439                 return;
440         }
441
442         const char* level1;
443         while ((level1 = gSystem->GetDirEntry(level0DirPtr))) {
444
445                 TString level1Str(level1);
446                 if (level1Str == "." || level1Str == "..") {
447                         continue;
448                 }
449
450                 if (queryId.GetAliCDBPath().Level1Comprises(level1)) {
451                         GetEntriesForLevel1(level0, level1, queryId, result);
452                 }
453         }
454
455         gSystem->FreeDirectory(level0DirPtr);
456 }
457
458 //_____________________________________________________________________________
459 void AliCDBLocal::GetEntriesForLevel1(const char* level0, const char* level1,
460         const AliCDBId& queryId, TList* result) {
461 // multiple request (AliCDBStorage::GetAll)
462
463         TString level1Dir = Form("%s/%s/%s", fBaseDirectory.Data(), level0,level1);
464
465         void* level1DirPtr = gSystem->OpenDirectory(level1Dir);
466         if (!level1DirPtr) {
467                 AliDebug(2,Form("Can't open level1 directory <%s>!",
468                         level1Dir.Data()));
469                 return;
470         }
471
472         const char* level2;
473         while ((level2 = gSystem->GetDirEntry(level1DirPtr))) {
474
475                 TString level2Str(level2);
476                 if (level2Str == "." || level2Str == "..") {
477                         continue;
478                 }
479
480                 if (queryId.GetAliCDBPath().Level2Comprises(level2)) {
481
482                         AliCDBPath entryPath(level0, level1, level2);
483                         AliCDBId entryId(entryPath, queryId.GetAliCDBRunRange(),
484                                 queryId.GetVersion(), queryId.GetSubVersion());
485
486                         AliCDBEntry* anEntry = GetEntry(entryId);
487                         if (anEntry) {
488                                 result->Add(anEntry);
489                         }
490                 }
491         }
492
493         gSystem->FreeDirectory(level1DirPtr);
494 }
495
496 //_____________________________________________________________________________
497 TList* AliCDBLocal::GetEntries(const AliCDBId& queryId) {
498 // multiple request (AliCDBStorage::GetAll)
499
500         void* storageDirPtr = gSystem->OpenDirectory(fBaseDirectory);
501         if (!storageDirPtr) {
502                 AliDebug(2,Form("Can't open storage directory <%s>",
503                         fBaseDirectory.Data()));
504                 return NULL;
505         }
506
507         TList* result = new TList();
508         result->SetOwner();
509
510         const char* level0;
511         while ((level0 = gSystem->GetDirEntry(storageDirPtr))) {
512
513                 TString level0Str(level0);
514                 if (level0Str == "." || level0Str == "..") {
515                         continue;
516                 }
517
518                 if (queryId.GetAliCDBPath().Level0Comprises(level0)) {
519                         GetEntriesForLevel0(level0, queryId, result);
520                 }
521         }
522
523         gSystem->FreeDirectory(storageDirPtr);
524
525         return result;  
526 }
527
528 //_____________________________________________________________________________
529 Bool_t AliCDBLocal::PutEntry(AliCDBEntry* entry) {
530 // put an AliCDBEntry object into the database
531
532         AliCDBId& id = entry->GetId();
533
534         // set version and subVersion for the entry to be stored
535         if (!PrepareId(id)) return kFALSE;
536
537         
538         // build filename from entry's id
539         TString filename="";
540         if (!IdToFilename(id, filename)) {
541
542                 AliDebug(2,Form("Bad ID encountered! Subnormal error!"));
543                 return kFALSE;
544         }
545
546         // open file
547         TFile file(filename, "CREATE");
548         if (!file.IsOpen()) {
549                 AliError(Form("Can't open file <%s>!", filename.Data()));
550                 return kFALSE;
551         }
552         
553         SetTreeToFile(entry, &file);
554
555         entry->SetVersion(id.GetVersion());
556         entry->SetSubVersion(id.GetSubVersion());
557
558         // write object (key name: "AliCDBEntry")
559         Bool_t result = file.WriteTObject(entry, "AliCDBEntry");
560         if (!result) AliDebug(2,Form("Can't write entry to file: %s", filename.Data()));
561
562         file.Close();
563         if(result) {
564                 if(!(id.GetPath().Contains("SHUTTLE/STATUS")))
565                         AliInfo(Form("CDB object stored into file %s",filename.Data()));
566         }
567
568         return result;
569 }
570
571 //_____________________________________________________________________________
572 TList* AliCDBLocal::GetIdListFromFile(const char* fileName){
573
574         TString fullFileName(fileName);
575         fullFileName.Prepend(fBaseDirectory+'/');
576         TFile *file = TFile::Open(fullFileName);
577         if (!file) {
578                 AliError(Form("Can't open selection file <%s>!", fullFileName.Data()));
579                 return NULL;
580         }
581         file->cd();
582
583         TList *list = new TList();
584         list->SetOwner();
585         int i=0;
586         TString keycycle;
587         
588         AliCDBId *id;
589         while(1){
590                 i++;
591                 keycycle = "AliCDBId;";
592                 keycycle+=i;
593                 
594                 id = (AliCDBId*) file->Get(keycycle);
595                 if(!id) break;
596                 list->AddFirst(id);
597         }
598         file->Close(); delete file; file=0;     
599         return list;
600 }
601
602 //_____________________________________________________________________________
603 Bool_t AliCDBLocal::Contains(const char* path) const{
604 // check for path in storage's fBaseDirectory
605
606         TString dirName = Form("%s/%s", fBaseDirectory.Data(), path);
607         Bool_t result=kFALSE;
608
609         void* dirPtr = gSystem->OpenDirectory(dirName); 
610         if (dirPtr) result=kTRUE;
611         gSystem->FreeDirectory(dirPtr);
612
613         return result;
614 }
615
616 //_____________________________________________________________________________
617 void AliCDBLocal::QueryValidFiles()
618 {
619 // Query the CDB for files valid for AliCDBStorage::fRun
620 // fills list fValidFileIds with AliCDBId objects created from file name
621
622         if(fVersion != -1) AliWarning ("Version parameter is not used by local storage query!");
623         if(fMetaDataFilter) {
624                 AliWarning ("CDB meta data parameters are not used by local storage query!");
625                 delete fMetaDataFilter; fMetaDataFilter=0;
626         }
627
628         void* storageDirPtr = gSystem->OpenDirectory(fBaseDirectory);
629
630         const char* level0;
631         while ((level0 = gSystem->GetDirEntry(storageDirPtr))) {
632
633                 TString level0Str(level0);
634                 if (level0Str == "." || level0Str == "..") {
635                         continue;
636                 }
637
638                 if (fPathFilter.Level0Comprises(level0)) {
639                         TString level0Dir = Form("%s/%s",fBaseDirectory.Data(),level0);
640                         void* level0DirPtr = gSystem->OpenDirectory(level0Dir);
641                         const char* level1;
642                         while ((level1 = gSystem->GetDirEntry(level0DirPtr))) {
643
644                                 TString level1Str(level1);
645                                 if (level1Str == "." || level1Str == "..") {
646                                         continue;
647                                 }
648
649                                 if (fPathFilter.Level1Comprises(level1)) {
650                                         TString level1Dir = Form("%s/%s/%s",
651                                                         fBaseDirectory.Data(),level0,level1);
652
653                                         void* level1DirPtr = gSystem->OpenDirectory(level1Dir);
654                                         const char* level2;
655                                         while ((level2 = gSystem->GetDirEntry(level1DirPtr))) {
656
657                                                 TString level2Str(level2);
658                                                 if (level2Str == "." || level2Str == "..") {
659                                                         continue;
660                                                 }
661
662                                                 if (fPathFilter.Level2Comprises(level2)) {
663                                                         TString dirName = Form("%s/%s/%s/%s",
664                                                                         fBaseDirectory.Data(),level0,level1,level2);
665
666                                                         void* dirPtr = gSystem->OpenDirectory(dirName);
667
668                                                         const char* filename;
669
670                                                         AliCDBRunRange aRunRange; // the runRange got from filename
671                                                         Int_t aVersion, aSubVersion; // the version and subVersion got from filename
672
673                                                         while ((filename = gSystem->GetDirEntry(dirPtr))) { // loop on files
674
675                                                                 TString aString(filename);
676                                                                 if (aString == "." || aString == "..") continue;
677
678                                                                 if (!FilenameToId(filename, aRunRange, aVersion, aSubVersion)) continue;
679                                                                 AliCDBRunRange runrg(fRun,fRun);
680                                                                 if (!aRunRange.Comprises(runrg)) continue;
681                                                                 // aRunRange contains requested run!
682                                                                 AliCDBPath validPath(level0,level1,level2);
683                                                                 AliCDBId validId(validPath,aRunRange,aVersion,aSubVersion);
684                                                                 fValidFileIds.AddLast(validId.Clone());
685                                                         }
686                                                         gSystem->FreeDirectory(dirPtr);
687                                                 }
688                                         }
689                                         gSystem->FreeDirectory(level1DirPtr);
690                                 }
691                         }
692                         gSystem->FreeDirectory(level0DirPtr);
693                 }
694         }
695         gSystem->FreeDirectory(storageDirPtr);
696
697 }
698
699 //_____________________________________________________________________________
700 Int_t AliCDBLocal::GetLatestVersion(const char* path, Int_t run){
701 // get last version found in the database valid for run and path
702
703         AliCDBPath aCDBPath(path);
704         if(!aCDBPath.IsValid() || aCDBPath.IsWildcard()) {
705                 AliError(Form("Invalid path in request: %s", path));
706                 return -1;
707         }
708
709         AliCDBId query(path, run, run, -1, -1);
710         AliCDBId dataId;
711
712         GetId(query,dataId);
713
714         return dataId.GetVersion();
715
716 }
717
718 //_____________________________________________________________________________
719 Int_t AliCDBLocal::GetLatestSubVersion(const char* path, Int_t run, Int_t version){
720 // get last version found in the database valid for run and path
721
722         AliCDBPath aCDBPath(path);
723         if(!aCDBPath.IsValid() || aCDBPath.IsWildcard()) {
724                 AliError(Form("Invalid path in request: %s", path));
725                 return -1;
726         }
727
728         AliCDBId query(path, run, run, version, -1);
729         AliCDBId dataId;
730
731         GetId(query,dataId);
732
733         return dataId.GetSubVersion();
734
735 }
736
737 /////////////////////////////////////////////////////////////////////////////////////////////////
738 //                                                                                             //
739 // AliCDBLocal factory                                                                         //
740 //                                                                                             //
741 /////////////////////////////////////////////////////////////////////////////////////////////////
742
743 ClassImp(AliCDBLocalFactory)
744
745 //_____________________________________________________________________________
746 Bool_t AliCDBLocalFactory::Validate(const char* dbString) {
747 // check if the string is valid local URI
748
749         TRegexp dbPattern("^local://.+$");
750
751         return TString(dbString).Contains(dbPattern);
752 }
753
754 //_____________________________________________________________________________
755 AliCDBParam* AliCDBLocalFactory::CreateParameter(const char* dbString) {
756 // create AliCDBLocalParam class from the URI string
757
758         if (!Validate(dbString)) {
759                 return NULL;
760         }
761
762         TString pathname(dbString + sizeof("local://") - 1);
763         
764         gSystem->ExpandPathName(pathname);
765
766         if (pathname[0] != '/') {
767                 pathname.Prepend(TString(gSystem->WorkingDirectory()) + '/');
768         }
769
770         return new AliCDBLocalParam(pathname);       
771 }
772
773 //_____________________________________________________________________________
774 AliCDBStorage* AliCDBLocalFactory::Create(const AliCDBParam* param) {
775 // create AliCDBLocal storage instance from parameters
776         
777         if (AliCDBLocalParam::Class() == param->IsA()) {
778                 
779                 const AliCDBLocalParam* localParam = 
780                         (const AliCDBLocalParam*) param;
781                 
782                 return new AliCDBLocal(localParam->GetPath());
783         }
784
785         return NULL;
786 }
787
788 /////////////////////////////////////////////////////////////////////////////////////////////////
789 //                                                                                             //
790 // AliCDBLocal Parameter class                                                                 //                                          //
791 //                                                                                             //
792 /////////////////////////////////////////////////////////////////////////////////////////////////
793
794 ClassImp(AliCDBLocalParam)
795
796 //_____________________________________________________________________________
797 AliCDBLocalParam::AliCDBLocalParam():
798  AliCDBParam(),
799  fDBPath()
800  {
801 // default constructor
802
803 }
804
805 //_____________________________________________________________________________
806 AliCDBLocalParam::AliCDBLocalParam(const char* dbPath):
807  AliCDBParam(),
808  fDBPath(dbPath)
809 {
810 // constructor
811
812         SetType("local");
813         SetURI(TString("local://") + dbPath);
814 }
815
816 //_____________________________________________________________________________
817 AliCDBLocalParam::~AliCDBLocalParam() {
818 // destructor
819
820 }
821
822 //_____________________________________________________________________________
823 AliCDBParam* AliCDBLocalParam::CloneParam() const {
824 // clone parameter
825
826         return new AliCDBLocalParam(fDBPath);
827 }
828
829 //_____________________________________________________________________________
830 ULong_t AliCDBLocalParam::Hash() const {
831 // return Hash function
832
833        return fDBPath.Hash();
834 }
835
836 //_____________________________________________________________________________
837 Bool_t AliCDBLocalParam::IsEqual(const TObject* obj) const {
838 // check if this object is equal to AliCDBParam obj
839
840         if (this == obj) {
841                 return kTRUE;
842         }
843
844         if (AliCDBLocalParam::Class() != obj->IsA()) {
845                 return kFALSE;
846         }
847
848         AliCDBLocalParam* other = (AliCDBLocalParam*) obj;
849
850         return fDBPath == other->fDBPath;
851 }
852