]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliQAChecker.cxx
Previous commit had the bad side-effect of changing the behaviour of Raw QA to comput...
[u/mrichter/AliRoot.git] / STEER / AliQAChecker.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 running the Quality Assurance Checker
21 //                                                                           //
22 ///////////////////////////////////////////////////////////////////////////////
23
24 #include "AliCDBEntry.h"
25 #include "AliQAManager.h"
26 #include "AliCDBStorage.h"
27 #include "AliRunInfo.h" 
28 #include "AliLog.h"
29 #include "AliModule.h" 
30 #include "AliQAv1.h"
31 #include "AliQAChecker.h"
32 #include "AliQACheckerBase.h"
33 #include "AliCorrQAChecker.h"
34 #include "AliGlobalQAChecker.h"
35 #include "AliGRPObject.h"
36
37 #include <TKey.h>
38 #include <TObjArray.h>
39 #include <TObjString.h>
40 #include <TPluginManager.h> 
41 #include <TROOT.h>
42 #include <TStopwatch.h> 
43 #include <TString.h> 
44 #include <TSystem.h> 
45 #include <TList.h>
46 #include <TNtupleD.h>
47
48 ClassImp(AliQAChecker)
49   AliQAChecker * AliQAChecker::fgQAChecker = 0x0 ;
50
51 //_____________________________________________________________________________
52 AliQAChecker::AliQAChecker(const char* name, const char* title) :
53   TNamed(name, title),
54   fDataFile(0x0), 
55   fRunInfo(0x0), 
56   fRunInfoOwner(kFALSE), 
57   fRefFile(0x0), 
58   fFoundDetectors("."), 
59   fEventSpecie(AliRecoParam::kDefault), 
60   fRun(0)
61 {
62   // ctor: initialise checkers and open the data file   
63   for (Int_t det = 0 ; det < AliQAv1::kNDET ; det++) 
64     fCheckers[det] = NULL ; 
65 }
66
67 //_____________________________________________________________________________
68 AliQAChecker::AliQAChecker(const AliQAChecker& qac) :
69   TNamed(qac),
70   fDataFile(qac.fDataFile), 
71   fRunInfo(qac.fRunInfo), 
72   fRunInfoOwner(kFALSE),   
73   fRefFile(qac.fRefFile), 
74   fFoundDetectors(qac.fFoundDetectors),
75   fEventSpecie(qac.fEventSpecie),
76   fRun(qac.fRun)
77 {
78   // copy constructor
79   
80   for (Int_t det = 0 ; det < AliQAv1::kNDET ; det++) 
81     fCheckers[det] = NULL ; 
82 }
83
84 //_____________________________________________________________________________
85 AliQAChecker& AliQAChecker::operator = (const AliQAChecker& qac)
86 {
87 // assignment operator
88
89   this->~AliQAChecker();
90   new(this) AliQAChecker(qac);
91   return *this;
92 }
93
94 //_____________________________________________________________________________
95 AliQAChecker::~AliQAChecker()
96 {
97 // clean up
98   if (fRunInfo)
99     delete fRunInfo ; 
100   delete [] fCheckers ; 
101   AliQAv1::Close() ; 
102 }
103
104 //_____________________________________________________________________________
105   AliQACheckerBase * AliQAChecker::GetDetQAChecker(Int_t det)
106 {
107         // Gets the Quality Assurance checker for the detector specified by its name
108         
109         if (fCheckers[det]) 
110     return fCheckers[det];
111
112         AliQACheckerBase * qac = NULL ;
113
114         TString detName(AliQAv1::GetDetName(det)) ; 
115         
116         if (det == AliQAv1::kGLOBAL) {
117                 qac = new AliGlobalQAChecker() ; 
118         } else if (det == AliQAv1::kCORR) {
119                 qac = new AliCorrQAChecker() ; 
120         } else {
121                 AliDebugClass(AliQAv1::GetQADebugLevel(), Form("Retrieving QA checker for %s", detName.Data())) ; 
122                 TPluginManager* pluginManager = gROOT->GetPluginManager() ;
123                 TString qacName = "Ali" + detName + "QAChecker" ;
124
125                 // first check if a plugin is defined for the quality assurance checker
126                 TPluginHandler* pluginHandler = pluginManager->FindHandler("AliQAChecker", detName.Data());
127                 // if not, add a plugin for it
128                 if (!pluginHandler) {
129                         //AliDebug(AliQAv1::GetQADebugLevel(), Form("defining plugin for %s", qacName.Data()));
130                         TString libs = gSystem->GetLibraries();
131                 
132                         if (libs.Contains("lib" + detName + "base.so") || (gSystem->Load("lib" + detName + "base.so") >= 0))
133                                 pluginManager->AddHandler("AliQAChecker", detName, qacName, detName + "qac", qacName + "()");
134                         else 
135                                 pluginManager->AddHandler("AliQAChecker", detName, qacName, detName, qacName + "()");
136
137                         pluginHandler = pluginManager->FindHandler("AliQAChecker", detName);    
138
139                         if (pluginHandler && (pluginHandler->LoadPlugin() == 0)) 
140                                 qac = (AliQACheckerBase *) pluginHandler->ExecPlugin(0);
141   
142                 }
143         }
144         if (qac) 
145                 fCheckers[det] = qac ;
146         
147         return qac ; 
148 }
149  
150 //_____________________________________________________________________________
151 void AliQAChecker::GetRefSubDir(const char * det, const char * task, TDirectory *& dirFile, TObjArray **& dirOCDB)     
152
153   // Opens and returns the file with the reference data 
154   dirFile = NULL ; 
155   TString refStorage(AliQAv1::GetQARefStorage()) ;
156 //  if (refStorage.Contains(AliQAv1::GetLabLocalFile())) {      
157 //    refStorage.ReplaceAll(AliQAv1::GetLabLocalFile(), "") ; 
158 //    refStorage += AliQAv1::GetQARefFileName() ;
159 //    if ( fRefFile ) 
160 //      if ( fRefFile->IsOpen() ) 
161 //                                      fRefFile->Close() ; 
162 //    fRefFile = TFile::Open(refStorage.Data()) ; 
163 //    if (!fRefFile) { 
164 //      AliError(Form("Cannot find reference file %s", refStorage.Data())) ; 
165 //      dirFile = NULL ; 
166 //    }
167 //    dirFile = fRefFile->GetDirectory(det) ; 
168 //    if (!dirFile) {
169 //      AliWarning(Form("Directory %s not found in %d", det, refStorage.Data())) ; 
170 //    } else {
171 //                      dirFile = dirFile->GetDirectory(task) ; 
172 //      if (!dirFile) 
173 //                              AliWarning(Form("Directory %s/%s not found in %s", det, task, refStorage.Data())) ; 
174 //    }  
175 //  } else 
176   if (!refStorage.Contains(AliQAv1::GetLabLocalOCDB()) && !refStorage.Contains(AliQAv1::GetLabAliEnOCDB())) {
177     AliError(Form("%s is not a valid location for reference data", refStorage.Data())) ; 
178     return ; 
179   } else {
180     AliQAManager* manQA = AliQAManager::QAManager(AliQAv1::GetTaskIndex(task)) ;
181     for (Int_t specie = 0 ; specie < AliRecoParam::kNSpecies ; specie++) {
182       if ( !AliQAv1::Instance()->IsEventSpecieSet(specie) ) 
183         continue ; 
184       //if ( strcmp(AliQAv1::GetRefDataDirName(), "") == 0 ) { // the name of the last level of the directory is not set (EventSpecie)
185         // Get it from RunInfo
186         //if (!fRunInfo)  // not yet set, get the info from GRP
187         //  LoadRunInfoFromGRP() ; 
188       AliQAv1::SetQARefDataDirName(specie) ;
189       //}
190       if ( ! manQA->GetLock() ) { 
191         manQA->SetDefaultStorage(AliQAv1::GetQARefStorage()) ; 
192         manQA->SetSpecificStorage("*", AliQAv1::GetQARefStorage()) ;
193         manQA->SetRun(AliCDBManager::Instance()->GetRun()) ; 
194         manQA->SetLock() ; 
195       }
196       char * detOCDBDir = Form("%s/%s/%s", det, AliQAv1::GetRefOCDBDirName(), AliQAv1::GetRefDataDirName()) ; 
197       AliCDBEntry * entry = manQA->Get(detOCDBDir, manQA->GetRun()) ;
198       if (entry) {
199         dirOCDB = new TObjArray*[AliRecoParam::kNSpecies] ;     
200         TList * listDetQAD =static_cast<TList *>(entry->GetObject()) ;
201         if ( strcmp(listDetQAD->ClassName(), "TList") != 0 ) {
202           AliError(Form("Expected a Tlist and found a %s for detector %s", listDetQAD->ClassName(), det)) ; 
203           continue ; 
204         }       
205         TIter next(listDetQAD) ;
206         TObjArray * ar ; 
207         while ( (ar = (TObjArray*)next()) )
208           if ( listDetQAD ) 
209           dirOCDB[specie] = static_cast<TObjArray *>(listDetQAD->FindObject(Form("%s/%s", task, AliRecoParam::GetEventSpecieName(specie)))) ; 
210       }
211     }
212   }
213 }
214
215 //_____________________________________________________________________________
216 AliQAChecker * AliQAChecker::Instance()
217 {
218         // returns unique instance of the checker
219   if ( ! fgQAChecker ) 
220    fgQAChecker = new AliQAChecker() ; 
221   return fgQAChecker ;  
222 }
223
224 //_____________________________________________________________________________
225 void AliQAChecker::LoadRunInfoFromGRP() 
226 {
227   AliCDBManager* man = AliCDBManager::Instance() ;
228   AliCDBEntry* entry = man->Get(AliQAv1::GetGRPPath().Data());
229   AliGRPObject* grpObject = 0x0;
230   if (entry) {
231
232           TMap* m = static_cast<TMap*>(entry->GetObject());  // old GRP entry
233
234           if (m) {
235             AliDebug(AliQAv1::GetQADebugLevel(), "It is a map");
236             //m->Print();
237             grpObject = new AliGRPObject();
238                  grpObject->ReadValuesFromMap(m);
239     }
240
241     else {
242             AliDebug(AliQAv1::GetQADebugLevel(), "It is a new GRP object");
243         grpObject = static_cast<AliGRPObject*>(entry->GetObject());  // new GRP entry
244     }
245
246     entry->SetOwner(0);
247     AliCDBManager::Instance()->UnloadFromCache("GRP/GRP/Data");
248   }
249
250   if (!grpObject) {
251      AliFatal("No GRP entry found in OCDB!");
252   }
253
254   TString lhcState = grpObject->GetLHCState();
255   if (lhcState==AliGRPObject::GetInvalidString()) {
256     AliError("GRP/GRP/Data entry:  missing value for the LHC state ! Using UNKNOWN");
257     lhcState = "UNKNOWN";
258   }
259
260   TString beamType = grpObject->GetBeamType();
261   if (beamType==AliGRPObject::GetInvalidString()) {
262     AliError("GRP/GRP/Data entry:  missing value for the beam type ! Using UNKNOWN");
263     beamType = "UNKNOWN";
264   }
265
266   Float_t beamEnergy = grpObject->GetBeamEnergy();
267   if (beamEnergy==AliGRPObject::GetInvalidFloat()) {
268     AliError("GRP/GRP/Data entry:  missing value for the beam energy ! Using 0");
269     beamEnergy = 0;
270   }
271
272   TString runType = grpObject->GetRunType();
273   if (runType==AliGRPObject::GetInvalidString()) {
274     AliError("GRP/GRP/Data entry:  missing value for the run type ! Using UNKNOWN");
275     runType = "UNKNOWN";
276   }
277
278   Int_t activeDetectors = grpObject->GetDetectorMask();
279   if (activeDetectors==AliGRPObject::GetInvalidInt()) {
280     AliError("GRP/GRP/Data entry:  missing value for the detector mask ! Using 1074790399");
281     activeDetectors = 1074790399;
282   }
283
284   fRunInfo = new AliRunInfo(lhcState, beamType, beamEnergy, runType, activeDetectors);
285
286   fRunInfoOwner = kTRUE ; 
287
288   // set the event specie
289   fEventSpecie = AliRecoParam::kDefault ;
290   if (strcmp(runType,"PHYSICS")) {
291     // Not a physics run, the event specie is set to kCalib
292     fEventSpecie = AliRecoParam::kCalib ;
293     return;
294   }
295   if (strcmp(lhcState,"STABLE_BEAMS") == 0) {
296     // Heavy ion run (any beam tha is not pp, the event specie is set to kHighMult
297     fEventSpecie = AliRecoParam::kHighMult ;
298     if ((strcmp(beamType,"p-p") == 0) ||
299         (strcmp(beamType,"p-")  == 0) ||
300         (strcmp(beamType,"-p")  == 0) ||
301         (strcmp(beamType,"P-P") == 0) ||
302         (strcmp(beamType,"P-")  == 0) ||
303         (strcmp(beamType,"-P")  == 0)) {
304       // Proton run, the event specie is set to kLowMult
305       fEventSpecie = AliRecoParam::kLowMult ;
306     }
307     else if (strcmp(beamType,"-") == 0) {
308       // No beams, we assume cosmic data
309       fEventSpecie = AliRecoParam::kCosmic ;
310     }
311     else if (strcmp(beamType,"UNKNOWN") == 0) {
312       // No LHC beam information is available, we use the default event specie
313       fEventSpecie = AliRecoParam::kDefault ;
314     }
315   }
316 }
317
318 //_____________________________________________________________________________
319 Bool_t AliQAChecker::Run(const char * fileName)
320 {
321   // run the Quality Assurance Checker for all tasks Hits, SDigits, Digits, DigitsR, RecPoints, TrackSegments, RecParticles and ESDs
322   // starting from data in file  
323   TStopwatch stopwatch;
324   stopwatch.Start();
325
326   //search for all detectors QA directories
327   TList * detKeyList = AliQAv1::GetQADataFile(fileName)->GetListOfKeys() ; 
328   TIter nextd(detKeyList) ; 
329   TKey * detKey ; 
330   while ( (detKey = static_cast<TKey *>(nextd()) ) ) {
331     AliDebug(AliQAv1::GetQADebugLevel(), Form("Found %s", detKey->GetName())) ;
332     //Check which detector
333     TString detName ; 
334     TString detNameQA(detKey->GetName()) ; 
335     Int_t det ; 
336     for ( det = 0; det < AliQAv1::kNDET ; det++) {
337       detName = AliQAv1::GetDetName(det) ; 
338       if (detNameQA.Contains(detName)) {
339         fFoundDetectors+=detName ; 
340         fFoundDetectors+="." ;          
341         break ; 
342       }
343     } 
344     TDirectory * detDir = AliQAv1::GetQADataFile(fileName)->GetDirectory(detKey->GetName()) ; 
345     TList * taskKeyList = detDir->GetListOfKeys() ;
346     TIter nextt(taskKeyList) ; 
347     TKey * taskKey ; 
348     // now search for the tasks dir
349     while ( (taskKey = static_cast<TKey *>(nextt()) ) ) {
350       TString taskName( taskKey->GetName() ) ; 
351       AliDebug(AliQAv1::GetQADebugLevel(), Form("Found %s", taskName.Data())) ;
352       TDirectory * taskDir = detDir->GetDirectory(taskName.Data()) ; 
353       taskDir->cd() ; 
354       AliQACheckerBase * qac = GetDetQAChecker(det) ; 
355       if (qac)
356         AliDebug(AliQAv1::GetQADebugLevel(), Form("QA checker found for %s", detName.Data())) ; 
357       if (!qac)
358         AliFatal(Form("QA checker not found for %s", detName.Data())) ; 
359       AliQAv1::ALITASK_t index = AliQAv1::kNULLTASK ; 
360       if ( taskName == AliQAv1::GetTaskName(AliQAv1::kHITS) ) 
361         index = AliQAv1::kSIM ; 
362       if ( taskName == AliQAv1::GetTaskName(AliQAv1::kSDIGITS) ) 
363         index = AliQAv1::kSIM ; 
364       if ( taskName == AliQAv1::GetTaskName(AliQAv1::kDIGITS) ) 
365         index = AliQAv1::kSIM ; 
366       if ( taskName == AliQAv1::GetTaskName(AliQAv1::kDIGITSR) ) 
367         index = AliQAv1::kREC ; 
368       if ( taskName == AliQAv1::GetTaskName(AliQAv1::kRECPOINTS) ) 
369         index = AliQAv1::kREC ; 
370       if ( taskName == AliQAv1::GetTaskName(AliQAv1::kTRACKSEGMENTS) ) 
371         index = AliQAv1::kREC ; 
372       if ( taskName == AliQAv1::GetTaskName(AliQAv1::kRECPARTICLES) ) 
373         index = AliQAv1::kREC ; 
374       if ( taskName == AliQAv1::GetTaskName(AliQAv1::kESDS) ) 
375         index = AliQAv1::kESD ; 
376       qac->Init(AliQAv1::DETECTORINDEX_t(det)) ; 
377       
378       TDirectory * refDir     = NULL ; 
379       TObjArray ** refOCDBDir = NULL ;  
380       GetRefSubDir(detNameQA.Data(), taskName.Data(), refDir, refOCDBDir) ;
381                   qac->SetRefandData(refDir, refOCDBDir, taskDir) ;
382                   qac->Run(index) ; 
383     }
384   }
385   AliInfo("QA performed for following detectors:") ; 
386   for ( Int_t det = 0; det < AliQAv1::kNDET; det++) {
387     if (fFoundDetectors.Contains(AliQAv1::GetDetName(det))) {
388       AliInfoClass(Form("%s, ",AliQAv1::GetDetName(det))) ; 
389       fFoundDetectors.ReplaceAll(AliQAv1::GetDetName(det), "") ; 
390     }   
391   }
392   printf("\n") ; 
393   return kTRUE ; 
394 }
395
396 //_____________________________________________________________________________
397 Bool_t AliQAChecker::Run(AliQAv1::DETECTORINDEX_t det, AliQAv1::TASKINDEX_t task, TObjArray ** list)
398 {
399         // run the Quality Assurance Checker for detector det, for task task starting from data in list
400
401         AliQACheckerBase * qac = GetDetQAChecker(det) ; 
402         if (qac)
403                 AliDebug(AliQAv1::GetQADebugLevel(), Form("QA checker found for %s", AliQAv1::GetDetName(det).Data())) ;
404         if (!qac)
405                 AliError(Form("QA checker not found for %s", AliQAv1::GetDetName(det).Data())) ; 
406   
407         AliQAv1::ALITASK_t index = AliQAv1::kNULLTASK ; 
408         if ( task == AliQAv1::kRAWS ) 
409                 index = AliQAv1::kRAW ; 
410         else if ( task == AliQAv1::kHITS ) 
411                 index = AliQAv1::kSIM ; 
412         else if ( task == AliQAv1::kSDIGITS ) 
413                 index = AliQAv1::kSIM ; 
414         else if ( task == AliQAv1::kDIGITS ) 
415                 index = AliQAv1::kSIM ; 
416         else if ( task == AliQAv1::kDIGITSR ) 
417                 index = AliQAv1::kREC ; 
418         else if ( task == AliQAv1::kRECPOINTS ) 
419                 index = AliQAv1::kREC ; 
420         else if ( task == AliQAv1::kTRACKSEGMENTS ) 
421                 index = AliQAv1::kREC ; 
422         else if ( task == AliQAv1::kRECPARTICLES ) 
423                 index = AliQAv1::kREC ; 
424         else if ( task == AliQAv1::kESDS ) 
425                 index = AliQAv1::kESD ; 
426
427         TDirectory * refDir     = NULL ; 
428         TObjArray ** refOCDBDir = NULL  ;       
429   qac->Init(det) ; 
430   GetRefSubDir(AliQAv1::GetDetName(det), AliQAv1::GetTaskName(task), refDir, refOCDBDir) ;
431   qac->SetRefandData(refDir, refOCDBDir) ; 
432   qac->Run(index, list) ; 
433   
434   // make the image 
435   qac->MakeImage(list, task, AliQAv1::Mode(task)) ; 
436   
437         return kTRUE ; 
438 }
439
440 //_____________________________________________________________________________
441 Bool_t AliQAChecker::Run(AliQAv1::DETECTORINDEX_t det, AliQAv1::TASKINDEX_t task, TNtupleD ** list)
442 {
443         // run the Quality Assurance Checker for detector det, for task task starting from data in list
444   
445         AliQACheckerBase * qac = GetDetQAChecker(det) ; 
446         if (qac)
447                 AliDebug(AliQAv1::GetQADebugLevel(), Form("QA checker found for %s", AliQAv1::GetDetName(det).Data())) ;
448         if (!qac)
449                 AliError(Form("QA checker not found for %s", AliQAv1::GetDetName(det).Data())) ; 
450   
451         AliQAv1::ALITASK_t index = AliQAv1::kNULLTASK ; 
452         if ( task == AliQAv1::kRAWS ) 
453                 index = AliQAv1::kRAW ; 
454         else if ( task == AliQAv1::kHITS ) 
455                 index = AliQAv1::kSIM ; 
456         else if ( task == AliQAv1::kSDIGITS ) 
457                 index = AliQAv1::kSIM ; 
458         else if ( task == AliQAv1::kDIGITS ) 
459                 index = AliQAv1::kSIM ; 
460         else if ( task == AliQAv1::kDIGITSR ) 
461                 index = AliQAv1::kREC ; 
462         else if ( task == AliQAv1::kRECPOINTS ) 
463                 index = AliQAv1::kREC ; 
464         else if ( task == AliQAv1::kTRACKSEGMENTS ) 
465                 index = AliQAv1::kREC ; 
466         else if ( task == AliQAv1::kRECPARTICLES ) 
467                 index = AliQAv1::kREC ; 
468         else if ( task == AliQAv1::kESDS ) 
469                 index = AliQAv1::kESD ; 
470   
471         TDirectory * refDir     = NULL ; 
472         TObjArray ** refOCDBDir = NULL ;        
473   qac->Init(det) ; 
474   GetRefSubDir(AliQAv1::GetDetName(det), AliQAv1::GetTaskName(task), refDir, refOCDBDir) ;
475   qac->SetRefandData(refDir, refOCDBDir) ; 
476   qac->Run(index, list) ; 
477
478   return kTRUE ; 
479 }