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