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