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