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