Lowered the condition for raising the fatal bit (for test purposes)
[u/mrichter/AliRoot.git] / STEER / AliQADataMakerSteer.cxx
CommitLineData
312e6f8d 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#include <TFile.h>
567d83d9 19#include <TFileMerger.h>
312e6f8d 20#include <TPluginManager.h>
21#include <TROOT.h>
22#include <TString.h>
23#include <TSystem.h>
24
25#include "AliESDEvent.h"
6441b07a 26#include "AliHeader.h"
312e6f8d 27#include "AliLog.h"
007fcebb 28#include "AliModule.h"
312e6f8d 29#include "AliQA.h"
30#include "AliQADataMaker.h"
31#include "AliQADataMakerSteer.h"
4ecde5fc 32#include "AliRawReaderDate.h"
33#include "AliRawReaderFile.h"
312e6f8d 34#include "AliRawReaderRoot.h"
35#include "AliRun.h"
36#include "AliRunLoader.h"
37
38ClassImp(AliQADataMakerSteer)
39
40//_____________________________________________________________________________
41AliQADataMakerSteer::AliQADataMakerSteer(const char* gAliceFilename, const char * name, const char * title) :
42 TNamed(name, title),
c65c502a 43 fCycleSame(kFALSE),
312e6f8d 44 fESD(NULL),
45 fESDTree(NULL),
46 fFirst(kTRUE),
47 fGAliceFileName(gAliceFilename),
48 fRunNumber(0),
49 fNumberOfEvents(0),
50 fRawReader(NULL),
6441b07a 51 fRawReaderDelete(kTRUE),
312e6f8d 52 fRunLoader(NULL)
53{
54 // default ctor
55 for (UInt_t iDet = 0; iDet < fgkNDetectors; iDet++) {
56 fLoader[iDet] = NULL ;
57 fQADataMaker[iDet] = NULL ;
58 fQACycles[iDet] = 999999 ;
59 }
60}
61
62//_____________________________________________________________________________
63AliQADataMakerSteer::AliQADataMakerSteer(const AliQADataMakerSteer & qas) :
64 TNamed(qas),
c65c502a 65 fCycleSame(kFALSE),
312e6f8d 66 fESD(NULL),
67 fESDTree(NULL),
68 fFirst(qas.fFirst),
69 fGAliceFileName(qas.fGAliceFileName),
70 fRunNumber(qas.fRunNumber),
71 fNumberOfEvents(qas.fNumberOfEvents),
72 fRawReader(NULL),
6441b07a 73 fRawReaderDelete(kTRUE),
312e6f8d 74 fRunLoader(NULL)
75{
76 // cpy ctor
77 for (UInt_t iDet = 0; iDet < fgkNDetectors; iDet++) {
78 fLoader[iDet] = qas.fLoader[iDet] ;
79 fQADataMaker[iDet] = qas.fQADataMaker[iDet] ;
80 fQACycles[iDet] = qas.fQACycles[iDet] ;
81 }
82}
83
84//_____________________________________________________________________________
85AliQADataMakerSteer & AliQADataMakerSteer::operator = (const AliQADataMakerSteer & qas)
86{
87 // assignment operator
88 this->~AliQADataMakerSteer() ;
89 new(this) AliQADataMakerSteer(qas) ;
90 return *this ;
91}
92
93//_____________________________________________________________________________
94AliQADataMakerSteer::~AliQADataMakerSteer()
95{
96 // dtor
97 for (UInt_t iDet = 0; iDet < fgkNDetectors; iDet++) {
98 fLoader[iDet] = NULL;
c65c502a 99 if (fQADataMaker[iDet]) {
100 (fQADataMaker[iDet])->Finish() ;
101 delete fQADataMaker[iDet] ;
102 fQADataMaker[iDet] = NULL ;
103 }
312e6f8d 104 }
105
6441b07a 106 if (fRawReaderDelete) {
107 fRunLoader = NULL ;
108 delete fRawReader ;
109 fRawReader = NULL ;
110 }
312e6f8d 111}
112
113//_____________________________________________________________________________
114AliLoader * AliQADataMakerSteer::GetLoader(Int_t iDet)
115{
116 // get the loader for a detector
117
118 TString detName = AliQA::GetDetName(iDet) ;
119 fLoader[iDet] = fRunLoader->GetLoader(detName + "Loader");
120 if (fLoader[iDet])
121 return fLoader[iDet] ;
122
123 // load the QA data maker object
124 TPluginManager* pluginManager = gROOT->GetPluginManager() ;
125 TString loaderName = "Ali" + detName + "Loader" ;
126
127 AliLoader * loader = NULL ;
128 // first check if a plugin is defined for the quality assurance data maker
129 TPluginHandler* pluginHandler = pluginManager->FindHandler("AliLoader", detName) ;
130 // if not, add a plugin for it
131 if (!pluginHandler) {
132 AliDebug(1, Form("defining plugin for %s", loaderName.Data())) ;
133 TString libs = gSystem->GetLibraries() ;
134 if (libs.Contains("lib" + detName + "base.so") || (gSystem->Load("lib" + detName + "base.so") >= 0)) {
135 pluginManager->AddHandler("AliQADataMaker", detName, loaderName, detName + "loader", loaderName + "()") ;
136 } else {
137 pluginManager->AddHandler("AliLoader", detName, loaderName, detName, loaderName + "()") ;
138 }
139 pluginHandler = pluginManager->FindHandler("AliLoader", detName) ;
140 }
141 if (pluginHandler && (pluginHandler->LoadPlugin() == 0)) {
142 loader = (AliLoader *) pluginHandler->ExecPlugin(0) ;
143 }
144 if (loader)
145 fLoader[iDet] = loader ;
146 return loader ;
147}
148
149//_____________________________________________________________________________
150AliQADataMaker * AliQADataMakerSteer::GetQADataMaker(Int_t iDet)
151{
152 // get the quality assurance data maker for a detector
153
154 if (fQADataMaker[iDet])
155 return fQADataMaker[iDet] ;
156
157 AliQADataMaker * qadm = NULL ;
158
159 if (fFirst) {
160 // load the QA data maker object
161 TPluginManager* pluginManager = gROOT->GetPluginManager() ;
162 TString detName = AliQA::GetDetName(iDet) ;
163 TString qadmName = "Ali" + detName + "QADataMaker" ;
164
165 // first check if a plugin is defined for the quality assurance data maker
166 TPluginHandler* pluginHandler = pluginManager->FindHandler("AliQADataMaker", detName) ;
167 // if not, add a plugin for it
168 if (!pluginHandler) {
169 AliDebug(1, Form("defining plugin for %s", qadmName.Data())) ;
170 TString libs = gSystem->GetLibraries() ;
171 if (libs.Contains("lib" + detName + "base.so") || (gSystem->Load("lib" + detName + "base.so") >= 0)) {
172 pluginManager->AddHandler("AliQADataMaker", detName, qadmName, detName + "qadm", qadmName + "()") ;
173 } else {
174 pluginManager->AddHandler("AliQADataMaker", detName, qadmName, detName, qadmName + "()") ;
175 }
176 pluginHandler = pluginManager->FindHandler("AliQADataMaker", detName) ;
177 }
178 if (pluginHandler && (pluginHandler->LoadPlugin() == 0)) {
179 qadm = (AliQADataMaker *) pluginHandler->ExecPlugin(0) ;
180 }
181 if (qadm)
182 fQADataMaker[iDet] = qadm ;
183 }
184 return qadm ;
185}
186
187//_____________________________________________________________________________
4ecde5fc 188Bool_t AliQADataMakerSteer::Init(const AliQA::TASKINDEX taskIndex, const char * input )
312e6f8d 189{
190 // Initialize the event source and QA data makers
191
6441b07a 192 if (taskIndex == AliQA::kRAWS) {
193 if (!fRawReader) {
194 TString fileName(input);
195 if (fileName.EndsWith("/")) {
196 fRawReader = new AliRawReaderFile(fileName);
197 } else if (fileName.EndsWith(".root")) {
198 fRawReader = new AliRawReaderRoot(fileName);
199 } else if (!fileName.IsNull()) {
200 fRawReader = new AliRawReaderDate(fileName);
201 fRawReader->SelectEvents(7);
202 }
4ecde5fc 203 }
312e6f8d 204 if ( ! fRawReader )
205 return kFALSE ;
206 fRawReader->NextEvent() ;
207 fRunNumber = fRawReader->GetRunNumber() ;
208 fRawReader->RewindEvents();
209 fNumberOfEvents = 999999 ;
ad265f3e 210 } else if (taskIndex == AliQA::kESDS) {
211 if (!gSystem->AccessPathName("AliESDs.root")) { // AliESDs.root exists
212 TFile * esdFile = TFile::Open("AliESDs.root") ;
213 fESDTree = dynamic_cast<TTree *> (esdFile->Get("esdTree")) ;
214 fESD = new AliESDEvent() ;
215 fESD->ReadFromTree(fESDTree) ;
216 fESDTree->GetEntry(0) ;
217 fRunNumber = fESD->GetRunNumber() ;
218 fNumberOfEvents = fESDTree->GetEntries() ;
219 } else {
220 AliError("AliESDs.root not found") ;
221 return kFALSE ;
222 }
312e6f8d 223 } else {
224 if ( !InitRunLoader() ) {
808b7099 225 AliError("Run Loader not found") ;
312e6f8d 226 } else {
6441b07a 227 if (fRunLoader->GetHeader())
228 fRunNumber = fRunLoader->GetHeader()->GetRun() ;
229 else
230 fRunNumber = 0 ;
312e6f8d 231 fNumberOfEvents = fRunLoader->GetNumberOfEvents() ;
232 }
ad265f3e 233 }
312e6f8d 234 // Initialize all QA data makers for all detectors
235 for (UInt_t iDet = 0; iDet < fgkNDetectors ; iDet++) {
236 AliQADataMaker * qadm = GetQADataMaker(iDet) ;
237 if (!qadm) {
238 AliWarning(Form("AliQADataMaker not found for %s", AliQA::GetDetName(iDet))) ;
239 } else {
240 AliInfo(Form("Data Maker found for %s", qadm->GetName())) ;
241 qadm->Init(taskIndex, fRunNumber, GetQACycles(iDet)) ;
c65c502a 242 qadm->StartOfCycle(taskIndex, fCycleSame) ;
312e6f8d 243 }
244 }
245 fFirst = kFALSE ;
246 return kTRUE ;
247}
248
249//_____________________________________________________________________________
250Bool_t AliQADataMakerSteer::InitRunLoader()
251{
252 // get or create the run loader
253 if (fRunLoader) {
c65c502a 254 fCycleSame = kTRUE ;
312e6f8d 255 return kTRUE ;
256 }
257
258 if (!gSystem->AccessPathName(fGAliceFileName.Data())) { // galice.root exists
259 // load all base libraries to get the loader classes
260 TString libs = gSystem->GetLibraries() ;
261 for (UInt_t iDet = 0; iDet < fgkNDetectors; iDet++) {
262 TString detName = AliQA::GetDetName(iDet) ;
263 if (detName == "HLT")
264 continue;
265 if (libs.Contains("lib" + detName + "base.so"))
266 continue;
267 gSystem->Load("lib" + detName + "base.so");
268 }
269 fRunLoader = AliRunLoader::Open(fGAliceFileName.Data());
270 if (!fRunLoader) {
271 AliError(Form("no run loader found in file %s", fGAliceFileName.Data()));
272 return kFALSE;
273 }
274 fRunLoader->CdGAFile();
275 if (fRunLoader->LoadgAlice() == 0) {
276 gAlice = fRunLoader->GetAliRun();
277 }
278 if (!gAlice) {
279 AliError(Form("no gAlice object found in file %s", fGAliceFileName.Data()));
280 return kFALSE;
281 }
282
283 } else { // galice.root does not exist
284 AliError(Form("the file %s does not exist", fGAliceFileName.Data()));
285 return kFALSE;
286 }
287
288 return kTRUE;
289}
290
291//_____________________________________________________________________________
292Bool_t AliQADataMakerSteer::Finish(const AliQA::TASKINDEX taskIndex)
293{
294 // write output to file for all detectors
295 for (UInt_t iDet = 0; iDet < fgkNDetectors ; iDet++) {
296 AliQADataMaker * qadm = GetQADataMaker(iDet) ;
297 if (qadm) {
298 qadm->EndOfCycle(taskIndex) ;
312e6f8d 299 }
300 }
301 return kTRUE ;
302}
303
315bba70 304//_____________________________________________________________________________
305Bool_t AliQADataMakerSteer::Merge()
306{
567d83d9 307 // Merge all the cycles from all detectors in one single file per run
308
309 gROOT->ProcessLine(".! ls *QA*.*.*.root > tempo.txt") ;
310 ifstream in("tempo.txt") ;
311 const Int_t runMax = 10 ;
312 TString file[AliQA::kNDET*runMax] ;
313 Int_t run[AliQA::kNDET*runMax] ;
314
315 Int_t index = 0 ;
316 while ( 1 ) {
317 in >> file[index++] ;
318 if ( !in.good() )
319 break ;
320 }
321 Int_t previousRun = -1 ;
322 Int_t runIndex = 0 ;
323 for (Int_t ifile = 0 ; ifile < index-1 ; ifile++) {
324 TString tmp(file[ifile]) ;
325 tmp.ReplaceAll(".root", "") ;
326 TString det = tmp(0, tmp.Index(".")) ;
327 tmp.Remove(0, tmp.Index(".QA.")+4) ;
328 TString ttmp = tmp(0, tmp.Index(".")) ;
329 Int_t newRun = ttmp.Atoi() ;
330 if (newRun != previousRun) {
331 run[runIndex] = newRun ;
332 previousRun = newRun ;
333 runIndex++ ;
334 }
335 ttmp = tmp(tmp.Index("."), tmp.Length()) ;
336 Int_t cycle = ttmp.Atoi() ;
337 AliInfo(Form("%s : det = %s run = %d cycle = %d \n", file[ifile].Data(), det.Data(), newRun, cycle)) ;
338 }
339 for (Int_t irun = 0 ; irun < runIndex ; irun++) {
340 TFileMerger merger ;
341 char outFileName[runMax] ;
342 sprintf(outFileName, "Merged.QA.%d.root", runIndex-1) ;
343 merger.OutputFile(outFileName) ;
344 for (Int_t ifile = 0 ; ifile < index-1 ; ifile++) {
345 char pattern[100] ;
346 sprintf(pattern, "QA.%d.", runIndex-1) ;
347 TString tmp(file[ifile]) ;
348 if (tmp.Contains(pattern))
349 merger.AddFile(tmp) ;
350 }
351 merger.Merge() ;
352 }
353
354 return kTRUE ;
315bba70 355}
356
c65c502a 357//_____________________________________________________________________________
358void AliQADataMakerSteer::Reset()
359{
360 // Reset the default data members
361 for (UInt_t iDet = 0; iDet < fgkNDetectors; iDet++) {
362 fLoader[iDet] = NULL;
363 if (fQADataMaker[iDet]) {
364 (fQADataMaker[iDet])->Reset() ;
365// delete fQADataMaker[iDet] ;
366// fQADataMaker[iDet] = NULL ;
367 }
368 }
369
6441b07a 370 if (fRawReaderDelete) {
371 delete fRawReader ;
372 fRawReader = NULL ;
373 }
c65c502a 374
375 fCycleSame = kFALSE ;
376 fESD = NULL ;
377 fESDTree = NULL ;
378 fFirst = kTRUE ;
379 fNumberOfEvents = 0 ;
380}
381
6441b07a 382//_____________________________________________________________________________
383Bool_t AliQADataMakerSteer::Run(AliRawReader * rawReader)
384{
385 //Runs all the QA data Maker for Raws only
386 fRawReader = rawReader ;
387 fRawReaderDelete = kFALSE ;
388 fCycleSame = kTRUE ;
389
390 // Initialize all QA data makers for all detectors
391 for (UInt_t iDet = 0; iDet < fgkNDetectors ; iDet++) {
392 AliQADataMaker * qadm = GetQADataMaker(iDet) ;
393 if (!qadm) {
394 AliWarning(Form("AliQADataMaker not found for %s", AliQA::GetDetName(iDet))) ;
395 } else {
396 AliInfo(Form("Data Maker found for %s", qadm->GetName())) ;
397 qadm->Init(AliQA::kRAWS, fRunNumber, GetQACycles(iDet)) ;
398 qadm->StartOfCycle(AliQA::kRAWS, fCycleSame) ;
399 }
400 }
401 fFirst = kFALSE ;
402
403 return DoIt(AliQA::kRAWS) ;
404}
405
312e6f8d 406//_____________________________________________________________________________
407Bool_t AliQADataMakerSteer::Run(const AliQA::TASKINDEX taskIndex, const char * fileName )
408{
409 // Runs all the QA data Maker for every detector
6441b07a 410
312e6f8d 411 Bool_t rv = kFALSE ;
412
413 if ( !Init(taskIndex, fileName) )
414 return kFALSE ;
6441b07a 415
416 return DoIt(taskIndex, fileName) ;
417
418}
419
420//_____________________________________________________________________________
421Bool_t AliQADataMakerSteer::DoIt(const AliQA::TASKINDEX taskIndex, const char * fileName )
422{
423 // Runs all the QA data Maker for every detector
424 Bool_t rv = kFALSE ;
425
312e6f8d 426 // Fill QA data in event loop
427 for (UInt_t iEvent = 0 ; iEvent < fNumberOfEvents ; iEvent++) {
428 // Get the event
808b7099 429 AliDebug(1, Form("processing event %d", iEvent));
312e6f8d 430 if ( taskIndex == AliQA::kRAWS ) {
431 if ( !fRawReader->NextEvent() )
432 break ;
433 } else if ( taskIndex == AliQA::kESDS ) {
434 fESDTree->GetEntry(iEvent) ;
435 } else {
436 fRunLoader->GetEvent(iEvent);
437 }
438 // loop over detectors
efc25b83 439 TObjArray* detArray = NULL ;
440 if (fRunLoader) // check if RunLoader exists
441 if ( fRunLoader->GetAliRun() ) // check if AliRun exists in gAlice.root
442 detArray = fRunLoader->GetAliRun()->Detectors() ;
312e6f8d 443 for (UInt_t iDet = 0 ; iDet < fgkNDetectors ; iDet++) {
efc25b83 444 if (detArray) {
445 AliModule* det = static_cast<AliModule*>(detArray->FindObject(AliQA::GetDetName(iDet))) ;
446 if (!det || !det->IsActive())
447 continue;
448 }
312e6f8d 449 AliQADataMaker * qadm = GetQADataMaker(iDet) ;
450 if (!qadm) {
451 rv = kFALSE ;
452 } else {
453 if ( qadm->IsCycleDone() ) {
454 qadm->EndOfCycle(AliQA::kRAWS) ;
455 qadm->StartOfCycle(AliQA::kRAWS) ;
456 }
457 TTree * data ;
458 switch (taskIndex) {
459 case AliQA::kRAWS :
460 qadm->Exec(taskIndex, fRawReader) ;
461 break ;
462 case AliQA::kHITS :
463 GetLoader(iDet)->LoadHits() ;
464 data = GetLoader(iDet)->TreeH() ;
465 if ( ! data ) {
466 AliWarning(Form(" Hit Tree not found for %s", AliQA::GetDetName(iDet))) ;
467 } else {
468 qadm->Exec(taskIndex, data) ;
469 }
470 break ;
471 case AliQA::kSDIGITS :
472 GetLoader(iDet)->LoadSDigits() ;
473 data = GetLoader(iDet)->TreeS() ;
474 if ( ! data ) {
475 AliWarning(Form(" SDigit Tree not found for %s", AliQA::GetDetName(iDet))) ;
476 } else {
477 qadm->Exec(taskIndex, data) ;
478 }
479 break;
480 case AliQA::kDIGITS :
481 GetLoader(iDet)->LoadDigits() ;
482 data = GetLoader(iDet)->TreeD() ;
483 if ( ! data ) {
484 AliWarning(Form(" Digit Tree not found for %s", AliQA::GetDetName(iDet))) ;
485 } else {
486 qadm->Exec(taskIndex, data) ;
487 }
488 break;
489 case AliQA::kRECPOINTS :
490 GetLoader(iDet)->LoadRecPoints() ;
491 data = GetLoader(iDet)->TreeR() ;
492 if (!data) {
493 AliWarning(Form("RecPoints not found for %s", AliQA::GetDetName(iDet))) ;
494 } else {
495 qadm->Exec(taskIndex, data) ;
496 }
497 break;
498 case AliQA::kTRACKSEGMENTS :
499 break;
500 case AliQA::kRECPARTICLES :
501 break;
502 case AliQA::kESDS :
503 qadm->Exec(taskIndex, fESD) ;
504 break;
505 } //task switch
506 qadm->Increment() ;
507 } //data maker exist
508 } // detector loop
509 } // event loop
510 // Save QA data for all detectors
511 rv = Finish(taskIndex) ;
512 return rv ;
14e1eccc 513}