New reader for the pedestal run and vdrift (Julian) and some bug fixing (Raphaelle)
[u/mrichter/AliRoot.git] / TRD / AliTRDCalibraFillHisto.h
1 #ifndef ALITRDCALIBRAFILLHISTO_H
2 #define ALITRDCALIBRAFILLHISTO_H
3 /* Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4  * See cxx source for full Copyright notice                               */
5
6 /* $Id$ */
7
8 ///////////////////////////////////////////////////////////////////////////////
9 //                                                                           //
10 //  TRD calibration class for the HLT parameters                             //
11 //                                                                           //
12 ///////////////////////////////////////////////////////////////////////////////
13
14 #ifndef ROOT_TObject
15 #  include <TObject.h>
16 #endif
17 #ifndef ROOT_TProfile2D
18 #  include <TProfile2D.h> 
19 #endif
20
21
22 class TProfile2D;
23 class TObjArray;
24 class TH1F;
25 class TH2I;
26 class TH2F;
27 class TH2;
28 class TLinearFitter;
29 class TTreeSRedirector;
30
31 class AliLog;
32 class AliRawReader;
33
34 class AliTRDCalibraMode;
35 class AliTRDCalibraVector;
36 class AliTRDCalibraVdriftLinearFit;
37 class AliTRDrawStreamBase;
38 class AliTRDcluster;
39 class AliTRDtrackV1;
40 class AliTRDtrack;
41 class AliTRDseedV1;
42 class AliTRDgeometry;
43 class AliTRDCalDet;
44 class AliTRDCalROC;
45
46 class AliTRDrawFastStream;
47 class AliTRDdigitsManager;
48 class AliTRDSignalIndex;
49
50 struct eventHeaderStruct;
51
52 class AliTRDCalibraFillHisto : public TObject {
53
54  public: 
55
56   // Instance
57   static AliTRDCalibraFillHisto *Instance();
58   static void Terminate();
59   static void Destroy();
60   void DestroyDebugStreamer();
61
62
63   AliTRDCalibraFillHisto(const AliTRDCalibraFillHisto &c);
64   AliTRDCalibraFillHisto &operator=(const AliTRDCalibraFillHisto &) { return *this; }
65
66   // Functions for initialising and filling with AliTRDtrackV1
67           Bool_t  Init2Dhistos(Int_t nboftimebin = -1);
68           Bool_t  UpdateHistograms(const AliTRDtrack *t);
69           Bool_t  UpdateHistogramsV1(const AliTRDtrackV1 *t);
70  
71   // Process events DAQ
72           Int_t   ProcessEventDAQ(AliTRDrawStreamBase *rawStream, Bool_t nocheck = kFALSE);
73           Int_t   ProcessEventDAQ(AliRawReader *rawReader, Bool_t nocheck = kFALSE);
74           Int_t   ProcessEventDAQ(const eventHeaderStruct *event, Bool_t nocheck = kFALSE);
75           Int_t   ProcessEventDAQ2(AliRawReader *rawReader);
76
77   // Is Pad on
78           Bool_t   IsPadOn(Int_t detector, Int_t row, Int_t col) const;
79
80   // Functions for write
81           void     Write2d(const Char_t *filename = "TRD.calibration.root", Bool_t append = kFALSE);
82
83   //For the statistics
84           Double_t *StatH(TH2 *ch, Int_t i);
85           Double_t *GetMeanMedianRMSNumberCH();
86           Double_t *GetMeanMedianRMSNumberLinearFitter() const;
87  // LinearFitter
88           void     AnalyseLinearFitter();
89        
90          
91   //
92   // Set of Get the variables
93   //
94
95           void     SetIsHLT(Bool_t isHLT = kTRUE)                            { fIsHLT = isHLT;                       }  
96           Bool_t   IsHLT() const                                             { return fIsHLT;                        }  
97
98   // Choice to fill or not the 2D
99           void     SetPH2dOn(Bool_t ph2don = kTRUE)                          { fPH2dOn          = ph2don;            }
100           void     SetCH2dOn(Bool_t ch2don = kTRUE)                          { fCH2dOn          = ch2don;            }
101           void     SetPRF2dOn(Bool_t prf2don = kTRUE)                        { fPRF2dOn         = prf2don;           }
102           void     SetHisto2d(Bool_t histo2d = kTRUE)                        { fHisto2d         = histo2d;           }
103           void     SetVector2d(Bool_t vector2d = kTRUE)                      { fVector2d        = vector2d;          }
104           void     SetLinearFitterOn(Bool_t linearfitteron = kTRUE)          { fLinearFitterOn      = linearfitteron;}
105           void     SetLinearFitterDebugOn(Bool_t debug = kTRUE)              { fLinearFitterDebugOn = debug;         }
106                   
107   
108           Bool_t   GetPH2dOn() const                                         { return fPH2dOn;                 }
109           Bool_t   GetCH2dOn() const                                         { return fCH2dOn;                 }
110           Bool_t   GetPRF2dOn() const                                        { return fPRF2dOn;                }
111           Bool_t   GetHisto2d() const                                        { return fHisto2d;                }
112           Bool_t   GetVector2d() const                                       { return fVector2d;               }
113           Bool_t   GetLinearFitterOn() const                                 { return fLinearFitterOn;         }
114           Bool_t   GetLinearFitterDebugOn() const                            { return fLinearFitterDebugOn; }
115
116
117   // Get stuff that are filled
118   TH2I            *GetCH2d();
119   TProfile2D      *GetPH2d(Int_t nbtimebin=24, Float_t samplefrequency= 10.0);
120   TProfile2D      *GetPRF2d() const                                          { return fPRF2d;                  } 
121   TObjArray        GetLinearFitterArray() const                              { return fLinearFitterArray;      }
122   TLinearFitter   *GetLinearFitter(Int_t detector, Bool_t force=kFALSE);
123   AliTRDCalibraVdriftLinearFit *GetVdriftLinearFit() const                   { return fLinearVdriftFit;        }
124   
125  
126   // How to fill the 2D
127           void     SetRelativeScale(Float_t relativeScale);                      
128           void     SetThresholdClusterPRF2(Float_t thresholdClusterPRF2)     { fThresholdClusterPRF2 = thresholdClusterPRF2; }
129           void     SetLimitChargeIntegration(Bool_t limitChargeIntegration)  { fLimitChargeIntegration = limitChargeIntegration; }
130           void     SetFillWithZero(Bool_t fillWithZero)                      { fFillWithZero = fillWithZero;   }
131           void     SetNormalizeNbOfCluster(Bool_t normalizeNbOfCluster)      { fNormalizeNbOfCluster = normalizeNbOfCluster; }
132           void     SetMaxCluster(Float_t maxCluster)                         { fMaxCluster = maxCluster; }
133           void     SetNbMaxCluster(Short_t nbMaxCluster)                     { fNbMaxCluster = nbMaxCluster; }
134           void     SetNz(Int_t i, Short_t nz);
135           void     SetNrphi(Int_t i, Short_t nrphi);
136           void     SetAllTogether(Int_t i);
137           void     SetPerSuperModule(Int_t i);
138           void     SetProcent(Float_t procent)                               { fProcent              = procent;              }
139           void     SetDifference(Short_t difference)                         { fDifference           = difference;           }
140           void     SetNumberClusters(Short_t numberClusters)                 { if(numberClusters >= 0) fNumberClusters       = numberClusters;       }
141           void     SetNumberClustersf(Short_t numberClustersf)               { fNumberClustersf      = numberClustersf;      }
142           void     SetNumberClustersProcent(Float_t numberClustersProcent)   { fNumberClustersProcent = numberClustersProcent;                       }
143           void     SetThresholdClustersDAQ(Float_t thresholdClustersDAQ)     { fThresholdClustersDAQ = thresholdClustersDAQ;                         }
144           void     SetNumberRowDAQ(Short_t numberRowDAQ)                     { fNumberRowDAQ         = numberRowDAQ;         }
145           void     SetNumberColDAQ(Short_t numberColDAQ)                     { fNumberColDAQ         = numberColDAQ;         }
146           void     SetNumberBinCharge(Short_t numberBinCharge)               { fNumberBinCharge      = numberBinCharge;      }
147           void     SetNumberBinPRF(Short_t numberBinPRF)                     { fNumberBinPRF         = numberBinPRF;         }
148           void     SetNumberGroupsPRF(Short_t numberGroupsPRF);
149   
150           Float_t  GetRelativeScale() const                                  { return fRelativeScale;          }
151           Float_t  GetThresholdClusterPRF2() const                           { return fThresholdClusterPRF2;   }
152           Bool_t   GetLimitChargeIntegration() const                         { return fLimitChargeIntegration; }
153           Bool_t   GetFillWithZero() const                                   { return fFillWithZero;           }
154           Bool_t   GetNormalizeNbOfCluster() const                           { return fNormalizeNbOfCluster;   }
155           Float_t  GetMaxCluster() const                                     { return fMaxCluster;             }
156           Short_t  GetNbMaxCluster() const                                   { return fNbMaxCluster;           }
157           Float_t  GetProcent() const                                        { return fProcent;                }
158           Short_t  GetDifference() const                                     { return fDifference;             }
159           Short_t  GetNumberClusters() const                                 { return fNumberClusters;         }
160           Short_t  GetNumberClustersf() const                                { return fNumberClustersf;        }
161           Short_t  GetNumberBinCharge() const                                { return fNumberBinCharge;        }
162           Short_t  GetNumberBinPRF() const                                   { return fNumberBinPRF;           }
163           Short_t  GetNumberGroupsPRF() const                                { return fNgroupprf;              }
164           Int_t    *GetEntriesLinearFitter() const                           { return fEntriesLinearFitter;    }
165
166  // Debug
167           void     SetDebugLevel(Short_t level)                              { fDebugLevel = level;           }
168
169   // Vector method
170 AliTRDCalibraVector *GetCalibraVector() const                                { return fCalibraVector;          }   
171   
172  protected:
173
174   // Geometry
175   AliTRDgeometry  *fGeo;                    //! The TRD geometry
176
177   // Is HLT
178           Bool_t   fIsHLT;                  // Now if HLT, the per detector
179
180   // Choice to fill or not the 2D
181           Bool_t   fCH2dOn;                 // Chose to fill the 2D histos or vectors for the relative gain calibration 
182           Bool_t   fPH2dOn;                 // Chose to fill the 2D histos or vectors for the drift velocity and T0
183           Bool_t   fPRF2dOn;                // Chose to fill the 2D histos or vectors for the pad response function calibration
184           Bool_t   fHisto2d;                // Chose to fill the 2D histos
185           Bool_t   fVector2d;               // Chose to fill vectors
186           Bool_t   fLinearFitterOn;         // Method with linear fit for drift velocity
187           Bool_t   fLinearFitterDebugOn;    // Method with linear fit for drift velocity
188
189   // How to fill the 2D
190           Float_t  fRelativeScale;          // Scale of the deposited charge
191           Float_t  fThresholdClusterPRF2;   // Threshold on cluster pad signals
192           Bool_t   fLimitChargeIntegration; // Integration range for the gain calibration
193           Bool_t   fFillWithZero;           // Fill with zero or not the average pulse height
194           Bool_t   fNormalizeNbOfCluster;   // Normalize with the number of cluster for the gain
195           Float_t  fMaxCluster;             // Max amplitude of one cluster
196           Short_t  fNbMaxCluster;           // Number of tb at the end
197   // Calibration mode
198           AliTRDCalibraMode *fCalibraMode;  // Calibration mode
199
200   //For debugging
201           TTreeSRedirector          *fDebugStreamer;                 //!Debug streamer
202           Short_t     fDebugLevel;                                   // Flag for debugging
203   //
204   // Internal variables
205   //
206
207   // Fill the 2D histos in the offline tracking
208           Int_t    fDetectorPreviousTrack;  // Change of detector
209           Int_t    fMCMPrevious;            // Change of MCM
210           Int_t    fROBPrevious;            // Change of ROB
211           Short_t  fNumberClusters;         // Minimum number of clusters in the tracklets
212           Short_t  fNumberClustersf;        // Maximum number of clusters in the tracklets
213           Float_t  fNumberClustersProcent;  // Procent of number of time bins for fNumberClusters
214           Float_t  fThresholdClustersDAQ;   // Threshold clusters for DAQ algorithm
215           Short_t  fNumberRowDAQ;           // Size of the spot for DAQ algorithm
216           Short_t  fNumberColDAQ;           // Size of the spot for DAQ algorithm
217           Float_t  fProcent;                // Limit to take the info of the most important calibration group if the track goes through 2 groups (CH)
218           Short_t  fDifference;             // Limit to take the info of the most important calibration group if the track goes through 2 groups (CH)
219           Int_t    fNumberTrack;            // How many tracks could be used (Debug for the moment)
220           Int_t    fNumberUsedCh[2];        // How many tracks have been really used for the gain (0, strict; 1 with fProcent)
221           Int_t    fNumberUsedPh[2];        // How many tracks have been really used for the drift velocity (0, strict; 1 with fDifference)
222           Int_t    fTimeMax;                // Number of time bins
223           Float_t  fSf;                     // Sampling frequence
224           Short_t  fNumberBinCharge;        // Number of bins for the gain factor
225           Short_t  fNumberBinPRF;           // Number of bin for the PRF
226           Short_t  fNgroupprf;              // Number of groups in tnp bins for PRF /2.0
227
228   // Variables per tracklet
229           Float_t       *fAmpTotal;                  // Energy deposited in the calibration group by the track
230           Short_t       *fPHPlace;                   // Calibration group of PH
231           Float_t       *fPHValue;                   // PH
232           Bool_t         fGoodTracklet;              // Good tracklet
233           TLinearFitter *fLinearFitterTracklet;      // linear fitter tracklet  
234   //Statistics
235           Int_t         *fEntriesCH;                 // Number of entries CH
236           Int_t         *fEntriesLinearFitter;       // Number of entries LinearFitter
237
238
239   //
240   // Vector method
241   //
242           
243           AliTRDCalibraVector *fCalibraVector; // The vector object
244  
245  
246   // Histograms to store the info from the digits, from the tracklets or from the tracks
247           TProfile2D      *fPH2d;                         // 2D average pulse height
248           TProfile2D      *fPRF2d;                        // 2D PRF
249           TH2I            *fCH2d;                         // 2D deposited charge
250           TObjArray       fLinearFitterArray;             // TObjArray of Linear Fitters for the detectors 
251           AliTRDCalibraVdriftLinearFit *fLinearVdriftFit; // Info Linear Fit
252           
253  // Current calib object: to correct for the database used
254           AliTRDCalDet *fCalDetGain;                      // Current calib object gain
255           AliTRDCalROC *fCalROCGain;                      // Current calib object gain
256            
257   //
258   // A lot of internal functions......
259   //
260   // Create the 2D histo to be filled Online
261           void     CreateCH2d(Int_t nn);
262           void     CreatePH2d(Int_t nn);
263           void     CreatePRF2d(Int_t nn);
264   
265   // Calibration with AliTRDtrackV1
266           void     FillTheInfoOfTheTrackPH();
267           void     FillTheInfoOfTheTrackCH(Int_t nbclusters);
268           Bool_t   FindP1TrackPHtracklet(const AliTRDtrack *t, Int_t index0, Int_t index1);
269           Bool_t   FindP1TrackPHtrackletV1(const AliTRDseedV1 *tracklet, Int_t nbclusters);
270           Bool_t   HandlePRFtracklet(const AliTRDtrack *t, Int_t index0, Int_t index1);
271           Bool_t   HandlePRFtrackletV1(const AliTRDseedV1 *tracklet, Int_t nbclusters);
272           void     ResetfVariablestracklet();
273           void     StoreInfoCHPHtrack(const AliTRDcluster *cl,const Double_t dqdl,const Int_t *group,const Int_t row,const Int_t col,const AliTRDcluster *cls=0x0);
274           void     FillCH2d(Int_t x, Float_t y);
275
276   // Calibration on DAQ
277
278           Int_t    FillDAQ(Double_t phvalue[16][144][36]);
279           Bool_t   UpdateDAQ(Int_t det, Int_t /*row*/, Int_t /*col*/, Int_t timebin, Float_t signal, Int_t nbtimebins);
280           
281  // row col calibration groups stuff
282           Bool_t   LocalisationDetectorXbins(Int_t detector);
283           Int_t    CalculateTotalNumberOfBins(Int_t i);
284           void     CheckGoodTrackletV0(const Int_t detector,const Int_t row,const Int_t col);
285           void     CheckGoodTrackletV1(const AliTRDcluster *cl);
286           Int_t    CalculateCalibrationGroup(Int_t i, Int_t row, Int_t col) const;
287           
288   // Clear
289           void     ClearHistos();
290       
291   // Some basic geometry function
292   virtual Int_t    GetLayer(Int_t d) const;
293   virtual Int_t    GetStack(Int_t d) const;
294   virtual Int_t    GetSector(Int_t d) const;
295           
296           
297   // Instance of this class and so on
298   static  AliTRDCalibraFillHisto *fgInstance;                // Instance
299   static  Bool_t   fgTerminated;                             // If terminated
300
301  private:
302   
303   // This is a singleton, contructor is private!
304   AliTRDCalibraFillHisto();
305   virtual ~AliTRDCalibraFillHisto(); 
306     
307   ClassDef(AliTRDCalibraFillHisto,4)                         // TRD Calibration class
308
309 };
310   
311 #endif
312