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