]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliCDBLocal.cxx
Pedestal suppression implemented
[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 AliCDBId* AliCDBLocal::GetId(const AliCDBId& query) {
366 // look for filename matching query (called by GetEntryId)
367
368         TString dirName = Form("%s/%s", fBaseDirectory.Data(), query.GetPath().Data());
369
370         void* dirPtr = gSystem->OpenDirectory(dirName);
371         if (!dirPtr) {
372                 AliDebug(2,Form("Directory <%s> not found", (query.GetPath()).Data()));
373                 AliDebug(2,Form("in DB folder %s", fBaseDirectory.Data()));
374                 return NULL;
375         }
376
377         const char* filename;
378         AliCDBId *result = new AliCDBId();
379         result->SetPath(query.GetPath());
380
381         AliCDBRunRange aRunRange; // the runRange got from filename
382         Int_t aVersion, aSubVersion; // the version and subVersion got from filename
383
384         if (!query.HasVersion()) { // neither version and subversion specified -> look for highest version and subVersion
385
386                 while ((filename = gSystem->GetDirEntry(dirPtr))) { // loop on files
387
388                         TString aString(filename);
389                         if (aString == "." || aString == "..") continue;
390
391                         if (!FilenameToId(filename, aRunRange, aVersion, aSubVersion)) continue;
392                         // aRunRange, aVersion, aSubVersion filled from filename
393
394                         if (!aRunRange.Comprises(query.GetAliCDBRunRange())) continue;
395                         // aRunRange contains requested run!
396
397                         if (result->GetVersion() < aVersion) {
398                                 result->SetVersion(aVersion);
399                                 result->SetSubVersion(aSubVersion);
400
401                                 result->SetFirstRun(
402                                         aRunRange.GetFirstRun());
403                                 result->SetLastRun(
404                                         aRunRange.GetLastRun());
405
406                         } else if (result->GetVersion() == aVersion
407                                 && result->GetSubVersion()
408                                         < aSubVersion) {
409
410                                 result->SetSubVersion(aSubVersion);
411
412                                 result->SetFirstRun(
413                                         aRunRange.GetFirstRun());
414                                 result->SetLastRun(
415                                         aRunRange.GetLastRun());
416                         } else if (result->GetVersion() == aVersion
417                                 && result->GetSubVersion() == aSubVersion){
418                                 AliError(Form("More than one object valid for run %d, version %d_%d!",
419                                         query.GetFirstRun(), aVersion, aSubVersion));
420                                 gSystem->FreeDirectory(dirPtr);
421                                 delete result;
422                                 return NULL;
423                                 }
424                 }
425
426         } else if (!query.HasSubVersion()) { // version specified but not subversion -> look for highest subVersion
427
428                 result->SetVersion(query.GetVersion());
429
430                 while ((filename = gSystem->GetDirEntry(dirPtr))) { // loop on files
431
432                         TString aString(filename);
433                         if (aString == "." || aString == "..") continue;
434
435                         if (!FilenameToId(filename, aRunRange, aVersion, aSubVersion)) continue;
436                         // aRunRange, aVersion, aSubVersion filled from filename
437
438                         if (!aRunRange.Comprises(query.GetAliCDBRunRange())) continue; 
439                         // aRunRange contains requested run!
440
441                         if(query.GetVersion() != aVersion) continue;
442                         // aVersion is requested version!
443
444                         if(result->GetSubVersion() == aSubVersion){
445                                 AliError(Form("More than one object valid for run %d, version %d_%d!",
446                                         query.GetFirstRun(), aVersion, aSubVersion));
447                                 gSystem->FreeDirectory(dirPtr);
448                                 delete result;
449                                 return NULL;
450                         }
451                         if( result->GetSubVersion() < aSubVersion) {
452
453                                 result->SetSubVersion(aSubVersion);
454
455                                 result->SetFirstRun(
456                                         aRunRange.GetFirstRun());
457                                 result->SetLastRun(
458                                         aRunRange.GetLastRun());
459                         } 
460                 }
461
462         } else { // both version and subversion specified
463
464                 //AliCDBId dataId(queryId.GetAliCDBPath(), -1, -1, -1, -1);
465         //Bool_t result;
466         while ((filename = gSystem->GetDirEntry(dirPtr))) { // loop on files
467
468                         TString aString(filename);
469                         if (aString == "." || aString == "..") continue;
470
471                         if (!FilenameToId(filename, aRunRange, aVersion, aSubVersion)) continue;
472                         // aRunRange, aVersion, aSubVersion filled from filename
473
474                         if (!aRunRange.Comprises(query.GetAliCDBRunRange())) continue;
475                         // aRunRange contains requested run!
476
477                         if(query.GetVersion() != aVersion || query.GetSubVersion() != aSubVersion) continue;
478                         // aVersion and aSubVersion are requested version and subVersion!
479
480                         if(result->GetVersion() == aVersion && result->GetSubVersion() == aSubVersion){
481                                 AliError(Form("More than one object valid for run %d, version %d_%d!",
482                                         query.GetFirstRun(), aVersion, aSubVersion));
483                                 gSystem->FreeDirectory(dirPtr);
484                                 delete result;
485                                 return NULL;
486                         }
487                         result->SetVersion(aVersion);
488                         result->SetSubVersion(aSubVersion);
489                         result->SetFirstRun(aRunRange.GetFirstRun());
490                         result->SetLastRun(aRunRange.GetLastRun());
491                         
492                 }
493         }
494
495         gSystem->FreeDirectory(dirPtr);
496
497         return result;
498 }
499
500 //_____________________________________________________________________________
501 AliCDBEntry* AliCDBLocal::GetEntry(const AliCDBId& queryId) {
502 // get AliCDBEntry from the database
503
504         AliCDBId* dataId = GetEntryId(queryId);
505
506         if (!dataId || !dataId->IsSpecified()) return NULL;
507
508         TString filename;
509         if (!IdToFilename(*dataId, filename)) {
510
511                 AliDebug(2,Form("Bad data ID encountered! Subnormal error!"));
512                 delete dataId;
513                 return NULL;
514         }
515
516         TFile file(filename, "READ"); // open file
517         if (!file.IsOpen()) {
518                 AliDebug(2,Form("Can't open file <%s>!", filename.Data()));
519                 delete dataId;
520                 return NULL;
521         }
522
523         // get the only AliCDBEntry object from the file
524         // the object in the file is an AliCDBEntry entry named "AliCDBEntry"
525
526         AliCDBEntry* anEntry = dynamic_cast<AliCDBEntry*> (file.Get("AliCDBEntry"));
527         if (!anEntry) {
528                 AliDebug(2,Form("Bad storage data: No AliCDBEntry in file!"));
529                 file.Close();
530                 delete dataId;
531                 return NULL;
532         }
533
534         AliCDBId& entryId = anEntry->GetId();
535
536         // The object's Id are not reset during storage
537         // If object's Id runRange or version do not match with filename,
538         // it means that someone renamed file by hand. In this case a warning msg is issued.
539
540         anEntry-> SetLastStorage("local");
541
542         if(!entryId.IsEqual(dataId)){
543                 AliWarning(Form("Mismatch between file name and object's Id!"));
544                 AliWarning(Form("File name: %s", dataId->ToString().Data()));
545                 AliWarning(Form("Object's Id: %s", entryId.ToString().Data()));
546         }
547
548         // Check whether entry contains a TTree. In case load the tree in memory!
549         LoadTreeFromFile(anEntry);
550
551         // close file, return retieved entry
552         file.Close();
553         delete dataId;
554         return anEntry;
555 }
556
557 //_____________________________________________________________________________
558 AliCDBId* AliCDBLocal::GetEntryId(const AliCDBId& queryId) {
559 // get AliCDBId from the database
560
561         AliCDBId* dataId = 0;
562
563         // look for a filename matching query requests (path, runRange, version, subVersion)
564         if (!queryId.HasVersion()) {
565                 // if version is not specified, first check the selection criteria list
566                 AliCDBId selectedId(queryId);
567                 GetSelection(&selectedId);
568                 dataId = GetId(selectedId);
569         } else {
570                 dataId = GetId(queryId);
571         }
572
573         if (dataId && !dataId->IsSpecified()) {
574                 delete dataId;
575                 return NULL;
576         }
577
578         return dataId;
579 }
580
581 //_____________________________________________________________________________
582 void AliCDBLocal::GetEntriesForLevel0(const char* level0,
583         const AliCDBId& queryId, TList* result) {
584 // multiple request (AliCDBStorage::GetAll)
585
586         TString level0Dir = Form("%s/%s", fBaseDirectory.Data(), level0);
587
588         void* level0DirPtr = gSystem->OpenDirectory(level0Dir);
589         if (!level0DirPtr) {
590                 AliDebug(2,Form("Can't open level0 directory <%s>!",
591                         level0Dir.Data()));
592                 return;
593         }
594
595         const char* level1;
596         Long_t flag=0;
597         while ((level1 = gSystem->GetDirEntry(level0DirPtr))) {
598
599                 TString level1Str(level1);
600                 if (level1Str == "." || level1Str == "..") {
601                         continue;
602                 }
603                 
604                 TString fullPath = Form("%s/%s",level0Dir.Data(), level1); 
605
606                 Int_t res=gSystem->GetPathInfo(fullPath.Data(), 0, (Long64_t*) 0, &flag, 0);
607                 
608                 if(res){
609                         AliDebug(2, Form("Error reading entry %s !",level1Str.Data()));
610                         continue;
611                 }
612                 if(!(flag&2)) continue; // bit 1 of flag = directory!
613                 
614                 if (queryId.GetAliCDBPath().Level1Comprises(level1)) {
615                         GetEntriesForLevel1(level0, level1, queryId, result);
616                 }
617         }
618
619         gSystem->FreeDirectory(level0DirPtr);
620 }
621
622 //_____________________________________________________________________________
623 void AliCDBLocal::GetEntriesForLevel1(const char* level0, const char* level1,
624         const AliCDBId& queryId, TList* result) {
625 // multiple request (AliCDBStorage::GetAll)
626
627         TString level1Dir = Form("%s/%s/%s", fBaseDirectory.Data(), level0,level1);
628
629         void* level1DirPtr = gSystem->OpenDirectory(level1Dir);
630         if (!level1DirPtr) {
631                 AliDebug(2,Form("Can't open level1 directory <%s>!",
632                         level1Dir.Data()));
633                 return;
634         }
635
636         const char* level2;
637         Long_t flag=0;
638         while ((level2 = gSystem->GetDirEntry(level1DirPtr))) {
639
640                 TString level2Str(level2);
641                 if (level2Str == "." || level2Str == "..") {
642                         continue;
643                 }
644
645                 TString fullPath = Form("%s/%s",level1Dir.Data(), level2); 
646
647                 Int_t res=gSystem->GetPathInfo(fullPath.Data(), 0, (Long64_t*) 0, &flag, 0);
648                 
649                 if(res){
650                         AliDebug(2, Form("Error reading entry %s !",level2Str.Data()));
651                         continue;
652                 }
653                 if(!(flag&2)) continue; // bit 1 of flag = directory!
654                 
655                 if (queryId.GetAliCDBPath().Level2Comprises(level2)) {
656
657                         AliCDBPath entryPath(level0, level1, level2);
658                         AliCDBId entryId(entryPath, queryId.GetAliCDBRunRange(),
659                                 queryId.GetVersion(), queryId.GetSubVersion());
660
661                         AliCDBEntry* anEntry = GetEntry(entryId);
662                         if (anEntry) {
663                                 result->Add(anEntry);
664                         }
665                 }
666         }
667
668         gSystem->FreeDirectory(level1DirPtr);
669 }
670
671 //_____________________________________________________________________________
672 TList* AliCDBLocal::GetEntries(const AliCDBId& queryId) {
673 // multiple request (AliCDBStorage::GetAll)
674         
675         void* storageDirPtr = gSystem->OpenDirectory(fBaseDirectory);
676         if (!storageDirPtr) {
677                 AliDebug(2,Form("Can't open storage directory <%s>",
678                         fBaseDirectory.Data()));
679                 return NULL;
680         }
681
682         TList* result = new TList();
683         result->SetOwner();
684
685         const char* level0;
686         Long_t flag=0;
687         while ((level0 = gSystem->GetDirEntry(storageDirPtr))) {
688
689                 TString level0Str(level0);
690                 if (level0Str == "." || level0Str == "..") {
691                         continue;
692                 }
693                 
694                 TString fullPath = Form("%s/%s",fBaseDirectory.Data(), level0); 
695
696                 Int_t res=gSystem->GetPathInfo(fullPath.Data(), 0, (Long64_t*) 0, &flag, 0);
697                 
698                 if(res){
699                         AliDebug(2, Form("Error reading entry %s !",level0Str.Data()));
700                         continue;
701                 }
702                 
703                 if(!(flag&2)) continue; // bit 1 of flag = directory!                           
704
705                 if (queryId.GetAliCDBPath().Level0Comprises(level0)) {
706                         GetEntriesForLevel0(level0, queryId, result);
707                 }
708         }
709
710         gSystem->FreeDirectory(storageDirPtr);
711
712         return result;  
713 }
714
715 //_____________________________________________________________________________
716 Bool_t AliCDBLocal::PutEntry(AliCDBEntry* entry) {
717 // put an AliCDBEntry object into the database
718
719         AliCDBId& id = entry->GetId();
720
721         // set version and subVersion for the entry to be stored
722         if (!PrepareId(id)) return kFALSE;
723
724         
725         // build filename from entry's id
726         TString filename="";
727         if (!IdToFilename(id, filename)) {
728
729                 AliDebug(2,Form("Bad ID encountered! Subnormal error!"));
730                 return kFALSE;
731         }
732
733         // open file
734         TFile file(filename, "CREATE");
735         if (!file.IsOpen()) {
736                 AliError(Form("Can't open file <%s>!", filename.Data()));
737                 return kFALSE;
738         }
739         
740         //SetTreeToFile(entry, &file);
741
742         entry->SetVersion(id.GetVersion());
743         entry->SetSubVersion(id.GetSubVersion());
744
745         // write object (key name: "AliCDBEntry")
746         Bool_t result = file.WriteTObject(entry, "AliCDBEntry");
747         if (!result) AliDebug(2,Form("Can't write entry to file: %s", filename.Data()));
748
749         file.Close();
750         if(result) {
751                 if(!(id.GetPath().Contains("SHUTTLE/STATUS")))
752                         AliInfo(Form("CDB object stored into file %s",filename.Data()));
753         }
754
755         return result;
756 }
757
758 //_____________________________________________________________________________
759 TList* AliCDBLocal::GetIdListFromFile(const char* fileName){
760
761         TString fullFileName(fileName);
762         fullFileName.Prepend(fBaseDirectory+'/');
763         TFile *file = TFile::Open(fullFileName);
764         if (!file) {
765                 AliError(Form("Can't open selection file <%s>!", fullFileName.Data()));
766                 return NULL;
767         }
768         file->cd();
769
770         TList *list = new TList();
771         list->SetOwner();
772         int i=0;
773         TString keycycle;
774         
775         AliCDBId *id;
776         while(1){
777                 i++;
778                 keycycle = "AliCDBId;";
779                 keycycle+=i;
780                 
781                 id = (AliCDBId*) file->Get(keycycle);
782                 if(!id) break;
783                 list->AddFirst(id);
784         }
785         file->Close(); delete file; file=0;     
786         return list;
787 }
788
789 //_____________________________________________________________________________
790 Bool_t AliCDBLocal::Contains(const char* path) const{
791 // check for path in storage's fBaseDirectory
792
793         TString dirName = Form("%s/%s", fBaseDirectory.Data(), path);
794         Bool_t result=kFALSE;
795
796         void* dirPtr = gSystem->OpenDirectory(dirName); 
797         if (dirPtr) result=kTRUE;
798         gSystem->FreeDirectory(dirPtr);
799
800         return result;
801 }
802
803 //_____________________________________________________________________________
804 void AliCDBLocal::QueryValidFiles()
805 {
806 // Query the CDB for files valid for AliCDBStorage::fRun
807 // fills list fValidFileIds with AliCDBId objects created from file name
808
809         if(fVersion != -1) AliWarning ("Version parameter is not used by local storage query!");
810         if(fMetaDataFilter) {
811                 AliWarning ("CDB meta data parameters are not used by local storage query!");
812                 delete fMetaDataFilter; fMetaDataFilter=0;
813         }
814
815         void* storageDirPtr = gSystem->OpenDirectory(fBaseDirectory);
816
817         const char* level0;
818         while ((level0 = gSystem->GetDirEntry(storageDirPtr))) {
819
820                 TString level0Str(level0);
821                 if (level0Str == "." || level0Str == "..") {
822                         continue;
823                 }
824
825                 if (fPathFilter.Level0Comprises(level0)) {
826                         TString level0Dir = Form("%s/%s",fBaseDirectory.Data(),level0);
827                         void* level0DirPtr = gSystem->OpenDirectory(level0Dir);
828                         const char* level1;
829                         while ((level1 = gSystem->GetDirEntry(level0DirPtr))) {
830
831                                 TString level1Str(level1);
832                                 if (level1Str == "." || level1Str == "..") {
833                                         continue;
834                                 }
835
836                                 if (fPathFilter.Level1Comprises(level1)) {
837                                         TString level1Dir = Form("%s/%s/%s",
838                                                         fBaseDirectory.Data(),level0,level1);
839
840                                         void* level1DirPtr = gSystem->OpenDirectory(level1Dir);
841                                         const char* level2;
842                                         while ((level2 = gSystem->GetDirEntry(level1DirPtr))) {
843
844                                                 TString level2Str(level2);
845                                                 if (level2Str == "." || level2Str == "..") {
846                                                         continue;
847                                                 }
848
849                                                 if (fPathFilter.Level2Comprises(level2)) {
850                                                         TString dirName = Form("%s/%s/%s/%s",
851                                                                         fBaseDirectory.Data(),level0,level1,level2);
852
853                                                         void* dirPtr = gSystem->OpenDirectory(dirName);
854
855                                                         const char* filename;
856
857                                                         AliCDBRunRange aRunRange; // the runRange got from filename
858                                                         Int_t aVersion, aSubVersion; // the version and subVersion got from filename
859
860                                                         while ((filename = gSystem->GetDirEntry(dirPtr))) { // loop on files
861
862                                                                 TString aString(filename);
863                                                                 if (aString == "." || aString == "..") continue;
864
865                                                                 if (!FilenameToId(filename, aRunRange, aVersion, aSubVersion)) continue;
866                                                                 AliCDBRunRange runrg(fRun,fRun);
867                                                                 if (!aRunRange.Comprises(runrg)) continue;
868                                                                 // aRunRange contains requested run!
869                                                                 AliCDBPath validPath(level0,level1,level2);
870                                                                 AliCDBId validId(validPath,aRunRange,aVersion,aSubVersion);
871                                                                 fValidFileIds.AddLast(validId.Clone());
872                                                         }
873                                                         gSystem->FreeDirectory(dirPtr);
874                                                 }
875                                         }
876                                         gSystem->FreeDirectory(level1DirPtr);
877                                 }
878                         }
879                         gSystem->FreeDirectory(level0DirPtr);
880                 }
881         }
882         gSystem->FreeDirectory(storageDirPtr);
883
884 }
885
886 //_____________________________________________________________________________
887 Int_t AliCDBLocal::GetLatestVersion(const char* path, Int_t run){
888 // get last version found in the database valid for run and path
889
890         AliCDBPath aCDBPath(path);
891         if(!aCDBPath.IsValid() || aCDBPath.IsWildcard()) {
892                 AliError(Form("Invalid path in request: %s", path));
893                 return -1;
894         }
895
896         AliCDBId query(path, run, run, -1, -1);
897         AliCDBId* dataId = GetId(query);
898
899         if(!dataId) return -1;
900
901         Int_t version = dataId->GetVersion();
902         delete dataId;
903
904         return version;
905
906 }
907
908 //_____________________________________________________________________________
909 Int_t AliCDBLocal::GetLatestSubVersion(const char* path, Int_t run, Int_t version){
910 // get last version found in the database valid for run and path
911
912         AliCDBPath aCDBPath(path);
913         if(!aCDBPath.IsValid() || aCDBPath.IsWildcard()) {
914                 AliError(Form("Invalid path in request: %s", path));
915                 return -1;
916         }
917
918         AliCDBId query(path, run, run, version, -1);
919         AliCDBId *dataId = GetId(query);
920
921         if(!dataId) return -1;
922
923         Int_t subVersion = dataId->GetSubVersion();
924
925         delete dataId;
926         return subVersion;
927
928 }
929
930 /////////////////////////////////////////////////////////////////////////////////////////////////
931 //                                                                                             //
932 // AliCDBLocal factory                                                                         //
933 //                                                                                             //
934 /////////////////////////////////////////////////////////////////////////////////////////////////
935
936 ClassImp(AliCDBLocalFactory)
937
938 //_____________________________________________________________________________
939 Bool_t AliCDBLocalFactory::Validate(const char* dbString) {
940 // check if the string is valid local URI
941
942         TRegexp dbPattern("^local://.+$");
943
944         return TString(dbString).Contains(dbPattern);
945 }
946
947 //_____________________________________________________________________________
948 AliCDBParam* AliCDBLocalFactory::CreateParameter(const char* dbString) {
949 // create AliCDBLocalParam class from the URI string
950
951         if (!Validate(dbString)) {
952                 return NULL;
953         }
954
955         TString pathname(dbString + sizeof("local://") - 1);
956         
957         gSystem->ExpandPathName(pathname);
958
959         if (pathname[0] != '/') {
960                 pathname.Prepend(TString(gSystem->WorkingDirectory()) + '/');
961         }
962
963         return new AliCDBLocalParam(pathname);
964 }
965
966 //_____________________________________________________________________________
967 AliCDBStorage* AliCDBLocalFactory::Create(const AliCDBParam* param) {
968 // create AliCDBLocal storage instance from parameters
969         
970         if (AliCDBLocalParam::Class() == param->IsA()) {
971                 
972                 const AliCDBLocalParam* localParam = 
973                         (const AliCDBLocalParam*) param;
974                 
975                 return new AliCDBLocal(localParam->GetPath());
976         }
977
978         return NULL;
979 }
980
981 /////////////////////////////////////////////////////////////////////////////////////////////////
982 //                                                                                             //
983 // AliCDBLocal Parameter class                                                                 //                                          //
984 //                                                                                             //
985 /////////////////////////////////////////////////////////////////////////////////////////////////
986
987 ClassImp(AliCDBLocalParam)
988
989 //_____________________________________________________________________________
990 AliCDBLocalParam::AliCDBLocalParam():
991  AliCDBParam(),
992  fDBPath()
993  {
994 // default constructor
995
996 }
997
998 //_____________________________________________________________________________
999 AliCDBLocalParam::AliCDBLocalParam(const char* dbPath):
1000  AliCDBParam(),
1001  fDBPath(dbPath)
1002 {
1003 // constructor
1004
1005         SetType("local");
1006         SetURI(TString("local://") + dbPath);
1007 }
1008
1009 //_____________________________________________________________________________
1010 AliCDBLocalParam::~AliCDBLocalParam() {
1011 // destructor
1012
1013 }
1014
1015 //_____________________________________________________________________________
1016 AliCDBParam* AliCDBLocalParam::CloneParam() const {
1017 // clone parameter
1018
1019         return new AliCDBLocalParam(fDBPath);
1020 }
1021
1022 //_____________________________________________________________________________
1023 ULong_t AliCDBLocalParam::Hash() const {
1024 // return Hash function
1025
1026        return fDBPath.Hash();
1027 }
1028
1029 //_____________________________________________________________________________
1030 Bool_t AliCDBLocalParam::IsEqual(const TObject* obj) const {
1031 // check if this object is equal to AliCDBParam obj
1032
1033         if (this == obj) {
1034                 return kTRUE;
1035         }
1036
1037         if (AliCDBLocalParam::Class() != obj->IsA()) {
1038                 return kFALSE;
1039         }
1040
1041         AliCDBLocalParam* other = (AliCDBLocalParam*) obj;
1042
1043         return fDBPath == other->fDBPath;
1044 }
1045