]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliQADataMakerSteer.cxx
New generator for beam-beam pileup simulation. The code is documented in the header...
[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 //                                                                           //
19 // class for running the QA makers                                           //
20 //                                                                           //
21 //   AliQADataMakerSteer qas;                                                //
22 //   qas.Run(AliQA::kRAWS, rawROOTFileName);                                 //
23 //   qas.Run(AliQA::kHITS);                                                  //
24 //   qas.Run(AliQA::kSDIGITS);                                               //
25 //   qas.Run(AliQA::kDIGITS);                                                //
26 //   qas.Run(AliQA::kRECPOINTS);                                             //
27 //   qas.Run(AliQA::kESDS);                                                  //
28 //                                                                           //
29 ///////////////////////////////////////////////////////////////////////////////
30
31 #include <TKey.h>
32 #include <TFile.h>
33 #include <TFileMerger.h>
34 #include <TPluginManager.h>
35 #include <TROOT.h>
36 #include <TString.h>
37 #include <TSystem.h>
38
39 #include "AliCDBManager.h"
40 #include "AliCDBEntry.h"
41 #include "AliCDBId.h"
42 #include "AliCDBMetaData.h"
43 #include "AliCodeTimer.h"
44 #include "AliCorrQADataMakerRec.h"
45 #include "AliDetectorRecoParam.h"
46 #include "AliESDEvent.h"
47 #include "AliGeomManager.h"
48 #include "AliGlobalQADataMaker.h"
49 #include "AliHeader.h"
50 #include "AliLog.h"
51 #include "AliModule.h"
52 #include "AliQA.h"
53 #include "AliQADataMakerRec.h"
54 #include "AliQADataMakerSim.h"
55 #include "AliQADataMakerSteer.h" 
56 #include "AliRawReaderDate.h"
57 #include "AliRawReaderFile.h"
58 #include "AliRawReaderRoot.h"
59 #include "AliRun.h"
60 #include "AliRunLoader.h"
61
62 ClassImp(AliQADataMakerSteer) 
63
64 //_____________________________________________________________________________
65 AliQADataMakerSteer::AliQADataMakerSteer(const Char_t * mode, const Char_t* gAliceFilename, const Char_t * name, const Char_t * title) :
66         TNamed(name, title), 
67         fCurrentEvent(0),  
68         fCycleSame(kFALSE),
69         fDetectors("ALL"), 
70         fDetectorsW("ALL"), 
71         fESD(NULL), 
72         fESDTree(NULL),
73         fGAliceFileName(gAliceFilename), 
74         fFirstEvent(0),        
75         fMaxEvents(0),   
76   fMode(mode), 
77         fNumberOfEvents(999999), 
78   fRecoParam(),
79         fRunNumber(0), 
80         fRawReader(NULL), 
81         fRawReaderDelete(kTRUE), 
82         fRunLoader(NULL), 
83   fTasks(""), 
84   fEventSpecie(AliRecoParam::kDefault)
85 {
86         // default ctor
87         fMaxEvents = fNumberOfEvents ; 
88         for (UInt_t iDet = 0; iDet < fgkNDetectors; iDet++) {
89                 if (IsSelected(AliQA::GetDetName(iDet))) {
90                         fLoader[iDet]      = NULL ;
91                         fQADataMaker[iDet] = NULL ;
92                         fQACycles[iDet]    = 999999 ;
93       fQAWriteExpert[iDet] = kTRUE ;
94                 }
95         }       
96 }
97
98 //_____________________________________________________________________________
99 AliQADataMakerSteer::AliQADataMakerSteer(const AliQADataMakerSteer & qas) : 
100         TNamed(qas), 
101         fCurrentEvent(qas.fCurrentEvent),  
102         fCycleSame(kFALSE),
103         fDetectors(qas.fDetectors), 
104         fDetectorsW(qas.fDetectorsW), 
105         fESD(NULL), 
106         fESDTree(NULL), 
107         fGAliceFileName(qas.fGAliceFileName), 
108         fFirstEvent(qas.fFirstEvent),        
109         fMaxEvents(qas.fMaxEvents),    
110         fMode(qas.fMode), 
111         fNumberOfEvents(qas.fNumberOfEvents), 
112         fRecoParam(),           
113         fRunNumber(qas.fRunNumber), 
114         fRawReader(NULL), 
115         fRawReaderDelete(kTRUE), 
116         fRunLoader(NULL), 
117   fTasks(qas.fTasks),
118   fEventSpecie(qas.fEventSpecie)
119 {
120         // cpy ctor
121         for (UInt_t iDet = 0; iDet < fgkNDetectors; iDet++) {
122                 fLoader[iDet]         = qas.fLoader[iDet] ;
123                 fQADataMaker[iDet]    = qas.fQADataMaker[iDet] ;
124                 fQACycles[iDet]       = qas.fQACycles[iDet] ;   
125     fQAWriteExpert[iDet] = qas.fQAWriteExpert[iDet] ;
126         }
127 }
128
129 //_____________________________________________________________________________
130 AliQADataMakerSteer & AliQADataMakerSteer::operator = (const AliQADataMakerSteer & qas) 
131 {
132         // assignment operator
133   this->~AliQADataMakerSteer() ;
134   new(this) AliQADataMakerSteer(qas) ;
135   return *this ;
136 }
137
138 //_____________________________________________________________________________
139 AliQADataMakerSteer::~AliQADataMakerSteer() 
140 {
141         // dtor
142         for (UInt_t iDet = 0; iDet < fgkNDetectors; iDet++) {
143                 if (IsSelected(AliQA::GetDetName(iDet))) {
144                   fLoader[iDet] = NULL;
145                   if (fQADataMaker[iDet]) {
146                           (fQADataMaker[iDet])->Finish() ; 
147                                 delete fQADataMaker[iDet] ;
148                   }
149                 }
150         }
151         if (fRawReaderDelete) { 
152                 fRunLoader = NULL ;
153                 delete fRawReader ;
154                 fRawReader = NULL ;
155         }
156 }
157
158 //_____________________________________________________________________________
159 Bool_t AliQADataMakerSteer::DoIt(const AliQA::TASKINDEX_t taskIndex)
160 {
161         // Runs all the QA data Maker for every detector
162                 
163         Bool_t rv = kFALSE ;
164     // Fill QA data in event loop 
165         for (UInt_t iEvent = fFirstEvent ; iEvent < (UInt_t)fMaxEvents ; iEvent++) {
166                 fCurrentEvent++ ; 
167                 // Get the event
168                 if ( iEvent%10 == 0  ) 
169                         AliInfo(Form("processing event %d", iEvent));
170                 if ( taskIndex == AliQA::kRAWS ) {
171                         if ( !fRawReader->NextEvent() )
172                                 break ;
173                 } else if ( taskIndex == AliQA::kESDS ) {
174                         if ( fESDTree->GetEntry(iEvent) == 0 )
175                                 break ;
176                 } else {
177                         if ( fRunLoader->GetEvent(iEvent) != 0 )
178                                 break ;
179                 }
180                 // loop  over active loaders
181                 for (UInt_t iDet = 0; iDet < fgkNDetectors ; iDet++) {
182                         if (IsSelected(AliQA::GetDetName(iDet))) {
183                                 AliQADataMaker * qadm = GetQADataMaker(iDet) ;
184                                 if (!qadm) continue; // This detector doesn't have any QA (for example, HLT)
185                                 if ( qadm->IsCycleDone() ) {
186           qadm->EndOfCycle(taskIndex) ;
187                                 }
188                                 TTree * data = NULL ; 
189                                 AliLoader* loader = GetLoader(qadm->GetUniqueID());
190                                 switch (taskIndex) {
191                                         case AliQA::kNULLTASKINDEX : 
192                                                 break ; 
193                                         case AliQA::kRAWS :
194                                                 qadm->Exec(taskIndex, fRawReader) ; 
195                                                 break ; 
196                                         case AliQA::kHITS :
197             if( loader ) {
198               loader->LoadHits() ; 
199               data = loader->TreeH() ; 
200               if ( ! data ) {
201                 AliWarning(Form(" Hit Tree not found for  %s", AliQA::GetDetName(iDet))) ; 
202                 break ; 
203               } 
204             } 
205             qadm->Exec(taskIndex, data) ;
206                                                 break ;
207                                                 case AliQA::kSDIGITS :
208             if( loader ) {      
209               loader->LoadSDigits() ; 
210               data = loader->TreeS() ; 
211               if ( ! data ) {
212                 AliWarning(Form(" SDigit Tree not found for  %s", AliQA::GetDetName(iDet))) ; 
213                 break ; 
214               } 
215             }
216             qadm->Exec(taskIndex, data) ; 
217                                                 break; 
218                                                 case AliQA::kDIGITS :
219             if( loader ) {      
220               loader->LoadDigits() ; 
221               data = loader->TreeD() ; 
222               if ( ! data ) {
223                 AliWarning(Form(" Digit Tree not found for  %s", AliQA::GetDetName(iDet))) ; 
224                 break ; 
225               } 
226             }
227             qadm->Exec(taskIndex, data) ;
228                                                 break; 
229                                                 case AliQA::kRECPOINTS :
230             if( loader ) {      
231               loader->LoadRecPoints() ; 
232               data = loader->TreeR() ; 
233               if (!data) {
234                 AliWarning(Form("RecPoints not found for %s", AliQA::GetDetName(iDet))) ; 
235                 break ; 
236               } 
237             }
238             qadm->Exec(taskIndex, data) ; 
239             break; 
240                                                 case AliQA::kTRACKSEGMENTS :
241                                                 break; 
242                                                 case AliQA::kRECPARTICLES :
243                                                 break; 
244                                                 case AliQA::kESDS :
245                                                 qadm->Exec(taskIndex, fESD) ;
246                                                 break; 
247                                                 case AliQA::kNTASKINDEX :
248                                                 break; 
249                                 } //task switch
250                         }
251                 } // detector loop
252     Increment() ; 
253         } // event loop 
254         // Save QA data for all detectors
255         rv = Finish(taskIndex) ;
256         
257         if ( taskIndex == AliQA::kRAWS ) 
258                 fRawReader->RewindEvents() ;
259
260         return rv ; 
261 }
262
263 //_____________________________________________________________________________
264 Bool_t AliQADataMakerSteer::Finish(const AliQA::TASKINDEX_t taskIndex) 
265 {
266         // write output to file for all detectors
267         for (UInt_t iDet = 0; iDet < fgkNDetectors ; iDet++) {
268                 if (IsSelected(AliQA::GetDetName(iDet))) {
269                         AliQADataMaker * qadm = GetQADataMaker(iDet) ;
270                         if (qadm) 
271         qadm->EndOfCycle(taskIndex) ;
272                 }
273         }
274         return kTRUE ; 
275 }
276
277 //_____________________________________________________________________________
278 TObjArray * AliQADataMakerSteer::GetFromOCDB(AliQA::DETECTORINDEX_t det, AliQA::TASKINDEX_t task, const char * year) const 
279 {
280         // Retrieve the list of QA data for a given detector and a given task 
281         TObjArray * rv = NULL ;
282         if ( !strlen(AliQA::GetQARefStorage()) ) { 
283                 AliError("No storage defined, use AliQA::SetQARefStorage") ; 
284                 return NULL ; 
285         }       
286         AliCDBManager* man = AliCDBManager::Instance() ; 
287         if ( ! man->IsDefaultStorageSet() ) {
288                 TString tmp(AliQA::GetQARefDefaultStorage()) ; 
289                 tmp.Append(year) ; 
290                 tmp.Append("/") ; 
291                 man->SetDefaultStorage(tmp.Data()) ;            
292                 man->SetSpecificStorage(Form("%s/*", AliQA::GetQAName()), AliQA::GetQARefStorage()) ;
293         }
294         TString detOCDBDir(Form("%s/%s/%s", AliQA::GetQAName(), AliQA::GetDetName((Int_t)det), AliQA::GetRefOCDBDirName())) ; 
295         AliInfo(Form("Retrieving reference data from %s/%s for %s", AliQA::GetQARefStorage(), detOCDBDir.Data(), AliQA::GetTaskName(task).Data())) ; 
296         AliCDBEntry* entry = man->Get(detOCDBDir.Data(), 0) ; //FIXME 0 --> Run Number
297         TList * listDetQAD = dynamic_cast<TList *>(entry->GetObject()) ;
298         if ( listDetQAD ) 
299                 rv = dynamic_cast<TObjArray *>(listDetQAD->FindObject(AliQA::GetTaskName(task))) ; 
300         return rv ; 
301 }
302
303 //_____________________________________________________________________________
304 AliLoader * AliQADataMakerSteer::GetLoader(Int_t iDet)
305 {
306         // get the loader for a detector
307
308         if ( !fRunLoader || iDet == AliQA::kCORR) 
309                 return NULL ; 
310         
311         TString detName = AliQA::GetDetName(iDet) ;
312     fLoader[iDet] = fRunLoader->GetLoader(detName + "Loader");
313         if (fLoader[iDet]) 
314                 return fLoader[iDet] ;
315         
316         // load the QA data maker object
317         TPluginManager* pluginManager = gROOT->GetPluginManager() ;
318         TString loaderName = "Ali" + detName + "Loader" ;
319
320         AliLoader * loader = NULL ;
321         // first check if a plugin is defined for the quality assurance data maker
322         TPluginHandler* pluginHandler = pluginManager->FindHandler("AliLoader", detName) ;
323         // if not, add a plugin for it
324         if (!pluginHandler) {
325                 AliDebug(1, Form("defining plugin for %s", loaderName.Data())) ;
326                 TString libs = gSystem->GetLibraries() ;
327                 if (libs.Contains("lib" + detName + "base.so") || (gSystem->Load("lib" + detName + "base.so") >= 0)) {
328                         pluginManager->AddHandler("AliQADataMaker", detName, loaderName, detName + "loader", loaderName + "()") ;
329                 } else {
330                         pluginManager->AddHandler("AliLoader", detName, loaderName, detName, loaderName + "()") ;
331                 }
332                 pluginHandler = pluginManager->FindHandler("AliLoader", detName) ;
333         }
334         if (pluginHandler && (pluginHandler->LoadPlugin() == 0)) {
335                 loader = (AliLoader *) pluginHandler->ExecPlugin(0) ;
336         }
337         if (loader) 
338                 fLoader[iDet] = loader ;
339         return loader ;
340 }
341
342 //_____________________________________________________________________________
343 AliQADataMaker * AliQADataMakerSteer::GetQADataMaker(const Int_t iDet) 
344 {
345         // get the quality assurance data maker for a detector
346         
347         if (fQADataMaker[iDet]) {
348     fQADataMaker[iDet]->SetEventSpecie(fEventSpecie) ; 
349                 return fQADataMaker[iDet] ;
350   }
351         
352         AliQADataMaker * qadm = NULL ;
353         
354         if (iDet == AliQA::kGLOBAL) { //Global QA
355                 qadm = new AliGlobalQADataMaker();
356                 qadm->SetName(AliQA::GetDetName(iDet));
357                 qadm->SetUniqueID(iDet);
358                 fQADataMaker[iDet] = qadm;
359     qadm->SetEventSpecie(fEventSpecie) ; 
360                 return qadm;
361         }
362
363         if (iDet == AliQA::kCORR) { //the data maker for correlations among detectors
364     qadm = new AliCorrQADataMakerRec(fQADataMaker) ; 
365                 qadm->SetName(AliQA::GetDetName(iDet));
366                 qadm->SetUniqueID(iDet);
367                 fQADataMaker[iDet] = qadm;
368     qadm->SetEventSpecie(fEventSpecie) ; 
369                 return qadm;
370   }
371
372         // load the QA data maker object
373         TPluginManager* pluginManager = gROOT->GetPluginManager() ;
374         TString detName = AliQA::GetDetName(iDet) ;
375         TString tmp(fMode) ; 
376         if (tmp.Contains("sim")) 
377                 tmp.ReplaceAll("s", "S") ; 
378         else if (tmp.Contains("rec")) 
379                 tmp.ReplaceAll("r", "R") ; 
380
381         TString qadmName = "Ali" + detName + "QADataMaker" + tmp ;
382
383         // first check if a plugin is defined for the quality assurance data maker
384         TPluginHandler* pluginHandler = pluginManager->FindHandler("AliQADataMaker", detName) ;
385         // if not, add a plugin for it
386         if (!pluginHandler) {
387                 AliDebug(1, Form("defining plugin for %s", qadmName.Data())) ;
388                 TString libs = gSystem->GetLibraries() ;
389                 if (libs.Contains("lib" + detName + fMode + ".so") || (gSystem->Load("lib" + detName + fMode + ".so") >= 0)) {
390                         pluginManager->AddHandler("AliQADataMaker", detName, qadmName, detName + "qadm", qadmName + "()") ;
391                 } else {
392                         pluginManager->AddHandler("AliQADataMaker", detName, qadmName, detName, qadmName + "()") ;
393                 }
394                 pluginHandler = pluginManager->FindHandler("AliQADataMaker", detName) ;
395         }
396         if (pluginHandler && (pluginHandler->LoadPlugin() == 0)) {
397                 qadm = (AliQADataMaker *) pluginHandler->ExecPlugin(0) ;
398         }
399         if (qadm) {
400                 qadm->SetName(AliQA::GetDetName(iDet));
401                 qadm->SetUniqueID(iDet);
402                 fQADataMaker[iDet] = qadm ;
403     qadm->SetEventSpecie(fEventSpecie) ; 
404         }
405
406   return qadm ;
407 }
408
409 //_____________________________________________________________________________
410 void  AliQADataMakerSteer::EndOfCycle(TObjArray * detArray) 
411 {
412         // End of cycle QADataMakers 
413         
414         for (UInt_t iDet = 0; iDet < fgkNDetectors ; iDet++) {
415                 if (IsSelected(AliQA::GetDetName(iDet))) {
416                         AliQADataMaker * qadm = GetQADataMaker(iDet) ;
417                         if (!qadm) 
418                                 continue ;      
419                         // skip non active detectors
420                         if (detArray) {
421                                 AliModule* det = static_cast<AliModule*>(detArray->FindObject(AliQA::GetDetName(iDet))) ;
422                                 if (!det || !det->IsActive())  
423                                         continue ;
424                         }
425                         for (UInt_t taskIndex = 0; taskIndex < AliQA::kNTASKINDEX; taskIndex++) {
426                                 if ( fTasks.Contains(Form("%d", taskIndex)) ) 
427                                         qadm->EndOfCycle(AliQA::GetTaskIndex(AliQA::GetTaskName(taskIndex))) ;
428                         }
429                         qadm->Finish();
430                 }
431         }
432 }
433
434 //_____________________________________________________________________________
435 void  AliQADataMakerSteer::EndOfCycle(TString detectors) 
436 {
437         // End of cycle QADataMakers 
438         
439         for (UInt_t iDet = 0; iDet < fgkNDetectors ; iDet++) {
440                 if (IsSelected(AliQA::GetDetName(iDet))) {
441                         AliQADataMaker * qadm = GetQADataMaker(iDet) ;
442                         if (!qadm) 
443                                 continue ;      
444                         // skip non active detectors
445       if (!detectors.Contains(AliQA::GetDetName(iDet))) 
446         continue ;
447                 for (UInt_t taskIndex = 0; taskIndex < AliQA::kNTASKINDEX; taskIndex++) {
448                                 if ( fTasks.Contains(Form("%d", taskIndex)) ) 
449                                         qadm->EndOfCycle(AliQA::GetTaskIndex(AliQA::GetTaskName(taskIndex))) ;
450                         }
451                         qadm->Finish();
452                 }
453         }
454 }
455
456 //_____________________________________________________________________________
457 void AliQADataMakerSteer::Increment()
458 {
459   // Increments the cycle counter for all QA Data Makers
460         for (UInt_t iDet = 0; iDet < fgkNDetectors ; iDet++) {
461                 if (IsSelected(AliQA::GetDetName(iDet))) {
462                         AliQADataMaker * qadm = GetQADataMaker(iDet) ;
463                         if (qadm) 
464         qadm->Increment() ;
465     }
466   }
467 }
468   
469 //_____________________________________________________________________________
470 Bool_t AliQADataMakerSteer::Init(const AliQA::TASKINDEX_t taskIndex, const  char * input )
471 {
472         // Initialize the event source and QA data makers
473         
474         //fTasks = AliQA::GetTaskName(taskIndex) ; 
475
476         if (taskIndex == AliQA::kRAWS) { 
477                 if (!fRawReader) {
478                         fRawReader = AliRawReader::Create(input);
479                 }
480                 if ( ! fRawReader ) 
481                         return kFALSE ; 
482                 fRawReaderDelete = kTRUE ; 
483                 fRawReader->NextEvent() ; 
484                 fRunNumber = fRawReader->GetRunNumber() ; 
485                 AliCDBManager::Instance()->SetRun(fRunNumber) ; 
486                 fRawReader->RewindEvents();
487                 fNumberOfEvents = 999999 ;
488                 if ( fMaxEvents < 0 ) 
489                         fMaxEvents = fNumberOfEvents ; 
490                 } else if (taskIndex == AliQA::kESDS) {
491                         fTasks = AliQA::GetTaskName(AliQA::kESDS) ; 
492       if (!gSystem->AccessPathName("AliESDs.root")) { // AliESDs.root exists
493         TFile * esdFile = TFile::Open("AliESDs.root") ;
494         fESDTree = dynamic_cast<TTree *> (esdFile->Get("esdTree")) ; 
495         if ( !fESDTree ) {
496           AliError("esdTree not found") ; 
497           return kFALSE ; 
498         } else {
499           fESD     = new AliESDEvent() ;
500           fESD->ReadFromTree(fESDTree) ;
501           fESDTree->GetEntry(0) ; 
502           fRunNumber = fESD->GetRunNumber() ; 
503           fNumberOfEvents = fESDTree->GetEntries() ;
504           if ( fMaxEvents < 0 ) 
505             fMaxEvents = fNumberOfEvents ; 
506         }
507       } else {
508         AliError("AliESDs.root not found") ; 
509         return kFALSE ; 
510       }                 
511     } else {
512       if ( !InitRunLoader() ) { 
513         AliWarning("No Run Loader not found") ; 
514       } else {
515         fNumberOfEvents = fRunLoader->GetNumberOfEvents() ;
516         if ( fMaxEvents < 0 ) 
517           fMaxEvents = fNumberOfEvents ; 
518       }
519     }
520
521   // Get Detectors 
522   TObjArray* detArray = NULL ; 
523         if (fRunLoader) // check if RunLoader exists 
524                 if ( fRunLoader->GetAliRun() ) { // check if AliRun exists in gAlice.root
525                         detArray = fRunLoader->GetAliRun()->Detectors() ;
526                         fRunNumber = fRunLoader->GetHeader()->GetRun() ; 
527                 }
528
529         // Initialize all QA data makers for all detectors
530         fRunNumber = AliCDBManager::Instance()->GetRun() ; 
531         if ( !  AliGeomManager::GetGeometry() ) 
532                 AliGeomManager::LoadGeometry() ; 
533         
534         InitQADataMaker(fRunNumber, detArray) ; //, fCycleSame, kTRUE, detArray) ; 
535         return kTRUE ; 
536 }
537
538 //_____________________________________________________________________________
539 void  AliQADataMakerSteer::InitQADataMaker(UInt_t run, TObjArray * detArray) 
540 {
541         // Initializes The QADataMaker for all active detectors and for all active tasks 
542         for (UInt_t iDet = 0; iDet < fgkNDetectors ; iDet++) {
543                 if (IsSelected(AliQA::GetDetName(iDet))) {
544                         AliQADataMaker * qadm = GetQADataMaker(iDet) ;
545                         if (!qadm) {
546                                 AliError(Form("AliQADataMaker not found for %s", AliQA::GetDetName(iDet))) ; 
547                                 fDetectorsW.ReplaceAll(AliQA::GetDetName(iDet), "") ; 
548                         } else {
549         if (fQAWriteExpert[iDet])
550           qadm->SetWriteExpert() ; 
551                                 AliDebug(1, Form("Data Maker found for %s", qadm->GetName())) ; 
552                                 // skip non active detectors
553                                 if (detArray) {
554                                         AliModule* det = static_cast<AliModule*>(detArray->FindObject(AliQA::GetDetName(iDet))) ;
555                                         if (!det || !det->IsActive())  
556                                                 continue ;
557                                 }
558                                 if (fQAWriteExpert[iDet]) qadm->SetWriteExpert() ; 
559               // Set default reco params
560         Bool_t sameCycle = kFALSE ; 
561                                 for (UInt_t taskIndex = 0; taskIndex < AliQA::kNTASKINDEX; taskIndex++) {
562                                         if ( fTasks.Contains(Form("%d", taskIndex)) ) {
563                                                 qadm->Init(AliQA::GetTaskIndex(AliQA::GetTaskName(taskIndex)), GetQACycles(qadm->GetUniqueID())) ;
564             qadm->StartOfCycle(AliQA::GetTaskIndex(AliQA::GetTaskName(taskIndex)), run,  sameCycle) ;
565             sameCycle = kTRUE ;
566                                         }
567                                 }
568                         }
569                 }
570         }
571 }
572
573
574 //_____________________________________________________________________________
575 Bool_t AliQADataMakerSteer::InitRunLoader()
576 {
577         // get or create the run loader
578         if (fRunLoader) {
579                 fCycleSame = kTRUE ; 
580         } else {
581                 if (!gSystem->AccessPathName(fGAliceFileName.Data())) { // galice.root exists
582                         // load all base libraries to get the loader classes
583                         TString libs = gSystem->GetLibraries() ;
584                         for (UInt_t iDet = 0; iDet < fgkNDetectors; iDet++) {
585                                 if (!IsSelected(AliQA::GetDetName(iDet))) 
586                                         continue ; 
587                                 TString detName = AliQA::GetDetName(iDet) ;
588                                 if (detName == "HLT") 
589                                         continue;
590                                 if (libs.Contains("lib" + detName + "base.so")) 
591                                         continue;
592                                 gSystem->Load("lib" + detName + "base.so");
593                         }
594                         fRunLoader = AliRunLoader::Open(fGAliceFileName.Data());
595                         if (!fRunLoader) {
596                                 AliError(Form("no run loader found in file %s", fGAliceFileName.Data()));
597                                 return kFALSE;
598                         }
599                         fRunLoader->CdGAFile();
600                         if (fRunLoader->LoadgAlice() == 0) {
601                                 gAlice = fRunLoader->GetAliRun();
602                         }
603
604                         if (!gAlice) {
605                                 AliError(Form("no gAlice object found in file %s", fGAliceFileName.Data()));
606                                 return kFALSE;
607                         }
608
609                 } else {               // galice.root does not exist
610                         AliError(Form("the file %s does not exist", fGAliceFileName.Data()));
611                         return kFALSE;
612                 }
613         }
614
615         if (!fRunNumber) { 
616                 fRunLoader->LoadHeader();
617                 fRunNumber = fRunLoader->GetHeader()->GetRun() ; 
618         }
619         return kTRUE;
620 }
621
622 //_____________________________________________________________________________
623 Bool_t AliQADataMakerSteer::IsSelected(const char * det) 
624 {
625         // check whether detName is contained in detectors
626         // if yes, it is removed from detectors
627         
628         Bool_t rv = kFALSE;
629         const TString detName(det) ;
630   // always activates Correlation
631   if ( detName.Contains(AliQA::GetDetName(AliQA::kCORR))) {
632     rv = kTRUE ; 
633   } else {
634     // check if all detectors are selected
635     if (fDetectors.Contains("ALL")) {
636       fDetectors = "ALL";
637       rv = kTRUE;
638     } else if ((fDetectors.CompareTo(detName) == 0) ||
639                fDetectors.BeginsWith(detName+" ") ||
640                fDetectors.EndsWith(" "+detName) ||
641                fDetectors.Contains(" "+detName+" ")) {
642       rv = kTRUE;
643     }
644   }
645         return rv ;
646 }
647
648 //_____________________________________________________________________________
649 Bool_t AliQADataMakerSteer::Merge(const Int_t runNumber) const
650 {
651         // Merge data from all the cycles from all detectors in one single file per run
652         // Merge the QA results from all the data chunks in one run 
653  Bool_t rv = MergeData(runNumber) ; 
654  rv *= MergeResults(runNumber) ;
655  return rv ; 
656 }
657         
658         
659 //_____________________________________________________________________________
660 Bool_t AliQADataMakerSteer::MergeData(const Int_t runNumber) const
661 {
662         // Merge all the cycles from all detectors in one single file per run
663         TString cmd ;
664         if (runNumber == -1) 
665                 cmd = Form(".! ls *%s*.%d.root > tempo.txt", AliQA::GetQADataFileName(), runNumber) ; 
666         else 
667                 cmd = Form(".! ls *%s*.*.root > tempo.txt", AliQA::GetQADataFileName()) ; 
668         gROOT->ProcessLine(cmd.Data()) ;
669         ifstream in("tempo.txt") ; 
670         const Int_t runMax = 10 ;  
671         TString file[AliQA::kNDET*runMax] ;
672         
673         Int_t index = 0 ; 
674         while ( 1 ) {
675                 in >> file[index] ; 
676                 if ( !in.good() ) 
677                         break ; 
678                 AliInfo(Form("index = %d file = %s", index, (file[index]).Data())) ; 
679                 index++ ;
680         }
681         
682         if ( index == 0 ) { 
683                 AliError(Form("run number %d not found", runNumber)) ; 
684                 return kFALSE ; 
685         }
686
687   TFileMerger merger ; 
688   TString outFileName(Form("Merged.%s.Data.%d.root",AliQA::GetQADataFileName(),runNumber)); 
689   merger.OutputFile(outFileName.Data()) ; 
690   for (Int_t ifile = 0 ; ifile < index-1 ; ifile++) {
691     TString pattern(Form("%s.%d.", AliQA::GetQADataFileName(), runNumber)); 
692     TString tmp(file[ifile]) ; 
693     if (tmp.Contains(pattern)) {
694       merger.AddFile(tmp) ; 
695     }
696         }
697   merger.Merge() ; 
698         return kTRUE ; 
699 }
700
701 //_____________________________________________________________________________
702 Bool_t AliQADataMakerSteer::MergeResults(const Int_t runNumber) const
703 {
704         // Merge the QA result from all the data chunks in a run 
705         TString cmd ;
706         cmd = Form(".! ls %s*.root > tempo.txt", AliQA::GetQADataFileName()) ; 
707         gROOT->ProcessLine(cmd.Data()) ;
708         ifstream in("tempo.txt") ; 
709         const Int_t chunkMax = 100 ;  
710         TString fileList[chunkMax] ;
711         
712         Int_t index = 0 ; 
713         while ( 1 ) {
714                 TString file ; 
715                 in >> fileList[index] ; 
716                 if ( !in.good() ) 
717                         break ; 
718                 AliInfo(Form("index = %d file = %s", index, (fileList[index].Data()))) ; 
719                 index++ ;
720         }
721         
722         if ( index == 0 ) { 
723                 AliError("No QA Result File found") ; 
724                 return kFALSE ; 
725         }
726         
727         TFileMerger merger ; 
728         TString outFileName(Form("Merged.%s.Result.%d.root", AliQA::GetQADataFileName(), runNumber));           
729         merger.OutputFile(outFileName.Data()) ; 
730         for (Int_t ifile = 0 ; ifile < index ; ifile++) {
731                 TString file = fileList[ifile] ; 
732                 merger.AddFile(file) ; 
733         }
734         merger.Merge() ; 
735         
736         return kTRUE ; 
737 }
738
739 //_____________________________________________________________________________
740 void AliQADataMakerSteer::Reset(const Bool_t sameCycle)
741 {
742         // Reset the default data members
743
744         for (UInt_t iDet = 0; iDet < fgkNDetectors ; iDet++) {
745                 if (IsSelected(AliQA::GetDetName(iDet))) {
746                         AliQADataMaker * qadm = GetQADataMaker(iDet);
747                         qadm->Reset();
748                 }
749         } 
750         if (fRawReaderDelete) { 
751                 delete fRawReader ;
752                 fRawReader      = NULL ;
753         }
754
755         fCycleSame      = sameCycle ; 
756         fESD            = NULL ; 
757         fESDTree        = NULL ; 
758         //fFirst          = kTRUE ;   
759         fNumberOfEvents = 999999 ;  
760 }
761
762 //_____________________________________________________________________________
763 TString AliQADataMakerSteer::Run(const char * detectors, AliRawReader * rawReader, const Bool_t sameCycle) 
764 {
765         //Runs all the QA data Maker for Raws only
766         
767         fCycleSame       = sameCycle ;
768         fRawReader       = rawReader ;
769         fDetectors       = detectors ; 
770         fDetectorsW      = detectors ;  
771         
772         AliCDBManager* man = AliCDBManager::Instance() ; 
773
774         if ( man->GetRun() == -1 ) {// check if run number not set previously and set it from raw data
775                 rawReader->NextEvent() ; 
776                 man->SetRun(fRawReader->GetRunNumber()) ;
777                 rawReader->RewindEvents() ;
778         }       
779         
780         if (!fCycleSame) 
781     if ( !Init(AliQA::kRAWS) ) 
782       return kFALSE ; 
783   fRawReaderDelete = kFALSE ; 
784
785         DoIt(AliQA::kRAWS) ; 
786         return  fDetectorsW ;
787 }
788
789 //_____________________________________________________________________________
790 TString AliQADataMakerSteer::Run(const char * detectors, const char * fileName, const Bool_t sameCycle) 
791 {
792         //Runs all the QA data Maker for Raws only
793
794         fCycleSame       = sameCycle ;
795         fDetectors       = detectors ; 
796         fDetectorsW      = detectors ;  
797         
798         AliCDBManager* man = AliCDBManager::Instance() ; 
799         if ( man->GetRun() == -1 ) { // check if run number not set previously and set it from AliRun
800                 AliRunLoader * rl = AliRunLoader::Open("galice.root") ;
801                 if ( ! rl ) {
802                         AliFatal("galice.root file not found in current directory") ; 
803                 } else {
804                         rl->CdGAFile() ; 
805                         rl->LoadgAlice() ;
806                         if ( ! rl->GetAliRun() ) {
807                                 AliFatal("AliRun not found in galice.root") ;
808                         } else {
809                                 rl->LoadHeader() ;
810                                 man->SetRun(rl->GetHeader()->GetRun());
811                         }
812                 }
813         }
814         
815         if (!fCycleSame) 
816     if ( !Init(AliQA::kRAWS, fileName) ) 
817       return kFALSE ; 
818         
819         DoIt(AliQA::kRAWS) ; 
820         return  fDetectorsW ;
821 }
822
823 //_____________________________________________________________________________
824 TString AliQADataMakerSteer::Run(const char * detectors, const AliQA::TASKINDEX_t taskIndex, Bool_t const sameCycle, const  char * fileName ) 
825 {
826         // Runs all the QA data Maker for every detector
827         
828         fCycleSame       = sameCycle ;
829         fDetectors       = detectors ; 
830         fDetectorsW      = detectors ;          
831         
832         AliCDBManager* man = AliCDBManager::Instance() ;        
833         if ( man->GetRun() == -1 ) { // check if run number not set previously and set it from AliRun
834                 AliRunLoader * rl = AliRunLoader::Open("galice.root") ;
835                 if ( ! rl ) {
836                         AliFatal("galice.root file not found in current directory") ; 
837                 } else {
838                         rl->CdGAFile() ; 
839                         rl->LoadgAlice() ;
840                         if ( ! rl->GetAliRun() ) {
841                                 AliInfo("AliRun not found in galice.root") ;
842                         } else {
843                                 rl->LoadHeader() ;
844                                 man->SetRun(rl->GetHeader()->GetRun()) ;
845                         }
846                 }
847         }
848         
849
850         if ( taskIndex == AliQA::kNULLTASKINDEX) { 
851                 for (UInt_t task = 0; task < AliQA::kNTASKINDEX; task++) {
852                         if ( fTasks.Contains(Form("%d", task)) ) {
853         if (!fCycleSame)
854           if ( !Init(AliQA::GetTaskIndex(AliQA::GetTaskName(task)), fileName) ) 
855             return kFALSE ;
856         DoIt(AliQA::GetTaskIndex(AliQA::GetTaskName(task))) ;
857                         }
858                 }
859         } else {
860     if (! fCycleSame )
861       if ( !Init(taskIndex, fileName) ) 
862         return kFALSE ; 
863       DoIt(taskIndex) ; 
864   }             
865         
866         return fDetectorsW ;
867
868 }
869
870 //_____________________________________________________________________________
871 void AliQADataMakerSteer::RunOneEvent(AliRawReader * rawReader) 
872 {
873         //Runs all the QA data Maker for Raws only and on one event only (event loop done by calling method)
874   if ( ! rawReader ) 
875     return ; 
876         AliCodeTimerAuto("") ;
877   if (fTasks.Contains(Form("%d", AliQA::kRAWS))){
878     for (UInt_t iDet = 0; iDet < fgkNDetectors; iDet++) {
879       if (!IsSelected(AliQA::GetDetName(iDet))) 
880         continue;
881       AliQADataMaker *qadm = GetQADataMaker(iDet);  
882       if (!qadm) 
883         continue;
884       if ( qadm->IsCycleDone() ) {
885         qadm->EndOfCycle() ;
886       }
887       AliCodeTimerStart(Form("running RAW quality assurance data maker for %s", AliQA::GetDetName(iDet))); 
888       qadm->SetEventSpecie(fEventSpecie) ; 
889                         qadm->Exec(AliQA::kRAWS, rawReader) ;
890       AliCodeTimerStop(Form("running RAW quality assurance data maker for %s", AliQA::GetDetName(iDet)));
891                 }
892   }
893 }
894
895 //_____________________________________________________________________________
896 void AliQADataMakerSteer::RunOneEvent(AliESDEvent *& esd) 
897 {
898         //Runs all the QA data Maker for ESDs only and on one event only (event loop done by calling method)
899         
900   AliCodeTimerAuto("") ;
901   if (fTasks.Contains(Form("%d", AliQA::kESDS))) {
902     for (UInt_t iDet = 0; iDet < fgkNDetectors; iDet++) {
903       if (!IsSelected(AliQA::GetDetName(iDet))) 
904         continue;
905       AliQADataMaker *qadm = GetQADataMaker(iDet);  
906       if (!qadm) 
907         continue;
908       if ( qadm->IsCycleDone() ) {
909         qadm->EndOfCycle() ;
910       }
911       AliCodeTimerStart(Form("running ESD quality assurance data maker for %s", AliQA::GetDetName(iDet)));
912                         qadm->Exec(AliQA::kESDS, esd) ;
913       AliCodeTimerStop(Form("running ESD quality assurance data maker for %s", AliQA::GetDetName(iDet)));
914                 }
915         }
916 }
917
918 //_____________________________________________________________________________
919 void AliQADataMakerSteer::RunOneEventInOneDetector(Int_t det, TTree * tree) 
920 {
921         // Runs all the QA data Maker for ESDs only and on one event only (event loop done by calling method)
922         AliCodeTimerAuto("") ;
923   if (fTasks.Contains(Form("%d", AliQA::kRECPOINTS))) {
924     if (IsSelected(AliQA::GetDetName(det))) {
925       AliQADataMaker *qadm = GetQADataMaker(det);  
926       if (qadm) { 
927         if ( qadm->IsCycleDone() ) {
928           qadm->EndOfCycle() ;
929         }
930         AliCodeTimerStart(Form("running RecPoints quality assurance data maker for %s", AliQA::GetDetName(det)));
931         qadm->Exec(AliQA::kRECPOINTS, tree) ;
932         AliCodeTimerStop(Form("running RecPoints quality assurance data maker for %s", AliQA::GetDetName(det)));
933       }
934     }
935   }
936 }
937
938 //_____________________________________________________________________________
939 Bool_t AliQADataMakerSteer::Save2OCDB(const Int_t runNumber, AliRecoParam::EventSpecie_t es, const char * year, const char * detectors) const
940 {
941         // take the locasl QA data merge into a single file and save in OCDB 
942         Bool_t rv = kTRUE ; 
943         TString tmp(AliQA::GetQARefStorage()) ; 
944         if ( tmp.IsNull() ) { 
945                 AliError("No storage defined, use AliQA::SetQARefStorage") ; 
946                 return kFALSE ; 
947         }
948         if ( !(tmp.Contains(AliQA::GetLabLocalOCDB()) || tmp.Contains(AliQA::GetLabAliEnOCDB())) ) {
949                 AliError(Form("%s is a wrong storage, use %s or %s", AliQA::GetQARefStorage(), AliQA::GetLabLocalOCDB().Data(), AliQA::GetLabAliEnOCDB().Data())) ; 
950                 return kFALSE ; 
951         }
952         TString sdet(detectors) ; 
953         sdet.ToUpper() ;
954         TFile * inputFile ; 
955         if ( sdet.Contains("ALL") ) {
956                 rv = Merge(runNumber) ; 
957                 if ( ! rv )
958                         return kFALSE ; 
959                 TString inputFileName(Form("Merged.%s.Data.%d.root", AliQA::GetQADataFileName(), runNumber)) ; 
960                 inputFile = TFile::Open(inputFileName.Data()) ; 
961                 rv = SaveIt2OCDB(runNumber, inputFile, year, es) ; 
962         } else {
963                 for (Int_t index = 0; index < AliQA::kNDET; index++) {
964                         if (sdet.Contains(AliQA::GetDetName(index))) {
965                                 TString inputFileName(Form("%s.%s.%d.root", AliQA::GetDetName(index), AliQA::GetQADataFileName(), runNumber)) ; 
966                                 inputFile = TFile::Open(inputFileName.Data()) ;                         
967                                 rv *= SaveIt2OCDB(runNumber, inputFile, year, es) ; 
968                         }
969                 }
970         }
971         return rv ; 
972 }
973
974 //_____________________________________________________________________________
975 Bool_t AliQADataMakerSteer::SaveIt2OCDB(const Int_t runNumber, TFile * inputFile, const char * year, AliRecoParam::EventSpecie_t es) const
976 {
977         // reads the TH1 from file and adds it to appropriate list before saving to OCDB
978         Bool_t rv = kTRUE ;
979         AliInfo(Form("Saving TH1s in %s to %s", inputFile->GetName(), AliQA::GetQARefStorage())) ; 
980         AliCDBManager* man = AliCDBManager::Instance() ; 
981         if ( ! man->IsDefaultStorageSet() ) {
982                 TString tmp( AliQA::GetQARefStorage() ) ; 
983                 if ( tmp.Contains(AliQA::GetLabLocalOCDB()) ) 
984                         man->SetDefaultStorage(AliQA::GetQARefStorage()) ;
985                 else {
986                         TString tmp1(AliQA::GetQARefDefaultStorage()) ; 
987                         tmp1.Append(year) ; 
988                         tmp1.Append("?user=alidaq") ; 
989                         man->SetDefaultStorage(tmp1.Data()) ; 
990                 }
991         }
992         man->SetSpecificStorage("*", AliQA::GetQARefStorage()) ; 
993         if(man->GetRun() < 0) 
994                 man->SetRun(runNumber);
995
996         AliCDBMetaData mdr ;
997         mdr.SetResponsible("yves schutz");
998
999         for ( Int_t detIndex = 0 ; detIndex < AliQA::kNDET ; detIndex++) {
1000                 TDirectory * detDir = inputFile->GetDirectory(AliQA::GetDetName(detIndex)) ; 
1001                 if ( detDir ) {
1002                         AliInfo(Form("Entering %s", detDir->GetName())) ;
1003       AliQA::SetQARefDataDirName(es) ;
1004                         TString detOCDBDir(Form("%s/%s/%s", AliQA::GetDetName(detIndex), AliQA::GetRefOCDBDirName(), AliQA::GetRefDataDirName())) ; 
1005                         AliCDBId idr(detOCDBDir.Data(), runNumber, AliCDBRunRange::Infinity())  ;
1006                         TList * listDetQAD = new TList() ;
1007                         TString listName(Form("%s QA data Reference", AliQA::GetDetName(detIndex))) ; 
1008                         mdr.SetComment(Form("%s QA stuff", AliQA::GetDetName(detIndex)));
1009                         listDetQAD->SetName(listName) ; 
1010                         TList * taskList = detDir->GetListOfKeys() ; 
1011                         TIter nextTask(taskList) ; 
1012                         TKey * taskKey ; 
1013                         while ( (taskKey = dynamic_cast<TKey*>(nextTask())) ) {
1014                                 TDirectory * taskDir = detDir->GetDirectory(taskKey->GetName()) ; 
1015         TDirectory * esDir   = taskDir->GetDirectory(AliRecoParam::GetEventSpecieName(es)) ; 
1016                                 AliInfo(Form("Saving %s", esDir->GetName())) ; 
1017                                 TObjArray * listTaskQAD = new TObjArray(100) ; 
1018                                 listTaskQAD->SetName(Form("%s/%s", taskKey->GetName(), AliRecoParam::GetEventSpecieName(es))) ;
1019                                 listDetQAD->Add(listTaskQAD) ; 
1020                                 TList * histList = esDir->GetListOfKeys() ; 
1021                                 TIter nextHist(histList) ; 
1022                                 TKey * histKey ; 
1023                                 while ( (histKey = dynamic_cast<TKey*>(nextHist())) ) {
1024                                         TObject * odata = esDir->Get(histKey->GetName()) ; 
1025                                         if ( !odata ) {
1026                                                 AliError(Form("%s in %s/%s returns a NULL pointer !!", histKey->GetName(), detDir->GetName(), taskDir->GetName())) ;
1027                                         } else {
1028             if ( AliQA::GetExpert() == histKey->GetName() ) {
1029               TDirectory * expertDir   = esDir->GetDirectory(histKey->GetName()) ; 
1030               TList * expertHistList = expertDir->GetListOfKeys() ; 
1031               TIter nextExpertHist(expertHistList) ; 
1032               TKey * expertHistKey ; 
1033               while ( (expertHistKey = dynamic_cast<TKey*>(nextExpertHist())) ) {
1034                 TObject * expertOdata = expertDir->Get(expertHistKey->GetName()) ; 
1035                 if ( !expertOdata ) {
1036                   AliError(Form("%s in %s/%s/Expert returns a NULL pointer !!", expertHistKey->GetName(), detDir->GetName(), taskDir->GetName())) ;
1037                 } else {
1038                   AliInfo(Form("Adding %s", expertHistKey->GetName())) ;
1039                   if ( expertOdata->IsA()->InheritsFrom("TH1") ) {
1040                     AliInfo(Form("Adding %s", expertHistKey->GetName())) ;
1041                     TH1 * hExpertdata = static_cast<TH1*>(expertOdata) ; 
1042                     listTaskQAD->Add(hExpertdata) ; 
1043                   }                  
1044                 }                
1045               }
1046             }
1047                                                 AliInfo(Form("Adding %s", histKey->GetName())) ;
1048                                                 if ( odata->IsA()->InheritsFrom("TH1") ) {
1049                                                         AliInfo(Form("Adding %s", histKey->GetName())) ;
1050                                                         TH1 * hdata = static_cast<TH1*>(odata) ; 
1051                                                         listTaskQAD->Add(hdata) ; 
1052                                                 }
1053                                         }
1054                                 }
1055                         }
1056                         man->Put(listDetQAD, idr, &mdr) ;
1057                 }
1058         }
1059         return rv ; 
1060 }       
1061
1062 //_____________________________________________________________________________
1063 void AliQADataMakerSteer::SetEventSpecie(AliRecoParam::EventSpecie_t es) 
1064 {
1065   // set the current event specie and inform AliQA that this event specie has been encountered
1066   fEventSpecie = es ;
1067   AliQA::Instance()->SetEventSpecie(es) ; 
1068 }
1069
1070 //_____________________________________________________________________________
1071 void AliQADataMakerSteer::SetRecoParam(const Int_t det, const AliDetectorRecoParam *par) 
1072 {
1073   // Set custom reconstruction parameters for a given detector
1074   // Single set of parameters for all the events
1075   GetQADataMaker(det)->SetRecoParam(par) ; 
1076 }
1077
1078