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