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