]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliQADataMakerSteer.cxx
the initialisation of data makers was a the wrong place
[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                 fQADataMaker[iDet] = qadm;
340                 return qadm;
341         }
342
343         // load the QA data maker object
344         TPluginManager* pluginManager = gROOT->GetPluginManager() ;
345         TString detName = AliQA::GetDetName(iDet) ;
346         TString tmp(fMode) ; 
347         if (tmp.Contains("sim")) 
348                 tmp.ReplaceAll("s", "S") ; 
349         else if (tmp.Contains("rec")) 
350                 tmp.ReplaceAll("r", "R") ; 
351
352         TString qadmName = "Ali" + detName + "QADataMaker" + tmp ;
353
354         // first check if a plugin is defined for the quality assurance data maker
355         TPluginHandler* pluginHandler = pluginManager->FindHandler("AliQADataMaker", detName) ;
356         // if not, add a plugin for it
357         if (!pluginHandler) {
358                 AliDebug(1, Form("defining plugin for %s", qadmName.Data())) ;
359                 TString libs = gSystem->GetLibraries() ;
360                 if (libs.Contains("lib" + detName + fMode + ".so") || (gSystem->Load("lib" + detName + fMode + ".so") >= 0)) {
361                         pluginManager->AddHandler("AliQADataMaker", detName, qadmName, detName + "qadm", qadmName + "()") ;
362                 } else {
363                         pluginManager->AddHandler("AliQADataMaker", detName, qadmName, detName, qadmName + "()") ;
364                 }
365                 pluginHandler = pluginManager->FindHandler("AliQADataMaker", detName) ;
366         }
367         if (pluginHandler && (pluginHandler->LoadPlugin() == 0)) {
368                 qadm = (AliQADataMaker *) pluginHandler->ExecPlugin(0) ;
369         }
370         if (qadm) {
371                 fQADataMaker[iDet] = qadm ;
372                 qadm->SetName(AliQA::GetDetName(iDet));
373                 qadm->SetUniqueID(iDet);
374         }
375         
376                 return qadm ;
377 }
378
379 //_____________________________________________________________________________
380 void  AliQADataMakerSteer::EndOfCycle(TObjArray * detArray) 
381 {
382         // End of cycle QADataMakers 
383         
384         for (UInt_t iDet = 0; iDet < fgkNDetectors ; iDet++) {
385                 if (IsSelected(AliQA::GetDetName(iDet))) {
386                         AliQADataMaker * qadm = GetQADataMaker(iDet) ;
387                         if (!qadm) 
388                                 continue ;      
389                         // skip non active detectors
390                         if (detArray) {
391                                 AliModule* det = static_cast<AliModule*>(detArray->FindObject(AliQA::GetDetName(iDet))) ;
392                                 if (!det || !det->IsActive())  
393                                         continue ;
394                         }
395                         for (UInt_t taskIndex = 0; taskIndex < AliQA::kNTASKINDEX; taskIndex++) {
396                                 if ( fTasks.Contains(Form("%d", taskIndex)) ) 
397                                         qadm->EndOfCycle(AliQA::GetTaskIndex(AliQA::GetTaskName(taskIndex))) ;
398                         }
399                         qadm->Finish();
400                 }
401         }
402 }
403
404 //_____________________________________________________________________________
405 Bool_t AliQADataMakerSteer::Init(const AliQA::TASKINDEX_t taskIndex, const  char * input )
406 {
407         // Initialize the event source and QA data makers
408         
409         //fTasks = AliQA::GetTaskName(taskIndex) ; 
410
411         if (taskIndex == AliQA::kRAWS) { 
412                 if (!fRawReader) {
413                         fRawReader = AliRawReader::Create(input);
414                 }
415                 if ( ! fRawReader ) 
416                         return kFALSE ; 
417                 fRawReaderDelete = kTRUE ; 
418                 fRawReader->NextEvent() ; 
419                 fRunNumber = fRawReader->GetRunNumber() ; 
420                 AliCDBManager::Instance()->SetRun(fRunNumber) ; 
421                 fRawReader->RewindEvents();
422                 fNumberOfEvents = 999999 ;
423                 if ( fMaxEvents < 0 ) 
424                         fMaxEvents = fNumberOfEvents ; 
425                 } else if (taskIndex == AliQA::kESDS) {
426                         fTasks = AliQA::GetTaskName(AliQA::kESDS) ; 
427                 if (!gSystem->AccessPathName("AliESDs.root")) { // AliESDs.root exists
428                         TFile * esdFile = TFile::Open("AliESDs.root") ;
429                         fESDTree = dynamic_cast<TTree *> (esdFile->Get("esdTree")) ; 
430                         if ( !fESDTree ) {
431                                 AliError("esdTree not found") ; 
432                                 return kFALSE ; 
433                         } else {
434                                 fESD     = new AliESDEvent() ;
435                                 fESD->ReadFromTree(fESDTree) ;
436                                 fESDTree->GetEntry(0) ; 
437                                 fRunNumber = fESD->GetRunNumber() ; 
438                                 fNumberOfEvents = fESDTree->GetEntries() ;
439                                 if ( fMaxEvents < 0 ) 
440                                         fMaxEvents = fNumberOfEvents ; 
441                         }
442                 } else {
443                         AliError("AliESDs.root not found") ; 
444                         return kFALSE ; 
445                 }                       
446                 } else {
447                 if ( !InitRunLoader() ) { 
448                         AliWarning("No Run Loader not found") ; 
449                 } else {
450                         fNumberOfEvents = fRunLoader->GetNumberOfEvents() ;
451                         if ( fMaxEvents < 0 ) 
452                                 fMaxEvents = fNumberOfEvents ; 
453                 }
454         }
455
456         // Get Detectors 
457         TObjArray* detArray = NULL ; 
458         if (fRunLoader) // check if RunLoader exists 
459                 if ( fRunLoader->GetAliRun() ) { // check if AliRun exists in gAlice.root
460                         detArray = fRunLoader->GetAliRun()->Detectors() ;
461                         fRunNumber = fRunLoader->GetHeader()->GetRun() ; 
462                 }
463
464         // Initialize all QA data makers for all detectors
465         fRunNumber = AliCDBManager::Instance()->GetRun() ; 
466         if ( !  AliGeomManager::GetGeometry() ) 
467                 AliGeomManager::LoadGeometry() ; 
468         
469         InitQADataMaker(fRunNumber, fRecoParam, fCycleSame, detArray) ; 
470         return kTRUE ; 
471 }
472
473 //_____________________________________________________________________________
474 void  AliQADataMakerSteer::InitQADataMaker(UInt_t run, const AliRecoParam & par, Bool_t sameCycle, Bool_t startOption, TObjArray * detArray) 
475 {
476         // Initializes The QADataMaker for all active detectors and for all active tasks 
477
478         for (UInt_t iDet = 0; iDet < fgkNDetectors ; iDet++) {
479                 if (IsSelected(AliQA::GetDetName(iDet))) {
480                         AliQADataMaker * qadm = GetQADataMaker(iDet) ;
481                         if (!qadm) {
482                                 AliError(Form("AliQADataMaker not found for %s", AliQA::GetDetName(iDet))) ; 
483                                 fDetectorsW.ReplaceAll(AliQA::GetDetName(iDet), "") ; 
484                         } else {
485                                 AliDebug(1, Form("Data Maker found for %s", qadm->GetName())) ; 
486                                 // skip non active detectors
487                                 if (detArray) {
488                                         AliModule* det = static_cast<AliModule*>(detArray->FindObject(AliQA::GetDetName(iDet))) ;
489                                         if (!det || !det->IsActive())  
490                                                 continue ;
491                                 }
492               // Set default reco params
493                                 qadm->SetRecoParam(par.GetDetRecoParam(iDet));
494                                 for (UInt_t taskIndex = 0; taskIndex < AliQA::kNTASKINDEX; taskIndex++) {
495                                         if ( fTasks.Contains(Form("%d", taskIndex)) ) {
496                                                 qadm->Init(AliQA::GetTaskIndex(AliQA::GetTaskName(taskIndex)), run, GetQACycles(qadm->GetUniqueID())) ;
497                                                 if (startOption) {
498                                                         qadm->StartOfCycle(AliQA::GetTaskIndex(AliQA::GetTaskName(taskIndex)), sameCycle) ;
499                                                         sameCycle = kTRUE ;
500                                                 }
501                                         }
502                                 }
503                         }
504                 }
505         }
506 }
507
508 //_____________________________________________________________________________
509 Bool_t AliQADataMakerSteer::InitRunLoader()
510 {
511         // get or create the run loader
512         if (fRunLoader) {
513                 fCycleSame = kTRUE ; 
514         } else {
515                 if (!gSystem->AccessPathName(fGAliceFileName.Data())) { // galice.root exists
516                         // load all base libraries to get the loader classes
517                         TString libs = gSystem->GetLibraries() ;
518                         for (UInt_t iDet = 0; iDet < fgkNDetectors; iDet++) {
519                                 if (!IsSelected(AliQA::GetDetName(iDet))) 
520                                         continue ; 
521                                 TString detName = AliQA::GetDetName(iDet) ;
522                                 if (detName == "HLT") 
523                                         continue;
524                                 if (libs.Contains("lib" + detName + "base.so")) 
525                                         continue;
526                                 gSystem->Load("lib" + detName + "base.so");
527                         }
528                         fRunLoader = AliRunLoader::Open(fGAliceFileName.Data());
529                         if (!fRunLoader) {
530                                 AliError(Form("no run loader found in file %s", fGAliceFileName.Data()));
531                                 return kFALSE;
532                         }
533                         fRunLoader->CdGAFile();
534                         if (fRunLoader->LoadgAlice() == 0) {
535                                 gAlice = fRunLoader->GetAliRun();
536                         }
537
538                         if (!gAlice) {
539                                 AliError(Form("no gAlice object found in file %s", fGAliceFileName.Data()));
540                                 return kFALSE;
541                         }
542
543                 } else {               // galice.root does not exist
544                         AliError(Form("the file %s does not exist", fGAliceFileName.Data()));
545                         return kFALSE;
546                 }
547         }
548
549         if (!fRunNumber) { 
550                 fRunLoader->LoadHeader();
551                 fRunNumber = fRunLoader->GetHeader()->GetRun() ; 
552         }
553         return kTRUE;
554 }
555
556 //_____________________________________________________________________________
557 Bool_t AliQADataMakerSteer::IsSelected(const char * det) 
558 {
559         // check whether detName is contained in detectors
560         // if yes, it is removed from detectors
561         
562         Bool_t rv = kFALSE;
563         const TString detName(det) ;
564         // check if all detectors are selected
565         if (fDetectors.Contains("ALL")) {
566                 fDetectors = "ALL";
567                 rv = kTRUE;
568         } else if ((fDetectors.CompareTo(detName) == 0) ||
569                            fDetectors.BeginsWith(detName+" ") ||
570                            fDetectors.EndsWith(" "+detName) ||
571                            fDetectors.Contains(" "+detName+" ")) {
572                 rv = kTRUE;
573         }
574                 
575         return rv ;
576 }
577
578 //_____________________________________________________________________________
579 Bool_t AliQADataMakerSteer::Merge(const Int_t runNumber) const
580 {
581         // Merge all the cycles from all detectors in one single file per run
582         TString cmd ;
583         if ( runNumber == -1 )
584                 cmd = Form(".! ls *%s*.*.*.root > tempo.txt", AliQA::GetQADataFileName()) ; 
585         else 
586                 cmd = Form(".! ls *%s*.%d.*.root > tempo.txt", AliQA::GetQADataFileName(), runNumber) ; 
587         gROOT->ProcessLine(cmd.Data()) ;
588         ifstream in("tempo.txt") ; 
589         const Int_t runMax = 10 ;  
590         TString file[AliQA::kNDET*runMax] ;
591         Int_t run[AliQA::kNDET*runMax] = {-1} ;
592         
593         Int_t index = 0 ; 
594         while ( 1 ) {
595                 in >> file[index] ; 
596                 if ( !in.good() ) 
597                         break ; 
598                 AliInfo(Form("index = %d file = %s", index, (file[index]).Data())) ; 
599                 index++ ;
600         }
601         
602         if ( index == 0 ) { 
603                 AliError(Form("run number %d not found", runNumber)) ; 
604                 return kFALSE ; 
605         }
606         
607         Int_t runIndex    = 0 ;  
608         Int_t runIndexMax = 0 ; 
609         TString stmp(Form(".%s.", AliQA::GetQADataFileName())) ; 
610         for (Int_t ifile = 0 ; ifile < index ; ifile++) {
611                 TString tmp(file[ifile]) ; 
612                 tmp.ReplaceAll(".root", "") ; 
613                 TString det = tmp(0, tmp.Index(".")) ; 
614                 tmp.Remove(0, tmp.Index(stmp)+4) ; 
615                 TString ttmp = tmp(0, tmp.Index(".")) ; 
616                 Int_t newRun = ttmp.Atoi() ;
617                 for (Int_t irun = 0; irun <= runIndexMax; irun++) {
618                         if (newRun == run[irun]) 
619                                 break ; 
620                         run[runIndex] = newRun ; 
621                         runIndex++ ; 
622                 }
623                 runIndexMax = runIndex ; 
624                 ttmp = tmp(tmp.Index(".")+1, tmp.Length()) ; 
625                 Int_t cycle = ttmp.Atoi() ;  
626                 AliDebug(1, Form("%s : det = %s run = %d cycle = %d \n", file[ifile].Data(), det.Data(), newRun, cycle)) ; 
627         }
628         for (Int_t irun = 0 ; irun < runIndexMax ; irun++) {
629                 TFileMerger merger ; 
630                 TString outFileName(Form("Merged.%s.%d.root",AliQA::GetQADataFileName(),run[irun]));            
631                 merger.OutputFile(outFileName.Data()) ; 
632                 for (Int_t ifile = 0 ; ifile < index-1 ; ifile++) {
633                         TString pattern(Form("%s.%d.", AliQA::GetQADataFileName(), run[irun])) ; 
634                         TString tmp(file[ifile]) ; 
635                         if (tmp.Contains(pattern)) {
636                                 merger.AddFile(tmp) ; 
637                         }
638                 }
639                 merger.Merge() ; 
640         }
641         
642         return kTRUE ; 
643 }
644
645 //_____________________________________________________________________________
646 void AliQADataMakerSteer::Reset(const Bool_t sameCycle)
647 {
648         // Reset the default data members
649
650         for (UInt_t iDet = 0; iDet < fgkNDetectors ; iDet++) {
651                 if (IsSelected(AliQA::GetDetName(iDet))) {
652                         AliQADataMaker * qadm = GetQADataMaker(iDet);
653                         qadm->Reset(sameCycle);
654                 }
655         } 
656         if (fRawReaderDelete) { 
657                 delete fRawReader ;
658                 fRawReader      = NULL ;
659         }
660
661         fCycleSame      = sameCycle ; 
662         fESD            = NULL ; 
663         fESDTree        = NULL ; 
664         //fFirst          = kTRUE ;   
665         fNumberOfEvents = 999999 ;  
666 }
667
668 //_____________________________________________________________________________
669 TString AliQADataMakerSteer::Run(const char * detectors, AliRawReader * rawReader, const Bool_t sameCycle) 
670 {
671         //Runs all the QA data Maker for Raws only
672         
673         fCycleSame       = sameCycle ;
674         fRawReader       = rawReader ;
675         fDetectors       = detectors ; 
676         fDetectorsW      = detectors ;  
677         
678         AliCDBManager* man = AliCDBManager::Instance() ; 
679
680         if ( man->GetRun() == -1 ) {// check if run number not set previously and set it from raw data
681                 rawReader->NextEvent() ; 
682                 man->SetRun(fRawReader->GetRunNumber()) ;
683                 rawReader->RewindEvents() ;
684         }       
685         
686         if ( !Init(AliQA::kRAWS) ) 
687                 return kFALSE ; 
688         fRawReaderDelete = kFALSE ; 
689
690         DoIt(AliQA::kRAWS) ; 
691         return  fDetectorsW ;
692 }
693
694 //_____________________________________________________________________________
695 TString AliQADataMakerSteer::Run(const char * detectors, const char * fileName, const Bool_t sameCycle) 
696 {
697         //Runs all the QA data Maker for Raws only
698
699         fCycleSame       = sameCycle ;
700         fDetectors       = detectors ; 
701         fDetectorsW      = detectors ;  
702         
703         AliCDBManager* man = AliCDBManager::Instance() ; 
704         if ( man->GetRun() == -1 ) { // check if run number not set previously and set it from AliRun
705                 AliRunLoader * rl = AliRunLoader::Open("galice.root") ;
706                 if ( ! rl ) {
707                         AliFatal("galice.root file not found in current directory") ; 
708                 } else {
709                         rl->CdGAFile() ; 
710                         rl->LoadgAlice() ;
711                         if ( ! rl->GetAliRun() ) {
712                                 AliFatal("AliRun not found in galice.root") ;
713                         } else {
714                                 rl->LoadHeader() ;
715                                 man->SetRun(rl->GetHeader()->GetRun());
716                         }
717                 }
718         }
719         
720         if ( !Init(AliQA::kRAWS, fileName) ) 
721                 return kFALSE ; 
722         
723         DoIt(AliQA::kRAWS) ; 
724         return  fDetectorsW ;
725 }
726
727 //_____________________________________________________________________________
728 TString AliQADataMakerSteer::Run(const char * detectors, const AliQA::TASKINDEX_t taskIndex, Bool_t const sameCycle, const  char * fileName ) 
729 {
730         // Runs all the QA data Maker for every detector
731         
732         fCycleSame       = sameCycle ;
733         fDetectors       = detectors ; 
734         fDetectorsW      = detectors ;          
735         
736         AliCDBManager* man = AliCDBManager::Instance() ;        
737         if ( man->GetRun() == -1 ) { // check if run number not set previously and set it from AliRun
738                 AliRunLoader * rl = AliRunLoader::Open("galice.root") ;
739                 if ( ! rl ) {
740                         AliFatal("galice.root file not found in current directory") ; 
741                 } else {
742                         rl->CdGAFile() ; 
743                         rl->LoadgAlice() ;
744                         if ( ! rl->GetAliRun() ) {
745                                 AliInfo("AliRun not found in galice.root") ;
746                         } else {
747                                 rl->LoadHeader() ;
748                                 man->SetRun(rl->GetHeader()->GetRun()) ;
749                         }
750                 }
751         }
752         
753
754         if ( taskIndex == AliQA::kNULLTASKINDEX) { 
755                 for (UInt_t task = 0; task < AliQA::kNTASKINDEX; task++) {
756                         if ( fTasks.Contains(Form("%d", task)) ) {
757                                 if ( !Init(AliQA::GetTaskIndex(AliQA::GetTaskName(task)), fileName) ) 
758                                         return kFALSE ; 
759                                 DoIt(AliQA::GetTaskIndex(AliQA::GetTaskName(task))) ;
760                         }
761                 }
762         } else {
763                 if ( !Init(taskIndex, fileName) ) 
764                         return kFALSE ; 
765                 DoIt(taskIndex) ; 
766         }               
767         
768         return fDetectorsW ;
769
770 }
771
772 //_____________________________________________________________________________
773 void AliQADataMakerSteer::RunOneEvent(AliRawReader * rawReader) 
774 {
775         //Runs all the QA data Maker for Raws only and on one event only (event loop done by calling method)
776
777         AliCodeTimerAuto("") ;
778         for (UInt_t iDet = 0; iDet < fgkNDetectors; iDet++) {
779                 if (!IsSelected(AliQA::GetDetName(iDet))) 
780                         continue;
781                 AliQADataMaker *qadm = GetQADataMaker(iDet);  
782                 if (!qadm) 
783                         continue;
784                 if (fTasks.Contains(Form("%d", AliQA::kRAWS))){
785                         AliCodeTimerStart(Form("running RAW quality assurance data maker for %s", AliQA::GetDetName(iDet))); 
786                         qadm->Exec(AliQA::kRAWS, rawReader) ;
787                         AliCodeTimerStop(Form("running RAW quality assurance data maker for %s", AliQA::GetDetName(iDet)));
788                 }
789         }
790 }
791
792 //_____________________________________________________________________________
793 void AliQADataMakerSteer::RunOneEvent(AliESDEvent *& esd) 
794 {
795         //Runs all the QA data Maker for ESDs only and on one event only (event loop done by calling method)
796         
797   AliCodeTimerAuto("") ;
798         for (UInt_t iDet = 0; iDet < fgkNDetectors; iDet++) {
799                 if (!IsSelected(AliQA::GetDetName(iDet))) 
800                         continue;
801                 AliQADataMaker *qadm = GetQADataMaker(iDet);  
802                 if (!qadm) 
803                         continue;
804                 if (fTasks.Contains(Form("%d", AliQA::kESDS))) {
805                         AliCodeTimerStart(Form("running ESD quality assurance data maker for %s", AliQA::GetDetName(iDet)));
806                         qadm->Exec(AliQA::kESDS, esd) ;
807                         AliCodeTimerStop(Form("running ESD quality assurance data maker for %s", AliQA::GetDetName(iDet)));
808                 }
809         }
810 }
811
812 //_____________________________________________________________________________
813 void AliQADataMakerSteer::RunOneEventInOneDetector(Int_t det, TTree * tree) 
814 {
815         // Runs all the QA data Maker for ESDs only and on one event only (event loop done by calling method)
816         AliCodeTimerAuto("") ;
817         if (!IsSelected(AliQA::GetDetName(det))) {
818                 AliQADataMaker *qadm = GetQADataMaker(det);  
819                 if (qadm) { 
820                         if (fTasks.Contains(Form("%d", AliQA::kRECPOINTS))) {
821                                 AliCodeTimerStart(Form("running RecPoints quality assurance data maker for %s", AliQA::GetDetName(det)));
822                                 qadm->Exec(AliQA::kRECPOINTS, tree) ;
823                                 AliCodeTimerStop(Form("running RecPoints quality assurance data maker for %s", AliQA::GetDetName(det)));
824                         }
825                 }
826         }
827 }
828
829 //_____________________________________________________________________________
830 Bool_t AliQADataMakerSteer::Save2OCDB(const Int_t runNumber, const char * year, const Int_t cycleNumber, const char * detectors) const
831 {
832         // take the locasl QA data merge into a single file and save in OCDB 
833         Bool_t rv = kTRUE ; 
834         TString tmp(AliQA::GetQARefStorage()) ; 
835         if ( tmp.IsNull() ) { 
836                 AliError("No storage defined, use AliQA::SetQARefStorage") ; 
837                 return kFALSE ; 
838         }
839         if ( !(tmp.Contains(AliQA::GetLabLocalOCDB()) || tmp.Contains(AliQA::GetLabAliEnOCDB())) ) {
840                 AliError(Form("%s is a wrong storage, use %s or %s", AliQA::GetQARefStorage(), AliQA::GetLabLocalOCDB().Data(), AliQA::GetLabAliEnOCDB().Data())) ; 
841                 return kFALSE ; 
842         }
843         TString sdet(detectors) ; 
844         sdet.ToUpper() ;
845         TFile * inputFile ; 
846         if ( sdet.Contains("ALL") ) {
847                 rv = Merge(runNumber) ; 
848                 if ( ! rv )
849                         return kFALSE ; 
850                 TString inputFileName(Form("Merged.%s.%d.root", AliQA::GetQADataFileName(), runNumber)) ; 
851                 inputFile = TFile::Open(inputFileName.Data()) ; 
852                 rv = SaveIt2OCDB(runNumber, inputFile, year) ; 
853         } else {
854                 for (Int_t index = 0; index < AliQA::kNDET; index++) {
855                         if (sdet.Contains(AliQA::GetDetName(index))) {
856                                 TString inputFileName(Form("%s.%s.%d.%d.root", AliQA::GetDetName(index), AliQA::GetQADataFileName(), runNumber, cycleNumber)) ; 
857                                 inputFile = TFile::Open(inputFileName.Data()) ;                         
858                                 rv *= SaveIt2OCDB(runNumber, inputFile, year) ; 
859                         }
860                 }
861         }
862         return rv ; 
863 }
864
865 //_____________________________________________________________________________
866 Bool_t AliQADataMakerSteer::SaveIt2OCDB(const Int_t runNumber, TFile * inputFile, const char * year) const
867 {
868         // reads the TH1 from file and adds it to appropriate list before saving to OCDB
869         Bool_t rv = kTRUE ;
870         AliInfo(Form("Saving TH1s in %s to %s", inputFile->GetName(), AliQA::GetQARefStorage())) ; 
871         AliCDBManager* man = AliCDBManager::Instance() ; 
872         if ( ! man->IsDefaultStorageSet() ) {
873                 TString tmp( AliQA::GetQARefStorage() ) ; 
874                 if ( tmp.Contains(AliQA::GetLabLocalOCDB()) ) 
875                         man->SetDefaultStorage(AliQA::GetQARefStorage()) ;
876                 else {
877                         TString tmp1(AliQA::GetQARefDefaultStorage()) ; 
878                         tmp1.Append(year) ; 
879                         tmp1.Append("?user=alidaq") ; 
880                         man->SetDefaultStorage(tmp1.Data()) ; 
881                 }
882         }
883         man->SetSpecificStorage("*", AliQA::GetQARefStorage()) ; 
884         if(man->GetRun() < 0) 
885                 man->SetRun(runNumber);
886
887         AliCDBMetaData mdr ;
888         mdr.SetResponsible("yves schutz");
889
890         for ( Int_t detIndex = 0 ; detIndex < AliQA::kNDET ; detIndex++) {
891                 TDirectory * detDir = inputFile->GetDirectory(AliQA::GetDetName(detIndex)) ; 
892                 if ( detDir ) {
893                         AliInfo(Form("Entering %s", detDir->GetName())) ;
894                         TString detOCDBDir(Form("%s/%s/%s", AliQA::GetDetName(detIndex), AliQA::GetRefOCDBDirName(), AliQA::GetRefDataDirName())) ; 
895                         AliCDBId idr(detOCDBDir.Data(), runNumber, AliCDBRunRange::Infinity())  ;
896                         TList * listDetQAD = new TList() ;
897                         TString listName(Form("%s QA data Reference", AliQA::GetDetName(detIndex))) ; 
898                         mdr.SetComment("HMPID QA stuff");
899                         listDetQAD->SetName(listName) ; 
900                         TList * taskList = detDir->GetListOfKeys() ; 
901                         TIter nextTask(taskList) ; 
902                         TKey * taskKey ; 
903                         while ( (taskKey = dynamic_cast<TKey*>(nextTask())) ) {
904                                 TDirectory * taskDir = detDir->GetDirectory(taskKey->GetName()) ; 
905                                 AliInfo(Form("Saving %s", taskDir->GetName())) ; 
906                                 TObjArray * listTaskQAD = new TObjArray(100) ; 
907                                 listTaskQAD->SetName(taskKey->GetName()) ;
908                                 listDetQAD->Add(listTaskQAD) ; 
909                                 TList * histList = taskDir->GetListOfKeys() ; 
910                                 TIter nextHist(histList) ; 
911                                 TKey * histKey ; 
912                                 while ( (histKey = dynamic_cast<TKey*>(nextHist())) ) {
913                                         TObject * odata = taskDir->Get(histKey->GetName()) ; 
914                                         if ( !odata ) {
915                                                 AliError(Form("%s in %s/%s returns a NULL pointer !!", histKey->GetName(), detDir->GetName(), taskDir->GetName())) ;
916                                         } else {
917                                                 AliInfo(Form("Adding %s", histKey->GetName())) ;
918                                                 if ( odata->IsA()->InheritsFrom("TH1") ) {
919                                                         AliInfo(Form("Adding %s", histKey->GetName())) ;
920                                                         TH1 * hdata = static_cast<TH1*>(odata) ; 
921                                                         listTaskQAD->Add(hdata) ; 
922                                                 }
923                                         }
924                                 }
925                         }
926                         man->Put(listDetQAD, idr, &mdr) ;
927                 }
928         }
929         return rv ; 
930 }       
931
932 //_____________________________________________________________________________
933 void AliQADataMakerSteer::SetRecoParam(const char* detector, AliDetectorRecoParam *par)
934 {
935   // Set custom reconstruction parameters for a given detector
936   // Single set of parameters for all the events
937   for (UInt_t iDet = 0; iDet < fgkNDetectors; iDet++) {
938     if(!strcmp(detector, AliQA::GetDetName(iDet))) {
939       par->SetAsDefault();
940       fRecoParam.AddDetRecoParam(iDet,par);
941       break;
942     }
943   }
944 }
945
946