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