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