]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliQADataMakerSteer.cxx
stopped a loop in merging of QA Results
[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 data from all the cycles from all detectors in one single file per run
584         // Merge the QA results from all the data chunks in one run 
585  Bool_t rv = MergeData(runNumber) ; 
586  rv *= MergeResults(runNumber) ;
587  return rv ; 
588 }
589         
590         
591 //_____________________________________________________________________________
592 Bool_t AliQADataMakerSteer::MergeData(const Int_t runNumber) const
593 {
594         // Merge all the cycles from all detectors in one single file per run
595         TString cmd ;
596         if (runNumber == -1) 
597                 cmd = Form(".! ls *%s*.%d.*.root > tempo.txt", AliQA::GetQADataFileName(), runNumber) ; 
598         else 
599                 cmd = Form(".! ls *%s*.*.*.root > tempo.txt", AliQA::GetQADataFileName()) ; 
600         gROOT->ProcessLine(cmd.Data()) ;
601         ifstream in("tempo.txt") ; 
602         const Int_t runMax = 10 ;  
603         TString file[AliQA::kNDET*runMax] ;
604         Int_t run[AliQA::kNDET*runMax] = {-1} ;
605         
606         Int_t index = 0 ; 
607         while ( 1 ) {
608                 in >> file[index] ; 
609                 if ( !in.good() ) 
610                         break ; 
611                 AliInfo(Form("index = %d file = %s", index, (file[index]).Data())) ; 
612                 index++ ;
613         }
614         
615         if ( index == 0 ) { 
616                 AliError(Form("run number %d not found", runNumber)) ; 
617                 return kFALSE ; 
618         }
619         
620         Int_t runIndex    = 0 ;  
621         Int_t runIndexMax = 0 ; 
622         TString stmp(Form(".%s.", AliQA::GetQADataFileName())) ; 
623         for (Int_t ifile = 0 ; ifile < index ; ifile++) {
624                 TString tmp(file[ifile]) ; 
625                 tmp.ReplaceAll(".root", "") ; 
626                 TString det = tmp(0, tmp.Index(".")) ; 
627                 tmp.Remove(0, tmp.Index(stmp)+4) ; 
628                 TString ttmp = tmp(0, tmp.Index(".")) ; 
629                 Int_t newRun = ttmp.Atoi() ;
630                 for (Int_t irun = 0; irun <= runIndexMax; irun++) {
631                         if (newRun == run[irun]) 
632                                 break ; 
633                         run[runIndex] = newRun ; 
634                         runIndex++ ; 
635                 }
636                 runIndexMax = runIndex ; 
637                 ttmp = tmp(tmp.Index(".")+1, tmp.Length()) ; 
638                 Int_t cycle = ttmp.Atoi() ;  
639                 AliDebug(1, Form("%s : det = %s run = %d cycle = %d \n", file[ifile].Data(), det.Data(), newRun, cycle)) ; 
640         }
641         for (Int_t irun = 0 ; irun < runIndexMax ; irun++) {
642                 TFileMerger merger ; 
643                 TString outFileName(Form("Merged.%s.Data.%d.root",AliQA::GetQADataFileName(),run[irun]));               
644                 merger.OutputFile(outFileName.Data()) ; 
645                 for (Int_t ifile = 0 ; ifile < index-1 ; ifile++) {
646                         TString pattern(Form("%s.%d.", AliQA::GetQADataFileName(), run[irun])) ; 
647                         TString tmp(file[ifile]) ; 
648                         if (tmp.Contains(pattern)) {
649                                 merger.AddFile(tmp) ; 
650                         }
651                 }
652                 merger.Merge() ; 
653         }
654         
655         return kTRUE ; 
656 }
657
658 //_____________________________________________________________________________
659 Bool_t AliQADataMakerSteer::MergeResults(const Int_t runNumber) const
660 {
661         // Merge the QA result from all the data chunks in a run 
662         TString cmd ;
663         cmd = Form(".! ls %s*.root > tempo.txt", AliQA::GetQADataFileName()) ; 
664         gROOT->ProcessLine(cmd.Data()) ;
665         ifstream in("tempo.txt") ; 
666         const Int_t chunkMax = 100 ;  
667         TString fileList[chunkMax] ;
668         
669         Int_t index = 0 ; 
670         while ( 1 ) {
671                 TString file ; 
672                 in >> fileList[index] ; 
673                 if ( !in.good() ) 
674                         break ; 
675                 AliInfo(Form("index = %d file = %s", index, (fileList[index].Data()))) ; 
676                 index++ ;
677         }
678         
679         if ( index == 0 ) { 
680                 AliError("No QA Result File found") ; 
681                 return kFALSE ; 
682         }
683         
684         TFileMerger merger ; 
685         TString outFileName(Form("Merged.%s.Result.%d.root", AliQA::GetQADataFileName(), runNumber));           
686         merger.OutputFile(outFileName.Data()) ; 
687         for (Int_t ifile = 0 ; ifile < index ; ifile++) {
688                 TString file = fileList[ifile] ; 
689                 merger.AddFile(file) ; 
690         }
691         merger.Merge() ; 
692         
693         return kTRUE ; 
694 }
695
696 //_____________________________________________________________________________
697 void AliQADataMakerSteer::Reset(const Bool_t sameCycle)
698 {
699         // Reset the default data members
700
701         for (UInt_t iDet = 0; iDet < fgkNDetectors ; iDet++) {
702                 if (IsSelected(AliQA::GetDetName(iDet))) {
703                         AliQADataMaker * qadm = GetQADataMaker(iDet);
704                         qadm->Reset(sameCycle);
705                 }
706         } 
707         if (fRawReaderDelete) { 
708                 delete fRawReader ;
709                 fRawReader      = NULL ;
710         }
711
712         fCycleSame      = sameCycle ; 
713         fESD            = NULL ; 
714         fESDTree        = NULL ; 
715         //fFirst          = kTRUE ;   
716         fNumberOfEvents = 999999 ;  
717 }
718
719 //_____________________________________________________________________________
720 TString AliQADataMakerSteer::Run(const char * detectors, AliRawReader * rawReader, const Bool_t sameCycle) 
721 {
722         //Runs all the QA data Maker for Raws only
723         
724         fCycleSame       = sameCycle ;
725         fRawReader       = rawReader ;
726         fDetectors       = detectors ; 
727         fDetectorsW      = detectors ;  
728         
729         AliCDBManager* man = AliCDBManager::Instance() ; 
730
731         if ( man->GetRun() == -1 ) {// check if run number not set previously and set it from raw data
732                 rawReader->NextEvent() ; 
733                 man->SetRun(fRawReader->GetRunNumber()) ;
734                 rawReader->RewindEvents() ;
735         }       
736         
737         if ( !Init(AliQA::kRAWS) ) 
738                 return kFALSE ; 
739         fRawReaderDelete = kFALSE ; 
740
741         DoIt(AliQA::kRAWS) ; 
742         return  fDetectorsW ;
743 }
744
745 //_____________________________________________________________________________
746 TString AliQADataMakerSteer::Run(const char * detectors, const char * fileName, const Bool_t sameCycle) 
747 {
748         //Runs all the QA data Maker for Raws only
749
750         fCycleSame       = sameCycle ;
751         fDetectors       = detectors ; 
752         fDetectorsW      = detectors ;  
753         
754         AliCDBManager* man = AliCDBManager::Instance() ; 
755         if ( man->GetRun() == -1 ) { // check if run number not set previously and set it from AliRun
756                 AliRunLoader * rl = AliRunLoader::Open("galice.root") ;
757                 if ( ! rl ) {
758                         AliFatal("galice.root file not found in current directory") ; 
759                 } else {
760                         rl->CdGAFile() ; 
761                         rl->LoadgAlice() ;
762                         if ( ! rl->GetAliRun() ) {
763                                 AliFatal("AliRun not found in galice.root") ;
764                         } else {
765                                 rl->LoadHeader() ;
766                                 man->SetRun(rl->GetHeader()->GetRun());
767                         }
768                 }
769         }
770         
771         if ( !Init(AliQA::kRAWS, fileName) ) 
772                 return kFALSE ; 
773         
774         DoIt(AliQA::kRAWS) ; 
775         return  fDetectorsW ;
776 }
777
778 //_____________________________________________________________________________
779 TString AliQADataMakerSteer::Run(const char * detectors, const AliQA::TASKINDEX_t taskIndex, Bool_t const sameCycle, const  char * fileName ) 
780 {
781         // Runs all the QA data Maker for every detector
782         
783         fCycleSame       = sameCycle ;
784         fDetectors       = detectors ; 
785         fDetectorsW      = detectors ;          
786         
787         AliCDBManager* man = AliCDBManager::Instance() ;        
788         if ( man->GetRun() == -1 ) { // check if run number not set previously and set it from AliRun
789                 AliRunLoader * rl = AliRunLoader::Open("galice.root") ;
790                 if ( ! rl ) {
791                         AliFatal("galice.root file not found in current directory") ; 
792                 } else {
793                         rl->CdGAFile() ; 
794                         rl->LoadgAlice() ;
795                         if ( ! rl->GetAliRun() ) {
796                                 AliInfo("AliRun not found in galice.root") ;
797                         } else {
798                                 rl->LoadHeader() ;
799                                 man->SetRun(rl->GetHeader()->GetRun()) ;
800                         }
801                 }
802         }
803         
804
805         if ( taskIndex == AliQA::kNULLTASKINDEX) { 
806                 for (UInt_t task = 0; task < AliQA::kNTASKINDEX; task++) {
807                         if ( fTasks.Contains(Form("%d", task)) ) {
808                                 if ( !Init(AliQA::GetTaskIndex(AliQA::GetTaskName(task)), fileName) ) 
809                                         return kFALSE ; 
810                                 DoIt(AliQA::GetTaskIndex(AliQA::GetTaskName(task))) ;
811                         }
812                 }
813         } else {
814                 if ( !Init(taskIndex, fileName) ) 
815                         return kFALSE ; 
816                 DoIt(taskIndex) ; 
817         }               
818         
819         return fDetectorsW ;
820
821 }
822
823 //_____________________________________________________________________________
824 void AliQADataMakerSteer::RunOneEvent(AliRawReader * rawReader) 
825 {
826         //Runs all the QA data Maker for Raws only and on one event only (event loop done by calling method)
827
828         AliCodeTimerAuto("") ;
829         for (UInt_t iDet = 0; iDet < fgkNDetectors; iDet++) {
830                 if (!IsSelected(AliQA::GetDetName(iDet))) 
831                         continue;
832                 AliQADataMaker *qadm = GetQADataMaker(iDet);  
833                 if (!qadm) 
834                         continue;
835                 if (fTasks.Contains(Form("%d", AliQA::kRAWS))){
836                         AliCodeTimerStart(Form("running RAW quality assurance data maker for %s", AliQA::GetDetName(iDet))); 
837                         qadm->Exec(AliQA::kRAWS, rawReader) ;
838                         AliCodeTimerStop(Form("running RAW quality assurance data maker for %s", AliQA::GetDetName(iDet)));
839                 }
840         }
841 }
842
843 //_____________________________________________________________________________
844 void AliQADataMakerSteer::RunOneEvent(AliESDEvent *& esd) 
845 {
846         //Runs all the QA data Maker for ESDs only and on one event only (event loop done by calling method)
847         
848   AliCodeTimerAuto("") ;
849         for (UInt_t iDet = 0; iDet < fgkNDetectors; iDet++) {
850                 if (!IsSelected(AliQA::GetDetName(iDet))) 
851                         continue;
852                 AliQADataMaker *qadm = GetQADataMaker(iDet);  
853                 if (!qadm) 
854                         continue;
855                 if (fTasks.Contains(Form("%d", AliQA::kESDS))) {
856                         AliCodeTimerStart(Form("running ESD quality assurance data maker for %s", AliQA::GetDetName(iDet)));
857                         qadm->Exec(AliQA::kESDS, esd) ;
858                         AliCodeTimerStop(Form("running ESD quality assurance data maker for %s", AliQA::GetDetName(iDet)));
859                 }
860         }
861 }
862
863 //_____________________________________________________________________________
864 void AliQADataMakerSteer::RunOneEventInOneDetector(Int_t det, TTree * tree) 
865 {
866         // Runs all the QA data Maker for ESDs only and on one event only (event loop done by calling method)
867         AliCodeTimerAuto("") ;
868         if (!IsSelected(AliQA::GetDetName(det))) {
869                 AliQADataMaker *qadm = GetQADataMaker(det);  
870                 if (qadm) { 
871                         if (fTasks.Contains(Form("%d", AliQA::kRECPOINTS))) {
872                                 AliCodeTimerStart(Form("running RecPoints quality assurance data maker for %s", AliQA::GetDetName(det)));
873                                 qadm->Exec(AliQA::kRECPOINTS, tree) ;
874                                 AliCodeTimerStop(Form("running RecPoints quality assurance data maker for %s", AliQA::GetDetName(det)));
875                         }
876                 }
877         }
878 }
879
880 //_____________________________________________________________________________
881 Bool_t AliQADataMakerSteer::Save2OCDB(const Int_t runNumber, const char * year, const Int_t cycleNumber, const char * detectors) const
882 {
883         // take the locasl QA data merge into a single file and save in OCDB 
884         Bool_t rv = kTRUE ; 
885         TString tmp(AliQA::GetQARefStorage()) ; 
886         if ( tmp.IsNull() ) { 
887                 AliError("No storage defined, use AliQA::SetQARefStorage") ; 
888                 return kFALSE ; 
889         }
890         if ( !(tmp.Contains(AliQA::GetLabLocalOCDB()) || tmp.Contains(AliQA::GetLabAliEnOCDB())) ) {
891                 AliError(Form("%s is a wrong storage, use %s or %s", AliQA::GetQARefStorage(), AliQA::GetLabLocalOCDB().Data(), AliQA::GetLabAliEnOCDB().Data())) ; 
892                 return kFALSE ; 
893         }
894         TString sdet(detectors) ; 
895         sdet.ToUpper() ;
896         TFile * inputFile ; 
897         if ( sdet.Contains("ALL") ) {
898                 rv = Merge(runNumber) ; 
899                 if ( ! rv )
900                         return kFALSE ; 
901                 TString inputFileName(Form("Merged.%s.%d.root", AliQA::GetQADataFileName(), runNumber)) ; 
902                 inputFile = TFile::Open(inputFileName.Data()) ; 
903                 rv = SaveIt2OCDB(runNumber, inputFile, year) ; 
904         } else {
905                 for (Int_t index = 0; index < AliQA::kNDET; index++) {
906                         if (sdet.Contains(AliQA::GetDetName(index))) {
907                                 TString inputFileName(Form("%s.%s.%d.%d.root", AliQA::GetDetName(index), AliQA::GetQADataFileName(), runNumber, cycleNumber)) ; 
908                                 inputFile = TFile::Open(inputFileName.Data()) ;                         
909                                 rv *= SaveIt2OCDB(runNumber, inputFile, year) ; 
910                         }
911                 }
912         }
913         return rv ; 
914 }
915
916 //_____________________________________________________________________________
917 Bool_t AliQADataMakerSteer::SaveIt2OCDB(const Int_t runNumber, TFile * inputFile, const char * year) const
918 {
919         // reads the TH1 from file and adds it to appropriate list before saving to OCDB
920         Bool_t rv = kTRUE ;
921         AliInfo(Form("Saving TH1s in %s to %s", inputFile->GetName(), AliQA::GetQARefStorage())) ; 
922         AliCDBManager* man = AliCDBManager::Instance() ; 
923         if ( ! man->IsDefaultStorageSet() ) {
924                 TString tmp( AliQA::GetQARefStorage() ) ; 
925                 if ( tmp.Contains(AliQA::GetLabLocalOCDB()) ) 
926                         man->SetDefaultStorage(AliQA::GetQARefStorage()) ;
927                 else {
928                         TString tmp1(AliQA::GetQARefDefaultStorage()) ; 
929                         tmp1.Append(year) ; 
930                         tmp1.Append("?user=alidaq") ; 
931                         man->SetDefaultStorage(tmp1.Data()) ; 
932                 }
933         }
934         man->SetSpecificStorage("*", AliQA::GetQARefStorage()) ; 
935         if(man->GetRun() < 0) 
936                 man->SetRun(runNumber);
937
938         AliCDBMetaData mdr ;
939         mdr.SetResponsible("yves schutz");
940
941         for ( Int_t detIndex = 0 ; detIndex < AliQA::kNDET ; detIndex++) {
942                 TDirectory * detDir = inputFile->GetDirectory(AliQA::GetDetName(detIndex)) ; 
943                 if ( detDir ) {
944                         AliInfo(Form("Entering %s", detDir->GetName())) ;
945                         TString detOCDBDir(Form("%s/%s/%s", AliQA::GetDetName(detIndex), AliQA::GetRefOCDBDirName(), AliQA::GetRefDataDirName())) ; 
946                         AliCDBId idr(detOCDBDir.Data(), runNumber, AliCDBRunRange::Infinity())  ;
947                         TList * listDetQAD = new TList() ;
948                         TString listName(Form("%s QA data Reference", AliQA::GetDetName(detIndex))) ; 
949                         mdr.SetComment("HMPID QA stuff");
950                         listDetQAD->SetName(listName) ; 
951                         TList * taskList = detDir->GetListOfKeys() ; 
952                         TIter nextTask(taskList) ; 
953                         TKey * taskKey ; 
954                         while ( (taskKey = dynamic_cast<TKey*>(nextTask())) ) {
955                                 TDirectory * taskDir = detDir->GetDirectory(taskKey->GetName()) ; 
956                                 AliInfo(Form("Saving %s", taskDir->GetName())) ; 
957                                 TObjArray * listTaskQAD = new TObjArray(100) ; 
958                                 listTaskQAD->SetName(taskKey->GetName()) ;
959                                 listDetQAD->Add(listTaskQAD) ; 
960                                 TList * histList = taskDir->GetListOfKeys() ; 
961                                 TIter nextHist(histList) ; 
962                                 TKey * histKey ; 
963                                 while ( (histKey = dynamic_cast<TKey*>(nextHist())) ) {
964                                         TObject * odata = taskDir->Get(histKey->GetName()) ; 
965                                         if ( !odata ) {
966                                                 AliError(Form("%s in %s/%s returns a NULL pointer !!", histKey->GetName(), detDir->GetName(), taskDir->GetName())) ;
967                                         } else {
968                                                 AliInfo(Form("Adding %s", histKey->GetName())) ;
969                                                 if ( odata->IsA()->InheritsFrom("TH1") ) {
970                                                         AliInfo(Form("Adding %s", histKey->GetName())) ;
971                                                         TH1 * hdata = static_cast<TH1*>(odata) ; 
972                                                         listTaskQAD->Add(hdata) ; 
973                                                 }
974                                         }
975                                 }
976                         }
977                         man->Put(listDetQAD, idr, &mdr) ;
978                 }
979         }
980         return rv ; 
981 }       
982
983 //_____________________________________________________________________________
984 void AliQADataMakerSteer::SetRecoParam(const char* detector, AliDetectorRecoParam *par)
985 {
986   // Set custom reconstruction parameters for a given detector
987   // Single set of parameters for all the events
988   for (UInt_t iDet = 0; iDet < fgkNDetectors; iDet++) {
989     if(!strcmp(detector, AliQA::GetDetName(iDet))) {
990       par->SetAsDefault();
991       fRecoParam.AddDetRecoParam(iDet,par);
992       break;
993     }
994   }
995 }
996
997