removed separate histograms for Calib and physics data
[u/mrichter/AliRoot.git] / T0 / AliT0CalibTimeEq.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
7  * Permission to use, copy, modify and distribute this software and its   *
8  * documentation strictly for non-commercial purposes is hereby granted   *
9  * without fee, provided that the above copyright notice appears in all   *
10  * copies and that both the copyright notice and this permission notice   *
11  * appear in the supporting documentation. The authors make no claims     *
12  * about the suitability of this software for any purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15
16 /* $Id$ */
17
18 ///////////////////////////////////////////////////////////////////////////////
19 //                                                                           //
20 // class for T0 calibration                       TM-AC-AM_6-02-2006  
21 // equalize time shift for each time CFD channel
22 //                                                                           //
23 ///////////////////////////////////////////////////////////////////////////////
24
25 #include "AliT0CalibTimeEq.h"
26 #include "AliLog.h"
27 #include <TFile.h>
28 #include <TMath.h>
29 #include <TF1.h>
30 #include <TSpectrum.h>
31 #include <TProfile.h>
32 #include <iostream>
33
34 ClassImp(AliT0CalibTimeEq)
35
36 //________________________________________________________________
37   AliT0CalibTimeEq::AliT0CalibTimeEq():TNamed(),
38                                        fMeanVertex(0),        
39                                        fRmsVertex(0)      
40 {
41   //
42
43 }
44
45 //________________________________________________________________
46 AliT0CalibTimeEq::AliT0CalibTimeEq(const char* name):TNamed(),
47                                        fMeanVertex(0),        
48                                        fRmsVertex(0)      
49 {
50   //constructor
51
52   TString namst = "Calib_";
53   namst += name;
54   SetName(namst.Data());
55   SetTitle(namst.Data());
56 }
57
58 //________________________________________________________________
59 AliT0CalibTimeEq::AliT0CalibTimeEq(const AliT0CalibTimeEq& calibda):TNamed(calibda),            
60                                        fMeanVertex(0),        
61                                        fRmsVertex(0)      
62 {
63 // copy constructor
64   SetName(calibda.GetName());
65   SetTitle(calibda.GetName());
66
67
68 }
69
70 //________________________________________________________________
71 AliT0CalibTimeEq &AliT0CalibTimeEq::operator =(const AliT0CalibTimeEq& calibda)
72 {
73 // assignment operator
74   SetName(calibda.GetName());
75   SetTitle(calibda.GetName());
76  
77   return *this;
78 }
79
80 //________________________________________________________________
81 AliT0CalibTimeEq::~AliT0CalibTimeEq()
82 {
83   //
84   // destrictor
85 }
86 //________________________________________________________________
87 void AliT0CalibTimeEq::Reset()
88 {
89   //reset values
90
91   memset(fCFDvalue,0,120*sizeof(Float_t));
92   memset(fTimeEq,1,24*sizeof(Float_t));
93 }
94
95
96 //________________________________________________________________
97 void  AliT0CalibTimeEq::Print(Option_t*) const
98 {
99   // print time values
100
101   printf("\n    ----    PM Arrays       ----\n\n");
102   printf(" Time delay CFD \n");
103   for (Int_t i=0; i<24; i++) printf(" CFD  %f ",fTimeEq[i]);
104   printf("\n Mean Vertex %f \n", fMeanVertex);
105
106
107
108 //________________________________________________________________
109 Bool_t AliT0CalibTimeEq::ComputeOnlineParams(const char* filePhys)
110 {
111   // compute online equalized time
112   Float_t meandiff, sigmadiff, meanver, meancfdtime, sigmacfdtime;
113   meandiff = sigmadiff =  meanver = meancfdtime = sigmacfdtime =0;
114   Double_t rmsver=0;
115   Int_t nent=0;
116   Bool_t ok=false;
117   gFile = TFile::Open(filePhys);
118   if(!gFile) {
119     AliError("No input PHYS data found ");
120   }
121   else
122     {
123       ok=true;
124       for (Int_t i=0; i<24; i++)
125         {
126           meandiff = sigmadiff =  meanver = meancfdtime = sigmacfdtime =0;
127           TH1F *cfd = (TH1F*) gFile->Get(Form("CFD1minCFD%d",i+1));
128           TH1F *cfdtime = (TH1F*) gFile->Get(Form("CFD%d",i+1));
129           if(!cfd) {
130             AliWarning(Form("no Diff histograms collected by PHYS DA for channel %i", i));
131             ok=false;
132             return ok;
133           }
134           if(!cfdtime) {
135             AliWarning(Form("no CFD histograms collected by PHYS DA for channel %i", i));
136             ok=false;
137             return ok;
138           }
139           if(cfd) {
140             nent = Int_t(cfd->GetEntries());
141             if(nent>500)  { 
142               if(cfd->GetRMS()>1.5 )
143                 GetMeanAndSigma(cfd, meandiff, sigmadiff);
144               if(cfd->GetRMS()<=1.5) 
145                 {
146                   meandiff = cfd->GetMean();
147                   sigmadiff=cfd->GetRMS();
148                 }
149               Int_t   maxBin = cfd->GetMaximumBin(); 
150               Double_t  meanEstimate = cfd->GetBinCenter( maxBin); 
151               if(TMath::Abs(meanEstimate - meandiff) > 20 ) meandiff = meanEstimate; 
152             }
153             else 
154               {
155                 ok=false;
156                 AliWarning(Form(" Not  enouph data in PMT %i- PMT1:  %i ", i, nent));
157               }
158           }
159             //      printf(" i = %d buf1 = %s\n", i, buf1);
160           if(cfdtime) {
161             nent = Int_t(cfdtime->GetEntries());
162             if(nent > 500 )  { //!!!!!!!!!!
163               if(cfdtime->GetRMS()>1.5 )
164                 GetMeanAndSigma(cfdtime,meancfdtime, sigmacfdtime);
165               if(cfdtime->GetRMS()<=1.5) 
166                 {
167                   meancfdtime = cfdtime->GetMean();
168                   sigmacfdtime = cfdtime->GetRMS();
169                 }
170               Int_t   maxBin = cfdtime->GetMaximumBin(); 
171               Double_t  meanEstimate = cfdtime->GetBinCenter( maxBin); 
172               if(TMath::Abs(meanEstimate - meancfdtime) > 20 ) meancfdtime = meanEstimate; 
173             }
174             else 
175               {
176                 ok=false;
177                 AliWarning(Form(" Not  enouph data in PMT in CFD peak %i - %i ", i, nent));
178               }
179           }
180           SetTimeEq(i,meandiff);
181           SetTimeEqRms(i,sigmadiff);
182           SetCFDvalue(i,0,meancfdtime);
183           if (cfd) delete cfd;
184           if (cfdtime) delete cfdtime;
185           
186         }
187       TH1F *ver = (TH1F*) gFile->Get("hVertex");
188       if(!ver) AliWarning("no T0 histogram collected by PHYS DA ");
189       if(ver) {
190         meanver = ver->GetMean();
191         rmsver = ver->GetRMS();
192       }
193       SetMeanVertex(meanver);
194       SetRmsVertex(rmsver);
195       
196       gFile->Close();
197       delete gFile;
198       
199     }
200     return ok; 
201 }
202
203 //________________________________________________________________
204   Bool_t AliT0CalibTimeEq::ComputeOfflineParams(const char* filePhys, Float_t *timecdb, Float_t *cfdvalue, Int_t badpmt)
205 {
206   // compute offline equalized time
207   Float_t meandiff, sigmadiff, meanver, meancfdtime, sigmacfdtime;
208   meandiff = sigmadiff =  meanver = meancfdtime = sigmacfdtime =0;
209   Int_t nent=0;
210   Bool_t ok=false;
211   TH1F *cfddiff = NULL; 
212   TH1F *cfdtime = NULL;
213   TObjArray * TzeroObj = NULL;
214
215   gFile = TFile::Open(filePhys);
216   if(!gFile) {
217     AliError("No input PHYS data found ");
218     return ok;
219   }
220   else
221     {
222       meandiff = sigmadiff =  meanver = meancfdtime = sigmacfdtime =0;
223       ok=true;
224       TDirectory *dr = (TDirectory*) gFile->Get("T0Calib");
225       /*      if (!dr ) { AliError("No input T0calib found "); 
226         ok=false;
227         return ok;
228         } */
229       if (dr)   TzeroObj = (TObjArray*) dr->Get("T0Calib");
230
231       for (Int_t i=0; i<24; i++)
232         {
233           if (i != badpmt) {
234             if(TzeroObj) {
235               cfddiff = (TH1F*)TzeroObj->FindObject(Form("CFD1minCFD%d",i+1));
236               cfdtime = (TH1F*)TzeroObj->FindObject(Form("CFD1minCFD%d",i+1));
237             }
238             else
239               {
240                 cfddiff = (TH1F*)gFile->Get(Form("CFD1minCFD%d",i+1));
241                 cfdtime = (TH1F*)gFile->Get(Form("CFD1minCFD%d",i+1));
242               }
243             if(!cfddiff || !cfdtime) {
244               AliWarning(Form("no  histograms collected by pass0 for channel %i", i));
245               ok=false;
246               delete gFile;
247               return ok;
248             }
249             if(cfddiff) {
250             nent = Int_t(cfddiff->GetEntries());
251             if(nent>500 )  { //!!!!!
252               if(cfddiff->GetRMS()>1.5 )
253                 GetMeanAndSigma(cfddiff, meandiff, sigmadiff);
254               if(cfddiff->GetRMS()<=1.5) 
255                 {
256                   meandiff = cfddiff->GetMean();
257                   sigmadiff = cfddiff->GetRMS();
258                 }
259                 Int_t   maxBin = cfddiff->GetMaximumBin(); 
260                 Double_t  meanEstimate = cfddiff->GetBinCenter( maxBin); 
261                 if(TMath::Abs(meanEstimate - meandiff) > 20 ) meandiff = meanEstimate; 
262               
263             }
264             else 
265               {
266                 ok=false;
267                 AliWarning(Form(" Not  enouph data in PMT %i- PMT1:  %i ", i, nent));
268               }
269           }         
270           
271           if(cfdtime) {
272             nent = Int_t(cfdtime->GetEntries());
273             if(nent>500 )  { //!!!!!
274               if(cfdtime->GetRMS()>1.5 )
275                 GetMeanAndSigma(cfdtime,meancfdtime, sigmacfdtime);
276               if(cfdtime->GetRMS()<=1.5) 
277                 {
278                   meancfdtime = cfdtime->GetMean();
279                   sigmacfdtime=cfdtime->GetRMS();
280                 }
281                 Int_t   maxBin = cfdtime->GetMaximumBin(); 
282                 Double_t  meanEstimate = cfdtime->GetBinCenter( maxBin); 
283                 if(TMath::Abs(meanEstimate - meancfdtime) > 20 ) meancfdtime = meanEstimate; 
284             }
285             else 
286               {
287                 ok=false;
288                 AliWarning(Form(" Not  enouph data in PMT in CFD peak %i - %i ", i, nent));
289               }
290           }
291           
292           SetTimeEq(i,timecdb[i] + meandiff);
293           SetTimeEqRms(i,sigmadiff);
294           SetCFDvalue(i,0,cfdvalue[i] );
295           if (cfddiff) cfddiff->Reset();
296           if (cfdtime) cfdtime->Reset();
297           } //bad pmt
298         }
299       
300       gFile->Close();
301       delete gFile;
302
303     }
304     return ok; 
305    }
306
307 //________________________________________________________________________
308 void AliT0CalibTimeEq::GetMeanAndSigma(TH1F* hist,  Float_t &mean, Float_t &sigma) {
309   
310   const double window = 2.;  //fit window 
311   
312   double meanEstimate, sigmaEstimate; 
313   int maxBin;
314   maxBin        =  hist->GetMaximumBin(); //position of maximum
315   meanEstimate  =  hist->GetBinCenter( maxBin); // mean of gaussian sitting in maximum
316   sigmaEstimate = hist->GetRMS();
317   TF1* fit= new TF1("fit","gaus", meanEstimate - window*sigmaEstimate, meanEstimate + window*sigmaEstimate);
318   fit->SetParameters(hist->GetBinContent(maxBin), meanEstimate, sigmaEstimate);
319   hist->Fit("fit","RQ","Q");
320
321   mean  = (Float_t) fit->GetParameter(1);
322   sigma = (Float_t) fit->GetParameter(2);
323
324   delete fit;
325 }
326
327