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