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