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