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