]> git.uio.no Git - u/mrichter/AliRoot.git/blob - T0/AliT0CalibTimeEq.cxx
fix mistyping
[u/mrichter/AliRoot.git] / T0 / AliT0CalibTimeEq.cxx
1
2 /**************************************************************************
3  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4  *                                                                        *
5  * Author: The ALICE Off-line Project.                                    *
6  * Contributors are mentioned in the code where appropriate.              *
7  *                                                                        *
8  * Permission to use, copy, modify and distribute this software and its   *
9  * documentation strictly for non-commercial purposes is hereby granted   *
10  * without fee, provided that the above copyright notice appears in all   *
11  * copies and that both the copyright notice and this permission notice   *
12  * appear in the supporting documentation. The authors make no claims     *
13  * about the suitability of this software for any purpose. It is          *
14  * provided "as is" without express or implied warranty.                  *
15  **************************************************************************/
16
17 /* $Id$ */
18
19 ///////////////////////////////////////////////////////////////////////////////
20 //                                                                           //
21 // class for T0 calibration                       TM-AC-AM_6-02-2006  
22 // equalize time shift for each time CFD channel
23 //                                                                           //
24 ///////////////////////////////////////////////////////////////////////////////
25
26 #include "AliT0CalibTimeEq.h"
27 #include "AliLog.h"
28 #include <TFile.h>
29 #include <TMath.h>
30 #include <TF1.h>
31 #include <TSpectrum.h>
32 #include <TProfile.h>
33 #include <iostream>
34
35 ClassImp(AliT0CalibTimeEq)
36
37 //________________________________________________________________
38   AliT0CalibTimeEq::AliT0CalibTimeEq():TNamed(),
39                                        fMeanVertex(0),        
40                                        fRmsVertex(0)      
41 {
42   //
43
44 }
45
46 //________________________________________________________________
47 AliT0CalibTimeEq::AliT0CalibTimeEq(const char* name):TNamed(),
48                                        fMeanVertex(0),        
49                                        fRmsVertex(0)      
50 {
51   //constructor
52
53   TString namst = "Calib_";
54   namst += name;
55   SetName(namst.Data());
56   SetTitle(namst.Data());
57 }
58
59 //________________________________________________________________
60 AliT0CalibTimeEq::AliT0CalibTimeEq(const AliT0CalibTimeEq& calibda):TNamed(calibda),            
61                                        fMeanVertex(0),        
62                                        fRmsVertex(0)      
63 {
64 // copy constructor
65   SetName(calibda.GetName());
66   SetTitle(calibda.GetName());
67
68
69 }
70
71 //________________________________________________________________
72 AliT0CalibTimeEq &AliT0CalibTimeEq::operator =(const AliT0CalibTimeEq& calibda)
73 {
74 // assignment operator
75   SetName(calibda.GetName());
76   SetTitle(calibda.GetName());
77  
78   return *this;
79 }
80
81 //________________________________________________________________
82 AliT0CalibTimeEq::~AliT0CalibTimeEq()
83 {
84   //
85   // destrictor
86 }
87 //________________________________________________________________
88 void AliT0CalibTimeEq::Reset()
89 {
90   //reset values
91
92   memset(fCFDvalue,0,120*sizeof(Float_t));
93   memset(fTimeEq,1,24*sizeof(Float_t));
94 }
95
96
97 //________________________________________________________________
98 void  AliT0CalibTimeEq::Print(Option_t*) const
99 {
100   // print time values
101
102   printf("\n    ----    PM Arrays       ----\n\n");
103   printf(" Time delay CFD \n");
104   for (Int_t i=0; i<24; i++) 
105     printf(" CFD  %f diff %f  \n",fCFDvalue[i][0],fTimeEq[i]);
106
107
108
109 //________________________________________________________________
110 Bool_t AliT0CalibTimeEq::ComputeOnlineParams(const char* filePhys)
111 {
112   // compute online equalized time
113   Float_t meandiff, sigmadiff, meanver, meancfdtime, sigmacfdtime;
114   meandiff = sigmadiff =  meanver = meancfdtime = sigmacfdtime =0;
115   Double_t rmsver=0;
116   Int_t nent=0;
117   Int_t okdiff=0;
118   Int_t oktime=0;
119   Bool_t ok=false;
120   gFile = TFile::Open(filePhys);
121   if(!gFile) {
122     AliError("No input PHYS data found ");
123   }
124   else
125     {
126       ok=true;
127       for (Int_t i=0; i<24; i++)
128         {
129
130           meandiff = sigmadiff =  meanver = meancfdtime = sigmacfdtime =0;
131           TH1F *cfd = (TH1F*) gFile->Get(Form("CFD1minCFD%d",i+1));
132           TH1F *cfdtime = (TH1F*) gFile->Get(Form("CFD%d",i+1));
133           if(!cfd) {
134             AliWarning(Form("no Diff histograms collected by PHYS DA for channel %i", i));
135             okdiff++;
136             if(okdiff<4) {
137               meandiff = 0;
138               sigmadiff = 0;
139             }
140             else
141               ok = false; 
142           }
143           if(!cfdtime) {
144             AliWarning(Form("no CFD histograms collected by PHYS DA for channel %i", i));
145             oktime++;
146             if(oktime<4) {
147               meancfdtime = 0;
148             }
149             else
150               ok = false; 
151           }
152           if(cfd) {
153             nent = Int_t(cfd->GetEntries());
154             if( nent<=50) {
155               okdiff++;
156               //              printf(" pmt %i nent %i cfd->GetRMS() %f cfd->GetMean() %f \n",
157               //     i, nent, cfd->GetRMS(), cfd->GetMean() );
158               if(okdiff<4) {
159                 meandiff = 0;
160                 sigmadiff = 0;
161               }
162               else
163                 {
164                   // printf(" OK fsle:: pmt %i nent %i cfd->GetRMS() %f cfd->GetMean() %f \n",
165                   // i, nent, cfd->GetRMS(), cfd->GetMean() );
166                   AliWarning(Form(" Not  enouph data in PMT %i- PMT1:  %i ", i, nent));
167                   ok = false; 
168                 }
169             }
170             if(nent>50)  { 
171               if(cfd->GetRMS()>1.5 )
172                 GetMeanAndSigma(cfd, meandiff, sigmadiff);
173               if(cfd->GetRMS()<=1.5) 
174                 {
175                   meandiff = cfd->GetMean();
176                   sigmadiff=cfd->GetRMS();
177                 }
178               Int_t   maxBin = cfd->GetMaximumBin(); 
179               Double_t  meanEstimate = cfd->GetBinCenter( maxBin); 
180               if(TMath::Abs(meanEstimate - meandiff) > 20 ) meandiff = meanEstimate; 
181             }
182           }
183           
184           if(cfdtime) {
185             nent = Int_t(cfdtime->GetEntries());
186             if( nent<=50 ) {
187               oktime++;
188               if(oktime<4) {
189                 meancfdtime = 0;
190               }
191               else
192                 {
193                   AliWarning(Form(" Not  enouph data in PMT %i:  %i ", i, nent));
194                   ok = false; 
195                 }
196             }
197             if(nent > 50  )  { //!!!!!!!!!!
198               if(cfdtime->GetRMS()>1.5 )
199                 GetMeanAndSigma(cfdtime,meancfdtime, sigmacfdtime);
200               if(cfdtime->GetRMS()<=1.5) 
201                 {
202                   if(cfdtime->GetRMS()==0 ||cfdtime->GetMean()==0 ) {
203                     ok = false;
204                   }
205                   meancfdtime = cfdtime->GetMean();
206                   sigmacfdtime = cfdtime->GetRMS();
207                 }
208               Int_t   maxBin = cfdtime->GetMaximumBin(); 
209               Double_t  meanEstimate = cfdtime->GetBinCenter( maxBin); 
210               if(TMath::Abs(meanEstimate - meancfdtime) > 20 ) meancfdtime = meanEstimate; 
211             }
212           }
213           SetTimeEq(i,meandiff);
214           SetTimeEqRms(i,sigmadiff);
215           SetCFDvalue(i,0,meancfdtime);
216           if (cfd) delete cfd;
217           if (cfdtime) delete cfdtime;
218         }
219       TH1F *ver = (TH1F*) gFile->Get("hVertex");
220       if(ver) {
221         meanver = ver->GetMean();
222         rmsver = ver->GetRMS();
223       }
224       SetMeanVertex(meanver);
225       SetRmsVertex(rmsver);
226       
227       gFile->Close();
228       delete gFile;
229       
230     }
231     return ok; 
232 }
233
234 //________________________________________________________________
235   Int_t AliT0CalibTimeEq::ComputeOfflineParams(const char* filePhys, Float_t *timecdb, Float_t *cfdvalue, Int_t badpmt)
236 {
237   // fStatus implementation:
238   //ok means
239   // 1000  - can not open file 
240   // for timediff
241   // 1-24  no histograms with time diff for more than okdiff channenls
242   // 100 >3 histos are empty
243   // - 1 if less 3 channels are empty ;for these channels OCDB value will be written back WARNING
244   // -2  low statistics in few channels; for these channels OCDB value will be written back WARNING
245   // for cfd
246   // 200 >3 histos are empty 
247   // -11 if less 3 channels are empty ;for these channels OCDB value will be written back WARNING
248   // -22  low statistics in few channels; for these channels OCDB value will be written back WARNING
249    //
250   // compute offline equalized time
251   Float_t meandiff, sigmadiff, meanver, meancfdtime, sigmacfdtime;
252   meandiff = sigmadiff =  meanver = meancfdtime = sigmacfdtime =0;
253   Int_t nent=0;
254   Int_t ok = 0;
255   Int_t okdiff=0;
256   Int_t okcfd=0;
257   TH1F *cfddiff = NULL; 
258   TH1F *cfdtime = NULL;
259   TObjArray * TzeroObj = NULL;
260
261   gFile = TFile::Open(filePhys);
262   if(!gFile) {
263     AliError("No input PHYS data found ");
264     ok = 1000;
265     return ok;
266   }
267   else
268     {
269       meandiff = sigmadiff =  meanver = meancfdtime = sigmacfdtime =0;
270       TDirectory *dr = (TDirectory*) gFile->Get("T0Calib");
271       if (dr)   TzeroObj = (TObjArray*) dr->Get("T0Calib");
272       for (Int_t i=0; i<24; i++)
273         {
274           if (i != badpmt) {
275             if(TzeroObj) {
276               cfddiff = (TH1F*)TzeroObj->FindObject(Form("CFD1minCFD%d",i+1));
277               cfdtime = (TH1F*)TzeroObj->FindObject(Form("CFD%d",i+1));
278             }
279             else
280               {
281                 cfddiff = (TH1F*)gFile->Get(Form("CFD1minCFD%d",i+1));
282                 cfdtime = (TH1F*)gFile->Get(Form("CFD%d",i+1));
283                 
284               }
285             if(!cfddiff ) {
286               AliWarning(Form("no  histograms collected by pass0 for diff channel %i", i));    
287               okdiff++;
288               if(okdiff<4) {
289                 meandiff = timecdb[i];
290                 sigmadiff = 0;
291                 ok = -1;
292               }
293               else
294                 return 100+okdiff; 
295             }
296             if(cfddiff) {
297               nent = Int_t(cfddiff->GetEntries());
298               if ( nent == 0) {
299                 okdiff++;
300                 if(okdiff<4) {
301                   meandiff = timecdb[i];
302                   sigmadiff = 0;
303                   ok = - 1;
304                 }
305                 else
306                   return 100; 
307               }       
308               if(nent<100 && nent>0) { //!!!!!
309                 AliWarning(Form(" Not  enouph data in PMT %i- PMT1:  %i ", i, nent));
310                 ok=-2;
311                 meandiff = timecdb[i];
312                 sigmadiff = 0; 
313               }
314               if(nent>100 )  { //!!!!!
315                 {
316                   if(cfddiff->GetRMS()>1.5 )
317                     GetMeanAndSigma(cfddiff, meandiff, sigmadiff);
318                   if(cfddiff->GetRMS()<=1.5) 
319                     {
320                       meandiff = cfddiff->GetMean();
321                       sigmadiff = cfddiff->GetRMS();
322                     }
323                   Int_t   maxBin = cfddiff->GetMaximumBin(); 
324                   Double_t  meanEstimate = cfddiff->GetBinCenter( maxBin); 
325                   if(TMath::Abs(meanEstimate - meandiff) > 20 ) meandiff = meanEstimate; 
326                 }
327               }     
328             }
329             if(!cfdtime ) {
330               AliWarning(Form("no  histograms collected by pass0 for time channel %i", i));
331               meancfdtime = cfdvalue[i];
332               okcfd++;
333               if(okcfd<4) {
334                 meancfdtime = cfdvalue[i];
335                 ok = -11;
336               }
337               else {
338                 AliError(Form("no  histograms collected by pass0 for time %i channels ", okcfd));
339                 return 200; 
340               }
341             }
342             if(cfdtime) {
343               nent = Int_t(cfdtime->GetEntries());
344               if (nent == 0 || 
345                   (cfdtime->GetRMS() == 0 || cfdtime->GetMean() ==0 ) ) 
346                 {
347                   okcfd++;
348                   if(okcfd<4) {
349                     meancfdtime = cfdvalue[i];
350                     ok = -11;
351                     printf("!!!!bad data:: pmt %i nent%i RMS %f mean %f cdbtime %f \n",
352                            i, nent, cfdtime->GetRMS(), cfdtime->GetMean(),  fCFDvalue[i][0]);
353                   }
354                   else
355                     {
356                     printf("!!!!fatal data:: pmt %i nent%i RMS %f mean %f cdbtime %f \n",
357                            i, nent, cfdtime->GetRMS(), cfdtime->GetMean(),  fCFDvalue[i][0]);
358                       AliError(Form(" histograms collected by pass0 for time %i channels are empty", okcfd));
359                       return 200; 
360                     }
361                 }
362               
363                   
364               if(nent<100 && nent>0 ) 
365                 {
366                 AliWarning(Form(" Not  enouph data in PMT in CFD peak %i - %i ", i, nent));
367                 if(okcfd<4) {
368                   meancfdtime = cfdvalue[i];
369                   ok = -22;
370                   printf("!!!!low statstics:: pmt %i nent%i RMS %f mean %f cdbtime %f \n",
371                          i, nent, cfdtime->GetRMS(), cfdtime->GetMean(),  fCFDvalue[i][0]);
372                 }
373                 else {
374                   printf("!!!!low fatal statstics:: pmt %i nent%i RMS %f mean %f cdbtime %f \n",
375                          i, nent, cfdtime->GetRMS(), cfdtime->GetMean(),  fCFDvalue[i][0]);
376
377                   AliError(Form(" Not  enouph data in PMT in CFD peak %i in channels ", okcfd));
378                   return 200; 
379                 }
380
381               }
382
383               if( nent>100 && cfdtime->GetRMS() != 0 )    { //!!!!!
384                 if(cfdtime->GetRMS()>1.5 )
385                   GetMeanAndSigma(cfdtime,meancfdtime, sigmacfdtime);
386                 if(cfdtime->GetRMS()<=1.5) 
387                   {
388                     meancfdtime = cfdtime->GetMean();
389                     sigmacfdtime=cfdtime->GetRMS();
390                   }
391                 Int_t   maxBin = cfdtime->GetMaximumBin(); 
392                 Double_t  meanEstimate = cfdtime->GetBinCenter( maxBin); 
393                 if(TMath::Abs(meanEstimate - meancfdtime) > 20 ) meancfdtime = meanEstimate; 
394             }
395           }
396           
397           SetTimeEq(i,meandiff);
398           SetTimeEqRms(i,sigmadiff);
399           SetCFDvalue(i,0, meancfdtime );
400           printf(" pmt %i diff %f sigma %f meancfdtime %f cdbtime %f \n",i, meandiff, sigmadiff, meancfdtime, fCFDvalue[i][0]);
401           if (cfddiff) cfddiff->Reset();
402           if (cfdtime) cfdtime->Reset();
403           } //bad pmt
404         }
405       
406       gFile->Close();
407       delete gFile;
408
409     }
410     return ok; 
411    }
412
413 //________________________________________________________________________
414 void AliT0CalibTimeEq::GetMeanAndSigma(TH1F* hist,  Float_t &mean, Float_t &sigma) {
415   
416   const double window = 2.;  //fit window 
417   
418   double meanEstimate, sigmaEstimate; 
419   int maxBin;
420   maxBin        =  hist->GetMaximumBin(); //position of maximum
421   meanEstimate  =  hist->GetBinCenter( maxBin); // mean of gaussian sitting in maximum
422   // sigmaEstimate = hist->GetRMS();
423   sigmaEstimate = 10;
424   TF1* fit= new TF1("fit","gaus", meanEstimate - window*sigmaEstimate, meanEstimate + window*sigmaEstimate);
425   fit->SetParameters(hist->GetBinContent(maxBin), meanEstimate, sigmaEstimate);
426   hist->Fit("fit","RQ","Q");
427
428   mean  = (Float_t) fit->GetParameter(1);
429   sigma = (Float_t) fit->GetParameter(2);
430
431   delete fit;
432 }
433
434