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