New steering class ro run QA stand alone
[u/mrichter/AliRoot.git] / STEER / AliQADataMakerSteer.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 #include <TFile.h>
19 #include <TPluginManager.h>
20 #include <TROOT.h>
21 #include <TString.h>
22 #include <TSystem.h>
23
24 #include "AliESDEvent.h"
25 #include "AliLog.h"
26 #include "AliQA.h"
27 #include "AliQADataMaker.h"
28 #include "AliQADataMakerSteer.h" 
29 #include "AliRawReaderRoot.h"
30 #include "AliRun.h"
31 #include "AliRunLoader.h"
32
33 ClassImp(AliQADataMakerSteer) 
34
35 //_____________________________________________________________________________
36 AliQADataMakerSteer::AliQADataMakerSteer(const char* gAliceFilename, const char * name, const char * title) :
37         TNamed(name, title), 
38         fCycleOption("new"), 
39         fESD(NULL), 
40         fESDTree(NULL),
41         fFirst(kTRUE),  
42         fGAliceFileName(gAliceFilename), 
43         fRunNumber(0), 
44         fNumberOfEvents(0), 
45         fRawReader(NULL), 
46         fRunLoader(NULL)  
47 {
48         // default ctor
49         for (UInt_t iDet = 0; iDet < fgkNDetectors; iDet++) {
50                 fLoader[iDet]      = NULL ;
51                 fQADataMaker[iDet] = NULL ;
52                 fQACycles[iDet]    = 999999 ;   
53         }       
54 }
55
56 //_____________________________________________________________________________
57 AliQADataMakerSteer::AliQADataMakerSteer(const AliQADataMakerSteer & qas) : 
58         TNamed(qas), 
59         fCycleOption("new"), 
60         fESD(NULL), 
61         fESDTree(NULL), 
62         fFirst(qas.fFirst),  
63         fGAliceFileName(qas.fGAliceFileName), 
64         fRunNumber(qas.fRunNumber), 
65         fNumberOfEvents(qas.fNumberOfEvents), 
66         fRawReader(NULL), 
67         fRunLoader(NULL)  
68 {
69         // cpy ctor
70         for (UInt_t iDet = 0; iDet < fgkNDetectors; iDet++) {
71                 fLoader[iDet]      = qas.fLoader[iDet] ;
72                 fQADataMaker[iDet] = qas.fQADataMaker[iDet] ;
73                 fQACycles[iDet]    = qas.fQACycles[iDet] ;      
74         }
75 }
76
77 //_____________________________________________________________________________
78 AliQADataMakerSteer & AliQADataMakerSteer::operator = (const AliQADataMakerSteer & qas) 
79 {
80         // assignment operator
81   this->~AliQADataMakerSteer() ;
82   new(this) AliQADataMakerSteer(qas) ;
83   return *this ;
84 }
85
86 //_____________________________________________________________________________
87 AliQADataMakerSteer::~AliQADataMakerSteer() 
88 {
89         // dtor
90   for (UInt_t iDet = 0; iDet < fgkNDetectors; iDet++) {
91     fLoader[iDet] = NULL;
92     delete fQADataMaker[iDet];
93     fQADataMaker[iDet] = NULL;
94   }
95
96   delete fRunLoader;
97   fRunLoader = NULL;
98   delete fRawReader;
99   fRawReader = NULL;
100 }
101
102 //_____________________________________________________________________________
103 AliLoader * AliQADataMakerSteer::GetLoader(Int_t iDet)
104 {
105         // get the loader for a detector
106         
107         TString detName = AliQA::GetDetName(iDet) ;
108     fLoader[iDet] = fRunLoader->GetLoader(detName + "Loader");
109         if (fLoader[iDet]) 
110                 return fLoader[iDet] ;
111         
112         // load the QA data maker object
113         TPluginManager* pluginManager = gROOT->GetPluginManager() ;
114         TString loaderName = "Ali" + detName + "Loader" ;
115
116         AliLoader * loader = NULL ;
117         // first check if a plugin is defined for the quality assurance data maker
118         TPluginHandler* pluginHandler = pluginManager->FindHandler("AliLoader", detName) ;
119         // if not, add a plugin for it
120         if (!pluginHandler) {
121                 AliDebug(1, Form("defining plugin for %s", loaderName.Data())) ;
122                 TString libs = gSystem->GetLibraries() ;
123                 if (libs.Contains("lib" + detName + "base.so") || (gSystem->Load("lib" + detName + "base.so") >= 0)) {
124                         pluginManager->AddHandler("AliQADataMaker", detName, loaderName, detName + "loader", loaderName + "()") ;
125                 } else {
126                         pluginManager->AddHandler("AliLoader", detName, loaderName, detName, loaderName + "()") ;
127                 }
128                 pluginHandler = pluginManager->FindHandler("AliLoader", detName) ;
129         }
130         if (pluginHandler && (pluginHandler->LoadPlugin() == 0)) {
131                 loader = (AliLoader *) pluginHandler->ExecPlugin(0) ;
132         }
133         if (loader) 
134                 fLoader[iDet] = loader ;
135         return loader ;
136 }
137
138 //_____________________________________________________________________________
139 AliQADataMaker * AliQADataMakerSteer::GetQADataMaker(Int_t iDet)
140 {
141         // get the quality assurance data maker for a detector
142         
143         if (fQADataMaker[iDet]) 
144                 return fQADataMaker[iDet] ;
145         
146         AliQADataMaker * qadm = NULL ;
147         
148         if (fFirst) {
149                 // load the QA data maker object
150                 TPluginManager* pluginManager = gROOT->GetPluginManager() ;
151                 TString detName = AliQA::GetDetName(iDet) ;
152                 TString qadmName = "Ali" + detName + "QADataMaker" ;
153
154                 // first check if a plugin is defined for the quality assurance data maker
155                 TPluginHandler* pluginHandler = pluginManager->FindHandler("AliQADataMaker", detName) ;
156                 // if not, add a plugin for it
157                 if (!pluginHandler) {
158                         AliDebug(1, Form("defining plugin for %s", qadmName.Data())) ;
159                         TString libs = gSystem->GetLibraries() ;
160                         if (libs.Contains("lib" + detName + "base.so") || (gSystem->Load("lib" + detName + "base.so") >= 0)) {
161                                 pluginManager->AddHandler("AliQADataMaker", detName, qadmName, detName + "qadm", qadmName + "()") ;
162                         } else {
163                                 pluginManager->AddHandler("AliQADataMaker", detName, qadmName, detName, qadmName + "()") ;
164                         }
165                         pluginHandler = pluginManager->FindHandler("AliQADataMaker", detName) ;
166                 }
167                 if (pluginHandler && (pluginHandler->LoadPlugin() == 0)) {
168                         qadm = (AliQADataMaker *) pluginHandler->ExecPlugin(0) ;
169                 }
170                 if (qadm) 
171                         fQADataMaker[iDet] = qadm ;
172         }
173         return qadm ;
174 }
175
176 //_____________________________________________________________________________
177 Bool_t AliQADataMakerSteer::Init(const AliQA::TASKINDEX taskIndex, const  char * fileName )
178 {
179         // Initialize the event source and QA data makers
180         
181         if (taskIndex == AliQA::kRAWS) {
182                 fRawReader = new AliRawReaderRoot(fileName) ; 
183             if ( ! fRawReader ) 
184                         return kFALSE ; 
185                 fRawReader->NextEvent() ; 
186                 fRunNumber = fRawReader->GetRunNumber() ; 
187                 fRawReader->RewindEvents();
188                 fNumberOfEvents = 999999 ;
189                 
190         } else if (taskIndex == AliQA::kESDS) {
191                 if (!gSystem->AccessPathName("AliESDs.root")) { // AliESDs.root exists
192                         TFile * esdFile = TFile::Open("AliESDs.root") ;
193                         fESDTree = dynamic_cast<TTree *> (esdFile->Get("esdTree")) ; 
194                 fESD     = new AliESDEvent() ;
195             fESD->ReadFromTree(fESDTree) ;
196                         fESDTree->GetEntry(0) ; 
197                         fRunNumber = fESD->GetRunNumber() ; 
198                         fNumberOfEvents = fESDTree->GetEntries() ;
199                 } else {
200                         AliError("AliESDs.root not found") ; 
201                         return kFALSE ; 
202                 }
203                 
204         } else {
205                 if ( !InitRunLoader() ) {
206                         AliError("Problems in getting the Run Loader") ; 
207                         return kFALSE ; 
208                 } else {
209                         fRunNumber      = fRunLoader->GetAliRun()->GetRunNumber() ; 
210                         fNumberOfEvents = fRunLoader->GetNumberOfEvents() ;
211                 }
212         }
213                 
214         // Initialize all QA data makers for all detectors
215         for (UInt_t iDet = 0; iDet < fgkNDetectors ; iDet++) {
216                 AliQADataMaker * qadm = GetQADataMaker(iDet) ;
217                 if (!qadm) {
218                         AliWarning(Form("AliQADataMaker not found for %s", AliQA::GetDetName(iDet))) ; 
219                 } else {
220                         AliInfo(Form("Data Maker found for %s", qadm->GetName())) ; 
221                         qadm->Init(taskIndex, fRunNumber, GetQACycles(iDet)) ;
222                         qadm->StartOfCycle(taskIndex, fCycleOption) ;
223                 }
224         } 
225         fFirst = kFALSE ;
226         return kTRUE ; 
227 }
228
229 //_____________________________________________________________________________
230 Bool_t AliQADataMakerSteer::InitRunLoader()
231 {
232         // get or create the run loader
233         if (fRunLoader) {
234                 fCycleOption = "same" ; 
235                 return kTRUE ;
236         } 
237                 
238         if (!gSystem->AccessPathName(fGAliceFileName.Data())) { // galice.root exists
239     // load all base libraries to get the loader classes
240                 TString libs = gSystem->GetLibraries() ;
241                 for (UInt_t iDet = 0; iDet < fgkNDetectors; iDet++) {
242                         TString detName = AliQA::GetDetName(iDet) ;
243                         if (detName == "HLT") 
244                                 continue;
245                         if (libs.Contains("lib" + detName + "base.so")) 
246                                 continue;
247                         gSystem->Load("lib" + detName + "base.so");
248                 }
249                 fRunLoader = AliRunLoader::Open(fGAliceFileName.Data());
250                 if (!fRunLoader) {
251                         AliError(Form("no run loader found in file %s", fGAliceFileName.Data()));
252                         return kFALSE;
253                 }
254                 fRunLoader->CdGAFile();
255                 if (fRunLoader->LoadgAlice() == 0) {
256                         gAlice = fRunLoader->GetAliRun();
257                 }
258                 if (!gAlice) {
259                         AliError(Form("no gAlice object found in file %s", fGAliceFileName.Data()));
260                         return kFALSE;
261                 }
262
263         } else {               // galice.root does not exist
264                 AliError(Form("the file %s does not exist", fGAliceFileName.Data()));
265                 return kFALSE;
266     }
267
268   return kTRUE;
269 }
270
271 //_____________________________________________________________________________
272 Bool_t AliQADataMakerSteer::Finish(const AliQA::TASKINDEX taskIndex) 
273 {
274         // write output to file for all detectors
275         for (UInt_t iDet = 0; iDet < fgkNDetectors ; iDet++) {
276                 AliQADataMaker * qadm = GetQADataMaker(iDet) ;
277                 if (qadm) {
278                         qadm->EndOfCycle(taskIndex) ; 
279                         qadm->Finish(taskIndex) ; 
280                 }
281         }
282         return kTRUE ; 
283 }
284
285 //_____________________________________________________________________________
286 Bool_t AliQADataMakerSteer::Run(const AliQA::TASKINDEX taskIndex, const  char * fileName )
287 {
288         // Runs all the QA data Maker for every detector
289         Bool_t rv = kFALSE ;
290         
291         if ( !Init(taskIndex, fileName) ) 
292                 return kFALSE ; 
293                 
294     // Fill QA data in event loop 
295         for (UInt_t iEvent = 0 ; iEvent < fNumberOfEvents ; iEvent++) {
296                 // Get the event
297                 AliInfo(Form("processing event %d", iEvent));
298                 if ( taskIndex == AliQA::kRAWS ) {
299                         if ( !fRawReader->NextEvent() )
300                                 break ;
301                 } else if ( taskIndex == AliQA::kESDS ) {
302                         fESDTree->GetEntry(iEvent) ;
303                 } else {
304                         fRunLoader->GetEvent(iEvent);
305                 }
306                 // loop over detectors
307                 for (UInt_t iDet = 0 ; iDet < fgkNDetectors ; iDet++) {
308                         AliQADataMaker * qadm = GetQADataMaker(iDet) ;
309                         if (!qadm) {
310                                 rv = kFALSE ;
311                         } else {
312                                 if ( qadm->IsCycleDone() ) {
313                                         qadm->EndOfCycle(AliQA::kRAWS) ;
314                                         qadm->StartOfCycle(AliQA::kRAWS) ;
315                                 }
316                                 TTree * data ; 
317                                 switch (taskIndex) {
318                                         case AliQA::kRAWS :
319                                                 qadm->Exec(taskIndex, fRawReader) ; 
320                                         break ; 
321                                         case AliQA::kHITS :
322                                                 GetLoader(iDet)->LoadHits() ; 
323                                                 data = GetLoader(iDet)->TreeH() ; 
324                                                 if ( ! data ) {
325                                                         AliWarning(Form(" Hit Tree not found for  %s", AliQA::GetDetName(iDet))) ; 
326                                                 } else {
327                                                         qadm->Exec(taskIndex, data) ;
328                                                 } 
329                                         break ;
330                                         case AliQA::kSDIGITS :
331                                                 GetLoader(iDet)->LoadSDigits() ; 
332                                                 data = GetLoader(iDet)->TreeS() ; 
333                                                 if ( ! data ) {
334                                                         AliWarning(Form(" SDigit Tree not found for  %s", AliQA::GetDetName(iDet))) ; 
335                                                 } else {
336                                                         qadm->Exec(taskIndex, data) ; 
337                                                 }
338                                         break; 
339                                         case AliQA::kDIGITS :
340                                                 GetLoader(iDet)->LoadDigits() ; 
341                                                 data = GetLoader(iDet)->TreeD() ; 
342                                                 if ( ! data ) {
343                                                         AliWarning(Form(" Digit Tree not found for  %s", AliQA::GetDetName(iDet))) ; 
344                                                 } else {
345                                                         qadm->Exec(taskIndex, data) ;
346                                                 }
347                                         break; 
348                                         case AliQA::kRECPOINTS :
349                                                 GetLoader(iDet)->LoadRecPoints() ; 
350                                                 data = GetLoader(iDet)->TreeR() ; 
351                                                 if (!data) {
352                                                         AliWarning(Form("RecPoints not found for %s", AliQA::GetDetName(iDet))) ; 
353                                                 } else {
354                                                         qadm->Exec(taskIndex, data) ; 
355                                                 }
356                                         break; 
357                                         case AliQA::kTRACKSEGMENTS :
358                                         break; 
359                                         case AliQA::kRECPARTICLES :
360                                         break; 
361                                         case AliQA::kESDS :
362                                                 qadm->Exec(taskIndex, fESD) ;
363                                         break; 
364                                 } //task switch
365                                 qadm->Increment() ; 
366                         } //data maker exist
367                 } // detector loop
368         } // event loop 
369         // Save QA data for all detectors
370         rv = Finish(taskIndex) ;
371         return rv ; 
372 }