- Correct setting of FUDGEM parameter.
[u/mrichter/AliRoot.git] / STEER / AliCDBDump.cxx
CommitLineData
2c8628dd 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
9e1ceb13 16/////////////////////////////////////////////////////////////////////
17// //
18// class AliCDBDump //
19// access class to a DataBase in a dump storage (single file) //
20// //
21/////////////////////////////////////////////////////////////////////
2c8628dd 22
9e1ceb13 23#include <TSystem.h>
2c8628dd 24#include <TKey.h>
9e1ceb13 25#include <TFile.h>
26#include <TRegexp.h>
27#include <TObjString.h>
28#include <TList.h>
2c8628dd 29
9e1ceb13 30#include "AliCDBDump.h"
31#include "AliCDBEntry.h"
32#include "AliLog.h"
2c8628dd 33
fe913d8f 34ClassImp(AliCDBDump)
2c8628dd 35
2c8628dd 36//_____________________________________________________________________________
9e1ceb13 37AliCDBDump::AliCDBDump(const char* dbFile, Bool_t readOnly):
38fFile(NULL), fReadOnly(readOnly) {
2c8628dd 39// constructor
40
9e1ceb13 41 // opening file
42 fFile = TFile::Open(dbFile, fReadOnly ? "READ" : "UPDATE");
43 if (!fFile) {
44 AliError(Form("Can't open file <%s>!" , dbFile));
45 } else {
46 AliDebug(2,Form("File <%s> opened",dbFile));
47 if(fReadOnly) AliDebug(2,Form("in read-only mode"));
48 }
2c8628dd 49}
50
51//_____________________________________________________________________________
9e1ceb13 52AliCDBDump::~AliCDBDump() {
2c8628dd 53// destructor
54
9e1ceb13 55 if (fFile) {
56 fFile->Close();
57 delete fFile;
58 }
2c8628dd 59}
60
9e1ceb13 61
2c8628dd 62//_____________________________________________________________________________
9e1ceb13 63Bool_t AliCDBDump::KeyNameToId(const char* keyname, AliCDBRunRange& runRange,
9e1ceb13 64 Int_t& version, Int_t& subVersion) {
fdf65bb5 65// build AliCDBId from keyname numbers
9e1ceb13 66
67 Ssiz_t mSize;
2c8628dd 68
9e1ceb13 69 // valid keyname: Run#firstRun_#lastRun_v#version_s#subVersion.root
70 TRegexp keyPattern("^Run[0-9]+_[0-9]+_v[0-9]+_s[0-9]+$");
71 keyPattern.Index(keyname, &mSize);
72 if (!mSize) {
73 AliDebug(2,Form("Bad keyname <%s>.", keyname));
74 return kFALSE;
75 }
76
77 TObjArray* strArray = (TObjArray*) TString(keyname).Tokenize("_");
78
79 TString firstRunString(((TObjString*) strArray->At(0))->GetString());
80 runRange.SetFirstRun(atoi(firstRunString.Data() + 3));
81 runRange.SetLastRun(atoi(((TObjString*) strArray->At(1))->GetString()));
82
83 TString verString(((TObjString*) strArray->At(2))->GetString());
84 version = atoi(verString.Data() + 1);
85
86 TString subVerString(((TObjString*) strArray->At(3))->GetString());
87 subVersion = atoi(subVerString.Data() + 1);
88
89 delete strArray;
90
91 return kTRUE;
2c8628dd 92}
93
94//_____________________________________________________________________________
9e1ceb13 95Bool_t AliCDBDump::IdToKeyName(const AliCDBRunRange& runRange, Int_t version,
96 Int_t subVersion, TString& keyname) {
97// build key name from AliCDBId data (run range, version, subVersion)
2c8628dd 98
9e1ceb13 99 if (!runRange.IsValid()) {
100 AliWarning(Form("Invalid run range <%d, %d>.",
101 runRange.GetFirstRun(), runRange.GetLastRun()));
102 return kFALSE;
103 }
104
105 if (version < 0) {
106 AliWarning(Form("Invalid version <%d>.", version));
107 return kFALSE;
108 }
109
110 if (subVersion < 0) {
111 AliWarning(Form("Invalid subversion <%s>.", subVersion));
112 return kFALSE;
113 }
114
115 keyname += "Run";
116 keyname += runRange.GetFirstRun();
117 keyname += "_";
118 keyname += runRange.GetLastRun();
119 keyname += "_v";
120 keyname += version;
121 keyname += "_s";
122 keyname += subVersion;
123
124 return kTRUE;
2c8628dd 125}
126
9e1ceb13 127//_____________________________________________________________________________
128Bool_t AliCDBDump::MkDir(const TString& path) {
129// descend into TDirectory, making TDirectories if they don't exist
130 TObjArray* strArray = (TObjArray*) path.Tokenize("/");
131
132 TIter iter(strArray);
133 TObjString* str;
134
135 while ((str = (TObjString*) iter.Next())) {
136
137 TString dirName(str->GetString());
138 if (!dirName.Length()) {
139 continue;
140 }
141
142 if (gDirectory->cd(dirName)) {
143 continue;
144 }
145
146 TDirectory* aDir = gDirectory->mkdir(dirName, "");
147 if (!aDir) {
148 AliError(Form("Can't create directory <%s>!",
149 dirName.Data()));
150 delete strArray;
151
152 return kFALSE;
153 }
154
155 aDir->cd();
156 }
157
158 delete strArray;
159
160 return kTRUE;
161}
2c8628dd 162
163//_____________________________________________________________________________
9e1ceb13 164Bool_t AliCDBDump::PrepareId(AliCDBId& id) {
165// prepare id (version, subVersion) of the object that will be stored (called by PutEntry)
166
167 AliCDBRunRange aRunRange; // the runRange got from filename
168 AliCDBRunRange lastRunRange(-1,-1); // highest runRange found
169 Int_t aVersion, aSubVersion; // the version subVersion got from filename
170 Int_t lastVersion = 0, lastSubVersion = -1; // highest version and subVersion found
171
172
173 TIter iter(gDirectory->GetListOfKeys());
174 TKey* key;
175
176 if (!id.HasVersion()) { // version not specified: look for highest version & subVersion
177
178 while ((key = (TKey*) iter.Next())) { // loop on keys
179
180 const char* keyName = key->GetName();
181
182 if (!KeyNameToId(keyName, aRunRange, aVersion,
183 aSubVersion)) {
184 AliWarning(Form(
185 "Bad keyname <%s>!I'll skip it.", keyName));
186 continue;
187 }
188
189 if (!aRunRange.Overlaps(id.GetAliCDBRunRange())) continue;
190 if(aVersion < lastVersion) continue;
191 if(aVersion > lastVersion) lastSubVersion = -1;
192 if(aSubVersion < lastSubVersion) continue;
193 lastVersion = aVersion;
194 lastSubVersion = aSubVersion;
195 lastRunRange = aRunRange;
196 }
197
198 id.SetVersion(lastVersion);
199 id.SetSubVersion(lastSubVersion + 1);
200
201 } else { // version specified, look for highest subVersion only
202
203 while ((key = (TKey*) iter.Next())) { // loop on the keys
204
205 const char* keyName = key->GetName();
206
207 if (!KeyNameToId(keyName, aRunRange, aVersion,
208 aSubVersion)) {
209 AliWarning(Form(
210 "Bad keyname <%s>!I'll skip it.", keyName));
211 continue;
212 }
213
214 if (aRunRange.Overlaps(id.GetAliCDBRunRange())
215 && aVersion == id.GetVersion()
216 && aSubVersion > lastSubVersion) {
217 lastSubVersion = aSubVersion;
218 lastRunRange = aRunRange;
219 }
220
221 }
222
223 id.SetSubVersion(lastSubVersion + 1);
224 }
225
226 TString lastStorage = id.GetLastStorage();
227 if(lastStorage.Contains(TString("grid"), TString::kIgnoreCase) &&
228 id.GetSubVersion() > 0 ){
229 AliError(Form("Grid to Dump Storage error! local object with version v%d_s%d found:",id.GetVersion(), id.GetSubVersion()-1));
230 AliError(Form("This object has been already transferred from Grid (check v%d_s0)!",id.GetVersion()));
231 return kFALSE;
232 }
233
234 if(lastStorage.Contains(TString("new"), TString::kIgnoreCase) &&
235 id.GetSubVersion() > 0 ){
236 AliWarning(Form("*** WARNING! a NEW object is being stored with version v%d_s%d",
237 id.GetVersion(),id.GetSubVersion()));
238 AliWarning(Form("and it will hide previously stored object with v%d_s%d!",
239 id.GetVersion(),id.GetSubVersion()-1));
240 }
241
242 if(!lastRunRange.IsAnyRange() && !(lastRunRange.IsEqual(& id.GetAliCDBRunRange())))
243 AliWarning(Form("Run range modified w.r.t. previous version (Run%d_%d_v%d_s%d)",
244 lastRunRange.GetFirstRun(), lastRunRange.GetLastRun(),
245 id.GetVersion(), id.GetSubVersion()-1));
246
247 return kTRUE;
248
249}
250
251
252//_____________________________________________________________________________
253AliCDBId AliCDBDump::GetId(const AliCDBId& query) {
254// look for filename matching query (called by GetEntry)
255
256 AliCDBId result(query.GetAliCDBPath(), -1, -1, -1, -1);
257
258 AliCDBRunRange aRunRange; // the runRange got from filename
259 Int_t aVersion, aSubVersion; // the version and subVersion got from filename
260
261 TIter iter(gDirectory->GetListOfKeys());
262 TKey* key;
263
264 if (!query.HasVersion()) { // neither version and subversion specified -> look for highest version and subVersion
265
266 while ((key = (TKey*) iter.Next())) { // loop on the keys
267
268 if (!KeyNameToId(key->GetName(), aRunRange, aVersion, aSubVersion)) continue;
269 // aRunRange, aVersion, aSubVersion filled from filename
270
271 if (!aRunRange.Comprises(query.GetAliCDBRunRange())) continue;
272 // aRunRange contains requested run!
273
274 if (result.GetVersion() < aVersion) {
275 result.SetVersion(aVersion);
276 result.SetSubVersion(aSubVersion);
277
278 result.SetFirstRun(
279 aRunRange.GetFirstRun());
280 result.SetLastRun(
281 aRunRange.GetLastRun());
282
283 } else if (result.GetVersion() == aVersion
284 && result.GetSubVersion()
285 < aSubVersion) {
286
287 result.SetSubVersion(aSubVersion);
288
289 result.SetFirstRun(
290 aRunRange.GetFirstRun());
291 result.SetLastRun(
292 aRunRange.GetLastRun());
293 } else if (result.GetVersion() == aVersion
294 && result.GetSubVersion() == aSubVersion){
295 AliError(Form("More than one object valid for run %d, version %d_%d!",
296 query.GetFirstRun(), aVersion, aSubVersion));
297 result.SetRunRange(-1,-1); result.SetVersion(-1); result.SetSubVersion(-1);
298 return result;
299 }
300 }
301
302 } else if (!query.HasSubVersion()) { // version specified but not subversion -> look for highest subVersion
303
304 result.SetVersion(query.GetVersion());
305
306 while ((key = (TKey*) iter.Next())) { // loop on the keys
307
308 if (!KeyNameToId(key->GetName(), aRunRange, aVersion, aSubVersion)) continue;
309 // aRunRange, aVersion, aSubVersion filled from filename
310
311 if (!aRunRange.Comprises(query.GetAliCDBRunRange())) continue;
312 // aRunRange contains requested run!
313
314 if(query.GetVersion() != aVersion) continue;
315 // aVersion is requested version!
316
317 if(result.GetSubVersion() == aSubVersion){
318 AliError(Form("More than one object valid for run %d, version %d_%d!",
319 query.GetFirstRun(), aVersion, aSubVersion));
320 result.SetRunRange(-1,-1); result.SetVersion(-1); result.SetSubVersion(-1);
321 return result;
322 }
323 if( result.GetSubVersion() < aSubVersion) {
324
325 result.SetSubVersion(aSubVersion);
326
327 result.SetFirstRun(
328 aRunRange.GetFirstRun());
329 result.SetLastRun(
330 aRunRange.GetLastRun());
331 }
332 }
333
334 } else { // both version and subversion specified
335
336 while ((key = (TKey*) iter.Next())) { // loop on the keys
337
338 if (!KeyNameToId(key->GetName(), aRunRange, aVersion, aSubVersion)) continue;
339 // aRunRange, aVersion, aSubVersion filled from filename
340
341 if (!aRunRange.Comprises(query.GetAliCDBRunRange())) continue;
342 // aRunRange contains requested run!
343
344 if(query.GetVersion() != aVersion || query.GetSubVersion() != aSubVersion) continue;
345 // aVersion and aSubVersion are requested version and subVersion!
346
347 if(result.GetVersion() == aVersion && result.GetSubVersion() == aSubVersion){
348 AliError(Form("More than one object valid for run %d, version %d_%d!",
349 query.GetFirstRun(), aVersion, aSubVersion));
350 result.SetRunRange(-1,-1); result.SetVersion(-1); result.SetSubVersion(-1);
351 return result;
352 }
353 result.SetVersion(aVersion);
354 result.SetSubVersion(aSubVersion);
355 result.SetFirstRun(aRunRange.GetFirstRun());
356 result.SetLastRun(aRunRange.GetLastRun());
357
358 }
359 }
360
361 return result;
362}
363
364//_____________________________________________________________________________
365AliCDBEntry* AliCDBDump::GetEntry(const AliCDBId& query) {
366// get AliCDBEntry from the database
367
368 TDirectory::TContext context(gDirectory, fFile);
369
370 if (!(fFile && fFile->IsOpen())) {
371 AliError("AliCDBDump storage is not initialized properly");
372 return NULL;
373 }
374
375 if (!gDirectory->cd(query.GetPath())) {
376 return NULL;
377 }
378
379 AliCDBId dataId;
380
381 // look for a filename matching query requests (path, runRange, version, subVersion)
382 if (!query.HasVersion()) {
383 // if version is not specified, first check the selection criteria list
384 dataId = GetId(GetSelection(query));
385 } else {
386 dataId = GetId(query);
387 }
388
389 if (!dataId.IsSpecified()) {
390 return NULL;
391 }
392
393 TString keyname;
394 if (!IdToKeyName(dataId.GetAliCDBRunRange(), dataId.GetVersion(),
395 dataId.GetSubVersion(), keyname)) {
396 AliError("Bad ID encountered! Subnormal error!");
397 return NULL;
398 }
399
400 // get the only AliCDBEntry object from the file
401 // the object in the file is an AliCDBEntry entry named keyname
402 // keyName = Run#firstRun_#lastRun_v#version_s#subVersion
403
404 TObject* anObject = gDirectory->Get(keyname);
405 if (!anObject) {
406 AliError("Bad storage data: NULL entry object!");
407 return NULL;
408 }
409
410 if (AliCDBEntry::Class() != anObject->IsA()) {
411 AliError("Bad storage data: Invalid entry object!");
412 return NULL;
413 }
414
415 ((AliCDBEntry*) anObject)->SetLastStorage("dump");
416
417 return (AliCDBEntry*) anObject;
418}
419
420//_____________________________________________________________________________
421void AliCDBDump::GetEntriesForLevel0(const AliCDBId& queryId, TList* result) {
422// multiple request (AliCDBStorage::GetAll)
423
424 TDirectory* saveDir = gDirectory;
425
426 TIter iter(gDirectory->GetListOfKeys());
427 TKey* key;
428
429 while ((key = (TKey*) iter.Next())) {
430
431 TString keyNameStr(key->GetName());
432 if (queryId.GetAliCDBPath().Level1Comprises(keyNameStr)) {
433 gDirectory->cd(keyNameStr);
434 GetEntriesForLevel1(queryId, result);
435
436 saveDir->cd();
437 }
438 }
439}
440
441//_____________________________________________________________________________
442void AliCDBDump::GetEntriesForLevel1(const AliCDBId& queryId, TList* result) {
443// multiple request (AliCDBStorage::GetAll)
444
445 TIter iter(gDirectory->GetListOfKeys());
446 TKey* key;
447
448 TDirectory* level0Dir = (TDirectory*) gDirectory->GetMother();
449
450 while ((key = (TKey*) iter.Next())) {
451
452 TString keyNameStr(key->GetName());
453 if (queryId.GetAliCDBPath().Level2Comprises(keyNameStr)) {
454
455 AliCDBPath aPath(level0Dir->GetName(),
456 gDirectory->GetName(), keyNameStr);
457 AliCDBId anId(aPath, queryId.GetAliCDBRunRange(),
458 queryId.GetVersion(), -1);
459
460 AliCDBEntry* anEntry = GetEntry(anId);
461 if (anEntry) {
462 result->Add(anEntry);
463 }
464
465 }
466 }
467
468}
469
470
471//_____________________________________________________________________________
472TList* AliCDBDump::GetEntries(const AliCDBId& queryId) {
473// multiple request (AliCDBStorage::GetAll)
474
475 TDirectory::TContext context(gDirectory, fFile);
476
477 if (!(fFile && fFile->IsOpen())) {
478 AliError("AliCDBDump storage is not initialized properly");
479 return NULL;
480 }
481
482 TList* result = new TList();
483 result->SetOwner();
484
485 TIter iter(gDirectory->GetListOfKeys());
486 TKey* key;
487
488 while ((key = (TKey*) iter.Next())) {
489
490 TString keyNameStr(key->GetName());
491 if (queryId.GetAliCDBPath().Level0Comprises(keyNameStr)) {
492 gDirectory->cd(keyNameStr);
493 GetEntriesForLevel0(queryId, result);
494
495 fFile->cd();
496 }
497 }
498
499 return result;
500}
501
502//_____________________________________________________________________________
503Bool_t AliCDBDump::PutEntry(AliCDBEntry* entry) {
504// put an AliCDBEntry object into the database
505
506 TDirectory::TContext context(gDirectory, fFile);
507
508 if (!(fFile && fFile->IsOpen())) {
509 AliError("AliCDBDump storage is not initialized properly");
510 return kFALSE;
511 }
512
513 if (fReadOnly) {
514 AliError("AliCDBDump storage is read only!");
515 return kFALSE;
516 }
517
518 AliCDBId& id = entry->GetId();
519
520 if (!gDirectory->cd(id.GetPath())) {
521 if (!MkDir(id.GetPath())) {
522 AliError(Form("Can't open directory <%s>!",
523 id.GetPath().Data()));
524 return kFALSE;
525 }
526 }
527
528 // set version and subVersion for the entry to be stored
529 if (!PrepareId(id)) {
530 return kFALSE;
531 }
532
533 // build keyname from entry's id
534 TString keyname;
535 if (!IdToKeyName(id.GetAliCDBRunRange(), id.GetVersion(), id.GetSubVersion(), keyname)) {
536 AliError("Invalid ID encountered! Subnormal error!");
537 return kFALSE;
538 }
539
540 // write object (key name: Run#firstRun_#lastRun_v#version_s#subVersion)
541 Bool_t result = gDirectory->WriteTObject(entry, keyname);
542 if (!result) {
543 AliError(Form("Can't write entry to file: %s",
544 fFile->GetName()));
545 }
546
547 if(result) {
548 AliInfo(Form("AliCDBEntry stored into file %s",fFile->GetName()));
549 AliInfo(Form("TDirectory/key name: %s/%s",id.GetPath().Data(),keyname.Data()));
550 }
551
552 return result;
553}
554
555/////////////////////////////////////////////////////////////////////////////////////////////////
556// //
557// AliCDBDump factory //
558// //
559/////////////////////////////////////////////////////////////////////////////////////////////////
560
561ClassImp(AliCDBDumpFactory)
562
563//_____________________________________________________________________________
564Bool_t AliCDBDumpFactory::Validate(const char* dbString) {
565// check if the string is valid dump URI
566
567 TRegexp dbPattern("^dump://.+$");
568
569 return TString(dbString).Contains(dbPattern);
570}
571
572//_____________________________________________________________________________
573AliCDBParam* AliCDBDumpFactory::CreateParameter(const char* dbString) {
574// create AliCDBDumpParam class from the URI string
575
576 if (!Validate(dbString)) {
577 return NULL;
578 }
579
580 TString pathname(dbString + sizeof("dump://") - 1);
581
582 Bool_t readOnly;
583
584 if (pathname.Contains(TRegexp(";ReadOnly$"))) {
585 pathname.Resize(pathname.Length() - sizeof(";ReadOnly") + 1);
586 readOnly = kTRUE;
587 } else {
588 readOnly = kFALSE;
589 }
590
591 gSystem->ExpandPathName(pathname);
592
593 if (pathname[0] != '/') {
594 pathname.Prepend(TString(gSystem->WorkingDirectory()) + '/');
595 }
596
597 AliInfo(pathname);
598
599 return new AliCDBDumpParam(pathname, readOnly);
600}
601
602//_____________________________________________________________________________
603AliCDBStorage* AliCDBDumpFactory::Create(const AliCDBParam* param) {
604// create AliCDBDump storage instance from parameters
605
606 if (AliCDBDumpParam::Class() == param->IsA()) {
607
608 const AliCDBDumpParam* dumpParam =
609 (const AliCDBDumpParam*) param;
610
611 return new AliCDBDump(dumpParam->GetPath(),
612 dumpParam->IsReadOnly());
613 }
614
615 return NULL;
616}
617
618/////////////////////////////////////////////////////////////////////////////////////////////////
619// //
620// AliCDBDump parameter class //
621// //
622/////////////////////////////////////////////////////////////////////////////////////////////////
623
624ClassImp(AliCDBDumpParam)
625
626//_____________________________________________________________________________
627AliCDBDumpParam::AliCDBDumpParam() {
628// default constructor
629
630}
631
632//_____________________________________________________________________________
633AliCDBDumpParam::AliCDBDumpParam(const char* dbPath, Bool_t readOnly):
634 fDBPath(dbPath), fReadOnly(readOnly)
2c8628dd 635{
9e1ceb13 636// constructor
637
638 TString uri;
639 uri += "dump://";
640 uri += dbPath;
641
642 if (fReadOnly) {
643 uri += ";ReadOnly";
644 }
645
646 SetURI(uri);
647 SetType("dump");
648}
649
650//_____________________________________________________________________________
651AliCDBDumpParam::~AliCDBDumpParam() {
652// destructor
653
654}
655
656//_____________________________________________________________________________
657AliCDBParam* AliCDBDumpParam::CloneParam() const {
658// clone parameter
659
660 return new AliCDBDumpParam(fDBPath, fReadOnly);
661}
662
663//_____________________________________________________________________________
664ULong_t AliCDBDumpParam::Hash() const {
665// return Hash function
666
667 return fDBPath.Hash();
668}
669
670//_____________________________________________________________________________
671Bool_t AliCDBDumpParam::IsEqual(const TObject* obj) const {
672// check if this object is equal to AliCDBParam obj
673
674 if (this == obj) {
675 return kTRUE;
676 }
677
678 if (AliCDBDumpParam::Class() != obj->IsA()) {
679 return kFALSE;
680 }
681
682 AliCDBDumpParam* other = (AliCDBDumpParam*) obj;
683
684 return fDBPath == other->fDBPath;
fe913d8f 685}