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