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