]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWGLF/FORWARD/analysis2/AliCorrectionManagerBase.h
Mega commit of many changes to PWGLFforward
[u/mrichter/AliRoot.git] / PWGLF / FORWARD / analysis2 / AliCorrectionManagerBase.h
1 // -*- mode: C++ -*-
2 /**
3  * @file   AliCorrectionManagerBase.h
4  * @author Christian Holm Christensen <cholm@master.hehi.nbi.dk>
5  * @date   Sun May 19 21:56:13 2013
6  * 
7  * @brief  Base class for correction managers
8  * 
9  * 
10  */
11 #ifndef ALICORRECTIONMANAGERBASE_H
12 #define ALICORRECTIONMANAGERBASE_H
13 #include <TString.h>
14 #include <TNamed.h>
15 #include <TObjArray.h>
16 class AliOADBForward;
17 class TBrowser;
18
19 /**
20  * Base class for correction managers. 
21  *
22  * A correction is added to the manager by calling RegisterCorrection 
23  *
24  * @code 
25  * class MyManager : public AliCorrectionManager
26  * {
27  * public: 
28  *   enum {
29  *     kA, 
30  *     kB 
31  *   };
32  *   MyManager() 
33  *     : AliCorrectionManager(Bool_t setup=false) 
34  *   {
35  *     if (setup) {
36  *       RegisterCorrection(kA, "A", "/some/path/to/file", 
37  *                          TH2D::Class(), kStandard);
38  *       RegisterCorrection(kB, "B", "/some/path/to/file", 
39  *                          TParameter<float>::Class(), kStandard);
40  *     }
41  *   }
42  *   void Init(Bool_t useA, Bool_t useB, ULong_t run, UShort_t sys,
43  *             UShort_t sNN, Short_t fld, Bool_t mc, Bool_t sat, 
44  *             Bool_t force=false)
45  *   {
46  *     if (useA) GetCorrection(kA)->fEnabled = true;
47  *     if (useB) GetCorrection(kB)->fEnabled = true;
48  * 
49  *     return InitCorrections(run, sys, sNN, fld, mc, sat, force);
50  *   }
51  *   TH2D* GetA() const { return static_cast<TH2D*>(Get(kA)); }
52  *   TParameter<float>* GetB() const { return static_cast<TParameter<float>*>(Get(kB)); }
53  *  };
54  * @endcode
55  *
56  * In case the derived object is to be a singleton, one has to take a
57  * little extra care about when the constructor is called - especially
58  * if the singleton is to be streamed to disk:
59  *
60  * @code 
61  * class MyManager : public AliCorrectionManager
62  * {
63  * public: 
64  *   // As above, except the constructor must be private and 
65  *   MyManager& Instance() 
66  *   {
67  *     if (!fgInstance) fgInstance = MyManager(true);
68  *     return fgInstance; 
69  *   }
70  *   static MyManager* fgInstance; 
71  * };
72  * @endcode 
73  * 
74  * It is important - for I/O that default construction does not
75  * register the corrections.  This should only be done in-code on
76  * first construction.
77  *
78  */
79 class AliCorrectionManagerBase : public TObject
80 {
81 public:
82   enum EConstants { 
83     kIgnoreValue = 0,
84     kIgnoreField = 999
85   };
86   enum EFields {
87     kRun       = 0x01, 
88     kSys       = 0x02, 
89     kSNN       = 0x04, 
90     kField     = 0x08, 
91     kMC        = 0x10, 
92     kSatellite = 0x20,
93     kStandard  = kRun|kSys|kSNN|kField,
94     kFull      = kStandard|kMC|kSatellite
95   };
96   /** 
97    * Destructor 
98    */
99   virtual ~AliCorrectionManagerBase();
100   /** 
101    * Check if the manager is initialized 
102    * 
103    * @return True if initialized
104    */
105   virtual Bool_t IsInit() const { return fIsInit; }
106   /** 
107    * Print information
108    * 
109    * @param option Options:
110    * 
111    *   - R  Recursive list each correction 
112    *   - D  Also give details for each correction
113    */
114   virtual void Print(Option_t* option="") const;
115   /** 
116    * Set the prefix to use when looking for the input files 
117    * 
118    * @param prefix Prefix to use for all corrections 
119    */
120   virtual void SetPrefix(const TString& prefix);
121   /** 
122    * Store a correction 
123    * 
124    * @param o      Object to store
125    * @param runNo  Run number of run this was created from
126    * @param sys    Collision system (1:pp, 2:PbPb, 3:pPb)
127    * @param sNN    Center of mass energy in GeV
128    * @param field  L3 magnetic field in kG
129    * @param mc     If true, this is for simulations
130    * @param sat    If true, contain info for satellite interactions
131    * @param file   (optional) file name to store in 
132    * @param meth   (optional) method for run look to use. 
133    * 
134    * @return true on success
135    */
136   virtual Bool_t Store(TObject*     o,
137                        ULong_t     runNo,
138                        UShort_t    sys, 
139                        UShort_t    sNN, 
140                        Short_t     field, 
141                        Bool_t      mc,
142                        Bool_t      sat, 
143                        const char* file,
144                        const char* meth="NEAR") const;
145   /** 
146    * Append the content of the file @a addition to the @a destination
147    * file for this manager.  This used TFileMerger::PartialMerge 
148    * 
149    * @param destination Filename of destination storage (in OADB_PATH)
150    * @param addition    Filename of addition. 
151    * 
152    * @return true on success 
153    */
154   virtual Bool_t Append(const TString& addition,
155                         const TString& destination="") const;
156   /** 
157    * Browse this object
158    * 
159    * @param b Browser to use 
160    */
161   virtual void Browse(TBrowser* b);
162   /** 
163    * Flag that this is a folder 
164    * 
165    * @return Always true
166    */
167   virtual Bool_t IsFolder() const { return true; }
168   /** 
169    * Set whehter to enable debug information 
170    * 
171    * @param debug if true, do verbose queries 
172    */
173   virtual void SetDebug(Bool_t debug) { fDebug = debug; }
174 protected:
175   /** 
176    * Correction registration
177    */
178   struct Correction : public TNamed
179   {
180     /** 
181      * Constructor - for I/O
182      */
183     Correction();
184     /** 
185      * Constructor 
186      * 
187      * @param tableName  Table name
188      * @param fileName   File name 
189      * @param cls        Class
190      * @param fields     Enabled fields
191      */
192     Correction(const TString& tableName, 
193                const TString& fileName, 
194                TClass*        cls,
195                UShort_t       queryFields=kStandard,
196                Bool_t         enabled=false);
197     /** 
198      * Copy constructor
199      * 
200      * @param o Object to copy from 
201      */
202     Correction(const Correction& o);
203     /** 
204      * Assignement operator
205      * 
206      * @param o Object to assign from 
207      * 
208      * @return reference to this obejct
209      */    
210     Correction& operator=(const Correction& o);
211     /** 
212      * Destructor
213      */
214     ~Correction() { delete fObject; }
215     /** 
216      * Read the correction
217      * 
218      * @param db   Database interface 
219      * @param run  Run number
220      * @param sys  Collision system
221      * @param sNN  Center of mass energy per nucleon
222      * @param fld  L3 magnetic field
223      * @param mc   If true, for simulated data, else real
224      * @param sat  If true, for satellite interactions
225      * @param vrb  If true, do verbose query
226      *
227      * @return true on success
228      */
229     Bool_t ReadIt(AliOADBForward* db,
230                   ULong_t    run, 
231                   UShort_t   sys, 
232                   UShort_t   sNN, 
233                   Short_t    fld, 
234                   Bool_t     mc, 
235                   Bool_t     sat,
236                   Bool_t     vrb=false);
237     /** 
238      * Store a correction
239      * 
240      * @param db    Possible database interface
241      * @param o     Object to store 
242      * @param run  Run number
243      * @param sys  Collision system
244      * @param sNN  Center of mass energy per nucleon
245      * @param fld  L3 magnetic field
246      * @param mc   If true, for simulated data, else real
247      * @param sat  If true, for satellite interactions
248      * @param file File to store in
249      * @param meth Default run method
250      * 
251      * @return true on success
252      */
253     Bool_t StoreIt(AliOADBForward* db,
254                    TObject*    o, 
255                    ULong_t    run, 
256                    UShort_t   sys, 
257                    UShort_t   sNN, 
258                    Short_t    fld, 
259                    Bool_t     mc, 
260                    Bool_t     sat,
261                    const char* file=0,
262                    const char* meth="NEAR") const;
263     /** 
264      * Enable this correction 
265      * 
266      * @param enabled If true, correction is enabled
267      */
268     void Enable(Bool_t enabled=true) { fEnabled = enabled; }
269     /** 
270      * Get the data of the correction
271      * 
272      * @return Data of the correction
273      */
274     TObject* Get();
275     /** 
276      * Get the data of the correction
277      * 
278      * @return Data of the correction
279      */
280     const TObject* Get() const;
281     /** 
282      * Set the file the table is stored in
283      * 
284      * @param fileName file name of file table is stored in 
285      */
286     void SetFile(const TString& fileName) { fTitle = fileName; }
287     /** 
288      * Get pointer to class meta information.  Sets the internal
289      * member if not done already.
290      * 
291      * @return Pointer to class meta information
292      */
293     const TClass* TheClass() const;
294     /** 
295      * Print information to the screen 
296      * 
297      * @param option Options
298      */
299     void Print(Option_t* option="") const;
300     /** 
301      * Browse this object
302      * 
303      * @param b Browser to use 
304      */
305     void Browse(TBrowser* b);
306     /** 
307      * Flag that this is a folder 
308      * 
309      * @return Always true
310      */
311     Bool_t IsFolder() const { return true; }
312
313
314     mutable TClass*  fCls;      //! Class of correction objects
315     const TString fClientCls; // Class name
316     UShort_t fQueryFields;    // Enabled query fields
317     Bool_t   fEnabled;   // Whether we're in use 
318     TString  fLastEntry; // Text representation of last entry
319     TObject* fObject;    // The data 
320     ClassDef(Correction,1) // Correction meta object
321   };
322   /**
323    * Constructor 
324    */
325   AliCorrectionManagerBase();
326   /**
327    * Constructor 
328    */
329   AliCorrectionManagerBase(Bool_t notUsed);
330   /**
331    * Copy Constructor 
332    */
333   AliCorrectionManagerBase(const AliCorrectionManagerBase& o);
334   /** 
335    * Assignement operator
336    * 
337    * @param o Object to assign from
338    * 
339    * @return Reference to this 
340    */
341   AliCorrectionManagerBase& operator=(const AliCorrectionManagerBase& o);
342   /** 
343    * Register a correction 
344    * 
345    * @param id   Identifier 
346    * @param corr Correction 
347    */  
348   void RegisterCorrection(Int_t id, Correction* corr);
349   /** 
350    * Register a new correction. 
351    * 
352    * @param id         Identifier 
353    * @param tableName  Table name 
354    * @param fileName   File name 
355    * @param cls        Class 
356    * @param fields     Fields 
357    * @param enabled    Enabled or not 
358    */
359   void RegisterCorrection(Int_t id, 
360                           const TString& tableName, 
361                           const TString& fileName, 
362                           TClass*        cls,
363                           UShort_t       fields=kStandard,
364                           Bool_t         enabled=false);  
365   /** 
366    * Enable the correction at @a id
367    * 
368    * @param id Identifier 
369    * @param enable Whether to enable (true) or disable (false)
370    */
371   void EnableCorrection(Int_t id, Bool_t enable=true);
372   /** 
373    * Get the correction at @a id
374    * 
375    * @param id Identifier 
376    * 
377    * @return Correction or null
378    */
379   Correction* GetCorrection(Int_t id);
380   /** 
381    * Get the correction at @a id
382    * 
383    * @param id Identifier 
384    * 
385    * @return Correction or null
386    */
387   const Correction* GetCorrection(Int_t id) const;
388   /** 
389    * Set the correction file 
390    * 
391    * @param id        Identifier 
392    * @param fileName  file 
393    */
394   void SetCorrectionFile(Int_t id, const TString& fileName) const;
395   /** 
396    * Get the id of the correction with a given name
397    * 
398    * @param what Name of correction to look for
399    * 
400    * @return Correction identifier 
401    */
402   Int_t GetId(const TString& what) const;
403   /** 
404    * Get the id of the correction to store an object
405    * 
406    * @param obj Correction object 
407    * 
408    * @return Correction identifier 
409    */
410   Int_t GetId(const TObject* obj) const;
411   /** 
412    * Get the object corresponding to ID
413    * 
414    * @param id Correction identifier 
415    * 
416    * @return Object of correction, or null if correction not found or in-active
417    */
418   TObject* Get(Int_t id);
419   /** 
420    * Get the object corresponding to ID
421    * 
422    * @param id Correction identifier 
423    * 
424    * @return Object of correction, or null if correction not found or in-active
425    */
426   const TObject* Get(Int_t id) const;
427   /** 
428    * Read in all corrections 
429    * 
430    * @param run    Run number 
431    * @param sys    System 
432    * @param sNN    Center of mass energy 
433    * @param fld    L3 magnetic field
434    * @param mc     For simulations 
435    * @param sat    For satellite interactions 
436    * 
437    * @return true on success 
438    */
439   Bool_t InitCorrections(ULong_t    run, 
440                          UShort_t   sys, 
441                          UShort_t   sNN, 
442                          Short_t    fld, 
443                          Bool_t     mc, 
444                          Bool_t     sat,
445                          Bool_t     force=false);
446   /** 
447    * Read in all corrections 
448    * 
449    * @param run    Run number 
450    * @param sys    System 
451    * @param sNN    Center of mass energy 
452    * @param fld    L3 magnetic field
453    * @param mc     For simulations 
454    * @param sat    For satellite interactions 
455    * 
456    * @return true on success 
457    */
458   Bool_t CheckConditions(ULong_t    run, 
459                          UShort_t   sys, 
460                          UShort_t   sNN, 
461                          Short_t    fld, 
462                          Bool_t     mc, 
463                          Bool_t     sat);
464   /** 
465    * Read in all corrections 
466    * 
467    * @param run    Run number 
468    * @param sys    System 
469    * @param sNN    Center of mass energy 
470    * @param fld    L3 magnetic field
471    * @param mc     For simulations 
472    * @param sat    For satellite interactions 
473    * 
474    * @return true on success 
475    */
476   Bool_t ReadCorrections(ULong_t    run, 
477                          UShort_t   sys, 
478                          UShort_t   sNN, 
479                          Short_t    fld, 
480                          Bool_t     mc, 
481                          Bool_t     sat);
482   /** 
483    * Read in a correction
484    * 
485    * @param id     Correction identifier 
486    * @param run    Run number 
487    * @param sys    System 
488    * @param sNN    Center of mass energy 
489    * @param fld    L3 magnetic field
490    * @param mc     For simulations 
491    * @param sat    For satellite interactions 
492    * 
493    * @return true on success 
494    */
495   Bool_t ReadCorrection(Int_t      id,
496                         ULong_t    run, 
497                         UShort_t   sys, 
498                         UShort_t   sNN, 
499                         Short_t    fld, 
500                         Bool_t     mc, 
501                         Bool_t     sat);
502   /** 
503    * Set the correction file name for correction at @a i
504    * 
505    * @param i    Identifier 
506    * @param file Filename 
507    */
508   void SetCorrectionFile(Int_t i, const TString& file);
509   
510   TObjArray       fCorrections; // List of corrections
511   Bool_t          fIsInit;      // Whether we're intialized or not 
512   ULong_t         fRun;         // Cached run number
513   UShort_t        fSys;         // Cached system (1:pp 2:PbPb 3:pPb)
514   UShort_t        fSNN;         // Cached center of mass energy [GeV]
515   Short_t         fField;       // Cached L3 magnetic field [kG]
516   Bool_t          fMC;          // Cached Simulation flag
517   Bool_t          fSatellite;   // Cached satellite interaction flat
518   AliOADBForward* fDB;          //! do not store 
519   Bool_t          fDebug;       // If true, do verbose queries 
520
521   ClassDef(AliCorrectionManagerBase,1);
522 };
523
524 #endif
525
526                           
527                   
528