]> git.uio.no Git - u/mrichter/AliRoot.git/blob - ESDCheck/AliFMDQATask.cxx
Adding OpenFile in CreateOutput (Yves)
[u/mrichter/AliRoot.git] / ESDCheck / AliFMDQATask.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 // An analysis task to check the FMD data in simulated data
20 //
21 //*-- Hans Hjersing Dalsgaard 
22 //////////////////////////////////////////////////////////////////////////////
23
24 #include <TCanvas.h> 
25 #include <TChain.h>
26 #include <TF1.h> 
27 #include <TFile.h> 
28 #include <TH1D.h> 
29 #include <TROOT.h>
30
31 #include "AliFMDQATask.h" 
32 #include "AliESD.h" 
33 #include "AliLog.h"
34
35 //______________________________________________________________________________
36 AliFMDQATask::AliFMDQATask(const char *name) : 
37   AliAnalysisTask(name,""),  
38   fChain(0),
39   fESD(0), 
40   fhFMD1i(0),
41   fhFMD2i(0), 
42   fhFMD2o(0), 
43   fhFMD3i(0), 
44   fhFMD3o(0) 
45 {
46   // Constructor.
47   // Input slot #0 works with an Ntuple
48   DefineInput(0, TChain::Class());
49   // Output slot #0 writes into a TH1 container
50   DefineOutput(0,  TObjArray::Class()) ; 
51 }
52
53 //______________________________________________________________________________
54 AliFMDQATask::~AliFMDQATask()
55 {
56   // dtor
57
58   fOutputContainer->Clear() ; 
59   delete fOutputContainer ; 
60
61   delete fhFMD1i ;
62   delete fhFMD2i ; 
63   delete fhFMD2o ; 
64   delete fhFMD3i ; 
65   delete fhFMD3o ;
66
67 }
68
69 //______________________________________________________________________________
70 void AliFMDQATask::ConnectInputData(const Option_t*)
71 {
72   // Initialisation of branch container and histograms 
73     
74   AliInfo(Form("*** Initialization of %s", GetName())) ; 
75   
76   // Get input data
77   fChain = dynamic_cast<TChain *>(GetInputData(0)) ;
78   if (!fChain) {
79     AliError(Form("Input 0 for %s not found\n", GetName()));
80     return ;
81   }
82   
83   // One should first check if the branch address was taken by some other task
84   char ** address = (char **)GetBranchAddress(0, "ESD");
85   if (address) {
86     fESD = (AliESD*)(*address);
87   } else {
88     fESD = new AliESD();
89     SetBranchAddress(0, "ESD", &fESD);
90   }
91 }
92
93 //________________________________________________________________________
94 void AliFMDQATask::CreateOutputObjects()
95 {  
96   // create histograms 
97
98   OpenFile(0) ; 
99
100   fhFMD1i = new TH1D("FMD1i", "FMD1i", 100, -0.5, 3);
101   fhFMD2i = new TH1D("FMD2i", "FMD2i", 100, -0.5, 3);
102   fhFMD2o = new TH1D("FMD2o", "FMD2o", 100, -0.5, 3);
103   fhFMD3i = new TH1D("FMD3i", "FMD3i", 100, -0.5, 3);
104   fhFMD3o = new TH1D("FMD3o", "FMD3o", 100, -0.5, 3);
105
106   // create output container
107   
108   fOutputContainer = new TObjArray(5) ; 
109   fOutputContainer->SetName(GetName()) ; 
110
111   fOutputContainer->AddAt(fhFMD1i,             0) ; 
112   fOutputContainer->AddAt(fhFMD2i,             1) ; 
113   fOutputContainer->AddAt(fhFMD2o,             2) ; 
114   fOutputContainer->AddAt(fhFMD3i,             3) ; 
115   fOutputContainer->AddAt(fhFMD3o,             4) ; 
116 }
117
118 //______________________________________________________________________________
119 void AliFMDQATask::Exec(Option_t *) 
120 {
121   // Processing of one event
122     
123   Long64_t entry = fChain->GetReadEntry() ;
124   
125   if (!fESD) {
126     AliError("fESD is not connected to the input!") ; 
127     return ; 
128   }
129   
130   TFile * currentFile = (dynamic_cast<TChain *>(fChain))->GetFile() ; 
131   if ( !((entry-1)%100) ) 
132     AliInfo(Form("%s ----> Processing event # %lld", currentFile->GetName(), entry)) ; 
133             
134   // ************************  FMD *************************************
135   
136  AliESDFMD * fmd = fESD->GetFMDData() ;
137   
138  //fmd->CheckNeedUShort(currentFile);
139
140   Int_t nFMD1 = 0, nFMD2i = 0, nFMD2o = 0, nFMD3i = 0, nFMD3o = 0 ;
141   
142   UShort_t detector = 1 ;
143   for(detector = 1 ; detector <= fmd->MaxDetectors() ; detector++) {
144     Char_t ring = 'O' ;
145     UShort_t sector ; 
146     for(sector = 0 ;sector < fmd->MaxSectors() ; sector++) {
147       UShort_t strip ;
148       for(strip = 0 ; strip < fmd->MaxStrips(); strip++) {
149         if(fmd->Multiplicity(detector, ring, sector, strip) != AliESDFMD::kInvalidMult)
150           RingSelector(detector, ring, fmd->Multiplicity(detector, ring, sector, strip)) ;
151         if( (fmd->Multiplicity(detector, ring, sector, strip) == AliESDFMD::kInvalidMult) && detector == 2 )
152           nFMD2o++ ;
153         if( (fmd->Multiplicity(detector, ring, sector, strip)==AliESDFMD::kInvalidMult) && detector == 3 )
154           nFMD3o++ ;
155       }
156     }
157     ring='I';
158     for(sector = 0; sector < fmd->MaxSectors(); sector++) {
159       UShort_t strip ;
160       for(strip = 0 ; strip < fmd->MaxStrips() ; strip++) {
161         if(fmd->Multiplicity(detector, ring, sector, strip) != AliESDFMD::kInvalidMult)
162           RingSelector(detector, ring, fmd->Multiplicity(detector, ring, sector, strip));
163         if( (fmd->Multiplicity(detector, ring, sector, strip) == AliESDFMD::kInvalidMult) && detector == 1 )
164           nFMD1++;
165         if( (fmd->Multiplicity(detector, ring, sector, strip) == AliESDFMD::kInvalidMult) && detector == 2 )
166           nFMD2i++;
167         if( (fmd->Multiplicity(detector, ring, sector, strip) == AliESDFMD::kInvalidMult) && detector == 3 )
168           nFMD3i++;
169       }     
170     }
171   }
172
173   if(nFMD1>100+10240)
174     AliWarning(Form("number of missing strips in FMD1i too high in event number %lld in file", entry, fChain->GetCurrentFile()->GetName())) ;
175   if(nFMD2i>100+10240)
176     AliWarning(Form("number of missing strips in FMD2i too high in event number %lld in file", entry, fChain->GetCurrentFile()->GetName())) ;
177   if(nFMD2o>100+10240)
178     AliWarning(Form("number of missing strips in FMD2o too high in event number %lld in file", entry, fChain->GetCurrentFile()->GetName())) ;
179   if(nFMD3i>100+10240)
180     AliWarning(Form("number of missing strips in FMD3i too high in event number %lld in file", entry, fChain->GetCurrentFile()->GetName())) ;
181   if(nFMD3o>100+10240)
182     AliWarning(Form("number of missing strips in FMD3o too high in event number %lld in file", entry, fChain->GetCurrentFile()->GetName())) ;
183   
184   PostData(0, fOutputContainer);
185 }
186
187 //______________________________________________________________________________
188 void AliFMDQATask::Terminate(Option_t *)
189 {
190   // Processing when the event loop is ended
191  
192   fOutputContainer = (TObjArray*)GetOutputData(0);
193   fhFMD1i = (TH1D*)fOutputContainer->At(0);
194   fhFMD2i = (TH1D*)fOutputContainer->At(1);
195   fhFMD2o = (TH1D*)fOutputContainer->At(2);
196   fhFMD3i = (TH1D*)fOutputContainer->At(3);
197   fhFMD3o = (TH1D*)fOutputContainer->At(4);
198
199   TCanvas * cFMD1 = new TCanvas("cFMD1", "FMD ESD Test", 400, 10, 600, 700);
200   cFMD1->Divide(3, 2) ; 
201
202   cFMD1->cd(1) ;; 
203   fhFMD1i->Draw() ; 
204   
205   cFMD1->cd(2) ;; 
206   fhFMD2i->Draw() ; 
207
208   cFMD1->cd(3) ;; 
209   fhFMD2o->Draw() ; 
210
211   cFMD1->cd(4) ;; 
212   fhFMD3i->Draw() ; 
213
214   cFMD1->cd(5) ;; 
215   fhFMD3o->Draw() ; 
216   
217   cFMD1->Print("FMD.eps") ;
218
219   Bool_t rv1i = TestHisto(fhFMD1i) ;
220   Bool_t rv2i = TestHisto(fhFMD2i) ;
221   Bool_t rv2o = TestHisto(fhFMD2o) ;
222   Bool_t rv3i = TestHisto(fhFMD2i) ;
223   Bool_t rv3o = TestHisto(fhFMD2o) ;
224   
225   if (  !(rv1i * rv2i * rv2o * rv3i * rv3o) )
226     AliWarning("Possible problem in file !!! Check output!") ;
227
228
229
230   char line[1024] ; 
231   sprintf(line, ".!tar -zcvf %s.tar.gz *.eps", GetName()) ; 
232   gROOT->ProcessLine(line);
233   sprintf(line, ".!rm -fR *.eps"); 
234   gROOT->ProcessLine(line);
235  
236   AliInfo(Form("!!! All the eps files are in %s.tar.gz !!! \n", GetName())) ;
237 }
238
239 //______________________________________________________________________________
240 void AliFMDQATask::RingSelector(const UShort_t detector, const Char_t ring, const Float_t mult) const 
241
242   // fill the histograms for each ring in each detector layer
243
244   if(ring == 'I' && detector == 1)
245     fhFMD1i->Fill(mult) ;
246   if(ring == 'I' && detector == 2)
247     fhFMD2i->Fill(mult) ;
248   if(ring == 'O' && detector == 2)
249     fhFMD2o ->Fill(mult) ;
250   if(ring == 'I' && detector == 3)
251     fhFMD3i ->Fill(mult) ;
252   if(ring == 'O' && detector == 3)
253     fhFMD3o ->Fill(mult) ;
254 }
255 //______________________________________________________________________________
256 Bool_t AliFMDQATask::TestHisto(TH1D * hTest) const 
257 {  
258   // analyses the histogram with a Landau function
259
260   Float_t chiMax = 3, chiLow=0.5 ;
261   Float_t chiSq ;
262   Float_t mpv ;
263   Int_t   ndf ;
264   
265   FitAll(hTest, chiSq, ndf, mpv, chiMax, chiLow);
266
267   if( (chiSq > chiMax || chiSq < chiLow) || mpv < 0.6 || mpv > 1 ) {
268     hTest->Rebin(2) ;
269     FitAll(hTest, chiSq, ndf, mpv, chiMax, chiLow) ;
270   }
271
272   Bool_t   ret   = kFALSE ;
273   Char_t * test  = "not OK";
274   Char_t * test2 = "not OK";
275   
276   if(chiSq < chiMax && chiSq > chiLow)
277     test = "OK" ;
278   if(mpv > 0.6 && mpv < 1)
279     test2 = "OK" ;
280  
281   if(test == "OK" && test2 == "OK")
282     ret = kTRUE;
283   
284   if(test == "not OK" || test2 == "not OK") {
285     AliWarning("Bad fit results") ; 
286     printf("Detector : %s\n", hTest->GetName()) ;
287     printf("Landau fit Chi Square / NDF = %f / %d which is %s\n", chiSq*ndf, ndf, test) ; 
288     printf("Landau fit MPV is: %f which is %s\n", mpv, test2) ;
289   }
290   return ret;  
291 }
292 //______________________________________________________________________________
293 void AliFMDQATask::FitAll(TH1D* hTest, Float_t &chiSq, Int_t &ndf, Float_t &mpv, Float_t chiMax, Float_t chiLow ) const 
294 {
295   // fit a histogram with a Landau distribution and returns chi square
296
297   Float_t fitMax = hTest->GetXaxis()->GetXmax() ;
298
299   TH1D hTmp = *hTest ;
300   hTmp.SetAxisRange(0.4,fitMax) ;
301   Int_t   maxBin = hTmp.GetMaximumBin();
302   Float_t max    = hTmp.GetBinCenter(maxBin);
303  
304   hTest->Fit("landau", "QOI", "", max-0.3, fitMax) ;
305   TF1* fitfunc = hTest->GetFunction("landau") ;
306   chiSq = fitfunc->GetChisquare() / fitfunc->GetNDF() ;
307   mpv   = fitfunc->GetParameter(1) ;
308   ndf   = fitfunc->GetNDF() ;
309
310   if( ( chiSq > chiMax || chiSq < chiLow ) || ( mpv < 0.6 || mpv > 1 ) ) {
311     hTest->Fit("landau", "QOI", "", max-0.2, fitMax) ;
312     fitfunc = hTest->GetFunction("landau") ;
313     chiSq   = fitfunc->GetChisquare() / fitfunc->GetNDF() ;
314     mpv     = fitfunc->GetParameter(1) ;
315     ndf     = fitfunc->GetNDF() ;
316   }
317   if( ( chiSq >chiMax || chiSq < chiLow ) || ( mpv < 0.6 || mpv > 1 ) ) {
318     hTest->Fit("landau", "QOI", "", max-0.1, fitMax) ;
319     fitfunc = hTest->GetFunction("landau") ;
320     chiSq   = fitfunc->GetChisquare() / fitfunc->GetNDF() ;
321     mpv     = fitfunc->GetParameter(1) ;
322     ndf     = fitfunc->GetNDF() ;
323   }
324   if( ( chiSq > chiMax || chiSq <chiLow ) || ( mpv < 0.6 || mpv > 1 ) ) {
325     hTest->Fit("landau", "QOI", "", max, fitMax) ;
326     fitfunc = hTest->GetFunction("landau") ;
327     chiSq   = fitfunc->GetChisquare() / fitfunc->GetNDF() ;
328     mpv     = fitfunc->GetParameter(1) ;
329     ndf     = fitfunc->GetNDF(); 
330   }
331   if( ( chiSq > chiMax || chiSq < chiLow ) || ( mpv < 0.6 ||mpv > 1 ) ) {
332     hTest->Fit("landau", "QOI", "", max+0.1, fitMax) ;
333     fitfunc = hTest->GetFunction("landau") ;
334     chiSq   = fitfunc->GetChisquare() / fitfunc->GetNDF() ;
335     mpv     = fitfunc->GetParameter(1) ;
336     ndf     = fitfunc->GetNDF() ;
337   } 
338 }