]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliQADataMakerSteer.cxx
change the trave of Reset
[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 tmp(mode) ; 
317                 if (tmp.Contains("sim")) 
318                         tmp.ReplaceAll("s", "S") ; 
319                 else if (tmp.Contains("rec")) 
320                         tmp.ReplaceAll("r", "R") ; 
321                 TString qadmName = "Ali" + detName + "QADataMaker" + tmp ;
322
323                 // first check if a plugin is defined for the quality assurance data maker
324                 TPluginHandler* pluginHandler = pluginManager->FindHandler("AliQADataMaker", detName) ;
325                 // if not, add a plugin for it
326                 if (!pluginHandler) {
327                         AliDebug(1, Form("defining plugin for %s", qadmName.Data())) ;
328                         TString libs = gSystem->GetLibraries() ;
329                         if (libs.Contains("lib" + detName + mode + ".so") || (gSystem->Load("lib" + detName + mode + ".so") >= 0)) {
330                                 pluginManager->AddHandler("AliQADataMaker", detName, qadmName, detName + "qadm", qadmName + "()") ;
331                         } else {
332                                 pluginManager->AddHandler("AliQADataMaker", detName, qadmName, detName, qadmName + "()") ;
333                         }
334                         pluginHandler = pluginManager->FindHandler("AliQADataMaker", detName) ;
335                 }
336                 if (pluginHandler && (pluginHandler->LoadPlugin() == 0)) {
337                         qadm = (AliQADataMaker *) pluginHandler->ExecPlugin(0) ;
338                 }
339                 if (qadm) 
340                         fQADataMaker[iDet] = qadm ;
341         }
342         return qadm ;
343 }
344
345 //_____________________________________________________________________________
346 Bool_t AliQADataMakerSteer::Init(const AliQA::TASKINDEX taskIndex, const char * mode, const  char * input )
347 {
348         // Initialize the event source and QA data makers
349         
350         if (taskIndex == AliQA::kRAWS) { 
351                 if (!fRawReader) {
352                         TString fileName(input);
353                         if (fileName.EndsWith("/")) {
354                                 fRawReader = new AliRawReaderFile(fileName);
355                         } else if (fileName.EndsWith(".root")) {
356                                 fRawReader = new AliRawReaderRoot(fileName);
357                         } else if (!fileName.IsNull()) {
358                                 fRawReader = new AliRawReaderDate(fileName);
359                                 fRawReader->SelectEvents(7);
360                         }
361                 }
362             if ( ! fRawReader ) 
363                         return kFALSE ; 
364                 fRawReaderDelete = kFALSE ; 
365                 fRawReader->NextEvent() ; 
366                 fRunNumber = fRawReader->GetRunNumber() ; 
367                 AliCDBManager::Instance()->SetRun(fRunNumber) ; 
368                 fRawReader->RewindEvents();
369                 fNumberOfEvents = 999999 ;
370         } else if (taskIndex == AliQA::kESDS) {
371                 if (!gSystem->AccessPathName("AliESDs.root")) { // AliESDs.root exists
372                         TFile * esdFile = TFile::Open("AliESDs.root") ;
373                         fESDTree = dynamic_cast<TTree *> (esdFile->Get("esdTree")) ; 
374                         if ( !fESDTree ) {
375                                 AliError("esdTree not found") ; 
376                                 return kFALSE ; 
377                         } else {
378                                 fESD     = new AliESDEvent() ;
379                                 fESD->ReadFromTree(fESDTree) ;
380                                 fESDTree->GetEntry(0) ; 
381                                 fRunNumber = fESD->GetRunNumber() ; 
382                                 fNumberOfEvents = fESDTree->GetEntries() ;
383                         }
384                 } else {
385                         AliError("AliESDs.root not found") ; 
386                         return kFALSE ; 
387                 }                       
388         } else {
389                 if ( !InitRunLoader() ) { 
390                         AliWarning("No Run Loader not found") ; 
391                 } else {
392                         fNumberOfEvents = fRunLoader->GetNumberOfEvents() ;
393                 }
394         }
395                 // Initialize all QA data makers for all detectors
396         for (UInt_t iDet = 0; iDet < fgkNDetectors ; iDet++) {
397                 if (IsSelected(AliQA::GetDetName(iDet))) {
398                         AliQADataMaker * qadm = GetQADataMaker(iDet, mode) ;
399                         if (!qadm) {
400                                 AliWarning(Form("AliQADataMaker not found for %s", AliQA::GetDetName(iDet))) ; 
401                         } else {
402                                 AliInfo(Form("Data Maker found for %s", qadm->GetName())) ; 
403                                 qadm->Init(taskIndex, fRunNumber, GetQACycles(iDet)) ;
404                                 qadm->StartOfCycle(taskIndex, fCycleSame) ;
405                         }
406                 }
407         } 
408         fFirst = kFALSE ;
409         return kTRUE ; 
410 }
411
412 //_____________________________________________________________________________
413 Bool_t AliQADataMakerSteer::InitRunLoader()
414 {
415         // get or create the run loader
416         if (fRunLoader) {
417                 fCycleSame = kTRUE ; 
418                 return kTRUE ;
419         } 
420                 
421         if (!gSystem->AccessPathName(fGAliceFileName.Data())) { // galice.root exists
422     // load all base libraries to get the loader classes
423                 TString libs = gSystem->GetLibraries() ;
424                 for (UInt_t iDet = 0; iDet < fgkNDetectors; iDet++) {
425                         if (!IsSelected(AliQA::GetDetName(iDet))) 
426                                 continue ; 
427                         TString detName = AliQA::GetDetName(iDet) ;
428                         if (detName == "HLT") 
429                                 continue;
430                         if (libs.Contains("lib" + detName + "base.so")) 
431                                 continue;
432                         gSystem->Load("lib" + detName + "base.so");
433                 }
434                 fRunLoader = AliRunLoader::Open(fGAliceFileName.Data());
435                 if (!fRunLoader) {
436                         AliError(Form("no run loader found in file %s", fGAliceFileName.Data()));
437                         return kFALSE;
438                 }
439                 fRunLoader->CdGAFile();
440                 if (fRunLoader->LoadgAlice() == 0) {
441                         gAlice = fRunLoader->GetAliRun();
442                 }
443
444                 if (!gAlice) {
445                         AliError(Form("no gAlice object found in file %s", fGAliceFileName.Data()));
446                         return kFALSE;
447                 }
448
449         } else {               // galice.root does not exist
450                 AliError(Form("the file %s does not exist", fGAliceFileName.Data()));
451                 return kFALSE;
452     }
453
454   return kTRUE;
455 }
456
457 //_____________________________________________________________________________
458 Bool_t AliQADataMakerSteer::IsSelected(const char * det) 
459 {
460         // check whether detName is contained in detectors
461         // if yes, it is removed from detectors
462         
463         const TString detName(det) ;
464         // check if all detectors are selected
465         if ((fDetectors.CompareTo("ALL") == 0) ||
466                 fDetectors.BeginsWith("ALL ") ||
467                 fDetectors.EndsWith(" ALL") ||
468                 fDetectors.Contains(" ALL ")) {
469                 fDetectors = "ALL";
470                 return kTRUE;
471         }
472         
473         // search for the given detector
474         Bool_t rv = kFALSE;
475         if ((fDetectors.CompareTo(detName) == 0) ||
476                 fDetectors.BeginsWith(detName+" ") ||
477                 fDetectors.EndsWith(" "+detName) ||
478                 fDetectors.Contains(" "+detName+" ")) {
479                 //              fDetectors.ReplaceAll(detName, "");
480                 rv = kTRUE;
481         }
482         
483         // clean up the detectors string
484         //      while (fDetectors.Contains("  ")) 
485         //              fDetectors.ReplaceAll("  ", " ");
486         //      while (fDetectors.BeginsWith(" ")) 
487         //              fDetectors.Remove(0, 1);
488         //      while (fDetectors.EndsWith(" ")) 
489         //              fDetectors.Remove(fDetectors.Length()-1, 1);
490         
491         return rv ;
492 }
493
494 //_____________________________________________________________________________
495 Bool_t AliQADataMakerSteer::Merge(const Int_t runNumber) const
496 {
497         // Merge all the cycles from all detectors in one single file per run
498         char cmd[80] ;
499         if ( runNumber == -1 )
500                 sprintf(cmd, ".! ls *%s*.*.*.root > tempo.txt", AliQA::GetQADataFileName()) ; 
501         else 
502                 sprintf(cmd, ".! ls *%s*.%d.*.root > tempo.txt", AliQA::GetQADataFileName(), runNumber) ; 
503         gROOT->ProcessLine(cmd) ;
504         ifstream in("tempo.txt") ; 
505         const Int_t runMax = 10 ;  
506         TString file[AliQA::kNDET*runMax] ;
507         Int_t run[AliQA::kNDET*runMax] ;
508         
509         Int_t index = 0 ; 
510         while ( 1 ) {
511                 in >> file[index] ; 
512                 if ( !in.good() ) 
513                         break ; 
514                 index++ ;
515         }
516         
517         if ( index == 0 ) { 
518                 AliError(Form("run number %d not found", runNumber)) ; 
519                 return kFALSE ; 
520         }
521         
522         Int_t runIndex = 1 ;  
523         char stmp[10] ; 
524         sprintf(stmp, ".%s.", AliQA::GetQADataFileName()) ; 
525         for (Int_t ifile = 0 ; ifile < index-1 ; ifile++) {
526                 TString tmp(file[ifile]) ; 
527                 tmp.ReplaceAll(".root", "") ; 
528                 TString det = tmp(0, tmp.Index(".")) ; 
529                 tmp.Remove(0, tmp.Index(stmp)+4) ; 
530                 TString ttmp = tmp(0, tmp.Index(".")) ; 
531                 Int_t newRun = ttmp.Atoi() ;
532                 for (Int_t irun = 0; irun < runIndex; irun++) {
533                         if (newRun == run[irun]) 
534                                 break ; 
535                         run[runIndex] = newRun ; 
536                         runIndex++ ; 
537                 }
538                 ttmp = tmp(tmp.Index("."), tmp.Length()) ; 
539                 Int_t cycle = ttmp.Atoi() ;  
540                 AliInfo(Form("%s : det = %s run = %d cycle = %d \n", file[ifile].Data(), det.Data(), newRun, cycle)) ; 
541         }
542         for (Int_t irun = 0 ; irun < runIndex ; irun++) {
543                 TFileMerger merger ; 
544                 char outFileName[20] ; 
545                 sprintf(outFileName, "Merged.%s.%d.root", AliQA::GetQADataFileName(), run[irun]) ; 
546                 merger.OutputFile(outFileName) ; 
547                 for (Int_t ifile = 0 ; ifile < index-1 ; ifile++) {
548                         char pattern[100] ; 
549                         sprintf(pattern, "%s.%d.", AliQA::GetQADataFileName(), run[irun]) ; 
550                         TString tmp(file[ifile]) ; 
551                         if (tmp.Contains(pattern))
552                                 merger.AddFile(tmp) ; 
553                 }
554                 merger.Merge() ; 
555         }
556         
557         return kTRUE ; 
558 }
559
560 //_____________________________________________________________________________
561 void AliQADataMakerSteer::Reset()
562 {
563         // Reset the default data members
564         for (UInt_t iDet = 0; iDet < fgkNDetectors; iDet++) {
565                 if (IsSelected(AliQA::GetDetName(iDet))) {
566                         fLoader[iDet] = NULL;
567                         if (fQADataMaker[iDet]) {
568                                 (fQADataMaker[iDet])->Reset() ; 
569                                 //delete fQADataMaker[iDet] ;
570                                 //fQADataMaker[iDet] = NULL ;
571                         }
572                 }
573         }
574
575         if (fRawReaderDelete) { 
576                 delete fRawReader ;
577                 fRawReader      = NULL ;
578         }
579
580         fCycleSame      = kFALSE ; 
581         fESD            = NULL ; 
582         fESDTree        = NULL ; 
583         fFirst          = kTRUE ;   
584         fNumberOfEvents = 999999 ;  
585 }
586
587 //_____________________________________________________________________________
588 Bool_t AliQADataMakerSteer::Run(const char * detectors, AliRawReader * rawReader) 
589 {
590         //Runs all the QA data Maker for Raws only
591         fRawReader       = rawReader ;          
592         fRawReaderDelete = kFALSE ; 
593         fCycleSame       = kTRUE ; 
594         fDetectors       = detectors ; 
595
596         // Initialize all QA data makers for all detectors
597         for (UInt_t iDet = 0; iDet < fgkNDetectors ; iDet++) {
598                 if (IsSelected(AliQA::GetDetName(iDet))) {
599                         AliQADataMaker * qadm = GetQADataMaker(iDet, "rec") ;
600                         if (!qadm) {
601                                 AliWarning(Form("AliQADataMaker not found for %s", AliQA::GetDetName(iDet))) ; 
602                         } else {
603                                 AliInfo(Form("Data Maker found for %s", qadm->GetName())) ; 
604                                 qadm->Init(AliQA::kRAWS, fRunNumber, GetQACycles(iDet)) ;
605                                 qadm->StartOfCycle(AliQA::kRAWS, fCycleSame) ;
606                         }
607                 }
608         } 
609         fFirst = kFALSE ;
610                 
611         return DoIt(AliQA::kRAWS, "rec") ; 
612 }
613
614 //_____________________________________________________________________________
615 Bool_t AliQADataMakerSteer::Run(const char * detectors, const char * fileName) 
616 {
617         //Runs all the QA data Maker for Raws only
618         fCycleSame       = kTRUE ; 
619         fDetectors       = detectors ; 
620         
621         if ( !Init(AliQA::kRAWS, "rec", fileName) ) 
622                 return kFALSE ; 
623
624         // Initialize all QA data makers for all detectors
625         for (UInt_t iDet = 0; iDet < fgkNDetectors ; iDet++) {
626                 if (IsSelected(AliQA::GetDetName(iDet))) {
627                         AliQADataMaker * qadm = GetQADataMaker(iDet, "rec") ;
628                         if (!qadm) {
629                                 AliWarning(Form("AliQADataMaker not found for %s", AliQA::GetDetName(iDet))) ; 
630                         } else {
631                                 AliInfo(Form("Data Maker found for %s", qadm->GetName())) ; 
632                                 qadm->Init(AliQA::kRAWS, fRunNumber, GetQACycles(iDet)) ;
633                                 qadm->StartOfCycle(AliQA::kRAWS, fCycleSame) ;
634                         }
635                 }
636         } 
637         fFirst = kFALSE ;
638         
639         return DoIt(AliQA::kRAWS, "rec") ; 
640 }
641
642 //_____________________________________________________________________________
643 Bool_t AliQADataMakerSteer::Run(const char * detectors, const AliQA::TASKINDEX taskIndex, const  char * fileName )
644 {
645         // Runs all the QA data Maker for every detector
646
647         Bool_t rv  = kFALSE ;
648         fDetectors = detectors ; 
649
650         char * mode ; 
651         if ( (taskIndex == AliQA::kHITS) || (taskIndex == AliQA::kSDIGITS) || (taskIndex == AliQA::kDIGITS) ) 
652                 mode = "sim" ; 
653         else if ( (taskIndex == AliQA::kRAWS) || (taskIndex == AliQA::kRECPOINTS) || (taskIndex == AliQA::kESDS) )
654                 mode = "rec" ; 
655         else {
656                 AliError(Form("%s not implemented", AliQA::GetTaskName(taskIndex).Data())) ; 
657                 return rv ;
658         }
659         
660         if ( !Init(taskIndex, mode, fileName) ) 
661                 return kFALSE ; 
662         
663         rv = DoIt(taskIndex, mode) ;
664         
665         return rv ;   
666
667 }
668
669 //_____________________________________________________________________________
670 Bool_t AliQADataMakerSteer::Save2OCDB(const Int_t runNumber, const Int_t cycleNumber, const char * detectors) const
671 {
672         // take the locasl QA data merge into a single file and save in OCDB 
673         Bool_t rv = kTRUE ; 
674         TString tmp(AliQA::GetQARefStorage()) ; 
675         if ( tmp.IsNull() ) { 
676                 AliError("No storage defined, use AliQA::SetQARefStorage") ; 
677                 return kFALSE ; 
678         }
679         if ( !(tmp.Contains(AliQA::GetLabLocalOCDB()) || tmp.Contains(AliQA::GetLabAliEnOCDB())) ) {
680                 AliError(Form("%s is a wrong storage, use %s or %s", AliQA::GetQARefStorage(), AliQA::GetLabLocalOCDB().Data(), AliQA::GetLabAliEnOCDB().Data())) ; 
681                 return kFALSE ; 
682         }
683         TString sdet(detectors) ; 
684         sdet.ToUpper() ;
685         TFile * inputFile ; 
686         if ( sdet.Contains("ALL") ) {
687                 rv = Merge(runNumber) ; 
688                 if ( ! rv )
689                         return kFALSE ; 
690                 char inputFileName[20] ; 
691                 sprintf(inputFileName, "Merged.%s.%d.root", AliQA::GetQADataFileName(), runNumber) ; 
692                 inputFile = TFile::Open(inputFileName) ; 
693                 rv = SaveIt2OCDB(runNumber, inputFile) ; 
694         } else {
695                 for (Int_t index = 0; index < AliQA::kNDET; index++) {
696                         if (sdet.Contains(AliQA::GetDetName(index))) {
697                                 char inputFileName[20] ; 
698                                 sprintf(inputFileName, "%s.%s.%d.%d.root", AliQA::GetDetName(index), AliQA::GetQADataFileName(), runNumber, cycleNumber) ; 
699                                 inputFile = TFile::Open(inputFileName) ;                        
700                                 rv *= SaveIt2OCDB(runNumber, inputFile) ; 
701                         }
702                 }
703         }
704         return rv ; 
705 }
706
707 //_____________________________________________________________________________
708 Bool_t AliQADataMakerSteer::SaveIt2OCDB(const Int_t runNumber, TFile * inputFile) const
709 {
710         // reads the TH1 from file and adds it to appropriate list before saving to OCDB
711         Bool_t rv = kTRUE ;
712         AliInfo(Form("Saving TH1s in %s to %s", inputFile->GetName(), AliQA::GetQARefStorage())) ; 
713         AliCDBManager* man = AliCDBManager::Instance() ; 
714         if ( ! man->IsDefaultStorageSet() ) {
715                 man->SetDefaultStorage(AliQA::GetQARefDefaultStorage()) ; 
716                 man->SetSpecificStorage(Form("%s/*", AliQA::GetQAOCDBDirName()), AliQA::GetQARefStorage()) ; 
717         }
718         if(man->GetRun() < 0) 
719                 man->SetRun(runNumber);
720
721         for ( Int_t detIndex = 0 ; detIndex < AliQA::kNDET ; detIndex++) {
722                 TDirectory * detDir = inputFile->GetDirectory(AliQA::GetDetName(detIndex)) ; 
723                 if ( detDir ) {
724                         AliInfo(Form("Entering %s", detDir->GetName())) ;
725                         char detOCDBDir[20] ;
726                         sprintf(detOCDBDir, "%s/%s/%s", AliQA::GetQAOCDBDirName(), AliQA::GetDetName(detIndex), AliQA::GetRefOCDBDirName()) ; 
727                         AliCDBId idr(detOCDBDir, runNumber, 999999999)  ;
728                         TList * listDetQAD = new TList() ;
729                         char listName[20] ; 
730                         sprintf(listName, "%s QA data Reference", AliQA::GetDetName(detIndex)) ; 
731                         listDetQAD->SetName(listName) ; 
732                         TList * taskList = detDir->GetListOfKeys() ; 
733                         TIter nextTask(taskList) ; 
734                         TKey * taskKey ; 
735                         while ( (taskKey = dynamic_cast<TKey*>(nextTask())) ) {
736                                 TDirectory * taskDir = detDir->GetDirectory(taskKey->GetName()) ; 
737                                 AliInfo(Form("Saving %s", taskDir->GetName())) ; 
738                                 TObjArray * listTaskQAD = new TObjArray(100) ; 
739                                 listTaskQAD->SetName(taskKey->GetName()) ;
740                                 listDetQAD->Add(listTaskQAD) ; 
741                                 TList * histList = taskDir->GetListOfKeys() ; 
742                                 TIter nextHist(histList) ; 
743                                 TKey * histKey ; 
744                                 while ( (histKey = dynamic_cast<TKey*>(nextHist())) ) {
745                                         TObject * odata = taskDir->Get(histKey->GetName()) ; 
746                                         if ( odata->IsA()->InheritsFrom("TH1") ) {
747                                                 AliInfo(Form("Adding %s", histKey->GetName())) ;
748                                                 TH1 * hdata = static_cast<TH1*>(odata) ; 
749                                                 listTaskQAD->Add(hdata) ; 
750                                         }
751                                 }
752                         }
753                         AliCDBMetaData mdr ;
754                         man->Put(listDetQAD, idr, &mdr) ;
755                 }
756         }
757         return rv ; 
758 }