12 #ifndef ALIROOT_SVN_REVISION
13 # define ALIROOT_SVN_REVISION 0
28 case kExact:
return "exact";
30 case kNear:
return "near";
31 case kOlder:
return "older";
32 case kNewer:
return "newer";
39 if (str.EqualTo(
"default", TString::kIgnoreCase))
return kDefault;
40 else if (str.EqualTo(
"exact", TString::kIgnoreCase))
return kExact;
41 else if (str.EqualTo(
"newest", TString::kIgnoreCase))
return kNewest;
42 else if (str.EqualTo(
"near", TString::kIgnoreCase))
return kNear;
43 else if (str.EqualTo(
"older", TString::kIgnoreCase))
return kOlder;
44 else if (str.EqualTo(
"newer", TString::kIgnoreCase))
return kNewer;
94 fSatellite(o.fSatellite),
111 if (
this == &o)
return *
this;
129 TDatime d(fTimestamp);
130 return Form(
"%09ld, %4s, %4huGeV, %+4hd, %4s, %3s, %19s: %p (%s) by %s",
131 (fRunNo == 0xFFFFFFFF ? -1 : fRunNo),
136 fSys == 5 ?
"XeXe" :
"?"),
137 fSNN, fField, (fMC ?
"mc" :
"real"),
138 (fSatellite ?
"sat" :
"nom"), d.AsSQLString(), fData,
139 (fData ? fData->GetName() :
"?"), fAuthor.Data());
146 Printf(
"%s", GetTitle());
150 : fTree(tree), fEntry(0), fVerbose(false), fMode(mode), fFallBack(false)
155 Info(
"Table",
"Making table %s (%s) with mode %s (%s)",
156 tree->GetName(), (isNew ?
"new" :
"old"), tree->GetTitle(),
160 fTree->Branch(
"e",
"AliOADBForward::Entry", &
fEntry);
164 if (fMode <= kDefault || fMode >
kNewer) {
179 fVerbose(o.fVerbose),
181 fFallBack(o.fFallBack)
204 if (
this == &o)
return *
this;
209 if (fTree) fTree->SetBranchAddress(
"e", &fEntry);
220 if (!fTree)
return 0;
221 return fTree->GetName();
230 if (!fTree)
return TObject::GetName();
231 return GetTableName();
241 Error(
"Update",
"No tree associated");
245 TFile*
file = fTree->GetCurrentFile();
247 Error(
"Update",
"No file associated with tree");
250 if (!file->IsWritable()) {
251 Error(
"Update",
"File %s not writeable", file->GetName());
255 Int_t nBytes = file->Write();
257 return (nBytes >= 0);
267 Error(
"Close",
"No tree associated");
290 return Query(runNo, mode,
Conditions(sys, sNN, fld, mc, sat));
305 Error(
"Close",
"No tree associated");
310 const char* smode =
"latest";
312 if (mode <= kDefault || mode >
kNewer) mode = fMode;
331 Fatal(
"Query",
"Mode should never be 'default'");
336 if (query.IsNull()) {
337 Warning(
"Query",
"Empty query!");
342 Printf(
"%s: Query is '%s'", GetName(), query.Data());
343 fTree->Draw(
"Entry$:fRunNo:fTimestamp", query,
"goff");
344 Int_t nRows = fTree->GetSelectedRows();
345 if (nRows <= 0)
return -1;
348 Printf(
"Query: %s (%s)\n"
349 " Entry | Run | Timestamp \n"
350 "--------+-----------+------------------------",
351 query.Data(), smode);
358 for (
Int_t row = 0; row < nRows; row++) {
362 ULong_t dist = (run > runNo ? run - runNo : runNo - run);
366 Printf(
" %6d | %9ld | %19s [dist: %9ld vs %9ld, %5s]",
367 ent, run > 0x7FFFFFFF ? -1 : run, t.AsSQLString(),
368 dist, oldDist, (tim<oldTim?
"older":
"newer"));
375 if (run < oldRun)
continue;
378 if (run > oldRun)
continue;
381 if (runNo > 0 && dist > oldDist)
continue;
396 if (tim < oldTim)
continue;
406 Printf(
"Returning entry # %d", entry);
429 Info(
"Insert",
"Inserting object %p for run=%lu, sys=%hu, sNN=%4hu, "
430 "field=%+2hd, mc=%d, sat=%d", o,runNo, sys, sNN, field, mc, sat);
433 Warning(
"Insert",
"No tree, or not write-able");
438 if (!fEntry) fEntry =
new Entry;
443 UserGroup_t* u =
gSystem->GetUserInfo();
445 auth = TString::Format(
"%s <%s@%s>", u->fRealName.Data(),
446 u->fUser.Data(), i.GetHostName());
451 fEntry->fRunNo = runNo;
454 fEntry->fField = field;
456 fEntry->fSatellite = sat;
458 fEntry->fAuthor = auth;
461 fEntry->fTimestamp = now.Convert(
true);
464 Int_t nBytes = fTree->Fill();
466 Warning(
"Insert",
"Failed to insert new entry");
471 fTree->AutoSave(
"FlushBaskets SaveSelf");
505 Printf(
"run=%lu mode=%s sys=%hu sNN=%hu fld=%hd mc=%d sat=%d (fall=%d)",
506 run,
Mode2String(mode), sys, sNN, fld, mc, sat, fFallBack);
507 Int_t entry = Query(run, mode, sys, sNN, fld, mc, sat);
508 if (entry < 0 && run > 0)
509 entry = Query(0, mode, sys, sNN, fld, mc, sat);
512 Printf(
"Fall-back enabled, trying without field=%d", fld);
513 entry = Query(run, mode, sys, sNN, fld, mc, sat);
515 if (entry < 0 && fFallBack && sNN > 0) {
517 Printf(
"Fall-back enabled, will try without sNN=%d", sNN);
518 entry = Query(run, mode, sys, 0, fld, mc, sat);
520 if (entry < 0 && fFallBack) {
522 Printf(
"Fall-back enabled, will try without any fields");
526 Warning(
"Get",
"No valid object could be found");
559 Int_t entry = GetEntry(run, mode, sys, sNN, fld, mc, sat);
560 if (entry < 0)
return 0;
562 Int_t nBytes = fTree->GetEntry(entry);
564 Warning(
"Get",
"Failed to get entry # %d\n", entry);
567 if (fVerbose) fEntry->Print();
584 Entry* e =
Get(run, mode, sys, sNN, fld, mc, sat);
595 if (!IsOpen())
return;
597 Printf(
"Table %s (default mode: %s)", GetName(),
Mode2String(fMode));
598 Int_t n = fTree->GetEntries();
599 for (
Int_t i = 0; i < n; i++) {
601 printf(
"%4d/%4d: ", i, n);
602 fEntry->Print(option);
610 if (fTree) b->Add(fTree);
616 if (!fTree)
return false;
617 if (!rw)
return true;
619 return fTree->GetCurrentFile()->IsWritable();
634 fTables(other.fTables)
657 if (&other ==
this)
return *
this;
674 if (absPath.IsNull()) {
675 Error(
"Open",
"Empty path for tables %s", tables.Data());
678 TObject* previous = gROOT->GetListOfFiles()->FindObject(absPath);
681 file =
static_cast<TFile*
>(previous);
684 file = TFile::Open(fileName, (rw ?
"UPDATE" :
"READ"));
687 Error(
"Open",
"Failed to open %s", GetName());
690 return Open(file, tables, rw, verb, fallback);
704 if (!file)
return false;
705 if (rw && !file->IsWritable()) {
706 Warning(
"Open",
"Read+write access requested, but %s opened read-only",
708 if (file->ReOpen(
"UPDATE") < 0) {
709 Error(
"Open",
"Failed to reopen file in read+write access mode");
714 if (tables.EqualTo(
"*")) {
716 Error(
"Open",
"Cannot open with '*' in read/write mode");
719 TList* l = file->GetListOfKeys();
722 while ((key = static_cast<TKey*>(next()))) {
723 TClass* cl = gROOT->GetClass(key->GetClassName());
725 if (!cl->InheritsFrom(TTree::Class()))
continue;
727 OpenTable(file, rw, key->GetName(),
"DEFAULT", verb, fallback);
732 TObjArray* tokens = tables.Tokenize(
":,");
733 TObjString* token = 0;
734 TIter nextToken(tokens);
735 while ((token = static_cast<TObjString*>(nextToken()))) {
737 if (tn.IsNull())
continue;
740 TObjString* onam =
static_cast<TObjString*
>(parts->At(0));
741 TString& name = onam->String();
743 if (parts->GetEntries() > 1)
744 mode = static_cast<TObjString*>(parts->At(1))->String();
747 OpenTable(file, rw, name, mode, verb, fallback);
770 TIter nextFile(&files);
772 while ((file = static_cast<TFile*>(nextFile()))) {
796 TIter nextFile(&files);
799 while ((file = static_cast<TFile*>(nextFile()))) {
800 if (!file->IsWritable()) {
801 Error(
"Update",
"File %s not writeable", file->GetName());
805 nBytes += file->Write();
807 return (nBytes >= 0);
826 return t->
Get(run, mode, sys, sNN, fld, mc, sat);
842 return t->
GetData(run, mode, sys, sNN, fld, mc, sat);
861 if (!t)
return false;
863 return t->
Insert(o, runNo, sys, sNN, field, mc, sat, aliRev, author);
880 if (!t)
return false;
883 Entry* e = t->
Get(oldRunNo, t->
fMode, oldSys, oldSNN, oldField, mc, sat);
884 if (!e)
return false;
886 return t->
Insert(e->
fData, newRunNo, newSys, newSNN, newField, mc, sat,
900 while ((key = static_cast<TObjString*>(nextTable()))) {
901 Printf(
"Table: %p", key->GetName());
903 if (!table)
continue;
904 table->
Print(option);
918 while ((key = static_cast<TObjString*>(nextTable()))) {
920 if (!table)
continue;
931 TPair* p =
static_cast<TPair*
>(
fTables.FindObject(name));
934 Warning(
"FindTable",
"Table %s not registered", name.Data());
937 return static_cast<Table*
>(p->Value());
950 while ((key = static_cast<TObjString*>(nextTable()))) {
952 if (!table->
fTree)
continue;
954 TFile* f = table->
fTree->GetCurrentFile();
957 if (files.FindObject(f))
continue;
982 Error(
"Open",
"No such object: %s in %s", name.Data(),
986 TDirectory* savDir = gDirectory;
989 t =
new TTree(name, mode);
990 t->SetDirectory(file);
991 if (savDir) savDir->cd();
996 t =
static_cast<TTree*
>(o);
1016 fTables.Add(
new TObjString(name), t);
1020 Printf(
"Found table %s. Opened with verbose=%d, fallback=%d",
1021 name.Data(), verb, fallback);
1031 if (!q.IsNull()) q.Append(andNotOr ?
" && " :
" || ");
1045 if (sNN > 0)
AppendToQuery(q, Form(
"abs(fSNN - %hu) < 11", sNN));
1046 if (TMath::Abs(fld) < 10)
AppendToQuery(q, Form(
"fField == %hd",fld));
1069 Printf(
"=== Test query: t=%s r=%ld s=%d t=%d f=%d m=%d v=%d",
1070 table.Data(), runNo, sys, sNN, fld, int(mc), int(sat));
1087 static Int_t cnt = 0;
1088 TString what = TString::Format(
"%s-%03d", table.Data(), cnt++);
1089 Printf(
"=== Insert: t=%s r=%ld s=%d t=%d f=%d m=%d v=%d w=%s",
1090 table.Data(), runNo, sys, sNN, fld, int(mc), int(sat), what.Data());
1091 t.
Insert(table,
new TObjString(what), runNo, sys, sNN, fld, mc, sat);
1100 if (!tt->
Open(
"db.root",
"A,B",
true,
true)) {
1101 ::Error(
"Test",
"Failed to open DB");
1117 if (!t.
Open(
"db.root",
"A,B",
false,
true)) {
1118 ::Error(
"Test",
"Failed to open DB");
1130 new TBrowser(
"b", tt);
Entry * Get(ULong_t run=0, ERunSelectMode mode=kNear, UShort_t sys=0, UShort_t sNN=0, Short_t fld=0, Bool_t mc=false, Bool_t sat=false) const
const Char_t * GetName() const
Bool_t IsOpen(Bool_t rw=false) const
void Print(Option_t *option="") const
void OpenTable(TFile *file, Bool_t rw, const TString &name, const TString &mode, Bool_t verb, Bool_t fallback)
static void AppendToQuery(TString &q, const TString &s, Bool_t andNotOr=true)
static const char * Mode2String(ERunSelectMode mode)
static TString Conditions(UShort_t sys=0, UShort_t sNN=0, Short_t fld=kInvalidField, Bool_t mc=false, Bool_t sat=false)
Entry(ULong_t runNo=0, UShort_t sys=0, UShort_t sNN=0, Short_t field=0, Bool_t mc=false, Bool_t sat=false, TObject *o=0)
Table & operator=(const Table &other)
Table * FindTable(const TString &name, Bool_t quite=false) const
#define ALIROOT_SVN_REVISION
static void TestInsert(AliOADBForward &t, const TString &table, ULong_t runNo=0, UShort_t sys=2, UShort_t sNN=2760, Short_t fld=-5, Bool_t mc=false, Bool_t sat=false)
static ERunSelectMode String2Mode(const TString &str)
Bool_t CopyEntry(const TString &table, ULong_t oldRunNo, UShort_t oldSys, UShort_t oldSNN, Short_t oldField, ULong_t newRunNo, UShort_t newSys, UShort_t newSNN, Short_t newField, Bool_t mc, Bool_t sat)
void SetVerbose(Bool_t verb=true)
void SetEnableFallBack(Bool_t use=true)
Table(TTree *tree, Bool_t isNew, ERunSelectMode mode=kNear)
Int_t GetFiles(TList &files) const
void Print(const Option_t *option="") const
Bool_t Insert(TObject *o, ULong_t runNo, UShort_t sys, UShort_t sNN, Short_t field, Bool_t mc=false, Bool_t sat=false, ULong_t aliRev=0, const TString &author="")
const char * GetTitle() const
void Print(Option_t *option="") const
static void TestGet(AliOADBForward &t, const TString &table, ULong_t runNo=0, ERunSelectMode mode=kNear, UShort_t sys=2, UShort_t sNN=2760, Short_t fld=-5, Bool_t mc=false, Bool_t sat=false)
Entry & operator=(const Entry &o)
TFile * file
TList with histograms for a given trigger.
Bool_t Insert(const TString &table, TObject *o, ULong_t runNo, UShort_t sys, UShort_t sNN, Short_t field, Bool_t mc=false, Bool_t sat=false, ULong_t aliRev=0, const TString &author="")
Entry * Get(const TString &table, ULong_t run=0, ERunSelectMode mode=kNear, UShort_t sys=0, UShort_t sNN=0, Short_t fld=0, Bool_t mc=false, Bool_t sat=false) const
TObject * GetData(const TString &table, ULong_t run=0, ERunSelectMode mode=kNear, UShort_t sys=0, UShort_t sNN=0, Short_t fld=0, Bool_t mc=false, Bool_t sat=false) const
static ERunSelectMode Int2Mode(Int_t mode)
const Char_t * GetTableName() const
Int_t GetEntry(ULong_t run=0, ERunSelectMode mode=kNear, UShort_t sys=0, UShort_t sNN=0, Short_t fld=0, Bool_t mc=false, Bool_t sat=false) const
Bool_t Open(const TString &fileName, const TString &tables="*", Bool_t rw=false, Bool_t verb=false, Bool_t fallback=false)
Table * GetTableFromFile(TFile *file, Bool_t rw, const TString &name, const TString &mode) const
Int_t Query(ULong_t runNo=0, ERunSelectMode mode=kNear, UShort_t sys=0, UShort_t sNN=0, Short_t fld=kInvalidField, Bool_t mc=false, Bool_t sat=false) const
TObject * GetData(ULong_t run=0, ERunSelectMode mode=kNear, UShort_t sys=0, UShort_t sNN=0, Short_t fld=0, Bool_t mc=false, Bool_t sat=false) const