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