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