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