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),
135 fSys == 4 ?
"Pbp" :
"?"),
136 fSNN, fField, (fMC ?
"mc" :
"real"),
137 (fSatellite ?
"sat" :
"nom"), d.AsSQLString(), fData,
138 (fData ? fData->GetName() :
"?"), fAuthor.Data());
145 Printf(
"%s", GetTitle());
149 : fTree(tree), fEntry(0), fVerbose(false), fMode(mode), fFallBack(false)
154 Info(
"Table",
"Making table %s (%s) with mode %s (%s)",
155 tree->GetName(), (isNew ?
"new" :
"old"), tree->GetTitle(),
159 fTree->Branch(
"e",
"AliOADBForward::Entry", &
fEntry);
163 if (fMode <= kDefault || fMode >
kNewer) {
178 fVerbose(o.fVerbose),
180 fFallBack(o.fFallBack)
203 if (
this == &o)
return *
this;
208 if (fTree) fTree->SetBranchAddress(
"e", &fEntry);
219 if (!fTree)
return 0;
220 return fTree->GetName();
229 if (!fTree)
return TObject::GetName();
230 return GetTableName();
240 Error(
"Update",
"No tree associated");
244 TFile*
file = fTree->GetCurrentFile();
246 Error(
"Update",
"No file associated with tree");
249 if (!file->IsWritable()) {
250 Error(
"Update",
"File %s not writeable", file->GetName());
254 Int_t nBytes = file->Write();
256 return (nBytes >= 0);
266 Error(
"Close",
"No tree associated");
289 return Query(runNo, mode,
Conditions(sys, sNN, fld, mc, sat));
304 Error(
"Close",
"No tree associated");
309 const char* smode =
"latest";
311 if (mode <= kDefault || mode >
kNewer) mode = fMode;
330 Fatal(
"Query",
"Mode should never be 'default'");
335 if (query.IsNull()) {
336 Warning(
"Query",
"Empty query!");
341 Printf(
"%s: Query is '%s'", GetName(), query.Data());
342 fTree->Draw(
"Entry$:fRunNo:fTimestamp", query,
"goff");
343 Int_t nRows = fTree->GetSelectedRows();
344 if (nRows <= 0)
return -1;
347 Printf(
"Query: %s (%s)\n"
348 " Entry | Run | Timestamp \n"
349 "--------+-----------+------------------------",
350 query.Data(), smode);
357 for (
Int_t row = 0; row < nRows; row++) {
361 ULong_t dist = (run > runNo ? run - runNo : runNo - run);
365 Printf(
" %6d | %9ld | %19s [dist: %9ld vs %9ld, %5s]",
366 ent, run > 0x7FFFFFFF ? -1 : run, t.AsSQLString(),
367 dist, oldDist, (tim<oldTim?
"older":
"newer"));
374 if (run < oldRun)
continue;
377 if (run > oldRun)
continue;
380 if (runNo > 0 && dist > oldDist)
continue;
395 if (tim < oldTim)
continue;
405 Printf(
"Returning entry # %d", entry);
428 Info(
"Insert",
"Inserting object %p for run=%lu, sys=%hu, sNN=%4hu, "
429 "field=%+2hd, mc=%d, sat=%d", o,runNo, sys, sNN, field, mc, sat);
432 Warning(
"Insert",
"No tree, or not write-able");
437 if (!fEntry) fEntry =
new Entry;
442 UserGroup_t* u =
gSystem->GetUserInfo();
444 auth = TString::Format(
"%s <%s@%s>", u->fRealName.Data(),
445 u->fUser.Data(), i.GetHostName());
450 fEntry->fRunNo = runNo;
453 fEntry->fField = field;
455 fEntry->fSatellite = sat;
457 fEntry->fAuthor = auth;
460 fEntry->fTimestamp = now.Convert(
true);
463 Int_t nBytes = fTree->Fill();
465 Warning(
"Insert",
"Failed to insert new entry");
470 fTree->AutoSave(
"FlushBaskets SaveSelf");
504 Printf(
"run=%lu mode=%s sys=%hu sNN=%hu fld=%hd mc=%d sat=%d (fall=%d)",
505 run,
Mode2String(mode), sys, sNN, fld, mc, sat, fFallBack);
506 Int_t entry = Query(run, mode, sys, sNN, fld, mc, sat);
507 if (entry < 0 && run > 0)
508 entry = Query(0, mode, sys, sNN, fld, mc, sat);
511 Printf(
"Fall-back enabled, trying without field=%d", fld);
512 entry = Query(run, mode, sys, sNN, fld, mc, sat);
514 if (entry < 0 && fFallBack && sNN > 0) {
516 Printf(
"Fall-back enabled, will try without sNN=%d", sNN);
517 entry = Query(run, mode, sys, 0, fld, mc, sat);
519 if (entry < 0 && fFallBack) {
521 Printf(
"Fall-back enabled, will try without any fields");
525 Warning(
"Get",
"No valid object could be found");
558 Int_t entry = GetEntry(run, mode, sys, sNN, fld, mc, sat);
559 if (entry < 0)
return 0;
561 Int_t nBytes = fTree->GetEntry(entry);
563 Warning(
"Get",
"Failed to get entry # %d\n", entry);
566 if (fVerbose) fEntry->Print();
583 Entry* e =
Get(run, mode, sys, sNN, fld, mc, sat);
594 if (!IsOpen())
return;
596 Printf(
"Table %s (default mode: %s)", GetName(),
Mode2String(fMode));
597 Int_t n = fTree->GetEntries();
598 for (
Int_t i = 0; i < n; i++) {
600 printf(
"%4d/%4d: ", i, n);
601 fEntry->Print(option);
609 if (fTree) b->Add(fTree);
615 if (!fTree)
return false;
616 if (!rw)
return true;
618 return fTree->GetCurrentFile()->IsWritable();
633 fTables(other.fTables)
656 if (&other ==
this)
return *
this;
673 if (absPath.IsNull()) {
674 Error(
"Open",
"Empty path for tables %s", tables.Data());
677 TObject* previous = gROOT->GetListOfFiles()->FindObject(absPath);
680 file =
static_cast<TFile*
>(previous);
683 file = TFile::Open(fileName, (rw ?
"UPDATE" :
"READ"));
686 Error(
"Open",
"Failed to open %s", GetName());
689 return Open(file, tables, rw, verb, fallback);
703 if (!file)
return false;
704 if (rw && !file->IsWritable()) {
705 Warning(
"Open",
"Read+write access requested, but %s opened read-only",
707 if (file->ReOpen(
"UPDATE") < 0) {
708 Error(
"Open",
"Failed to reopen file in read+write access mode");
713 if (tables.EqualTo(
"*")) {
715 Error(
"Open",
"Cannot open with '*' in read/write mode");
718 TList* l = file->GetListOfKeys();
721 while ((key = static_cast<TKey*>(next()))) {
722 TClass* cl = gROOT->GetClass(key->GetClassName());
724 if (!cl->InheritsFrom(TTree::Class()))
continue;
726 OpenTable(file, rw, key->GetName(),
"DEFAULT", verb, fallback);
731 TObjArray* tokens = tables.Tokenize(
":,");
732 TObjString* token = 0;
733 TIter nextToken(tokens);
734 while ((token = static_cast<TObjString*>(nextToken()))) {
736 if (tn.IsNull())
continue;
739 TObjString* onam =
static_cast<TObjString*
>(parts->At(0));
740 TString& name = onam->String();
742 if (parts->GetEntries() > 1)
743 mode = static_cast<TObjString*>(parts->At(1))->String();
746 OpenTable(file, rw, name, mode, verb, fallback);
769 TIter nextFile(&files);
771 while ((file = static_cast<TFile*>(nextFile()))) {
795 TIter nextFile(&files);
798 while ((file = static_cast<TFile*>(nextFile()))) {
799 if (!file->IsWritable()) {
800 Error(
"Update",
"File %s not writeable", file->GetName());
804 nBytes += file->Write();
806 return (nBytes >= 0);
825 return t->
Get(run, mode, sys, sNN, fld, mc, sat);
841 return t->
GetData(run, mode, sys, sNN, fld, mc, sat);
860 if (!t)
return false;
862 return t->
Insert(o, runNo, sys, sNN, field, mc, sat, aliRev, author);
879 if (!t)
return false;
882 Entry* e = t->
Get(oldRunNo, t->
fMode, oldSys, oldSNN, oldField, mc, sat);
883 if (!e)
return false;
885 return t->
Insert(e->
fData, newRunNo, newSys, newSNN, newField, mc, sat,
899 while ((key = static_cast<TObjString*>(nextTable()))) {
900 Printf(
"Table: %p", key->GetName());
902 if (!table)
continue;
903 table->
Print(option);
917 while ((key = static_cast<TObjString*>(nextTable()))) {
919 if (!table)
continue;
930 TPair* p =
static_cast<TPair*
>(
fTables.FindObject(name));
933 Warning(
"FindTable",
"Table %s not registered", name.Data());
936 return static_cast<Table*
>(p->Value());
949 while ((key = static_cast<TObjString*>(nextTable()))) {
951 if (!table->
fTree)
continue;
953 TFile* f = table->
fTree->GetCurrentFile();
956 if (files.FindObject(f))
continue;
981 Error(
"Open",
"No such object: %s in %s", name.Data(),
985 TDirectory* savDir = gDirectory;
988 t =
new TTree(name, mode);
989 t->SetDirectory(file);
990 if (savDir) savDir->cd();
995 t =
static_cast<TTree*
>(o);
1015 fTables.Add(
new TObjString(name), t);
1019 Printf(
"Found table %s. Opened with verbose=%d, fallback=%d",
1020 name.Data(), verb, fallback);
1030 if (!q.IsNull()) q.Append(andNotOr ?
" && " :
" || ");
1044 if (sNN > 0)
AppendToQuery(q, Form(
"abs(fSNN - %hu) < 11", sNN));
1045 if (TMath::Abs(fld) < 10)
AppendToQuery(q, Form(
"fField == %hd",fld));
1068 Printf(
"=== Test query: t=%s r=%ld s=%d t=%d f=%d m=%d v=%d",
1069 table.Data(), runNo, sys, sNN, fld, int(mc), int(sat));
1086 static Int_t cnt = 0;
1087 TString what = TString::Format(
"%s-%03d", table.Data(), cnt++);
1088 Printf(
"=== Insert: t=%s r=%ld s=%d t=%d f=%d m=%d v=%d w=%s",
1089 table.Data(), runNo, sys, sNN, fld, int(mc), int(sat), what.Data());
1090 t.
Insert(table,
new TObjString(what), runNo, sys, sNN, fld, mc, sat);
1099 if (!tt->
Open(
"db.root",
"A,B",
true,
true)) {
1100 ::Error(
"Test",
"Failed to open DB");
1116 if (!t.
Open(
"db.root",
"A,B",
false,
true)) {
1117 ::Error(
"Test",
"Failed to open DB");
1129 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