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