replace Rec/Sim by rec/sim
[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 #include <TKey.h>
19 #include <TFile.h>
20 #include <TFileMerger.h>
21 #include <TPluginManager.h>
22 #include <TROOT.h>
23 #include <TString.h>
24 #include <TSystem.h>
25
26 #include "AliCDBManager.h"
27 #include "AliCDBEntry.h"
28 #include "AliCDBId.h"
29 #include "AliCDBMetaData.h"
30 #include "AliESDEvent.h"
31 #include "AliHeader.h"
32 #include "AliLog.h"
33 #include "AliModule.h"
34 #include "AliQA.h"
35 #include "AliQADataMakerRec.h"
36 #include "AliQADataMakerSim.h"
37 #include "AliQADataMakerSteer.h" 
38 #include "AliRawReaderDate.h"
39 #include "AliRawReaderFile.h"
40 #include "AliRawReaderRoot.h"
41 #include "AliRun.h"
42 #include "AliRunLoader.h"
43
44 ClassImp(AliQADataMakerSteer) 
45
46 //_____________________________________________________________________________
47 AliQADataMakerSteer::AliQADataMakerSteer(const char* gAliceFilename, const char * name, const char * title) :
48         TNamed(name, title), 
49         fCycleSame(kFALSE),
50     fDetectors("ALL"), 
51         fESD(NULL), 
52         fESDTree(NULL),
53         fFirst(kTRUE),  
54         fGAliceFileName(gAliceFilename), 
55         fRunNumber(0), 
56         fNumberOfEvents(999999), 
57         fRawReader(NULL), 
58         fRawReaderDelete(kTRUE), 
59         fRunLoader(NULL)  
60 {
61         // default ctor
62         for (UInt_t iDet = 0; iDet < fgkNDetectors; iDet++) {
63                 if (IsSelected(AliQA::GetDetName(iDet))) {
64                         fLoader[iDet]      = NULL ;
65                         fQADataMaker[iDet] = NULL ;
66                         fQACycles[iDet]    = 999999 ;
67                 }
68         }       
69 }
70
71 //_____________________________________________________________________________
72 AliQADataMakerSteer::AliQADataMakerSteer(const AliQADataMakerSteer & qas) : 
73         TNamed(qas), 
74         fCycleSame(kFALSE),
75     fDetectors(qas.fDetectors), 
76         fESD(NULL), 
77         fESDTree(NULL), 
78         fFirst(qas.fFirst),  
79         fGAliceFileName(qas.fGAliceFileName), 
80         fRunNumber(qas.fRunNumber), 
81         fNumberOfEvents(qas.fNumberOfEvents), 
82         fRawReader(NULL), 
83         fRawReaderDelete(kTRUE), 
84         fRunLoader(NULL)  
85 {
86         // cpy ctor
87         for (UInt_t iDet = 0; iDet < fgkNDetectors; iDet++) {
88                 fLoader[iDet]      = qas.fLoader[iDet] ;
89                 fQADataMaker[iDet] = qas.fQADataMaker[iDet] ;
90                 fQACycles[iDet]    = qas.fQACycles[iDet] ;      
91         }
92 }
93
94 //_____________________________________________________________________________
95 AliQADataMakerSteer & AliQADataMakerSteer::operator = (const AliQADataMakerSteer & qas) 
96 {
97         // assignment operator
98   this->~AliQADataMakerSteer() ;
99   new(this) AliQADataMakerSteer(qas) ;
100   return *this ;
101 }
102
103 //_____________________________________________________________________________
104 AliQADataMakerSteer::~AliQADataMakerSteer() 
105 {
106         // dtor
107   for (UInt_t iDet = 0; iDet < fgkNDetectors; iDet++) {
108           if (IsSelected(AliQA::GetDetName(iDet))) {
109                   fLoader[iDet] = NULL;
110                   if (fQADataMaker[iDet]) {
111                           (fQADataMaker[iDet])->Finish() ; 
112                           delete fQADataMaker[iDet] ;
113                           fQADataMaker[iDet] = NULL ;
114                   }
115           }
116   }
117
118   if (fRawReaderDelete) { 
119         fRunLoader = NULL ;
120         delete fRawReader ;
121         fRawReader = NULL ;
122   }
123 }
124
125 //_____________________________________________________________________________
126 Bool_t AliQADataMakerSteer::DoIt(const AliQA::TASKINDEX taskIndex, const char * mode)
127 {
128         // Runs all the QA data Maker for every detector
129         Bool_t rv = kFALSE ;
130         
131     // Fill QA data in event loop 
132         for (UInt_t iEvent = 0 ; iEvent < fNumberOfEvents ; iEvent++) {
133                 // Get the event
134                 AliDebug(1, Form("processing event %d", iEvent));
135                 if ( taskIndex == AliQA::kRAWS ) {
136                         if ( !fRawReader->NextEvent() )
137                                 break ;
138                 } else if ( taskIndex == AliQA::kESDS ) {
139                         if ( fESDTree->GetEntry(iEvent) == 0 )
140                                 break ;
141                 } else {
142                         if ( fRunLoader->GetEvent(iEvent) != 0 )
143                                 break ;
144                 }
145                 // loop over detectors
146                 TObjArray* detArray = NULL ; 
147                 if (fRunLoader) // check if RunLoader exists 
148                         if ( fRunLoader->GetAliRun() ) // check if AliRun exists in gAlice.root
149                                 detArray = fRunLoader->GetAliRun()->Detectors() ;
150                 for (UInt_t iDet = 0 ; iDet < fgkNDetectors ; iDet++) {
151                         if (detArray) {
152                                 AliModule* det = static_cast<AliModule*>(detArray->FindObject(AliQA::GetDetName(iDet))) ;
153                                 if (!det || !det->IsActive()) 
154                                         continue ;
155                         }
156                         if (!IsSelected(AliQA::GetDetName(iDet)))
157                                 continue ;
158                         AliQADataMaker * qadm = GetQADataMaker(iDet, mode) ;
159                         if (!qadm) {
160                                 rv = kFALSE ;
161                         } else {
162                                 if ( qadm->IsCycleDone() ) {
163                                         qadm->EndOfCycle(AliQA::kRAWS) ;
164                                         qadm->StartOfCycle(AliQA::kRAWS) ;
165                                 }
166                                 TTree * data ; 
167                                 switch (taskIndex) {
168                                         case AliQA::kRAWS :
169                                                 qadm->Exec(taskIndex, fRawReader) ; 
170                                                 break ; 
171                                         case AliQA::kHITS :
172                                                 GetLoader(iDet)->LoadHits() ; 
173                                                 data = GetLoader(iDet)->TreeH() ; 
174                                                 if ( ! data ) {
175                                                         AliWarning(Form(" Hit Tree not found for  %s", AliQA::GetDetName(iDet))) ; 
176                                                 } else {
177                                                         qadm->Exec(taskIndex, data) ;
178                                                 } 
179                                                 break ;
180                                                 case AliQA::kSDIGITS :
181                                                 GetLoader(iDet)->LoadSDigits() ; 
182                                                 data = GetLoader(iDet)->TreeS() ; 
183                                                 if ( ! data ) {
184                                                         AliWarning(Form(" SDigit Tree not found for  %s", AliQA::GetDetName(iDet))) ; 
185                                                 } else {
186                                                         qadm->Exec(taskIndex, data) ; 
187                                                 }
188                                                 break; 
189                                                 case AliQA::kDIGITS :
190                                                 GetLoader(iDet)->LoadDigits() ; 
191                                                 data = GetLoader(iDet)->TreeD() ; 
192                                                 if ( ! data ) {
193                                                         AliWarning(Form(" Digit Tree not found for  %s", AliQA::GetDetName(iDet))) ; 
194                                                 } else {
195                                                         qadm->Exec(taskIndex, data) ;
196                                                 }
197                                                 break; 
198                                                 case AliQA::kRECPOINTS :
199                                                 GetLoader(iDet)->LoadRecPoints() ; 
200                                                 data = GetLoader(iDet)->TreeR() ; 
201                                                 if (!data) {
202                                                         AliWarning(Form("RecPoints not found for %s", AliQA::GetDetName(iDet))) ; 
203                                                 } else {
204                                                         qadm->Exec(taskIndex, data) ; 
205                                                 }
206                                                 break; 
207                                                 case AliQA::kTRACKSEGMENTS :
208                                                 break; 
209                                                 case AliQA::kRECPARTICLES :
210                                                 break; 
211                                                 case AliQA::kESDS :
212                                                 qadm->Exec(taskIndex, fESD) ;
213                                                 break; 
214                                                 case AliQA::kNTASKINDEX :
215                                                 break; 
216                                 } //task switch
217                                 qadm->Increment() ; 
218                         } //data maker exist
219                 } // detector loop
220         } // event loop 
221         // Save QA data for all detectors
222         rv = Finish(taskIndex, mode) ;
223         return rv ; 
224 }
225
226 //_____________________________________________________________________________
227 Bool_t AliQADataMakerSteer::Finish(const AliQA::TASKINDEX taskIndex, const char * mode) 
228 {
229         // write output to file for all detectors
230         for (UInt_t iDet = 0; iDet < fgkNDetectors ; iDet++) {
231                 if (IsSelected(AliQA::GetDetName(iDet))) {
232                         AliQADataMaker * qadm = GetQADataMaker(iDet, mode) ;
233                         if (qadm) {
234                                 qadm->EndOfCycle(taskIndex) ; 
235                         }
236                 }
237         }
238         return kTRUE ; 
239 }
240
241 //_____________________________________________________________________________
242 TObjArray * AliQADataMakerSteer::GetFromOCDB(AliQA::DETECTORINDEX det, AliQA::TASKINDEX task) const 
243 {
244         // Retrieve the list of QA data for a given detector and a given task 
245         TObjArray * rv = NULL ;
246         TString tmp(AliQA::GetQARefStorage()) ; 
247         if ( tmp.IsNull() ) { 
248                 AliError("No storage defined, use AliQA::SetQARefStorage") ; 
249                 return NULL ; 
250         }       
251         AliCDBManager* man = AliCDBManager::Instance() ; 
252         if ( ! man->IsDefaultStorageSet() ) {
253                 man->SetDefaultStorage(AliQA::GetQARefDefaultStorage()) ; 
254                 man->SetSpecificStorage(Form("%s/*", AliQA::GetQAOCDBDirName()), AliQA::GetQARefStorage()) ;
255         }
256         char detOCDBDir[10] ; 
257         sprintf(detOCDBDir, "%s/%s/%s", AliQA::GetQAOCDBDirName(), AliQA::GetDetName((Int_t)det), AliQA::GetRefOCDBDirName()) ; 
258         AliInfo(Form("Retrieving reference data from %s/%s for %s", AliQA::GetQARefStorage(), detOCDBDir, AliQA::GetTaskName(task).Data())) ; 
259         AliCDBEntry* entry = man->Get(detOCDBDir, 0) ; //FIXME 0 --> Run Number
260         TList * listDetQAD = dynamic_cast<TList *>(entry->GetObject()) ;
261         if ( listDetQAD ) 
262                 rv = dynamic_cast<TObjArray *>(listDetQAD->FindObject(AliQA::GetTaskName(task))) ; 
263         return rv ; 
264 }
265
266 //_____________________________________________________________________________
267 AliLoader * AliQADataMakerSteer::GetLoader(Int_t iDet)
268 {
269         // get the loader for a detector
270         
271         TString detName = AliQA::GetDetName(iDet) ;
272     fLoader[iDet] = fRunLoader->GetLoader(detName + "Loader");
273         if (fLoader[iDet]) 
274                 return fLoader[iDet] ;
275         
276         // load the QA data maker object
277         TPluginManager* pluginManager = gROOT->GetPluginManager() ;
278         TString loaderName = "Ali" + detName + "Loader" ;
279
280         AliLoader * loader = NULL ;
281         // first check if a plugin is defined for the quality assurance data maker
282         TPluginHandler* pluginHandler = pluginManager->FindHandler("AliLoader", detName) ;
283         // if not, add a plugin for it
284         if (!pluginHandler) {
285                 AliDebug(1, Form("defining plugin for %s", loaderName.Data())) ;
286                 TString libs = gSystem->GetLibraries() ;
287                 if (libs.Contains("lib" + detName + "base.so") || (gSystem->Load("lib" + detName + "base.so") >= 0)) {
288                         pluginManager->AddHandler("AliQADataMaker", detName, loaderName, detName + "loader", loaderName + "()") ;
289                 } else {
290                         pluginManager->AddHandler("AliLoader", detName, loaderName, detName, loaderName + "()") ;
291                 }
292                 pluginHandler = pluginManager->FindHandler("AliLoader", detName) ;
293         }
294         if (pluginHandler && (pluginHandler->LoadPlugin() == 0)) {
295                 loader = (AliLoader *) pluginHandler->ExecPlugin(0) ;
296         }
297         if (loader) 
298                 fLoader[iDet] = loader ;
299         return loader ;
300 }
301
302 //_____________________________________________________________________________
303 AliQADataMaker * AliQADataMakerSteer::GetQADataMaker(const Int_t iDet, const char * mode )
304 {
305         // get the quality assurance data maker for a detector
306         
307         if (fQADataMaker[iDet]) 
308                 return fQADataMaker[iDet] ;
309         
310         AliQADataMaker * qadm = NULL ;
311         
312         if (fFirst) {
313                 // load the QA data maker object
314                 TPluginManager* pluginManager = gROOT->GetPluginManager() ;
315                 TString detName = AliQA::GetDetName(iDet) ;
316                 TString qadmName = "Ali" + detName + "QADataMaker" + mode ;
317
318                 // first check if a plugin is defined for the quality assurance data maker
319                 TPluginHandler* pluginHandler = pluginManager->FindHandler("AliQADataMaker", detName) ;
320                 // if not, add a plugin for it
321                 if (!pluginHandler) {
322                         AliDebug(1, Form("defining plugin for %s", qadmName.Data())) ;
323                         TString libs = gSystem->GetLibraries() ;
324                         if (libs.Contains("lib" + detName + mode + ".so") || (gSystem->Load("lib" + detName + mode + ".so") >= 0)) {
325                                 pluginManager->AddHandler("AliQADataMaker", detName, qadmName, detName + "qadm", qadmName + "()") ;
326                         } else {
327                                 pluginManager->AddHandler("AliQADataMaker", detName, qadmName, detName, qadmName + "()") ;
328                         }
329                         pluginHandler = pluginManager->FindHandler("AliQADataMaker", detName) ;
330                 }
331                 if (pluginHandler && (pluginHandler->LoadPlugin() == 0)) {
332                         qadm = (AliQADataMaker *) pluginHandler->ExecPlugin(0) ;
333                 }
334                 if (qadm) 
335                         fQADataMaker[iDet] = qadm ;
336         }
337         return qadm ;
338 }
339
340 //_____________________________________________________________________________
341 Bool_t AliQADataMakerSteer::Init(const AliQA::TASKINDEX taskIndex, const char * mode, const  char * input )
342 {
343         // Initialize the event source and QA data makers
344         
345         if (taskIndex == AliQA::kRAWS) { 
346                 if (!fRawReader) {
347                         TString fileName(input);
348                         if (fileName.EndsWith("/")) {
349                                 fRawReader = new AliRawReaderFile(fileName);
350                         } else if (fileName.EndsWith(".root")) {
351                                 fRawReader = new AliRawReaderRoot(fileName);
352                         } else if (!fileName.IsNull()) {
353                                 fRawReader = new AliRawReaderDate(fileName);
354                                 fRawReader->SelectEvents(7);
355                         }
356                 }
357             if ( ! fRawReader ) 
358                         return kFALSE ; 
359                 fRawReaderDelete = kFALSE ; 
360                 fRawReader->NextEvent() ; 
361                 fRunNumber = fRawReader->GetRunNumber() ; 
362                 AliCDBManager::Instance()->SetRun(fRunNumber) ; 
363                 fRawReader->RewindEvents();
364                 fNumberOfEvents = 999999 ;
365         } else if (taskIndex == AliQA::kESDS) {
366                 if (!gSystem->AccessPathName("AliESDs.root")) { // AliESDs.root exists
367                         TFile * esdFile = TFile::Open("AliESDs.root") ;
368                         fESDTree = dynamic_cast<TTree *> (esdFile->Get("esdTree")) ; 
369                         if ( !fESDTree ) {
370                                 AliError("esdTree not found") ; 
371                                 return kFALSE ; 
372                         } else {
373                                 fESD     = new AliESDEvent() ;
374                                 fESD->ReadFromTree(fESDTree) ;
375                                 fESDTree->GetEntry(0) ; 
376                                 fRunNumber = fESD->GetRunNumber() ; 
377                                 fNumberOfEvents = fESDTree->GetEntries() ;
378                         }
379                 } else {
380                         AliError("AliESDs.root not found") ; 
381                         return kFALSE ; 
382                 }                       
383         } else {
384                 if ( !InitRunLoader() ) { 
385                         AliWarning("No Run Loader not found") ; 
386                 } else {
387                         fNumberOfEvents = fRunLoader->GetNumberOfEvents() ;
388                 }
389         }
390                 // Initialize all QA data makers for all detectors
391         for (UInt_t iDet = 0; iDet < fgkNDetectors ; iDet++) {
392                 if (IsSelected(AliQA::GetDetName(iDet))) {
393                         AliQADataMaker * qadm = GetQADataMaker(iDet, mode) ;
394                         if (!qadm) {
395                                 AliWarning(Form("AliQADataMaker not found for %s", AliQA::GetDetName(iDet))) ; 
396                         } else {
397                                 AliInfo(Form("Data Maker found for %s", qadm->GetName())) ; 
398                                 qadm->Init(taskIndex, fRunNumber, GetQACycles(iDet)) ;
399                                 qadm->StartOfCycle(taskIndex, fCycleSame) ;
400                         }
401                 }
402         } 
403         fFirst = kFALSE ;
404         return kTRUE ; 
405 }
406
407 //_____________________________________________________________________________
408 Bool_t AliQADataMakerSteer::InitRunLoader()
409 {
410         // get or create the run loader
411         if (fRunLoader) {
412                 fCycleSame = kTRUE ; 
413                 return kTRUE ;
414         } 
415                 
416         if (!gSystem->AccessPathName(fGAliceFileName.Data())) { // galice.root exists
417     // load all base libraries to get the loader classes
418                 TString libs = gSystem->GetLibraries() ;
419                 for (UInt_t iDet = 0; iDet < fgkNDetectors; iDet++) {
420                         if (!IsSelected(AliQA::GetDetName(iDet))) 
421                                 continue ; 
422                         TString detName = AliQA::GetDetName(iDet) ;
423                         if (detName == "HLT") 
424                                 continue;
425                         if (libs.Contains("lib" + detName + "base.so")) 
426                                 continue;
427                         gSystem->Load("lib" + detName + "base.so");
428                 }
429                 fRunLoader = AliRunLoader::Open(fGAliceFileName.Data());
430                 if (!fRunLoader) {
431                         AliError(Form("no run loader found in file %s", fGAliceFileName.Data()));
432                         return kFALSE;
433                 }
434                 fRunLoader->CdGAFile();
435                 if (fRunLoader->LoadgAlice() == 0) {
436                         gAlice = fRunLoader->GetAliRun();
437                 }
438
439                 if (!gAlice) {
440                         AliError(Form("no gAlice object found in file %s", fGAliceFileName.Data()));
441                         return kFALSE;
442                 }
443
444         } else {               // galice.root does not exist
445                 AliError(Form("the file %s does not exist", fGAliceFileName.Data()));
446                 return kFALSE;
447     }
448
449   return kTRUE;
450 }
451
452 //_____________________________________________________________________________
453 Bool_t AliQADataMakerSteer::IsSelected(const char * det) 
454 {
455         // check whether detName is contained in detectors
456         // if yes, it is removed from detectors
457         
458         const TString detName(det) ;
459         // check if all detectors are selected
460         if ((fDetectors.CompareTo("ALL") == 0) ||
461                 fDetectors.BeginsWith("ALL ") ||
462                 fDetectors.EndsWith(" ALL") ||
463                 fDetectors.Contains(" ALL ")) {
464                 fDetectors = "ALL";
465                 return kTRUE;
466         }
467         
468         // search for the given detector
469         Bool_t rv = kFALSE;
470         if ((fDetectors.CompareTo(detName) == 0) ||
471                 fDetectors.BeginsWith(detName+" ") ||
472                 fDetectors.EndsWith(" "+detName) ||
473                 fDetectors.Contains(" "+detName+" ")) {
474                 //              fDetectors.ReplaceAll(detName, "");
475                 rv = kTRUE;
476         }
477         
478         // clean up the detectors string
479         //      while (fDetectors.Contains("  ")) 
480         //              fDetectors.ReplaceAll("  ", " ");
481         //      while (fDetectors.BeginsWith(" ")) 
482         //              fDetectors.Remove(0, 1);
483         //      while (fDetectors.EndsWith(" ")) 
484         //              fDetectors.Remove(fDetectors.Length()-1, 1);
485         
486         return rv ;
487 }
488
489 //_____________________________________________________________________________
490 Bool_t AliQADataMakerSteer::Merge(const Int_t runNumber) const
491 {
492         // Merge all the cycles from all detectors in one single file per run
493         char cmd[80] ;
494         if ( runNumber == -1 )
495                 sprintf(cmd, ".! ls *%s*.*.*.root > tempo.txt", AliQA::GetQADataFileName()) ; 
496         else 
497                 sprintf(cmd, ".! ls *%s*.%d.*.root > tempo.txt", AliQA::GetQADataFileName(), runNumber) ; 
498         gROOT->ProcessLine(cmd) ;
499         ifstream in("tempo.txt") ; 
500         const Int_t runMax = 10 ;  
501         TString file[AliQA::kNDET*runMax] ;
502         Int_t run[AliQA::kNDET*runMax] ;
503         
504         Int_t index = 0 ; 
505         while ( 1 ) {
506                 in >> file[index] ; 
507                 if ( !in.good() ) 
508                         break ; 
509                 index++ ;
510         }
511         
512         if ( index == 0 ) { 
513                 AliError(Form("run number %d not found", runNumber)) ; 
514                 return kFALSE ; 
515         }
516         
517         Int_t previousRun = -1 ;
518         Int_t runIndex = 0 ;  
519         char stmp[10] ; 
520         sprintf(stmp, ".%s.", AliQA::GetQADataFileName()) ; 
521         for (Int_t ifile = 0 ; ifile < index-1 ; ifile++) {
522                 TString tmp(file[ifile]) ; 
523                 tmp.ReplaceAll(".root", "") ; 
524                 TString det = tmp(0, tmp.Index(".")) ; 
525                 tmp.Remove(0, tmp.Index(stmp)+4) ; 
526                 TString ttmp = tmp(0, tmp.Index(".")) ; 
527                 Int_t newRun = ttmp.Atoi() ;
528                 if (newRun != previousRun) {
529                         run[runIndex] = newRun ; 
530                         previousRun = newRun ; 
531                         runIndex++ ; 
532                 }
533                 ttmp = tmp(tmp.Index("."), tmp.Length()) ; 
534                 Int_t cycle = ttmp.Atoi() ;  
535                 AliInfo(Form("%s : det = %s run = %d cycle = %d \n", file[ifile].Data(), det.Data(), newRun, cycle)) ; 
536         }
537         for (Int_t irun = 0 ; irun < runIndex ; irun++) {
538                 TFileMerger merger ; 
539                 char outFileName[20] ; 
540                 sprintf(outFileName, "Merged.%s.%d.root", AliQA::GetQADataFileName(), runIndex-1) ; 
541                 merger.OutputFile(outFileName) ; 
542                 for (Int_t ifile = 0 ; ifile < index-1 ; ifile++) {
543                         char pattern[100] ; 
544                         sprintf(pattern, "%s.%d.", AliQA::GetQADataFileName(), runIndex-1) ; 
545                         TString tmp(file[ifile]) ; 
546                         if (tmp.Contains(pattern))
547                                 merger.AddFile(tmp) ; 
548                 }
549                 merger.Merge() ; 
550         }
551         
552         return kTRUE ; 
553 }
554
555 //_____________________________________________________________________________
556 void AliQADataMakerSteer::Reset()
557 {
558         // Reset the default data members
559         for (UInt_t iDet = 0; iDet < fgkNDetectors; iDet++) {
560                 if (IsSelected(AliQA::GetDetName(iDet))) {
561                         fLoader[iDet] = NULL;
562                         if (fQADataMaker[iDet]) {
563                                 (fQADataMaker[iDet])->Reset() ; 
564                                 //delete fQADataMaker[iDet] ;
565                                 //fQADataMaker[iDet] = NULL ;
566                         }
567                 }
568         }
569
570         if (fRawReaderDelete) { 
571                 delete fRawReader ;
572                 fRawReader      = NULL ;
573         }
574
575         fCycleSame      = kFALSE ; 
576         fESD            = NULL ; 
577         fESDTree        = NULL ; 
578         fFirst          = kTRUE ;   
579         fNumberOfEvents = 999999 ;  
580 }
581
582 //_____________________________________________________________________________
583 Bool_t AliQADataMakerSteer::Run(const char * detectors, AliRawReader * rawReader) 
584 {
585         //Runs all the QA data Maker for Raws only
586         fRawReader       = rawReader ;          
587         fRawReaderDelete = kFALSE ; 
588         fCycleSame       = kTRUE ; 
589         fDetectors       = detectors ; 
590
591         // Initialize all QA data makers for all detectors
592         for (UInt_t iDet = 0; iDet < fgkNDetectors ; iDet++) {
593                 if (IsSelected(AliQA::GetDetName(iDet))) {
594                         AliQADataMaker * qadm = GetQADataMaker(iDet, "rec") ;
595                         if (!qadm) {
596                                 AliWarning(Form("AliQADataMaker not found for %s", AliQA::GetDetName(iDet))) ; 
597                         } else {
598                                 AliInfo(Form("Data Maker found for %s", qadm->GetName())) ; 
599                                 qadm->Init(AliQA::kRAWS, fRunNumber, GetQACycles(iDet)) ;
600                                 qadm->StartOfCycle(AliQA::kRAWS, fCycleSame) ;
601                         }
602                 }
603         } 
604         fFirst = kFALSE ;
605                 
606         return DoIt(AliQA::kRAWS, "rec") ; 
607 }
608
609 //_____________________________________________________________________________
610 Bool_t AliQADataMakerSteer::Run(const char * detectors, const char * fileName) 
611 {
612         //Runs all the QA data Maker for Raws only
613         fCycleSame       = kTRUE ; 
614         fDetectors       = detectors ; 
615         
616         if ( !Init(AliQA::kRAWS, "rec", fileName) ) 
617                 return kFALSE ; 
618
619         // Initialize all QA data makers for all detectors
620         for (UInt_t iDet = 0; iDet < fgkNDetectors ; iDet++) {
621                 if (IsSelected(AliQA::GetDetName(iDet))) {
622                         AliQADataMaker * qadm = GetQADataMaker(iDet, "rec") ;
623                         if (!qadm) {
624                                 AliWarning(Form("AliQADataMaker not found for %s", AliQA::GetDetName(iDet))) ; 
625                         } else {
626                                 AliInfo(Form("Data Maker found for %s", qadm->GetName())) ; 
627                                 qadm->Init(AliQA::kRAWS, fRunNumber, GetQACycles(iDet)) ;
628                                 qadm->StartOfCycle(AliQA::kRAWS, fCycleSame) ;
629                         }
630                 }
631         } 
632         fFirst = kFALSE ;
633         
634         return DoIt(AliQA::kRAWS, "rec") ; 
635 }
636
637 //_____________________________________________________________________________
638 Bool_t AliQADataMakerSteer::Run(const char * detectors, const AliQA::TASKINDEX taskIndex, const  char * fileName )
639 {
640         // Runs all the QA data Maker for every detector
641
642         Bool_t rv  = kFALSE ;
643         fDetectors = detectors ; 
644
645         char * mode ; 
646         if ( (taskIndex == AliQA::kHITS) || (taskIndex == AliQA::kSDIGITS) || (taskIndex == AliQA::kDIGITS) ) 
647                 mode = "sim" ; 
648         else if ( (taskIndex == AliQA::kRAWS) || (taskIndex == AliQA::kRECPOINTS) || (taskIndex == AliQA::kESDS) )
649                 mode = "rec" ; 
650         else {
651                 AliError(Form("%s not implemented", AliQA::GetTaskName(taskIndex).Data())) ; 
652                 return rv ;
653         }
654         
655         if ( !Init(taskIndex, mode, fileName) ) 
656                 return kFALSE ; 
657         
658         rv = DoIt(taskIndex, mode) ;
659         
660         return rv ;   
661
662 }
663
664 //_____________________________________________________________________________
665 Bool_t AliQADataMakerSteer::Save2OCDB(const Int_t runNumber, const Int_t cycleNumber, const char * detectors) const
666 {
667         // take the locasl QA data merge into a single file and save in OCDB 
668         Bool_t rv = kTRUE ; 
669         TString tmp(AliQA::GetQARefStorage()) ; 
670         if ( tmp.IsNull() ) { 
671                 AliError("No storage defined, use AliQA::SetQARefStorage") ; 
672                 return kFALSE ; 
673         }
674         if ( !(tmp.Contains(AliQA::GetLabLocalOCDB()) || tmp.Contains(AliQA::GetLabAliEnOCDB())) ) {
675                 AliError(Form("%s is a wrong storage, use %s or %s", AliQA::GetQARefStorage(), AliQA::GetLabLocalOCDB().Data(), AliQA::GetLabAliEnOCDB().Data())) ; 
676                 return kFALSE ; 
677         }
678         TString sdet(detectors) ; 
679         sdet.ToUpper() ;
680         TFile * inputFile ; 
681         if ( sdet.Contains("ALL") ) {
682                 rv = Merge(runNumber) ; 
683                 if ( ! rv )
684                         return kFALSE ; 
685                 char inputFileName[20] ; 
686                 sprintf(inputFileName, "Merged.%s.%d.root", AliQA::GetQADataFileName(), runNumber) ; 
687                 inputFile = TFile::Open(inputFileName) ; 
688                 rv = SaveIt2OCDB(runNumber, inputFile) ; 
689         } else {
690                 for (Int_t index = 0; index < AliQA::kNDET; index++) {
691                         if (sdet.Contains(AliQA::GetDetName(index))) {
692                                 char inputFileName[20] ; 
693                                 sprintf(inputFileName, "%s.%s.%d.%d.root", AliQA::GetDetName(index), AliQA::GetQADataFileName(), runNumber, cycleNumber) ; 
694                                 inputFile = TFile::Open(inputFileName) ;                        
695                                 rv *= SaveIt2OCDB(runNumber, inputFile) ; 
696                         }
697                 }
698         }
699         return rv ; 
700 }
701
702 //_____________________________________________________________________________
703 Bool_t AliQADataMakerSteer::SaveIt2OCDB(const Int_t runNumber, TFile * inputFile) const
704 {
705         // reads the TH1 from file and adds it to appropriate list before saving to OCDB
706         Bool_t rv = kTRUE ;
707         AliInfo(Form("Saving TH1s in %s to %s", inputFile->GetName(), AliQA::GetQARefStorage())) ; 
708         AliCDBManager* man = AliCDBManager::Instance() ; 
709         if ( ! man->IsDefaultStorageSet() ) {
710                 man->SetDefaultStorage(AliQA::GetQARefDefaultStorage()) ; 
711                 man->SetSpecificStorage(Form("%s/*", AliQA::GetQAOCDBDirName()), AliQA::GetQARefStorage()) ; 
712         }
713         if(man->GetRun() < 0) 
714                 man->SetRun(runNumber);
715
716         for ( Int_t detIndex = 0 ; detIndex < AliQA::kNDET ; detIndex++) {
717                 TDirectory * detDir = inputFile->GetDirectory(AliQA::GetDetName(detIndex)) ; 
718                 if ( detDir ) {
719                         AliInfo(Form("Entering %s", detDir->GetName())) ;
720                         char detOCDBDir[20] ;
721                         sprintf(detOCDBDir, "%s/%s/%s", AliQA::GetQAOCDBDirName(), AliQA::GetDetName(detIndex), AliQA::GetRefOCDBDirName()) ; 
722                         AliCDBId idr(detOCDBDir, runNumber, 999999999)  ;
723                         TList * listDetQAD = new TList() ;
724                         char listName[20] ; 
725                         sprintf(listName, "%s QA data Reference", AliQA::GetDetName(detIndex)) ; 
726                         listDetQAD->SetName(listName) ; 
727                         TList * taskList = detDir->GetListOfKeys() ; 
728                         TIter nextTask(taskList) ; 
729                         TKey * taskKey ; 
730                         while ( (taskKey = dynamic_cast<TKey*>(nextTask())) ) {
731                                 TDirectory * taskDir = detDir->GetDirectory(taskKey->GetName()) ; 
732                                 AliInfo(Form("Saving %s", taskDir->GetName())) ; 
733                                 TObjArray * listTaskQAD = new TObjArray(100) ; 
734                                 listTaskQAD->SetName(taskKey->GetName()) ;
735                                 listDetQAD->Add(listTaskQAD) ; 
736                                 TList * histList = taskDir->GetListOfKeys() ; 
737                                 TIter nextHist(histList) ; 
738                                 TKey * histKey ; 
739                                 while ( (histKey = dynamic_cast<TKey*>(nextHist())) ) {
740                                         TObject * odata = taskDir->Get(histKey->GetName()) ; 
741                                         if ( odata->IsA()->InheritsFrom("TH1") ) {
742                                                 AliInfo(Form("Adding %s", histKey->GetName())) ;
743                                                 TH1 * hdata = static_cast<TH1*>(odata) ; 
744                                                 listTaskQAD->Add(hdata) ; 
745                                         }
746                                 }
747                         }
748                         AliCDBMetaData mdr ;
749                         man->Put(listDetQAD, idr, &mdr) ;
750                 }
751         }
752         return rv ; 
753 }