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