]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliQAManager.cxx
The track number and track pid associated with the cluster are included
[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
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  if ( runNumber == -1)
768    runNumber = fRunNumber ; 
769  Bool_t rv = MergeData(runNumber) ; 
770  //rv *= MergeResults(runNumber) ; // not needed for the time being
771  return rv ; 
772 }
773         
774 //______________________________________________________________________
775 Bool_t AliQAManager::MergeXML(const Char_t * collectionFile, const Char_t * subFile, const Char_t * outFile) 
776 {
777   // merges files listed in a xml collection 
778   // usage Merge(collection, outputFile))
779   //              collection: is a xml collection  
780   
781   Bool_t rv = kFALSE ; 
782   
783   if ( strstr(collectionFile, ".xml") == 0 ) {
784     AliError("Input collection file must be an \".xml\" file\n") ; 
785     return kFALSE ; 
786   }
787     
788  if ( !gGrid ) 
789    TGrid::Connect("alien://"); 
790  if ( !gGrid ) 
791    return kFALSE ; 
792  
793   // Open the file collection 
794   AliInfoClass(Form("*** Create Collection       ***\n***  Wk-Dir = |%s|             \n***  Coll   = |%s|             \n",gSystem->WorkingDirectory(), collectionFile));                
795   
796   TGridCollection * collection = (TGridCollection*)gROOT->ProcessLine(Form("TAlienCollection::Open(\"%s\")",collectionFile));
797   TGridResult* result = collection->GetGridResult("", 0, 0);
798   
799   Int_t index = 0  ;
800   const Char_t * turl ;
801   TFileMerger merger(kFALSE) ; 
802   if (!outFile) {
803     TString tempo(collectionFile) ; 
804     if ( subFile) 
805       tempo.ReplaceAll(".xml", subFile) ; 
806     else 
807       tempo.ReplaceAll(".xml", "_Merged.root") ; 
808     outFile = tempo.Data() ; 
809   }
810   merger.OutputFile(outFile) ; 
811   
812   while ( (turl = result->GetKey(index, "turl")) ) {
813     Char_t * file ;
814     if ( subFile )
815       file = Form("%s#%s", turl, subFile) ; 
816     else 
817       file = Form("%s", turl) ; 
818     
819     AliDebug(AliQAv1::GetQADebugLevel(), Form("%s\n", file)) ; 
820     merger.AddFile(file) ; 
821     index++ ;  
822   }
823   
824   if (index) 
825     merger.Merge() ; 
826   
827   AliDebug(AliQAv1::GetQADebugLevel(), Form("Files merged into %s\n", outFile)) ;
828   
829   rv = kFALSE;
830   return rv ;
831 }
832
833 //_____________________________________________________________________________
834 void AliQAManager::MergeCustom() const
835 {
836         // Custom Merge of QA data from all detectors for all runs in one single file 
837   // search all the run numbers
838   // search all the run numbers
839   gROOT->ProcessLine(".! ls *QA*.root > QAtempo.txt") ;
840   TString QAfile ; 
841   FILE * QAfiles = fopen("QAtempo.txt", "r") ; 
842   Int_t index = 0 ; 
843   TList srunList ; 
844   TIter nextRun(&srunList) ; 
845   TObjString * srun = NULL ; 
846   Int_t loRun = 999999999 ; 
847   Int_t hiRun = 0 ; 
848   while ( QAfile.Gets(QAfiles) ) {
849     Bool_t runExist = kFALSE ; 
850     TString srunNew(QAfile(QAfile.Index("QA.")+3, QAfile.Index(".root")-(QAfile.Index("QA.")+3))) ; 
851     Int_t cuRun = srunNew.Atoi() ;
852     if (cuRun < loRun) 
853       loRun = cuRun ; 
854     if (cuRun > hiRun)
855       hiRun = cuRun ; 
856     while ( (srun = dynamic_cast<TObjString *> (nextRun())) ) {
857       if ( cuRun == (srun->String()).Atoi() ) {
858         runExist = kTRUE ; 
859         break ; 
860       } 
861     }
862     nextRun.Reset() ; 
863     if ( ! runExist ) 
864       srunList.Add(new TObjString(srunNew.Data()));
865   }
866   nextRun.Reset() ;    
867   Int_t runNumber = 0 ; 
868   TFile mergedFile(Form("Merged.%s.Data.root", AliQAv1::GetQADataFileName()), "RECREATE") ; 
869   TH1I * hisRun = new TH1I("hLMR", "List of merged runs", hiRun-loRun+10, loRun, hiRun+10) ; 
870   // create the structure into the merged file
871   for (Int_t iDet = 0; iDet < AliQAv1::kNDET ; iDet++) {
872     TDirectory * detDir = mergedFile.mkdir(AliQAv1::GetDetName(iDet)) ; 
873     for (Int_t taskIndex = 0; taskIndex < AliQAv1::kNTASKINDEX; taskIndex++) {
874       detDir->cd() ; 
875       TDirectory * taskDir = gDirectory->mkdir(AliQAv1::GetTaskName(taskIndex)) ; 
876       for (Int_t es = 0 ; es < AliRecoParam::kNSpecies ; es++) {
877         taskDir->cd() ; 
878         TDirectory * esDir = gDirectory->mkdir(AliRecoParam::GetEventSpecieName(es)) ;
879         esDir->cd() ; 
880         gDirectory->mkdir(AliQAv1::GetExpert()) ; 
881       }
882     }
883   }
884   while ( (srun = dynamic_cast<TObjString *> (nextRun())) ) {
885     runNumber = (srun->String()).Atoi() ; 
886     hisRun->Fill(runNumber) ; 
887     AliDebug(AliQAv1::GetQADebugLevel(), Form("Merging run number %d", runNumber)) ; 
888     // search all QA files for runNumber in the current directory
889     Char_t * fileList[AliQAv1::kNDET] ;
890     index = 0 ; 
891     for (Int_t iDet = 0; iDet < AliQAv1::kNDET ; iDet++) {
892       Char_t * file = gSystem->Which(gSystem->WorkingDirectory(), Form("%s.%s.%d.root", AliQAv1::GetDetName(iDet), AliQAv1::GetQADataFileName(), runNumber)); 
893       if (file) 
894         fileList[index++] = file ;
895     }
896     if ( index == 0 ) {
897       AliError("No QA data file found\n") ; 
898       return ; 
899     }
900     for ( Int_t i = 0 ; i < index ; i++) {
901       TFile * inFile = TFile::Open(fileList[i]) ;  
902       TList * listOfKeys =inFile->GetListOfKeys() ; 
903       TIter nextkey(listOfKeys) ; 
904       TObject * obj1 ; 
905       TString dirName("") ; 
906       while ( (obj1 = nextkey()) ) {
907         TDirectory * directoryDet = inFile->GetDirectory(obj1->GetName()) ; 
908         if ( directoryDet ) {
909           AliDebug(AliQAv1::GetQADebugLevel(), Form("%s dir = %s", inFile->GetName(), directoryDet->GetName())) ; 
910           dirName += Form("%s/", directoryDet->GetName() ) ; 
911           directoryDet->cd() ;
912           TList * listOfTasks = directoryDet->GetListOfKeys() ; 
913           TIter nextTask(listOfTasks) ; 
914           TObject * obj2 ; 
915           while ( (obj2 = nextTask()) ) {
916             TDirectory * directoryTask = directoryDet->GetDirectory(obj2->GetName()) ; 
917             if ( directoryTask ) {
918               dirName += Form("%s", obj2->GetName()) ; 
919               AliDebug(AliQAv1::GetQADebugLevel(), Form("%s", dirName.Data())) ; 
920               directoryTask->cd() ; 
921               TList * listOfEventSpecie = directoryTask->GetListOfKeys() ; 
922               TIter nextEventSpecie(listOfEventSpecie) ; 
923               TObject * obj3 ; 
924               while ( (obj3 = nextEventSpecie()) ) {
925                 TDirectory * directoryEventSpecie = directoryTask->GetDirectory(obj3->GetName()) ; 
926                 if ( directoryEventSpecie ) {
927                   dirName += Form("/%s/", obj3->GetName()) ; 
928                   AliDebug(AliQAv1::GetQADebugLevel(), Form("%s\n", dirName.Data())) ; 
929                   directoryEventSpecie->cd() ; 
930                   // histograms are here
931                   TDirectory * mergedDirectory = mergedFile.GetDirectory(dirName.Data()) ;
932                   TList * listOfData = directoryEventSpecie->GetListOfKeys() ; 
933                   TIter nextData(listOfData) ; 
934                   TKey * key ; 
935                   while ( (key = dynamic_cast<TKey *>(nextData())) ) {
936                     TString className(key->GetClassName()) ; 
937                     if (  className.Contains("TH") || className.Contains("TProfile") ) {
938                       TH1 * histIn = dynamic_cast<TH1*> (key->ReadObj()) ; 
939                       TH1 * histOu = dynamic_cast<TH1*> (mergedDirectory->FindObjectAny(histIn->GetName())) ; 
940                       AliDebug(AliQAv1::GetQADebugLevel(), Form("%s %x %x\n", key->GetName(), histIn, histOu)) ; 
941                       mergedDirectory->cd() ; 
942                       if ( ! histOu ) {
943                         histIn->Write() ; 
944                       } else {
945                         histOu->Add(histIn) ; 
946                         histOu->Write(histOu->GetName(), kOverwrite) ; 
947                       }
948                     }
949                     else if ( className.Contains("TDirectoryFile") ) {
950                       TDirectory * dirExpert = directoryEventSpecie->GetDirectory(key->GetName()) ; 
951                       dirExpert->cd() ; 
952                       TDirectory * mergedDirectoryExpert = mergedDirectory->GetDirectory(dirExpert->GetName()) ; 
953                       TList * listOfExpertData = dirExpert->GetListOfKeys() ; 
954                       TIter nextExpertData(listOfExpertData) ; 
955                       TKey * keykey ; 
956                       while ( (keykey = dynamic_cast<TKey *>(nextExpertData())) ) {
957                         TString classNameExpert(keykey->GetClassName()) ; 
958                         if (classNameExpert.Contains("TH")) {
959                           TH1 * histInExpert = dynamic_cast<TH1*> (keykey->ReadObj()) ; 
960                           TH1 * histOuExpert = dynamic_cast<TH1*> (mergedDirectory->FindObjectAny(histInExpert->GetName())) ; 
961                           mergedDirectoryExpert->cd() ; 
962                           if ( ! histOuExpert ) {
963                             histInExpert->Write() ; 
964                           } else {
965                             histOuExpert->Add(histInExpert) ; 
966                             histOuExpert->Write(histOuExpert->GetName(), kOverwrite) ; 
967                           }
968                         }
969                       }
970                     } else {
971                       AliError(Form("No merge done for this object %s in %s", key->GetName(), dirName.Data())) ; 
972                     }
973                   }
974                   dirName.ReplaceAll(Form("/%s/",obj3->GetName()), "") ; 
975                 }
976               }
977               dirName.ReplaceAll(obj2->GetName(), "") ; 
978             }
979           }
980         }
981       }
982       inFile->Close() ; 
983     }
984   }
985   mergedFile.cd() ;
986   hisRun->Write() ; 
987   mergedFile.Close() ; 
988   srunList.Delete() ;   
989 }
990
991 //_____________________________________________________________________________
992 Bool_t AliQAManager::MergeData(const Int_t runNumber) const
993 {
994         // Merge QA data from all detectors for a given run in one single file 
995   
996   TFileMerger merger ; 
997   TString outFileName = Form("Merged.%s.Data.root",AliQAv1::GetQADataFileName()) ;
998   merger.OutputFile(outFileName.Data()) ; 
999   for (UInt_t iDet = 0; iDet < fgkNDetectors ; iDet++) {
1000     Char_t * file = gSystem->Which(gSystem->WorkingDirectory(), Form("%s.%s.%d.root", AliQAv1::GetDetName(iDet), AliQAv1::GetQADataFileName(), runNumber)); 
1001     if (file) 
1002       merger.AddFile(file) ; 
1003   }
1004   merger.Merge() ; 
1005         return kTRUE ; 
1006 }
1007
1008 //_____________________________________________________________________________
1009 Bool_t AliQAManager::MergeResults(const Int_t runNumber) const
1010 {
1011         // Merge the QA result from all the data chunks in a run 
1012   // to be revised whwn it will be used (see MergeData)
1013         TString cmd ;
1014         cmd = Form(".! ls %s*.root > tempo.txt", AliQAv1::GetQADataFileName()) ; 
1015         gROOT->ProcessLine(cmd.Data()) ;
1016         ifstream in("tempo.txt") ; 
1017         const Int_t chunkMax = 100 ;  
1018         TString fileList[chunkMax] ;
1019         
1020         Int_t index = 0 ; 
1021         while ( 1 ) {
1022                 TString file ; 
1023                 in >> fileList[index] ; 
1024                 if ( !in.good() ) 
1025                         break ; 
1026                 AliDebug(AliQAv1::GetQADebugLevel(), Form("index = %d file = %s", index, (fileList[index].Data()))) ; 
1027                 index++ ;
1028         }
1029         
1030         if ( index == 0 ) { 
1031                 AliError("No QA Result File found") ; 
1032                 return kFALSE ; 
1033         }
1034         
1035         TFileMerger merger ; 
1036   TString outFileName ;
1037   if (runNumber != -1) 
1038     outFileName = Form("Merged.%s.Result.%d.root",AliQAv1::GetQADataFileName(),runNumber); 
1039   else 
1040     outFileName = Form("Merged.%s.Result.root",AliQAv1::GetQADataFileName()); 
1041         merger.OutputFile(outFileName.Data()) ; 
1042         for (Int_t ifile = 0 ; ifile < index ; ifile++) {
1043                 TString file = fileList[ifile] ; 
1044                 merger.AddFile(file) ; 
1045         }
1046         merger.Merge() ; 
1047         
1048         return kTRUE ; 
1049 }
1050
1051 //_____________________________________________________________________________
1052 void AliQAManager::Reset(const Bool_t sameCycle)
1053 {
1054         // Reset the default data members
1055
1056         for (UInt_t iDet = 0; iDet < fgkNDetectors ; iDet++) {
1057                 if (IsSelected(AliQAv1::GetDetName(iDet))) {
1058                         AliQADataMaker * qadm = GetQADataMaker(iDet);
1059                         qadm->Reset();
1060                 }
1061         } 
1062         if (fRawReaderDelete) { 
1063                 delete fRawReader ;
1064                 fRawReader      = NULL ;
1065         }
1066
1067         fCycleSame      = sameCycle ; 
1068         fESD            = NULL ; 
1069         fESDTree        = NULL ; 
1070         //fFirst          = kTRUE ;   
1071         fNumberOfEvents = 999999 ;  
1072 }
1073
1074 //_____________________________________________________________________________
1075 AliQAManager * AliQAManager::QAManager(Char_t * mode, TMap *entryCache, Int_t run) 
1076 {
1077   // returns AliQAManager instance (singleton)
1078   
1079         if (!fgQAInstance) {
1080     if ( (strcmp(mode, "sim") != 0) && (strcmp(mode, "rec") != 0) )
1081       AliFatalClass("You must specify sim or rec") ; 
1082     fgQAInstance = new AliQAManager(mode) ;  
1083     if (!entryCache)
1084                   fgQAInstance->Init();
1085                 else
1086                   fgQAInstance->InitFromCache(entryCache,run);
1087   }
1088         return fgQAInstance;
1089 }
1090
1091 //_____________________________________________________________________________
1092 TString AliQAManager::Run(const Char_t * detectors, AliRawReader * rawReader, const Bool_t sameCycle) 
1093 {
1094         //Runs all the QA data Maker for Raws only
1095         
1096         fCycleSame       = sameCycle ;
1097         fRawReader       = rawReader ;
1098         fDetectors       = detectors ; 
1099         fDetectorsW      = detectors ;  
1100         
1101         AliCDBManager* man = AliCDBManager::Instance() ; 
1102
1103         if ( man->GetRun() == -1 ) {// check if run number not set previously and set it from raw data
1104                 rawReader->NextEvent() ; 
1105                 man->SetRun(fRawReader->GetRunNumber()) ;
1106                 rawReader->RewindEvents() ;
1107         }       
1108         
1109         if (!fCycleSame) 
1110     if ( !InitQA(AliQAv1::kRAWS) ) 
1111       return kFALSE ; 
1112   fRawReaderDelete = kFALSE ; 
1113
1114         DoIt(AliQAv1::kRAWS) ; 
1115         return  fDetectorsW ;
1116 }
1117
1118 //_____________________________________________________________________________
1119 TString AliQAManager::Run(const Char_t * detectors, const Char_t * fileName, const Bool_t sameCycle) 
1120 {
1121         //Runs all the QA data Maker for Raws only
1122
1123         fCycleSame       = sameCycle ;
1124         fDetectors       = detectors ; 
1125         fDetectorsW      = detectors ;  
1126         
1127         AliCDBManager* man = AliCDBManager::Instance() ; 
1128         if ( man->GetRun() == -1 ) { // check if run number not set previously and set it from AliRun
1129                 AliRunLoader * rl = AliRunLoader::Open("galice.root") ;
1130                 if ( ! rl ) {
1131                         AliFatal("galice.root file not found in current directory") ; 
1132                 } else {
1133                         rl->CdGAFile() ; 
1134                         rl->LoadgAlice() ;
1135                         if ( ! rl->GetAliRun() ) {
1136                                 AliFatal("AliRun not found in galice.root") ;
1137                         } else {
1138                                 rl->LoadHeader() ;
1139                                 man->SetRun(rl->GetHeader()->GetRun());
1140                         }
1141                 }
1142         }
1143         
1144         if (!fCycleSame) 
1145     if ( !InitQA(AliQAv1::kRAWS, fileName) ) 
1146       return kFALSE ; 
1147         
1148         DoIt(AliQAv1::kRAWS) ; 
1149         return  fDetectorsW ;
1150 }
1151
1152 //_____________________________________________________________________________
1153 TString AliQAManager::Run(const Char_t * detectors, const AliQAv1::TASKINDEX_t taskIndex, Bool_t const sameCycle, const  Char_t * fileName ) 
1154 {
1155         // Runs all the QA data Maker for every detector
1156         
1157         fCycleSame       = sameCycle ;
1158         fDetectors       = detectors ; 
1159         fDetectorsW      = detectors ;          
1160         
1161         AliCDBManager* man = AliCDBManager::Instance() ;        
1162         if ( man->GetRun() == -1 ) { // check if run number not set previously and set it from AliRun
1163                 AliRunLoader * rl = AliRunLoader::Open("galice.root") ;
1164                 if ( ! rl ) {
1165                         AliFatal("galice.root file not found in current directory") ; 
1166                 } else {
1167                         rl->CdGAFile() ; 
1168                         rl->LoadgAlice() ;
1169                         if ( ! rl->GetAliRun() ) {
1170                                 AliDebug(AliQAv1::GetQADebugLevel(), "AliRun not found in galice.root") ;
1171                         } else {
1172                                 rl->LoadHeader() ;
1173                                 man->SetRun(rl->GetHeader()->GetRun()) ;
1174                         }
1175                 }
1176         }
1177   if ( taskIndex == AliQAv1::kNULLTASKINDEX) { 
1178                 for (UInt_t task = 0; task < AliQAv1::kNTASKINDEX; task++) {
1179                         if ( fTasks.Contains(Form("%d", task)) ) {
1180         if (!fCycleSame)
1181           if ( !InitQA(AliQAv1::GetTaskIndex(AliQAv1::GetTaskName(task)), fileName) ) 
1182             return kFALSE ;
1183         DoIt(AliQAv1::GetTaskIndex(AliQAv1::GetTaskName(task))) ;
1184                         }
1185                 }
1186         } else {
1187     if (! fCycleSame )
1188       if ( !InitQA(taskIndex, fileName) ) 
1189         return kFALSE ; 
1190       DoIt(taskIndex) ; 
1191   }             
1192         return fDetectorsW ;
1193 }
1194
1195 //_____________________________________________________________________________
1196 void AliQAManager::RunOneEvent(AliRawReader * rawReader) 
1197 {
1198         //Runs all the QA data Maker for Raws only and on one event only (event loop done by calling method)
1199   if ( ! rawReader ) 
1200     return ; 
1201         AliCodeTimerAuto("") ;
1202   if (fTasks.Contains(Form("%d", AliQAv1::kRAWS))){
1203     for (UInt_t iDet = 0; iDet < fgkNDetectors; iDet++) {
1204       if (!IsSelected(AliQAv1::GetDetName(iDet))) 
1205         continue;
1206       AliQADataMaker *qadm = GetQADataMaker(iDet);  
1207       if (!qadm) 
1208         continue;
1209       if ( qadm->IsCycleDone() ) {
1210         qadm->EndOfCycle() ;
1211       }
1212       AliCodeTimerStart(Form("running RAW quality assurance data maker for %s", AliQAv1::GetDetName(iDet))); 
1213       qadm->SetEventSpecie(fEventSpecie) ;  
1214       if ( qadm->GetRecoParam() ) 
1215         if ( AliRecoParam::Convert(qadm->GetRecoParam()->GetEventSpecie()) != AliRecoParam::kDefault)  
1216           qadm->SetEventSpecie(qadm->GetRecoParam()->GetEventSpecie()) ; 
1217                         qadm->Exec(AliQAv1::kRAWS, rawReader) ;
1218       AliCodeTimerStop(Form("running RAW quality assurance data maker for %s", AliQAv1::GetDetName(iDet)));
1219                 }
1220   }
1221 }
1222
1223 //_____________________________________________________________________________
1224 void AliQAManager::RunOneEvent(AliESDEvent *& esd) 
1225 {
1226         //Runs all the QA data Maker for ESDs only and on one event only (event loop done by calling method)
1227         
1228   AliCodeTimerAuto("") ;
1229   if (fTasks.Contains(Form("%d", AliQAv1::kESDS))) {
1230     for (UInt_t iDet = 0; iDet < fgkNDetectors; iDet++) {
1231       if (!IsSelected(AliQAv1::GetDetName(iDet))) 
1232         continue;
1233       AliQADataMaker *qadm = GetQADataMaker(iDet);  
1234       if (!qadm) 
1235         continue;
1236       qadm->SetEventSpecie(fEventSpecie) ;  
1237       if ( qadm->GetRecoParam() ) 
1238         if ( AliRecoParam::Convert(qadm->GetRecoParam()->GetEventSpecie()) != AliRecoParam::kDefault)  
1239           qadm->SetEventSpecie(qadm->GetRecoParam()->GetEventSpecie()) ; 
1240       if ( qadm->IsCycleDone() ) {
1241         qadm->EndOfCycle() ;
1242       }
1243       AliCodeTimerStart(Form("running ESD quality assurance data maker for %s", AliQAv1::GetDetName(iDet)));
1244                         qadm->Exec(AliQAv1::kESDS, esd) ;
1245       AliCodeTimerStop(Form("running ESD quality assurance data maker for %s", AliQAv1::GetDetName(iDet)));
1246                 }
1247         }
1248 }
1249
1250 //_____________________________________________________________________________
1251 void AliQAManager::RunOneEventInOneDetector(Int_t det, TTree * tree) 
1252 {
1253         // Runs all the QA data Maker for ESDs only and on one event only (event loop done by calling method)
1254   
1255   TString test(tree->GetName()) ; 
1256         AliCodeTimerAuto("") ;
1257   if (fTasks.Contains(Form("%d", AliQAv1::kRECPOINTS))) {
1258     if (IsSelected(AliQAv1::GetDetName(det))) {
1259       AliQADataMaker *qadm = GetQADataMaker(det);  
1260       if (qadm) { 
1261         qadm->SetEventSpecie(fEventSpecie) ;  
1262         if ( qadm->GetRecoParam() ) 
1263           if ( AliRecoParam::Convert(qadm->GetRecoParam()->GetEventSpecie()) != AliRecoParam::kDefault)  
1264             qadm->SetEventSpecie(qadm->GetRecoParam()->GetEventSpecie()) ;         
1265         if ( qadm->IsCycleDone() ) {
1266           qadm->EndOfCycle() ;
1267         }
1268         AliCodeTimerStart(Form("running RecPoints quality assurance data maker for %s", AliQAv1::GetDetName(det)));
1269         if (test.Contains("TreeD")) {
1270           qadm->Exec(AliQAv1::kDIGITSR, tree) ;
1271           AliCodeTimerStop(Form("running Digits quality assurance data maker for %s", AliQAv1::GetDetName(det)));
1272         } else  if (test.Contains("TreeR")) {
1273           qadm->Exec(AliQAv1::kRECPOINTS, tree) ;
1274           AliCodeTimerStop(Form("running RecPoints quality assurance data maker for %s", AliQAv1::GetDetName(det)));
1275         }
1276       }
1277     }
1278   }
1279 }
1280
1281 //_____________________________________________________________________________
1282 Bool_t AliQAManager::Save2OCDB(const Int_t runNumber, AliRecoParam::EventSpecie_t es, const Char_t * year, const Char_t * detectors) const
1283 {
1284         // take the locasl QA data merge into a single file and save in OCDB 
1285         Bool_t rv = kTRUE ; 
1286         TString tmp(AliQAv1::GetQARefStorage()) ; 
1287         if ( tmp.IsNull() ) { 
1288                 AliError("No storage defined, use AliQAv1::SetQARefStorage") ; 
1289                 return kFALSE ; 
1290         }
1291         if ( !(tmp.Contains(AliQAv1::GetLabLocalOCDB()) || tmp.Contains(AliQAv1::GetLabAliEnOCDB())) ) {
1292                 AliError(Form("%s is a wrong storage, use %s or %s", AliQAv1::GetQARefStorage(), AliQAv1::GetLabLocalOCDB().Data(), AliQAv1::GetLabAliEnOCDB().Data())) ; 
1293                 return kFALSE ; 
1294         }
1295         TString sdet(detectors) ; 
1296         sdet.ToUpper() ;
1297         TFile * inputFile ; 
1298         if ( sdet.Contains("ALL") ) {
1299                 rv = Merge(runNumber) ; 
1300                 if ( ! rv )
1301                         return kFALSE ; 
1302                 TString inputFileName(Form("Merged.%s.Data.%d.root", AliQAv1::GetQADataFileName(), runNumber)) ; 
1303                 inputFile = TFile::Open(inputFileName.Data()) ; 
1304                 rv = SaveIt2OCDB(runNumber, inputFile, year, es) ; 
1305         } else {
1306                 for (Int_t index = 0; index < AliQAv1::kNDET; index++) {
1307                         if (sdet.Contains(AliQAv1::GetDetName(index))) {
1308                                 TString inputFileName(Form("%s.%s.%d.root", AliQAv1::GetDetName(index), AliQAv1::GetQADataFileName(), runNumber)) ; 
1309                                 inputFile = TFile::Open(inputFileName.Data()) ;                         
1310                                 rv *= SaveIt2OCDB(runNumber, inputFile, year, es) ; 
1311                         }
1312                 }
1313         }
1314         return rv ; 
1315 }
1316
1317 //_____________________________________________________________________________
1318 Bool_t AliQAManager::SaveIt2OCDB(const Int_t runNumber, TFile * inputFile, const Char_t * year, AliRecoParam::EventSpecie_t es) const
1319 {
1320         // reads the TH1 from file and adds it to appropriate list before saving to OCDB
1321         Bool_t rv = kTRUE ;
1322         AliDebug(AliQAv1::GetQADebugLevel(), Form("Saving TH1s in %s to %s", inputFile->GetName(), AliQAv1::GetQARefStorage())) ; 
1323         if ( ! IsDefaultStorageSet() ) {
1324                 TString tmp( AliQAv1::GetQARefStorage() ) ; 
1325                 if ( tmp.Contains(AliQAv1::GetLabLocalOCDB()) ) 
1326                         Instance()->SetDefaultStorage(AliQAv1::GetQARefStorage()) ;
1327                 else {
1328                         TString tmp1(AliQAv1::GetQARefDefaultStorage()) ; 
1329                         tmp1.Append(year) ; 
1330                         tmp1.Append("?user=alidaq") ; 
1331                         Instance()->SetDefaultStorage(tmp1.Data()) ; 
1332                 }
1333         }
1334         Instance()->SetSpecificStorage("*", AliQAv1::GetQARefStorage()) ; 
1335         if(GetRun() < 0) 
1336                 Instance()->SetRun(runNumber);
1337
1338         AliCDBMetaData mdr ;
1339         mdr.SetResponsible("yves schutz");
1340
1341         for ( Int_t detIndex = 0 ; detIndex < AliQAv1::kNDET ; detIndex++) {
1342                 TDirectory * detDir = inputFile->GetDirectory(AliQAv1::GetDetName(detIndex)) ; 
1343                 if ( detDir ) {
1344                         AliDebug(AliQAv1::GetQADebugLevel(), Form("Entering %s", detDir->GetName())) ;
1345       AliQAv1::SetQARefDataDirName(es) ;
1346                         TString detOCDBDir(Form("%s/%s/%s", AliQAv1::GetDetName(detIndex), AliQAv1::GetRefOCDBDirName(), AliQAv1::GetRefDataDirName())) ; 
1347                         AliCDBId idr(detOCDBDir.Data(), runNumber, AliCDBRunRange::Infinity())  ;
1348                         TList * listDetQAD = new TList() ;
1349                         TString listName(Form("%s QA data Reference", AliQAv1::GetDetName(detIndex))) ; 
1350                         mdr.SetComment(Form("%s QA stuff", AliQAv1::GetDetName(detIndex)));
1351                         listDetQAD->SetName(listName) ; 
1352                         TList * taskList = detDir->GetListOfKeys() ; 
1353                         TIter nextTask(taskList) ; 
1354                         TKey * taskKey ; 
1355                         while ( (taskKey = dynamic_cast<TKey*>(nextTask())) ) {
1356                                 TDirectory * taskDir = detDir->GetDirectory(taskKey->GetName()) ; 
1357         TDirectory * esDir   = taskDir->GetDirectory(AliRecoParam::GetEventSpecieName(es)) ; 
1358                                 AliDebug(AliQAv1::GetQADebugLevel(), Form("Saving %s", esDir->GetName())) ; 
1359                                 TObjArray * listTaskQAD = new TObjArray(100) ; 
1360                                 listTaskQAD->SetName(Form("%s/%s", taskKey->GetName(), AliRecoParam::GetEventSpecieName(es))) ;
1361                                 listDetQAD->Add(listTaskQAD) ; 
1362                                 TList * histList = esDir->GetListOfKeys() ; 
1363                                 TIter nextHist(histList) ; 
1364                                 TKey * histKey ; 
1365                                 while ( (histKey = dynamic_cast<TKey*>(nextHist())) ) {
1366                                         TObject * odata = esDir->Get(histKey->GetName()) ; 
1367                                         if ( !odata ) {
1368                                                 AliError(Form("%s in %s/%s returns a NULL pointer !!", histKey->GetName(), detDir->GetName(), taskDir->GetName())) ;
1369                                         } else {
1370             if ( AliQAv1::GetExpert() == histKey->GetName() ) {
1371               TDirectory * expertDir   = esDir->GetDirectory(histKey->GetName()) ; 
1372               TList * expertHistList = expertDir->GetListOfKeys() ; 
1373               TIter nextExpertHist(expertHistList) ; 
1374               TKey * expertHistKey ; 
1375               while ( (expertHistKey = dynamic_cast<TKey*>(nextExpertHist())) ) {
1376                 TObject * expertOdata = expertDir->Get(expertHistKey->GetName()) ; 
1377                 if ( !expertOdata ) {
1378                   AliError(Form("%s in %s/%s/Expert returns a NULL pointer !!", expertHistKey->GetName(), detDir->GetName(), taskDir->GetName())) ;
1379                 } else {
1380                   AliDebug(AliQAv1::GetQADebugLevel(), Form("Adding %s", expertHistKey->GetName())) ;
1381                   if ( expertOdata->IsA()->InheritsFrom("TH1") ) {
1382                     AliDebug(AliQAv1::GetQADebugLevel(), Form("Adding %s", expertHistKey->GetName())) ;
1383                     TH1 * hExpertdata = static_cast<TH1*>(expertOdata) ; 
1384                     listTaskQAD->Add(hExpertdata) ; 
1385                   }                  
1386                 }                
1387               }
1388             }
1389                                                 AliDebug(AliQAv1::GetQADebugLevel(), Form("Adding %s", histKey->GetName())) ;
1390                                                 if ( odata->IsA()->InheritsFrom("TH1") ) {
1391                                                         AliDebug(AliQAv1::GetQADebugLevel(), Form("Adding %s", histKey->GetName())) ;
1392                                                         TH1 * hdata = static_cast<TH1*>(odata) ; 
1393                                                         listTaskQAD->Add(hdata) ; 
1394                                                 }
1395                                         }
1396                                 }
1397                         }
1398                         Instance()->Put(listDetQAD, idr, &mdr) ;
1399                 }
1400         }
1401         return rv ; 
1402 }       
1403
1404 //_____________________________________________________________________________
1405 void AliQAManager::SetEventSpecie(AliRecoParam::EventSpecie_t es) 
1406 {
1407   // set the current event specie and inform AliQAv1 that this event specie has been encountered
1408   fEventSpecie = es ; 
1409   AliQAv1::Instance()->SetEventSpecie(es) ; 
1410 }
1411
1412 //_____________________________________________________________________________
1413 void AliQAManager::SetRecoParam(const Int_t det, const AliDetectorRecoParam *par) 
1414 {
1415   // Set custom reconstruction parameters for a given detector
1416   // Single set of parameters for all the events
1417   GetQADataMaker(det)->SetRecoParam(par) ; 
1418 }
1419
1420 //_____________________________________________________________________________
1421 void AliQAManager::SetWriteExpert()
1422 {
1423   // enable the writing of QA expert data
1424   for (UInt_t iDet = 0; iDet < fgkNDetectors; iDet++) {
1425         if (IsSelected(AliQAv1::GetDetName(iDet))) 
1426       fQAWriteExpert[iDet] = kTRUE ;
1427   }
1428 }