]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliQAManager.cxx
Protection added in case no ESD files have been chained.
[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) { //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(), "Rec") == 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     
483     // load the QA data maker object
484     TPluginManager* pluginManager = gROOT->GetPluginManager() ;
485     TString detName = AliQAv1::GetDetName(iDet) ;
486     TString qadmName = "Ali" + detName + "QADataMaker" + GetMode() ;
487     
488     // first check if a plugin is defined for the quality assurance data maker
489     TPluginHandler* pluginHandler = pluginManager->FindHandler("AliQADataMaker", detName) ;
490     // if not, add a plugin for it
491     if (!pluginHandler) {
492       AliDebug(AliQAv1::GetQADebugLevel(), Form("defining plugin for %s", qadmName.Data())) ;
493       TString libs = gSystem->GetLibraries() ;
494       TString temp(GetMode()) ;
495       temp.ToLower() ; 
496       if (libs.Contains("lib" + detName + GetMode() + ".so") || (gSystem->Load("lib" + detName + temp.Data() + ".so") >= 0)) {
497         pluginManager->AddHandler("AliQADataMaker", detName, qadmName, detName + "qadm", qadmName + "()") ;
498       } else {
499         pluginManager->AddHandler("AliQADataMaker", detName, qadmName, detName, qadmName + "()") ;
500       }
501       pluginHandler = pluginManager->FindHandler("AliQADataMaker", detName) ;
502     }
503     if (pluginHandler && (pluginHandler->LoadPlugin() == 0)) {
504       qadm = (AliQADataMaker *) pluginHandler->ExecPlugin(0) ;
505     }
506     if (qadm) {
507       qadm->SetName(AliQAv1::GetDetName(iDet));
508       qadm->SetUniqueID(iDet);
509       fQADataMaker[iDet] = qadm ;
510       qadm->SetEventSpecie(fEventSpecie) ;  
511       if ( qadm->GetRecoParam() ) 
512         if ( AliRecoParam::Convert(qadm->GetRecoParam()->GetEventSpecie()) != AliRecoParam::kDefault)  
513           qadm->SetEventSpecie(qadm->GetRecoParam()->GetEventSpecie()) ; 
514     }
515   }
516   return qadm ;
517 }
518
519 //_____________________________________________________________________________
520 void  AliQAManager::EndOfCycle(TObjArray * detArray) 
521 {
522         // End of cycle QADataMakers 
523         
524   AliQAChecker::Instance()->SetRunNumber(fRunNumber) ; 
525   TCanvas fakeCanvas ; 
526
527     fakeCanvas.Print(Form("%s%s%d.%s[", AliQAv1::GetImageFileName(), GetMode(), fRunNumber, AliQAv1::GetImageFileFormat()), "ps") ; 
528         for (UInt_t iDet = 0; iDet < fgkNDetectors ; iDet++) {
529                 if (IsSelected(AliQAv1::GetDetName(iDet))) {
530                         AliQADataMaker * qadm = GetQADataMaker(iDet) ;
531                         if (!qadm) 
532                                 continue ;      
533                         // skip non active detectors
534                         if (detArray) {
535                                 AliModule* det = static_cast<AliModule*>(detArray->FindObject(AliQAv1::GetDetName(iDet))) ;
536                                 if (!det || !det->IsActive())  
537                                         continue ;
538                         }
539                         AliQACheckerBase * qac = AliQAChecker::Instance()->GetDetQAChecker(iDet) ;
540       if (qac) 
541         qac->SetPrintImage(fPrintImage) ;
542       for (UInt_t taskIndex = 0; taskIndex < AliQAv1::kNTASKINDEX; taskIndex++) {
543         if ( fTasks.Contains(Form("%d", taskIndex)) ) 
544           qadm->EndOfCycle(AliQAv1::GetTaskIndex(AliQAv1::GetTaskName(taskIndex))) ;
545       }
546                         qadm->Finish();
547                 }
548         }
549   if (fPrintImage) 
550     fakeCanvas.Print(Form("%s%s%d.%s]", AliQAv1::GetImageFileName(), GetMode(), fRunNumber, AliQAv1::GetImageFileFormat()), "ps"); 
551 }
552
553 //_____________________________________________________________________________
554 void  AliQAManager::EndOfCycle(TString detectors) 
555 {
556         // End of cycle QADataMakers 
557         
558   AliQAChecker::Instance()->SetRunNumber(fRunNumber) ; 
559   TCanvas fakeCanvas ; 
560   if (fPrintImage) 
561     fakeCanvas.Print(Form("%s%s%d.%s[", AliQAv1::GetImageFileName(), GetMode(), fRunNumber, AliQAv1::GetImageFileFormat()), "ps") ; 
562   for (UInt_t iDet = 0; iDet < fgkNDetectors ; iDet++) {
563                 if (IsSelected(AliQAv1::GetDetName(iDet))) {
564                         AliQADataMaker * qadm = GetQADataMaker(iDet) ;
565                         if (!qadm) 
566                                 continue ;      
567                         // skip non active detectors
568       if (!detectors.Contains(AliQAv1::GetDetName(iDet))) 
569         continue ;
570                         AliQACheckerBase * qac = AliQAChecker::Instance()->GetDetQAChecker(iDet) ;
571       if (qac) 
572         qac->SetPrintImage(fPrintImage) ;
573       for (UInt_t taskIndex = 0; taskIndex < AliQAv1::kNTASKINDEX; taskIndex++) {
574         if ( fTasks.Contains(Form("%d", taskIndex)) ) 
575           qadm->EndOfCycle(AliQAv1::GetTaskIndex(AliQAv1::GetTaskName(taskIndex))) ;
576       }
577                         qadm->Finish();
578                 }
579         }
580   if (fPrintImage) 
581     fakeCanvas.Print(Form("%s%s%d.%s]", AliQAv1::GetImageFileName(), GetMode(), fRunNumber, AliQAv1::GetImageFileFormat()), "ps"); 
582 }
583
584 //_____________________________________________________________________________
585 void AliQAManager::Increment(const AliQAv1::TASKINDEX_t taskIndex)
586 {
587   // Increments the cycle counter for all QA Data Makers
588   static AliQAv1::TASKINDEX_t currentTask = AliQAv1::kNTASKINDEX ; 
589   if (currentTask == taskIndex) 
590     return ; 
591   else 
592     currentTask = taskIndex ; 
593         for (UInt_t iDet = 0; iDet < fgkNDetectors ; iDet++) {
594                 if (IsSelected(AliQAv1::GetDetName(iDet))) {
595                         AliQADataMaker * qadm = GetQADataMaker(iDet) ;
596                         if (qadm) 
597         qadm->Increment() ;
598     }
599   }
600 }
601   
602 //_____________________________________________________________________________
603 Bool_t AliQAManager::InitQA(const AliQAv1::TASKINDEX_t taskIndex, const  Char_t * input )
604 {
605         // Initialize the event source and QA data makers
606         
607         fTasks += Form("%d", taskIndex) ; 
608
609         if (taskIndex == AliQAv1::kRAWS) { 
610                 if (!fRawReader) {
611                         fRawReader = AliRawReader::Create(input);
612                 }
613                 if ( ! fRawReader ) 
614                         return kFALSE ; 
615                 fRawReaderDelete = kTRUE ; 
616                 fRawReader->NextEvent() ; 
617                 fRunNumber = fRawReader->GetRunNumber() ; 
618                 SetRun(fRunNumber) ; 
619                 fRawReader->RewindEvents();
620                 fNumberOfEvents = 999999 ;
621                 if ( fMaxEvents < 0 ) 
622                         fMaxEvents = fNumberOfEvents ; 
623                 } else if (taskIndex == AliQAv1::kESDS) {
624                         fTasks = AliQAv1::GetTaskName(AliQAv1::kESDS) ; 
625       if (!gSystem->AccessPathName("AliESDs.root")) { // AliESDs.root exists
626         TFile * esdFile = TFile::Open("AliESDs.root") ;
627         fESDTree = static_cast<TTree *> (esdFile->Get("esdTree")) ; 
628         if ( !fESDTree ) {
629           AliError("esdTree not found") ; 
630           return kFALSE ; 
631         } else {
632           fESD     = new AliESDEvent() ;
633           fESD->ReadFromTree(fESDTree) ;
634           fESDTree->GetEntry(0) ; 
635           fRunNumber = fESD->GetRunNumber() ; 
636           fNumberOfEvents = fESDTree->GetEntries() ;
637           if ( fMaxEvents < 0 ) 
638             fMaxEvents = fNumberOfEvents ; 
639         }
640       } else {
641         AliError("AliESDs.root not found") ; 
642         return kFALSE ; 
643       }                 
644     } else {
645       if ( !InitRunLoader() ) { 
646         AliWarning("No Run Loader not found") ; 
647       } else {
648         fNumberOfEvents = fRunLoader->GetNumberOfEvents() ;
649         if ( fMaxEvents < 0 ) 
650           fMaxEvents = fNumberOfEvents ; 
651       }
652     }
653
654   // Get Detectors 
655   TObjArray* detArray = NULL ; 
656         if (fRunLoader) // check if RunLoader exists 
657                 if ( fRunLoader->GetAliRun() ) { // check if AliRun exists in gAlice.root
658                         detArray = fRunLoader->GetAliRun()->Detectors() ;
659                         fRunNumber = fRunLoader->GetHeader()->GetRun() ; 
660                 }
661
662         // Initialize all QA data makers for all detectors
663         fRunNumber = AliCDBManager::Instance()->GetRun() ; 
664         if ( !  AliGeomManager::GetGeometry() ) 
665                 AliGeomManager::LoadGeometry() ; 
666         
667         InitQADataMaker(fRunNumber, detArray) ; //, fCycleSame, kTRUE, detArray) ; 
668   if (fPrintImage) {
669     TCanvas fakeCanvas ; 
670     TStopwatch timer ; 
671     timer.Start() ; 
672     while (timer.CpuTime()<5) {
673       timer.Continue();
674       gSystem->ProcessEvents();
675     }
676     fakeCanvas.Print(Form("%s%s%d.%s[", AliQAv1::GetImageFileName(), GetMode(), fRunNumber, AliQAv1::GetImageFileFormat()), "ps") ;    
677   }    
678         return kTRUE ; 
679 }
680
681 //_____________________________________________________________________________
682 void  AliQAManager::InitQADataMaker(UInt_t run, TObjArray * detArray) 
683 {
684         // Initializes The QADataMaker for all active detectors and for all active tasks 
685   fRunNumber = run ; 
686         for (UInt_t iDet = 0; iDet < fgkNDetectors ; iDet++) {
687                 if (IsSelected(AliQAv1::GetDetName(iDet))) {
688                         AliQADataMaker * qadm = GetQADataMaker(iDet) ;
689                         if (!qadm) {
690                                 AliError(Form("AliQADataMaker not found for %s", AliQAv1::GetDetName(iDet))) ; 
691                                 fDetectorsW.ReplaceAll(AliQAv1::GetDetName(iDet), "") ; 
692                         } else {
693         if (fQAWriteExpert[iDet])
694           qadm->SetWriteExpert() ; 
695                                 AliDebug(AliQAv1::GetQADebugLevel(), Form("Data Maker found for %s %d", qadm->GetName(), qadm->WriteExpert())) ; 
696                                 // skip non active detectors
697                                 if (detArray) {
698                                         AliModule* det = static_cast<AliModule*>(detArray->FindObject(AliQAv1::GetDetName(iDet))) ;
699                                         if (!det || !det->IsActive())  
700                                                 continue ;
701                                 }
702               // Set default reco params
703         Bool_t sameCycle = kFALSE ; 
704                                 for (UInt_t taskIndex = 0; taskIndex < AliQAv1::kNTASKINDEX; taskIndex++) {
705                                         if ( fTasks.Contains(Form("%d", taskIndex)) ) {
706                                                 qadm->Init(AliQAv1::GetTaskIndex(AliQAv1::GetTaskName(taskIndex)), GetQACycles(qadm->GetUniqueID())) ;
707             qadm->StartOfCycle(AliQAv1::GetTaskIndex(AliQAv1::GetTaskName(taskIndex)), run,  sameCycle) ;
708             sameCycle = kTRUE ;
709                                         }
710                                 }
711                         }
712                 }
713         }
714 }
715
716
717 //_____________________________________________________________________________
718 Bool_t AliQAManager::InitRunLoader()
719 {
720         // get or create the run loader
721         if (fRunLoader) {
722                 fCycleSame = kTRUE ; 
723         } else {
724                 if (!gSystem->AccessPathName(fGAliceFileName.Data())) { // galice.root exists
725                         // load all base libraries to get the loader classes
726                         TString libs = gSystem->GetLibraries() ;
727                         for (UInt_t iDet = 0; iDet < fgkNDetectors; iDet++) {
728                                 if (!IsSelected(AliQAv1::GetDetName(iDet))) 
729                                         continue ; 
730                                 TString detName = AliQAv1::GetDetName(iDet) ;
731                                 if (detName == "HLT") 
732                                         continue;
733                                 if (libs.Contains("lib" + detName + "base.so")) 
734                                         continue;
735                                 gSystem->Load("lib" + detName + "base.so");
736                         }
737                         fRunLoader = AliRunLoader::Open(fGAliceFileName.Data());
738                         if (!fRunLoader) {
739                                 AliError(Form("no run loader found in file %s", fGAliceFileName.Data()));
740                                 return kFALSE;
741                         }
742                         fRunLoader->CdGAFile();
743                         if (fRunLoader->LoadgAlice() == 0) {
744                                 gAlice = fRunLoader->GetAliRun();
745                         }
746
747                         if (!gAlice) {
748                                 AliError(Form("no gAlice object found in file %s", fGAliceFileName.Data()));
749                                 return kFALSE;
750                         }
751
752                 } else {               // galice.root does not exist
753                         AliError(Form("the file %s does not exist", fGAliceFileName.Data()));
754                         return kFALSE;
755                 }
756         }
757
758         if (!fRunNumber) { 
759                 fRunLoader->LoadHeader();
760                 fRunNumber = fRunLoader->GetHeader()->GetRun() ; 
761         }
762         return kTRUE;
763 }
764
765 //_____________________________________________________________________________
766 Bool_t AliQAManager::IsSelected(const Char_t * det) 
767 {
768   // check whether detName is contained in detectors
769         // if yes, it is removed from detectors
770         
771         Bool_t rv = kFALSE;
772         const TString detName(det) ;
773   // always activates Correlation
774   if ( detName.Contains(AliQAv1::GetDetName(AliQAv1::kCORR)) || detName.Contains(AliQAv1::GetDetName(AliQAv1::kGLOBAL))) {
775     rv = kTRUE ; 
776   } else {
777     // check if all detectors are selected
778     if (fDetectors.Contains("ALL")) {
779       fDetectors = "ALL";
780       rv = kTRUE;
781     } else if ((fDetectors.CompareTo(detName) == 0) ||
782                fDetectors.BeginsWith(detName+" ") ||
783                fDetectors.EndsWith(" "+detName) ||
784                fDetectors.Contains(" "+detName+" ")) {
785       rv = kTRUE;
786     }
787   }
788         return rv ;
789 }
790
791 //_____________________________________________________________________________
792 Bool_t AliQAManager::Merge(Int_t runNumber, const char *fileName) const
793 {
794         // Merge data from all detectors from a given run in one single file 
795         // Merge the QA results from all the data chunks in one run
796   // The 'fileName' is name of the output file with merged QA data  
797  if ( runNumber == -1)
798    runNumber = fRunNumber ; 
799  Bool_t rv = MergeData(runNumber,fileName) ; 
800  //rv *= MergeResults(runNumber) ; // not needed for the time being
801  return rv ; 
802 }
803         
804 //______________________________________________________________________
805 Bool_t AliQAManager::MergeXML(const Char_t * collectionFile, const Char_t * subFile, const Char_t * outFile) 
806 {
807   // merges files listed in a xml collection 
808   // usage Merge(collection, outputFile))
809   //              collection: is a xml collection  
810   
811   Bool_t rv = kFALSE ; 
812   
813   if ( strstr(collectionFile, ".xml") == 0 ) {
814     AliError("Input collection file must be an \".xml\" file\n") ; 
815     return kFALSE ; 
816   }
817     
818  if ( !gGrid ) 
819    TGrid::Connect("alien://"); 
820  if ( !gGrid ) 
821    return kFALSE ; 
822  
823   // Open the file collection 
824   AliInfoClass(Form("*** Create Collection       ***\n***  Wk-Dir = |%s|             \n***  Coll   = |%s|             \n",gSystem->WorkingDirectory(), collectionFile));                
825   
826   TGridCollection * collection = (TGridCollection*)gROOT->ProcessLine(Form("TAlienCollection::Open(\"%s\")",collectionFile));
827   TGridResult* result = collection->GetGridResult("", 0, 0);
828   
829   Int_t index = 0  ;
830   const Char_t * turl ;
831   TFileMerger merger(kFALSE) ; 
832   if (!outFile) {
833     TString tempo(collectionFile) ; 
834     if ( subFile) 
835       tempo.ReplaceAll(".xml", subFile) ; 
836     else 
837       tempo.ReplaceAll(".xml", "_Merged.root") ; 
838     outFile = tempo.Data() ; 
839   }
840   merger.OutputFile(outFile) ; 
841   
842   while ( (turl = result->GetKey(index, "turl")) ) {
843     Char_t * file ;
844     if ( subFile )
845       file = Form("%s#%s", turl, subFile) ; 
846     else 
847       file = Form("%s", turl) ; 
848     
849     AliDebug(AliQAv1::GetQADebugLevel(), Form("%s\n", file)) ; 
850     merger.AddFile(file) ; 
851     index++ ;  
852   }
853   
854   if (index) 
855     merger.Merge() ; 
856   
857   AliDebug(AliQAv1::GetQADebugLevel(), Form("Files merged into %s\n", outFile)) ;
858   
859   rv = kFALSE;
860   return rv ;
861 }
862
863 //_____________________________________________________________________________
864 void AliQAManager::MergeCustom() const
865 {
866         // Custom Merge of QA data from all detectors for all runs in one single file 
867   // search all the run numbers
868   // search all the run numbers
869   gROOT->ProcessLine(".! ls *QA*.root > QAtempo.txt") ;
870   TString QAfile ; 
871   FILE * QAfiles = fopen("QAtempo.txt", "r") ; 
872   Int_t index = 0 ; 
873   TList srunList ; 
874   TIter nextRun(&srunList) ; 
875   TObjString * srun = NULL ; 
876   Int_t loRun = 999999999 ; 
877   Int_t hiRun = 0 ; 
878   while ( QAfile.Gets(QAfiles) ) {
879     Bool_t runExist = kFALSE ; 
880     TString srunNew(QAfile(QAfile.Index("QA.")+3, QAfile.Index(".root")-(QAfile.Index("QA.")+3))) ; 
881     Int_t cuRun = srunNew.Atoi() ;
882     if (cuRun < loRun) 
883       loRun = cuRun ; 
884     if (cuRun > hiRun)
885       hiRun = cuRun ; 
886     while ( (srun = static_cast<TObjString *> (nextRun())) ) {
887       if ( cuRun == (srun->String()).Atoi() ) {
888         runExist = kTRUE ; 
889         break ; 
890       } 
891     }
892     nextRun.Reset() ; 
893     if ( ! runExist ) 
894       srunList.Add(new TObjString(srunNew.Data()));
895   }
896   nextRun.Reset() ;    
897   Int_t runNumber = 0 ; 
898   TFile mergedFile(Form("Merged.%s.Data.root", AliQAv1::GetQADataFileName()), "RECREATE") ; 
899   TH1I * hisRun = new TH1I("hLMR", "List of merged runs", hiRun-loRun+10, loRun, hiRun+10) ; 
900   // create the structure into the merged file
901   for (Int_t iDet = 0; iDet < AliQAv1::kNDET ; iDet++) {
902     TDirectory * detDir = mergedFile.mkdir(AliQAv1::GetDetName(iDet)) ; 
903     for (Int_t taskIndex = 0; taskIndex < AliQAv1::kNTASKINDEX; taskIndex++) {
904       detDir->cd() ; 
905       TDirectory * taskDir = gDirectory->mkdir(AliQAv1::GetTaskName(taskIndex)) ; 
906       for (Int_t es = 0 ; es < AliRecoParam::kNSpecies ; es++) {
907         taskDir->cd() ; 
908         TDirectory * esDir = gDirectory->mkdir(AliRecoParam::GetEventSpecieName(es)) ;
909         esDir->cd() ; 
910         gDirectory->mkdir(AliQAv1::GetExpert()) ; 
911       }
912     }
913   }
914   while ( (srun = static_cast<TObjString *> (nextRun())) ) {
915     runNumber = (srun->String()).Atoi() ; 
916     hisRun->Fill(runNumber) ; 
917     AliDebug(AliQAv1::GetQADebugLevel(), Form("Merging run number %d", runNumber)) ; 
918     // search all QA files for runNumber in the current directory
919     Char_t * fileList[AliQAv1::kNDET] ;
920     index = 0 ; 
921     for (Int_t iDet = 0; iDet < AliQAv1::kNDET ; iDet++) {
922       Char_t * file = gSystem->Which(gSystem->WorkingDirectory(), Form("%s.%s.%d.root", AliQAv1::GetDetName(iDet), AliQAv1::GetQADataFileName(), runNumber)); 
923       if (file) 
924         fileList[index++] = file ;
925     }
926     if ( index == 0 ) {
927       AliError("No QA data file found\n") ; 
928       return ; 
929     }
930     for ( Int_t i = 0 ; i < index ; i++) {
931       TFile * inFile = TFile::Open(fileList[i]) ;  
932       TList * listOfKeys =inFile->GetListOfKeys() ; 
933       TIter nextkey(listOfKeys) ; 
934       TObject * obj1 ; 
935       TString dirName("") ; 
936       while ( (obj1 = nextkey()) ) {
937         TDirectory * directoryDet = inFile->GetDirectory(obj1->GetName()) ; 
938         if ( directoryDet ) {
939           AliDebug(AliQAv1::GetQADebugLevel(), Form("%s dir = %s", inFile->GetName(), directoryDet->GetName())) ; 
940           dirName += Form("%s/", directoryDet->GetName() ) ; 
941           directoryDet->cd() ;
942           TList * listOfTasks = directoryDet->GetListOfKeys() ; 
943           TIter nextTask(listOfTasks) ; 
944           TObject * obj2 ; 
945           while ( (obj2 = nextTask()) ) {
946             TDirectory * directoryTask = directoryDet->GetDirectory(obj2->GetName()) ; 
947             if ( directoryTask ) {
948               dirName += Form("%s", obj2->GetName()) ; 
949               AliDebug(AliQAv1::GetQADebugLevel(), Form("%s", dirName.Data())) ; 
950               directoryTask->cd() ; 
951               TList * listOfEventSpecie = directoryTask->GetListOfKeys() ; 
952               TIter nextEventSpecie(listOfEventSpecie) ; 
953               TObject * obj3 ; 
954               while ( (obj3 = nextEventSpecie()) ) {
955                 TDirectory * directoryEventSpecie = directoryTask->GetDirectory(obj3->GetName()) ; 
956                 if ( directoryEventSpecie ) {
957                   dirName += Form("/%s/", obj3->GetName()) ; 
958                   AliDebug(AliQAv1::GetQADebugLevel(), Form("%s\n", dirName.Data())) ; 
959                   directoryEventSpecie->cd() ; 
960                   // histograms are here
961                   TDirectory * mergedDirectory = mergedFile.GetDirectory(dirName.Data()) ;
962                   TList * listOfData = directoryEventSpecie->GetListOfKeys() ; 
963                   TIter nextData(listOfData) ; 
964                   TKey * key ; 
965                   while ( (key = static_cast<TKey *>(nextData())) ) {
966                     TString className(key->GetClassName()) ; 
967                     if (  className.Contains("TH") || className.Contains("TProfile") ) {
968                       TH1 * histIn = static_cast<TH1*> (key->ReadObj()) ; 
969                       TH1 * histOu = static_cast<TH1*> (mergedDirectory->FindObjectAny(histIn->GetName())) ; 
970                       AliDebug(AliQAv1::GetQADebugLevel(), Form("%s %x %x\n", key->GetName(), histIn, histOu)) ; 
971                       mergedDirectory->cd() ; 
972                       if ( ! histOu ) {
973                         histIn->Write() ; 
974                       } else {
975                         histOu->Add(histIn) ; 
976                         histOu->Write(histOu->GetName(), kOverwrite) ; 
977                       }
978                     }
979                     else if ( className.Contains("TDirectoryFile") ) {
980                       TDirectory * dirExpert = directoryEventSpecie->GetDirectory(key->GetName()) ; 
981                       dirExpert->cd() ; 
982                       TDirectory * mergedDirectoryExpert = mergedDirectory->GetDirectory(dirExpert->GetName()) ; 
983                       TList * listOfExpertData = dirExpert->GetListOfKeys() ; 
984                       TIter nextExpertData(listOfExpertData) ; 
985                       TKey * keykey ; 
986                       while ( (keykey = static_cast<TKey *>(nextExpertData())) ) {
987                         TString classNameExpert(keykey->GetClassName()) ; 
988                         if (classNameExpert.Contains("TH")) {
989                           TH1 * histInExpert = static_cast<TH1*> (keykey->ReadObj()) ; 
990                           TH1 * histOuExpert = static_cast<TH1*> (mergedDirectory->FindObjectAny(histInExpert->GetName())) ; 
991                           mergedDirectoryExpert->cd() ; 
992                           if ( ! histOuExpert ) {
993                             histInExpert->Write() ; 
994                           } else {
995                             histOuExpert->Add(histInExpert) ; 
996                             histOuExpert->Write(histOuExpert->GetName(), kOverwrite) ; 
997                           }
998                         }
999                       }
1000                     } else {
1001                       AliError(Form("No merge done for this object %s in %s", key->GetName(), dirName.Data())) ; 
1002                     }
1003                   }
1004                   dirName.ReplaceAll(Form("/%s/",obj3->GetName()), "") ; 
1005                 }
1006               }
1007               dirName.ReplaceAll(obj2->GetName(), "") ; 
1008             }
1009           }
1010         }
1011       }
1012       inFile->Close() ; 
1013     }
1014   }
1015   mergedFile.cd() ;
1016   hisRun->Write() ; 
1017   mergedFile.Close() ; 
1018   srunList.Delete() ;   
1019 }
1020
1021 //_____________________________________________________________________________
1022 Bool_t AliQAManager::MergeData(const Int_t runNumber, const char *fileName) const
1023 {
1024         // Merge QA data from all detectors for a given run in one single file 
1025   
1026   TFileMerger merger(kFALSE) ; 
1027   TString outFileName = fileName;
1028   if (outFileName.IsNull()) outFileName.Form("Merged.%s.Data.root",AliQAv1::GetQADataFileName());
1029   merger.OutputFile(outFileName.Data()) ; 
1030   for (UInt_t iDet = 0; iDet < fgkNDetectors ; iDet++) {
1031     Char_t * file = gSystem->Which(gSystem->WorkingDirectory(), Form("%s.%s.%d.root", AliQAv1::GetDetName(iDet), AliQAv1::GetQADataFileName(), runNumber)); 
1032     if (file) 
1033       merger.AddFile(file) ; 
1034   }
1035   merger.Merge() ; 
1036         return kTRUE ; 
1037 }
1038
1039 //_____________________________________________________________________________
1040 Bool_t AliQAManager::MergeResults(const Int_t runNumber) const
1041 {
1042         // Merge the QA result from all the data chunks in a run 
1043   // to be revised whwn it will be used (see MergeData)
1044         TString cmd ;
1045         cmd = Form(".! ls %s*.root > tempo.txt", AliQAv1::GetQADataFileName()) ; 
1046         gROOT->ProcessLine(cmd.Data()) ;
1047         ifstream in("tempo.txt") ; 
1048         const Int_t chunkMax = 100 ;  
1049         TString fileList[chunkMax] ;
1050         
1051         Int_t index = 0 ; 
1052         while ( 1 ) {
1053                 TString file ; 
1054                 in >> fileList[index] ; 
1055                 if ( !in.good() ) 
1056                         break ; 
1057                 AliDebug(AliQAv1::GetQADebugLevel(), Form("index = %d file = %s", index, (fileList[index].Data()))) ; 
1058                 index++ ;
1059         }
1060         
1061         if ( index == 0 ) { 
1062                 AliError("No QA Result File found") ; 
1063                 return kFALSE ; 
1064         }
1065         
1066         TFileMerger merger ; 
1067   TString outFileName ;
1068   if (runNumber != -1) 
1069     outFileName = Form("Merged.%s.Result.%d.root",AliQAv1::GetQADataFileName(),runNumber); 
1070   else 
1071     outFileName = Form("Merged.%s.Result.root",AliQAv1::GetQADataFileName()); 
1072         merger.OutputFile(outFileName.Data()) ; 
1073         for (Int_t ifile = 0 ; ifile < index ; ifile++) {
1074                 TString file = fileList[ifile] ; 
1075                 merger.AddFile(file) ; 
1076         }
1077         merger.Merge() ; 
1078         
1079         return kTRUE ; 
1080 }
1081
1082 //_____________________________________________________________________________
1083 void AliQAManager::Reset(const Bool_t sameCycle)
1084 {
1085         // Reset the default data members
1086
1087         for (UInt_t iDet = 0; iDet < fgkNDetectors ; iDet++) {
1088                 if (IsSelected(AliQAv1::GetDetName(iDet))) {
1089                         AliQADataMaker * qadm = GetQADataMaker(iDet);
1090                         qadm->Reset();
1091                 }
1092         } 
1093         if (fRawReaderDelete) { 
1094                 delete fRawReader ;
1095                 fRawReader      = NULL ;
1096         }
1097
1098         fCycleSame      = sameCycle ; 
1099         fESD            = NULL ; 
1100         fESDTree        = NULL ; 
1101         //fFirst          = kTRUE ;   
1102         fNumberOfEvents = 999999 ;  
1103 }
1104
1105 //_____________________________________________________________________________
1106 void AliQAManager::ResetDetectors(AliQAv1::TASKINDEX_t task, AliQAv1::DETECTORINDEX_t det)
1107 {
1108     //calls ResetDetector of specified or all detectors
1109   UInt_t iDet    = 0 ;
1110   UInt_t iDetMax = fgkNDetectors ;    
1111   if ( det != AliQAv1::kNULLDET ) {
1112     iDet    = det ;
1113     iDetMax = det+1 ;    
1114   }
1115   
1116         for (iDet = 0; iDet < iDetMax ; iDet++) {
1117                 if (IsSelected(AliQAv1::GetDetName(iDet))) {
1118                         AliQADataMaker * qadm = GetQADataMaker(iDet);
1119                         qadm->ResetDetector(task);
1120                 }
1121         }   
1122 }
1123
1124 //_____________________________________________________________________________
1125 AliQAManager * AliQAManager::QAManager(AliQAv1::MODE_t mode, TMap *entryCache, Int_t run) 
1126 {
1127   // returns AliQAManager instance (singleton)
1128   
1129         if (!fgQAInstance) {
1130     if ( (mode != AliQAv1::kSIMMODE) && (mode != AliQAv1::kRECMODE) ) {
1131       AliWarningClass("You must specify kSIMMODE or kRECMODE") ; 
1132       return NULL ; 
1133     }
1134     fgQAInstance = new AliQAManager(mode) ;  
1135     if (!entryCache)
1136                   fgQAInstance->Init();
1137                 else
1138                   fgQAInstance->InitFromCache(entryCache,run);
1139   }
1140         return fgQAInstance;
1141 }
1142
1143 //_____________________________________________________________________________
1144 AliQAManager * AliQAManager::QAManager(AliQAv1::TASKINDEX_t task) 
1145 {
1146   // returns AliQAManager instance (singleton)
1147   return QAManager(AliQAv1::Mode(task)) ; 
1148 }
1149
1150 //_____________________________________________________________________________
1151 TString AliQAManager::Run(const Char_t * detectors, AliRawReader * rawReader, const Bool_t sameCycle) 
1152 {
1153         //Runs all the QA data Maker for Raws only
1154         
1155         fCycleSame       = sameCycle ;
1156         fRawReader       = rawReader ;
1157         fDetectors       = detectors ; 
1158         fDetectorsW      = detectors ;  
1159         
1160         AliCDBManager* man = AliCDBManager::Instance() ; 
1161
1162         if ( man->GetRun() == -1 ) {// check if run number not set previously and set it from raw data
1163                 rawReader->NextEvent() ; 
1164                 man->SetRun(fRawReader->GetRunNumber()) ;
1165                 rawReader->RewindEvents() ;
1166         }       
1167         
1168         if (!fCycleSame) 
1169     if ( !InitQA(AliQAv1::kRAWS) ) 
1170       return "" ; 
1171   fRawReaderDelete = kFALSE ; 
1172
1173         DoIt(AliQAv1::kRAWS) ; 
1174         return  fDetectorsW ;
1175 }
1176
1177 //_____________________________________________________________________________
1178 TString AliQAManager::Run(const Char_t * detectors, const Char_t * fileName, const Bool_t sameCycle) 
1179 {
1180         //Runs all the QA data Maker for Raws only
1181
1182         fCycleSame       = sameCycle ;
1183         fDetectors       = detectors ; 
1184         fDetectorsW      = detectors ;  
1185         
1186         AliCDBManager* man = AliCDBManager::Instance() ; 
1187         if ( man->GetRun() == -1 ) { // check if run number not set previously and set it from AliRun
1188                 AliRunLoader * rl = AliRunLoader::Open("galice.root") ;
1189                 if ( ! rl ) {
1190                         AliFatal("galice.root file not found in current directory") ; 
1191                 } else {
1192                         rl->CdGAFile() ; 
1193                         rl->LoadgAlice() ;
1194                         if ( ! rl->GetAliRun() ) {
1195                                 AliFatal("AliRun not found in galice.root") ;
1196                         } else {
1197                                 rl->LoadHeader() ;
1198                                 man->SetRun(rl->GetHeader()->GetRun());
1199                         }
1200                 }
1201         }
1202         
1203         if (!fCycleSame) 
1204     if ( !InitQA(AliQAv1::kRAWS, fileName) ) 
1205       return "" ; 
1206         
1207         DoIt(AliQAv1::kRAWS) ; 
1208         return  fDetectorsW ;
1209 }
1210
1211 //_____________________________________________________________________________
1212 TString AliQAManager::Run(const Char_t * detectors, const AliQAv1::TASKINDEX_t taskIndex, Bool_t const sameCycle, const  Char_t * fileName ) 
1213 {
1214         // Runs all the QA data Maker for every detector
1215         
1216         fCycleSame       = sameCycle ;
1217         fDetectors       = detectors ; 
1218         fDetectorsW      = detectors ;          
1219         
1220         AliCDBManager* man = AliCDBManager::Instance() ;        
1221         if ( man->GetRun() == -1 ) { // check if run number not set previously and set it from AliRun
1222                 AliRunLoader * rl = AliRunLoader::Open("galice.root") ;
1223                 if ( ! rl ) {
1224                         AliFatal("galice.root file not found in current directory") ; 
1225                 } else {
1226                         rl->CdGAFile() ; 
1227                         rl->LoadgAlice() ;
1228                         if ( ! rl->GetAliRun() ) {
1229                                 AliDebug(AliQAv1::GetQADebugLevel(), "AliRun not found in galice.root") ;
1230                         } else {
1231                                 rl->LoadHeader() ;
1232                                 man->SetRun(rl->GetHeader()->GetRun()) ;
1233                         }
1234                 }
1235         }
1236   if ( taskIndex == AliQAv1::kNULLTASKINDEX) { 
1237                 for (UInt_t task = 0; task < AliQAv1::kNTASKINDEX; task++) {
1238                         if ( fTasks.Contains(Form("%d", task)) ) {
1239         if (!fCycleSame)
1240           if ( !InitQA(AliQAv1::GetTaskIndex(AliQAv1::GetTaskName(task)), fileName) ) 
1241             return "" ;
1242         DoIt(AliQAv1::GetTaskIndex(AliQAv1::GetTaskName(task))) ;
1243                         }
1244                 }
1245         } else {
1246     if (! fCycleSame )
1247       if ( !InitQA(taskIndex, fileName) ) 
1248         return "" ; 
1249       DoIt(taskIndex) ; 
1250   }             
1251         return fDetectorsW ;
1252 }
1253
1254 //_____________________________________________________________________________
1255 void AliQAManager::RunOneEvent(AliRawReader * rawReader) 
1256 {
1257         //Runs all the QA data Maker for Raws only and on one event only (event loop done by calling method)
1258   if ( ! rawReader ) 
1259     return ; 
1260   if (fTasks.Contains(Form("%d", AliQAv1::kRAWS))){
1261     for (UInt_t iDet = 0; iDet < fgkNDetectors; iDet++) {
1262       if (!IsSelected(AliQAv1::GetDetName(iDet))) 
1263         continue;
1264       AliQADataMaker *qadm = GetQADataMaker(iDet);  
1265       if (!qadm) 
1266         continue;
1267       if ( qadm->IsCycleDone() ) {
1268         qadm->EndOfCycle() ;
1269       }
1270       qadm->SetEventSpecie(fEventSpecie) ;  
1271       if ( qadm->GetRecoParam() ) 
1272         if ( AliRecoParam::Convert(qadm->GetRecoParam()->GetEventSpecie()) != AliRecoParam::kDefault)  
1273           qadm->SetEventSpecie(qadm->GetRecoParam()->GetEventSpecie()) ; 
1274                         qadm->Exec(AliQAv1::kRAWS, rawReader) ;
1275                 }
1276   }
1277 }
1278
1279 //_____________________________________________________________________________
1280 void AliQAManager::RunOneEvent(AliESDEvent *& esd, AliESDEvent *& hltesd) 
1281 {
1282         //Runs all the QA data Maker for ESDs only and on one event only (event loop done by calling method)
1283         
1284   if (fTasks.Contains(Form("%d", AliQAv1::kESDS))) {
1285     for (UInt_t iDet = 0; iDet < fgkNDetectors; iDet++) {
1286       if (!IsSelected(AliQAv1::GetDetName(iDet))) 
1287         continue;
1288       AliQADataMaker *qadm = GetQADataMaker(iDet);  
1289       if (!qadm) 
1290         continue;
1291       qadm->SetEventSpecie(fEventSpecie) ;  
1292       if ( qadm->GetRecoParam() ) 
1293         if ( AliRecoParam::Convert(qadm->GetRecoParam()->GetEventSpecie()) != AliRecoParam::kDefault)  
1294           qadm->SetEventSpecie(qadm->GetRecoParam()->GetEventSpecie()) ; 
1295       if ( qadm->IsCycleDone() ) {
1296         qadm->EndOfCycle() ;
1297       }
1298       if (iDet == AliQAv1::kHLT) {
1299         TObjArray esdarray;
1300         esdarray.Add(esd); 
1301         esdarray.Add(hltesd); 
1302         qadm->Exec(AliQAv1::kESDS, &esdarray);
1303       } else {
1304         qadm->Exec(AliQAv1::kESDS, esd) ;        
1305       }
1306                 }
1307         }
1308 }
1309
1310 //_____________________________________________________________________________
1311 void AliQAManager::RunOneEventInOneDetector(Int_t det, TTree * tree) 
1312 {
1313         // Runs all the QA data Maker for ESDs only and on one event only (event loop done by calling method)
1314   
1315   TString test(tree->GetName()) ; 
1316   if (fTasks.Contains(Form("%d", AliQAv1::kRECPOINTS))) {
1317     if (IsSelected(AliQAv1::GetDetName(det))) {
1318       AliQADataMaker *qadm = GetQADataMaker(det);  
1319       if (qadm) { 
1320         qadm->SetEventSpecie(fEventSpecie) ;  
1321         if ( qadm->GetRecoParam() ) {
1322           if ( AliRecoParam::Convert(qadm->GetRecoParam()->GetEventSpecie()) != AliRecoParam::kDefault)  
1323             qadm->SetEventSpecie(qadm->GetRecoParam()->GetEventSpecie()) ; 
1324           else
1325             AliError(Form("%d defined by %s is not an event specie", qadm->GetRecoParam()->GetEventSpecie(), qadm->GetName())) ; 
1326         }                    
1327         if ( qadm->IsCycleDone() ) {
1328           qadm->EndOfCycle() ;
1329         }
1330         if (test.Contains("TreeD")) {
1331           qadm->Exec(AliQAv1::kDIGITSR, tree) ;
1332         } else  if (test.Contains("TreeR")) {
1333           qadm->Exec(AliQAv1::kRECPOINTS, tree) ;
1334         }
1335       }
1336     }
1337   }
1338 }
1339
1340 //_____________________________________________________________________________
1341 Bool_t AliQAManager::Save2OCDB(const Int_t runNumber, AliRecoParam::EventSpecie_t es, const Char_t * year, const Char_t * detectors) const
1342 {
1343         // take the locasl QA data merge into a single file and save in OCDB 
1344         Bool_t rv = kTRUE ; 
1345         TString tmp(AliQAv1::GetQARefStorage()) ; 
1346         if ( tmp.IsNull() ) { 
1347                 AliError("No storage defined, use AliQAv1::SetQARefStorage") ; 
1348                 return kFALSE ; 
1349         }
1350         if ( !(tmp.Contains(AliQAv1::GetLabLocalOCDB()) || tmp.Contains(AliQAv1::GetLabAliEnOCDB())) ) {
1351                 AliError(Form("%s is a wrong storage, use %s or %s", AliQAv1::GetQARefStorage(), AliQAv1::GetLabLocalOCDB().Data(), AliQAv1::GetLabAliEnOCDB().Data())) ; 
1352                 return kFALSE ; 
1353         }
1354         TString sdet(detectors) ; 
1355         sdet.ToUpper() ;
1356         TFile * inputFile ; 
1357         if ( sdet.Contains("ALL") ) {
1358                 rv = Merge(runNumber) ; 
1359                 if ( ! rv )
1360                         return kFALSE ; 
1361                 TString inputFileName(Form("Merged.%s.Data.%d.root", AliQAv1::GetQADataFileName(), runNumber)) ; 
1362                 inputFile = TFile::Open(inputFileName.Data()) ; 
1363                 rv = SaveIt2OCDB(runNumber, inputFile, year, es) ; 
1364         } else {
1365                 for (Int_t index = 0; index < AliQAv1::kNDET; index++) {
1366                         if (sdet.Contains(AliQAv1::GetDetName(index))) {
1367                                 TString inputFileName(Form("%s.%s.%d.root", AliQAv1::GetDetName(index), AliQAv1::GetQADataFileName(), runNumber)) ; 
1368                                 inputFile = TFile::Open(inputFileName.Data()) ;                         
1369                                 rv *= SaveIt2OCDB(runNumber, inputFile, year, es) ; 
1370                         }
1371                 }
1372         }
1373         return rv ; 
1374 }
1375
1376 //_____________________________________________________________________________
1377 Bool_t AliQAManager::SaveIt2OCDB(const Int_t runNumber, TFile * inputFile, const Char_t * year, AliRecoParam::EventSpecie_t es) const
1378 {
1379         // reads the TH1 from file and adds it to appropriate list before saving to OCDB
1380         Bool_t rv = kTRUE ;
1381         AliDebug(AliQAv1::GetQADebugLevel(), Form("Saving TH1s in %s to %s", inputFile->GetName(), AliQAv1::GetQARefStorage())) ; 
1382         if ( ! IsDefaultStorageSet() ) {
1383                 TString tmp( AliQAv1::GetQARefStorage() ) ; 
1384                 if ( tmp.Contains(AliQAv1::GetLabLocalOCDB()) ) 
1385                         Instance()->SetDefaultStorage(AliQAv1::GetQARefStorage()) ;
1386                 else {
1387                         TString tmp1(AliQAv1::GetQARefDefaultStorage()) ; 
1388                         tmp1.Append(year) ; 
1389                         tmp1.Append("?user=alidaq") ; 
1390                         Instance()->SetDefaultStorage(tmp1.Data()) ; 
1391                 }
1392         }
1393         Instance()->SetSpecificStorage("*", AliQAv1::GetQARefStorage()) ; 
1394         if(GetRun() < 0) 
1395                 Instance()->SetRun(runNumber);
1396
1397         AliCDBMetaData mdr ;
1398         mdr.SetResponsible("yves schutz");
1399
1400         for ( Int_t detIndex = 0 ; detIndex < AliQAv1::kNDET ; detIndex++) {
1401                 TDirectory * detDir = inputFile->GetDirectory(AliQAv1::GetDetName(detIndex)) ; 
1402                 if ( detDir ) {
1403                         AliDebug(AliQAv1::GetQADebugLevel(), Form("Entering %s", detDir->GetName())) ;
1404       AliQAv1::SetQARefDataDirName(es) ;
1405                         TString detOCDBDir(Form("%s/%s/%s", AliQAv1::GetDetName(detIndex), AliQAv1::GetRefOCDBDirName(), AliQAv1::GetRefDataDirName())) ; 
1406                         AliCDBId idr(detOCDBDir.Data(), runNumber, AliCDBRunRange::Infinity())  ;
1407                         TList * listDetQAD = new TList() ;
1408                         TString listName(Form("%s QA data Reference", AliQAv1::GetDetName(detIndex))) ; 
1409                         mdr.SetComment(Form("%s QA stuff", AliQAv1::GetDetName(detIndex)));
1410                         listDetQAD->SetName(listName) ; 
1411                         TList * taskList = detDir->GetListOfKeys() ; 
1412                         TIter nextTask(taskList) ; 
1413                         TKey * taskKey ; 
1414                         while ( (taskKey = static_cast<TKey*>(nextTask())) ) {
1415                                 TDirectory * taskDir = detDir->GetDirectory(taskKey->GetName()) ; 
1416         TDirectory * esDir   = taskDir->GetDirectory(AliRecoParam::GetEventSpecieName(es)) ; 
1417                                 AliDebug(AliQAv1::GetQADebugLevel(), Form("Saving %s", esDir->GetName())) ; 
1418                                 TObjArray * listTaskQAD = new TObjArray(100) ; 
1419                                 listTaskQAD->SetName(Form("%s/%s", taskKey->GetName(), AliRecoParam::GetEventSpecieName(es))) ;
1420                                 listDetQAD->Add(listTaskQAD) ; 
1421                                 TList * histList = esDir->GetListOfKeys() ; 
1422                                 TIter nextHist(histList) ; 
1423                                 TKey * histKey ; 
1424                                 while ( (histKey = static_cast<TKey*>(nextHist())) ) {
1425                                         TObject * odata = esDir->Get(histKey->GetName()) ; 
1426                                         if ( !odata ) {
1427                                                 AliError(Form("%s in %s/%s returns a NULL pointer !!", histKey->GetName(), detDir->GetName(), taskDir->GetName())) ;
1428                                         } else {
1429             if ( AliQAv1::GetExpert() == histKey->GetName() ) {
1430               TDirectory * expertDir   = esDir->GetDirectory(histKey->GetName()) ; 
1431               TList * expertHistList = expertDir->GetListOfKeys() ; 
1432               TIter nextExpertHist(expertHistList) ; 
1433               TKey * expertHistKey ; 
1434               while ( (expertHistKey = static_cast<TKey*>(nextExpertHist())) ) {
1435                 TObject * expertOdata = expertDir->Get(expertHistKey->GetName()) ; 
1436                 if ( !expertOdata ) {
1437                   AliError(Form("%s in %s/%s/Expert returns a NULL pointer !!", expertHistKey->GetName(), detDir->GetName(), taskDir->GetName())) ;
1438                 } else {
1439                   AliDebug(AliQAv1::GetQADebugLevel(), Form("Adding %s", expertHistKey->GetName())) ;
1440                   if ( expertOdata->IsA()->InheritsFrom("TH1") ) {
1441                     AliDebug(AliQAv1::GetQADebugLevel(), Form("Adding %s", expertHistKey->GetName())) ;
1442                     TH1 * hExpertdata = static_cast<TH1*>(expertOdata) ; 
1443                     listTaskQAD->Add(hExpertdata) ; 
1444                   }                  
1445                 }                
1446               }
1447             }
1448                                                 AliDebug(AliQAv1::GetQADebugLevel(), Form("Adding %s", histKey->GetName())) ;
1449                                                 if ( odata->IsA()->InheritsFrom("TH1") ) {
1450                                                         AliDebug(AliQAv1::GetQADebugLevel(), Form("Adding %s", histKey->GetName())) ;
1451                                                         TH1 * hdata = static_cast<TH1*>(odata) ; 
1452                                                         listTaskQAD->Add(hdata) ; 
1453                                                 }
1454                                         }
1455                                 }
1456                         }
1457                         Instance()->Put(listDetQAD, idr, &mdr) ;
1458                 }
1459         }
1460         return rv ; 
1461 }       
1462
1463 //_____________________________________________________________________________
1464
1465 void AliQAManager::SetCheckerExternParam(AliQAv1::DETECTORINDEX_t detIndex, TList * parameterList) 
1466 {
1467   // set the external parameters list for the detector checkers 
1468   AliQACheckerBase * qac = AliQAChecker::Instance()->GetDetQAChecker(detIndex) ; 
1469   qac->SetExternParamlist(parameterList) ; 
1470   qac->PrintExternParam() ;  
1471 }
1472
1473 //_____________________________________________________________________________
1474 void AliQAManager::SetEventSpecie(AliRecoParam::EventSpecie_t es) 
1475 {
1476   // set the current event specie and inform AliQAv1 that this event specie has been encountered
1477   fEventSpecie = es ; 
1478   AliQAv1::Instance()->SetEventSpecie(es) ; 
1479 }
1480
1481 //_____________________________________________________________________________
1482 void AliQAManager::SetRecoParam(const Int_t det, const AliDetectorRecoParam *par) 
1483 {
1484   // Set custom reconstruction parameters for a given detector
1485   // Single set of parameters for all the events
1486   GetQADataMaker(det)->SetRecoParam(par) ; 
1487 }
1488
1489 //_____________________________________________________________________________
1490 void AliQAManager::SetWriteExpert()
1491 {
1492   // enable the writing of QA expert data
1493   for (UInt_t iDet = 0; iDet < fgkNDetectors; iDet++) {
1494         if (IsSelected(AliQAv1::GetDetName(iDet))) 
1495       fQAWriteExpert[iDet] = kTRUE ;
1496   }
1497 }  
1498
1499 //_____________________________________________________________________________
1500 void AliQAManager::Destroy() {
1501   // delete AliQAManager instance and
1502   // all associated objects
1503
1504   if (fgQAInstance) {
1505     delete fgQAInstance ;
1506     fgQAInstance = NULL ;
1507   }
1508 }
1509
1510 //_____________________________________________________________________________
1511 void AliQAManager::ShowQA() {
1512   // Show the result of the QA checking
1513   // for all detectors 
1514         for ( Int_t detIndex = 0 ; detIndex < AliQAv1::kNDET ; detIndex++) 
1515     AliQAv1::Instance(AliQAv1::GetDetIndex(AliQAv1::GetDetName(detIndex)))->Show() ; 
1516 }