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