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, %+2hd, %4s, %3s, %19s: %p (%s) by %s",
131 (fRunNo == 0xFFFFFFFF ? -1 : fRunNo),
132 (fSys == 1 ? "pp" : fSys == 2 ? "PbPb" : fSys == 3 ? "pPb" :"?"),
133 fSNN, fField, (fMC ? "mc" : "real"),
134 (fSatellite ? "sat" : "nom"), d.AsSQLString(), fData,
135 (fData ? fData->GetName() : "?"), fAuthor.Data());
138 //____________________________________________________________________
140 AliOADBForward::Entry::Print(Option_t* /*option*/) const
144 //====================================================================
145 AliOADBForward::Table::Table(TTree* tree, Bool_t isNew, ERunSelectMode mode)
146 : fTree(tree), fEntry(0), fVerbose(false), fMode(mode), fFallBack(false)
151 fTree->Branch("e", "AliOADBForward::Entry", &fEntry);
152 fMode = String2Mode(fTree->GetTitle());
155 if (fMode <= kDefault || fMode > kNewer) {
156 fMode = String2Mode(fTree->GetTitle());
157 if (fMode == kDefault) fMode = kNear;
159 fTree->SetBranchAddress("e", &fEntry);
162 //____________________________________________________________________
163 AliOADBForward::Table::Table(const Table& o)
167 fVerbose(o.fVerbose),
169 fFallBack(o.fFallBack)
174 fTree->SetBranchAddress("e", &fEntry);
177 //____________________________________________________________________
178 AliOADBForward::Table::~Table()
185 //____________________________________________________________________
186 AliOADBForward::Table&
187 AliOADBForward::Table::operator=(const Table& o)
190 // Assignment operator
192 if (this == &o) return *this;
195 fVerbose = o.fVerbose;
197 if (fTree) fTree->SetBranchAddress("e", &fEntry);
202 //____________________________________________________________________
204 AliOADBForward::Table::GetTableName() const
207 // Get the table name or null
208 if (!fTree) return 0;
209 return fTree->GetName();
211 //____________________________________________________________________
213 AliOADBForward::Table::GetName() const
216 // Overload of TObject::GetName
218 if (!fTree) return TObject::GetName();
219 return GetTableName();
221 //____________________________________________________________________
223 AliOADBForward::Table::Update()
229 Error("Update", "No tree associated");
233 TFile* file = fTree->GetCurrentFile();
235 Error("Update", "No file associated with tree");
238 if (!file->IsWritable()) {
239 Error("Update", "File %s not writeable", file->GetName());
243 Int_t nBytes = file->Write();
245 return (nBytes >= 0);
247 //____________________________________________________________________
249 AliOADBForward::Table::Close()
252 // Close the connection
255 Error("Close", "No tree associated");
259 // if (fTree) delete fTree;
260 // if (fEntry) delete fEntry;
265 //____________________________________________________________________
267 AliOADBForward::Table::Query(ULong_t runNo,
278 return Query(runNo, mode, Conditions(sys, sNN, fld, mc, sat));
281 //____________________________________________________________________
283 AliOADBForward::Table::Query(ULong_t runNo,
285 const TString& q) const
288 // Run a query against the table
293 Error("Close", "No tree associated");
298 const char* smode = "latest";
300 if (mode <= kDefault || mode > kNewer) mode = fMode;
301 smode = Mode2String(mode);
304 AppendToQuery(query, Form("fRunNo == %lu", runNo));
309 AppendToQuery(query, Form("abs(fRunNo-%lu)<=%d",
310 runNo,kMaxNearDistance));
313 AppendToQuery(query, Form("fRunNo <= %lu", runNo));
316 AppendToQuery(query, Form("fRunNo >= %lu", runNo));
319 Fatal("Query", "Mode should never be 'default'");
324 if (query.IsNull()) {
325 Warning("Query", "Empty query!");
330 Printf("%s: Query is '%s'", GetName(), query.Data());
331 fTree->Draw("Entry$:fRunNo:fTimestamp", query, "goff");
332 Int_t nRows = fTree->GetSelectedRows();
333 if (nRows <= 0) return -1;
336 Printf("Query: %s (%s)\n"
337 " Entry | Run | Timestamp \n"
338 "--------+-----------+------------------------",
339 query.Data(), smode);
341 ULong_t oldRun = (mode == kNewer ? 0xFFFFFFFF : 0);
343 ULong_t oldDist = 0xFFFFFFFF;
345 for (Int_t row = 0; row < nRows; row++) {
346 Int_t ent = fTree->GetV1()[row];
347 ULong_t run = fTree->GetV2()[row];
348 ULong_t tim = fTree->GetV3()[row];
349 ULong_t dist = (run > runNo ? run - runNo : runNo - run);
353 Printf(" %6d | %9ld | %19s ", ent, run > 0x7FFFFFFF ? -1 : run,
358 case kExact: break; // Done in the draw `query'
359 case kNewest: // Fall-through
361 if (run < oldRun) continue;
364 if (run > oldRun) continue;
367 if (runNo > 0 && dist > oldDist) continue;
372 // If we get here, then we have the best run according to the
375 // Finally, check the timestamp
376 if (tim < oldTim) continue;
378 // Now update last values and set current best entry
386 Printf("Returning entry # %d", entry);
391 //____________________________________________________________________
393 AliOADBForward::Table::Insert(TObject* o,
401 const TString& author)
404 // Insert a new row in the table
407 // Check if the file is read/write
409 Info("Insert", "Inserting object %p for run=%lu, sys=%hu, sNN=%4hu, "
410 "field=%+2hd, mc=%d, sat=%d", o,runNo, sys, sNN, field, mc, sat);
413 Warning("Insert", "No tree, or not write-able");
417 // If the entry doesn't exists
418 if (!fEntry) fEntry = new Entry;
421 TString auth(author);
423 UserGroup_t* u = gSystem->GetUserInfo();
424 TInetAddress i = gSystem->GetHostByName(gSystem->HostName());
425 auth = TString::Format("%s <%s@%s>", u->fRealName.Data(),
426 u->fUser.Data(), i.GetHostName());
431 fEntry->fRunNo = runNo; // (runNo <= 0 ? 0xFFFFFFFF : runNo);
434 fEntry->fField = field;
436 fEntry->fSatellite = sat;
437 fEntry->fAliROOTRevision = (aliRev != 0 ? aliRev : ALIROOT_SVN_REVISION);
438 fEntry->fAuthor = auth;
441 fEntry->fTimestamp = now.Convert(true);
444 Int_t nBytes = fTree->Fill();
446 Warning("Insert", "Failed to insert new entry");
450 // do an Auto-save and flush-baskets now
451 fTree->AutoSave("FlushBaskets SaveSelf");
456 //____________________________________________________________________
457 AliOADBForward::Entry*
458 AliOADBForward::Table::Get(ULong_t run,
466 // Query the tree for an object. The strategy is as follows.
468 // - First query with all fields
469 // - If this returns a single entry, return that.
470 // - If not, then ignore the run number (if given)
471 // - If this returns a single entry, return that
472 // - If not, and fall-back is enabled, then
473 // - Ignore the collision energy (if given)
474 // - If this returns a single entry, return that.
475 // - If not, ignore all passed values
476 // - If this returns a single entry, return that.
477 // - Otherwise, give up and return null
478 // - Otherwise, give up and return null
480 // This allow us to specify default objects for a period, and for
481 // collision system, energy, and field setting.
484 Printf("run=%lu mode=%s sys=%hu sNN=%hu fld=%hd mc=%d sat=%d (fall=%d)",
485 run, Mode2String(mode), sys, sNN, fld, mc, sat, fFallBack);
486 Int_t entry = Query(run, mode, sys, sNN, fld, mc, sat);
487 if (entry < 0 && run > 0)
488 entry = Query(0, mode, sys, sNN, fld, mc, sat);
489 if (entry < 0 && fFallBack && sNN > 0) {
491 Printf("Fall-back enabled, will try without sNN=%d", sNN);
492 entry = Query(run, mode, sys, 0, fld, mc, sat);
494 if (entry < 0 && fFallBack) {
496 Printf("Fall-back enabled, will try without any fields");
497 entry = Query(0, mode, 0, 0, kInvalidField, false, false);
500 Warning("Get", "No valid object could be found");
504 Int_t nBytes = fTree->GetEntry(entry);
506 Warning("Get", "Failed to get entry # %d\n", entry);
509 if (fVerbose) fEntry->Print();
512 //____________________________________________________________________
514 AliOADBForward::Table::GetData(ULong_t run,
523 // Get data associated with a row or null.
524 // See also AliOADBForward::Get
526 Entry* e = Get(run, mode, sys, sNN, fld, mc, sat);
530 //____________________________________________________________________
532 AliOADBForward::Table::Print(Option_t* option) const
535 // Print the full table
537 if (!IsOpen()) return;
539 Printf("Table %s (default mode: %s)", GetName(), Mode2String(fMode));
540 Int_t n = fTree->GetEntries();
541 for (Int_t i = 0; i < n; i++) {
543 printf("%4d/%4d: ", i, n);
544 fEntry->Print(option);
547 //____________________________________________________________________
549 AliOADBForward::Table::Browse(TBrowser* b)
552 if (fTree) b->Add(fTree);
554 //____________________________________________________________________
556 AliOADBForward::Table::IsOpen(Bool_t rw) const
558 if (!fTree) return false;
559 if (!rw) return true;
561 return fTree->GetCurrentFile()->IsWritable();
563 //====================================================================
564 AliOADBForward::AliOADBForward()
573 //____________________________________________________________________
574 AliOADBForward::AliOADBForward(const AliOADBForward& other)
576 fTables(other.fTables)
583 //____________________________________________________________________
584 AliOADBForward::~AliOADBForward()
592 //____________________________________________________________________
594 AliOADBForward::operator=(const AliOADBForward& other)
599 if (&other == this) return *this;
601 fTables = other.fTables;
607 //____________________________________________________________________
609 AliOADBForward::Open(const TString& fileName,
610 const TString& tables,
615 TString absPath(gSystem->ExpandPathName(fileName));
616 if (absPath.IsNull()) {
617 Error("Open", "Empty path for tables %s", tables.Data());
620 TObject* previous = gROOT->GetListOfFiles()->FindObject(absPath);
623 file = static_cast<TFile*>(previous);
626 file = TFile::Open(fileName, (rw ? "UPDATE" : "READ"));
629 Error("Open", "Failed to open %s", GetName());
632 return Open(file, tables, rw, verb, fallback);
635 //____________________________________________________________________
637 AliOADBForward::Open(TFile* file,
638 const TString& tables,
644 // Open database file and find or create listed tables
646 if (!file) return false;
647 if (rw && !file->IsWritable()) {
648 Warning("Open", "Read+write access requested, but %s opened read-only",
650 if (file->ReOpen("UPDATE") < 0) {
651 Error("Open", "Failed to reopen file in read+write access mode");
656 if (tables.EqualTo("*")) {
658 Error("Open", "Cannot open with '*' in read/write mode");
661 TList* l = file->GetListOfKeys();
664 while ((key = static_cast<TKey*>(next()))) {
665 TClass* cl = gROOT->GetClass(key->GetClassName());
667 if (!cl->InheritsFrom(TTree::Class())) continue;
669 Table* t = GetTableFromFile(file, false, key->GetName(), "DEFAULT");
672 fTables.Add(new TObjString(key->GetName()), t);
674 t->SetEnableFallBack(fallback);
676 Printf("Found table %s. Opened with verbose=%d, fallback=%d",
677 key->GetName(), verb, fallback);
682 TObjArray* tokens = tables.Tokenize(":,");
683 TObjString* token = 0;
684 TIter nextToken(tokens);
685 while ((token = static_cast<TObjString*>(nextToken()))) {
686 TString& tn = token->String();
687 if (tn.IsNull()) continue;
689 TObjArray* parts = tn.Tokenize("/");
690 TObjString* onam = static_cast<TObjString*>(parts->At(0));
691 TString& name = onam->String();
692 TString mode = "DEFAULT";
693 if (parts->GetEntries() > 1)
694 mode = static_cast<TObjString*>(parts->At(1))->String();
696 Table* t = GetTableFromFile(file, rw, name, mode);
700 t->SetEnableFallBack(fallback);
701 fTables.Add(onam->Clone(), t);
703 Printf("Has table %s. Opened with verbose=%d, fallback=%d",
704 name.Data(), verb, fallback);
713 //____________________________________________________________________
715 AliOADBForward::Close()
718 // Flush all tables and close all files
721 Int_t nFiles = GetFiles(files);
727 TIter nextFile(&files);
729 while ((file = static_cast<TFile*>(nextFile()))) {
730 // if (file->IsWritable()) file->Write();
739 //____________________________________________________________________
741 AliOADBForward::Update()
747 Int_t nFiles = GetFiles(files);
753 TIter nextFile(&files);
756 while ((file = static_cast<TFile*>(nextFile()))) {
757 if (!file->IsWritable()) {
758 Error("Update", "File %s not writeable", file->GetName());
762 nBytes += file->Write();
764 return (nBytes >= 0);
766 //____________________________________________________________________
767 AliOADBForward::Entry*
768 AliOADBForward::Get(const TString& table,
778 // Get a row from selected table
780 Table* t = FindTable(table);
783 return t->Get(run, mode, sys, sNN, fld, mc, sat);
785 //____________________________________________________________________
787 AliOADBForward::GetData(const TString& table,
796 Table* t = FindTable(table);
799 return t->GetData(run, mode, sys, sNN, fld, mc, sat);
801 //____________________________________________________________________
803 AliOADBForward::Insert(const TString& table,
812 const TString& author)
815 // Insert a row into the selected table
817 Table* t = FindTable(table);
818 if (!t) return false;
820 return t->Insert(o, runNo, sys, sNN, field, mc, sat, aliRev, author);
822 //____________________________________________________________________
824 AliOADBForward::Print(const Option_t* option) const
829 TIter nextTable(&fTables);
832 while ((key = static_cast<TObjString*>(nextTable()))) {
833 Printf("Table: %p", key->GetName());
834 table = static_cast<Table*>(fTables.GetValue(key));
835 if (!table) continue;
836 table->Print(option);
840 //____________________________________________________________________
842 AliOADBForward::Browse(TBrowser* b)
845 // Browse this object
847 TIter nextTable(&fTables);
850 while ((key = static_cast<TObjString*>(nextTable()))) {
851 table = static_cast<Table*>(fTables.GetValue(key));
852 if (!table) continue;
853 b->Add(table, key->GetName());
856 //____________________________________________________________________
857 AliOADBForward::Table*
858 AliOADBForward::FindTable(const TString& name, Bool_t quite) const
861 // Find a table by name
863 TPair* p = static_cast<TPair*>(fTables.FindObject(name));
866 Warning("FindTable", "Table %s not registered", name.Data());
869 return static_cast<Table*>(p->Value());
871 //____________________________________________________________________
873 AliOADBForward::GetFiles(TList& files) const
876 // Get all associated files
879 TIter nextTable(&fTables);
882 while ((key = static_cast<TObjString*>(nextTable()))) {
883 table = static_cast<Table*>(fTables.GetValue(key));
884 if (!table->fTree) continue;
886 TFile* f = table->fTree->GetCurrentFile();
889 if (files.FindObject(f)) continue;
895 //____________________________________________________________________
896 AliOADBForward::Table*
897 AliOADBForward::GetTableFromFile(TFile* file,
900 const TString& mode) const
903 // Get a table from a file, or make a new table
906 if (FindTable(name, true)) return 0;
908 TObject* o = file->Get(name);
913 // We only fail if in read-only mode
914 Error("Open", "No such object: %s in %s", name.Data(),
918 // Create the tree in the file
919 t = new TTree(name, mode);
920 t->SetDirectory(file);
924 // Get the tree, and set the branch
925 t = static_cast<TTree*>(o);
927 Table* ret = new Table(t, n, String2Mode(mode));
930 //____________________________________________________________________
932 AliOADBForward::AppendToQuery(TString& q, const TString& s, Bool_t andNotOr)
937 if (!q.IsNull()) q.Append(andNotOr ? " && " : " || ");
940 //____________________________________________________________________
942 AliOADBForward::Conditions(UShort_t sys,
948 // Build query string
950 if (sys > 0) AppendToQuery(q, Form("fSys == %hu", sys));
951 if (sNN > 0) AppendToQuery(q, Form("abs(fSNN - %hu) < 11", sNN));
952 if (TMath::Abs(fld) < 10) AppendToQuery(q, Form("fField == %hd",fld));
953 // Boolean fields always queried. In cases where these do not matter,
954 // we always write down the false value, so we get the correct query
956 AppendToQuery(q, Form("%sfMC", (mc ? " " : "!")));
957 AppendToQuery(q, Form("%sfSatellite", (sat ? " " : "!")));
962 //____________________________________________________________________
964 AliOADBForward::TestGet(AliOADBForward& t,
965 const TString& table,
975 Printf("=== Test query: t=%s r=%ld s=%d t=%d f=%d m=%d v=%d",
976 table.Data(), runNo, sys, sNN, fld, int(mc), int(sat));
977 AliOADBForward::Entry* e = t.Get(table, runNo, mode, sys, sNN,
982 //____________________________________________________________________
984 AliOADBForward::TestInsert(AliOADBForward& t,
985 const TString& table,
993 static Int_t cnt = 0;
994 TString what = TString::Format("%s-%03d", table.Data(), cnt++);
995 Printf("=== Insert: t=%s r=%ld s=%d t=%d f=%d m=%d v=%d w=%s",
996 table.Data(), runNo, sys, sNN, fld, int(mc), int(sat), what.Data());
997 t.Insert(table, new TObjString(what), runNo, sys, sNN, fld, mc, sat);
1001 //____________________________________________________________________
1003 AliOADBForward::Test()
1005 AliOADBForward* tt = new AliOADBForward();
1006 if (!tt->Open("db.root", "A,B", true, true)) {
1007 ::Error("Test", "Failed to open DB");
1010 AliOADBForward& t = *tt;
1011 TestInsert(t, "A", 137161);
1012 TestInsert(t, "A", 137161);
1013 TestInsert(t, "A", 0 );
1014 TestInsert(t, "A", 999999);
1015 TestInsert(t, "A", 137166);
1018 TestInsert(t, "B", 137161);
1019 TestInsert(t, "B", 0 );
1023 if (!t.Open("db.root", "A,B",false,true)) {
1024 ::Error("Test", "Failed to open DB");
1028 TestGet(t, "A", 137161);
1029 TestGet(t, "A", 137160);
1030 TestGet(t, "A", 0 );
1031 TestGet(t, "A", 137160, kNewest);
1032 TestGet(t, "A", 137160, kNewer);
1033 TestGet(t, "A", 137168, kOlder);
1034 TestGet(t, "A", 137161, kExact);
1036 new TBrowser("b", tt);