1 #include "AliOADBForward.h"
12 #ifndef ALIROOT_SVN_REVISION
13 # define ALIROOT_SVN_REVISION 0
17 ClassImp(AliOADBForward)
19 ; // Do not remove - for Emacs
22 //====================================================================
24 AliOADBForward::Mode2String(ERunSelectMode mode)
27 case kDefault: return "default";
28 case kExact: return "exact";
29 case kNewest: return "newest";
30 case kNear: return "near";
31 case kOlder: return "older";
32 case kNewer: return "newer";
34 return "?"; // Should never get here
36 AliOADBForward::ERunSelectMode
37 AliOADBForward::String2Mode(const TString& str)
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;
47 AliOADBForward::ERunSelectMode
48 AliOADBForward::Int2Mode(Int_t mode)
51 case kDefault: return kDefault;
52 case kExact: return kExact;
53 case kNewest: return kNewest;
54 case kNear: return kNear;
55 case kOlder: return kOlder;
56 case kNewer: return kNewer;
58 return kDefault; // Should never get here
62 //====================================================================
63 AliOADBForward::Entry::Entry(ULong_t runNo,
86 //____________________________________________________________________
87 AliOADBForward::Entry::Entry(const Entry& o)
94 fSatellite(o.fSatellite),
104 //____________________________________________________________________
105 AliOADBForward::Entry&
106 AliOADBForward::Entry::operator=(const Entry& o)
109 // Assignment operator
111 if (this == &o) return *this;
117 fSatellite = o.fSatellite;
119 fTimestamp = o.fTimestamp;
120 fAliROOTRevision = o.fAliROOTRevision;
125 //____________________________________________________________________
127 AliOADBForward::Entry::GetTitle() const
129 TDatime d(fTimestamp);
130 return Form("%09ld, %4s, %4huGeV, %+4hd, %4s, %3s, %19s: %p (%s) by %s",
131 (fRunNo == 0xFFFFFFFF ? -1 : fRunNo),
134 fSys == 3 ? "pPb" : "?"),
135 fSNN, fField, (fMC ? "mc" : "real"),
136 (fSatellite ? "sat" : "nom"), d.AsSQLString(), fData,
137 (fData ? fData->GetName() : "?"), fAuthor.Data());
140 //____________________________________________________________________
142 AliOADBForward::Entry::Print(Option_t* /*option*/) const
144 Printf("%s", GetTitle());
146 //====================================================================
147 AliOADBForward::Table::Table(TTree* tree, Bool_t isNew, ERunSelectMode mode)
148 : fTree(tree), fEntry(0), fVerbose(false), fMode(mode), fFallBack(false)
153 Info("Table", "Making table %s (%s) with mode %s (%s)",
154 tree->GetName(), (isNew ? "new" : "old"), tree->GetTitle(),
158 fTree->Branch("e", "AliOADBForward::Entry", &fEntry);
159 fMode = String2Mode(fTree->GetTitle());
162 if (fMode <= kDefault || fMode > kNewer) {
163 fMode = String2Mode(fTree->GetTitle());
164 if (fMode == kDefault) fMode = kNear;
166 fTree->SetBranchAddress("e", &fEntry);
169 Info("", "Mode set to %d (%s)", fMode, Mode2String(fMode));
172 //____________________________________________________________________
173 AliOADBForward::Table::Table(const Table& o)
177 fVerbose(o.fVerbose),
179 fFallBack(o.fFallBack)
184 fTree->SetBranchAddress("e", &fEntry);
187 //____________________________________________________________________
188 AliOADBForward::Table::~Table()
195 //____________________________________________________________________
196 AliOADBForward::Table&
197 AliOADBForward::Table::operator=(const Table& o)
200 // Assignment operator
202 if (this == &o) return *this;
205 fVerbose = o.fVerbose;
207 if (fTree) fTree->SetBranchAddress("e", &fEntry);
212 //____________________________________________________________________
214 AliOADBForward::Table::GetTableName() const
217 // Get the table name or null
218 if (!fTree) return 0;
219 return fTree->GetName();
221 //____________________________________________________________________
223 AliOADBForward::Table::GetName() const
226 // Overload of TObject::GetName
228 if (!fTree) return TObject::GetName();
229 return GetTableName();
231 //____________________________________________________________________
233 AliOADBForward::Table::Update()
239 Error("Update", "No tree associated");
243 TFile* file = fTree->GetCurrentFile();
245 Error("Update", "No file associated with tree");
248 if (!file->IsWritable()) {
249 Error("Update", "File %s not writeable", file->GetName());
253 Int_t nBytes = file->Write();
255 return (nBytes >= 0);
257 //____________________________________________________________________
259 AliOADBForward::Table::Close()
262 // Close the connection
265 Error("Close", "No tree associated");
269 // if (fTree) delete fTree;
270 // if (fEntry) delete fEntry;
275 //____________________________________________________________________
277 AliOADBForward::Table::Query(ULong_t runNo,
288 return Query(runNo, mode, Conditions(sys, sNN, fld, mc, sat));
291 //____________________________________________________________________
293 AliOADBForward::Table::Query(ULong_t runNo,
295 const TString& q) const
298 // Run a query against the table
303 Error("Close", "No tree associated");
308 const char* smode = "latest";
310 if (mode <= kDefault || mode > kNewer) mode = fMode;
311 smode = Mode2String(mode);
314 AppendToQuery(query, Form("fRunNo == %lu", runNo));
319 AppendToQuery(query, Form("abs(fRunNo-%lu)<=%d",
320 runNo,kMaxNearDistance));
323 AppendToQuery(query, Form("fRunNo <= %lu", runNo));
326 AppendToQuery(query, Form("fRunNo >= %lu", runNo));
329 Fatal("Query", "Mode should never be 'default'");
334 if (query.IsNull()) {
335 Warning("Query", "Empty query!");
340 Printf("%s: Query is '%s'", GetName(), query.Data());
341 fTree->Draw("Entry$:fRunNo:fTimestamp", query, "goff");
342 Int_t nRows = fTree->GetSelectedRows();
343 if (nRows <= 0) return -1;
346 Printf("Query: %s (%s)\n"
347 " Entry | Run | Timestamp \n"
348 "--------+-----------+------------------------",
349 query.Data(), smode);
351 ULong_t oldRun = (mode == kNewer ? 0xFFFFFFFF : 0);
353 ULong_t oldDist = 0xFFFFFFFF;
355 for (Int_t row = 0; row < nRows; row++) {
356 Int_t ent = Int_t(fTree->GetV1()[row]);
357 ULong_t run = ULong_t(fTree->GetV2()[row]);
358 ULong_t tim = ULong_t(fTree->GetV3()[row]);
359 ULong_t dist = (run > runNo ? run - runNo : runNo - run);
363 Printf(" %6d | %9ld | %19s ", ent, run > 0x7FFFFFFF ? -1 : run,
368 case kExact: break; // Done in the draw `query'
369 case kNewest: // Fall-through
371 if (run < oldRun) continue;
374 if (run > oldRun) continue;
377 if (runNo > 0 && dist > oldDist) continue;
382 // If we get here, then we have the best run according to the
385 // Finally, check the timestamp
386 if (tim < oldTim) continue;
388 // Now update last values and set current best entry
396 Printf("Returning entry # %d", entry);
401 //____________________________________________________________________
403 AliOADBForward::Table::Insert(TObject* o,
411 const TString& author)
414 // Insert a new row in the table
417 // Check if the file is read/write
419 Info("Insert", "Inserting object %p for run=%lu, sys=%hu, sNN=%4hu, "
420 "field=%+2hd, mc=%d, sat=%d", o,runNo, sys, sNN, field, mc, sat);
423 Warning("Insert", "No tree, or not write-able");
427 // If the entry doesn't exists
428 if (!fEntry) fEntry = new Entry;
431 TString auth(author);
433 UserGroup_t* u = gSystem->GetUserInfo();
434 TInetAddress i = gSystem->GetHostByName(gSystem->HostName());
435 auth = TString::Format("%s <%s@%s>", u->fRealName.Data(),
436 u->fUser.Data(), i.GetHostName());
441 fEntry->fRunNo = runNo; // (runNo <= 0 ? 0xFFFFFFFF : runNo);
444 fEntry->fField = field;
446 fEntry->fSatellite = sat;
447 fEntry->fAliROOTRevision = (aliRev != 0 ? aliRev : ALIROOT_SVN_REVISION);
448 fEntry->fAuthor = auth;
451 fEntry->fTimestamp = now.Convert(true);
454 Int_t nBytes = fTree->Fill();
456 Warning("Insert", "Failed to insert new entry");
460 // do an Auto-save and flush-baskets now
461 fTree->AutoSave("FlushBaskets SaveSelf");
466 //____________________________________________________________________
468 AliOADBForward::Table::GetEntry(ULong_t run,
476 // Query the tree for an object. The strategy is as follows.
478 // - First query with all fields
479 // - If this returns a single entry, return that.
480 // - If not, then ignore the run number (if given)
481 // - If this returns a single entry, return that
482 // - If not, and fall-back is enabled, then
483 // - Ignore the collision energy (if given)
484 // - If this returns a single entry, return that.
485 // - If not, ignore all passed values
486 // - If this returns a single entry, return that.
487 // - Otherwise, give up and return -1
488 // - Otherwise, give up and return -1
490 // This allow us to specify default objects for a period, and for
491 // collision system, energy, and field setting.
495 Printf("run=%lu mode=%s sys=%hu sNN=%hu fld=%hd mc=%d sat=%d (fall=%d)",
496 run, Mode2String(mode), sys, sNN, fld, mc, sat, fFallBack);
497 Int_t entry = Query(run, mode, sys, sNN, fld, mc, sat);
498 if (entry < 0 && run > 0)
499 entry = Query(0, mode, sys, sNN, fld, mc, sat);
500 if (entry < 0 && fFallBack && sNN > 0) {
502 Printf("Fall-back enabled, will try without sNN=%d", sNN);
503 entry = Query(run, mode, sys, 0, fld, mc, sat);
505 if (entry < 0 && fFallBack) {
507 Printf("Fall-back enabled, will try without any fields");
508 entry = Query(0, mode, 0, 0, kInvalidField, false, false);
511 Warning("Get", "No valid object could be found");
517 //____________________________________________________________________
518 AliOADBForward::Entry*
519 AliOADBForward::Table::Get(ULong_t run,
527 // Query the tree for an object. The strategy is as follows.
529 // - First query with all fields
530 // - If this returns a single entry, return that.
531 // - If not, then ignore the run number (if given)
532 // - If this returns a single entry, return that
533 // - If not, and fall-back is enabled, then
534 // - Ignore the collision energy (if given)
535 // - If this returns a single entry, return that.
536 // - If not, ignore all passed values
537 // - If this returns a single entry, return that.
538 // - Otherwise, give up and return null
539 // - Otherwise, give up and return null
541 // This allow us to specify default objects for a period, and for
542 // collision system, energy, and field setting.
544 Int_t entry = GetEntry(run, mode, sys, sNN, fld, mc, sat);
545 if (entry < 0) return 0;
547 Int_t nBytes = fTree->GetEntry(entry);
549 Warning("Get", "Failed to get entry # %d\n", entry);
552 if (fVerbose) fEntry->Print();
555 //____________________________________________________________________
557 AliOADBForward::Table::GetData(ULong_t run,
566 // Get data associated with a row or null.
567 // See also AliOADBForward::Get
569 Entry* e = Get(run, mode, sys, sNN, fld, mc, sat);
573 //____________________________________________________________________
575 AliOADBForward::Table::Print(Option_t* option) const
578 // Print the full table
580 if (!IsOpen()) return;
582 Printf("Table %s (default mode: %s)", GetName(), Mode2String(fMode));
583 Int_t n = fTree->GetEntries();
584 for (Int_t i = 0; i < n; i++) {
586 printf("%4d/%4d: ", i, n);
587 fEntry->Print(option);
590 //____________________________________________________________________
592 AliOADBForward::Table::Browse(TBrowser* b)
595 if (fTree) b->Add(fTree);
597 //____________________________________________________________________
599 AliOADBForward::Table::IsOpen(Bool_t rw) const
601 if (!fTree) return false;
602 if (!rw) return true;
604 return fTree->GetCurrentFile()->IsWritable();
606 //====================================================================
607 AliOADBForward::AliOADBForward()
616 //____________________________________________________________________
617 AliOADBForward::AliOADBForward(const AliOADBForward& other)
619 fTables(other.fTables)
626 //____________________________________________________________________
627 AliOADBForward::~AliOADBForward()
635 //____________________________________________________________________
637 AliOADBForward::operator=(const AliOADBForward& other)
642 if (&other == this) return *this;
644 fTables = other.fTables;
650 //____________________________________________________________________
652 AliOADBForward::Open(const TString& fileName,
653 const TString& tables,
658 TString absPath(gSystem->ExpandPathName(fileName));
659 if (absPath.IsNull()) {
660 Error("Open", "Empty path for tables %s", tables.Data());
663 TObject* previous = gROOT->GetListOfFiles()->FindObject(absPath);
666 file = static_cast<TFile*>(previous);
669 file = TFile::Open(fileName, (rw ? "UPDATE" : "READ"));
672 Error("Open", "Failed to open %s", GetName());
675 return Open(file, tables, rw, verb, fallback);
678 //____________________________________________________________________
680 AliOADBForward::Open(TFile* file,
681 const TString& tables,
687 // Open database file and find or create listed tables
689 if (!file) return false;
690 if (rw && !file->IsWritable()) {
691 Warning("Open", "Read+write access requested, but %s opened read-only",
693 if (file->ReOpen("UPDATE") < 0) {
694 Error("Open", "Failed to reopen file in read+write access mode");
699 if (tables.EqualTo("*")) {
701 Error("Open", "Cannot open with '*' in read/write mode");
704 TList* l = file->GetListOfKeys();
707 while ((key = static_cast<TKey*>(next()))) {
708 TClass* cl = gROOT->GetClass(key->GetClassName());
710 if (!cl->InheritsFrom(TTree::Class())) continue;
712 OpenTable(file, rw, key->GetName(), "DEFAULT", verb, fallback);
717 TObjArray* tokens = tables.Tokenize(":,");
718 TObjString* token = 0;
719 TIter nextToken(tokens);
720 while ((token = static_cast<TObjString*>(nextToken()))) {
721 TString& tn = token->String();
722 if (tn.IsNull()) continue;
724 TObjArray* parts = tn.Tokenize("/");
725 TObjString* onam = static_cast<TObjString*>(parts->At(0));
726 TString& name = onam->String();
727 TString mode = "DEFAULT";
728 if (parts->GetEntries() > 1)
729 mode = static_cast<TObjString*>(parts->At(1))->String();
732 OpenTable(file, rw, name, mode, verb, fallback);
741 //____________________________________________________________________
743 AliOADBForward::Close()
746 // Flush all tables and close all files
749 Int_t nFiles = GetFiles(files);
755 TIter nextFile(&files);
757 while ((file = static_cast<TFile*>(nextFile()))) {
758 // if (file->IsWritable()) file->Write();
767 //____________________________________________________________________
769 AliOADBForward::Update()
775 Int_t nFiles = GetFiles(files);
781 TIter nextFile(&files);
784 while ((file = static_cast<TFile*>(nextFile()))) {
785 if (!file->IsWritable()) {
786 Error("Update", "File %s not writeable", file->GetName());
790 nBytes += file->Write();
792 return (nBytes >= 0);
794 //____________________________________________________________________
795 AliOADBForward::Entry*
796 AliOADBForward::Get(const TString& table,
806 // Get a row from selected table
808 Table* t = FindTable(table);
811 return t->Get(run, mode, sys, sNN, fld, mc, sat);
813 //____________________________________________________________________
815 AliOADBForward::GetData(const TString& table,
824 Table* t = FindTable(table);
827 return t->GetData(run, mode, sys, sNN, fld, mc, sat);
829 //____________________________________________________________________
831 AliOADBForward::Insert(const TString& table,
840 const TString& author)
843 // Insert a row into the selected table
845 Table* t = FindTable(table);
846 if (!t) return false;
848 return t->Insert(o, runNo, sys, sNN, field, mc, sat, aliRev, author);
850 //____________________________________________________________________
852 AliOADBForward::CopyEntry(const TString& table,
864 Table* t = FindTable(table);
865 if (!t) return false;
867 Entry* e = t->Get(oldRunNo, t->fMode, oldSys, oldSNN, oldField, mc, sat);
868 if (!e) return false;
870 return t->Insert(e->fData, newRunNo, newSys, newSNN, newField, mc, sat,
871 e->fAliROOTRevision, e->fAuthor);
874 //____________________________________________________________________
876 AliOADBForward::Print(const Option_t* option) const
881 TIter nextTable(&fTables);
884 while ((key = static_cast<TObjString*>(nextTable()))) {
885 Printf("Table: %p", key->GetName());
886 table = static_cast<Table*>(fTables.GetValue(key));
887 if (!table) continue;
888 table->Print(option);
892 //____________________________________________________________________
894 AliOADBForward::Browse(TBrowser* b)
897 // Browse this object
899 TIter nextTable(&fTables);
902 while ((key = static_cast<TObjString*>(nextTable()))) {
903 table = static_cast<Table*>(fTables.GetValue(key));
904 if (!table) continue;
905 b->Add(table, key->GetName());
908 //____________________________________________________________________
909 AliOADBForward::Table*
910 AliOADBForward::FindTable(const TString& name, Bool_t quite) const
913 // Find a table by name
915 TPair* p = static_cast<TPair*>(fTables.FindObject(name));
918 Warning("FindTable", "Table %s not registered", name.Data());
921 return static_cast<Table*>(p->Value());
923 //____________________________________________________________________
925 AliOADBForward::GetFiles(TList& files) const
928 // Get all associated files
931 TIter nextTable(&fTables);
934 while ((key = static_cast<TObjString*>(nextTable()))) {
935 table = static_cast<Table*>(fTables.GetValue(key));
936 if (!table->fTree) continue;
938 TFile* f = table->fTree->GetCurrentFile();
941 if (files.FindObject(f)) continue;
947 //____________________________________________________________________
948 AliOADBForward::Table*
949 AliOADBForward::GetTableFromFile(TFile* file,
952 const TString& mode) const
955 // Get a table from a file, or make a new table
958 if (FindTable(name, true)) return 0;
960 TObject* o = file->Get(name);
965 // We only fail if in read-only mode
966 Error("Open", "No such object: %s in %s", name.Data(),
970 // Create the tree in the file
971 t = new TTree(name, mode);
972 t->SetDirectory(file);
976 // Get the tree, and set the branch
977 t = static_cast<TTree*>(o);
979 Table* ret = new Table(t, n, String2Mode(mode));
983 //____________________________________________________________________
985 AliOADBForward::OpenTable(TFile* file,
994 Table* t = GetTableFromFile(file, rw, name, mode);
997 fTables.Add(new TObjString(name), t);
999 t->SetEnableFallBack(fallback);
1001 Printf("Found table %s. Opened with verbose=%d, fallback=%d",
1002 name.Data(), verb, fallback);
1005 //____________________________________________________________________
1007 AliOADBForward::AppendToQuery(TString& q, const TString& s, Bool_t andNotOr)
1012 if (!q.IsNull()) q.Append(andNotOr ? " && " : " || ");
1015 //____________________________________________________________________
1017 AliOADBForward::Conditions(UShort_t sys,
1023 // Build query string
1025 if (sys > 0) AppendToQuery(q, Form("fSys == %hu", sys));
1026 if (sNN > 0) AppendToQuery(q, Form("abs(fSNN - %hu) < 11", sNN));
1027 if (TMath::Abs(fld) < 10) AppendToQuery(q, Form("fField == %hd",fld));
1028 // Boolean fields always queried. In cases where these do not matter,
1029 // we always write down the false value, so we get the correct query
1031 AppendToQuery(q, Form("%sfMC", (mc ? " " : "!")));
1032 AppendToQuery(q, Form("%sfSatellite", (sat ? " " : "!")));
1037 //____________________________________________________________________
1039 AliOADBForward::TestGet(AliOADBForward& t,
1040 const TString& table,
1042 ERunSelectMode mode,
1050 Printf("=== Test query: t=%s r=%ld s=%d t=%d f=%d m=%d v=%d",
1051 table.Data(), runNo, sys, sNN, fld, int(mc), int(sat));
1052 AliOADBForward::Entry* e = t.Get(table, runNo, mode, sys, sNN,
1057 //____________________________________________________________________
1059 AliOADBForward::TestInsert(AliOADBForward& t,
1060 const TString& table,
1068 static Int_t cnt = 0;
1069 TString what = TString::Format("%s-%03d", table.Data(), cnt++);
1070 Printf("=== Insert: t=%s r=%ld s=%d t=%d f=%d m=%d v=%d w=%s",
1071 table.Data(), runNo, sys, sNN, fld, int(mc), int(sat), what.Data());
1072 t.Insert(table, new TObjString(what), runNo, sys, sNN, fld, mc, sat);
1073 gSystem->Sleep(500);
1076 //____________________________________________________________________
1078 AliOADBForward::Test()
1080 AliOADBForward* tt = new AliOADBForward();
1081 if (!tt->Open("db.root", "A,B", true, true)) {
1082 ::Error("Test", "Failed to open DB");
1085 AliOADBForward& t = *tt;
1086 TestInsert(t, "A", 137161);
1087 TestInsert(t, "A", 137161);
1088 TestInsert(t, "A", 0 );
1089 TestInsert(t, "A", 999999);
1090 TestInsert(t, "A", 137166);
1093 TestInsert(t, "B", 137161);
1094 TestInsert(t, "B", 0 );
1098 if (!t.Open("db.root", "A,B",false,true)) {
1099 ::Error("Test", "Failed to open DB");
1103 TestGet(t, "A", 137161);
1104 TestGet(t, "A", 137160);
1105 TestGet(t, "A", 0 );
1106 TestGet(t, "A", 137160, kNewest);
1107 TestGet(t, "A", 137160, kNewer);
1108 TestGet(t, "A", 137168, kOlder);
1109 TestGet(t, "A", 137161, kExact);
1111 new TBrowser("b", tt);