AliPhysics  323987d (323987d)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
AliOADBForward.cxx
Go to the documentation of this file.
1 #include "AliOADBForward.h"
2 #include <TBrowser.h>
3 #include <TROOT.h>
4 #include <TKey.h>
5 #include <TList.h>
6 #include <TDatime.h>
7 #include <TTree.h>
8 #include <TFile.h>
9 #include <TError.h>
10 #include <TSystem.h>
11 
12 #ifndef ALIROOT_SVN_REVISION
13 # define ALIROOT_SVN_REVISION 0
14 #endif
15 
16 
18 #if 0
19 ; // Do not remove - for Emacs
20 #endif
21 
22 //====================================================================
23 const char*
24 AliOADBForward::Mode2String(ERunSelectMode mode)
25 {
26  switch (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";
33  }
34  return "?"; // Should never get here
35 }
38 {
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;
45  return kDefault;
46 }
49 {
50  switch (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;
57  }
58  return kDefault; // Should never get here
59 }
60 
61 
62 //====================================================================
64  UShort_t sys,
65  UShort_t sNN,
66  Short_t field,
67  Bool_t mc,
68  Bool_t sat,
69  TObject* o)
70  : fRunNo(runNo),
71  fSys(sys),
72  fSNN(sNN),
73  fField(field),
74  fMC(mc),
75  fSatellite(sat),
76  fData(o),
77  fTimestamp(0),
78  fAliROOTRevision(0),
79  fAuthor("unknown")
80 {
81  //
82  // Constructor
83  //
84 }
85 
86 //____________________________________________________________________
88  : TObject(o),
89  fRunNo(o.fRunNo),
90  fSys(o.fSys),
91  fSNN(o.fSNN),
92  fField(o.fField),
93  fMC(o.fMC),
94  fSatellite(o.fSatellite),
95  fData(o.fData),
96  fTimestamp(0),
97  fAliROOTRevision(0),
98  fAuthor(o.fAuthor)
99 {
100  //
101  // Copy constructor
102  //
103 }
104 //____________________________________________________________________
107 {
108  //
109  // Assignment operator
110  //
111  if (this == &o) return *this;
112  fRunNo = o.fRunNo;
113  fSys = o.fSys;
114  fSNN = o.fSNN;
115  fField = o.fField;
116  fMC = o.fMC;
117  fSatellite = o.fSatellite;
118  fData = o.fData;
119  fTimestamp = o.fTimestamp;
120  fAliROOTRevision = o.fAliROOTRevision;
121  fAuthor = o.fAuthor;
122 
123  return *this;
124 }
125 //____________________________________________________________________
126 const char*
128 {
129  TDatime d(fTimestamp);
130  return Form("%09ld, %4s, %4huGeV, %+4hd, %4s, %3s, %19s: %p (%s) by %s",
131  (fRunNo == 0xFFFFFFFF ? -1 : fRunNo),
132  (fSys == 1 ? "pp" :
133  fSys == 2 ? "PbPb" :
134  fSys == 3 ? "pPb" :
135  fSys == 4 ? "Pbp" : "?"),
136  fSNN, fField, (fMC ? "mc" : "real"),
137  (fSatellite ? "sat" : "nom"), d.AsSQLString(), fData,
138  (fData ? fData->GetName() : "?"), fAuthor.Data());
139 
140 }
141 //____________________________________________________________________
142 void
144 {
145  Printf("%s", GetTitle());
146 }
147 //====================================================================
149  : fTree(tree), fEntry(0), fVerbose(false), fMode(mode), fFallBack(false)
150 {
151  if (!tree) return;
152 
153 #if 0
154  Info("Table", "Making table %s (%s) with mode %s (%s)",
155  tree->GetName(), (isNew ? "new" : "old"), tree->GetTitle(),
156  Mode2String(fMode));
157 #endif
158  if (isNew) {
159  fTree->Branch("e", "AliOADBForward::Entry", &fEntry);
160  fMode = String2Mode(fTree->GetTitle());
161  }
162  else {
163  if (fMode <= kDefault || fMode > kNewer) {
164  fMode = String2Mode(fTree->GetTitle());
165  if (fMode == kDefault) fMode = kNear;
166  }
167  fTree->SetBranchAddress("e", &fEntry);
168  }
169 #if 0
170  Info("", "Mode set to %d (%s)", fMode, Mode2String(fMode));
171 #endif
172 }
173 //____________________________________________________________________
175  : TObject(o),
176  fTree(o.fTree),
177  fEntry(o.fEntry),
178  fVerbose(o.fVerbose),
179  fMode(o.fMode),
180  fFallBack(o.fFallBack)
181 {
182  //
183  // Copy constructor
184  if (!fTree) return;
185  fTree->SetBranchAddress("e", &fEntry);
186 }
187 
188 //____________________________________________________________________
190 {
191  //
192  // Close this table
193  //
194  Close();
195 }
196 //____________________________________________________________________
199 {
200  //
201  // Assignment operator
202  //
203  if (this == &o) return *this;
204  fTree = o.fTree;
205  fEntry = o.fEntry;
206  fVerbose = o.fVerbose;
207  fMode = o.fMode;
208  if (fTree) fTree->SetBranchAddress("e", &fEntry);
209 
210  return *this;
211 }
212 
213 //____________________________________________________________________
214 const Char_t*
216 {
217  //
218  // Get the table name or null
219  if (!fTree) return 0;
220  return fTree->GetName();
221 }
222 //____________________________________________________________________
223 const Char_t*
225 {
226  //
227  // Overload of TObject::GetName
228  //
229  if (!fTree) return TObject::GetName();
230  return GetTableName();
231 }
232 //____________________________________________________________________
233 Bool_t
235 {
236  //
237  // Flush to disk
238  //
239  if (!IsOpen()) {
240  Error("Update", "No tree associated");
241  return false;
242  }
243 
244  TFile* file = fTree->GetCurrentFile();
245  if (!file) {
246  Error("Update", "No file associated with tree");
247  return false;
248  }
249  if (!file->IsWritable()) {
250  Error("Update", "File %s not writeable", file->GetName());
251  return false;
252  }
253 
254  Int_t nBytes = file->Write();
255 
256  return (nBytes >= 0);
257 }
258 //____________________________________________________________________
259 Bool_t
261 {
262  //
263  // Close the connection
264  //
265  if (!IsOpen()) {
266  Error("Close", "No tree associated");
267  return false;
268  }
269 
270  // if (fTree) delete fTree;
271  // if (fEntry) delete fEntry;
272  fTree = 0;
273  fEntry = 0;
274  return true;
275 }
276 //____________________________________________________________________
277 Int_t
280  UShort_t sys,
281  UShort_t sNN,
282  Short_t fld,
283  Bool_t mc,
284  Bool_t sat) const
285 {
286  //
287  // Query the tree
288  //
289  return Query(runNo, mode, Conditions(sys, sNN, fld, mc, sat));
290 }
291 
292 //____________________________________________________________________
293 Int_t
296  const TString& q) const
297 {
298  //
299  // Run a query against the table
300  //
301 
302  // Check the tree
303  if (!IsOpen()) {
304  Error("Close", "No tree associated");
305  return -1;
306  }
307 
308  TString query = q;
309  const char* smode = "latest";
310  if (runNo > 0) {
311  if (mode <= kDefault || mode > kNewer) mode = fMode;
312  smode = Mode2String(mode);
313  switch (mode) {
314  case kExact:
315  AppendToQuery(query, Form("fRunNo == %lu", runNo));
316  break;
317  case kNewest:
318  break;
319  case kNear:
320  AppendToQuery(query, Form("abs(fRunNo-%lu)<=%d",
321  runNo,kMaxNearDistance));
322  break;
323  case kOlder:
324  AppendToQuery(query, Form("fRunNo <= %lu", runNo));
325  break;
326  case kNewer:
327  AppendToQuery(query, Form("fRunNo >= %lu", runNo));
328  break;
329  case kDefault:
330  Fatal("Query", "Mode should never be 'default'");
331  break;
332  }
333  }
334 
335  if (query.IsNull()) {
336  Warning("Query", "Empty query!");
337  return -1;
338  }
339 
340  if (fVerbose)
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;
345 
346  if (fVerbose)
347  Printf("Query: %s (%s)\n"
348  " Entry | Run | Timestamp \n"
349  "--------+-----------+------------------------",
350  query.Data(), smode);
351 
352  ULong_t oldRun = (mode == kNewer ? 0xFFFFFFFF : 0);
353  ULong_t oldTim = 0;
354  ULong_t oldDist = 0xFFFFFFFF;
355  Int_t entry = -1;
356 
357  for (Int_t row = 0; row < nRows; row++) {
358  Int_t ent = Int_t(fTree->GetV1()[row]);
359  ULong_t run = ULong_t(fTree->GetV2()[row]);
360  ULong_t tim = ULong_t(fTree->GetV3()[row]);
361  ULong_t dist = (run > runNo ? run - runNo : runNo - run);
362 
363  if (fVerbose) {
364  TDatime t(tim);
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"));
368  }
369 
370  switch (mode) {
371  case kExact: break; // Done in the draw `query'
372  case kNewest: // Fall-through
373  case kOlder:
374  if (run < oldRun) continue;
375  break;
376  case kNewer:
377  if (run > oldRun) continue;
378  break;
379  case kNear:
380  if (runNo > 0 && dist > oldDist) continue;
381  break;
382  case kDefault:
383  break;
384  }
385  // If we get here, then we have the best run according to the
386  // criteria
387 
388  // Now update last values and set current best entry
389  oldTim = tim;
390  oldDist = dist;
391  oldRun = run;
392  entry = ent;
393 
394  // Finally, check the timestamp
395  if (tim < oldTim) continue;
396 
397  // Now update last values and set current best entry
398  oldTim = tim;
399  oldDist = dist;
400  oldRun = run;
401  entry = ent;
402  }
403 
404  if (fVerbose) {
405  Printf("Returning entry # %d", entry);
406  }
407  return entry;
408 }
409 
410 //____________________________________________________________________
411 Bool_t
413  ULong_t runNo,
414  UShort_t sys,
415  UShort_t sNN,
416  Short_t field,
417  Bool_t mc,
418  Bool_t sat,
419  ULong_t aliRev,
420  const TString& author)
421 {
422  //
423  // Insert a new row in the table
424  //
425 
426  // Check if the file is read/write
427  if (fVerbose)
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);
430 
431  if (!IsOpen(true)) {
432  Warning("Insert", "No tree, or not write-able");
433  return false;
434  }
435 
436  // If the entry doesn't exists
437  if (!fEntry) fEntry = new Entry;
438 
439  // Make author
440  TString auth(author);
441  if (auth.IsNull()) {
442  UserGroup_t* u = gSystem->GetUserInfo();
443  TInetAddress i = gSystem->GetHostByName(gSystem->HostName());
444  auth = TString::Format("%s <%s@%s>", u->fRealName.Data(),
445  u->fUser.Data(), i.GetHostName());
446  }
447 
448  // Set fields
449  fEntry->fData = o;
450  fEntry->fRunNo = runNo; // (runNo <= 0 ? 0xFFFFFFFF : runNo);
451  fEntry->fSys = sys;
452  fEntry->fSNN = sNN;
453  fEntry->fField = field;
454  fEntry->fMC = mc;
455  fEntry->fSatellite = sat;
456  fEntry->fAliROOTRevision = (aliRev != 0 ? aliRev : ALIROOT_SVN_REVISION);
457  fEntry->fAuthor = auth;
458 
459  TDatime now;
460  fEntry->fTimestamp = now.Convert(true);
461 
462  // Fill into tree
463  Int_t nBytes = fTree->Fill();
464  if (nBytes <= 0) {
465  Warning("Insert", "Failed to insert new entry");
466  return false;
467  }
468 
469  // do an Auto-save and flush-baskets now
470  fTree->AutoSave("FlushBaskets SaveSelf");
471 
472  return true;
473 }
474 
475 //____________________________________________________________________
476 Int_t
479  UShort_t sys,
480  UShort_t sNN,
481  Short_t fld,
482  Bool_t mc,
483  Bool_t sat) const
484 {
485  // Query the tree for an object. The strategy is as follows.
486  //
487  // - First query with all fields
488  // - If this returns a single entry, return that.
489  // - If not, then ignore the run number (if given)
490  // - If this returns a single entry, return that
491  // - If not, and fall-back is enabled, then
492  // - Ignore the collision energy (if given)
493  // - If this returns a single entry, return that.
494  // - If not, ignore all passed values
495  // - If this returns a single entry, return that.
496  // - Otherwise, give up and return -1
497  // - Otherwise, give up and return -1
498  //
499  // This allow us to specify default objects for a period, and for
500  // collision system, energy, and field setting.
501  //
502 
503  if (fVerbose)
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);
509  if (entry < 0 && fFallBack && fld != kInvalidField) {
510  if (fVerbose)
511  Printf("Fall-back enabled, trying without field=%d", fld);
512  entry = Query(run, mode, sys, sNN, fld, mc, sat);
513  }
514  if (entry < 0 && fFallBack && sNN > 0) {
515  if (fVerbose)
516  Printf("Fall-back enabled, will try without sNN=%d", sNN);
517  entry = Query(run, mode, sys, 0, fld, mc, sat);
518  }
519  if (entry < 0 && fFallBack) {
520  if (fVerbose)
521  Printf("Fall-back enabled, will try without any fields");
522  entry = Query(0, mode, 0, 0, kInvalidField, false, false);
523  }
524  if (entry < 0) {
525  Warning("Get", "No valid object could be found");
526  return -1;
527  }
528  return entry;
529 }
530 
531 //____________________________________________________________________
535  UShort_t sys,
536  UShort_t sNN,
537  Short_t fld,
538  Bool_t mc,
539  Bool_t sat) const
540 {
541  // Query the tree for an object. The strategy is as follows.
542  //
543  // - First query with all fields
544  // - If this returns a single entry, return that.
545  // - If not, then ignore the run number (if given)
546  // - If this returns a single entry, return that
547  // - If not, and fall-back is enabled, then
548  // - Ignore the collision energy (if given)
549  // - If this returns a single entry, return that.
550  // - If not, ignore all passed values
551  // - If this returns a single entry, return that.
552  // - Otherwise, give up and return null
553  // - Otherwise, give up and return null
554  //
555  // This allow us to specify default objects for a period, and for
556  // collision system, energy, and field setting.
557  //
558  Int_t entry = GetEntry(run, mode, sys, sNN, fld, mc, sat);
559  if (entry < 0) return 0;
560 
561  Int_t nBytes = fTree->GetEntry(entry);
562  if (nBytes <= 0) {
563  Warning("Get", "Failed to get entry # %d\n", entry);
564  return 0;
565  }
566  if (fVerbose) fEntry->Print();
567  return fEntry;
568 }
569 //____________________________________________________________________
570 TObject*
573  UShort_t sys,
574  UShort_t sNN,
575  Short_t fld,
576  Bool_t mc,
577  Bool_t sat) const
578 {
579  //
580  // Get data associated with a row or null.
581  // See also AliOADBForward::Get
582  //
583  Entry* e = Get(run, mode, sys, sNN, fld, mc, sat);
584  if (!e) return 0;
585  return e->fData;
586 }
587 //____________________________________________________________________
588 void
590 {
591  //
592  // Print the full table
593  //
594  if (!IsOpen()) return;
595 
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++) {
599  fTree->GetEntry(i);
600  printf("%4d/%4d: ", i, n);
601  fEntry->Print(option);
602  }
603 }
604 //____________________________________________________________________
605 void
607 {
608  // Browse this table
609  if (fTree) b->Add(fTree);
610 }
611 //____________________________________________________________________
612 Bool_t
614 {
615  if (!fTree) return false;
616  if (!rw) return true;
617 
618  return fTree->GetCurrentFile()->IsWritable();
619 }
620 //====================================================================
622  : TObject(),
623  fTables()
624 {
625  //
626  // Constructor
627  //
628 }
629 #if 0
630 //____________________________________________________________________
632  : TObject(other),
633  fTables(other.fTables)
634 {
635  //
636  // Copy constructor
637  //
638 }
639 #endif
640 //____________________________________________________________________
642 {
643  //
644  // Destructor
645  //
646  Close();
647 }
648 #if 0
649 //____________________________________________________________________
651 AliOADBForward::operator=(const AliOADBForward& other)
652 {
653  //
654  // Copy constructor
655  //
656  if (&other == this) return *this;
657 
658  fTables = other.fTables;
659 
660  return *this;
661 }
662 #endif
663 
664 //____________________________________________________________________
665 Bool_t
667  const TString& tables,
668  Bool_t rw,
669  Bool_t verb,
670  Bool_t fallback)
671 {
672  TString absPath(gSystem->ExpandPathName(fileName));
673  if (absPath.IsNull()) {
674  Error("Open", "Empty path for tables %s", tables.Data());
675  return false;
676  }
677  TObject* previous = gROOT->GetListOfFiles()->FindObject(absPath);
678  TFile* file = 0;
679  if (previous) {
680  file = static_cast<TFile*>(previous);
681  }
682  else {
683  file = TFile::Open(fileName, (rw ? "UPDATE" : "READ"));
684  }
685  if (!file) {
686  Error("Open", "Failed to open %s", GetName());
687  return false;
688  }
689  return Open(file, tables, rw, verb, fallback);
690 }
691 
692 //____________________________________________________________________
693 Bool_t
695  const TString& tables,
696  Bool_t rw,
697  Bool_t verb,
698  Bool_t fallback)
699 {
700  //
701  // Open database file and find or create listed tables
702  //
703  if (!file) return false;
704  if (rw && !file->IsWritable()) {
705  Warning("Open", "Read+write access requested, but %s opened read-only",
706  file->GetName());
707  if (file->ReOpen("UPDATE") < 0) {
708  Error("Open", "Failed to reopen file in read+write access mode");
709  return false;
710  }
711  }
712 
713  if (tables.EqualTo("*")) {
714  if (rw) {
715  Error("Open", "Cannot open with '*' in read/write mode");
716  // return false;
717  }
718  TList* l = file->GetListOfKeys();
719  TIter next(l);
720  TKey* key = 0;
721  while ((key = static_cast<TKey*>(next()))) {
722  TClass* cl = gROOT->GetClass(key->GetClassName());
723  if (!cl) continue;
724  if (!cl->InheritsFrom(TTree::Class())) continue;
725 
726  OpenTable(file, rw, key->GetName(), "DEFAULT", verb, fallback);
727  }
728  // file->Close();
729  return true;
730  }
731  TObjArray* tokens = tables.Tokenize(":,");
732  TObjString* token = 0;
733  TIter nextToken(tokens);
734  while ((token = static_cast<TObjString*>(nextToken()))) {
735  TString& tn = token->String();
736  if (tn.IsNull()) continue;
737 
738  TObjArray* parts = tn.Tokenize("/");
739  TObjString* onam = static_cast<TObjString*>(parts->At(0));
740  TString& name = onam->String();
741  TString mode = "DEFAULT";
742  if (parts->GetEntries() > 1)
743  mode = static_cast<TObjString*>(parts->At(1))->String();
744  mode.ToUpper();
745 
746  OpenTable(file, rw, name, mode, verb, fallback);
747 
748  delete parts;
749  }
750  delete tokens;
751 
752  return true;
753 }
754 
755 //____________________________________________________________________
756 Bool_t
758 {
759  //
760  // Flush all tables and close all files
761  //
762  TList files;
763  Int_t nFiles = GetFiles(files);
764  if (nFiles <= 0) {
765  // Nothing to close
766  return true;
767  }
768 
769  TIter nextFile(&files);
770  TFile* file = 0;
771  while ((file = static_cast<TFile*>(nextFile()))) {
772  // if (file->IsWritable()) file->Write();
773 
774  file->Close();
775  }
776 
777  fTables.DeleteAll();
778 
779  return true;
780 }
781 //____________________________________________________________________
782 Bool_t
784 {
785  //
786  // Flush all tables
787  //
788  TList files;
789  Int_t nFiles = GetFiles(files);
790  if (nFiles <= 0) {
791  // Nothing to close
792  return true;
793  }
794 
795  TIter nextFile(&files);
796  TFile* file = 0;
797  Int_t nBytes = 0;
798  while ((file = static_cast<TFile*>(nextFile()))) {
799  if (!file->IsWritable()) {
800  Error("Update", "File %s not writeable", file->GetName());
801  continue;
802  }
803 
804  nBytes += file->Write();
805  }
806  return (nBytes >= 0);
807 }
808 //____________________________________________________________________
811  ULong_t run,
813  UShort_t sys,
814  UShort_t sNN,
815  Short_t fld,
816  Bool_t mc,
817  Bool_t sat) const
818 {
819  //
820  // Get a row from selected table
821  //
822  Table* t = FindTable(table);
823  if (!t) return 0;
824 
825  return t->Get(run, mode, sys, sNN, fld, mc, sat);
826 }
827 //____________________________________________________________________
828 TObject*
830  ULong_t run,
832  UShort_t sys,
833  UShort_t sNN,
834  Short_t fld,
835  Bool_t mc,
836  Bool_t sat) const
837 {
838  Table* t = FindTable(table);
839  if (!t) return 0;
840 
841  return t->GetData(run, mode, sys, sNN, fld, mc, sat);
842 }
843 //____________________________________________________________________
844 Bool_t
846  TObject* o,
847  ULong_t runNo,
848  UShort_t sys,
849  UShort_t sNN,
850  Short_t field,
851  Bool_t mc,
852  Bool_t sat,
853  ULong_t aliRev,
854  const TString& author)
855 {
856  //
857  // Insert a row into the selected table
858  //
859  Table* t = FindTable(table);
860  if (!t) return false;
861 
862  return t->Insert(o, runNo, sys, sNN, field, mc, sat, aliRev, author);
863 }
864 //____________________________________________________________________
865 Bool_t
867  ULong_t oldRunNo,
868  UShort_t oldSys,
869  UShort_t oldSNN,
870  Short_t oldField,
871  ULong_t newRunNo,
872  UShort_t newSys,
873  UShort_t newSNN,
874  Short_t newField,
875  Bool_t mc,
876  Bool_t sat)
877 {
878  Table* t = FindTable(table);
879  if (!t) return false;
880 
881  Printf("Copy entry: table mode: %s", Mode2String(t->fMode));
882  Entry* e = t->Get(oldRunNo, t->fMode, oldSys, oldSNN, oldField, mc, sat);
883  if (!e) return false;
884 
885  return t->Insert(e->fData, newRunNo, newSys, newSNN, newField, mc, sat,
886  e->fAliROOTRevision, e->fAuthor);
887 }
888 
889 //____________________________________________________________________
890 void
891 AliOADBForward::Print(const Option_t* option) const
892 {
893  //
894  // Print everything
895  //
896  TIter nextTable(&fTables);
897  TObjString* key = 0;
898  Table* table = 0;
899  while ((key = static_cast<TObjString*>(nextTable()))) {
900  Printf("Table: %p", key->GetName());
901  table = static_cast<Table*>(fTables.GetValue(key));
902  if (!table) continue;
903  table->Print(option);
904  }
905 }
906 
907 //____________________________________________________________________
908 void
910 {
911  //
912  // Browse this object
913  //
914  TIter nextTable(&fTables);
915  TObjString* key = 0;
916  Table* table = 0;
917  while ((key = static_cast<TObjString*>(nextTable()))) {
918  table = static_cast<Table*>(fTables.GetValue(key));
919  if (!table) continue;
920  b->Add(table, key->GetName());
921  }
922 }
923 //____________________________________________________________________
925 AliOADBForward::FindTable(const TString& name, Bool_t quite) const
926 {
927  //
928  // Find a table by name
929  //
930  TPair* p = static_cast<TPair*>(fTables.FindObject(name));
931  if (!p) {
932  if (!quite)
933  Warning("FindTable", "Table %s not registered", name.Data());
934  return 0;
935  }
936  return static_cast<Table*>(p->Value());
937 }
938 //____________________________________________________________________
939 Int_t
941 {
942  //
943  // Get all associated files
944  //
945  Int_t ret = 0;
946  TIter nextTable(&fTables);
947  TObjString* key = 0;
948  Table* table = 0;
949  while ((key = static_cast<TObjString*>(nextTable()))) {
950  table = static_cast<Table*>(fTables.GetValue(key));
951  if (!table->fTree) continue;
952 
953  TFile* f = table->fTree->GetCurrentFile();
954  if (!f) continue;
955 
956  if (files.FindObject(f)) continue;
957  files.Add(f);
958  ret++;
959  }
960  return ret;
961 }
962 //____________________________________________________________________
965  Bool_t rw,
966  const TString& name,
967  const TString& mode) const
968 {
969  //
970  // Get a table from a file, or make a new table
971  //
972  if (!file) return 0;
973  if (FindTable(name, true)) return 0;
974 
975  TObject* o = file->Get(name);
976  TTree* t = 0;
977  Bool_t n = false;
978  if (!o) {
979  if (!rw) {
980  // We only fail if in read-only mode
981  Error("Open", "No such object: %s in %s", name.Data(),
982  file->GetName());
983  return 0;
984  }
985  TDirectory* savDir = gDirectory;
986  file->cd();
987  // Create the tree in the file
988  t = new TTree(name, mode);
989  t->SetDirectory(file);
990  if (savDir) savDir->cd();
991  n = true;
992  }
993  else {
994  // Get the tree, and set the branch
995  t = static_cast<TTree*>(o);
996  }
997  Table* ret = new Table(t, n, String2Mode(mode));
998  return ret;
999 }
1000 
1001 //____________________________________________________________________
1002 void
1004  Bool_t rw,
1005  const TString& name,
1006  const TString& mode,
1007  Bool_t verb,
1008  Bool_t fallback)
1009 {
1010  if (!file) return;
1011 
1012  Table* t = GetTableFromFile(file, rw, name, mode);
1013  if (!t) return;
1014 
1015  fTables.Add(new TObjString(name), t);
1016  t->SetVerbose(verb);
1017  t->SetEnableFallBack(fallback);
1018  if (verb)
1019  Printf("Found table %s. Opened with verbose=%d, fallback=%d",
1020  name.Data(), verb, fallback);
1021 }
1022 
1023 //____________________________________________________________________
1024 void
1026 {
1027  //
1028  // Helper function
1029  //
1030  if (!q.IsNull()) q.Append(andNotOr ? " && " : " || ");
1031  q.Append(s);
1032 }
1033 //____________________________________________________________________
1034 TString
1036  UShort_t sNN,
1037  Short_t fld,
1038  Bool_t mc,
1039  Bool_t sat)
1040 {
1041  // Build query string
1042  TString q;
1043  if (sys > 0) AppendToQuery(q, Form("fSys == %hu", sys));
1044  if (sNN > 0) AppendToQuery(q, Form("abs(fSNN - %hu) < 11", sNN));
1045  if (TMath::Abs(fld) < 10) AppendToQuery(q, Form("fField == %hd",fld));
1046  // Boolean fields always queried. In cases where these do not matter,
1047  // we always write down the false value, so we get the correct query
1048  // anyways.
1049  AppendToQuery(q, Form("%sfMC", (mc ? " " : "!")));
1050  AppendToQuery(q, Form("%sfSatellite", (sat ? " " : "!")));
1051 
1052  return q;
1053 }
1054 
1055 //____________________________________________________________________
1056 void
1058  const TString& table,
1059  ULong_t runNo,
1061  UShort_t sys,
1062  UShort_t sNN,
1063  Short_t fld,
1064  Bool_t mc,
1065  Bool_t sat)
1066 {
1067 
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));
1070  AliOADBForward::Entry* e = t.Get(table, runNo, mode, sys, sNN,
1071  fld, mc, sat);
1072  if (!e) return;
1073  e->Print();
1074 }
1075 //____________________________________________________________________
1076 void
1078  const TString& table,
1079  ULong_t runNo,
1080  UShort_t sys,
1081  UShort_t sNN,
1082  Short_t fld,
1083  Bool_t mc,
1084  Bool_t sat)
1085 {
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);
1091  gSystem->Sleep(500);
1092 }
1093 
1094 //____________________________________________________________________
1095 void
1097 {
1098  AliOADBForward* tt = new AliOADBForward();
1099  if (!tt->Open("db.root", "A,B", true, true)) {
1100  ::Error("Test", "Failed to open DB");
1101  return;
1102  }
1103  AliOADBForward& t = *tt;
1104  TestInsert(t, "A", 137161);
1105  TestInsert(t, "A", 137161);
1106  TestInsert(t, "A", 0 );
1107  TestInsert(t, "A", 999999);
1108  TestInsert(t, "A", 137166);
1109 
1110 
1111  TestInsert(t, "B", 137161);
1112  TestInsert(t, "B", 0 );
1113  t.Print();
1114  t.Close();
1115 
1116  if (!t.Open("db.root", "A,B",false,true)) {
1117  ::Error("Test", "Failed to open DB");
1118  return;
1119  }
1120 
1121  TestGet(t, "A", 137161);
1122  TestGet(t, "A", 137160);
1123  TestGet(t, "A", 0 );
1124  TestGet(t, "A", 137160, kNewest);
1125  TestGet(t, "A", 137160, kNewer);
1126  TestGet(t, "A", 137168, kOlder);
1127  TestGet(t, "A", 137161, kExact);
1128 
1129  new TBrowser("b", tt);
1130 }
1131 
1132 //
1133 // EOF
1134 //
static const char * Mode2String(ERunSelectMode mode)
void Browse(TBrowser *b)
return jsonbuilder str().c_str()
ULong_t fAliROOTRevision
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
UShort_t fSys
void Print(Option_t *option="") const
UShort_t fSNN
TString fileName
void OpenTable(TFile *file, Bool_t rw, const TString &name, const TString &mode, Bool_t verb, Bool_t fallback)
TSystem * gSystem
static void AppendToQuery(TString &q, const TString &s, Bool_t andNotOr=true)
char Char_t
Definition: External.C:18
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)
static void Test()
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)
TString fAuthor
static ERunSelectMode String2Mode(const TString &str)
ULong_t fRunNo
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)
int Int_t
Definition: External.C:63
Bool_t fMC
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="")
unsigned long ULong_t
Definition: External.C:38
Int_t mode
Definition: anaM.C:40
const char * GetTitle() const
Bool_t fSatellite
void Print(Option_t *option="") const
short Short_t
Definition: External.C:23
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)
void Browse(TBrowser *b)
Entry & operator=(const Entry &o)
TFile * file
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
unsigned short UShort_t
Definition: External.C:28
static ERunSelectMode Int2Mode(Int_t mode)
const char Option_t
Definition: External.C:48
UInt_t fTimestamp
const Char_t * GetTableName() const
ERunSelectMode fMode
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 Bool_t
Definition: External.C:53
Short_t fField
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 * fData
ClassImp(AliOADBForward) const char *AliOADBForward
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