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)
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),
173 fTree->SetBranchAddress("e", &fEntry);
176 //____________________________________________________________________
177 AliOADBForward::Table::~Table()
184 //____________________________________________________________________
185 AliOADBForward::Table&
186 AliOADBForward::Table::operator=(const Table& o)
189 // Assignment operator
191 if (this == &o) return *this;
194 fVerbose = o.fVerbose;
196 if (fTree) fTree->SetBranchAddress("e", &fEntry);
201 //____________________________________________________________________
203 AliOADBForward::Table::GetTableName() const
206 // Get the table name or null
207 if (!fTree) return 0;
208 return fTree->GetName();
210 //____________________________________________________________________
212 AliOADBForward::Table::GetName() const
215 // Overload of TObject::GetName
217 if (!fTree) return TObject::GetName();
218 return GetTableName();
220 //____________________________________________________________________
222 AliOADBForward::Table::Update()
228 Error("Update", "No tree associated");
232 TFile* file = fTree->GetCurrentFile();
234 Error("Update", "No file associated with tree");
237 if (!file->IsWritable()) {
238 Error("Update", "File %s not writeable", file->GetName());
242 Int_t nBytes = file->Write();
244 return (nBytes >= 0);
246 //____________________________________________________________________
248 AliOADBForward::Table::Close()
251 // Close the connection
254 Error("Close", "No tree associated");
258 // if (fTree) delete fTree;
259 // if (fEntry) delete fEntry;
264 //____________________________________________________________________
266 AliOADBForward::Table::Query(ULong_t runNo,
277 return Query(runNo, mode, Conditions(sys, sNN, fld, mc, sat));
280 //____________________________________________________________________
282 AliOADBForward::Table::Query(ULong_t runNo,
284 const TString& q) const
287 // Run a query against the table
292 Error("Close", "No tree associated");
297 const char* smode = "latest";
299 if (mode <= kDefault || mode > kNewer) mode = fMode;
300 smode = Mode2String(mode);
303 AppendToQuery(query, Form("fRunNo == %lu", runNo));
308 AppendToQuery(query, Form("abs(fRunNo-%lu)<=%d",
309 runNo,kMaxNearDistance));
312 AppendToQuery(query, Form("fRunNo <= %lu", runNo));
315 AppendToQuery(query, Form("fRunNo >= %lu", runNo));
318 Fatal("Query", "Mode should never be 'default'");
323 if (query.IsNull()) {
324 Warning("Query", "Empty query!");
329 Printf("%s: Query is '%s'", GetName(), query.Data());
330 fTree->Draw("Entry$:fRunNo:fTimestamp", query, "goff");
331 Int_t nRows = fTree->GetSelectedRows();
332 if (nRows <= 0) return -1;
335 Printf("Query: %s (%s)\n"
336 " Entry | Run | Timestamp \n"
337 "--------+-----------+------------------------",
338 query.Data(), smode);
340 ULong_t oldRun = (mode == kNewer ? 0xFFFFFFFF : 0);
342 ULong_t oldDist = 0xFFFFFFFF;
344 for (Int_t row = 0; row < nRows; row++) {
345 Int_t ent = fTree->GetV1()[row];
346 ULong_t run = fTree->GetV2()[row];
347 ULong_t tim = fTree->GetV3()[row];
348 ULong_t dist = (run > runNo ? run - runNo : runNo - run);
352 Printf(" %6d | %9ld | %19s ", ent, run > 0x7FFFFFFF ? -1 : run,
357 case kExact: break; // Done in the draw `query'
358 case kNewest: // Fall-through
360 if (run < oldRun) continue;
363 if (run > oldRun) continue;
366 if (runNo > 0 && dist > oldDist) continue;
371 // If we get here, then we have the best run according to the
374 // Finally, check the timestamp
375 if (tim < oldTim) continue;
377 // Now update last values and set current best entry
385 Printf("Returning entry # %d", entry);
390 //____________________________________________________________________
392 AliOADBForward::Table::Insert(TObject* o,
400 const TString& author)
403 // Insert a new row in the table
406 // Check if the file is read/write
408 Info("Insert", "Inserting object %p for run=%lu, sys=%hu, sNN=%4hu, "
409 "field=%+2hd, mc=%d, sat=%d", o,runNo, sys, sNN, field, mc, sat);
412 Warning("Insert", "No tree, or not write-able");
416 // If the entry doesn't exists
417 if (!fEntry) fEntry = new Entry;
420 TString auth(author);
422 UserGroup_t* u = gSystem->GetUserInfo();
423 TInetAddress i = gSystem->GetHostByName(gSystem->HostName());
424 auth = TString::Format("%s <%s@%s>", u->fRealName.Data(),
425 u->fUser.Data(), i.GetHostName());
430 fEntry->fRunNo = runNo; // (runNo <= 0 ? 0xFFFFFFFF : runNo);
433 fEntry->fField = field;
435 fEntry->fSatellite = sat;
436 fEntry->fAliROOTRevision = (aliRev != 0 ? aliRev : ALIROOT_SVN_REVISION);
437 fEntry->fAuthor = auth;
440 fEntry->fTimestamp = now.Convert(true);
443 Int_t nBytes = fTree->Fill();
445 Warning("Insert", "Failed to insert new entry");
449 // do an Auto-save and flush-baskets now
450 fTree->AutoSave("FlushBaskets SaveSelf");
455 //____________________________________________________________________
456 AliOADBForward::Entry*
457 AliOADBForward::Table::Get(ULong_t run,
465 // Query the tree for an object. The strategy is as follows.
467 // - First query with all fields
468 // - If this returns a single entry, return that.
469 // - If not, then ignore the run number (if given)
470 // - If this returns a single entry, return that
471 // - Otherwise, give up and return null
473 // This allow us to specify default objects for a period, and for
474 // collision system, energy, and field setting.
477 Printf("run=%lu mode=%s sys=%hu sNN=%hu fld=%hd mc=%d sat=%d",
478 run, Mode2String(mode), sys, sNN, fld, mc, sat);
479 Int_t entry = Query(run, mode, sys, sNN, fld, mc, sat);
480 if (entry < 0 && run > 0)
481 entry = Query(0, mode, sys, sNN, fld, mc, sat);
483 Warning("Get", "No valid object could be found");
487 Int_t nBytes = fTree->GetEntry(entry);
489 Warning("Get", "Failed to get entry # %d\n", entry);
492 if (fVerbose) fEntry->Print();
495 //____________________________________________________________________
497 AliOADBForward::Table::GetData(ULong_t run,
506 // Get data associated with a row or null.
507 // See also AliOADBForward::Get
509 Entry* e = Get(run, mode, sys, sNN, fld, mc, sat);
513 //____________________________________________________________________
515 AliOADBForward::Table::Print(Option_t* option) const
518 // Print the full table
520 if (!IsOpen()) return;
522 Printf("Table %s (default mode: %s)", GetName(), Mode2String(fMode));
523 Int_t n = fTree->GetEntries();
524 for (Int_t i = 0; i < n; i++) {
526 printf("%4d/%4d: ", i, n);
527 fEntry->Print(option);
530 //____________________________________________________________________
532 AliOADBForward::Table::Browse(TBrowser* b)
535 if (fTree) b->Add(fTree);
537 //____________________________________________________________________
539 AliOADBForward::Table::IsOpen(Bool_t rw) const
541 if (!fTree) return false;
542 if (!rw) return true;
544 return fTree->GetCurrentFile()->IsWritable();
546 //====================================================================
547 AliOADBForward::AliOADBForward()
556 //____________________________________________________________________
557 AliOADBForward::AliOADBForward(const AliOADBForward& other)
559 fTables(other.fTables)
566 //____________________________________________________________________
567 AliOADBForward::~AliOADBForward()
575 //____________________________________________________________________
577 AliOADBForward::operator=(const AliOADBForward& other)
582 if (&other == this) return *this;
584 fTables = other.fTables;
590 //____________________________________________________________________
592 AliOADBForward::Open(const TString& fileName,
593 const TString& tables,
597 TString absPath(gSystem->ExpandPathName(fileName));
598 if (absPath.IsNull()) {
599 Error("Open", "Empty path for tables %s", tables.Data());
602 TObject* previous = gROOT->GetListOfFiles()->FindObject(absPath);
605 file = static_cast<TFile*>(previous);
608 file = TFile::Open(fileName, (rw ? "UPDATE" : "READ"));
611 Error("Open", "Failed to open %s", GetName());
614 return Open(file, tables, rw, verb);
617 //____________________________________________________________________
619 AliOADBForward::Open(TFile* file,
620 const TString& tables,
625 // Open database file and find or create listed tables
627 if (!file) return false;
628 if (rw && !file->IsWritable()) {
629 Warning("Open", "Read+write access requested, but %s opened read-only",
631 if (file->ReOpen("UPDATE") < 0) {
632 Error("Open", "Failed to reopen file in read+write access mode");
637 if (tables.EqualTo("*")) {
639 Error("Open", "Cannot open with '*' in read/write mode");
642 TList* l = file->GetListOfKeys();
645 while ((key = static_cast<TKey*>(next()))) {
646 TClass* cl = gROOT->GetClass(key->GetClassName());
648 if (!cl->InheritsFrom(TTree::Class())) continue;
650 Table* t = GetTableFromFile(file, false, key->GetName(), "DEFAULT");
653 fTables.Add(new TObjString(key->GetName()), t);
659 TObjArray* tokens = tables.Tokenize(":,");
660 TObjString* token = 0;
661 TIter nextToken(tokens);
662 while ((token = static_cast<TObjString*>(nextToken()))) {
663 TString& tn = token->String();
664 if (tn.IsNull()) continue;
666 TObjArray* parts = tn.Tokenize("/");
667 TObjString* onam = static_cast<TObjString*>(parts->At(0));
668 TString& name = onam->String();
669 TString mode = "DEFAULT";
670 if (parts->GetEntries() > 1)
671 mode = static_cast<TObjString*>(parts->At(1))->String();
673 Table* t = GetTableFromFile(file, rw, name, mode);
677 fTables.Add(onam->Clone(), t);
686 //____________________________________________________________________
688 AliOADBForward::Close()
691 // Flush all tables and close all files
694 Int_t nFiles = GetFiles(files);
700 TIter nextFile(&files);
702 while ((file = static_cast<TFile*>(nextFile()))) {
703 // if (file->IsWritable()) file->Write();
712 //____________________________________________________________________
714 AliOADBForward::Update()
720 Int_t nFiles = GetFiles(files);
726 TIter nextFile(&files);
729 while ((file = static_cast<TFile*>(nextFile()))) {
730 if (!file->IsWritable()) {
731 Error("Update", "File %s not writeable", file->GetName());
735 nBytes += file->Write();
737 return (nBytes >= 0);
739 //____________________________________________________________________
740 AliOADBForward::Entry*
741 AliOADBForward::Get(const TString& table,
751 // Get a row from selected table
753 Table* t = FindTable(table);
756 return t->Get(run, mode, sys, sNN, fld, mc, sat);
758 //____________________________________________________________________
760 AliOADBForward::GetData(const TString& table,
769 Table* t = FindTable(table);
772 return t->GetData(run, mode, sys, sNN, fld, mc, sat);
774 //____________________________________________________________________
776 AliOADBForward::Insert(const TString& table,
785 const TString& author)
788 // Insert a row into the selected table
790 Table* t = FindTable(table);
791 if (!t) return false;
793 return t->Insert(o, runNo, sys, sNN, field, mc, sat, aliRev, author);
795 //____________________________________________________________________
797 AliOADBForward::Print(const Option_t* option) const
802 TIter nextTable(&fTables);
805 while ((key = static_cast<TObjString*>(nextTable()))) {
806 Printf("Table: %p", key->GetName());
807 table = static_cast<Table*>(fTables.GetValue(key));
808 if (!table) continue;
809 table->Print(option);
813 //____________________________________________________________________
815 AliOADBForward::Browse(TBrowser* b)
818 // Browse this object
820 TIter nextTable(&fTables);
823 while ((key = static_cast<TObjString*>(nextTable()))) {
824 table = static_cast<Table*>(fTables.GetValue(key));
825 if (!table) continue;
826 b->Add(table, key->GetName());
829 //____________________________________________________________________
830 AliOADBForward::Table*
831 AliOADBForward::FindTable(const TString& name, Bool_t quite) const
834 // Find a table by name
836 TPair* p = static_cast<TPair*>(fTables.FindObject(name));
839 Warning("FindTable", "Table %s not registered", name.Data());
842 return static_cast<Table*>(p->Value());
844 //____________________________________________________________________
846 AliOADBForward::GetFiles(TList& files) const
849 // Get all associated files
852 TIter nextTable(&fTables);
855 while ((key = static_cast<TObjString*>(nextTable()))) {
856 table = static_cast<Table*>(fTables.GetValue(key));
857 if (!table->fTree) continue;
859 TFile* f = table->fTree->GetCurrentFile();
862 if (files.FindObject(f)) continue;
868 //____________________________________________________________________
869 AliOADBForward::Table*
870 AliOADBForward::GetTableFromFile(TFile* file,
873 const TString& mode) const
876 // Get a table from a file, or make a new table
879 if (FindTable(name, true)) return 0;
881 TObject* o = file->Get(name);
886 // We only fail if in read-only mode
887 Error("Open", "No such object: %s in %s", name.Data(),
891 // Create the tree in the file
892 t = new TTree(name, mode);
893 t->SetDirectory(file);
897 // Get the tree, and set the branch
898 t = static_cast<TTree*>(o);
900 Table* ret = new Table(t, n, String2Mode(mode));
903 //____________________________________________________________________
905 AliOADBForward::AppendToQuery(TString& q, const TString& s, Bool_t andNotOr)
910 if (!q.IsNull()) q.Append(andNotOr ? " && " : " || ");
913 //____________________________________________________________________
915 AliOADBForward::Conditions(UShort_t sys,
921 // Build query string
923 if (sys > 0) AppendToQuery(q, Form("fSys == %hu", sys));
924 if (sNN > 0) AppendToQuery(q, Form("abs(fSNN - %hu) < 11", sNN));
925 if (TMath::Abs(fld) < 10) AppendToQuery(q, Form("fField == %hd",fld));
926 // Boolean fields always queried. In cases where these do not matter,
927 // we always write down the false value, so we get the correct query
929 AppendToQuery(q, Form("%sfMC", (mc ? " " : "!")));
930 AppendToQuery(q, Form("%sfSatellite", (sat ? " " : "!")));
935 //____________________________________________________________________
937 AliOADBForward::TestGet(AliOADBForward& t,
938 const TString& table,
948 Printf("=== Test query: t=%s r=%ld s=%d t=%d f=%d m=%d v=%d",
949 table.Data(), runNo, sys, sNN, fld, int(mc), int(sat));
950 AliOADBForward::Entry* e = t.Get(table, runNo, mode, sys, sNN,
955 //____________________________________________________________________
957 AliOADBForward::TestInsert(AliOADBForward& t,
958 const TString& table,
966 static Int_t cnt = 0;
967 TString what = TString::Format("%s-%03d", table.Data(), cnt++);
968 Printf("=== Insert: t=%s r=%ld s=%d t=%d f=%d m=%d v=%d w=%s",
969 table.Data(), runNo, sys, sNN, fld, int(mc), int(sat), what.Data());
970 t.Insert(table, new TObjString(what), runNo, sys, sNN, fld, mc, sat);
974 //____________________________________________________________________
976 AliOADBForward::Test()
978 AliOADBForward* tt = new AliOADBForward();
979 if (!tt->Open("db.root", "A,B", true, true)) {
980 ::Error("Test", "Failed to open DB");
983 AliOADBForward& t = *tt;
984 TestInsert(t, "A", 137161);
985 TestInsert(t, "A", 137161);
986 TestInsert(t, "A", 0 );
987 TestInsert(t, "A", 999999);
988 TestInsert(t, "A", 137166);
991 TestInsert(t, "B", 137161);
992 TestInsert(t, "B", 0 );
996 if (!t.Open("db.root", "A,B",false,true)) {
997 ::Error("Test", "Failed to open DB");
1001 TestGet(t, "A", 137161);
1002 TestGet(t, "A", 137160);
1003 TestGet(t, "A", 0 );
1004 TestGet(t, "A", 137160, kNewest);
1005 TestGet(t, "A", 137160, kNewer);
1006 TestGet(t, "A", 137168, kOlder);
1007 TestGet(t, "A", 137161, kExact);
1009 new TBrowser("b", tt);