]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliQAManager.cxx
Fix for bug #78521: Cmake - recompiling AliRoot when switching to a new ROOT tag
[u/mrichter/AliRoot.git] / STEER / AliQAManager.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: AliQAManager.cxx 30894 2009-02-05 13:46:48Z schutz $ */
17 ///////////////////////////////////////////////////////////////////////////////
18 //                                                                           //
19 // class for running the QA makers                                           //
20 //                                                                           //
21 //   AliQAManager qas;                                                       //
22 //   qas.Run(AliQAv1::kRAWS, rawROOTFileName);                               //
23 //   qas.Run(AliQAv1::kHITS);                                                //
24 //   qas.Run(AliQAv1::kSDIGITS);                                             //
25 //   qas.Run(AliQAv1::kDIGITS);                                              //
26 //   qas.Run(AliQAv1::kRECPOINTS);                                           //
27 //   qas.Run(AliQAv1::kESDS);                                                //
28 //                                                                           //
29 ///////////////////////////////////////////////////////////////////////////////
30
31 #include <TCanvas.h>
32 #include <TKey.h>
33 #include <TFile.h>
34 #include <TFileMerger.h>
35 #include <TGrid.h>
36 #include <TGridCollection.h>
37 #include <TGridResult.h>
38 #include <TPluginManager.h>
39 #include <TROOT.h>
40 #include <TString.h>
41 #include <TSystem.h>
42 #include <TStopwatch.h>
43
44 #include "AliCDBManager.h"
45 #include "AliCDBEntry.h"
46 #include "AliCDBId.h"
47 #include "AliCDBMetaData.h"
48 #include "AliCodeTimer.h"
49 #include "AliCorrQADataMakerRec.h"
50 #include "AliDetectorRecoParam.h"
51 #include "AliESDEvent.h"
52 #include "AliGeomManager.h"
53 #include "AliGlobalQADataMaker.h"
54 #include "AliHeader.h"
55 #include "AliLog.h"
56 #include "AliModule.h"
57 #include "AliQAv1.h"
58 #include "AliQAChecker.h"
59 #include "AliQACheckerBase.h"
60 #include "AliQADataMakerRec.h"
61 #include "AliQADataMakerSim.h"
62 #include "AliQAManager.h" 
63 #include "AliRawReaderDate.h"
64 #include "AliRawReaderFile.h"
65 #include "AliRawReaderRoot.h"
66 #include "AliRun.h"
67 #include "AliRunLoader.h"
68 #include "AliRunTag.h"
69
70 ClassImp(AliQAManager) 
71 AliQAManager* AliQAManager::fgQAInstance = 0x0;
72
73 //_____________________________________________________________________________
74 AliQAManager::AliQAManager() :
75   AliCDBManager(), 
76   fCurrentEvent(0),   
77   fCycleSame(kFALSE),
78   fDetectors("ALL"), 
79   fDetectorsW("ALL"), 
80   fESD(NULL), 
81   fESDTree(NULL),
82   fGAliceFileName(""), 
83   fFirstEvent(0),        
84   fMaxEvents(0),   
85   fMode(""), 
86   fNumberOfEvents(999999), 
87   fRecoParam(),
88   fRunNumber(0), 
89   fRawReader(NULL), 
90   fRawReaderDelete(kTRUE), 
91   fRunLoader(NULL), 
92   fTasks(""),  
93   fEventSpecie(AliRecoParam::kDefault), 
94   fPrintImage(kTRUE), 
95   fSaveData(kTRUE) 
96 {
97         // default ctor
98         fMaxEvents = fNumberOfEvents ; 
99         for (UInt_t iDet = 0; iDet < fgkNDetectors; iDet++) {
100                 if (IsSelected(AliQAv1::GetDetName(iDet))) {
101                         fLoader[iDet]      = NULL ;
102                         fQADataMaker[iDet] = NULL ;
103                         fQACycles[iDet]    = 999999 ;
104                 }
105         }       
106   SetWriteExpert() ; 
107 }
108
109 //_____________________________________________________________________________
110 AliQAManager::AliQAManager(AliQAv1::MODE_t mode, const Char_t* gAliceFilename) :
111   AliCDBManager(), 
112   fCurrentEvent(0),  
113         fCycleSame(kFALSE),
114         fDetectors("ALL"), 
115         fDetectorsW("ALL"), 
116         fESD(NULL), 
117         fESDTree(NULL),
118         fGAliceFileName(gAliceFilename), 
119         fFirstEvent(0),        
120         fMaxEvents(0),   
121   fMode(AliQAv1::GetModeName(mode)), 
122         fNumberOfEvents(999999), 
123   fRecoParam(),
124         fRunNumber(0), 
125         fRawReader(NULL), 
126         fRawReaderDelete(kTRUE), 
127         fRunLoader(NULL), 
128   fTasks(""), 
129   fEventSpecie(AliRecoParam::kDefault), 
130   fPrintImage(kTRUE), 
131   fSaveData(kTRUE) 
132 {
133         // default ctor
134         fMaxEvents = fNumberOfEvents ; 
135         for (UInt_t iDet = 0; iDet < fgkNDetectors; iDet++) {
136                 if (IsSelected(AliQAv1::GetDetName(iDet))) {
137                         fLoader[iDet]      = NULL ;
138                         fQADataMaker[iDet] = NULL ;
139                         fQACycles[iDet]    = 999999 ;
140     }
141   }
142   SetWriteExpert() ; 
143 }
144
145 //_____________________________________________________________________________
146 AliQAManager::AliQAManager(const AliQAManager & qas) : 
147   AliCDBManager(), 
148         fCurrentEvent(qas.fCurrentEvent),  
149         fCycleSame(kFALSE),
150         fDetectors(qas.fDetectors), 
151         fDetectorsW(qas.fDetectorsW), 
152         fESD(NULL), 
153         fESDTree(NULL), 
154         fGAliceFileName(qas.fGAliceFileName), 
155         fFirstEvent(qas.fFirstEvent),        
156         fMaxEvents(qas.fMaxEvents),    
157         fMode(qas.fMode), 
158         fNumberOfEvents(qas.fNumberOfEvents), 
159         fRecoParam(),           
160         fRunNumber(qas.fRunNumber), 
161         fRawReader(NULL), 
162         fRawReaderDelete(kTRUE), 
163         fRunLoader(NULL), 
164   fTasks(qas.fTasks), 
165   fEventSpecie(qas.fEventSpecie), 
166   fPrintImage(qas.fPrintImage), 
167   fSaveData(qas.fSaveData) 
168
169 {
170         // cpy ctor
171         for (UInt_t iDet = 0; iDet < fgkNDetectors; iDet++) {
172                 fLoader[iDet]         = qas.fLoader[iDet] ;
173                 fQADataMaker[iDet]    = qas.fQADataMaker[iDet] ;
174                 fQACycles[iDet]       = qas.fQACycles[iDet] ;   
175     fQAWriteExpert[iDet] = qas.fQAWriteExpert[iDet] ;
176   }
177 }
178
179 //_____________________________________________________________________________
180 AliQAManager & AliQAManager::operator = (const AliQAManager & qas) 
181 {
182         // assignment operator
183   this->~AliQAManager() ;
184   new(this) AliQAManager(qas) ;
185   return *this ;
186 }
187
188 //_____________________________________________________________________________
189 AliQAManager::~AliQAManager() 
190 {
191         // dtor
192         for (UInt_t iDet = 0; iDet < fgkNDetectors; iDet++) {
193                 if (IsSelected(AliQAv1::GetDetName(iDet))) {
194                   fLoader[iDet] = NULL;
195                   if (fQADataMaker[iDet]) {
196                           (fQADataMaker[iDet])->Finish() ; 
197                                 delete fQADataMaker[iDet] ;
198                   }
199                 }
200         }
201         if (fRawReaderDelete) { 
202                 fRunLoader = NULL ;
203                 delete fRawReader ;
204                 fRawReader = NULL ;
205         }
206 }
207 //_____________________________________________________________________________
208 Bool_t AliQAManager::DoIt(const AliQAv1::TASKINDEX_t taskIndex)
209 {
210         // Runs all the QA data Maker for every detector
211                 
212         Bool_t rv = kFALSE ;
213     // Fill QA data in event loop 
214         for (UInt_t iEvent = fFirstEvent ; iEvent < (UInt_t)fMaxEvents ; iEvent++) {
215                 fCurrentEvent++ ; 
216                 // Get the event
217                 if ( iEvent%10 == 0  ) 
218                         AliDebug(AliQAv1::GetQADebugLevel(), Form("processing event %d", iEvent));
219                 if ( taskIndex == AliQAv1::kRAWS ) {
220                         if ( !fRawReader->NextEvent() )
221                                 break ;
222                 } else if ( taskIndex == AliQAv1::kESDS ) {
223                         if ( fESDTree->GetEntry(iEvent) == 0 )
224                                 break ;
225                 } else {
226                         if ( fRunLoader->GetEvent(iEvent) != 0 )
227                                 break ;
228                 }
229                 // loop  over active loaders
230                 for (UInt_t iDet = 0; iDet < fgkNDetectors ; iDet++) {
231                         if (IsSelected(AliQAv1::GetDetName(iDet))) {
232                                 AliQADataMaker * qadm = GetQADataMaker(iDet) ;
233                                 if (!qadm) continue; // This detector doesn't have any QA (for example, HLT)
234                                 if ( qadm->IsCycleDone() ) {
235           qadm->EndOfCycle(taskIndex) ;
236                                 }
237                                 TTree * data = NULL ; 
238                                 AliLoader* loader = GetLoader(qadm->GetUniqueID());
239                                 switch (taskIndex) {
240                                         case AliQAv1::kNULLTASKINDEX : 
241                                                 break ; 
242                                         case AliQAv1::kRAWS :
243                                                 qadm->Exec(taskIndex, fRawReader) ; 
244                                                 break ; 
245                                         case AliQAv1::kHITS :
246             if( loader ) {
247               loader->LoadHits() ; 
248               data = loader->TreeH() ; 
249               if ( ! data ) {
250                 AliWarning(Form(" Hit Tree not found for  %s", AliQAv1::GetDetName(iDet))) ; 
251                 break ; 
252               } 
253               qadm->Exec(taskIndex, data) ;
254             } 
255                                                 break ;
256                                                 case AliQAv1::kSDIGITS :
257           {
258             
259             TString fileName(Form("%s.SDigits.root", AliQAv1::GetDetName(iDet))) ; 
260             if (gSystem->FindFile("./", fileName)) {
261               if( loader ) {      
262                 loader->LoadSDigits() ; 
263                 data = loader->TreeS() ; 
264                 if ( ! data ) {
265                   AliWarning(Form(" SDigit Tree not found for  %s", AliQAv1::GetDetName(iDet))) ; 
266                   break ; 
267                 } 
268                 qadm->Exec(taskIndex, data) ; 
269               }
270             }
271           }
272                                                 break; 
273                                                 case AliQAv1::kDIGITS :
274               if( loader ) {      
275                 loader->LoadDigits() ; 
276                 data = loader->TreeD() ; 
277                 if ( ! data ) {
278                   AliWarning(Form(" Digit Tree not found for  %s", AliQAv1::GetDetName(iDet))) ; 
279                   break ; 
280                 } 
281                 qadm->Exec(taskIndex, data) ;
282               }
283             break;
284             case AliQAv1::kDIGITSR :
285               if( loader ) {      
286                 loader->LoadDigits() ; 
287                 data = loader->TreeD() ; 
288                 if ( ! data ) {
289                   AliWarning(Form(" Digit Tree not found for  %s", AliQAv1::GetDetName(iDet))) ; 
290                   break ; 
291                 } 
292                 qadm->Exec(taskIndex, data) ;
293               }
294                                                 break; 
295                                                 case AliQAv1::kRECPOINTS :
296             if( loader ) {      
297               loader->LoadRecPoints() ; 
298               data = loader->TreeR() ; 
299               if (!data) {
300                 AliWarning(Form("RecPoints not found for %s", AliQAv1::GetDetName(iDet))) ; 
301                 break ; 
302               } 
303               qadm->Exec(taskIndex, data) ; 
304             }
305             break; 
306                                                 case AliQAv1::kTRACKSEGMENTS :
307                                                 break; 
308                                                 case AliQAv1::kRECPARTICLES :
309                                                 break; 
310                                                 case AliQAv1::kESDS :
311                                                 qadm->Exec(taskIndex, fESD) ;
312                                                 break; 
313                                                 case AliQAv1::kNTASKINDEX :
314                                                 break; 
315                                 } //task switch
316                         }
317                 } // detector loop
318     Increment(taskIndex) ; 
319         } // event loop 
320         // Save QA data for all detectors
321
322   EndOfCycle() ; 
323         
324         if ( taskIndex == AliQAv1::kRAWS ) 
325                 fRawReader->RewindEvents() ;
326
327         return rv ; 
328 }
329
330 //_____________________________________________________________________________
331 Bool_t AliQAManager::Finish(const AliQAv1::TASKINDEX_t taskIndex) 
332 {
333         // write output to file for all detectors
334   
335   AliQAChecker::Instance()->SetRunNumber(fRunNumber) ; 
336
337         for (UInt_t iDet = 0; iDet < fgkNDetectors ; iDet++) {
338                 if (IsSelected(AliQAv1::GetDetName(iDet))) {
339                         AliQADataMaker * qadm = GetQADataMaker(iDet) ;
340                         if (qadm) 
341         qadm->EndOfCycle(taskIndex) ;
342                 }
343         }
344         return kTRUE ; 
345 }
346
347 //_____________________________________________________________________________
348 TObjArray * AliQAManager::GetFromOCDB(AliQAv1::DETECTORINDEX_t det, AliQAv1::TASKINDEX_t task, const Char_t * year) const 
349 {
350         // Retrieve the list of QA data for a given detector and a given task 
351         TObjArray * rv = NULL ;
352         if ( !strlen(AliQAv1::GetQARefStorage()) ) { 
353                 AliError("No storage defined, use AliQAv1::SetQARefStorage") ; 
354                 return NULL ; 
355         }       
356         if ( ! IsDefaultStorageSet() ) {
357                 TString tmp(AliQAv1::GetQARefDefaultStorage()) ; 
358                 tmp.Append(year) ; 
359                 tmp.Append("/") ; 
360                 Instance()->SetDefaultStorage(tmp.Data()) ;             
361                 Instance()->SetSpecificStorage(Form("%s/*", AliQAv1::GetQAName()), AliQAv1::GetQARefStorage()) ;
362         }
363         TString detOCDBDir(Form("%s/%s/%s", AliQAv1::GetQAName(), AliQAv1::GetDetName((Int_t)det), AliQAv1::GetRefOCDBDirName())) ; 
364      AliDebug(AliQAv1::GetQADebugLevel(), Form("Retrieving reference data from %s/%s for %s", AliQAv1::GetQARefStorage(), detOCDBDir.Data(), AliQAv1::GetTaskName(task).Data())) ; 
365         AliCDBEntry* entry = QAManager()->Get(detOCDBDir.Data(), 0) ; //FIXME 0 --> Run Number
366         TList * listDetQAD = static_cast<TList *>(entry->GetObject()) ;
367         if ( listDetQAD ) 
368                 rv = static_cast<TObjArray *>(listDetQAD->FindObject(AliQAv1::GetTaskName(task))) ; 
369         return rv ; 
370 }
371
372 //_____________________________________________________________________________
373 TCanvas ** AliQAManager::GetImage(Char_t * detName)
374 {
375   // retrieves QA Image for the given detector
376   TCanvas ** rv = NULL ; 
377   Int_t detIndex = AliQAv1::GetDetIndex(detName) ; 
378   AliQACheckerBase * qac = AliQAChecker::Instance()->GetDetQAChecker(detIndex) ; 
379   rv = qac->GetImage() ;
380   return rv ; 
381 }
382
383 //_____________________________________________________________________________
384 AliLoader * AliQAManager::GetLoader(Int_t iDet)
385 {
386         // get the loader for a detector
387
388         if ( !fRunLoader || iDet == AliQAv1::kCORR || iDet == AliQAv1::kGLOBAL ) 
389                 return NULL ; 
390         
391         TString detName = AliQAv1::GetDetName(iDet) ;
392     fLoader[iDet] = fRunLoader->GetLoader(detName + "Loader");
393         if (fLoader[iDet]) 
394                 return fLoader[iDet] ;
395         
396         // load the QA data maker object
397         TPluginManager* pluginManager = gROOT->GetPluginManager() ;
398         TString loaderName = "Ali" + detName + "Loader" ;
399
400         AliLoader * loader = NULL ;
401         // first check if a plugin is defined for the quality assurance data maker
402         TPluginHandler* pluginHandler = pluginManager->FindHandler("AliLoader", detName) ;
403         // if not, add a plugin for it
404         if (!pluginHandler) {
405                 AliDebug(AliQAv1::GetQADebugLevel(), Form("defining plugin for %s", loaderName.Data())) ;
406                 TString libs = gSystem->GetLibraries() ;
407                 if (libs.Contains("lib" + detName + "base.so") || (gSystem->Load("lib" + detName + "base.so") >= 0)) {
408                         pluginManager->AddHandler("AliQADataMaker", detName, loaderName, detName + "loader", loaderName + "()") ;
409                 } else {
410                         pluginManager->AddHandler("AliLoader", detName, loaderName, detName, loaderName + "()") ;
411                 }
412                 pluginHandler = pluginManager->FindHandler("AliLoader", detName) ;
413         }
414         if (pluginHandler && (pluginHandler->LoadPlugin() == 0)) {
415                 loader = (AliLoader *) pluginHandler->ExecPlugin(0) ;
416         }
417         if (loader) 
418                 fLoader[iDet] = loader ;
419         return loader ;
420 }
421
422 //_____________________________________________________________________________
423 AliQAv1 * AliQAManager::GetQA(UInt_t run, UInt_t evt) 
424 {
425 // retrieves the QA object stored in a file named "Run{run}.Event{evt}_1.ESD.tag.root"  
426   Char_t * fileName = Form("Run%d.Event%d_1.ESD.tag.root", run, evt) ; 
427   TFile * tagFile = TFile::Open(fileName) ;
428   if ( !tagFile ) {
429     AliError(Form("File %s not found", fileName)) ;
430     return NULL ; 
431   }
432   TTree * tagTree = static_cast<TTree *>(tagFile->Get("T")) ; 
433   if ( !tagTree ) {
434     AliError(Form("Tree T not found in %s", fileName)) ; 
435     tagFile->Close() ; 
436     return NULL ; 
437   }
438   AliRunTag * tag = new AliRunTag ; 
439   tagTree->SetBranchAddress("AliTAG", &tag) ; 
440   tagTree->GetEntry(evt) ; 
441   AliQAv1 * qa = AliQAv1::Instance(tag->GetQALength(), tag->GetQAArray(), tag->GetESLength(), tag->GetEventSpecies()) ; 
442   tagFile->Close() ; 
443   return qa ; 
444 }
445
446 //_____________________________________________________________________________
447 AliQADataMaker * AliQAManager::GetQADataMaker(const Int_t iDet) 
448 {
449         // get the quality assurance data maker for a detector
450         
451   AliQADataMaker * qadm =  fQADataMaker[iDet] ; 
452   
453         if (qadm) {
454  
455     qadm->SetEventSpecie(fEventSpecie) ;  
456     if ( qadm->GetRecoParam() ) 
457       if ( AliRecoParam::Convert(qadm->GetRecoParam()->GetEventSpecie()) != AliRecoParam::kDefault)
458         qadm->SetEventSpecie(qadm->GetRecoParam()->GetEventSpecie()) ; 
459
460   } else if (iDet == AliQAv1::kGLOBAL && strcmp(GetMode(), AliQAv1::GetModeName(AliQAv1::kRECMODE)) == 0) { //Global QA
461
462                 qadm = new AliGlobalQADataMaker();
463                 qadm->SetName(AliQAv1::GetDetName(iDet));
464                 qadm->SetUniqueID(iDet);
465                 fQADataMaker[iDet] = qadm;
466     qadm->SetEventSpecie(fEventSpecie) ;  
467     if ( qadm->GetRecoParam() ) 
468       if ( AliRecoParam::Convert(qadm->GetRecoParam()->GetEventSpecie()) != AliRecoParam::kDefault)  
469         qadm->SetEventSpecie(qadm->GetRecoParam()->GetEventSpecie()) ; 
470
471         }       else if (iDet == AliQAv1::kCORR && strcmp(GetMode(), AliQAv1::GetModeName(AliQAv1::kRECMODE)) == 0 ) { //the data maker for correlations among detectors
472     qadm = new AliCorrQADataMakerRec(fQADataMaker) ; 
473                 qadm->SetName(AliQAv1::GetDetName(iDet));
474                 qadm->SetUniqueID(iDet);
475                 fQADataMaker[iDet] = qadm;
476     qadm->SetEventSpecie(fEventSpecie) ;  
477     if ( qadm->GetRecoParam() ) 
478       if ( AliRecoParam::Convert(qadm->GetRecoParam()->GetEventSpecie()) != AliRecoParam::kDefault)  
479         qadm->SetEventSpecie(qadm->GetRecoParam()->GetEventSpecie()) ; 
480
481   } else if ( iDet < AliQAv1::kGLOBAL ) {
482     TString  smode(GetMode()) ; 
483     if (smode.Contains(AliQAv1::GetModeName(AliQAv1::kQAMODE)))
484       smode = AliQAv1::GetModeName(AliQAv1::kRECMODE) ; 
485     // load the QA data maker object
486     TPluginManager* pluginManager = gROOT->GetPluginManager() ;
487     TString detName = AliQAv1::GetDetName(iDet) ;
488     TString qadmName = "Ali" + detName + "QADataMaker" + smode ;
489     
490     // first check if a plugin is defined for the quality assurance data maker
491     TPluginHandler* pluginHandler = pluginManager->FindHandler("AliQADataMaker", detName) ;
492     // if not, add a plugin for it
493     if (!pluginHandler) {
494       AliDebug(AliQAv1::GetQADebugLevel(), Form("defining plugin for %s", qadmName.Data())) ;
495       TString libs = gSystem->GetLibraries() ;
496       TString temp(smode) ;
497       temp.ToLower() ; 
498       if (libs.Contains("lib" + detName + smode + ".so") || (gSystem->Load("lib" + detName + temp.Data() + ".so") >= 0)) {
499         pluginManager->AddHandler("AliQADataMaker", detName, qadmName, detName + "qadm", qadmName + "()") ;
500       } else {
501         pluginManager->AddHandler("AliQADataMaker", detName, qadmName, detName, qadmName + "()") ;
502       }
503       pluginHandler = pluginManager->FindHandler("AliQADataMaker", detName) ;
504     }
505     if (pluginHandler && (pluginHandler->LoadPlugin() == 0)) {
506       qadm = (AliQADataMaker *) pluginHandler->ExecPlugin(0) ;
507     }
508     if (qadm) {
509       qadm->SetName(AliQAv1::GetDetName(iDet));
510       qadm->SetUniqueID(iDet);
511       fQADataMaker[iDet] = qadm ;
512       qadm->SetEventSpecie(fEventSpecie) ;  
513       if ( qadm->GetRecoParam() ) 
514         if ( AliRecoParam::Convert(qadm->GetRecoParam()->GetEventSpecie()) != AliRecoParam::kDefault)  
515           qadm->SetEventSpecie(qadm->GetRecoParam()->GetEventSpecie()) ; 
516     }
517   }
518   return qadm ;
519 }
520
521 //_____________________________________________________________________________
522 void  AliQAManager::EndOfCycle(TObjArray * detArray) 
523 {
524         // End of cycle QADataMakers 
525         
526   AliQAChecker::Instance()->SetRunNumber(fRunNumber) ; 
527   TCanvas fakeCanvas ; 
528
529     fakeCanvas.Print(Form("%s%s%d.%s[", AliQAv1::GetImageFileName(), GetMode(), fRunNumber, AliQAv1::GetImageFileFormat()), "ps") ; 
530         for (UInt_t iDet = 0; iDet < fgkNDetectors ; iDet++) {
531                 if (IsSelected(AliQAv1::GetDetName(iDet))) {
532                         AliQADataMaker * qadm = GetQADataMaker(iDet) ;
533                         if (!qadm) 
534                                 continue ;      
535                         // skip non active detectors
536                         if (detArray) {
537                                 AliModule* det = static_cast<AliModule*>(detArray->FindObject(AliQAv1::GetDetName(iDet))) ;
538                                 if (!det || !det->IsActive())  
539                                         continue ;
540                         }
541                         AliQACheckerBase * qac = AliQAChecker::Instance()->GetDetQAChecker(iDet) ;
542       if (qac) 
543         qac->SetPrintImage(fPrintImage) ;
544       for (UInt_t taskIndex = 0; taskIndex < AliQAv1::kNTASKINDEX; taskIndex++) {
545         if ( fTasks.Contains(Form("%d", taskIndex)) ) 
546           qadm->EndOfCycle(AliQAv1::GetTaskIndex(AliQAv1::GetTaskName(taskIndex))) ;
547       }
548                         qadm->Finish();
549                 }
550         }
551   if (fPrintImage) 
552     fakeCanvas.Print(Form("%s%s%d.%s]", AliQAv1::GetImageFileName(), GetMode(), fRunNumber, AliQAv1::GetImageFileFormat()), "ps"); 
553 }
554
555 //_____________________________________________________________________________
556 void  AliQAManager::EndOfCycle(TString detectors) 
557 {
558         // End of cycle QADataMakers 
559         
560   AliQAChecker::Instance()->SetRunNumber(fRunNumber) ; 
561   TCanvas fakeCanvas ; 
562   if (fPrintImage) 
563     fakeCanvas.Print(Form("%s%s%d.%s[", AliQAv1::GetImageFileName(), GetMode(), fRunNumber, AliQAv1::GetImageFileFormat()), "ps") ; 
564   for (UInt_t iDet = 0; iDet < fgkNDetectors ; iDet++) {
565                 if (IsSelected(AliQAv1::GetDetName(iDet))) {
566                         AliQADataMaker * qadm = GetQADataMaker(iDet) ;
567                         if (!qadm) 
568                                 continue ;      
569                         // skip non active detectors
570       if (!detectors.Contains(AliQAv1::GetDetName(iDet))) 
571         continue ;
572                         AliQACheckerBase * qac = AliQAChecker::Instance()->GetDetQAChecker(iDet) ;
573       if (qac) 
574         qac->SetPrintImage(fPrintImage) ;
575       for (UInt_t taskIndex = 0; taskIndex < AliQAv1::kNTASKINDEX; taskIndex++) {
576         if ( fTasks.Contains(Form("%d", taskIndex)) ) 
577           qadm->EndOfCycle(AliQAv1::GetTaskIndex(AliQAv1::GetTaskName(taskIndex))) ;
578       }
579                         qadm->Finish();
580                 }
581         }
582   if (fPrintImage) 
583     fakeCanvas.Print(Form("%s%s%d.%s]", AliQAv1::GetImageFileName(), GetMode(), fRunNumber, AliQAv1::GetImageFileFormat()), "ps"); 
584 }
585
586 //_____________________________________________________________________________
587 AliRecoParam::EventSpecie_t AliQAManager::GetEventSpecieFromESD() 
588 {
589   AliRecoParam::EventSpecie_t runtype = AliRecoParam::kDefault ; 
590   if (!gSystem->AccessPathName("AliESDs.root")) { // AliESDs.root exists
591     TFile * esdFile = TFile::Open("AliESDs.root") ;
592     TTree * esdTree = static_cast<TTree *> (esdFile->Get("esdTree")) ; 
593     if ( !esdTree ) {
594       AliError("esdTree not found") ; 
595     } else {
596       AliESDEvent * esd    = new AliESDEvent() ;
597       esd->ReadFromTree(esdTree) ;
598       esdTree->GetEntry(0) ; 
599       runtype = AliRecoParam::Convert(esd->GetEventType()) ; 
600     }
601   } else {
602     AliError("AliESDs.root not found") ; 
603   }
604   return runtype ;
605 }
606
607 //_____________________________________________________________________________
608 void AliQAManager::Increment(const AliQAv1::TASKINDEX_t taskIndex)
609 {
610   // Increments the cycle counter for all QA Data Makers
611   static AliQAv1::TASKINDEX_t currentTask = AliQAv1::kNTASKINDEX ; 
612   if ( (currentTask == taskIndex) && taskIndex != AliQAv1::kNULLTASKINDEX )
613     return ; 
614   else 
615     currentTask = taskIndex ; 
616         for (UInt_t iDet = 0; iDet < fgkNDetectors ; iDet++) {
617                 if (IsSelected(AliQAv1::GetDetName(iDet))) {
618                         AliQADataMaker * qadm = GetQADataMaker(iDet) ;
619                         if (qadm) 
620         qadm->Increment() ;
621     }
622   }
623 }
624   
625 //_____________________________________________________________________________
626 Bool_t AliQAManager::InitQA(const AliQAv1::TASKINDEX_t taskIndex, const  Char_t * input )
627 {
628         // Initialize the event source and QA data makers
629         
630         fTasks += Form("%d", taskIndex) ; 
631
632         if (taskIndex == AliQAv1::kRAWS) { 
633                 if (!fRawReader) {
634                         fRawReader = AliRawReader::Create(input);
635                 }
636                 if ( ! fRawReader ) 
637                         return kFALSE ; 
638                 fRawReaderDelete = kTRUE ; 
639                 fRawReader->NextEvent() ; 
640                 fRunNumber = fRawReader->GetRunNumber() ; 
641                 SetRun(fRunNumber) ; 
642                 fRawReader->RewindEvents();
643                 fNumberOfEvents = 999999 ;
644                 if ( fMaxEvents < 0 ) 
645                         fMaxEvents = fNumberOfEvents ; 
646                 } else if (taskIndex == AliQAv1::kESDS) {
647                         fTasks = AliQAv1::GetTaskName(AliQAv1::kESDS) ; 
648       if (!gSystem->AccessPathName("AliESDs.root")) { // AliESDs.root exists
649         TFile * esdFile = TFile::Open("AliESDs.root") ;
650         fESDTree = static_cast<TTree *> (esdFile->Get("esdTree")) ; 
651         if ( !fESDTree ) {
652           AliError("esdTree not found") ; 
653           return kFALSE ; 
654         } else {
655           fESD     = new AliESDEvent() ;
656           fESD->ReadFromTree(fESDTree) ;
657           fESDTree->GetEntry(0) ; 
658           fRunNumber = fESD->GetRunNumber() ; 
659           fNumberOfEvents = fESDTree->GetEntries() ;
660           if ( fMaxEvents < 0 ) 
661             fMaxEvents = fNumberOfEvents ; 
662         }
663       } else {
664         AliError("AliESDs.root not found") ; 
665         return kFALSE ; 
666       }                 
667     } else {
668       if ( !InitRunLoader() ) { 
669         AliWarning("No Run Loader not found") ; 
670       } else {
671         fNumberOfEvents = fRunLoader->GetNumberOfEvents() ;
672         if ( fMaxEvents < 0 ) 
673           fMaxEvents = fNumberOfEvents ; 
674       }
675     }
676
677   // Get Detectors 
678   TObjArray* detArray = NULL ; 
679         if (fRunLoader) // check if RunLoader exists 
680                 if ( fRunLoader->GetAliRun() ) { // check if AliRun exists in gAlice.root
681                         detArray = fRunLoader->GetAliRun()->Detectors() ;
682                         fRunNumber = fRunLoader->GetHeader()->GetRun() ; 
683                 }
684
685         // Initialize all QA data makers for all detectors
686         fRunNumber = AliCDBManager::Instance()->GetRun() ; 
687         if ( !  AliGeomManager::GetGeometry() ) 
688                 AliGeomManager::LoadGeometry() ; 
689         
690         InitQADataMaker(fRunNumber, detArray) ; //, fCycleSame, kTRUE, detArray) ; 
691   if (fPrintImage) {
692     TCanvas fakeCanvas ; 
693     TStopwatch timer ; 
694     timer.Start() ; 
695     while (timer.CpuTime()<5) {
696       timer.Continue();
697       gSystem->ProcessEvents();
698     }
699     fakeCanvas.Print(Form("%s%s%d.%s[", AliQAv1::GetImageFileName(), GetMode(), fRunNumber, AliQAv1::GetImageFileFormat()), "ps") ;    
700   }    
701         return kTRUE ; 
702 }
703
704 //_____________________________________________________________________________
705 void  AliQAManager::InitQADataMaker(UInt_t run, TObjArray * detArray) 
706 {
707         // Initializes The QADataMaker for all active detectors and for all active tasks 
708   fRunNumber = run ; 
709         for (UInt_t iDet = 0; iDet < fgkNDetectors ; iDet++) {
710                 if (IsSelected(AliQAv1::GetDetName(iDet))) {
711                         AliQADataMaker * qadm = GetQADataMaker(iDet) ;
712                         if (!qadm) {
713                                 AliError(Form("AliQADataMaker not found for %s", AliQAv1::GetDetName(iDet))) ; 
714                                 fDetectorsW.ReplaceAll(AliQAv1::GetDetName(iDet), "") ; 
715                         } else {
716         if (fQAWriteExpert[iDet])
717           qadm->SetWriteExpert() ; 
718                                 AliDebug(AliQAv1::GetQADebugLevel(), Form("Data Maker found for %s %d", qadm->GetName(), qadm->WriteExpert())) ; 
719                                 // skip non active detectors
720                                 if (detArray) {
721                                         AliModule* det = static_cast<AliModule*>(detArray->FindObject(AliQAv1::GetDetName(iDet))) ;
722                                         if (!det || !det->IsActive())  
723                                                 continue ;
724                                 }
725               // Set default reco params
726         Bool_t sameCycle = kFALSE ; 
727                                 for (UInt_t taskIndex = 0; taskIndex < AliQAv1::kNTASKINDEX; taskIndex++) {
728                                         if ( fTasks.Contains(Form("%d", taskIndex)) ) {
729                                                 qadm->Init(AliQAv1::GetTaskIndex(AliQAv1::GetTaskName(taskIndex)), GetQACycles(qadm->GetUniqueID())) ;
730             qadm->StartOfCycle(AliQAv1::GetTaskIndex(AliQAv1::GetTaskName(taskIndex)), run,  sameCycle) ;
731             sameCycle = kTRUE ;
732                                         }
733                                 }
734                         }
735                 }
736         }
737 }
738
739
740 //_____________________________________________________________________________
741 Bool_t AliQAManager::InitRunLoader()
742 {
743         // get or create the run loader
744         if (fRunLoader) {
745                 fCycleSame = kTRUE ; 
746         } else {
747                 if (!gSystem->AccessPathName(fGAliceFileName.Data())) { // galice.root exists
748                         // load all base libraries to get the loader classes
749                         TString libs = gSystem->GetLibraries() ;
750                         for (UInt_t iDet = 0; iDet < fgkNDetectors; iDet++) {
751                                 if (!IsSelected(AliQAv1::GetDetName(iDet))) 
752                                         continue ; 
753                                 TString detName = AliQAv1::GetDetName(iDet) ;
754                                 if (detName == "HLT") 
755                                         continue;
756                                 if (libs.Contains("lib" + detName + "base.so")) 
757                                         continue;
758                                 gSystem->Load("lib" + detName + "base.so");
759                         }
760                         fRunLoader = AliRunLoader::Open(fGAliceFileName.Data());
761                         if (!fRunLoader) {
762                                 AliError(Form("no run loader found in file %s", fGAliceFileName.Data()));
763                                 return kFALSE;
764                         }
765                         fRunLoader->CdGAFile();
766                         if (fRunLoader->LoadgAlice() == 0) {
767                                 gAlice = fRunLoader->GetAliRun();
768                         }
769
770                         if (!gAlice) {
771                                 AliError(Form("no gAlice object found in file %s", fGAliceFileName.Data()));
772                                 return kFALSE;
773                         }
774
775                 } else {               // galice.root does not exist
776                         AliError(Form("the file %s does not exist", fGAliceFileName.Data()));
777                         return kFALSE;
778                 }
779         }
780
781         if (!fRunNumber) { 
782                 fRunLoader->LoadHeader();
783                 fRunNumber = fRunLoader->GetHeader()->GetRun() ; 
784         }
785         return kTRUE;
786 }
787
788 //_____________________________________________________________________________
789 Bool_t AliQAManager::IsSelected(const Char_t * det) 
790 {
791   // check whether detName is contained in detectors
792         // if yes, it is removed from detectors
793         
794         Bool_t rv = kFALSE;
795         const TString detName(det) ;
796   // always activates Correlation
797   if ( detName.Contains(AliQAv1::GetDetName(AliQAv1::kCORR)) || detName.Contains(AliQAv1::GetDetName(AliQAv1::kGLOBAL))) {
798     rv = kTRUE ; 
799   } else {
800     // check if all detectors are selected
801     if (fDetectors.Contains("ALL")) {
802       fDetectors = "ALL";
803       rv = kTRUE;
804     } else if ((fDetectors.CompareTo(detName) == 0) ||
805                fDetectors.BeginsWith(detName+" ") ||
806                fDetectors.EndsWith(" "+detName) ||
807                fDetectors.Contains(" "+detName+" ")) {
808       rv = kTRUE;
809     }
810   }
811         return rv ;
812 }
813
814 //_____________________________________________________________________________
815 Bool_t AliQAManager::Merge(Int_t runNumber, const char *fileName) const
816 {
817         // Merge data from all detectors from a given run in one single file 
818         // Merge the QA results from all the data chunks in one run
819   // The 'fileName' is name of the output file with merged QA data  
820  if ( runNumber == -1)
821    runNumber = fRunNumber ; 
822  Bool_t rv = MergeData(runNumber,fileName) ; 
823  //rv *= MergeResults(runNumber) ; // not needed for the time being
824  return rv ; 
825 }
826         
827 //______________________________________________________________________
828 Bool_t AliQAManager::MergeXML(const Char_t * collectionFile, const Char_t * subFile, const Char_t * outFile) 
829 {
830   // merges files listed in a xml collection 
831   // usage Merge(collection, outputFile))
832   //              collection: is a xml collection  
833   
834   Bool_t rv = kFALSE ; 
835   
836   if ( strstr(collectionFile, ".xml") == 0 ) {
837     AliError("Input collection file must be an \".xml\" file\n") ; 
838     return kFALSE ; 
839   }
840     
841  if ( !gGrid ) 
842    TGrid::Connect("alien://"); 
843  if ( !gGrid ) 
844    return kFALSE ; 
845  
846   // Open the file collection 
847   AliInfoClass(Form("*** Create Collection       ***\n***  Wk-Dir = |%s|             \n***  Coll   = |%s|             \n",gSystem->WorkingDirectory(), collectionFile));                
848   
849   TGridCollection * collection = (TGridCollection*)gROOT->ProcessLine(Form("TAlienCollection::Open(\"%s\")",collectionFile));
850   TGridResult* result = collection->GetGridResult("", 0, 0);
851   
852   Int_t index = 0  ;
853   const Char_t * turl ;
854   TFileMerger merger(kFALSE) ; 
855   if (!outFile) {
856     TString tempo(collectionFile) ; 
857     if ( subFile) 
858       tempo.ReplaceAll(".xml", subFile) ; 
859     else 
860       tempo.ReplaceAll(".xml", "_Merged.root") ; 
861     outFile = tempo.Data() ; 
862   }
863   merger.OutputFile(outFile) ; 
864   
865   while ( (turl = result->GetKey(index, "turl")) ) {
866     Char_t * file ;
867     if ( subFile )
868       file = Form("%s#%s", turl, subFile) ; 
869     else 
870       file = Form("%s", turl) ; 
871     
872     AliDebug(AliQAv1::GetQADebugLevel(), Form("%s\n", file)) ; 
873     merger.AddFile(file) ; 
874     index++ ;  
875   }
876   
877   if (index) 
878     merger.Merge() ; 
879   
880   AliDebug(AliQAv1::GetQADebugLevel(), Form("Files merged into %s\n", outFile)) ;
881   
882   rv = kFALSE;
883   return rv ;
884 }
885
886 //_____________________________________________________________________________
887 void AliQAManager::MergeCustom() const
888 {
889         // Custom Merge of QA data from all detectors for all runs in one single file 
890   // search all the run numbers
891   // search all the run numbers
892   gROOT->ProcessLine(".! ls *QA*.root > QAtempo.txt") ;
893   TString QAfile ; 
894   FILE * QAfiles = fopen("QAtempo.txt", "r") ; 
895   Int_t index = 0 ; 
896   TList srunList ; 
897   TIter nextRun(&srunList) ; 
898   TObjString * srun = NULL ; 
899   Int_t loRun = 999999999 ; 
900   Int_t hiRun = 0 ; 
901   while ( QAfile.Gets(QAfiles) ) {
902     Bool_t runExist = kFALSE ; 
903     TString srunNew(QAfile(QAfile.Index("QA.")+3, QAfile.Index(".root")-(QAfile.Index("QA.")+3))) ; 
904     Int_t cuRun = srunNew.Atoi() ;
905     if (cuRun < loRun) 
906       loRun = cuRun ; 
907     if (cuRun > hiRun)
908       hiRun = cuRun ; 
909     while ( (srun = static_cast<TObjString *> (nextRun())) ) {
910       if ( cuRun == (srun->String()).Atoi() ) {
911         runExist = kTRUE ; 
912         break ; 
913       } 
914     }
915     nextRun.Reset() ; 
916     if ( ! runExist ) 
917       srunList.Add(new TObjString(srunNew.Data()));
918   }
919   nextRun.Reset() ;    
920   Int_t runNumber = 0 ; 
921   TFile mergedFile(Form("Merged.%s.Data.root", AliQAv1::GetQADataFileName()), "RECREATE") ; 
922   TH1I * hisRun = new TH1I("hLMR", "List of merged runs", hiRun-loRun+10, loRun, hiRun+10) ; 
923   // create the structure into the merged file
924   for (Int_t iDet = 0; iDet < AliQAv1::kNDET ; iDet++) {
925     TDirectory * detDir = mergedFile.mkdir(AliQAv1::GetDetName(iDet)) ; 
926     for (Int_t taskIndex = 0; taskIndex < AliQAv1::kNTASKINDEX; taskIndex++) {
927       detDir->cd() ; 
928       TDirectory * taskDir = gDirectory->mkdir(AliQAv1::GetTaskName(taskIndex)) ; 
929       for (Int_t es = 0 ; es < AliRecoParam::kNSpecies ; es++) {
930         taskDir->cd() ; 
931         TDirectory * esDir = gDirectory->mkdir(AliRecoParam::GetEventSpecieName(es)) ;
932         esDir->cd() ; 
933         gDirectory->mkdir(AliQAv1::GetExpert()) ; 
934       }
935     }
936   }
937   while ( (srun = static_cast<TObjString *> (nextRun())) ) {
938     runNumber = (srun->String()).Atoi() ; 
939     hisRun->Fill(runNumber) ; 
940     AliDebug(AliQAv1::GetQADebugLevel(), Form("Merging run number %d", runNumber)) ; 
941     // search all QA files for runNumber in the current directory
942     Char_t * fileList[AliQAv1::kNDET] ;
943     index = 0 ; 
944     for (Int_t iDet = 0; iDet < AliQAv1::kNDET ; iDet++) {
945       Char_t * file = gSystem->Which(gSystem->WorkingDirectory(), Form("%s.%s.%d.root", AliQAv1::GetDetName(iDet), AliQAv1::GetQADataFileName(), runNumber)); 
946       if (file) 
947         fileList[index++] = file ;
948     }
949     if ( index == 0 ) {
950       AliError("No QA data file found\n") ; 
951       return ; 
952     }
953     for ( Int_t i = 0 ; i < index ; i++) {
954       TFile * inFile = TFile::Open(fileList[i]) ;  
955       TList * listOfKeys =inFile->GetListOfKeys() ; 
956       TIter nextkey(listOfKeys) ; 
957       TObject * obj1 ; 
958       TString dirName("") ; 
959       while ( (obj1 = nextkey()) ) {
960         TDirectory * directoryDet = inFile->GetDirectory(obj1->GetName()) ; 
961         if ( directoryDet ) {
962           AliDebug(AliQAv1::GetQADebugLevel(), Form("%s dir = %s", inFile->GetName(), directoryDet->GetName())) ; 
963           dirName += Form("%s/", directoryDet->GetName() ) ; 
964           directoryDet->cd() ;
965           TList * listOfTasks = directoryDet->GetListOfKeys() ; 
966           TIter nextTask(listOfTasks) ; 
967           TObject * obj2 ; 
968           while ( (obj2 = nextTask()) ) {
969             TDirectory * directoryTask = directoryDet->GetDirectory(obj2->GetName()) ; 
970             if ( directoryTask ) {
971               dirName += Form("%s", obj2->GetName()) ; 
972               AliDebug(AliQAv1::GetQADebugLevel(), Form("%s", dirName.Data())) ; 
973               directoryTask->cd() ; 
974               TList * listOfEventSpecie = directoryTask->GetListOfKeys() ; 
975               TIter nextEventSpecie(listOfEventSpecie) ; 
976               TObject * obj3 ; 
977               while ( (obj3 = nextEventSpecie()) ) {
978                 TDirectory * directoryEventSpecie = directoryTask->GetDirectory(obj3->GetName()) ; 
979                 if ( directoryEventSpecie ) {
980                   dirName += Form("/%s/", obj3->GetName()) ; 
981                   AliDebug(AliQAv1::GetQADebugLevel(), Form("%s\n", dirName.Data())) ; 
982                   directoryEventSpecie->cd() ; 
983                   // histograms are here
984                   TDirectory * mergedDirectory = mergedFile.GetDirectory(dirName.Data()) ;
985                   TList * listOfData = directoryEventSpecie->GetListOfKeys() ; 
986                   TIter nextData(listOfData) ; 
987                   TKey * key ; 
988                   while ( (key = static_cast<TKey *>(nextData())) ) {
989                     TString className(key->GetClassName()) ; 
990                     if (  className.Contains("TH") || className.Contains("TProfile") ) {
991                       TH1 * histIn = static_cast<TH1*> (key->ReadObj()) ; 
992                       TH1 * histOu = static_cast<TH1*> (mergedDirectory->FindObjectAny(histIn->GetName())) ; 
993                       AliDebug(AliQAv1::GetQADebugLevel(), Form("%s %p %p\n", key->GetName(), histIn, histOu)) ; 
994                       mergedDirectory->cd() ; 
995                       if ( ! histOu ) {
996                         histIn->Write() ; 
997                       } else {
998                         histOu->Add(histIn) ; 
999                         histOu->Write(histOu->GetName(), kOverwrite) ; 
1000                       }
1001                     }
1002                     else if ( className.Contains("TDirectoryFile") ) {
1003                       TDirectory * dirExpert = directoryEventSpecie->GetDirectory(key->GetName()) ; 
1004                       dirExpert->cd() ; 
1005                       TDirectory * mergedDirectoryExpert = mergedDirectory->GetDirectory(dirExpert->GetName()) ; 
1006                       TList * listOfExpertData = dirExpert->GetListOfKeys() ; 
1007                       TIter nextExpertData(listOfExpertData) ; 
1008                       TKey * keykey ; 
1009                       while ( (keykey = static_cast<TKey *>(nextExpertData())) ) {
1010                         TString classNameExpert(keykey->GetClassName()) ; 
1011                         if (classNameExpert.Contains("TH")) {
1012                           TH1 * histInExpert = static_cast<TH1*> (keykey->ReadObj()) ; 
1013                           TH1 * histOuExpert = static_cast<TH1*> (mergedDirectory->FindObjectAny(histInExpert->GetName())) ; 
1014                           mergedDirectoryExpert->cd() ; 
1015                           if ( ! histOuExpert ) {
1016                             histInExpert->Write() ; 
1017                           } else {
1018                             histOuExpert->Add(histInExpert) ; 
1019                             histOuExpert->Write(histOuExpert->GetName(), kOverwrite) ; 
1020                           }
1021                         }
1022                       }
1023                     } else {
1024                       AliError(Form("No merge done for this object %s in %s", key->GetName(), dirName.Data())) ; 
1025                     }
1026                   }
1027                   dirName.ReplaceAll(Form("/%s/",obj3->GetName()), "") ; 
1028                 }
1029               }
1030               dirName.ReplaceAll(obj2->GetName(), "") ; 
1031             }
1032           }
1033         }
1034       }
1035       inFile->Close() ; 
1036     }
1037   }
1038   mergedFile.cd() ;
1039   hisRun->Write() ; 
1040   mergedFile.Close() ; 
1041   srunList.Delete() ;   
1042 }
1043
1044 //_____________________________________________________________________________
1045 Bool_t AliQAManager::MergeData(const Int_t runNumber, const char *fileName) const
1046 {
1047         // Merge QA data from all detectors for a given run in one single file 
1048   
1049   TFileMerger merger(kFALSE) ; 
1050   TString outFileName = fileName;
1051   if (outFileName.IsNull()) outFileName.Form("Merged.%s.Data.root",AliQAv1::GetQADataFileName());
1052   merger.OutputFile(outFileName.Data()) ; 
1053   for (UInt_t iDet = 0; iDet < fgkNDetectors ; iDet++) {
1054     Char_t * file = gSystem->Which(gSystem->WorkingDirectory(), Form("%s.%s.%d.root", AliQAv1::GetDetName(iDet), AliQAv1::GetQADataFileName(), runNumber)); 
1055     if (file) 
1056       merger.AddFile(file);
1057     delete[] file;
1058   }
1059   merger.Merge() ; 
1060         return kTRUE ; 
1061 }
1062
1063 //_____________________________________________________________________________
1064 Bool_t AliQAManager::MergeResults(const Int_t runNumber) const
1065 {
1066         // Merge the QA result from all the data chunks in a run 
1067   // to be revised whwn it will be used (see MergeData)
1068         TString cmd ;
1069         cmd = Form(".! ls %s*.root > tempo.txt", AliQAv1::GetQADataFileName()) ; 
1070         gROOT->ProcessLine(cmd.Data()) ;
1071         ifstream in("tempo.txt") ; 
1072         const Int_t chunkMax = 100 ;  
1073         TString fileList[chunkMax] ;
1074         
1075         Int_t index = 0 ; 
1076         while ( 1 ) {
1077                 TString file ; 
1078                 in >> fileList[index] ; 
1079                 if ( !in.good() ) 
1080                         break ; 
1081                 AliDebug(AliQAv1::GetQADebugLevel(), Form("index = %d file = %s", index, (fileList[index].Data()))) ; 
1082                 index++ ;
1083         }
1084         
1085         if ( index == 0 ) { 
1086                 AliError("No QA Result File found") ; 
1087                 return kFALSE ; 
1088         }
1089         
1090         TFileMerger merger ; 
1091   TString outFileName ;
1092   if (runNumber != -1) 
1093     outFileName = Form("Merged.%s.Result.%d.root",AliQAv1::GetQADataFileName(),runNumber); 
1094   else 
1095     outFileName = Form("Merged.%s.Result.root",AliQAv1::GetQADataFileName()); 
1096         merger.OutputFile(outFileName.Data()) ; 
1097         for (Int_t ifile = 0 ; ifile < index ; ifile++) {
1098                 TString file = fileList[ifile] ; 
1099                 merger.AddFile(file) ; 
1100         }
1101         merger.Merge() ; 
1102         
1103         return kTRUE ; 
1104 }
1105
1106 //_____________________________________________________________________________
1107 void AliQAManager::Reset(const Bool_t sameCycle)
1108 {
1109         // Reset the default data members
1110
1111         for (UInt_t iDet = 0; iDet < fgkNDetectors ; iDet++) {
1112                 if (IsSelected(AliQAv1::GetDetName(iDet))) {
1113                         AliQADataMaker * qadm = GetQADataMaker(iDet);
1114                         if (qadm) 
1115         qadm->Reset();
1116                 }
1117         } 
1118         if (fRawReaderDelete) { 
1119                 delete fRawReader ;
1120                 fRawReader      = NULL ;
1121         }
1122
1123         fCycleSame      = sameCycle ; 
1124         fESD            = NULL ; 
1125         fESDTree        = NULL ; 
1126         //fFirst          = kTRUE ;   
1127         fNumberOfEvents = 999999 ;  
1128 }
1129
1130 //_____________________________________________________________________________
1131 void AliQAManager::ResetDetectors(AliQAv1::TASKINDEX_t task, AliQAv1::DETECTORINDEX_t det)
1132 {
1133     //calls ResetDetector of specified or all detectors
1134   UInt_t iDet    = 0 ;
1135   UInt_t iDetMax = fgkNDetectors ;    
1136   if ( det != AliQAv1::kNULLDET ) {
1137     iDet    = det ;
1138     iDetMax = det+1 ;    
1139   }
1140   
1141         for (iDet = 0; iDet < iDetMax ; iDet++) {
1142                 if (IsSelected(AliQAv1::GetDetName(iDet))) {
1143                         AliQADataMaker * qadm = GetQADataMaker(iDet);
1144                         qadm->ResetDetector(task);
1145                 }
1146         }   
1147 }
1148
1149 //_____________________________________________________________________________
1150 AliQAManager * AliQAManager::QAManager(AliQAv1::MODE_t mode, TMap *entryCache, Int_t run) 
1151 {
1152   // returns AliQAManager instance (singleton)
1153   
1154         if (!fgQAInstance) {
1155     if ( (mode != AliQAv1::kSIMMODE) && (mode != AliQAv1::kRECMODE) && (mode != AliQAv1::kQAMODE) ) {
1156       AliWarningClass("You must specify kSIMMODE or kRECMODE or kQAMODE") ; 
1157       return NULL ; 
1158     }
1159     fgQAInstance = new AliQAManager(mode) ;  
1160     if (!entryCache)
1161                   fgQAInstance->Init();
1162                 else
1163                   fgQAInstance->InitFromCache(entryCache,run);
1164   }
1165         return fgQAInstance;
1166 }
1167
1168 //_____________________________________________________________________________
1169 AliQAManager * AliQAManager::QAManager(AliQAv1::TASKINDEX_t task) 
1170 {
1171   // returns AliQAManager instance (singleton)
1172   return QAManager(AliQAv1::Mode(task)) ; 
1173 }
1174
1175 //_____________________________________________________________________________
1176 TString AliQAManager::Run(const Char_t * detectors, AliRawReader * rawReader, const Bool_t sameCycle) 
1177 {
1178         //Runs all the QA data Maker for Raws only
1179         
1180         fCycleSame       = sameCycle ;
1181         fRawReader       = rawReader ;
1182         fDetectors       = detectors ; 
1183         fDetectorsW      = detectors ;  
1184         
1185         AliCDBManager* man = AliCDBManager::Instance() ; 
1186
1187         if ( man->GetRun() == -1 ) {// check if run number not set previously and set it from raw data
1188                 rawReader->NextEvent() ; 
1189                 man->SetRun(fRawReader->GetRunNumber()) ;
1190                 rawReader->RewindEvents() ;
1191         }       
1192         
1193         if (!fCycleSame) 
1194     if ( !InitQA(AliQAv1::kRAWS) ) 
1195       return "" ; 
1196   fRawReaderDelete = kFALSE ; 
1197
1198         DoIt(AliQAv1::kRAWS) ; 
1199         return  fDetectorsW ;
1200 }
1201
1202 //_____________________________________________________________________________
1203 TString AliQAManager::Run(const Char_t * detectors, const Char_t * fileName, const Bool_t sameCycle) 
1204 {
1205         //Runs all the QA data Maker for Raws only
1206
1207         fCycleSame       = sameCycle ;
1208         fDetectors       = detectors ; 
1209         fDetectorsW      = detectors ;  
1210         
1211         AliCDBManager* man = AliCDBManager::Instance() ; 
1212         if ( man->GetRun() == -1 ) { // check if run number not set previously and set it from AliRun
1213                 AliRunLoader * rl = AliRunLoader::Open("galice.root") ;
1214                 if ( ! rl ) {
1215                         AliFatal("galice.root file not found in current directory") ; 
1216                 } else {
1217                         rl->CdGAFile() ; 
1218                         rl->LoadgAlice() ;
1219                         if ( ! rl->GetAliRun() ) {
1220                                 AliFatal("AliRun not found in galice.root") ;
1221                         } else {
1222                                 rl->LoadHeader() ;
1223                                 man->SetRun(rl->GetHeader()->GetRun());
1224                         }
1225                 }
1226         }
1227         
1228         if (!fCycleSame) 
1229     if ( !InitQA(AliQAv1::kRAWS, fileName) ) 
1230       return "" ; 
1231         
1232         DoIt(AliQAv1::kRAWS) ; 
1233         return  fDetectorsW ;
1234 }
1235
1236 //_____________________________________________________________________________
1237 TString AliQAManager::Run(const Char_t * detectors, const AliQAv1::TASKINDEX_t taskIndex, Bool_t const sameCycle, const  Char_t * fileName ) 
1238 {
1239         // Runs all the QA data Maker for every detector
1240         
1241         fCycleSame       = sameCycle ;
1242         fDetectors       = detectors ; 
1243         fDetectorsW      = detectors ;          
1244         
1245         AliCDBManager* man = AliCDBManager::Instance() ;        
1246         if ( man->GetRun() == -1 ) { // check if run number not set previously and set it from AliRun
1247                 AliRunLoader * rl = AliRunLoader::Open("galice.root") ;
1248                 if ( ! rl ) {
1249                         AliFatal("galice.root file not found in current directory") ; 
1250                 } else {
1251                         rl->CdGAFile() ; 
1252                         rl->LoadgAlice() ;
1253                         if ( ! rl->GetAliRun() ) {
1254                                 AliDebug(AliQAv1::GetQADebugLevel(), "AliRun not found in galice.root") ;
1255                         } else {
1256                                 rl->LoadHeader() ;
1257                                 man->SetRun(rl->GetHeader()->GetRun()) ;
1258                         }
1259                 }
1260         }
1261   if ( taskIndex == AliQAv1::kNULLTASKINDEX) { 
1262                 for (UInt_t task = 0; task < AliQAv1::kNTASKINDEX; task++) {
1263                         if ( fTasks.Contains(Form("%d", task)) ) {
1264         if (!fCycleSame)
1265           if ( !InitQA(AliQAv1::GetTaskIndex(AliQAv1::GetTaskName(task)), fileName) ) 
1266             return "" ;
1267         DoIt(AliQAv1::GetTaskIndex(AliQAv1::GetTaskName(task))) ;
1268                         }
1269                 }
1270         } else {
1271     if (! fCycleSame )
1272       if ( !InitQA(taskIndex, fileName) ) 
1273         return "" ; 
1274       DoIt(taskIndex) ; 
1275   }             
1276         return fDetectorsW ;
1277 }
1278
1279 //_____________________________________________________________________________
1280 void AliQAManager::RunOneEvent(AliRawReader * rawReader) 
1281 {
1282         //Runs all the QA data Maker for Raws only and on one event only (event loop done by calling method)
1283   if ( ! rawReader ) 
1284     return ; 
1285   if (fTasks.Contains(Form("%d", AliQAv1::kRAWS))){
1286     for (UInt_t iDet = 0; iDet < fgkNDetectors; iDet++) {
1287       if (!IsSelected(AliQAv1::GetDetName(iDet))) 
1288         continue;
1289       AliQADataMaker *qadm = GetQADataMaker(iDet);  
1290       if (!qadm) 
1291         continue;
1292       if ( qadm->IsCycleDone() ) {
1293         qadm->EndOfCycle() ;
1294       }
1295       qadm->SetEventSpecie(fEventSpecie) ;  
1296       if ( qadm->GetRecoParam() ) 
1297         if ( AliRecoParam::Convert(qadm->GetRecoParam()->GetEventSpecie()) != AliRecoParam::kDefault)  
1298           qadm->SetEventSpecie(qadm->GetRecoParam()->GetEventSpecie()) ; 
1299                         qadm->Exec(AliQAv1::kRAWS, rawReader) ;
1300                 }
1301   }
1302 }
1303
1304 //_____________________________________________________________________________
1305 void AliQAManager::RunOneEvent(AliESDEvent *& esd, AliESDEvent *& hltesd) 
1306 {
1307         //Runs all the QA data Maker for ESDs only and on one event only (event loop done by calling method)
1308         
1309   if (fTasks.Contains(Form("%d", AliQAv1::kESDS))) {
1310     for (UInt_t iDet = 0; iDet < fgkNDetectors; iDet++) {
1311       if (!IsSelected(AliQAv1::GetDetName(iDet))) 
1312         continue;
1313       AliQADataMaker *qadm = GetQADataMaker(iDet);  
1314       if (!qadm) 
1315         continue;
1316       qadm->SetEventSpecie(fEventSpecie) ;  
1317       if ( qadm->GetRecoParam() ) 
1318         if ( AliRecoParam::Convert(qadm->GetRecoParam()->GetEventSpecie()) != AliRecoParam::kDefault)  
1319           qadm->SetEventSpecie(qadm->GetRecoParam()->GetEventSpecie()) ; 
1320       if ( qadm->IsCycleDone() ) {
1321         qadm->EndOfCycle() ;
1322       }
1323       if (iDet == AliQAv1::kHLT) {
1324         TObjArray esdarray;
1325         esdarray.Add(esd); 
1326         esdarray.Add(hltesd); 
1327         qadm->Exec(AliQAv1::kESDS, &esdarray);
1328       } else {
1329         qadm->Exec(AliQAv1::kESDS, esd) ;        
1330       }
1331                 }
1332         }
1333 }
1334
1335 //_____________________________________________________________________________
1336 void AliQAManager::RunOneEventInOneDetector(Int_t det, TTree * tree) 
1337 {
1338         // Runs all the QA data Maker for ESDs only and on one event only (event loop done by calling method)
1339   
1340   TString test(tree->GetName()) ; 
1341   if (fTasks.Contains(Form("%d", AliQAv1::kRECPOINTS))) {
1342     if (IsSelected(AliQAv1::GetDetName(det))) {
1343       AliQADataMaker *qadm = GetQADataMaker(det);  
1344       if (qadm) { 
1345         qadm->SetEventSpecie(fEventSpecie) ;  
1346         if ( qadm->GetRecoParam() ) {
1347           if ( AliRecoParam::Convert(qadm->GetRecoParam()->GetEventSpecie()) != AliRecoParam::kDefault)  
1348             qadm->SetEventSpecie(qadm->GetRecoParam()->GetEventSpecie()) ; 
1349           else
1350             AliError(Form("%d defined by %s is not an event specie", qadm->GetRecoParam()->GetEventSpecie(), qadm->GetName())) ; 
1351         }                    
1352         if ( qadm->IsCycleDone() ) {
1353           qadm->EndOfCycle() ;
1354         }
1355         if (test.Contains("TreeD")) {
1356           qadm->Exec(AliQAv1::kDIGITSR, tree) ;
1357         } else  if (test.Contains("TreeR")) {
1358           qadm->Exec(AliQAv1::kRECPOINTS, tree) ;
1359         }
1360       }
1361     }
1362   }
1363 }
1364
1365 //_____________________________________________________________________________
1366 Bool_t AliQAManager::Save2OCDB(const Int_t runNumber, AliRecoParam::EventSpecie_t es, const Char_t * year, const Char_t * detectors) const
1367 {
1368         // take the locasl QA data merge into a single file and save in OCDB 
1369         Bool_t rv = kTRUE ; 
1370         TString tmp(AliQAv1::GetQARefStorage()) ; 
1371         if ( tmp.IsNull() ) { 
1372                 AliError("No storage defined, use AliQAv1::SetQARefStorage") ; 
1373                 return kFALSE ; 
1374         }
1375         if ( !(tmp.Contains(AliQAv1::GetLabLocalOCDB()) || tmp.Contains(AliQAv1::GetLabAliEnOCDB())) ) {
1376                 AliError(Form("%s is a wrong storage, use %s or %s", AliQAv1::GetQARefStorage(), AliQAv1::GetLabLocalOCDB().Data(), AliQAv1::GetLabAliEnOCDB().Data())) ; 
1377                 return kFALSE ; 
1378         }
1379         TString sdet(detectors) ; 
1380         sdet.ToUpper() ;
1381         TFile * inputFile ; 
1382         if ( sdet.Contains("ALL") ) {
1383                 rv = Merge(runNumber) ; 
1384                 if ( ! rv )
1385                         return kFALSE ; 
1386                 TString inputFileName(Form("Merged.%s.Data.%d.root", AliQAv1::GetQADataFileName(), runNumber)) ; 
1387                 inputFile = TFile::Open(inputFileName.Data()) ; 
1388                 rv = SaveIt2OCDB(runNumber, inputFile, year, es) ; 
1389         } else {
1390                 for (Int_t index = 0; index < AliQAv1::kNDET; index++) {
1391                         if (sdet.Contains(AliQAv1::GetDetName(index))) {
1392                                 TString inputFileName(Form("%s.%s.%d.root", AliQAv1::GetDetName(index), AliQAv1::GetQADataFileName(), runNumber)) ; 
1393                                 inputFile = TFile::Open(inputFileName.Data()) ;                         
1394                                 rv *= SaveIt2OCDB(runNumber, inputFile, year, es) ; 
1395                         }
1396                 }
1397         }
1398         return rv ; 
1399 }
1400
1401 //_____________________________________________________________________________
1402 Bool_t AliQAManager::SaveIt2OCDB(const Int_t runNumber, TFile * inputFile, const Char_t * year, AliRecoParam::EventSpecie_t es) const
1403 {
1404         // reads the TH1 from file and adds it to appropriate list before saving to OCDB
1405         Bool_t rv = kTRUE ;
1406         AliDebug(AliQAv1::GetQADebugLevel(), Form("Saving TH1s in %s to %s", inputFile->GetName(), AliQAv1::GetQARefStorage())) ; 
1407         if ( ! IsDefaultStorageSet() ) {
1408                 TString tmp( AliQAv1::GetQARefStorage() ) ; 
1409                 if ( tmp.Contains(AliQAv1::GetLabLocalOCDB()) ) 
1410                         Instance()->SetDefaultStorage(AliQAv1::GetQARefStorage()) ;
1411                 else {
1412                         TString tmp1(AliQAv1::GetQARefDefaultStorage()) ; 
1413                         tmp1.Append(year) ; 
1414                         tmp1.Append("?user=alidaq") ; 
1415                         Instance()->SetDefaultStorage(tmp1.Data()) ; 
1416                 }
1417         }
1418         Instance()->SetSpecificStorage("*", AliQAv1::GetQARefStorage()) ; 
1419         if(GetRun() < 0) 
1420                 Instance()->SetRun(runNumber);
1421
1422         AliCDBMetaData mdr ;
1423         mdr.SetResponsible("yves schutz");
1424
1425         for ( Int_t detIndex = 0 ; detIndex < AliQAv1::kNDET ; detIndex++) {
1426                 TDirectory * detDir = inputFile->GetDirectory(AliQAv1::GetDetName(detIndex)) ; 
1427                 if ( detDir ) {
1428                         AliDebug(AliQAv1::GetQADebugLevel(), Form("Entering %s", detDir->GetName())) ;
1429       AliQAv1::SetQARefDataDirName(es) ;
1430                         TString detOCDBDir(Form("%s/%s/%s", AliQAv1::GetDetName(detIndex), AliQAv1::GetRefOCDBDirName(), AliQAv1::GetRefDataDirName())) ; 
1431                         AliCDBId idr(detOCDBDir.Data(), runNumber, AliCDBRunRange::Infinity())  ;
1432                         TList * listDetQAD = new TList() ;
1433                         TString listName(Form("%s QA data Reference", AliQAv1::GetDetName(detIndex))) ; 
1434                         mdr.SetComment(Form("%s QA stuff", AliQAv1::GetDetName(detIndex)));
1435                         listDetQAD->SetName(listName) ; 
1436                         TList * taskList = detDir->GetListOfKeys() ; 
1437                         TIter nextTask(taskList) ; 
1438                         TKey * taskKey ; 
1439                         while ( (taskKey = static_cast<TKey*>(nextTask())) ) {
1440                                 TDirectory * taskDir = detDir->GetDirectory(taskKey->GetName()) ; 
1441         TDirectory * esDir   = taskDir->GetDirectory(AliRecoParam::GetEventSpecieName(es)) ; 
1442                                 AliDebug(AliQAv1::GetQADebugLevel(), Form("Saving %s", esDir->GetName())) ; 
1443                                 TObjArray * listTaskQAD = new TObjArray(100) ; 
1444                                 listTaskQAD->SetName(Form("%s/%s", taskKey->GetName(), AliRecoParam::GetEventSpecieName(es))) ;
1445                                 listDetQAD->Add(listTaskQAD) ; 
1446                                 TList * histList = esDir->GetListOfKeys() ; 
1447                                 TIter nextHist(histList) ; 
1448                                 TKey * histKey ; 
1449                                 while ( (histKey = static_cast<TKey*>(nextHist())) ) {
1450                                         TObject * odata = esDir->Get(histKey->GetName()) ; 
1451                                         if ( !odata ) {
1452                                                 AliError(Form("%s in %s/%s returns a NULL pointer !!", histKey->GetName(), detDir->GetName(), taskDir->GetName())) ;
1453                                         } else {
1454             if ( AliQAv1::GetExpert() == histKey->GetName() ) {
1455               TDirectory * expertDir   = esDir->GetDirectory(histKey->GetName()) ; 
1456               TList * expertHistList = expertDir->GetListOfKeys() ; 
1457               TIter nextExpertHist(expertHistList) ; 
1458               TKey * expertHistKey ; 
1459               while ( (expertHistKey = static_cast<TKey*>(nextExpertHist())) ) {
1460                 TObject * expertOdata = expertDir->Get(expertHistKey->GetName()) ; 
1461                 if ( !expertOdata ) {
1462                   AliError(Form("%s in %s/%s/Expert returns a NULL pointer !!", expertHistKey->GetName(), detDir->GetName(), taskDir->GetName())) ;
1463                 } else {
1464                   AliDebug(AliQAv1::GetQADebugLevel(), Form("Adding %s", expertHistKey->GetName())) ;
1465                   if ( expertOdata->IsA()->InheritsFrom("TH1") ) {
1466                     AliDebug(AliQAv1::GetQADebugLevel(), Form("Adding %s", expertHistKey->GetName())) ;
1467                     TH1 * hExpertdata = static_cast<TH1*>(expertOdata) ; 
1468                     listTaskQAD->Add(hExpertdata) ; 
1469                   }                  
1470                 }                
1471               }
1472             }
1473                                                 AliDebug(AliQAv1::GetQADebugLevel(), Form("Adding %s", histKey->GetName())) ;
1474                                                 if ( odata->IsA()->InheritsFrom("TH1") ) {
1475                                                         AliDebug(AliQAv1::GetQADebugLevel(), Form("Adding %s", histKey->GetName())) ;
1476                                                         TH1 * hdata = static_cast<TH1*>(odata) ; 
1477                                                         listTaskQAD->Add(hdata) ; 
1478                                                 }
1479                                         }
1480                                 }
1481                         }
1482                         Instance()->Put(listDetQAD, idr, &mdr) ;
1483                 }
1484         }
1485         return rv ; 
1486 }       
1487
1488 //_____________________________________________________________________________
1489
1490 void AliQAManager::SetCheckerExternParam(AliQAv1::DETECTORINDEX_t detIndex, TList * parameterList) 
1491 {
1492   // set the external parameters list for the detector checkers 
1493   AliQACheckerBase * qac = AliQAChecker::Instance()->GetDetQAChecker(detIndex) ; 
1494   qac->SetExternParamlist(parameterList) ; 
1495   qac->PrintExternParam() ;  
1496 }
1497
1498 //_____________________________________________________________________________
1499 void AliQAManager::SetEventSpecie(AliRecoParam::EventSpecie_t es) 
1500 {
1501   // set the current event specie and inform AliQAv1 that this event specie has been encountered
1502   fEventSpecie = es ; 
1503   AliQAv1::Instance()->SetEventSpecie(es) ; 
1504 }
1505
1506 //_____________________________________________________________________________
1507 void AliQAManager::SetRecoParam(const Int_t det, const AliDetectorRecoParam *par) 
1508 {
1509   // Set custom reconstruction parameters for a given detector
1510   // Single set of parameters for all the events
1511   GetQADataMaker(det)->SetRecoParam(par) ; 
1512 }
1513
1514 //_____________________________________________________________________________
1515 void AliQAManager::SetWriteExpert()
1516 {
1517   // enable the writing of QA expert data
1518   for (UInt_t iDet = 0; iDet < fgkNDetectors; iDet++) {
1519         if (IsSelected(AliQAv1::GetDetName(iDet))) 
1520       fQAWriteExpert[iDet] = kTRUE ;
1521   }
1522 }  
1523
1524 //_____________________________________________________________________________
1525 void AliQAManager::Destroy() {
1526   // delete AliQAManager instance and
1527   // all associated objects
1528
1529   if (fgQAInstance) {
1530     delete fgQAInstance ;
1531     fgQAInstance = NULL ;
1532   }
1533 }
1534
1535 //_____________________________________________________________________________
1536 void AliQAManager::ShowQA() {
1537   // Show the result of the QA checking
1538   // for all detectors 
1539         for ( Int_t detIndex = 0 ; detIndex < AliQAv1::kNDET ; detIndex++) 
1540     AliQAv1::Instance(AliQAv1::GetDetIndex(AliQAv1::GetDetName(detIndex)))->Show() ; 
1541 }