]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWGLF/FORWARD/analysis2/AliOADBForward.h
merging trunk to TPCdev
[u/mrichter/AliRoot.git] / PWGLF / FORWARD / analysis2 / AliOADBForward.h
1 // -*- mode: C++ -*-
2 #ifndef ALIOADBFORWARD_H
3 #define ALIOADBFORWARD_H
4 #include <TNamed.h>
5 #include <TString.h>
6 #include <TMap.h>
7 class TFile;
8 class TTree;
9 class TBrowser;
10 class TList;
11
12 /**
13  * Container/handler of Forward calibration objects. 
14  */
15 class AliOADBForward : public TObject
16 {
17 public:
18   //=== Options ======================================================
19   /**
20    * Options for selecting the entries according to the run number
21    * given in the query. 
22    */
23   enum ERunSelectMode { 
24     kDefault = 0,
25     /** 
26      * Select only entries from exactly the run given. 
27      */
28     kExact = 1,
29     /** 
30      * Select the entry with the largest run number 
31      */
32     kNewest = 2,
33     /**
34      * Select entries from the run nearest (possibly constrained to be
35      * older or newer) to the run given in the query 
36      */ 
37     kNear = 3, 
38     /** 
39      * Select entries from runs that are older than the run given in
40      * the query. The oldest entry with the largest run number
41      * smaller than the given run is selected.  
42      */
43     kOlder = 4, 
44     /**
45      * Select entries from runs that are newer than the run given.
46      * Select the entries with the smallest run number which is larger
47      * than the given run.
48      */
49     kNewer = 5
50   };
51   static const char* Mode2String(ERunSelectMode mode);
52   static ERunSelectMode String2Mode(const TString& str);
53   static ERunSelectMode Int2Mode(Int_t mode);
54   /**
55    * Various flags
56    * 
57    */
58   enum { 
59     /** 
60      * The maximum distance to the given run when selecting in kNear
61      * mode.  Currently this is set to a large number to allow
62      * selection of any entry.  This should change 
63      */
64     kMaxNearDistance = 1000000,
65     kInvalidField    = 999
66   };
67   //=== Entry ========================================================
68   /**
69    * An entry in the FORWARD OADB database tables
70    * 
71    */
72   class Entry : public TObject
73   {
74   public:
75     /** 
76      * Constructor 
77      * 
78      * @param runNo   Run number 
79      * @param sys     Collision system (1:pp, 2:pbpb, 3:ppb) 
80      * @param sNN     Center of mass energy (GeV)
81      * @param field   L3 magnetic field (kG)
82      * @param mc      True if for MC only
83      * @param o       Correction object
84      */
85     Entry(ULong_t  runNo  = 0, 
86           UShort_t sys    = 0, 
87           UShort_t sNN    = 0, 
88           Short_t  field  = 0, 
89           Bool_t   mc     = false, 
90           Bool_t   sat    = false,
91           TObject* o      = 0);
92     /** 
93      * Copy constructor
94      * 
95      * @param o Object to copy from
96      */
97     Entry(const Entry& o);
98     /** 
99      * Destructor 
100      */
101     virtual ~Entry() {}
102     /** 
103      * Assignment operator 
104      * 
105      * @param o Object to assign from 
106      * 
107      * @return Reference to this object
108      */
109     Entry& operator=(const Entry& o);
110     /** 
111      * Get the title of the entry 
112      * 
113      * @return stringified fields
114      */
115     const char* GetTitle() const;
116     /** 
117      * Print this entry 
118      * 
119      * @param option Not used 
120      */
121     void Print(Option_t* option="") const; //*MENU*
122     ULong_t  fRunNo;           // Run number 
123     UShort_t fSys;             // Collision system (1: pp, 2: pbpb, 3: ppb)
124     UShort_t fSNN;             // Center of mass energy 
125     Short_t  fField;           // L3 magnetic field
126     Bool_t   fMC;              // True if only for MC 
127     Bool_t   fSatellite;       // Satelitte events
128     TObject* fData;            // Correction object
129     UInt_t   fTimestamp;       // When the object was stored 
130     ULong_t  fAliROOTRevision; // Revision of AliROOT used 
131     TString  fAuthor;          // Author of calibration/correction
132
133     ClassDef(Entry,2); // Entry in PWGLF/Forward OADB
134   };
135
136   //=== Table ========================================================
137   /** 
138    * A table on the Forward OADB - the underlying storage is a TTree
139    * containing Entry objects.
140    * 
141    */
142   class Table : public TObject
143   {
144   public:
145     /** 
146      * Constructor 
147      * 
148      * @param tree   Tree 
149      * @param isNew  Whether to make the branch 
150      * 
151      * @return 
152      */    
153     Table(TTree* tree, Bool_t isNew, ERunSelectMode mode=kNear);
154     /** 
155      * Copy constructor 
156      * 
157      * @param other Object to copy from 
158      */
159     Table(const Table& other);
160     /** 
161      * Destructor. 
162      *
163      * Closes the corresponding file 
164      */
165     ~Table();
166     /** 
167      * Assignemt operator
168      * 
169      * @param other Object to assign form 
170      * 
171      * @return Reference to this object 
172      */
173     Table& operator=(const Table& other);
174     /** 
175      * Set the verbosity
176      * 
177      * @param verb 
178      */
179     void SetVerbose(Bool_t verb=true) { fVerbose = verb; }
180     /** 
181      * Set wheter to enable fall-back queries  
182      * 
183      * @param use If true, enable fall-back queries
184      */
185     void SetEnableFallBack(Bool_t use=true) { fFallBack = use; }
186     // -----------------------------------------------------------------
187     /** 
188      * Get the name of the tree 
189      * 
190      * @return Table name 
191      */
192     const Char_t* GetTableName() const;
193     /** 
194      * Overload TObject::GetName 
195      * 
196      * @return Name 
197      */
198     const Char_t* GetName() const;
199
200     // -----------------------------------------------------------------
201     /** 
202      * @{ 
203      * @name Open/close/flush 
204      */
205     /** 
206      * Flush to disk 
207      * 
208      * @return true on success
209      */
210     Bool_t Update();
211     /** 
212      * Close connection
213      * 
214      * @return true on success 
215      */
216     Bool_t Close();
217     /* @} */
218     // -----------------------------------------------------------------
219     /** 
220      * @{ 
221      * @name Queries 
222      */
223     /** 
224      * Query the tree 
225      * 
226      * @param run    Run number 
227      * @param sys    Collision system (1: pp, 2: PbPb, 3: pPb)
228      * @param sNN    Center of mass energy (GeV)
229      * @param fld    L3 magnetic field (kG)
230      * @param mc     For MC only 
231      * @param sat    For satellite events
232      * @param mode   How to select on the run number 
233      * 
234      * @return Found entry number or negative number in case of problems
235      */
236     Int_t Query(ULong_t        runNo  = 0,
237                 ERunSelectMode mode   = kNear,
238                 UShort_t       sys    = 0,
239                 UShort_t       sNN    = 0, 
240                 Short_t        fld    = kInvalidField,
241                 Bool_t         mc     = false,
242                 Bool_t         sat    = false) const;
243     /** 
244      * Run a query with pre-build conditions
245      * 
246      * @param q     query string 
247      * @param runNo The given run number 
248      * @param mode  Run selection mode 
249      * 
250      * @return Entry number of selected entry 
251      */
252     Int_t Query(ULong_t        runNo,
253                 ERunSelectMode mode,
254                 const TString& q) const;
255     /** 
256      * Insert a new entry into the tree 
257      * 
258      * @param o         Object to write 
259      * @param runNo     Run number 
260      * @param sys       Collision system (1: pp, 2:PbPb, 3:pPb)
261      * @param sNN       Center of mass energy (GeV)
262      * @param field     L3 magnetic field (kG)
263      * @param mc        If true, only for MC 
264      * @param aliRev    AliROOT revision
265      * 
266      * @return true on success 
267      */
268     Bool_t Insert(TObject* o, 
269                   ULong_t  runNo, 
270                   UShort_t sys, 
271                   UShort_t sNN, 
272                   Short_t  field, 
273                   Bool_t   mc=false, 
274                   Bool_t   sat=false,
275                   ULong_t  aliRev=0,
276                   const TString& author="");
277     /** 
278      * Query the tree for an object.  The strategy is as follows. 
279      * 
280      *  - First query with all fields 
281      *    - If this returns a single entry, return that. 
282      *    - If not, then ignore the run number (if given)
283      *      - If this returns a single entry, return that 
284      *      - If not, and fall-back is enabled, then 
285      *        - Ignore the collision energy (if given) 
286      *          - If this returns a single entry, return that.  
287      *          - If not, ignore all passed values 
288      *            - If this returns a single entry, return that.
289      *            - Otherwise, give up and return null
290      *      - Otherwise, give up and return null
291      *
292      * This allow us to specify default objects for a period, and for
293      * collision system, energy, and field setting.
294      *
295      * @param run    Run number 
296      * @param sys    Collision system (1: pp, 2: PbPb, 3: pPb)
297      * @param sNN    Center of mass energy (GeV)
298      * @param fld    L3 magnetic field (kG)
299      * @param mc     For MC only 
300      * 
301      * @return Found entry, or null
302      */
303     Entry* Get(ULong_t        run      = 0,
304                ERunSelectMode mode     = kNear,
305                UShort_t       sys      = 0,
306                UShort_t       sNN      = 0, 
307                Short_t        fld      = 0,
308                Bool_t         mc       = false,
309                Bool_t         sat      = false) const;
310     /** 
311      * Query the tree for an object.  The strategy is as follows. 
312      * 
313      *  - First query with all fields 
314      *    - If this returns a single entry, return that. 
315      *    - If not, then ignore the run number (if given)
316      *      - If this returns a single entry, return that 
317      *      - Otherwise, give up and return null
318      *
319      * This allow us to specify default objects for a period, and for
320      * collision system, energy, and field setting.
321      *
322      * @param run    Run number 
323      * @param sys    Collision system (1: pp, 2: PbPb, 3: pPb)
324      * @param sNN    Center of mass energy (GeV)
325      * @param fld    L3 magnetic field (kG)
326      * @param mc     For MC only 
327      * 
328      * @return Found data, or null
329      */
330     TObject* GetData(ULong_t        run      = 0,
331                      ERunSelectMode mode     = kNear,
332                      UShort_t       sys      = 0,
333                      UShort_t       sNN      = 0, 
334                      Short_t        fld      = 0,
335                      Bool_t         mc       = false,
336                      Bool_t         sat      = false) const;
337     /* @} */
338     /** 
339      * Print the contents of the tree 
340      * 
341      * @param option Passed to TTree::Scan
342      */
343     void Print(Option_t* option="") const; //*MENU*
344     /** 
345      * Browse this table 
346      */
347     void Browse(TBrowser* b); 
348     /** 
349      * Check if the tree was opened. 
350      * 
351      * @param rw If true, also check if the file is read/write 
352      *
353      * @return true if everything is dandy
354      */
355     Bool_t IsOpen(Bool_t rw=false) const; 
356
357     TTree*         fTree;     // Our tree
358     Entry*         fEntry;    // Entry cache 
359     Bool_t         fVerbose;  // To be verbose or not 
360     ERunSelectMode fMode;     // Run query mode 
361     Bool_t         fFallBack; // Enable fall-back
362
363     ClassDef(Table,1); 
364   };
365   // === Interface ===================================================
366   /** 
367    * Constructor 
368    * 
369    * @param file File that stores the DB
370    */
371   AliOADBForward();
372   /**
373    * Destructor
374    * 
375    */
376   ~AliOADBForward();
377   // --- Open/close/update -------------------------------------------
378   /** 
379    * @{ 
380    * @name Input/output 
381    */
382   /** 
383    * Open a file containing tables.  Note, this member function can be
384    * called multiple times to open tables in different files.
385    * 
386    * If a table is already associated with this handler, it will not
387    * be re-associated.
388    *
389    * @param fileName  Path to file to get/write tables from/in
390    * @param rw        if true, open read+write, otherwise read-only
391    * @param tables    Tables to open 
392    * @param verb      Verbosity flag 
393    * 
394    * @return true on success 
395    */
396   Bool_t Open(const TString& fileName, 
397               const TString& tables  = "*", 
398               Bool_t         rw      = false, 
399               Bool_t         verb    = false,
400               Bool_t         fallback= false);
401   /** 
402    * Open a file containing tables.  Note, this member function can be
403    * called multiple times to open tables in different files.
404    *
405    * If a table is already associated with this handler, it will not
406    * be re-associated.
407    * 
408    * @param file    File to get/write tables from/in
409    * @param rw      if true, open read+write, otherwise read-only
410    * @param tables  Tables to open 
411    * @param verb    Verbosity flag 
412    * 
413    * @return true on success 
414    */
415   Bool_t Open(TFile*         file,
416               const TString& tables, 
417               Bool_t         rw      = false, 
418               Bool_t         verb    = false,
419               Bool_t         fallback= false);
420   /** 
421    * Close this database 
422    * 
423    * 
424    * @return true on success
425    */
426   Bool_t Close();
427   /** 
428    * Flush to disk 
429    * 
430    * @return true on success
431    */
432   Bool_t Update();
433   /* @} */
434   // --- Queries -----------------------------------------------------
435   /** 
436    * @{ 
437    * @name Queries 
438    */
439   /** 
440    * Query the table for an object.  The strategy is as follows. 
441    * 
442    *  - First query with all fields 
443    *    - If this returns a single entry, return that. 
444    *    - If not, then ignore the run number (if given)
445    *      - If this returns a single entry, return that 
446    *      - Otherwise, give up and return null
447    *
448    * This allow us to specify default objects for a period, and for
449    * collision system, energy, and field setting.
450    *
451    * @param run    Run number 
452    * @param sys    Collision system (1: pp, 2: PbPb, 3: pPb)
453    * @param sNN    Center of mass energy (GeV)
454    * @param fld    L3 magnetic field (kG)
455    * @param mc     For MC only 
456    * 
457    * @return Found entry, or null
458    */
459   Entry* Get(const TString& table, 
460              ULong_t        run        = 0,
461              ERunSelectMode mode       = kNear, 
462              UShort_t       sys        = 0,
463              UShort_t       sNN        = 0, 
464              Short_t        fld        = 0,
465              Bool_t         mc         = false,
466              Bool_t         sat        = false) const;
467   /** 
468    * Query the table for an object.  The strategy is as follows. 
469    * 
470    *  - First query with all fields 
471    *    - If this returns a single entry, return that. 
472    *    - If not, then ignore the run number (if given)
473    *      - If this returns a single entry, return that 
474    *      - Otherwise, give up and return null
475    *
476    * This allow us to specify default objects for a period, and for
477    * collision system, energy, and field setting.
478    *
479    * @param run    Run number 
480    * @param sys    Collision system (1: pp, 2: PbPb, 3: pPb)
481    * @param sNN    Center of mass energy (GeV)
482    * @param fld    L3 magnetic field (kG)
483    * @param mc     For MC only 
484    * 
485    * @return Found data, or null
486    */
487   TObject* GetData(const TString& table, 
488                    ULong_t        run      = 0,
489                    ERunSelectMode mode     = kNear, 
490                    UShort_t       sys      = 0,
491                    UShort_t       sNN      = 0, 
492                    Short_t        fld      = 0,
493                    Bool_t         mc       = false,
494                    Bool_t         sat      = false) const;
495   // --- Insert ------------------------------------------------------
496   /** 
497    * Insert a new entry into the table 
498    * 
499    * @param o         Object to write 
500    * @param runNo     Run number 
501    * @param sys       Collision system (1: pp, 2:PbPb, 3:pPb)
502    * @param sNN       Center of mass energy (GeV)
503    * @param field     L3 magnetic field (kG)
504    * @param mc        If true, only for MC 
505    * @param aliRev    AliROOT revision
506    * 
507    * @return true on success 
508    */
509   Bool_t Insert(const TString& table, 
510                 TObject*       o, 
511                 ULong_t        runNo, 
512                 UShort_t       sys, 
513                 UShort_t       sNN, 
514                 Short_t        field, 
515                 Bool_t         mc=false, 
516                 Bool_t         sat=false,
517                 ULong_t        aliRev=0,
518                 const TString& author="");
519   /* @} */
520   /** 
521    * Print the content of all tables
522    * 
523    * @param option Passed on to tables 
524    */  
525   void Print(const Option_t* option="") const; //*MENU*
526   /** 
527    * Browse this database
528    * 
529    * @param b Browser 
530    */
531   void Browse(TBrowser* b); 
532   /** 
533    * Find a table by name 
534    * 
535    * @param name  Name of table 
536    * @param quite Do not print warning if not found 
537    * @return Table or null 
538    */
539   Table* FindTable(const TString& name, Bool_t quite=false) const;
540   /** 
541    * Get all tables 
542    * 
543    * 
544    * @return Map of all tables
545    */
546   const TMap& GetTables() const { return fTables; }
547 protected:
548   /** 
549    * Get a list of associated files 
550    * 
551    * @param files On return, contains list of files 
552    * 
553    * @return Number of associated files found 
554    */
555   Int_t GetFiles(TList& files) const;
556   /** 
557    * Get a table (TTree) from a file 
558    * 
559    * @param file   File to look in 
560    * @param rw     If true, open read/write, false read-only
561    * @param name   Name of the table 
562    * @param mode   Default mode for new table, or override mode 
563    *               for existing tables if not default
564    * 
565    * @return Table or (if rw=true) possibly newly created table
566    */
567   Table* GetTableFromFile(TFile* file, Bool_t rw, 
568                           const TString& name,
569                           const TString& mode) const;
570   /** 
571    * Helper function to append to query string 
572    * 
573    * @param q         String to attach to
574    * @param s         What to attach 
575    * @param andNotOr  If true, assume @b and, otherwise @b or
576    */
577   static void AppendToQuery(TString& q, const TString& s, Bool_t andNotOr=true);
578   /** 
579    * Helper function to build a query string 
580    * 
581    * @param sys    Collision system (1:pp, 2: PbPb, 3:pPb)
582    * @param sNN    Collision energy in GeV
583    * @param fld    L3-Field strength and polarity 
584    * @param mc     For Monte-carlo
585    * @param sat    For satelitte collisions 
586    * 
587    * @return Query string 
588    */
589   static TString Conditions(UShort_t       sys    = 0,
590                             UShort_t       sNN    = 0, 
591                             Short_t        fld    = kInvalidField,
592                             Bool_t         mc     = false,
593                             Bool_t         sat    = false);
594   TMap fTables;
595   ClassDef(AliOADBForward,0); // PWGLF/Forward OADB interface
596
597 public:
598   // =================================================================
599   /** 
600    * @{ 
601    * @name Tests
602    */
603   static void TestGet(AliOADBForward& t, 
604                       const TString& table,
605                       ULong_t  runNo      = 0,
606                       ERunSelectMode mode = kNear,
607                       UShort_t sys        = 2,
608                       UShort_t sNN        = 2760, 
609                       Short_t  fld        = -5,
610                       Bool_t   mc         = false,
611                       Bool_t   sat        = false);
612   static void TestInsert(AliOADBForward& t, 
613                          const TString&  table,
614                          ULong_t         runNo  = 0,
615                          UShort_t        sys    = 2,
616                          UShort_t        sNN    = 2760, 
617                          Short_t         fld    = -5,
618                          Bool_t          mc     = false,
619                          Bool_t          sat    = false);
620   static void Test();
621   /* @} */
622 };
623
624 #endif
625 /* Local Variables:
626  *  mode: C++
627  * End:
628  */