]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliQAChecker.cxx
formatting
[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 "AliEventInfo.h"
28 #include "AliLog.h"
29 #include "AliModule.h" 
30 #include "AliQA.h"
31 #include "AliQAChecker.h"
32 #include "AliQACheckerBase.h"
33
34 #include <TKey.h>
35 #include <TObjArray.h>
36 #include <TObjString.h>
37 #include <TPluginManager.h> 
38 #include <TROOT.h>
39 #include <TStopwatch.h> 
40 #include <TString.h> 
41 #include <TSystem.h> 
42 #include <TList.h>
43
44 ClassImp(AliQAChecker)
45   AliQAChecker * AliQAChecker::fgQAChecker = 0x0 ;
46
47 //_____________________________________________________________________________
48 AliQAChecker::AliQAChecker(const char* name, const char* title) :
49   TNamed(name, title),
50   fDataFile(0x0), 
51   fEventInfo(0x0), 
52   fEventInfoOwner(kFALSE), 
53   fRefFile(0x0), 
54   fFoundDetectors(".")
55 {
56   // ctor: initialise checkers and open the data file   
57   for (Int_t det = 0 ; det < AliQA::kNDET ; det++) 
58     fCheckers[det] = NULL ; 
59 }
60
61 //_____________________________________________________________________________
62 AliQAChecker::AliQAChecker(const AliQAChecker& qac) :
63   TNamed(qac),
64   fDataFile(qac.fDataFile), 
65   fEventInfo(qac.fEventInfo), 
66   fEventInfoOwner(kFALSE),   
67   fRefFile(qac.fRefFile), 
68   fFoundDetectors(qac.fFoundDetectors)
69 {
70   // copy constructor
71   
72   for (Int_t det = 0 ; det < AliQA::kNDET ; det++) 
73     fCheckers[det] = NULL ; 
74 }
75
76 //_____________________________________________________________________________
77 AliQAChecker& AliQAChecker::operator = (const AliQAChecker& qac)
78 {
79 // assignment operator
80
81   this->~AliQAChecker();
82   new(this) AliQAChecker(qac);
83   return *this;
84 }
85
86 //_____________________________________________________________________________
87 AliQAChecker::~AliQAChecker()
88 {
89 // clean up
90   if (fEventInfo)
91     delete fEventInfo ; 
92   delete [] fCheckers ; 
93   AliQA::Close() ; 
94 }
95
96 //_____________________________________________________________________________
97   AliQACheckerBase * AliQAChecker::GetDetQAChecker(Int_t det)
98 {
99         // Gets the Quality Assurance checker for the detector specified by its name
100         
101         if (fCheckers[det]) 
102     return fCheckers[det];
103
104         TString detName(AliQA::GetDetName(det)) ; 
105
106   AliDebug(1, Form("Retrieving QA checker for %s", detName.Data())) ; 
107   TPluginManager* pluginManager = gROOT->GetPluginManager() ;
108   TString qacName = "Ali" + detName + "QAChecker" ;
109
110   AliQACheckerBase * qac = NULL ;
111   // first check if a plugin is defined for the quality assurance checker
112   TPluginHandler* pluginHandler = pluginManager->FindHandler("AliQAChecker", detName.Data());
113   // if not, add a plugin for it
114   if (!pluginHandler) {
115     //AliInfo(Form("defining plugin for %s", qacName.Data()));
116     TString libs = gSystem->GetLibraries();
117                 
118                 if (libs.Contains("lib" + detName + "base.so") || (gSystem->Load("lib" + detName + "base.so") >= 0))
119       pluginManager->AddHandler("AliQAChecker", detName, qacName, detName + "qac", qacName + "()");
120                 else 
121       pluginManager->AddHandler("AliQAChecker", detName, qacName, detName, qacName + "()");
122
123                 pluginHandler = pluginManager->FindHandler("AliQAChecker", detName);
124
125                 if (pluginHandler && (pluginHandler->LoadPlugin() == 0)) 
126                         qac = (AliQACheckerBase *) pluginHandler->ExecPlugin(0);
127   
128                 if (qac) 
129                         fCheckers[det] = qac ;
130         }
131
132  return qac ; 
133 }
134  
135 //_____________________________________________________________________________
136 void AliQAChecker::GetRefSubDir(const char * det, const char * task, TDirectory *& dirFile, TObjArray *& dirOCDB)     
137
138   // Opens and returns the file with the reference data 
139         
140   dirFile = NULL ; 
141   dirOCDB = NULL ; 
142   TString refStorage(AliQA::GetQARefStorage()) ; 
143   //refStorage += AliQA::GetQARefFileName() ;
144   if (refStorage.Contains(AliQA::GetLabLocalFile())) {  
145     refStorage.ReplaceAll(AliQA::GetLabLocalFile(), "") ; 
146     if ( fRefFile ) 
147       if ( fRefFile->IsOpen() ) 
148                                         fRefFile->Close() ; 
149     fRefFile = TFile::Open(refStorage.Data()) ; 
150     if (!fRefFile) { 
151       AliError(Form("Cannot find reference file %s", refStorage.Data())) ; 
152       dirFile = NULL ; 
153     }
154     dirFile = fRefFile->GetDirectory(det) ; 
155     if (!dirFile) {
156       AliWarning(Form("Directory %s not found in %d", det, refStorage.Data())) ; 
157     } else {
158                         dirFile = dirFile->GetDirectory(task) ; 
159       if (!dirFile) 
160                                 AliWarning(Form("Directory %s/%s not found in %s", det, task, refStorage.Data())) ; 
161     }  
162   } else if (refStorage.Contains(AliQA::GetLabLocalOCDB()) || refStorage.Contains(AliQA::GetLabAliEnOCDB())) {  
163     AliCDBManager* man = AliCDBManager::Instance() ;
164     if ( strcmp(AliQA::GetRefDataDirName(), "") == 0 ) { // the name of the last level of the directory is not set (RUNTYPE)
165       // Get it from EventInfo
166       if (!fEventInfo)  // not yet set, get the info from GRP
167                                 LoadEventInfoFromGRP() ; 
168       AliQA::SetQARefDataDirName(fEventInfo->GetRunType()) ;
169     }
170     if ( ! man->GetLock() ) { 
171       man->SetDefaultStorage(AliQA::GetQARefStorage()) ; 
172       man->SetSpecificStorage("*", AliQA::GetQARefStorage()) ;
173     }
174     char * detOCDBDir = Form("%s/%s/%s", det, AliQA::GetRefOCDBDirName(), AliQA::GetRefDataDirName()) ; 
175     AliInfo(Form("Reference QA data are taken from %s", detOCDBDir)) ;
176     AliCDBEntry * entry = man->Get(detOCDBDir, man->GetRun()) ;
177     if (entry) {
178       TList * listDetQAD = dynamic_cast<TList *>(entry->GetObject()) ;
179       if ( listDetQAD ) 
180                                 dirOCDB = dynamic_cast<TObjArray *>(listDetQAD->FindObject(task)) ; 
181     }
182   }
183 }
184
185 //_____________________________________________________________________________
186 AliQAChecker * AliQAChecker::Instance()
187 {
188         // returns unique instance of the checker
189   if ( ! fgQAChecker ) 
190    fgQAChecker = new AliQAChecker() ; 
191   return fgQAChecker ;  
192 }
193
194 //_____________________________________________________________________________
195 void AliQAChecker::LoadEventInfoFromGRP() 
196 {
197   AliCDBManager* man = AliCDBManager::Instance() ;
198   AliCDBEntry* entry = man->Get(AliQA::GetGRPPath().Data());
199   TMap * data = 0x0 ; 
200   if (entry) 
201     data = dynamic_cast<TMap*>(entry->GetObject());  
202   if (!data) {
203     AliFatal("No GRP entry found in OCDB!");      
204   }
205   TObjString *lhcState=  
206     dynamic_cast<TObjString*>(data->GetValue("fLHCState"));
207   if (!lhcState) {
208     AliWarning(Form("%s entry:  missing value for the LHC state ! Using UNKNOWN", AliQA::GetGRPPath().Data()));
209   }
210   TObjString *beamType=
211     dynamic_cast<TObjString*>(data->GetValue("fAliceBeamType"));
212   if (!beamType) {
213     AliWarning(Form("%s entry:  missing value for the LHC state ! Using UNKNOWN", AliQA::GetGRPPath().Data()));
214   }
215   TObjString *runType=
216     dynamic_cast<TObjString*>(data->GetValue("fRunType"));
217   if (!runType) {
218     AliWarning(Form("%s entry:  missing value for the run type ! Using UNKNOWN", AliQA::GetGRPPath().Data()));  }
219   TObjString *activeDetectors=
220     dynamic_cast<TObjString*>(data->GetValue("fDetectorMask"));
221   if (!activeDetectors) {
222     AliWarning(Form("%s entry:  missing value for the detector mask ! Using ALL", AliQA::GetGRPPath().Data()));  
223   }
224   fEventInfo = new AliEventInfo(lhcState ? lhcState->GetString().Data() : "UNKNOWN",
225                                 beamType ? beamType->GetString().Data() : "UNKNOWN",
226                                 runType  ? runType->GetString().Data()  : "UNKNOWN",
227                                 activeDetectors ? activeDetectors->GetString().Data() : "ALL");
228   fEventInfoOwner = kTRUE ; 
229 }
230
231 //_____________________________________________________________________________
232 Bool_t AliQAChecker::Run(const char * fileName)
233 {
234   // run the Quality Assurance Checker for all tasks Hits, SDigits, Digits, recpoints, tracksegments, recparticles and ESDs
235   // starting from data in file
236
237   Bool_t rv = kFALSE ; 
238   
239   TStopwatch stopwatch;
240   stopwatch.Start();
241
242   //search for all detectors QA directories
243   TList * detKeyList = AliQA::GetQADataFile(fileName)->GetListOfKeys() ; 
244   TIter nextd(detKeyList) ; 
245   TKey * detKey ; 
246   while ( (detKey = dynamic_cast<TKey *>(nextd()) ) ) {
247     AliDebug(1, Form("Found %s", detKey->GetName())) ;
248     //Check which detector
249     TString detName ; 
250     TString detNameQA(detKey->GetName()) ; 
251     Int_t det ; 
252     for ( det = 0; det < AliQA::kNDET ; det++) {
253       detName = AliQA::GetDetName(det) ; 
254       if (detNameQA.Contains(detName)) {
255         fFoundDetectors+=detName ; 
256         fFoundDetectors+="." ;          
257         break ; 
258       }
259     } 
260     TDirectory * detDir = AliQA::GetQADataFile(fileName)->GetDirectory(detKey->GetName()) ; 
261     TList * taskKeyList = detDir->GetListOfKeys() ;
262     TIter nextt(taskKeyList) ; 
263     TKey * taskKey ; 
264     // now search for the tasks dir
265     while ( (taskKey = static_cast<TKey *>(nextt()) ) ) {
266       TString taskName( taskKey->GetName() ) ; 
267       AliInfo(Form("Found %s", taskName.Data())) ;
268       TDirectory * taskDir = detDir->GetDirectory(taskName.Data()) ; 
269       taskDir->cd() ; 
270       AliQACheckerBase * qac = GetDetQAChecker(det) ; 
271       if (qac)
272                 AliInfo(Form("QA checker found for %s", detName.Data())) ; 
273       if (!qac)
274                 AliFatal(Form("QA checker not found for %s", detName.Data())) ; 
275       AliQA::ALITASK_t index = AliQA::kNULLTASK ; 
276       if ( taskName == AliQA::GetTaskName(AliQA::kHITS) ) 
277                 index = AliQA::kSIM ; 
278       if ( taskName == AliQA::GetTaskName(AliQA::kSDIGITS) ) 
279                 index = AliQA::kSIM ; 
280       if ( taskName == AliQA::GetTaskName(AliQA::kDIGITS) ) 
281                 index = AliQA::kSIM ; 
282       if ( taskName == AliQA::GetTaskName(AliQA::kRECPOINTS) ) 
283                 index = AliQA::kREC ; 
284       if ( taskName == AliQA::GetTaskName(AliQA::kTRACKSEGMENTS) ) 
285                 index = AliQA::kREC ; 
286       if ( taskName == AliQA::GetTaskName(AliQA::kRECPARTICLES) ) 
287                 index = AliQA::kREC ; 
288       if ( taskName == AliQA::GetTaskName(AliQA::kESDS) ) 
289                 index = AliQA::kESD ; 
290       qac->Init(AliQA::DETECTORINDEX_t(det)) ; 
291
292           TDirectory * refDir    = NULL ; 
293           TObjArray * refOCDBDir = NULL ;       
294           GetRefSubDir(detNameQA.Data(), taskName.Data(), refDir, refOCDBDir) ;
295           if ( refDir || refOCDBDir) {
296                   qac->SetRefandData(refDir, refOCDBDir, taskDir) ;
297                   qac->Run(index) ; 
298           }
299         }
300   }
301   AliInfo("QA performed for following detectors:") ; 
302   for ( Int_t det = 0; det < AliQA::kNDET; det++) {
303     if (fFoundDetectors.Contains(AliQA::GetDetName(det))) {
304       printf("%s, ",AliQA::GetDetName(det)) ; 
305       fFoundDetectors.ReplaceAll(AliQA::GetDetName(det), "") ; 
306     }   
307   }
308   printf("\n") ; 
309   rv = kTRUE ; 
310
311   return rv ; 
312   
313 }
314
315 //_____________________________________________________________________________
316 Bool_t AliQAChecker::Run(AliQA::DETECTORINDEX_t det, AliQA::TASKINDEX_t task, TObjArray * list)
317 {
318         // run the Quality Assurance Checker for detector det, for task task starting from data in list
319
320         AliQACheckerBase * qac = GetDetQAChecker(det) ; 
321         if (qac)
322                 AliDebug(1, Form("QA checker found for %s", AliQA::GetDetName(det).Data())) ;
323         if (!qac)
324                 AliError(Form("QA checker not found for %s", AliQA::GetDetName(det).Data())) ; 
325   
326         AliQA::ALITASK_t index = AliQA::kNULLTASK ; 
327         if ( task == AliQA::kRAWS ) 
328                 index = AliQA::kRAW ; 
329         else if ( task == AliQA::kHITS ) 
330                 index = AliQA::kSIM ; 
331         else if ( task == AliQA::kSDIGITS ) 
332                 index = AliQA::kSIM ; 
333         else if ( task == AliQA::kDIGITS ) 
334                 index = AliQA::kSIM ; 
335         else if ( task == AliQA::kRECPOINTS ) 
336                 index = AliQA::kREC ; 
337         else if ( task == AliQA::kTRACKSEGMENTS ) 
338                 index = AliQA::kREC ; 
339         else if ( task == AliQA::kRECPARTICLES ) 
340                 index = AliQA::kREC ; 
341         else if ( task == AliQA::kESDS ) 
342                 index = AliQA::kESD ; 
343
344         TDirectory * refDir    = NULL ; 
345         TObjArray * refOCDBDir = NULL ; 
346         qac->Init(det) ; 
347         GetRefSubDir(AliQA::GetDetName(det), AliQA::GetTaskName(task), refDir, refOCDBDir) ;
348         if ( refDir || refOCDBDir)  // references found
349           qac->SetRefandData(refDir, refOCDBDir) ; 
350         qac->Run(index, list) ; 
351         return kTRUE ; 
352 }
353
354
355