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