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