]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliQACheckerBase.cxx
Additional cuts for leading particle alignment in event mixing. (Alessandro Grelli)
[u/mrichter/AliRoot.git] / STEER / AliQACheckerBase.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
17 /* $Id$ */
18
19 //
20 //  Base class for detectors quality assurance checkers 
21 //  Compares Data made by QADataMakers with reference data
22 //  Y. Schutz CERN August 2007
23 //
24
25 // --- ROOT system ---
26 #include <TCanvas.h>
27 #include <TClass.h>
28 #include <TH1F.h> 
29 #include <TH1I.h> 
30 #include <TIterator.h> 
31 #include <TKey.h> 
32 #include <TFile.h> 
33 #include <TList.h>
34 #include <TNtupleD.h>
35 #include <TPaveText.h>
36
37 // --- Standard library ---
38
39 // --- AliRoot header files ---
40 #include "AliLog.h"
41 #include "AliQAv1.h"
42 #include "AliQAChecker.h"
43 #include "AliQACheckerBase.h"
44 #include "AliQADataMaker.h"
45 #include "AliDetectorRecoParam.h"
46
47 ClassImp(AliQACheckerBase)
48
49            
50 //____________________________________________________________________________ 
51 AliQACheckerBase::AliQACheckerBase(const char * name, const char * title) : 
52   TNamed(name, title), 
53   fDataSubDir(0x0),
54   fRefSubDir(0x0), 
55   fRefOCDBSubDir(new TObjArray*[AliRecoParam::kNSpecies]), 
56   fLowTestValue(0x0),
57   fUpTestValue(0x0),
58   fImage(new TCanvas*[AliRecoParam::kNSpecies]), 
59   fPrintImage(kTRUE)
60 {
61   // ctor
62   fLowTestValue = new Float_t[AliQAv1::kNBIT] ; 
63   fUpTestValue  = new Float_t[AliQAv1::kNBIT] ; 
64   fLowTestValue[AliQAv1::kINFO]    =  0.5   ; 
65   fUpTestValue[AliQAv1::kINFO]     = 1.0 ; 
66   fLowTestValue[AliQAv1::kWARNING] =  0.002 ; 
67   fUpTestValue[AliQAv1::kWARNING]  = 0.5 ; 
68   fLowTestValue[AliQAv1::kERROR]   =  0.0   ; 
69   fUpTestValue[AliQAv1::kERROR]    = 0.002 ; 
70   fLowTestValue[AliQAv1::kFATAL]   = -1.0   ; 
71   fUpTestValue[AliQAv1::kFATAL]    = 0.0 ; 
72   
73   AliDebug(AliQAv1::GetQADebugLevel(), "Default setting is:") ;
74   if ( AliDebugLevel()  == AliQAv1::GetQADebugLevel() ) {
75     const Char_t * text= Form(" INFO    -> %1.5f <  value <  %1.5f  WARNING -> %1.5f <  value <= %1.5f \n ERROR   -> %1.5f <  value <= %1.5f \n FATAL   -> %1.5f <= value <  %1.5f \n", 
76                               fLowTestValue[AliQAv1::kINFO], fUpTestValue[AliQAv1::kINFO], 
77                               fLowTestValue[AliQAv1::kWARNING], fUpTestValue[AliQAv1::kWARNING], 
78                               fLowTestValue[AliQAv1::kERROR], fUpTestValue[AliQAv1::kERROR], 
79                               fLowTestValue[AliQAv1::kFATAL], fUpTestValue[AliQAv1::kFATAL]) ; 
80     AliInfo(Form("%s", text)) ; 
81   }
82   
83   for (Int_t specie = 0 ; specie < AliRecoParam::kNSpecies ; specie++) {
84     fImage[specie] = NULL ; 
85     fRefOCDBSubDir[specie] = NULL ;
86   }
87 }
88
89 //____________________________________________________________________________ 
90 AliQACheckerBase::AliQACheckerBase(const AliQACheckerBase& qac) :
91   TNamed(qac.GetName(), qac.GetTitle()),
92   fDataSubDir(qac.fDataSubDir), 
93   fRefSubDir(qac.fRefSubDir), 
94   fRefOCDBSubDir(qac.fRefOCDBSubDir), 
95   fLowTestValue(qac.fLowTestValue),
96   fUpTestValue(qac.fLowTestValue), 
97   fImage(NULL),  
98   fPrintImage(kTRUE)
99 {
100   //copy ctor
101   for (Int_t index = 0 ; index < AliQAv1::kNBIT ; index++) {
102     fLowTestValue[index]  = qac.fLowTestValue[index] ; 
103     fUpTestValue[index] = qac.fUpTestValue[index] ; 
104   }
105     for (Int_t specie = 0 ; specie < AliRecoParam::kNSpecies ; specie++) {
106       fImage[specie] = qac.fImage[specie] ; 
107       fRefOCDBSubDir[specie] = qac.fRefOCDBSubDir[specie] ; 
108     }
109 }
110
111 //____________________________________________________________________________
112 AliQACheckerBase& AliQACheckerBase::operator = (const AliQACheckerBase& qac )
113 {
114   // Equal operator.
115   this->~AliQACheckerBase();
116   new(this) AliQACheckerBase(qac);
117   return *this;
118 }
119
120 //____________________________________________________________________________ 
121 AliQACheckerBase::~AliQACheckerBase()
122 {
123   delete [] fLowTestValue ; 
124   delete [] fUpTestValue ; 
125   for (Int_t esIndex = 0 ; esIndex < AliRecoParam::kNSpecies ; esIndex++) {
126     if ( fImage[esIndex] ) 
127       delete fImage[esIndex] ;
128     if ( fRefOCDBSubDir[esIndex] ) 
129       delete fRefOCDBSubDir[esIndex] ; 
130   }
131   delete[] fImage ; 
132   delete[] fRefOCDBSubDir ; 
133 }
134
135 //____________________________________________________________________________
136 Double_t * AliQACheckerBase::Check(AliQAv1::ALITASK_t index, AliDetectorRecoParam * recoParam) 
137 {
138   // Performs a basic checking
139   // Compares all the histograms stored in the directory
140   // With reference histograms either in a file of in OCDB  
141
142   TObjArray ** list = new TObjArray *[AliRecoParam::kNSpecies] ; 
143   Int_t specie ;
144   for (specie = 0 ; specie < AliRecoParam::kNSpecies ; specie++) {
145     list[specie] =  new TObjArray(AliQAv1::GetMaxQAObj()) ; 
146     if ( !AliQAv1::Instance()->IsEventSpecieSet(specie) ) 
147       continue ; 
148     if (fDataSubDir) {
149       TList * keyList = fDataSubDir->GetListOfKeys() ; 
150       TIter next(keyList) ; 
151       TKey * key ;
152       while ( (key = static_cast<TKey *>(next())) ) {
153         TDirectory * specieDir = fDataSubDir->GetDirectory(key->GetName()) ; 
154         TList * keykeyList = specieDir->GetListOfKeys() ; 
155         TIter next2(keykeyList) ; 
156         TKey * keykey ;
157         while ( (keykey = static_cast<TKey *>(next2())) ) {
158           TObject * odata = specieDir->Get(keykey->GetName()) ; 
159           if ( odata->IsA()->InheritsFrom("TH1") ) {
160             TH1 * hdata = static_cast<TH1*>(odata) ;
161             list[specie]->Add(hdata) ; 
162           } else if (!odata->IsA()->InheritsFrom("TDirectory")) // skip the expert directory
163             AliError(Form("%s Is a Classname that cannot be processed", key->GetClassName())) ;
164         }
165       }
166     }
167   }
168  
169   Double_t * test = Check(index, list, recoParam) ;
170   
171   delete[] list ; 
172     
173   return test ;
174 }  
175
176 //____________________________________________________________________________
177 Double_t * AliQACheckerBase::Check(AliQAv1::ALITASK_t /*index*/, TObjArray ** list, AliDetectorRecoParam * /*recoParam*/) 
178 {
179   // Performs a basic checking
180   // Compares all the histograms in the list
181
182         Double_t * test = new Double_t[AliRecoParam::kNSpecies] ;
183         Int_t count[AliRecoParam::kNSpecies]   = { 0 }; 
184
185   for (Int_t specie = 0 ; specie < AliRecoParam::kNSpecies ; specie++) {
186     test[specie] = 1.0 ; 
187     if ( !AliQAv1::Instance()->IsEventSpecieSet(specie)) 
188       continue ; 
189     if (list[specie]->GetEntries() == 0)  
190       test[specie] = 0. ; // nothing to check
191     else {
192       if (!fRefSubDir && !fRefOCDBSubDir)
193         test[specie] = -1 ; // no reference data
194       else {
195         TIter next(list[specie]) ; 
196         TH1 * hdata ;
197         count[specie] = 0 ; 
198         while ( (hdata = static_cast<TH1 *>(next())) ) {
199           TString cln(hdata->ClassName()) ; 
200           if ( cln.Contains("TH1") ) {
201             if ( hdata) { 
202               if ( hdata->TestBit(AliQAv1::GetExpertBit()) )  // does not perform the test for expert data
203                 continue ; 
204               TH1 * href = NULL ; 
205               if (fRefSubDir) 
206                 href  = static_cast<TH1*>(fRefSubDir->Get(hdata->GetName())) ;
207               else if (fRefOCDBSubDir[specie])
208                 href  = static_cast<TH1*>(fRefOCDBSubDir[specie]->FindObject(hdata->GetName())) ;
209               if (!href) 
210                 test[specie] = -1 ; // no reference data ; 
211               else {
212                 Double_t rv =  DiffK(hdata, href) ;
213                 AliDebug(AliQAv1::GetQADebugLevel(), Form("%s ->Test = %f", hdata->GetName(), rv)) ; 
214                 test[specie] += rv ; 
215                 count[specie]++ ;
216               }
217             }
218           } else
219             AliError("Data type cannot be processed") ;
220           if (count[specie] != 0) 
221             test[specie] /= count[specie] ;
222         }
223       }
224     }
225   }
226   return test ;
227 }  
228
229
230 //____________________________________________________________________________ 
231 Double_t AliQACheckerBase::DiffC(const TH1 * href, const TH1 * hin) const
232 {
233   // compares two histograms using the Chi2 test
234   if ( hin->Integral() == 0 ) {
235     AliDebug(AliQAv1::GetQADebugLevel(), Form("Spectrum %s is empty", hin->GetName())) ; 
236     return 0. ;
237   }
238     
239   return hin->Chi2Test(href) ;  
240 }
241
242 //____________________________________________________________________________ 
243 Double_t AliQACheckerBase::DiffK(const TH1 * href, const TH1 * hin) const
244 {
245   // compares two histograms using the Kolmogorov test
246   if ( hin->Integral() == 0 || href->Integral() == 0) {
247     AliDebug(AliQAv1::GetQADebugLevel(), Form("Spectrum %s or its reference is empty", hin->GetName())) ; 
248     return 0. ;
249   }
250     
251   return hin->KolmogorovTest(href) ;  
252 }
253
254 //____________________________________________________________________________
255 void AliQACheckerBase::Run(AliQAv1::ALITASK_t index, AliDetectorRecoParam * recoParam) 
256
257         AliDebug(AliQAv1::GetQADebugLevel(), Form("Processing %s", AliQAv1::GetAliTaskName(index))) ; 
258   
259         Double_t * rv = NULL ;
260   rv = Check(index, recoParam) ;
261         SetQA(index, rv) ;      
262         
263   AliDebug(AliQAv1::GetQADebugLevel(), Form("Test result of %s", AliQAv1::GetAliTaskName(index))) ;
264         
265   if (rv) 
266     delete [] rv ; 
267   Finish() ; 
268 }
269
270 //____________________________________________________________________________
271 void AliQACheckerBase::Run(AliQAv1::ALITASK_t index, TObjArray ** list, AliDetectorRecoParam * recoParam) 
272
273         AliDebug(AliQAv1::GetQADebugLevel(), Form("Processing %s", AliQAv1::GetAliTaskName(index))) ; 
274   
275         Double_t * rv = NULL ;
276   rv = Check(index, list, recoParam) ;
277         SetQA(index, rv) ;      
278         
279   AliDebug(AliQAv1::GetQADebugLevel(), Form("Test result of %s", AliQAv1::GetAliTaskName(index))) ;
280         
281   if (rv) 
282     delete [] rv ; 
283   Finish() ; 
284 }
285
286 //____________________________________________________________________________
287 void AliQACheckerBase::Finish() const 
288 {
289         // wrap up and save QA in proper file
290         AliQAv1 * qa = AliQAv1::Instance() ; 
291         //qa->Show() ;
292         AliQAv1::GetQAResultFile()->cd() ; 
293         qa->Write(qa->GetName(), kWriteDelete) ;   
294         AliQAv1::GetQAResultFile()->Close() ; 
295 }
296
297 //____________________________________________________________________________ 
298 void AliQACheckerBase::MakeImage( TObjArray ** list, AliQAv1::TASKINDEX_t task, AliQAv1::MODE_t mode) 
299 {
300   // makes the QA image for sim and rec
301   Int_t nImages = 0 ;
302   for (Int_t esIndex = 0 ; esIndex < AliRecoParam::kNSpecies ; esIndex++) {
303     if (! AliQAv1::Instance(AliQAv1::GetDetIndex(GetName()))->IsEventSpecieSet(AliRecoParam::ConvertIndex(esIndex)) ) 
304       continue ;
305     TIter next(list[esIndex]) ;  
306     TH1 * hdata = NULL ; 
307     while ( (hdata=static_cast<TH1 *>(next())) ) {
308       TString cln(hdata->ClassName()) ; 
309       if ( ! cln.Contains("TH") )
310         continue ; 
311       if ( hdata->TestBit(AliQAv1::GetImageBit()) )
312         nImages++; 
313     }
314     break ; 
315   }
316   if ( nImages == 0 ) {
317     AliDebug(AliQAv1::GetQADebugLevel(), Form("No histogram will be plotted for %s %s\n", GetName(), AliQAv1::GetTaskName(task).Data())) ;  
318   } else {
319     AliDebug(AliQAv1::GetQADebugLevel(), Form("%d histograms will be plotted for %s %s\n", nImages, GetName(), AliQAv1::GetTaskName(task).Data())) ;  
320     for (Int_t esIndex = 0 ; esIndex < AliRecoParam::kNSpecies ; esIndex++) {
321       if (! AliQAv1::Instance(AliQAv1::GetDetIndex(GetName()))->IsEventSpecieSet(AliRecoParam::ConvertIndex(esIndex)) || list[esIndex]->GetEntries() == 0) 
322         continue ;
323       const Char_t * title = Form("QA_%s_%s_%s", GetName(), AliQAv1::GetTaskName(task).Data(), AliRecoParam::GetEventSpecieName(esIndex)) ; 
324       if ( !fImage[esIndex] ) {
325         fImage[esIndex] = new TCanvas(title, title) ;
326       }
327       fImage[esIndex]->Clear() ; 
328       fImage[esIndex]->SetTitle(title) ; 
329       fImage[esIndex]->cd() ; 
330       TPaveText someText(0.015, 0.015, 0.98, 0.98) ;
331       someText.AddText(title) ;
332       someText.Draw() ; 
333       fImage[esIndex]->Print(Form("%s%s%d.%s", AliQAv1::GetImageFileName(), AliQAv1::GetModeName(mode), AliQAChecker::Instance()->GetRunNumber(), AliQAv1::GetImageFileFormat())) ; 
334       fImage[esIndex]->Clear() ; 
335       Int_t nx = TMath::Sqrt(nImages) ; 
336       Int_t ny = nx  ; 
337       while ( nx*ny <= nImages) 
338         ny++ ; 
339       
340       fImage[esIndex]->Divide(nx, ny) ; 
341       TIter nexthist(list[esIndex]) ; 
342       TH1* hist = NULL ;
343       Int_t npad = 1 ; 
344       fImage[esIndex]->cd(npad) ; 
345       while ( (hist=static_cast<TH1*>(nexthist())) ) {
346         TString cln(hist->ClassName()) ; 
347         if ( ! cln.Contains("TH") )
348           continue ; 
349         if(hist->TestBit(AliQAv1::GetImageBit())) {
350           hist->Draw() ; 
351           fImage[esIndex]->cd(++npad) ; 
352         }
353       }
354       fImage[esIndex]->Print(Form("%s%s%d.%s", AliQAv1::GetImageFileName(), AliQAv1::GetModeName(mode), AliQAChecker::Instance()->GetRunNumber(), AliQAv1::GetImageFileFormat())) ; 
355     }
356   }  
357 }
358
359 //____________________________________________________________________________
360 void AliQACheckerBase::SetHiLo(Float_t * hiValue, Float_t * lowValue) 
361 {
362   AliDebug(AliQAv1::GetQADebugLevel(), "Previous setting was:") ;
363   if ( AliDebugLevel() == AliQAv1::GetQADebugLevel() ) {
364     const Char_t * text= Form(" INFO    -> %1.5f <  value <  %1.5f  WARNING -> %1.5f <  value <= %1.5f \n ERROR   -> %1.5f <  value <= %1.5f \n FATAL   -> %1.5f <= value <  %1.5f \n", 
365                               fLowTestValue[AliQAv1::kINFO], fUpTestValue[AliQAv1::kINFO], 
366                               fLowTestValue[AliQAv1::kWARNING], fUpTestValue[AliQAv1::kWARNING], 
367                               fLowTestValue[AliQAv1::kERROR], fUpTestValue[AliQAv1::kERROR], 
368                               fLowTestValue[AliQAv1::kFATAL], fUpTestValue[AliQAv1::kFATAL]) ; 
369     AliInfo(Form("%s", text)) ; 
370   }
371   
372   for (Int_t index = 0 ; index < AliQAv1::kNBIT ; index++) {
373     fLowTestValue[index]  = lowValue[index] ; 
374     fUpTestValue[index]   = hiValue[index] ; 
375   }
376   AliDebug(AliQAv1::GetQADebugLevel(), "Current setting is:") ;
377   if ( AliDebugLevel()  == AliQAv1::GetQADebugLevel() ) {
378     const Char_t * text= Form(" INFO    -> %1.5f <  value <  %1.5f  WARNING -> %1.5f <  value <= %1.5f \n ERROR   -> %1.5f <  value <= %1.5f \n FATAL   -> %1.5f <= value <  %1.5f \n", 
379                               fLowTestValue[AliQAv1::kINFO], fUpTestValue[AliQAv1::kINFO], 
380                               fLowTestValue[AliQAv1::kWARNING], fUpTestValue[AliQAv1::kWARNING], 
381                               fLowTestValue[AliQAv1::kERROR], fUpTestValue[AliQAv1::kERROR], 
382                               fLowTestValue[AliQAv1::kFATAL], fUpTestValue[AliQAv1::kFATAL]) ;     AliInfo(Form("%s", text)) ; 
383   }
384 }
385
386 //____________________________________________________________________________
387 void AliQACheckerBase::SetQA(AliQAv1::ALITASK_t index, Double_t * value) const
388 {
389         // sets the QA according the return value of the Check
390
391   AliQAv1 * qa = AliQAv1::Instance(index) ;
392   for (Int_t specie = 0 ; specie < AliRecoParam::kNSpecies ; specie++) {
393     if (  value == NULL ) { // No checker is implemented, set all QA to Fatal
394       qa->Set(AliQAv1::kFATAL, specie) ; 
395     } else {
396       if ( value[specie] >= fLowTestValue[AliQAv1::kFATAL] && value[specie] < fUpTestValue[AliQAv1::kFATAL] ) 
397         qa->Set(AliQAv1::kFATAL, specie) ; 
398       else if ( value[specie] > fLowTestValue[AliQAv1::kERROR] && value[specie] <= fUpTestValue[AliQAv1::kERROR]  )
399         qa->Set(AliQAv1::kERROR, specie) ; 
400       else if ( value[specie] > fLowTestValue[AliQAv1::kWARNING] && value[specie] <= fUpTestValue[AliQAv1::kWARNING]  )
401         qa->Set(AliQAv1::kWARNING, specie) ;
402       else if ( value[specie] > fLowTestValue[AliQAv1::kINFO] && value[specie] <= fUpTestValue[AliQAv1::kINFO] ) 
403         qa->Set(AliQAv1::kINFO, specie) ;       
404     }
405   }
406 }