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