]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliQADataMakerSteer.cxx
Removal of out-of-event-loop QA. Correlation QA. Some clean-up in QA code (Yves)
[u/mrichter/AliRoot.git] / STEER / AliQADataMakerSteer.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$ */
17 ///////////////////////////////////////////////////////////////////////////////
18 //                                                                           //
19 // class for running the QA makers                                           //
20 //                                                                           //
21 //   AliQADataMakerSteer qas;                                                //
22 //   qas.Run(AliQA::kRAWS, rawROOTFileName);                                 //
23 //   qas.Run(AliQA::kHITS);                                                  //
24 //   qas.Run(AliQA::kSDIGITS);                                               //
25 //   qas.Run(AliQA::kDIGITS);                                                //
26 //   qas.Run(AliQA::kRECPOINTS);                                             //
27 //   qas.Run(AliQA::kESDS);                                                  //
28 //                                                                           //
29 ///////////////////////////////////////////////////////////////////////////////
30
31 #include <TKey.h>
32 #include <TFile.h>
33 #include <TFileMerger.h>
34 #include <TPluginManager.h>
35 #include <TROOT.h>
36 #include <TString.h>
37 #include <TSystem.h>
38
39 #include "AliCDBManager.h"
40 #include "AliCDBEntry.h"
41 #include "AliCDBId.h"
42 #include "AliCDBMetaData.h"
43 #include "AliCodeTimer.h"
44 #include "AliCorrQADataMakerRec.h"
45 #include "AliDetectorRecoParam.h"
46 #include "AliESDEvent.h"
47 #include "AliGeomManager.h"
48 #include "AliGlobalQADataMaker.h"
49 #include "AliHeader.h"
50 #include "AliLog.h"
51 #include "AliModule.h"
52 #include "AliQA.h"
53 #include "AliQADataMakerRec.h"
54 #include "AliQADataMakerSim.h"
55 #include "AliQADataMakerSteer.h" 
56 #include "AliRawReaderDate.h"
57 #include "AliRawReaderFile.h"
58 #include "AliRawReaderRoot.h"
59 #include "AliRun.h"
60 #include "AliRunLoader.h"
61
62 ClassImp(AliQADataMakerSteer) 
63
64 //_____________________________________________________________________________
65 AliQADataMakerSteer::AliQADataMakerSteer(char * mode, const char* gAliceFilename, const char * name, const char * title) :
66         TNamed(name, title), 
67         fCurrentEvent(0),  
68         fCycleSame(kFALSE),
69         fDetectors("ALL"), 
70         fDetectorsW("ALL"), 
71         fESD(NULL), 
72         fESDTree(NULL),
73         fGAliceFileName(gAliceFilename), 
74         fFirstEvent(0),        
75         fMaxEvents(0),   
76   fMode(mode), 
77         fNumberOfEvents(999999), 
78   fRecoParam(),
79         fRunNumber(0), 
80         fRawReader(NULL), 
81         fRawReaderDelete(kTRUE), 
82         fRunLoader(NULL), 
83   fTasks("")
84 {
85         // default ctor
86         fMaxEvents = fNumberOfEvents ; 
87         for (UInt_t iDet = 0; iDet < fgkNDetectors; iDet++) {
88                 if (IsSelected(AliQA::GetDetName(iDet))) {
89                         fLoader[iDet]      = NULL ;
90                         fQADataMaker[iDet] = NULL ;
91                         fQACycles[iDet]    = 999999 ;
92                 }
93         }       
94 }
95
96 //_____________________________________________________________________________
97 AliQADataMakerSteer::AliQADataMakerSteer(const AliQADataMakerSteer & qas) : 
98         TNamed(qas), 
99         fCurrentEvent(qas.fCurrentEvent),  
100         fCycleSame(kFALSE),
101         fDetectors(qas.fDetectors), 
102         fDetectorsW(qas.fDetectorsW), 
103         fESD(NULL), 
104         fESDTree(NULL), 
105         fGAliceFileName(qas.fGAliceFileName), 
106         fFirstEvent(qas.fFirstEvent),        
107         fMaxEvents(qas.fMaxEvents),    
108         fMode(qas.fMode), 
109         fNumberOfEvents(qas.fNumberOfEvents), 
110         fRecoParam(),           
111         fRunNumber(qas.fRunNumber), 
112         fRawReader(NULL), 
113         fRawReaderDelete(kTRUE), 
114         fRunLoader(NULL), 
115   fTasks(qas.fTasks)
116 {
117         // cpy ctor
118         for (UInt_t iDet = 0; iDet < fgkNDetectors; iDet++) {
119                 fLoader[iDet]      = qas.fLoader[iDet] ;
120                 fQADataMaker[iDet] = qas.fQADataMaker[iDet] ;
121                 fQACycles[iDet]    = qas.fQACycles[iDet] ;      
122         }
123 }
124
125 //_____________________________________________________________________________
126 AliQADataMakerSteer & AliQADataMakerSteer::operator = (const AliQADataMakerSteer & qas) 
127 {
128         // assignment operator
129   this->~AliQADataMakerSteer() ;
130   new(this) AliQADataMakerSteer(qas) ;
131   return *this ;
132 }
133
134 //_____________________________________________________________________________
135 AliQADataMakerSteer::~AliQADataMakerSteer() 
136 {
137         // dtor
138         for (UInt_t iDet = 0; iDet < fgkNDetectors; iDet++) {
139                 if (IsSelected(AliQA::GetDetName(iDet))) {
140                   fLoader[iDet] = NULL;
141                   if (fQADataMaker[iDet]) {
142                           (fQADataMaker[iDet])->Finish() ; 
143                                 delete fQADataMaker[iDet] ;
144                   }
145                 }
146         }
147         if (fRawReaderDelete) { 
148                 fRunLoader = NULL ;
149                 delete fRawReader ;
150                 fRawReader = NULL ;
151         }
152 }
153
154 //_____________________________________________________________________________
155 Bool_t AliQADataMakerSteer::DoIt(const AliQA::TASKINDEX_t taskIndex)
156 {
157         // Runs all the QA data Maker for every detector
158                 
159         Bool_t rv = kFALSE ;
160     // Fill QA data in event loop 
161         for (UInt_t iEvent = fFirstEvent ; iEvent < (UInt_t)fMaxEvents ; iEvent++) {
162                 fCurrentEvent++ ; 
163                 // Get the event
164                 if ( iEvent%10 == 0  ) 
165                         AliInfo(Form("processing event %d", iEvent));
166                 if ( taskIndex == AliQA::kRAWS ) {
167                         if ( !fRawReader->NextEvent() )
168                                 break ;
169                 } else if ( taskIndex == AliQA::kESDS ) {
170                         if ( fESDTree->GetEntry(iEvent) == 0 )
171                                 break ;
172                 } else {
173                         if ( fRunLoader->GetEvent(iEvent) != 0 )
174                                 break ;
175                 }
176                 // loop  over active loaders
177                 for (UInt_t iDet = 0; iDet < fgkNDetectors ; iDet++) {
178                         if (IsSelected(AliQA::GetDetName(iDet))) {
179                                 AliQADataMaker * qadm = GetQADataMaker(iDet) ;
180                                 if ( qadm->IsCycleDone() ) {
181                                         qadm->EndOfCycle(taskIndex) ;
182                                 }
183                                 TTree * data = NULL ; 
184                                 AliLoader* loader = GetLoader(qadm->GetUniqueID());
185                                 switch (taskIndex) {
186                                         case AliQA::kNULLTASKINDEX : 
187                                                 break ; 
188                                         case AliQA::kRAWS :
189                                                 qadm->Exec(taskIndex, fRawReader) ; 
190                                                 break ; 
191                                         case AliQA::kHITS :
192             if( loader ) {
193               loader->LoadHits() ; 
194               data = loader->TreeH() ; 
195               if ( ! data ) {
196                 AliWarning(Form(" Hit Tree not found for  %s", AliQA::GetDetName(iDet))) ; 
197                 break ; 
198               } 
199             } 
200             qadm->Exec(taskIndex, data) ;
201                                                 break ;
202                                                 case AliQA::kSDIGITS :
203             if( loader ) {      
204               loader->LoadSDigits() ; 
205               data = loader->TreeS() ; 
206               if ( ! data ) {
207                 AliWarning(Form(" SDigit Tree not found for  %s", AliQA::GetDetName(iDet))) ; 
208                 break ; 
209               } 
210             }
211             qadm->Exec(taskIndex, data) ; 
212                                                 break; 
213                                                 case AliQA::kDIGITS :
214             if( loader ) {      
215               loader->LoadDigits() ; 
216               data = loader->TreeD() ; 
217               if ( ! data ) {
218                 AliWarning(Form(" Digit Tree not found for  %s", AliQA::GetDetName(iDet))) ; 
219                 break ; 
220               } 
221             }
222             qadm->Exec(taskIndex, data) ;
223                                                 break; 
224                                                 case AliQA::kRECPOINTS :
225             if( loader ) {      
226               loader->LoadRecPoints() ; 
227               data = loader->TreeR() ; 
228               if (!data) {
229                 AliWarning(Form("RecPoints not found for %s", AliQA::GetDetName(iDet))) ; 
230                 break ; 
231               } 
232             }
233             qadm->Exec(taskIndex, data) ; 
234             break; 
235                                                 case AliQA::kTRACKSEGMENTS :
236                                                 break; 
237                                                 case AliQA::kRECPARTICLES :
238                                                 break; 
239                                                 case AliQA::kESDS :
240                                                 qadm->Exec(taskIndex, fESD) ;
241                                                 break; 
242                                                 case AliQA::kNTASKINDEX :
243                                                 break; 
244                                 } //task switch
245                         }
246                 } // detector loop
247     Increment() ; 
248         } // event loop 
249         // Save QA data for all detectors
250         rv = Finish(taskIndex) ;
251         
252         if ( taskIndex == AliQA::kRAWS ) 
253                 fRawReader->RewindEvents() ;
254
255         return rv ; 
256 }
257
258 //_____________________________________________________________________________
259 Bool_t AliQADataMakerSteer::Finish(const AliQA::TASKINDEX_t taskIndex) 
260 {
261         // write output to file for all detectors
262         for (UInt_t iDet = 0; iDet < fgkNDetectors ; iDet++) {
263                 if (IsSelected(AliQA::GetDetName(iDet))) {
264                         AliQADataMaker * qadm = GetQADataMaker(iDet) ;
265                         qadm->EndOfCycle(taskIndex) ; 
266                 }
267         }
268         return kTRUE ; 
269 }
270
271 //_____________________________________________________________________________
272 TObjArray * AliQADataMakerSteer::GetFromOCDB(AliQA::DETECTORINDEX_t det, AliQA::TASKINDEX_t task, const char * year) const 
273 {
274         // Retrieve the list of QA data for a given detector and a given task 
275         TObjArray * rv = NULL ;
276         if ( !strlen(AliQA::GetQARefStorage()) ) { 
277                 AliError("No storage defined, use AliQA::SetQARefStorage") ; 
278                 return NULL ; 
279         }       
280         AliCDBManager* man = AliCDBManager::Instance() ; 
281         if ( ! man->IsDefaultStorageSet() ) {
282                 TString tmp(AliQA::GetQARefDefaultStorage()) ; 
283                 tmp.Append(year) ; 
284                 tmp.Append("/") ; 
285                 man->SetDefaultStorage(tmp.Data()) ;            
286                 man->SetSpecificStorage(Form("%s/*", AliQA::GetQAName()), AliQA::GetQARefStorage()) ;
287         }
288         TString detOCDBDir(Form("%s/%s/%s", AliQA::GetQAName(), AliQA::GetDetName((Int_t)det), AliQA::GetRefOCDBDirName())) ; 
289         AliInfo(Form("Retrieving reference data from %s/%s for %s", AliQA::GetQARefStorage(), detOCDBDir.Data(), AliQA::GetTaskName(task).Data())) ; 
290         AliCDBEntry* entry = man->Get(detOCDBDir.Data(), 0) ; //FIXME 0 --> Run Number
291         TList * listDetQAD = dynamic_cast<TList *>(entry->GetObject()) ;
292         if ( listDetQAD ) 
293                 rv = dynamic_cast<TObjArray *>(listDetQAD->FindObject(AliQA::GetTaskName(task))) ; 
294         return rv ; 
295 }
296
297 //_____________________________________________________________________________
298 AliLoader * AliQADataMakerSteer::GetLoader(Int_t iDet)
299 {
300         // get the loader for a detector
301
302         if ( !fRunLoader || iDet == AliQA::kCORR) 
303                 return NULL ; 
304         
305         TString detName = AliQA::GetDetName(iDet) ;
306     fLoader[iDet] = fRunLoader->GetLoader(detName + "Loader");
307         if (fLoader[iDet]) 
308                 return fLoader[iDet] ;
309         
310         // load the QA data maker object
311         TPluginManager* pluginManager = gROOT->GetPluginManager() ;
312         TString loaderName = "Ali" + detName + "Loader" ;
313
314         AliLoader * loader = NULL ;
315         // first check if a plugin is defined for the quality assurance data maker
316         TPluginHandler* pluginHandler = pluginManager->FindHandler("AliLoader", detName) ;
317         // if not, add a plugin for it
318         if (!pluginHandler) {
319                 AliDebug(1, Form("defining plugin for %s", loaderName.Data())) ;
320                 TString libs = gSystem->GetLibraries() ;
321                 if (libs.Contains("lib" + detName + "base.so") || (gSystem->Load("lib" + detName + "base.so") >= 0)) {
322                         pluginManager->AddHandler("AliQADataMaker", detName, loaderName, detName + "loader", loaderName + "()") ;
323                 } else {
324                         pluginManager->AddHandler("AliLoader", detName, loaderName, detName, loaderName + "()") ;
325                 }
326                 pluginHandler = pluginManager->FindHandler("AliLoader", detName) ;
327         }
328         if (pluginHandler && (pluginHandler->LoadPlugin() == 0)) {
329                 loader = (AliLoader *) pluginHandler->ExecPlugin(0) ;
330         }
331         if (loader) 
332                 fLoader[iDet] = loader ;
333         return loader ;
334 }
335
336 //_____________________________________________________________________________
337 AliQADataMaker * AliQADataMakerSteer::GetQADataMaker(const Int_t iDet)
338 {
339         // get the quality assurance data maker for a detector
340         
341         if (fQADataMaker[iDet]) 
342                 return fQADataMaker[iDet] ;
343         
344         AliQADataMaker * qadm = NULL ;
345         
346         if (iDet == AliQA::kGLOBAL) { //Global QA
347                 qadm = new AliGlobalQADataMaker();
348                 qadm->SetName(AliQA::GetDetName(iDet));
349                 qadm->SetUniqueID(iDet);
350                 fQADataMaker[iDet] = qadm;
351                 return qadm;
352         }
353
354         if (iDet == AliQA::kCORR) { //the data maker for correlations among detectors
355     qadm = new AliCorrQADataMakerRec(fQADataMaker) ; 
356                 qadm->SetName(AliQA::GetDetName(iDet));
357                 qadm->SetUniqueID(iDet);
358                 fQADataMaker[iDet] = qadm;
359                 return qadm;
360   }
361
362         // load the QA data maker object
363         TPluginManager* pluginManager = gROOT->GetPluginManager() ;
364         TString detName = AliQA::GetDetName(iDet) ;
365         TString tmp(fMode) ; 
366         if (tmp.Contains("sim")) 
367                 tmp.ReplaceAll("s", "S") ; 
368         else if (tmp.Contains("rec")) 
369                 tmp.ReplaceAll("r", "R") ; 
370
371         TString qadmName = "Ali" + detName + "QADataMaker" + tmp ;
372
373         // first check if a plugin is defined for the quality assurance data maker
374         TPluginHandler* pluginHandler = pluginManager->FindHandler("AliQADataMaker", detName) ;
375         // if not, add a plugin for it
376         if (!pluginHandler) {
377                 AliDebug(1, Form("defining plugin for %s", qadmName.Data())) ;
378                 TString libs = gSystem->GetLibraries() ;
379                 if (libs.Contains("lib" + detName + fMode + ".so") || (gSystem->Load("lib" + detName + fMode + ".so") >= 0)) {
380                         pluginManager->AddHandler("AliQADataMaker", detName, qadmName, detName + "qadm", qadmName + "()") ;
381                 } else {
382                         pluginManager->AddHandler("AliQADataMaker", detName, qadmName, detName, qadmName + "()") ;
383                 }
384                 pluginHandler = pluginManager->FindHandler("AliQADataMaker", detName) ;
385         }
386         if (pluginHandler && (pluginHandler->LoadPlugin() == 0)) {
387                 qadm = (AliQADataMaker *) pluginHandler->ExecPlugin(0) ;
388         }
389         if (qadm) {
390                 qadm->SetName(AliQA::GetDetName(iDet));
391                 qadm->SetUniqueID(iDet);
392                 fQADataMaker[iDet] = qadm ;
393         }
394         
395                 return qadm ;
396 }
397
398 //_____________________________________________________________________________
399 void  AliQADataMakerSteer::EndOfCycle(TObjArray * detArray) 
400 {
401         // End of cycle QADataMakers 
402         
403         for (UInt_t iDet = 0; iDet < fgkNDetectors ; iDet++) {
404                 if (IsSelected(AliQA::GetDetName(iDet))) {
405                         AliQADataMaker * qadm = GetQADataMaker(iDet) ;
406                         if (!qadm) 
407                                 continue ;      
408                         // skip non active detectors
409                         if (detArray) {
410                                 AliModule* det = static_cast<AliModule*>(detArray->FindObject(AliQA::GetDetName(iDet))) ;
411                                 if (!det || !det->IsActive())  
412                                         continue ;
413                         }
414                         for (UInt_t taskIndex = 0; taskIndex < AliQA::kNTASKINDEX; taskIndex++) {
415                                 if ( fTasks.Contains(Form("%d", taskIndex)) ) 
416                                         qadm->EndOfCycle(AliQA::GetTaskIndex(AliQA::GetTaskName(taskIndex))) ;
417                         }
418                         qadm->Finish();
419                 }
420         }
421 }
422
423 //_____________________________________________________________________________
424 void AliQADataMakerSteer::Increment()
425 {
426   // Increments the cycle counter for all QA Data Makers
427         for (UInt_t iDet = 0; iDet < fgkNDetectors ; iDet++) {
428                 if (IsSelected(AliQA::GetDetName(iDet))) {
429                         AliQADataMaker * qadm = GetQADataMaker(iDet) ;
430                         if (qadm) 
431         qadm->Increment() ;
432     }
433   }
434 }
435   
436 //_____________________________________________________________________________
437 Bool_t AliQADataMakerSteer::Init(const AliQA::TASKINDEX_t taskIndex, const  char * input )
438 {
439         // Initialize the event source and QA data makers
440         
441         //fTasks = AliQA::GetTaskName(taskIndex) ; 
442
443         if (taskIndex == AliQA::kRAWS) { 
444                 if (!fRawReader) {
445                         fRawReader = AliRawReader::Create(input);
446                 }
447                 if ( ! fRawReader ) 
448                         return kFALSE ; 
449                 fRawReaderDelete = kTRUE ; 
450                 fRawReader->NextEvent() ; 
451                 fRunNumber = fRawReader->GetRunNumber() ; 
452                 AliCDBManager::Instance()->SetRun(fRunNumber) ; 
453                 fRawReader->RewindEvents();
454                 fNumberOfEvents = 999999 ;
455                 if ( fMaxEvents < 0 ) 
456                         fMaxEvents = fNumberOfEvents ; 
457                 } else if (taskIndex == AliQA::kESDS) {
458                         fTasks = AliQA::GetTaskName(AliQA::kESDS) ; 
459                 if (!gSystem->AccessPathName("AliESDs.root")) { // AliESDs.root exists
460                         TFile * esdFile = TFile::Open("AliESDs.root") ;
461                         fESDTree = dynamic_cast<TTree *> (esdFile->Get("esdTree")) ; 
462                         if ( !fESDTree ) {
463                                 AliError("esdTree not found") ; 
464                                 return kFALSE ; 
465                         } else {
466                                 fESD     = new AliESDEvent() ;
467                                 fESD->ReadFromTree(fESDTree) ;
468                                 fESDTree->GetEntry(0) ; 
469                                 fRunNumber = fESD->GetRunNumber() ; 
470                                 fNumberOfEvents = fESDTree->GetEntries() ;
471                                 if ( fMaxEvents < 0 ) 
472                                         fMaxEvents = fNumberOfEvents ; 
473                         }
474                 } else {
475                         AliError("AliESDs.root not found") ; 
476                         return kFALSE ; 
477                 }                       
478                 } else {
479                 if ( !InitRunLoader() ) { 
480                         AliWarning("No Run Loader not found") ; 
481                 } else {
482                         fNumberOfEvents = fRunLoader->GetNumberOfEvents() ;
483                         if ( fMaxEvents < 0 ) 
484                                 fMaxEvents = fNumberOfEvents ; 
485                 }
486         }
487
488         // Get Detectors 
489         TObjArray* detArray = NULL ; 
490         if (fRunLoader) // check if RunLoader exists 
491                 if ( fRunLoader->GetAliRun() ) { // check if AliRun exists in gAlice.root
492                         detArray = fRunLoader->GetAliRun()->Detectors() ;
493                         fRunNumber = fRunLoader->GetHeader()->GetRun() ; 
494                 }
495
496         // Initialize all QA data makers for all detectors
497         fRunNumber = AliCDBManager::Instance()->GetRun() ; 
498         if ( !  AliGeomManager::GetGeometry() ) 
499                 AliGeomManager::LoadGeometry() ; 
500         
501         InitQADataMaker(fRunNumber, fRecoParam, detArray) ; //, fCycleSame, kTRUE, detArray) ; 
502         return kTRUE ; 
503 }
504
505 //_____________________________________________________________________________
506 void  AliQADataMakerSteer::InitQADataMaker(UInt_t run, const AliRecoParam & par, TObjArray * detArray) 
507 {
508         // Initializes The QADataMaker for all active detectors and for all active tasks 
509
510         for (UInt_t iDet = 0; iDet < fgkNDetectors ; iDet++) {
511                 if (IsSelected(AliQA::GetDetName(iDet))) {
512                         AliQADataMaker * qadm = GetQADataMaker(iDet) ;
513                         if (!qadm) {
514                                 AliError(Form("AliQADataMaker not found for %s", AliQA::GetDetName(iDet))) ; 
515                                 fDetectorsW.ReplaceAll(AliQA::GetDetName(iDet), "") ; 
516                         } else {
517                                 AliDebug(1, Form("Data Maker found for %s", qadm->GetName())) ; 
518                                 // skip non active detectors
519                                 if (detArray) {
520                                         AliModule* det = static_cast<AliModule*>(detArray->FindObject(AliQA::GetDetName(iDet))) ;
521                                         if (!det || !det->IsActive())  
522                                                 continue ;
523                                 }
524               // Set default reco params
525                                 qadm->SetRecoParam(par.GetDetRecoParam(iDet));
526         Bool_t sameCycle = kFALSE ; 
527                                 for (UInt_t taskIndex = 0; taskIndex < AliQA::kNTASKINDEX; taskIndex++) {
528                                         if ( fTasks.Contains(Form("%d", taskIndex)) ) {
529             qadm->StartOfCycle(AliQA::GetTaskIndex(AliQA::GetTaskName(taskIndex)), run,  sameCycle) ;
530                                                 qadm->Init(AliQA::GetTaskIndex(AliQA::GetTaskName(taskIndex)), GetQACycles(qadm->GetUniqueID())) ;
531             sameCycle = kTRUE ;
532                                         }
533                                 }
534                         }
535                 }
536         }
537 }
538
539
540 //_____________________________________________________________________________
541 Bool_t AliQADataMakerSteer::InitRunLoader()
542 {
543         // get or create the run loader
544         if (fRunLoader) {
545                 fCycleSame = kTRUE ; 
546         } else {
547                 if (!gSystem->AccessPathName(fGAliceFileName.Data())) { // galice.root exists
548                         // load all base libraries to get the loader classes
549                         TString libs = gSystem->GetLibraries() ;
550                         for (UInt_t iDet = 0; iDet < fgkNDetectors; iDet++) {
551                                 if (!IsSelected(AliQA::GetDetName(iDet))) 
552                                         continue ; 
553                                 TString detName = AliQA::GetDetName(iDet) ;
554                                 if (detName == "HLT") 
555                                         continue;
556                                 if (libs.Contains("lib" + detName + "base.so")) 
557                                         continue;
558                                 gSystem->Load("lib" + detName + "base.so");
559                         }
560                         fRunLoader = AliRunLoader::Open(fGAliceFileName.Data());
561                         if (!fRunLoader) {
562                                 AliError(Form("no run loader found in file %s", fGAliceFileName.Data()));
563                                 return kFALSE;
564                         }
565                         fRunLoader->CdGAFile();
566                         if (fRunLoader->LoadgAlice() == 0) {
567                                 gAlice = fRunLoader->GetAliRun();
568                         }
569
570                         if (!gAlice) {
571                                 AliError(Form("no gAlice object found in file %s", fGAliceFileName.Data()));
572                                 return kFALSE;
573                         }
574
575                 } else {               // galice.root does not exist
576                         AliError(Form("the file %s does not exist", fGAliceFileName.Data()));
577                         return kFALSE;
578                 }
579         }
580
581         if (!fRunNumber) { 
582                 fRunLoader->LoadHeader();
583                 fRunNumber = fRunLoader->GetHeader()->GetRun() ; 
584         }
585         return kTRUE;
586 }
587
588 //_____________________________________________________________________________
589 Bool_t AliQADataMakerSteer::IsSelected(const char * det) 
590 {
591         // check whether detName is contained in detectors
592         // if yes, it is removed from detectors
593         
594         Bool_t rv = kFALSE;
595         const TString detName(det) ;
596   // always activates Correlation
597   if ( detName.Contains(AliQA::GetDetName(AliQA::kCORR))) {
598     rv = kTRUE ; 
599   } else {
600     // check if all detectors are selected
601     if (fDetectors.Contains("ALL")) {
602       fDetectors = "ALL";
603       rv = kTRUE;
604     } else if ((fDetectors.CompareTo(detName) == 0) ||
605                fDetectors.BeginsWith(detName+" ") ||
606                fDetectors.EndsWith(" "+detName) ||
607                fDetectors.Contains(" "+detName+" ")) {
608       rv = kTRUE;
609     }
610   }
611         return rv ;
612 }
613
614 //_____________________________________________________________________________
615 Bool_t AliQADataMakerSteer::Merge(const Int_t runNumber) const
616 {
617         // Merge data from all the cycles from all detectors in one single file per run
618         // Merge the QA results from all the data chunks in one run 
619  Bool_t rv = MergeData(runNumber) ; 
620  rv *= MergeResults(runNumber) ;
621  return rv ; 
622 }
623         
624         
625 //_____________________________________________________________________________
626 Bool_t AliQADataMakerSteer::MergeData(const Int_t runNumber) const
627 {
628         // Merge all the cycles from all detectors in one single file per run
629         TString cmd ;
630         if (runNumber == -1) 
631                 cmd = Form(".! ls *%s*.%d.*.root > tempo.txt", AliQA::GetQADataFileName(), runNumber) ; 
632         else 
633                 cmd = Form(".! ls *%s*.*.*.root > tempo.txt", AliQA::GetQADataFileName()) ; 
634         gROOT->ProcessLine(cmd.Data()) ;
635         ifstream in("tempo.txt") ; 
636         const Int_t runMax = 10 ;  
637         TString file[AliQA::kNDET*runMax] ;
638         Int_t run[AliQA::kNDET*runMax] = {-1} ;
639         
640         Int_t index = 0 ; 
641         while ( 1 ) {
642                 in >> file[index] ; 
643                 if ( !in.good() ) 
644                         break ; 
645                 AliInfo(Form("index = %d file = %s", index, (file[index]).Data())) ; 
646                 index++ ;
647         }
648         
649         if ( index == 0 ) { 
650                 AliError(Form("run number %d not found", runNumber)) ; 
651                 return kFALSE ; 
652         }
653         
654         Int_t runIndex    = 0 ;  
655         Int_t runIndexMax = 0 ; 
656         TString stmp(Form(".%s.", AliQA::GetQADataFileName())) ; 
657         for (Int_t ifile = 0 ; ifile < index ; ifile++) {
658                 TString tmp(file[ifile]) ; 
659                 tmp.ReplaceAll(".root", "") ; 
660                 TString det = tmp(0, tmp.Index(".")) ; 
661                 tmp.Remove(0, tmp.Index(stmp)+4) ; 
662                 TString ttmp = tmp(0, tmp.Index(".")) ; 
663                 Int_t newRun = ttmp.Atoi() ;
664                 for (Int_t irun = 0; irun <= runIndexMax; irun++) {
665                         if (newRun == run[irun]) 
666                                 break ; 
667                         run[runIndex] = newRun ; 
668                         runIndex++ ; 
669                 }
670                 runIndexMax = runIndex ; 
671                 ttmp = tmp(tmp.Index(".")+1, tmp.Length()) ; 
672                 Int_t cycle = ttmp.Atoi() ;  
673                 AliDebug(1, Form("%s : det = %s run = %d cycle = %d \n", file[ifile].Data(), det.Data(), newRun, cycle)) ; 
674         }
675         for (Int_t irun = 0 ; irun < runIndexMax ; irun++) {
676                 TFileMerger merger ; 
677                 TString outFileName(Form("Merged.%s.Data.%d.root",AliQA::GetQADataFileName(),run[irun]));               
678                 merger.OutputFile(outFileName.Data()) ; 
679                 for (Int_t ifile = 0 ; ifile < index-1 ; ifile++) {
680                         TString pattern(Form("%s.%d.", AliQA::GetQADataFileName(), run[irun])) ; 
681                         TString tmp(file[ifile]) ; 
682                         if (tmp.Contains(pattern)) {
683                                 merger.AddFile(tmp) ; 
684                         }
685                 }
686                 merger.Merge() ; 
687         }
688         
689         return kTRUE ; 
690 }
691
692 //_____________________________________________________________________________
693 Bool_t AliQADataMakerSteer::MergeResults(const Int_t runNumber) const
694 {
695         // Merge the QA result from all the data chunks in a run 
696         TString cmd ;
697         cmd = Form(".! ls %s*.root > tempo.txt", AliQA::GetQADataFileName()) ; 
698         gROOT->ProcessLine(cmd.Data()) ;
699         ifstream in("tempo.txt") ; 
700         const Int_t chunkMax = 100 ;  
701         TString fileList[chunkMax] ;
702         
703         Int_t index = 0 ; 
704         while ( 1 ) {
705                 TString file ; 
706                 in >> fileList[index] ; 
707                 if ( !in.good() ) 
708                         break ; 
709                 AliInfo(Form("index = %d file = %s", index, (fileList[index].Data()))) ; 
710                 index++ ;
711         }
712         
713         if ( index == 0 ) { 
714                 AliError("No QA Result File found") ; 
715                 return kFALSE ; 
716         }
717         
718         TFileMerger merger ; 
719         TString outFileName(Form("Merged.%s.Result.%d.root", AliQA::GetQADataFileName(), runNumber));           
720         merger.OutputFile(outFileName.Data()) ; 
721         for (Int_t ifile = 0 ; ifile < index ; ifile++) {
722                 TString file = fileList[ifile] ; 
723                 merger.AddFile(file) ; 
724         }
725         merger.Merge() ; 
726         
727         return kTRUE ; 
728 }
729
730 //_____________________________________________________________________________
731 void AliQADataMakerSteer::Reset(const Bool_t sameCycle)
732 {
733         // Reset the default data members
734
735         for (UInt_t iDet = 0; iDet < fgkNDetectors ; iDet++) {
736                 if (IsSelected(AliQA::GetDetName(iDet))) {
737                         AliQADataMaker * qadm = GetQADataMaker(iDet);
738                         qadm->Reset();
739                 }
740         } 
741         if (fRawReaderDelete) { 
742                 delete fRawReader ;
743                 fRawReader      = NULL ;
744         }
745
746         fCycleSame      = sameCycle ; 
747         fESD            = NULL ; 
748         fESDTree        = NULL ; 
749         //fFirst          = kTRUE ;   
750         fNumberOfEvents = 999999 ;  
751 }
752
753 //_____________________________________________________________________________
754 TString AliQADataMakerSteer::Run(const char * detectors, AliRawReader * rawReader, const Bool_t sameCycle) 
755 {
756         //Runs all the QA data Maker for Raws only
757         
758         fCycleSame       = sameCycle ;
759         fRawReader       = rawReader ;
760         fDetectors       = detectors ; 
761         fDetectorsW      = detectors ;  
762         
763         AliCDBManager* man = AliCDBManager::Instance() ; 
764
765         if ( man->GetRun() == -1 ) {// check if run number not set previously and set it from raw data
766                 rawReader->NextEvent() ; 
767                 man->SetRun(fRawReader->GetRunNumber()) ;
768                 rawReader->RewindEvents() ;
769         }       
770         
771         if ( !Init(AliQA::kRAWS) ) 
772                 return kFALSE ; 
773         fRawReaderDelete = kFALSE ; 
774
775         DoIt(AliQA::kRAWS) ; 
776         return  fDetectorsW ;
777 }
778
779 //_____________________________________________________________________________
780 TString AliQADataMakerSteer::Run(const char * detectors, const char * fileName, const Bool_t sameCycle) 
781 {
782         //Runs all the QA data Maker for Raws only
783
784         fCycleSame       = sameCycle ;
785         fDetectors       = detectors ; 
786         fDetectorsW      = detectors ;  
787         
788         AliCDBManager* man = AliCDBManager::Instance() ; 
789         if ( man->GetRun() == -1 ) { // check if run number not set previously and set it from AliRun
790                 AliRunLoader * rl = AliRunLoader::Open("galice.root") ;
791                 if ( ! rl ) {
792                         AliFatal("galice.root file not found in current directory") ; 
793                 } else {
794                         rl->CdGAFile() ; 
795                         rl->LoadgAlice() ;
796                         if ( ! rl->GetAliRun() ) {
797                                 AliFatal("AliRun not found in galice.root") ;
798                         } else {
799                                 rl->LoadHeader() ;
800                                 man->SetRun(rl->GetHeader()->GetRun());
801                         }
802                 }
803         }
804         
805         if ( !Init(AliQA::kRAWS, fileName) ) 
806                 return kFALSE ; 
807         
808         DoIt(AliQA::kRAWS) ; 
809         return  fDetectorsW ;
810 }
811
812 //_____________________________________________________________________________
813 TString AliQADataMakerSteer::Run(const char * detectors, const AliQA::TASKINDEX_t taskIndex, Bool_t const sameCycle, const  char * fileName ) 
814 {
815         // Runs all the QA data Maker for every detector
816         
817         fCycleSame       = sameCycle ;
818         fDetectors       = detectors ; 
819         fDetectorsW      = detectors ;          
820         
821         AliCDBManager* man = AliCDBManager::Instance() ;        
822         if ( man->GetRun() == -1 ) { // check if run number not set previously and set it from AliRun
823                 AliRunLoader * rl = AliRunLoader::Open("galice.root") ;
824                 if ( ! rl ) {
825                         AliFatal("galice.root file not found in current directory") ; 
826                 } else {
827                         rl->CdGAFile() ; 
828                         rl->LoadgAlice() ;
829                         if ( ! rl->GetAliRun() ) {
830                                 AliInfo("AliRun not found in galice.root") ;
831                         } else {
832                                 rl->LoadHeader() ;
833                                 man->SetRun(rl->GetHeader()->GetRun()) ;
834                         }
835                 }
836         }
837         
838
839         if ( taskIndex == AliQA::kNULLTASKINDEX) { 
840                 for (UInt_t task = 0; task < AliQA::kNTASKINDEX; task++) {
841                         if ( fTasks.Contains(Form("%d", task)) ) {
842                                 if ( !Init(AliQA::GetTaskIndex(AliQA::GetTaskName(task)), fileName) ) 
843                                         return kFALSE ; 
844                                 DoIt(AliQA::GetTaskIndex(AliQA::GetTaskName(task))) ;
845                         }
846                 }
847         } else {
848                 if ( !Init(taskIndex, fileName) ) 
849                         return kFALSE ; 
850                 DoIt(taskIndex) ; 
851         }               
852         
853         return fDetectorsW ;
854
855 }
856
857 //_____________________________________________________________________________
858 void AliQADataMakerSteer::RunOneEvent(AliRawReader * rawReader) 
859 {
860         //Runs all the QA data Maker for Raws only and on one event only (event loop done by calling method)
861   if ( ! rawReader ) 
862     return ; 
863         AliCodeTimerAuto("") ;
864   if (fTasks.Contains(Form("%d", AliQA::kRAWS))){
865     for (UInt_t iDet = 0; iDet < fgkNDetectors; iDet++) {
866       if (!IsSelected(AliQA::GetDetName(iDet))) 
867         continue;
868       AliQADataMaker *qadm = GetQADataMaker(iDet);  
869       if (!qadm) 
870         continue;
871       if ( qadm->IsCycleDone() ) {
872         qadm->EndOfCycle() ;
873       }
874       AliCodeTimerStart(Form("running RAW quality assurance data maker for %s", AliQA::GetDetName(iDet))); 
875                         qadm->Exec(AliQA::kRAWS, rawReader) ;
876       AliCodeTimerStop(Form("running RAW quality assurance data maker for %s", AliQA::GetDetName(iDet)));
877                 }
878   }
879 }
880
881 //_____________________________________________________________________________
882 void AliQADataMakerSteer::RunOneEvent(AliESDEvent *& esd) 
883 {
884         //Runs all the QA data Maker for ESDs only and on one event only (event loop done by calling method)
885         
886   AliCodeTimerAuto("") ;
887   if (fTasks.Contains(Form("%d", AliQA::kESDS))) {
888     for (UInt_t iDet = 0; iDet < fgkNDetectors; iDet++) {
889       if (!IsSelected(AliQA::GetDetName(iDet))) 
890         continue;
891       AliQADataMaker *qadm = GetQADataMaker(iDet);  
892       if (!qadm) 
893         continue;
894       if ( qadm->IsCycleDone() ) {
895         qadm->EndOfCycle() ;
896       }
897       AliCodeTimerStart(Form("running ESD quality assurance data maker for %s", AliQA::GetDetName(iDet)));
898                         qadm->Exec(AliQA::kESDS, esd) ;
899       AliCodeTimerStop(Form("running ESD quality assurance data maker for %s", AliQA::GetDetName(iDet)));
900                 }
901         }
902 }
903
904 //_____________________________________________________________________________
905 void AliQADataMakerSteer::RunOneEventInOneDetector(Int_t det, TTree * tree) 
906 {
907         // Runs all the QA data Maker for ESDs only and on one event only (event loop done by calling method)
908         AliCodeTimerAuto("") ;
909   if (fTasks.Contains(Form("%d", AliQA::kRECPOINTS))) {
910     if (IsSelected(AliQA::GetDetName(det))) {
911       AliQADataMaker *qadm = GetQADataMaker(det);  
912       if (qadm) { 
913         if ( qadm->IsCycleDone() ) {
914           qadm->EndOfCycle() ;
915         }
916         AliCodeTimerStart(Form("running RecPoints quality assurance data maker for %s", AliQA::GetDetName(det)));
917         qadm->Exec(AliQA::kRECPOINTS, tree) ;
918         AliCodeTimerStop(Form("running RecPoints quality assurance data maker for %s", AliQA::GetDetName(det)));
919       }
920     }
921   }
922 }
923
924 //_____________________________________________________________________________
925 Bool_t AliQADataMakerSteer::Save2OCDB(const Int_t runNumber, const char * year, const Int_t cycleNumber, const char * detectors) const
926 {
927         // take the locasl QA data merge into a single file and save in OCDB 
928         Bool_t rv = kTRUE ; 
929         TString tmp(AliQA::GetQARefStorage()) ; 
930         if ( tmp.IsNull() ) { 
931                 AliError("No storage defined, use AliQA::SetQARefStorage") ; 
932                 return kFALSE ; 
933         }
934         if ( !(tmp.Contains(AliQA::GetLabLocalOCDB()) || tmp.Contains(AliQA::GetLabAliEnOCDB())) ) {
935                 AliError(Form("%s is a wrong storage, use %s or %s", AliQA::GetQARefStorage(), AliQA::GetLabLocalOCDB().Data(), AliQA::GetLabAliEnOCDB().Data())) ; 
936                 return kFALSE ; 
937         }
938         TString sdet(detectors) ; 
939         sdet.ToUpper() ;
940         TFile * inputFile ; 
941         if ( sdet.Contains("ALL") ) {
942                 rv = Merge(runNumber) ; 
943                 if ( ! rv )
944                         return kFALSE ; 
945                 TString inputFileName(Form("Merged.%s.%d.root", AliQA::GetQADataFileName(), runNumber)) ; 
946                 inputFile = TFile::Open(inputFileName.Data()) ; 
947                 rv = SaveIt2OCDB(runNumber, inputFile, year) ; 
948         } else {
949                 for (Int_t index = 0; index < AliQA::kNDET; index++) {
950                         if (sdet.Contains(AliQA::GetDetName(index))) {
951                                 TString inputFileName(Form("%s.%s.%d.%d.root", AliQA::GetDetName(index), AliQA::GetQADataFileName(), runNumber, cycleNumber)) ; 
952                                 inputFile = TFile::Open(inputFileName.Data()) ;                         
953                                 rv *= SaveIt2OCDB(runNumber, inputFile, year) ; 
954                         }
955                 }
956         }
957         return rv ; 
958 }
959
960 //_____________________________________________________________________________
961 Bool_t AliQADataMakerSteer::SaveIt2OCDB(const Int_t runNumber, TFile * inputFile, const char * year) const
962 {
963         // reads the TH1 from file and adds it to appropriate list before saving to OCDB
964         Bool_t rv = kTRUE ;
965         AliInfo(Form("Saving TH1s in %s to %s", inputFile->GetName(), AliQA::GetQARefStorage())) ; 
966         AliCDBManager* man = AliCDBManager::Instance() ; 
967         if ( ! man->IsDefaultStorageSet() ) {
968                 TString tmp( AliQA::GetQARefStorage() ) ; 
969                 if ( tmp.Contains(AliQA::GetLabLocalOCDB()) ) 
970                         man->SetDefaultStorage(AliQA::GetQARefStorage()) ;
971                 else {
972                         TString tmp1(AliQA::GetQARefDefaultStorage()) ; 
973                         tmp1.Append(year) ; 
974                         tmp1.Append("?user=alidaq") ; 
975                         man->SetDefaultStorage(tmp1.Data()) ; 
976                 }
977         }
978         man->SetSpecificStorage("*", AliQA::GetQARefStorage()) ; 
979         if(man->GetRun() < 0) 
980                 man->SetRun(runNumber);
981
982         AliCDBMetaData mdr ;
983         mdr.SetResponsible("yves schutz");
984
985         for ( Int_t detIndex = 0 ; detIndex < AliQA::kNDET ; detIndex++) {
986                 TDirectory * detDir = inputFile->GetDirectory(AliQA::GetDetName(detIndex)) ; 
987                 if ( detDir ) {
988                         AliInfo(Form("Entering %s", detDir->GetName())) ;
989                         TString detOCDBDir(Form("%s/%s/%s", AliQA::GetDetName(detIndex), AliQA::GetRefOCDBDirName(), AliQA::GetRefDataDirName())) ; 
990                         AliCDBId idr(detOCDBDir.Data(), runNumber, AliCDBRunRange::Infinity())  ;
991                         TList * listDetQAD = new TList() ;
992                         TString listName(Form("%s QA data Reference", AliQA::GetDetName(detIndex))) ; 
993                         mdr.SetComment("HMPID QA stuff");
994                         listDetQAD->SetName(listName) ; 
995                         TList * taskList = detDir->GetListOfKeys() ; 
996                         TIter nextTask(taskList) ; 
997                         TKey * taskKey ; 
998                         while ( (taskKey = dynamic_cast<TKey*>(nextTask())) ) {
999                                 TDirectory * taskDir = detDir->GetDirectory(taskKey->GetName()) ; 
1000                                 AliInfo(Form("Saving %s", taskDir->GetName())) ; 
1001                                 TObjArray * listTaskQAD = new TObjArray(100) ; 
1002                                 listTaskQAD->SetName(taskKey->GetName()) ;
1003                                 listDetQAD->Add(listTaskQAD) ; 
1004                                 TList * histList = taskDir->GetListOfKeys() ; 
1005                                 TIter nextHist(histList) ; 
1006                                 TKey * histKey ; 
1007                                 while ( (histKey = dynamic_cast<TKey*>(nextHist())) ) {
1008                                         TObject * odata = taskDir->Get(histKey->GetName()) ; 
1009                                         if ( !odata ) {
1010                                                 AliError(Form("%s in %s/%s returns a NULL pointer !!", histKey->GetName(), detDir->GetName(), taskDir->GetName())) ;
1011                                         } else {
1012                                                 AliInfo(Form("Adding %s", histKey->GetName())) ;
1013                                                 if ( odata->IsA()->InheritsFrom("TH1") ) {
1014                                                         AliInfo(Form("Adding %s", histKey->GetName())) ;
1015                                                         TH1 * hdata = static_cast<TH1*>(odata) ; 
1016                                                         listTaskQAD->Add(hdata) ; 
1017                                                 }
1018                                         }
1019                                 }
1020                         }
1021                         man->Put(listDetQAD, idr, &mdr) ;
1022                 }
1023         }
1024         return rv ; 
1025 }       
1026
1027 //_____________________________________________________________________________
1028 void AliQADataMakerSteer::SetRecoParam(const char* detector, AliDetectorRecoParam *par)
1029 {
1030   // Set custom reconstruction parameters for a given detector
1031   // Single set of parameters for all the events
1032   for (UInt_t iDet = 0; iDet < fgkNDetectors; iDet++) {
1033     if(!strcmp(detector, AliQA::GetDetName(iDet))) {
1034       par->SetAsDefault();
1035       fRecoParam.AddDetRecoParam(iDet,par);
1036       break;
1037     }
1038   }
1039 }
1040
1041