]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliCDBLocal.cxx
Reconstruction QA by Sylwester
[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         // close file, return retieved entry
421         file.Close();
422         return anEntry;
423 }
424
425 //_____________________________________________________________________________
426 void AliCDBLocal::GetEntriesForLevel0(const char* level0,
427         const AliCDBId& queryId, TList* result) {
428 // multiple request (AliCDBStorage::GetAll)
429
430         TString level0Dir = Form("%s/%s", fBaseDirectory.Data(), level0);
431
432         void* level0DirPtr = gSystem->OpenDirectory(level0Dir);
433         if (!level0DirPtr) {
434                 AliDebug(2,Form("Can't open level0 directory <%s>!",
435                         level0Dir.Data()));
436                 return;
437         }
438
439         const char* level1;
440         while ((level1 = gSystem->GetDirEntry(level0DirPtr))) {
441
442                 TString level1Str(level1);
443                 if (level1Str == "." || level1Str == "..") {
444                         continue;
445                 }
446
447                 if (queryId.GetAliCDBPath().Level1Comprises(level1)) {
448                         GetEntriesForLevel1(level0, level1, queryId, result);
449                 }
450         }
451
452         gSystem->FreeDirectory(level0DirPtr);
453 }
454
455 //_____________________________________________________________________________
456 void AliCDBLocal::GetEntriesForLevel1(const char* level0, const char* level1,
457         const AliCDBId& queryId, TList* result) {
458 // multiple request (AliCDBStorage::GetAll)
459
460         TString level1Dir = Form("%s/%s/%s", fBaseDirectory.Data(), level0,level1);
461
462         void* level1DirPtr = gSystem->OpenDirectory(level1Dir);
463         if (!level1DirPtr) {
464                 AliDebug(2,Form("Can't open level1 directory <%s>!",
465                         level1Dir.Data()));
466                 return;
467         }
468
469         const char* level2;
470         while ((level2 = gSystem->GetDirEntry(level1DirPtr))) {
471
472                 TString level2Str(level2);
473                 if (level2Str == "." || level2Str == "..") {
474                         continue;
475                 }
476
477                 if (queryId.GetAliCDBPath().Level2Comprises(level2)) {
478
479                         AliCDBPath entryPath(level0, level1, level2);
480                         AliCDBId entryId(entryPath, queryId.GetAliCDBRunRange(),
481                                 queryId.GetVersion(), queryId.GetSubVersion());
482
483                         AliCDBEntry* anEntry = GetEntry(entryId);
484                         if (anEntry) {
485                                 result->Add(anEntry);
486                         }
487                 }
488         }
489
490         gSystem->FreeDirectory(level1DirPtr);
491 }
492
493 //_____________________________________________________________________________
494 TList* AliCDBLocal::GetEntries(const AliCDBId& queryId) {
495 // multiple request (AliCDBStorage::GetAll)
496
497         void* storageDirPtr = gSystem->OpenDirectory(fBaseDirectory);
498         if (!storageDirPtr) {
499                 AliDebug(2,Form("Can't open storage directory <%s>",
500                         fBaseDirectory.Data()));
501                 return NULL;
502         }
503
504         TList* result = new TList();
505         result->SetOwner();
506
507         const char* level0;
508         while ((level0 = gSystem->GetDirEntry(storageDirPtr))) {
509
510                 TString level0Str(level0);
511                 if (level0Str == "." || level0Str == "..") {
512                         continue;
513                 }
514
515                 if (queryId.GetAliCDBPath().Level0Comprises(level0)) {
516                         GetEntriesForLevel0(level0, queryId, result);
517                 }
518         }
519
520         gSystem->FreeDirectory(storageDirPtr);
521
522         return result;  
523 }
524
525 //_____________________________________________________________________________
526 Bool_t AliCDBLocal::PutEntry(AliCDBEntry* entry) {
527 // put an AliCDBEntry object into the database
528
529         AliCDBId& id = entry->GetId();
530
531         // set version and subVersion for the entry to be stored
532         if (!PrepareId(id)) return kFALSE;
533
534         
535         // build filename from entry's id
536         TString filename="";
537         if (!IdToFilename(id, filename)) {
538
539                 AliDebug(2,Form("Bad ID encountered! Subnormal error!"));
540                 return kFALSE;
541         }
542
543         // open file
544         TFile file(filename, "CREATE");
545         if (!file.IsOpen()) {
546                 AliError(Form("Can't open file <%s>!", filename.Data()));
547                 return kFALSE;
548         }
549         
550         entry->SetVersion(id.GetVersion());
551         entry->SetSubVersion(id.GetSubVersion());
552
553         // write object (key name: "AliCDBEntry")
554         Bool_t result = file.WriteTObject(entry, "AliCDBEntry");
555         if (!result) AliDebug(2,Form("Can't write entry to file: %s", filename.Data()));
556
557         file.Close();
558         if(result) {
559                 if(!(id.GetPath().Contains("SHUTTLE/STATUS")))
560                         AliInfo(Form("CDB object stored into file %s",filename.Data()));
561         }
562
563         return result;
564 }
565
566 //_____________________________________________________________________________
567 TList* AliCDBLocal::GetIdListFromFile(const char* fileName){
568
569         TString fullFileName(fileName);
570         fullFileName.Prepend(fBaseDirectory+'/');
571         TFile *file = TFile::Open(fullFileName);
572         if (!file) {
573                 AliError(Form("Can't open selection file <%s>!", fullFileName.Data()));
574                 return NULL;
575         }
576         file->cd();
577
578         TList *list = new TList();
579         list->SetOwner();
580         int i=0;
581         TString keycycle;
582         
583         AliCDBId *id;
584         while(1){
585                 i++;
586                 keycycle = "AliCDBId;";
587                 keycycle+=i;
588                 
589                 id = (AliCDBId*) file->Get(keycycle);
590                 if(!id) break;
591                 list->AddFirst(id);
592         }
593         file->Close(); delete file; file=0;     
594         return list;
595 }
596
597 //_____________________________________________________________________________
598 Bool_t AliCDBLocal::Contains(const char* path) const{
599 // check for path in storage's fBaseDirectory
600
601         TString dirName = Form("%s/%s", fBaseDirectory.Data(), path);
602         Bool_t result=kFALSE;
603
604         void* dirPtr = gSystem->OpenDirectory(dirName); 
605         if (dirPtr) result=kTRUE;
606         gSystem->FreeDirectory(dirPtr);
607
608         return result;
609 }
610
611 //_____________________________________________________________________________
612 void AliCDBLocal::QueryValidFiles()
613 {
614 // Query the CDB for files valid for AliCDBStorage::fRun
615 // fills list fValidFileIds with AliCDBId objects created from file name
616
617         if(fVersion != -1) AliWarning ("Version parameter is not used by local storage query!");
618         if(fMetaDataFilter) {
619                 AliWarning ("CDB meta data parameters are not used by local storage query!");
620                 delete fMetaDataFilter; fMetaDataFilter=0;
621         }
622
623         void* storageDirPtr = gSystem->OpenDirectory(fBaseDirectory);
624
625         const char* level0;
626         while ((level0 = gSystem->GetDirEntry(storageDirPtr))) {
627
628                 TString level0Str(level0);
629                 if (level0Str == "." || level0Str == "..") {
630                         continue;
631                 }
632
633                 if (fPathFilter.Level0Comprises(level0)) {
634                         TString level0Dir = Form("%s/%s",fBaseDirectory.Data(),level0);
635                         void* level0DirPtr = gSystem->OpenDirectory(level0Dir);
636                         const char* level1;
637                         while ((level1 = gSystem->GetDirEntry(level0DirPtr))) {
638
639                                 TString level1Str(level1);
640                                 if (level1Str == "." || level1Str == "..") {
641                                         continue;
642                                 }
643
644                                 if (fPathFilter.Level1Comprises(level1)) {
645                                         TString level1Dir = Form("%s/%s/%s",
646                                                         fBaseDirectory.Data(),level0,level1);
647
648                                         void* level1DirPtr = gSystem->OpenDirectory(level1Dir);
649                                         const char* level2;
650                                         while ((level2 = gSystem->GetDirEntry(level1DirPtr))) {
651
652                                                 TString level2Str(level2);
653                                                 if (level2Str == "." || level2Str == "..") {
654                                                         continue;
655                                                 }
656
657                                                 if (fPathFilter.Level2Comprises(level2)) {
658                                                         TString dirName = Form("%s/%s/%s/%s",
659                                                                         fBaseDirectory.Data(),level0,level1,level2);
660
661                                                         void* dirPtr = gSystem->OpenDirectory(dirName);
662
663                                                         const char* filename;
664
665                                                         AliCDBRunRange aRunRange; // the runRange got from filename
666                                                         Int_t aVersion, aSubVersion; // the version and subVersion got from filename
667
668                                                         while ((filename = gSystem->GetDirEntry(dirPtr))) { // loop on files
669
670                                                                 TString aString(filename);
671                                                                 if (aString == "." || aString == "..") continue;
672
673                                                                 if (!FilenameToId(filename, aRunRange, aVersion, aSubVersion)) continue;
674                                                                 AliCDBRunRange runrg(fRun,fRun);
675                                                                 if (!aRunRange.Comprises(runrg)) continue;
676                                                                 // aRunRange contains requested run!
677                                                                 AliCDBPath validPath(level0,level1,level2);
678                                                                 AliCDBId validId(validPath,aRunRange,aVersion,aSubVersion);
679                                                                 fValidFileIds.AddLast(validId.Clone());
680                                                         }
681                                                         gSystem->FreeDirectory(dirPtr);
682                                                 }
683                                         }
684                                         gSystem->FreeDirectory(level1DirPtr);
685                                 }
686                         }
687                         gSystem->FreeDirectory(level0DirPtr);
688                 }
689         }
690         gSystem->FreeDirectory(storageDirPtr);
691
692 }
693
694 //_____________________________________________________________________________
695 Int_t AliCDBLocal::GetLatestVersion(const char* path, Int_t run){
696 // get last version found in the database valid for run and path
697
698         AliCDBPath aCDBPath(path);
699         if(!aCDBPath.IsValid() || aCDBPath.IsWildcard()) {
700                 AliError(Form("Invalid path in request: %s", path));
701                 return -1;
702         }
703
704         AliCDBId query(path, run, run, -1, -1);
705         AliCDBId dataId;
706
707         GetId(query,dataId);
708
709         return dataId.GetVersion();
710
711 }
712
713 //_____________________________________________________________________________
714 Int_t AliCDBLocal::GetLatestSubVersion(const char* path, Int_t run, Int_t version){
715 // get last version found in the database valid for run and path
716
717         AliCDBPath aCDBPath(path);
718         if(!aCDBPath.IsValid() || aCDBPath.IsWildcard()) {
719                 AliError(Form("Invalid path in request: %s", path));
720                 return -1;
721         }
722
723         AliCDBId query(path, run, run, version, -1);
724         AliCDBId dataId;
725
726         GetId(query,dataId);
727
728         return dataId.GetSubVersion();
729
730 }
731
732 /////////////////////////////////////////////////////////////////////////////////////////////////
733 //                                                                                             //
734 // AliCDBLocal factory                                                                         //
735 //                                                                                             //
736 /////////////////////////////////////////////////////////////////////////////////////////////////
737
738 ClassImp(AliCDBLocalFactory)
739
740 //_____________________________________________________________________________
741 Bool_t AliCDBLocalFactory::Validate(const char* dbString) {
742 // check if the string is valid local URI
743
744         TRegexp dbPattern("^local://.+$");
745
746         return TString(dbString).Contains(dbPattern);
747 }
748
749 //_____________________________________________________________________________
750 AliCDBParam* AliCDBLocalFactory::CreateParameter(const char* dbString) {
751 // create AliCDBLocalParam class from the URI string
752
753         if (!Validate(dbString)) {
754                 return NULL;
755         }
756
757         TString pathname(dbString + sizeof("local://") - 1);
758         
759         gSystem->ExpandPathName(pathname);
760
761         if (pathname[0] != '/') {
762                 pathname.Prepend(TString(gSystem->WorkingDirectory()) + '/');
763         }
764
765         return new AliCDBLocalParam(pathname);       
766 }
767
768 //_____________________________________________________________________________
769 AliCDBStorage* AliCDBLocalFactory::Create(const AliCDBParam* param) {
770 // create AliCDBLocal storage instance from parameters
771         
772         if (AliCDBLocalParam::Class() == param->IsA()) {
773                 
774                 const AliCDBLocalParam* localParam = 
775                         (const AliCDBLocalParam*) param;
776                 
777                 return new AliCDBLocal(localParam->GetPath());
778         }
779
780         return NULL;
781 }
782
783 /////////////////////////////////////////////////////////////////////////////////////////////////
784 //                                                                                             //
785 // AliCDBLocal Parameter class                                                                 //                                          //
786 //                                                                                             //
787 /////////////////////////////////////////////////////////////////////////////////////////////////
788
789 ClassImp(AliCDBLocalParam)
790
791 //_____________________________________________________________________________
792 AliCDBLocalParam::AliCDBLocalParam():
793  AliCDBParam(),
794  fDBPath()
795  {
796 // default constructor
797
798 }
799
800 //_____________________________________________________________________________
801 AliCDBLocalParam::AliCDBLocalParam(const char* dbPath):
802  AliCDBParam(),
803  fDBPath(dbPath)
804 {
805 // constructor
806
807         SetType("local");
808         SetURI(TString("local://") + dbPath);
809 }
810
811 //_____________________________________________________________________________
812 AliCDBLocalParam::~AliCDBLocalParam() {
813 // destructor
814
815 }
816
817 //_____________________________________________________________________________
818 AliCDBParam* AliCDBLocalParam::CloneParam() const {
819 // clone parameter
820
821         return new AliCDBLocalParam(fDBPath);
822 }
823
824 //_____________________________________________________________________________
825 ULong_t AliCDBLocalParam::Hash() const {
826 // return Hash function
827
828        return fDBPath.Hash();
829 }
830
831 //_____________________________________________________________________________
832 Bool_t AliCDBLocalParam::IsEqual(const TObject* obj) const {
833 // check if this object is equal to AliCDBParam obj
834
835         if (this == obj) {
836                 return kTRUE;
837         }
838
839         if (AliCDBLocalParam::Class() != obj->IsA()) {
840                 return kFALSE;
841         }
842
843         AliCDBLocalParam* other = (AliCDBLocalParam*) obj;
844
845         return fDBPath == other->fDBPath;
846 }
847