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