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 fTree->Branch("e", "AliOADBForward::Entry", &fEntry);
154 fMode = String2Mode(fTree->GetTitle());
157 if (fMode <= kDefault || fMode > kNewer) {
158 fMode = String2Mode(fTree->GetTitle());
159 if (fMode == kDefault) fMode = kNear;
161 fTree->SetBranchAddress("e", &fEntry);
164 //____________________________________________________________________
165 AliOADBForward::Table::Table(const Table& o)
169 fVerbose(o.fVerbose),
171 fFallBack(o.fFallBack)
176 fTree->SetBranchAddress("e", &fEntry);
179 //____________________________________________________________________
180 AliOADBForward::Table::~Table()
187 //____________________________________________________________________
188 AliOADBForward::Table&
189 AliOADBForward::Table::operator=(const Table& o)
192 // Assignment operator
194 if (this == &o) return *this;
197 fVerbose = o.fVerbose;
199 if (fTree) fTree->SetBranchAddress("e", &fEntry);
204 //____________________________________________________________________
206 AliOADBForward::Table::GetTableName() const
209 // Get the table name or null
210 if (!fTree) return 0;
211 return fTree->GetName();
213 //____________________________________________________________________
215 AliOADBForward::Table::GetName() const
218 // Overload of TObject::GetName
220 if (!fTree) return TObject::GetName();
221 return GetTableName();
223 //____________________________________________________________________
225 AliOADBForward::Table::Update()
231 Error("Update", "No tree associated");
235 TFile* file = fTree->GetCurrentFile();
237 Error("Update", "No file associated with tree");
240 if (!file->IsWritable()) {
241 Error("Update", "File %s not writeable", file->GetName());
245 Int_t nBytes = file->Write();
247 return (nBytes >= 0);
249 //____________________________________________________________________
251 AliOADBForward::Table::Close()
254 // Close the connection
257 Error("Close", "No tree associated");
261 // if (fTree) delete fTree;
262 // if (fEntry) delete fEntry;
267 //____________________________________________________________________
269 AliOADBForward::Table::Query(ULong_t runNo,
280 return Query(runNo, mode, Conditions(sys, sNN, fld, mc, sat));
283 //____________________________________________________________________
285 AliOADBForward::Table::Query(ULong_t runNo,
287 const TString& q) const
290 // Run a query against the table
295 Error("Close", "No tree associated");
300 const char* smode = "latest";
302 if (mode <= kDefault || mode > kNewer) mode = fMode;
303 smode = Mode2String(mode);
306 AppendToQuery(query, Form("fRunNo == %lu", runNo));
311 AppendToQuery(query, Form("abs(fRunNo-%lu)<=%d",
312 runNo,kMaxNearDistance));
315 AppendToQuery(query, Form("fRunNo <= %lu", runNo));
318 AppendToQuery(query, Form("fRunNo >= %lu", runNo));
321 Fatal("Query", "Mode should never be 'default'");
326 if (query.IsNull()) {
327 Warning("Query", "Empty query!");
332 Printf("%s: Query is '%s'", GetName(), query.Data());
333 fTree->Draw("Entry$:fRunNo:fTimestamp", query, "goff");
334 Int_t nRows = fTree->GetSelectedRows();
335 if (nRows <= 0) return -1;
338 Printf("Query: %s (%s)\n"
339 " Entry | Run | Timestamp \n"
340 "--------+-----------+------------------------",
341 query.Data(), smode);
343 ULong_t oldRun = (mode == kNewer ? 0xFFFFFFFF : 0);
345 ULong_t oldDist = 0xFFFFFFFF;
347 for (Int_t row = 0; row < nRows; row++) {
348 Int_t ent = Int_t(fTree->GetV1()[row]);
349 ULong_t run = ULong_t(fTree->GetV2()[row]);
350 ULong_t tim = ULong_t(fTree->GetV3()[row]);
351 ULong_t dist = (run > runNo ? run - runNo : runNo - run);
355 Printf(" %6d | %9ld | %19s ", ent, run > 0x7FFFFFFF ? -1 : run,
360 case kExact: break; // Done in the draw `query'
361 case kNewest: // Fall-through
363 if (run < oldRun) continue;
366 if (run > oldRun) continue;
369 if (runNo > 0 && dist > oldDist) continue;
374 // If we get here, then we have the best run according to the
377 // Finally, check the timestamp
378 if (tim < oldTim) continue;
380 // Now update last values and set current best entry
388 Printf("Returning entry # %d", entry);
393 //____________________________________________________________________
395 AliOADBForward::Table::Insert(TObject* o,
403 const TString& author)
406 // Insert a new row in the table
409 // Check if the file is read/write
411 Info("Insert", "Inserting object %p for run=%lu, sys=%hu, sNN=%4hu, "
412 "field=%+2hd, mc=%d, sat=%d", o,runNo, sys, sNN, field, mc, sat);
415 Warning("Insert", "No tree, or not write-able");
419 // If the entry doesn't exists
420 if (!fEntry) fEntry = new Entry;
423 TString auth(author);
425 UserGroup_t* u = gSystem->GetUserInfo();
426 TInetAddress i = gSystem->GetHostByName(gSystem->HostName());
427 auth = TString::Format("%s <%s@%s>", u->fRealName.Data(),
428 u->fUser.Data(), i.GetHostName());
433 fEntry->fRunNo = runNo; // (runNo <= 0 ? 0xFFFFFFFF : runNo);
436 fEntry->fField = field;
438 fEntry->fSatellite = sat;
439 fEntry->fAliROOTRevision = (aliRev != 0 ? aliRev : ALIROOT_SVN_REVISION);
440 fEntry->fAuthor = auth;
443 fEntry->fTimestamp = now.Convert(true);
446 Int_t nBytes = fTree->Fill();
448 Warning("Insert", "Failed to insert new entry");
452 // do an Auto-save and flush-baskets now
453 fTree->AutoSave("FlushBaskets SaveSelf");
458 //____________________________________________________________________
460 AliOADBForward::Table::GetEntry(ULong_t run,
468 // Query the tree for an object. The strategy is as follows.
470 // - First query with all fields
471 // - If this returns a single entry, return that.
472 // - If not, then ignore the run number (if given)
473 // - If this returns a single entry, return that
474 // - If not, and fall-back is enabled, then
475 // - Ignore the collision energy (if given)
476 // - If this returns a single entry, return that.
477 // - If not, ignore all passed values
478 // - If this returns a single entry, return that.
479 // - Otherwise, give up and return -1
480 // - Otherwise, give up and return -1
482 // This allow us to specify default objects for a period, and for
483 // collision system, energy, and field setting.
487 Printf("run=%lu mode=%s sys=%hu sNN=%hu fld=%hd mc=%d sat=%d (fall=%d)",
488 run, Mode2String(mode), sys, sNN, fld, mc, sat, fFallBack);
489 Int_t entry = Query(run, mode, sys, sNN, fld, mc, sat);
490 if (entry < 0 && run > 0)
491 entry = Query(0, mode, sys, sNN, fld, mc, sat);
492 if (entry < 0 && fFallBack && sNN > 0) {
494 Printf("Fall-back enabled, will try without sNN=%d", sNN);
495 entry = Query(run, mode, sys, 0, fld, mc, sat);
497 if (entry < 0 && fFallBack) {
499 Printf("Fall-back enabled, will try without any fields");
500 entry = Query(0, mode, 0, 0, kInvalidField, false, false);
503 Warning("Get", "No valid object could be found");
509 //____________________________________________________________________
510 AliOADBForward::Entry*
511 AliOADBForward::Table::Get(ULong_t run,
519 // Query the tree for an object. The strategy is as follows.
521 // - First query with all fields
522 // - If this returns a single entry, return that.
523 // - If not, then ignore the run number (if given)
524 // - If this returns a single entry, return that
525 // - If not, and fall-back is enabled, then
526 // - Ignore the collision energy (if given)
527 // - If this returns a single entry, return that.
528 // - If not, ignore all passed values
529 // - If this returns a single entry, return that.
530 // - Otherwise, give up and return null
531 // - Otherwise, give up and return null
533 // This allow us to specify default objects for a period, and for
534 // collision system, energy, and field setting.
536 Int_t entry = GetEntry(run, mode, sys, sNN, fld, mc, sat);
537 if (entry < 0) return 0;
539 Int_t nBytes = fTree->GetEntry(entry);
541 Warning("Get", "Failed to get entry # %d\n", entry);
544 if (fVerbose) fEntry->Print();
547 //____________________________________________________________________
549 AliOADBForward::Table::GetData(ULong_t run,
558 // Get data associated with a row or null.
559 // See also AliOADBForward::Get
561 Entry* e = Get(run, mode, sys, sNN, fld, mc, sat);
565 //____________________________________________________________________
567 AliOADBForward::Table::Print(Option_t* option) const
570 // Print the full table
572 if (!IsOpen()) return;
574 Printf("Table %s (default mode: %s)", GetName(), Mode2String(fMode));
575 Int_t n = fTree->GetEntries();
576 for (Int_t i = 0; i < n; i++) {
578 printf("%4d/%4d: ", i, n);
579 fEntry->Print(option);
582 //____________________________________________________________________
584 AliOADBForward::Table::Browse(TBrowser* b)
587 if (fTree) b->Add(fTree);
589 //____________________________________________________________________
591 AliOADBForward::Table::IsOpen(Bool_t rw) const
593 if (!fTree) return false;
594 if (!rw) return true;
596 return fTree->GetCurrentFile()->IsWritable();
598 //====================================================================
599 AliOADBForward::AliOADBForward()
608 //____________________________________________________________________
609 AliOADBForward::AliOADBForward(const AliOADBForward& other)
611 fTables(other.fTables)
618 //____________________________________________________________________
619 AliOADBForward::~AliOADBForward()
627 //____________________________________________________________________
629 AliOADBForward::operator=(const AliOADBForward& other)
634 if (&other == this) return *this;
636 fTables = other.fTables;
642 //____________________________________________________________________
644 AliOADBForward::Open(const TString& fileName,
645 const TString& tables,
650 TString absPath(gSystem->ExpandPathName(fileName));
651 if (absPath.IsNull()) {
652 Error("Open", "Empty path for tables %s", tables.Data());
655 TObject* previous = gROOT->GetListOfFiles()->FindObject(absPath);
658 file = static_cast<TFile*>(previous);
661 file = TFile::Open(fileName, (rw ? "UPDATE" : "READ"));
664 Error("Open", "Failed to open %s", GetName());
667 return Open(file, tables, rw, verb, fallback);
670 //____________________________________________________________________
672 AliOADBForward::Open(TFile* file,
673 const TString& tables,
679 // Open database file and find or create listed tables
681 if (!file) return false;
682 if (rw && !file->IsWritable()) {
683 Warning("Open", "Read+write access requested, but %s opened read-only",
685 if (file->ReOpen("UPDATE") < 0) {
686 Error("Open", "Failed to reopen file in read+write access mode");
691 if (tables.EqualTo("*")) {
693 Error("Open", "Cannot open with '*' in read/write mode");
696 TList* l = file->GetListOfKeys();
699 while ((key = static_cast<TKey*>(next()))) {
700 TClass* cl = gROOT->GetClass(key->GetClassName());
702 if (!cl->InheritsFrom(TTree::Class())) continue;
704 OpenTable(file, rw, key->GetName(), "DEFAULT", verb, fallback);
709 TObjArray* tokens = tables.Tokenize(":,");
710 TObjString* token = 0;
711 TIter nextToken(tokens);
712 while ((token = static_cast<TObjString*>(nextToken()))) {
713 TString& tn = token->String();
714 if (tn.IsNull()) continue;
716 TObjArray* parts = tn.Tokenize("/");
717 TObjString* onam = static_cast<TObjString*>(parts->At(0));
718 TString& name = onam->String();
719 TString mode = "DEFAULT";
720 if (parts->GetEntries() > 1)
721 mode = static_cast<TObjString*>(parts->At(1))->String();
724 OpenTable(file, rw, name, mode, verb, fallback);
733 //____________________________________________________________________
735 AliOADBForward::Close()
738 // Flush all tables and close all files
741 Int_t nFiles = GetFiles(files);
747 TIter nextFile(&files);
749 while ((file = static_cast<TFile*>(nextFile()))) {
750 // if (file->IsWritable()) file->Write();
759 //____________________________________________________________________
761 AliOADBForward::Update()
767 Int_t nFiles = GetFiles(files);
773 TIter nextFile(&files);
776 while ((file = static_cast<TFile*>(nextFile()))) {
777 if (!file->IsWritable()) {
778 Error("Update", "File %s not writeable", file->GetName());
782 nBytes += file->Write();
784 return (nBytes >= 0);
786 //____________________________________________________________________
787 AliOADBForward::Entry*
788 AliOADBForward::Get(const TString& table,
798 // Get a row from selected table
800 Table* t = FindTable(table);
803 return t->Get(run, mode, sys, sNN, fld, mc, sat);
805 //____________________________________________________________________
807 AliOADBForward::GetData(const TString& table,
816 Table* t = FindTable(table);
819 return t->GetData(run, mode, sys, sNN, fld, mc, sat);
821 //____________________________________________________________________
823 AliOADBForward::Insert(const TString& table,
832 const TString& author)
835 // Insert a row into the selected table
837 Table* t = FindTable(table);
838 if (!t) return false;
840 return t->Insert(o, runNo, sys, sNN, field, mc, sat, aliRev, author);
842 //____________________________________________________________________
844 AliOADBForward::CopyEntry(const TString& table,
856 Table* t = FindTable(table);
857 if (!t) return false;
859 Entry* e = t->Get(oldRunNo, t->fMode, oldSys, oldSNN, oldField, mc, sat);
860 if (!e) return false;
862 return t->Insert(e->fData, newRunNo, newSys, newSNN, newField, mc, sat,
863 e->fAliROOTRevision, e->fAuthor);
866 //____________________________________________________________________
868 AliOADBForward::Print(const Option_t* option) const
873 TIter nextTable(&fTables);
876 while ((key = static_cast<TObjString*>(nextTable()))) {
877 Printf("Table: %p", key->GetName());
878 table = static_cast<Table*>(fTables.GetValue(key));
879 if (!table) continue;
880 table->Print(option);
884 //____________________________________________________________________
886 AliOADBForward::Browse(TBrowser* b)
889 // Browse this object
891 TIter nextTable(&fTables);
894 while ((key = static_cast<TObjString*>(nextTable()))) {
895 table = static_cast<Table*>(fTables.GetValue(key));
896 if (!table) continue;
897 b->Add(table, key->GetName());
900 //____________________________________________________________________
901 AliOADBForward::Table*
902 AliOADBForward::FindTable(const TString& name, Bool_t quite) const
905 // Find a table by name
907 TPair* p = static_cast<TPair*>(fTables.FindObject(name));
910 Warning("FindTable", "Table %s not registered", name.Data());
913 return static_cast<Table*>(p->Value());
915 //____________________________________________________________________
917 AliOADBForward::GetFiles(TList& files) const
920 // Get all associated files
923 TIter nextTable(&fTables);
926 while ((key = static_cast<TObjString*>(nextTable()))) {
927 table = static_cast<Table*>(fTables.GetValue(key));
928 if (!table->fTree) continue;
930 TFile* f = table->fTree->GetCurrentFile();
933 if (files.FindObject(f)) continue;
939 //____________________________________________________________________
940 AliOADBForward::Table*
941 AliOADBForward::GetTableFromFile(TFile* file,
944 const TString& mode) const
947 // Get a table from a file, or make a new table
950 if (FindTable(name, true)) return 0;
952 TObject* o = file->Get(name);
957 // We only fail if in read-only mode
958 Error("Open", "No such object: %s in %s", name.Data(),
962 // Create the tree in the file
963 t = new TTree(name, mode);
964 t->SetDirectory(file);
968 // Get the tree, and set the branch
969 t = static_cast<TTree*>(o);
971 Table* ret = new Table(t, n, String2Mode(mode));
975 //____________________________________________________________________
977 AliOADBForward::OpenTable(TFile* file,
986 Table* t = GetTableFromFile(file, rw, name, mode);
989 fTables.Add(new TObjString(name), t);
991 t->SetEnableFallBack(fallback);
993 Printf("Found table %s. Opened with verbose=%d, fallback=%d",
994 name.Data(), verb, fallback);
997 //____________________________________________________________________
999 AliOADBForward::AppendToQuery(TString& q, const TString& s, Bool_t andNotOr)
1004 if (!q.IsNull()) q.Append(andNotOr ? " && " : " || ");
1007 //____________________________________________________________________
1009 AliOADBForward::Conditions(UShort_t sys,
1015 // Build query string
1017 if (sys > 0) AppendToQuery(q, Form("fSys == %hu", sys));
1018 if (sNN > 0) AppendToQuery(q, Form("abs(fSNN - %hu) < 11", sNN));
1019 if (TMath::Abs(fld) < 10) AppendToQuery(q, Form("fField == %hd",fld));
1020 // Boolean fields always queried. In cases where these do not matter,
1021 // we always write down the false value, so we get the correct query
1023 AppendToQuery(q, Form("%sfMC", (mc ? " " : "!")));
1024 AppendToQuery(q, Form("%sfSatellite", (sat ? " " : "!")));
1029 //____________________________________________________________________
1031 AliOADBForward::TestGet(AliOADBForward& t,
1032 const TString& table,
1034 ERunSelectMode mode,
1042 Printf("=== Test query: t=%s r=%ld s=%d t=%d f=%d m=%d v=%d",
1043 table.Data(), runNo, sys, sNN, fld, int(mc), int(sat));
1044 AliOADBForward::Entry* e = t.Get(table, runNo, mode, sys, sNN,
1049 //____________________________________________________________________
1051 AliOADBForward::TestInsert(AliOADBForward& t,
1052 const TString& table,
1060 static Int_t cnt = 0;
1061 TString what = TString::Format("%s-%03d", table.Data(), cnt++);
1062 Printf("=== Insert: t=%s r=%ld s=%d t=%d f=%d m=%d v=%d w=%s",
1063 table.Data(), runNo, sys, sNN, fld, int(mc), int(sat), what.Data());
1064 t.Insert(table, new TObjString(what), runNo, sys, sNN, fld, mc, sat);
1065 gSystem->Sleep(500);
1068 //____________________________________________________________________
1070 AliOADBForward::Test()
1072 AliOADBForward* tt = new AliOADBForward();
1073 if (!tt->Open("db.root", "A,B", true, true)) {
1074 ::Error("Test", "Failed to open DB");
1077 AliOADBForward& t = *tt;
1078 TestInsert(t, "A", 137161);
1079 TestInsert(t, "A", 137161);
1080 TestInsert(t, "A", 0 );
1081 TestInsert(t, "A", 999999);
1082 TestInsert(t, "A", 137166);
1085 TestInsert(t, "B", 137161);
1086 TestInsert(t, "B", 0 );
1090 if (!t.Open("db.root", "A,B",false,true)) {
1091 ::Error("Test", "Failed to open DB");
1095 TestGet(t, "A", 137161);
1096 TestGet(t, "A", 137160);
1097 TestGet(t, "A", 0 );
1098 TestGet(t, "A", 137160, kNewest);
1099 TestGet(t, "A", 137160, kNewer);
1100 TestGet(t, "A", 137168, kOlder);
1101 TestGet(t, "A", 137161, kExact);
1103 new TBrowser("b", tt);