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