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