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 TList * AliQADataMakerSteer::GetFromOCDB(AliQA::DETECTORINDEX det, AliQA::TASKINDEX task) const
243 // Retrieve the list of QA data for a given detector and a given task
245 AliCDBManager* man = AliCDBManager::Instance() ;
246 man->SetDefaultStorage("local://TestCDB") ;
247 char detOCDBDir[20] ;
248 sprintf(detOCDBDir, "QA/Ref/%s", AliQA::GetDetName((Int_t)det)) ;
249 AliInfo(Form("Retrieving reference data from local://TestCDB/%s %s", detOCDBDir, AliQA::GetTaskName(task).Data())) ;
250 AliCDBEntry* entry = man->Get(detOCDBDir,0) ;
251 TList * listDetQAD = dynamic_cast<TList *>(entry->GetObject()) ;
253 rv = dynamic_cast<TList *>(listDetQAD->FindObject(AliQA::GetTaskName(task))) ;
258 //_____________________________________________________________________________
259 AliLoader * AliQADataMakerSteer::GetLoader(Int_t iDet)
261 // get the loader for a detector
263 TString detName = AliQA::GetDetName(iDet) ;
264 fLoader[iDet] = fRunLoader->GetLoader(detName + "Loader");
266 return fLoader[iDet] ;
268 // load the QA data maker object
269 TPluginManager* pluginManager = gROOT->GetPluginManager() ;
270 TString loaderName = "Ali" + detName + "Loader" ;
272 AliLoader * loader = NULL ;
273 // first check if a plugin is defined for the quality assurance data maker
274 TPluginHandler* pluginHandler = pluginManager->FindHandler("AliLoader", detName) ;
275 // if not, add a plugin for it
276 if (!pluginHandler) {
277 AliDebug(1, Form("defining plugin for %s", loaderName.Data())) ;
278 TString libs = gSystem->GetLibraries() ;
279 if (libs.Contains("lib" + detName + "base.so") || (gSystem->Load("lib" + detName + "base.so") >= 0)) {
280 pluginManager->AddHandler("AliQADataMaker", detName, loaderName, detName + "loader", loaderName + "()") ;
282 pluginManager->AddHandler("AliLoader", detName, loaderName, detName, loaderName + "()") ;
284 pluginHandler = pluginManager->FindHandler("AliLoader", detName) ;
286 if (pluginHandler && (pluginHandler->LoadPlugin() == 0)) {
287 loader = (AliLoader *) pluginHandler->ExecPlugin(0) ;
290 fLoader[iDet] = loader ;
294 //_____________________________________________________________________________
295 AliQADataMaker * AliQADataMakerSteer::GetQADataMaker(Int_t iDet)
297 // get the quality assurance data maker for a detector
299 if (fQADataMaker[iDet])
300 return fQADataMaker[iDet] ;
302 AliQADataMaker * qadm = NULL ;
305 // load the QA data maker object
306 TPluginManager* pluginManager = gROOT->GetPluginManager() ;
307 TString detName = AliQA::GetDetName(iDet) ;
308 TString qadmName = "Ali" + detName + "QADataMaker" ;
310 // first check if a plugin is defined for the quality assurance data maker
311 TPluginHandler* pluginHandler = pluginManager->FindHandler("AliQADataMaker", detName) ;
312 // if not, add a plugin for it
313 if (!pluginHandler) {
314 AliDebug(1, Form("defining plugin for %s", qadmName.Data())) ;
315 TString libs = gSystem->GetLibraries() ;
316 if (libs.Contains("lib" + detName + "base.so") || (gSystem->Load("lib" + detName + "base.so") >= 0)) {
317 pluginManager->AddHandler("AliQADataMaker", detName, qadmName, detName + "qadm", qadmName + "()") ;
319 pluginManager->AddHandler("AliQADataMaker", detName, qadmName, detName, qadmName + "()") ;
321 pluginHandler = pluginManager->FindHandler("AliQADataMaker", detName) ;
323 if (pluginHandler && (pluginHandler->LoadPlugin() == 0)) {
324 qadm = (AliQADataMaker *) pluginHandler->ExecPlugin(0) ;
327 fQADataMaker[iDet] = qadm ;
332 //_____________________________________________________________________________
333 Bool_t AliQADataMakerSteer::Init(const AliQA::TASKINDEX taskIndex, const char * input )
335 // Initialize the event source and QA data makers
337 if (taskIndex == AliQA::kRAWS) {
339 TString fileName(input);
340 if (fileName.EndsWith("/")) {
341 fRawReader = new AliRawReaderFile(fileName);
342 } else if (fileName.EndsWith(".root")) {
343 fRawReader = new AliRawReaderRoot(fileName);
344 } else if (!fileName.IsNull()) {
345 fRawReader = new AliRawReaderDate(fileName);
346 fRawReader->SelectEvents(7);
351 fRawReader->NextEvent() ;
352 fRunNumber = fRawReader->GetRunNumber() ;
353 fRawReader->RewindEvents();
354 fNumberOfEvents = 999999 ;
355 } else if (taskIndex == AliQA::kESDS) {
356 if (!gSystem->AccessPathName("AliESDs.root")) { // AliESDs.root exists
357 TFile * esdFile = TFile::Open("AliESDs.root") ;
358 fESDTree = dynamic_cast<TTree *> (esdFile->Get("esdTree")) ;
359 fESD = new AliESDEvent() ;
360 fESD->ReadFromTree(fESDTree) ;
361 fESDTree->GetEntry(0) ;
362 fRunNumber = fESD->GetRunNumber() ;
363 fNumberOfEvents = fESDTree->GetEntries() ;
365 AliError("AliESDs.root not found") ;
369 if ( !InitRunLoader() ) {
370 AliWarning("No Run Loader not found") ;
372 fNumberOfEvents = fRunLoader->GetNumberOfEvents() ;
375 // Initialize all QA data makers for all detectors
376 for (UInt_t iDet = 0; iDet < fgkNDetectors ; iDet++) {
377 if (IsSelected(AliQA::GetDetName(iDet))) {
378 AliQADataMaker * qadm = GetQADataMaker(iDet) ;
380 AliWarning(Form("AliQADataMaker not found for %s", AliQA::GetDetName(iDet))) ;
382 AliInfo(Form("Data Maker found for %s", qadm->GetName())) ;
383 qadm->Init(taskIndex, fRunNumber, GetQACycles(iDet)) ;
384 qadm->StartOfCycle(taskIndex, fCycleSame) ;
392 //_____________________________________________________________________________
393 Bool_t AliQADataMakerSteer::IsSelected(const char * det)
395 // check whether detName is contained in detectors
396 // if yes, it is removed from detectors
398 const TString detName(det) ;
399 // check if all detectors are selected
400 if ((fDetectors.CompareTo("ALL") == 0) ||
401 fDetectors.BeginsWith("ALL ") ||
402 fDetectors.EndsWith(" ALL") ||
403 fDetectors.Contains(" ALL ")) {
408 // search for the given detector
410 if ((fDetectors.CompareTo(detName) == 0) ||
411 fDetectors.BeginsWith(detName+" ") ||
412 fDetectors.EndsWith(" "+detName) ||
413 fDetectors.Contains(" "+detName+" ")) {
414 // fDetectors.ReplaceAll(detName, "");
418 // clean up the detectors string
419 // while (fDetectors.Contains(" "))
420 // fDetectors.ReplaceAll(" ", " ");
421 // while (fDetectors.BeginsWith(" "))
422 // fDetectors.Remove(0, 1);
423 // while (fDetectors.EndsWith(" "))
424 // fDetectors.Remove(fDetectors.Length()-1, 1);
429 //_____________________________________________________________________________
430 Bool_t AliQADataMakerSteer::InitRunLoader()
432 // get or create the run loader
438 if (!gSystem->AccessPathName(fGAliceFileName.Data())) { // galice.root exists
439 // load all base libraries to get the loader classes
440 TString libs = gSystem->GetLibraries() ;
441 for (UInt_t iDet = 0; iDet < fgkNDetectors; iDet++) {
442 if (!IsSelected(AliQA::GetDetName(iDet)))
444 TString detName = AliQA::GetDetName(iDet) ;
445 if (detName == "HLT")
447 if (libs.Contains("lib" + detName + "base.so"))
449 gSystem->Load("lib" + detName + "base.so");
451 fRunLoader = AliRunLoader::Open(fGAliceFileName.Data());
453 AliError(Form("no run loader found in file %s", fGAliceFileName.Data()));
456 fRunLoader->CdGAFile();
457 if (fRunLoader->LoadgAlice() == 0) {
458 gAlice = fRunLoader->GetAliRun();
462 AliError(Form("no gAlice object found in file %s", fGAliceFileName.Data()));
466 } else { // galice.root does not exist
467 AliError(Form("the file %s does not exist", fGAliceFileName.Data()));
474 //_____________________________________________________________________________
475 Bool_t AliQADataMakerSteer::Merge(const Int_t runNumber) const
477 // Merge all the cycles from all detectors in one single file per run
479 if ( runNumber == -1 )
480 sprintf(cmd, ".! ls *%s*.*.*.root > tempo.txt", AliQA::GetQADataFileName()) ;
482 sprintf(cmd, ".! ls *%s*.%d.*.root > tempo.txt", AliQA::GetQADataFileName(), runNumber) ;
483 gROOT->ProcessLine(cmd) ;
484 ifstream in("tempo.txt") ;
485 const Int_t runMax = 10 ;
486 TString file[AliQA::kNDET*runMax] ;
487 Int_t run[AliQA::kNDET*runMax] ;
498 AliError(Form("run number %d not found", runNumber)) ;
502 Int_t previousRun = -1 ;
505 sprintf(stmp, ".%s.", AliQA::GetQADataFileName()) ;
506 for (Int_t ifile = 0 ; ifile < index-1 ; ifile++) {
507 TString tmp(file[ifile]) ;
508 tmp.ReplaceAll(".root", "") ;
509 TString det = tmp(0, tmp.Index(".")) ;
510 tmp.Remove(0, tmp.Index(stmp)+4) ;
511 TString ttmp = tmp(0, tmp.Index(".")) ;
512 Int_t newRun = ttmp.Atoi() ;
513 if (newRun != previousRun) {
514 run[runIndex] = newRun ;
515 previousRun = newRun ;
518 ttmp = tmp(tmp.Index("."), tmp.Length()) ;
519 Int_t cycle = ttmp.Atoi() ;
520 AliInfo(Form("%s : det = %s run = %d cycle = %d \n", file[ifile].Data(), det.Data(), newRun, cycle)) ;
522 for (Int_t irun = 0 ; irun < runIndex ; irun++) {
524 char outFileName[20] ;
525 sprintf(outFileName, "Merged.%s.%d.root", AliQA::GetQADataFileName(), runIndex-1) ;
526 merger.OutputFile(outFileName) ;
527 for (Int_t ifile = 0 ; ifile < index-1 ; ifile++) {
529 sprintf(pattern, "%s.%d.", AliQA::GetQADataFileName(), runIndex-1) ;
530 TString tmp(file[ifile]) ;
531 if (tmp.Contains(pattern))
532 merger.AddFile(tmp) ;
540 //_____________________________________________________________________________
541 void AliQADataMakerSteer::Reset()
543 // Reset the default data members
544 for (UInt_t iDet = 0; iDet < fgkNDetectors; iDet++) {
545 if (IsSelected(AliQA::GetDetName(iDet))) {
546 fLoader[iDet] = NULL;
547 if (fQADataMaker[iDet]) {
548 (fQADataMaker[iDet])->Reset() ;
549 //delete fQADataMaker[iDet] ;
550 //fQADataMaker[iDet] = NULL ;
555 if (fRawReaderDelete) {
560 fCycleSame = kFALSE ;
564 fNumberOfEvents = 999999 ;
567 //_____________________________________________________________________________
568 Bool_t AliQADataMakerSteer::Run(const char * detectors, AliRawReader * rawReader)
570 //Runs all the QA data Maker for Raws only
571 fRawReader = rawReader ;
572 fRawReaderDelete = kFALSE ;
574 fDetectors = detectors ;
576 // Initialize all QA data makers for all detectors
577 for (UInt_t iDet = 0; iDet < fgkNDetectors ; iDet++) {
578 if (IsSelected(AliQA::GetDetName(iDet))) {
579 AliQADataMaker * qadm = GetQADataMaker(iDet) ;
581 AliWarning(Form("AliQADataMaker not found for %s", AliQA::GetDetName(iDet))) ;
583 AliInfo(Form("Data Maker found for %s", qadm->GetName())) ;
584 qadm->Init(AliQA::kRAWS, fRunNumber, GetQACycles(iDet)) ;
585 qadm->StartOfCycle(AliQA::kRAWS, fCycleSame) ;
591 return DoIt(AliQA::kRAWS) ;
594 //_____________________________________________________________________________
595 Bool_t AliQADataMakerSteer::Run(const char * detectors, const AliQA::TASKINDEX taskIndex, const char * fileName )
597 // Runs all the QA data Maker for every detector
600 fDetectors = detectors ;
602 if ( !Init(taskIndex, fileName) )
605 rv = DoIt(taskIndex) ;
611 //_____________________________________________________________________________
612 Bool_t AliQADataMakerSteer::Save2OCDB(const Int_t runNumber, const Int_t cycleNumber, const char * detectors) const
614 // take the locasl QA data merge into a single file and save in OCDB
616 TString tmp(AliQA::GetQARefStorage()) ;
617 if ( tmp.IsNull() ) {
618 AliError("No storage defined, use AliQA::SetQARefStorage") ;
621 if ( !(tmp.Contains(AliQA::GetLabLocalOCDB()) || tmp.Contains(AliQA::GetLabAliEnOCDB())) ) {
622 AliError(Form("%s is a wrong storage, use %s or %s", AliQA::GetQARefStorage(), AliQA::GetLabLocalOCDB().Data(), AliQA::GetLabAliEnOCDB().Data())) ;
625 TString sdet(detectors) ;
628 if ( sdet.Contains("ALL") ) {
629 rv = Merge(runNumber) ;
632 char inputFileName[20] ;
633 sprintf(inputFileName, "Merged.%s.%d.root", AliQA::GetQADataFileName(), runNumber) ;
634 inputFile = TFile::Open(inputFileName) ;
635 rv = SaveIt2OCDB(inputFile) ;
637 for (Int_t index = 0; index < AliQA::kNDET; index++) {
638 if (sdet.Contains(AliQA::GetDetName(index))) {
639 char inputFileName[20] ;
640 sprintf(inputFileName, "%s.%s.%d.%d.root", AliQA::GetDetName(index), AliQA::GetQADataFileName(), runNumber, cycleNumber) ;
641 inputFile = TFile::Open(inputFileName) ;
642 rv *= SaveIt2OCDB(inputFile) ;
649 //_____________________________________________________________________________
650 Bool_t AliQADataMakerSteer::SaveIt2OCDB(TFile * inputFile) const
652 // reads the TH1 from file and adds it to appropriate list before saving to OCDB
654 AliInfo(Form("Saving TH1s in %s to %s", inputFile->GetName(), AliQA::GetQARefStorage())) ;
655 AliCDBManager* man = AliCDBManager::Instance() ;
656 man->SetDefaultStorage(AliQA::GetQARefStorage()) ;
658 for ( Int_t detIndex = 0 ; detIndex < AliQA::kNDET ; detIndex++) {
659 TDirectory * detDir = inputFile->GetDirectory(AliQA::GetDetName(detIndex)) ;
661 AliInfo(Form("Entering %s", detDir->GetName())) ;
662 char detOCDBDir[20] ;
663 sprintf(detOCDBDir, "QA/Ref/%s", AliQA::GetDetName(detIndex)) ;
664 AliCDBId idr(detOCDBDir,0,999999999) ;
665 TList * listDetQAD = new TList() ;
667 sprintf(listName, "%s QA data Reference", AliQA::GetDetName(detIndex)) ;
668 listDetQAD->SetName(listName) ;
669 TList * taskList = detDir->GetListOfKeys() ;
670 TIter nextTask(taskList) ;
672 while ( (taskKey = dynamic_cast<TKey*>(nextTask())) ) {
673 TDirectory * taskDir = detDir->GetDirectory(taskKey->GetName()) ;
674 AliInfo(Form("Saving %s", taskDir->GetName())) ;
675 TList * listTaskQAD = new TList() ;
676 listTaskQAD->SetName(taskKey->GetName()) ;
677 listDetQAD->Add(listTaskQAD) ;
678 TList * histList = taskDir->GetListOfKeys() ;
679 TIter nextHist(histList) ;
681 while ( (histKey = dynamic_cast<TKey*>(nextHist())) ) {
682 TObject * odata = taskDir->Get(histKey->GetName()) ;
683 if ( odata->IsA()->InheritsFrom("TH1") ) {
684 AliInfo(Form("Adding %s", histKey->GetName())) ;
685 TH1 * hdata = static_cast<TH1*>(odata) ;
686 listTaskQAD->Add(hdata) ;
691 man->Put(listDetQAD, idr, &mdr) ;