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