1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
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 **************************************************************************/
20 #include <TFileMerger.h>
21 #include <TPluginManager.h>
26 #include "AliCDBManager.h"
27 #include "AliCDBEntry.h"
29 #include "AliCDBMetaData.h"
30 #include "AliESDEvent.h"
31 #include "AliHeader.h"
33 #include "AliModule.h"
35 #include "AliQADataMaker.h"
36 #include "AliQADataMakerSteer.h"
37 #include "AliRawReaderDate.h"
38 #include "AliRawReaderFile.h"
39 #include "AliRawReaderRoot.h"
41 #include "AliRunLoader.h"
43 ClassImp(AliQADataMakerSteer)
45 //_____________________________________________________________________________
46 AliQADataMakerSteer::AliQADataMakerSteer(const char* gAliceFilename, const char * name, const char * title) :
53 fGAliceFileName(gAliceFilename),
55 fNumberOfEvents(999999),
57 fRawReaderDelete(kTRUE),
61 for (UInt_t iDet = 0; iDet < fgkNDetectors; iDet++) {
62 if (IsSelected(AliQA::GetDetName(iDet))) {
63 fLoader[iDet] = NULL ;
64 fQADataMaker[iDet] = NULL ;
65 fQACycles[iDet] = 999999 ;
70 //_____________________________________________________________________________
71 AliQADataMakerSteer::AliQADataMakerSteer(const AliQADataMakerSteer & qas) :
74 fDetectors(qas.fDetectors),
78 fGAliceFileName(qas.fGAliceFileName),
79 fRunNumber(qas.fRunNumber),
80 fNumberOfEvents(qas.fNumberOfEvents),
82 fRawReaderDelete(kTRUE),
86 for (UInt_t iDet = 0; iDet < fgkNDetectors; iDet++) {
87 fLoader[iDet] = qas.fLoader[iDet] ;
88 fQADataMaker[iDet] = qas.fQADataMaker[iDet] ;
89 fQACycles[iDet] = qas.fQACycles[iDet] ;
93 //_____________________________________________________________________________
94 AliQADataMakerSteer & AliQADataMakerSteer::operator = (const AliQADataMakerSteer & qas)
96 // assignment operator
97 this->~AliQADataMakerSteer() ;
98 new(this) AliQADataMakerSteer(qas) ;
102 //_____________________________________________________________________________
103 AliQADataMakerSteer::~AliQADataMakerSteer()
106 for (UInt_t iDet = 0; iDet < fgkNDetectors; iDet++) {
107 if (IsSelected(AliQA::GetDetName(iDet))) {
108 fLoader[iDet] = NULL;
109 if (fQADataMaker[iDet]) {
110 (fQADataMaker[iDet])->Finish() ;
111 delete fQADataMaker[iDet] ;
112 fQADataMaker[iDet] = NULL ;
117 if (fRawReaderDelete) {
124 //_____________________________________________________________________________
125 Bool_t AliQADataMakerSteer::DoIt(const AliQA::TASKINDEX taskIndex)
127 // Runs all the QA data Maker for every detector
130 // Fill QA data in event loop
131 for (UInt_t iEvent = 0 ; iEvent < fNumberOfEvents ; iEvent++) {
133 AliDebug(1, Form("processing event %d", iEvent));
134 if ( taskIndex == AliQA::kRAWS ) {
135 if ( !fRawReader->NextEvent() )
137 } else if ( taskIndex == AliQA::kESDS ) {
138 if ( fESDTree->GetEntry(iEvent) == 0 )
141 if ( fRunLoader->GetEvent(iEvent) != 0 )
144 // loop over detectors
145 TObjArray* detArray = NULL ;
146 if (fRunLoader) // check if RunLoader exists
147 if ( fRunLoader->GetAliRun() ) // check if AliRun exists in gAlice.root
148 detArray = fRunLoader->GetAliRun()->Detectors() ;
149 for (UInt_t iDet = 0 ; iDet < fgkNDetectors ; iDet++) {
151 AliModule* det = static_cast<AliModule*>(detArray->FindObject(AliQA::GetDetName(iDet))) ;
152 if (!det || !det->IsActive())
155 if (!IsSelected(AliQA::GetDetName(iDet)))
157 AliQADataMaker * qadm = GetQADataMaker(iDet) ;
161 if ( qadm->IsCycleDone() ) {
162 qadm->EndOfCycle(AliQA::kRAWS) ;
163 qadm->StartOfCycle(AliQA::kRAWS) ;
168 qadm->Exec(taskIndex, fRawReader) ;
171 GetLoader(iDet)->LoadHits() ;
172 data = GetLoader(iDet)->TreeH() ;
174 AliWarning(Form(" Hit Tree not found for %s", AliQA::GetDetName(iDet))) ;
176 qadm->Exec(taskIndex, data) ;
179 case AliQA::kSDIGITS :
180 GetLoader(iDet)->LoadSDigits() ;
181 data = GetLoader(iDet)->TreeS() ;
183 AliWarning(Form(" SDigit Tree not found for %s", AliQA::GetDetName(iDet))) ;
185 qadm->Exec(taskIndex, data) ;
188 case AliQA::kDIGITS :
189 GetLoader(iDet)->LoadDigits() ;
190 data = GetLoader(iDet)->TreeD() ;
192 AliWarning(Form(" Digit Tree not found for %s", AliQA::GetDetName(iDet))) ;
194 qadm->Exec(taskIndex, data) ;
197 case AliQA::kRECPOINTS :
198 GetLoader(iDet)->LoadRecPoints() ;
199 data = GetLoader(iDet)->TreeR() ;
201 AliWarning(Form("RecPoints not found for %s", AliQA::GetDetName(iDet))) ;
203 qadm->Exec(taskIndex, data) ;
206 case AliQA::kTRACKSEGMENTS :
208 case AliQA::kRECPARTICLES :
211 qadm->Exec(taskIndex, fESD) ;
213 case AliQA::kNTASKINDEX :
220 // Save QA data for all detectors
221 rv = Finish(taskIndex) ;
225 //_____________________________________________________________________________
226 Bool_t AliQADataMakerSteer::Finish(const AliQA::TASKINDEX taskIndex)
228 // write output to file for all detectors
229 for (UInt_t iDet = 0; iDet < fgkNDetectors ; iDet++) {
230 if (IsSelected(AliQA::GetDetName(iDet))) {
231 AliQADataMaker * qadm = GetQADataMaker(iDet) ;
233 qadm->EndOfCycle(taskIndex) ;
240 //_____________________________________________________________________________
241 TObjArray * AliQADataMakerSteer::GetFromOCDB(AliQA::DETECTORINDEX det, AliQA::TASKINDEX task) const
243 // Retrieve the list of QA data for a given detector and a given task
244 TObjArray * rv = NULL ;
245 TString tmp(AliQA::GetQARefStorage()) ;
246 if ( tmp.IsNull() ) {
247 AliError("No storage defined, use AliQA::SetQARefStorage") ;
250 AliCDBManager* man = AliCDBManager::Instance() ;
251 if ( ! man->IsDefaultStorageSet() ) {
252 man->SetDefaultStorage(AliQA::GetQARefDefaultStorage()) ;
253 man->SetSpecificStorage(Form("%s/*", AliQA::GetQAOCDBDirName()), AliQA::GetQARefStorage()) ;
255 char detOCDBDir[10] ;
256 sprintf(detOCDBDir, "%s/%s/%s", AliQA::GetQAOCDBDirName(), AliQA::GetDetName((Int_t)det), AliQA::GetRefOCDBDirName()) ;
257 AliInfo(Form("Retrieving reference data from %s/%s for %s", AliQA::GetQARefStorage(), detOCDBDir, AliQA::GetTaskName(task).Data())) ;
258 AliCDBEntry* entry = man->Get(detOCDBDir, 0) ; //FIXME 0 --> Run Number
259 TList * listDetQAD = dynamic_cast<TList *>(entry->GetObject()) ;
261 rv = dynamic_cast<TObjArray *>(listDetQAD->FindObject(AliQA::GetTaskName(task))) ;
265 //_____________________________________________________________________________
266 AliLoader * AliQADataMakerSteer::GetLoader(Int_t iDet)
268 // get the loader for a detector
270 TString detName = AliQA::GetDetName(iDet) ;
271 fLoader[iDet] = fRunLoader->GetLoader(detName + "Loader");
273 return fLoader[iDet] ;
275 // load the QA data maker object
276 TPluginManager* pluginManager = gROOT->GetPluginManager() ;
277 TString loaderName = "Ali" + detName + "Loader" ;
279 AliLoader * loader = NULL ;
280 // first check if a plugin is defined for the quality assurance data maker
281 TPluginHandler* pluginHandler = pluginManager->FindHandler("AliLoader", detName) ;
282 // if not, add a plugin for it
283 if (!pluginHandler) {
284 AliDebug(1, Form("defining plugin for %s", loaderName.Data())) ;
285 TString libs = gSystem->GetLibraries() ;
286 if (libs.Contains("lib" + detName + "base.so") || (gSystem->Load("lib" + detName + "base.so") >= 0)) {
287 pluginManager->AddHandler("AliQADataMaker", detName, loaderName, detName + "loader", loaderName + "()") ;
289 pluginManager->AddHandler("AliLoader", detName, loaderName, detName, loaderName + "()") ;
291 pluginHandler = pluginManager->FindHandler("AliLoader", detName) ;
293 if (pluginHandler && (pluginHandler->LoadPlugin() == 0)) {
294 loader = (AliLoader *) pluginHandler->ExecPlugin(0) ;
297 fLoader[iDet] = loader ;
301 //_____________________________________________________________________________
302 AliQADataMaker * AliQADataMakerSteer::GetQADataMaker(Int_t iDet)
304 // get the quality assurance data maker for a detector
306 if (fQADataMaker[iDet])
307 return fQADataMaker[iDet] ;
309 AliQADataMaker * qadm = NULL ;
312 // load the QA data maker object
313 TPluginManager* pluginManager = gROOT->GetPluginManager() ;
314 TString detName = AliQA::GetDetName(iDet) ;
315 TString qadmName = "Ali" + detName + "QADataMaker" ;
317 // first check if a plugin is defined for the quality assurance data maker
318 TPluginHandler* pluginHandler = pluginManager->FindHandler("AliQADataMaker", detName) ;
319 // if not, add a plugin for it
320 if (!pluginHandler) {
321 AliDebug(1, Form("defining plugin for %s", qadmName.Data())) ;
322 TString libs = gSystem->GetLibraries() ;
323 if (libs.Contains("lib" + detName + "base.so") || (gSystem->Load("lib" + detName + "base.so") >= 0)) {
324 pluginManager->AddHandler("AliQADataMaker", detName, qadmName, detName + "qadm", qadmName + "()") ;
326 pluginManager->AddHandler("AliQADataMaker", detName, qadmName, detName, qadmName + "()") ;
328 pluginHandler = pluginManager->FindHandler("AliQADataMaker", detName) ;
330 if (pluginHandler && (pluginHandler->LoadPlugin() == 0)) {
331 qadm = (AliQADataMaker *) pluginHandler->ExecPlugin(0) ;
334 fQADataMaker[iDet] = qadm ;
339 //_____________________________________________________________________________
340 Bool_t AliQADataMakerSteer::Init(const AliQA::TASKINDEX taskIndex, const char * input )
342 // Initialize the event source and QA data makers
344 if (taskIndex == AliQA::kRAWS) {
346 TString fileName(input);
347 if (fileName.EndsWith("/")) {
348 fRawReader = new AliRawReaderFile(fileName);
349 } else if (fileName.EndsWith(".root")) {
350 fRawReader = new AliRawReaderRoot(fileName);
351 } else if (!fileName.IsNull()) {
352 fRawReader = new AliRawReaderDate(fileName);
353 fRawReader->SelectEvents(7);
358 fRawReader->NextEvent() ;
359 fRunNumber = fRawReader->GetRunNumber() ;
360 fRawReader->RewindEvents();
361 fNumberOfEvents = 999999 ;
362 } else if (taskIndex == AliQA::kESDS) {
363 if (!gSystem->AccessPathName("AliESDs.root")) { // AliESDs.root exists
364 TFile * esdFile = TFile::Open("AliESDs.root") ;
365 fESDTree = dynamic_cast<TTree *> (esdFile->Get("esdTree")) ;
366 fESD = new AliESDEvent() ;
367 fESD->ReadFromTree(fESDTree) ;
368 fESDTree->GetEntry(0) ;
369 fRunNumber = fESD->GetRunNumber() ;
370 fNumberOfEvents = fESDTree->GetEntries() ;
372 AliError("AliESDs.root not found") ;
376 if ( !InitRunLoader() ) {
377 AliWarning("No Run Loader not found") ;
379 fNumberOfEvents = fRunLoader->GetNumberOfEvents() ;
382 // Initialize all QA data makers for all detectors
383 for (UInt_t iDet = 0; iDet < fgkNDetectors ; iDet++) {
384 if (IsSelected(AliQA::GetDetName(iDet))) {
385 AliQADataMaker * qadm = GetQADataMaker(iDet) ;
387 AliWarning(Form("AliQADataMaker not found for %s", AliQA::GetDetName(iDet))) ;
389 AliInfo(Form("Data Maker found for %s", qadm->GetName())) ;
390 qadm->Init(taskIndex, fRunNumber, GetQACycles(iDet)) ;
391 qadm->StartOfCycle(taskIndex, fCycleSame) ;
399 //_____________________________________________________________________________
400 Bool_t AliQADataMakerSteer::IsSelected(const char * det)
402 // check whether detName is contained in detectors
403 // if yes, it is removed from detectors
405 const TString detName(det) ;
406 // check if all detectors are selected
407 if ((fDetectors.CompareTo("ALL") == 0) ||
408 fDetectors.BeginsWith("ALL ") ||
409 fDetectors.EndsWith(" ALL") ||
410 fDetectors.Contains(" ALL ")) {
415 // search for the given detector
417 if ((fDetectors.CompareTo(detName) == 0) ||
418 fDetectors.BeginsWith(detName+" ") ||
419 fDetectors.EndsWith(" "+detName) ||
420 fDetectors.Contains(" "+detName+" ")) {
421 // fDetectors.ReplaceAll(detName, "");
425 // clean up the detectors string
426 // while (fDetectors.Contains(" "))
427 // fDetectors.ReplaceAll(" ", " ");
428 // while (fDetectors.BeginsWith(" "))
429 // fDetectors.Remove(0, 1);
430 // while (fDetectors.EndsWith(" "))
431 // fDetectors.Remove(fDetectors.Length()-1, 1);
436 //_____________________________________________________________________________
437 Bool_t AliQADataMakerSteer::InitRunLoader()
439 // get or create the run loader
445 if (!gSystem->AccessPathName(fGAliceFileName.Data())) { // galice.root exists
446 // load all base libraries to get the loader classes
447 TString libs = gSystem->GetLibraries() ;
448 for (UInt_t iDet = 0; iDet < fgkNDetectors; iDet++) {
449 if (!IsSelected(AliQA::GetDetName(iDet)))
451 TString detName = AliQA::GetDetName(iDet) ;
452 if (detName == "HLT")
454 if (libs.Contains("lib" + detName + "base.so"))
456 gSystem->Load("lib" + detName + "base.so");
458 fRunLoader = AliRunLoader::Open(fGAliceFileName.Data());
460 AliError(Form("no run loader found in file %s", fGAliceFileName.Data()));
463 fRunLoader->CdGAFile();
464 if (fRunLoader->LoadgAlice() == 0) {
465 gAlice = fRunLoader->GetAliRun();
469 AliError(Form("no gAlice object found in file %s", fGAliceFileName.Data()));
473 } else { // galice.root does not exist
474 AliError(Form("the file %s does not exist", fGAliceFileName.Data()));
481 //_____________________________________________________________________________
482 Bool_t AliQADataMakerSteer::Merge(const Int_t runNumber) const
484 // Merge all the cycles from all detectors in one single file per run
486 if ( runNumber == -1 )
487 sprintf(cmd, ".! ls *%s*.*.*.root > tempo.txt", AliQA::GetQADataFileName()) ;
489 sprintf(cmd, ".! ls *%s*.%d.*.root > tempo.txt", AliQA::GetQADataFileName(), runNumber) ;
490 gROOT->ProcessLine(cmd) ;
491 ifstream in("tempo.txt") ;
492 const Int_t runMax = 10 ;
493 TString file[AliQA::kNDET*runMax] ;
494 Int_t run[AliQA::kNDET*runMax] ;
505 AliError(Form("run number %d not found", runNumber)) ;
509 Int_t previousRun = -1 ;
512 sprintf(stmp, ".%s.", AliQA::GetQADataFileName()) ;
513 for (Int_t ifile = 0 ; ifile < index-1 ; ifile++) {
514 TString tmp(file[ifile]) ;
515 tmp.ReplaceAll(".root", "") ;
516 TString det = tmp(0, tmp.Index(".")) ;
517 tmp.Remove(0, tmp.Index(stmp)+4) ;
518 TString ttmp = tmp(0, tmp.Index(".")) ;
519 Int_t newRun = ttmp.Atoi() ;
520 if (newRun != previousRun) {
521 run[runIndex] = newRun ;
522 previousRun = newRun ;
525 ttmp = tmp(tmp.Index("."), tmp.Length()) ;
526 Int_t cycle = ttmp.Atoi() ;
527 AliInfo(Form("%s : det = %s run = %d cycle = %d \n", file[ifile].Data(), det.Data(), newRun, cycle)) ;
529 for (Int_t irun = 0 ; irun < runIndex ; irun++) {
531 char outFileName[20] ;
532 sprintf(outFileName, "Merged.%s.%d.root", AliQA::GetQADataFileName(), runIndex-1) ;
533 merger.OutputFile(outFileName) ;
534 for (Int_t ifile = 0 ; ifile < index-1 ; ifile++) {
536 sprintf(pattern, "%s.%d.", AliQA::GetQADataFileName(), runIndex-1) ;
537 TString tmp(file[ifile]) ;
538 if (tmp.Contains(pattern))
539 merger.AddFile(tmp) ;
547 //_____________________________________________________________________________
548 void AliQADataMakerSteer::Reset()
550 // Reset the default data members
551 for (UInt_t iDet = 0; iDet < fgkNDetectors; iDet++) {
552 if (IsSelected(AliQA::GetDetName(iDet))) {
553 fLoader[iDet] = NULL;
554 if (fQADataMaker[iDet]) {
555 (fQADataMaker[iDet])->Reset() ;
556 //delete fQADataMaker[iDet] ;
557 //fQADataMaker[iDet] = NULL ;
562 if (fRawReaderDelete) {
567 fCycleSame = kFALSE ;
571 fNumberOfEvents = 999999 ;
574 //_____________________________________________________________________________
575 Bool_t AliQADataMakerSteer::Run(const char * detectors, AliRawReader * rawReader)
577 //Runs all the QA data Maker for Raws only
578 fRawReader = rawReader ;
579 fRawReaderDelete = kFALSE ;
581 fDetectors = detectors ;
583 // Initialize all QA data makers for all detectors
584 for (UInt_t iDet = 0; iDet < fgkNDetectors ; iDet++) {
585 if (IsSelected(AliQA::GetDetName(iDet))) {
586 AliQADataMaker * qadm = GetQADataMaker(iDet) ;
588 AliWarning(Form("AliQADataMaker not found for %s", AliQA::GetDetName(iDet))) ;
590 AliInfo(Form("Data Maker found for %s", qadm->GetName())) ;
591 qadm->Init(AliQA::kRAWS, fRunNumber, GetQACycles(iDet)) ;
592 qadm->StartOfCycle(AliQA::kRAWS, fCycleSame) ;
598 return DoIt(AliQA::kRAWS) ;
601 //_____________________________________________________________________________
602 Bool_t AliQADataMakerSteer::Run(const char * detectors, const AliQA::TASKINDEX taskIndex, const char * fileName )
604 // Runs all the QA data Maker for every detector
607 fDetectors = detectors ;
609 if ( !Init(taskIndex, fileName) )
612 rv = DoIt(taskIndex) ;
618 //_____________________________________________________________________________
619 Bool_t AliQADataMakerSteer::Save2OCDB(const Int_t runNumber, const Int_t cycleNumber, const char * detectors) const
621 // take the locasl QA data merge into a single file and save in OCDB
623 TString tmp(AliQA::GetQARefStorage()) ;
624 if ( tmp.IsNull() ) {
625 AliError("No storage defined, use AliQA::SetQARefStorage") ;
628 if ( !(tmp.Contains(AliQA::GetLabLocalOCDB()) || tmp.Contains(AliQA::GetLabAliEnOCDB())) ) {
629 AliError(Form("%s is a wrong storage, use %s or %s", AliQA::GetQARefStorage(), AliQA::GetLabLocalOCDB().Data(), AliQA::GetLabAliEnOCDB().Data())) ;
632 TString sdet(detectors) ;
635 if ( sdet.Contains("ALL") ) {
636 rv = Merge(runNumber) ;
639 char inputFileName[20] ;
640 sprintf(inputFileName, "Merged.%s.%d.root", AliQA::GetQADataFileName(), runNumber) ;
641 inputFile = TFile::Open(inputFileName) ;
642 rv = SaveIt2OCDB(runNumber, inputFile) ;
644 for (Int_t index = 0; index < AliQA::kNDET; index++) {
645 if (sdet.Contains(AliQA::GetDetName(index))) {
646 char inputFileName[20] ;
647 sprintf(inputFileName, "%s.%s.%d.%d.root", AliQA::GetDetName(index), AliQA::GetQADataFileName(), runNumber, cycleNumber) ;
648 inputFile = TFile::Open(inputFileName) ;
649 rv *= SaveIt2OCDB(runNumber, inputFile) ;
656 //_____________________________________________________________________________
657 Bool_t AliQADataMakerSteer::SaveIt2OCDB(const Int_t runNumber, TFile * inputFile) const
659 // reads the TH1 from file and adds it to appropriate list before saving to OCDB
661 AliInfo(Form("Saving TH1s in %s to %s", inputFile->GetName(), AliQA::GetQARefStorage())) ;
662 AliCDBManager* man = AliCDBManager::Instance() ;
663 if ( ! man->IsDefaultStorageSet() ) {
664 man->SetDefaultStorage(AliQA::GetQARefDefaultStorage()) ;
665 man->SetSpecificStorage(Form("%s/*", AliQA::GetQAOCDBDirName()), AliQA::GetQARefStorage()) ;
667 for ( Int_t detIndex = 0 ; detIndex < AliQA::kNDET ; detIndex++) {
668 TDirectory * detDir = inputFile->GetDirectory(AliQA::GetDetName(detIndex)) ;
670 AliInfo(Form("Entering %s", detDir->GetName())) ;
671 char detOCDBDir[20] ;
672 sprintf(detOCDBDir, "%s/%s/%s", AliQA::GetQAOCDBDirName(), AliQA::GetDetName(detIndex), AliQA::GetRefOCDBDirName()) ;
673 AliCDBId idr(detOCDBDir, runNumber, 999999999) ;
674 TList * listDetQAD = new TList() ;
676 sprintf(listName, "%s QA data Reference", AliQA::GetDetName(detIndex)) ;
677 listDetQAD->SetName(listName) ;
678 TList * taskList = detDir->GetListOfKeys() ;
679 TIter nextTask(taskList) ;
681 while ( (taskKey = dynamic_cast<TKey*>(nextTask())) ) {
682 TDirectory * taskDir = detDir->GetDirectory(taskKey->GetName()) ;
683 AliInfo(Form("Saving %s", taskDir->GetName())) ;
684 TObjArray * listTaskQAD = new TObjArray(100) ;
685 listTaskQAD->SetName(taskKey->GetName()) ;
686 listDetQAD->Add(listTaskQAD) ;
687 TList * histList = taskDir->GetListOfKeys() ;
688 TIter nextHist(histList) ;
690 while ( (histKey = dynamic_cast<TKey*>(nextHist())) ) {
691 TObject * odata = taskDir->Get(histKey->GetName()) ;
692 if ( odata->IsA()->InheritsFrom("TH1") ) {
693 AliInfo(Form("Adding %s", histKey->GetName())) ;
694 TH1 * hdata = static_cast<TH1*>(odata) ;
695 listTaskQAD->Add(hdata) ;
700 man->Put(listDetQAD, idr, &mdr) ;