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