]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliReconstruction.cxx
Fix for report #60402 Problem with AliHMPIDPreprocessor. The access to the time stamp...
[u/mrichter/AliRoot.git] / STEER / AliReconstruction.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 //                                                                           //
20 // class for running the reconstruction                                      //
21 //                                                                           //
22 // Clusters and tracks are created for all detectors and all events by       //
23 // typing:                                                                   //
24 //                                                                           //
25 //   AliReconstruction rec;                                                  //
26 //   rec.Run();                                                              //
27 //                                                                           //
28 // The Run method returns kTRUE in case of successful execution.             //
29 //                                                                           //
30 // If the input to the reconstruction are not simulated digits but raw data, //
31 // this can be specified by an argument of the Run method or by the method   //
32 //                                                                           //
33 //   rec.SetInput("...");                                                    //
34 //                                                                           //
35 // The input formats and the corresponding argument are:                     //
36 // - DDL raw data files: directory name, ends with "/"                       //
37 // - raw data root file: root file name, extension ".root"                   //
38 // - raw data DATE file: DATE file name, any other non-empty string          //
39 // - MC root files     : empty string, default                               //
40 //                                                                           //
41 // By default all events are reconstructed. The reconstruction can be        //
42 // limited to a range of events by giving the index of the first and the     //
43 // last event as an argument to the Run method or by calling                 //
44 //                                                                           //
45 //   rec.SetEventRange(..., ...);                                            //
46 //                                                                           //
47 // The index -1 (default) can be used for the last event to indicate no      //
48 // upper limit of the event range.                                           //
49 //                                                                           //
50 // In case of raw-data reconstruction the user can modify the default        //
51 // number of events per digits/clusters/tracks file. In case the option      //
52 // is not used the number is set 1. In case the user provides 0, than        //
53 // the number of events is equal to the number of events inside the          //
54 // raw-data file (i.e. one digits/clusters/tracks file):                     //
55 //                                                                           //
56 //   rec.SetNumberOfEventsPerFile(...);                                      //
57 //                                                                           //
58 //                                                                           //
59 // The name of the galice file can be changed from the default               //
60 // "galice.root" by passing it as argument to the AliReconstruction          //
61 // constructor or by                                                         //
62 //                                                                           //
63 //   rec.SetGAliceFile("...");                                               //
64 //                                                                           //
65 // The local reconstruction can be switched on or off for individual         //
66 // detectors by                                                              //
67 //                                                                           //
68 //   rec.SetRunLocalReconstruction("...");                                   //
69 //                                                                           //
70 // The argument is a (case sensitive) string with the names of the           //
71 // detectors separated by a space. The special string "ALL" selects all      //
72 // available detectors. This is the default.                                 //
73 //                                                                           //
74 // The reconstruction of the primary vertex position can be switched off by  //
75 //                                                                           //
76 //   rec.SetRunVertexFinder(kFALSE);                                         //
77 //                                                                           //
78 // The tracking and the creation of ESD tracks can be switched on for        //
79 // selected detectors by                                                     //
80 //                                                                           //
81 //   rec.SetRunTracking("...");                                              //
82 //                                                                           //
83 // Uniform/nonuniform field tracking switches (default: uniform field)       //
84 //                                                                           //
85 //   rec.SetUniformFieldTracking(); ( rec.SetUniformFieldTracking(kFALSE); ) //
86 //                                                                           //
87 // The filling of additional ESD information can be steered by               //
88 //                                                                           //
89 //   rec.SetFillESD("...");                                                  //
90 //                                                                           //
91 // Again, for both methods the string specifies the list of detectors.       //
92 // The default is "ALL".                                                     //
93 //                                                                           //
94 // The call of the shortcut method                                           //
95 //                                                                           //
96 //   rec.SetRunReconstruction("...");                                        //
97 //                                                                           //
98 // is equivalent to calling SetRunLocalReconstruction, SetRunTracking and    //
99 // SetFillESD with the same detector selecting string as argument.           //
100 //                                                                           //
101 // The reconstruction requires digits or raw data as input. For the creation //
102 // of digits and raw data have a look at the class AliSimulation.            //
103 //                                                                           //
104 // The input data of a detector can be replaced by the corresponding HLT     //
105 // data by calling (usual detector string)                                   //
106 // SetUseHLTData("...");                                                     //
107 //                                                                           //
108 //                                                                           //
109 ///////////////////////////////////////////////////////////////////////////////
110
111 #include <TArrayD.h>
112 #include <TArrayF.h>
113 #include <TArrayS.h>
114 #include <TChain.h>
115 #include <TFile.h>
116 #include <TGeoGlobalMagField.h>
117 #include <TGeoManager.h>
118 #include <TList.h>
119 #include <TLorentzVector.h>
120 #include <TMap.h>
121 #include <TObjArray.h>
122 #include <TPRegexp.h>
123 #include <TParameter.h>
124 #include <TPluginManager.h>
125 #include <TProof.h>
126 #include <TProofOutputFile.h>
127 #include <TROOT.h>
128 #include <TSystem.h>
129 #include <THashTable.h>
130 #include <TGrid.h>
131 #include <TMessage.h>
132
133 #include "AliAlignObj.h"
134 #include "AliCDBEntry.h"
135 #include "AliCDBManager.h"
136 #include "AliCDBStorage.h"
137 #include "AliCTPRawStream.h"
138 #include "AliCascadeVertexer.h"
139 #include "AliCentralTrigger.h"
140 #include "AliCodeTimer.h"
141 #include "AliDAQ.h"
142 #include "AliDetectorRecoParam.h"
143 #include "AliESDCaloCells.h"
144 #include "AliESDCaloCluster.h"
145 #include "AliESDEvent.h"
146 #include "AliESDMuonTrack.h"
147 #include "AliESDPmdTrack.h"
148 #include "AliESDTagCreator.h"
149 #include "AliESDVertex.h"
150 #include "AliESDcascade.h"
151 #include "AliESDfriend.h"
152 #include "AliESDkink.h"
153 #include "AliESDpid.h"
154 #include "AliESDtrack.h"
155 #include "AliESDtrack.h"
156 #include "AliEventInfo.h"
157 #include "AliGRPObject.h"
158 #include "AliGRPRecoParam.h"
159 #include "AliGenEventHeader.h"
160 #include "AliGeomManager.h"
161 #include "AliGlobalQADataMaker.h" 
162 #include "AliHeader.h"
163 #include "AliLog.h"
164 #include "AliMagF.h"
165 #include "AliMultiplicity.h"
166 #include "AliPID.h"
167 #include "AliPlaneEff.h"
168 #include "AliQAv1.h"
169 #include "AliQADataMakerRec.h" 
170 #include "AliQAManager.h"
171 #include "AliRawVEvent.h"
172 #include "AliRawEventHeaderBase.h"
173 #include "AliRawHLTManager.h"
174 #include "AliRawReaderDate.h"
175 #include "AliRawReaderFile.h"
176 #include "AliRawReaderRoot.h"
177 #include "AliReconstruction.h"
178 #include "AliReconstructor.h"
179 #include "AliRun.h"
180 #include "AliRunInfo.h"
181 #include "AliRunLoader.h"
182 #include "AliSysInfo.h" // memory snapshots
183 #include "AliTrackPointArray.h"
184 #include "AliTracker.h"
185 #include "AliTriggerClass.h"
186 #include "AliTriggerCluster.h"
187 #include "AliTriggerIR.h"
188 #include "AliTriggerConfiguration.h"
189 #include "AliV0vertexer.h"
190 #include "AliVertexer.h"
191 #include "AliVertexerTracks.h"
192 #include "AliTriggerRunScalers.h"
193 #include "AliCTPTimeParams.h" 
194 #include "AliESDHLTDecision.h"
195
196 ClassImp(AliReconstruction)
197
198 //_____________________________________________________________________________
199 const char* AliReconstruction::fgkDetectorName[AliReconstruction::kNDetectors] = {"ITS", "TPC", "TRD", "TOF", "PHOS", "HMPID", "EMCAL", "MUON", "FMD", "ZDC", "PMD", "T0", "VZERO", "ACORDE", "HLT"};
200
201 //_____________________________________________________________________________
202 AliReconstruction::AliReconstruction(const char* gAliceFilename) :
203   TSelector(),
204   fRunVertexFinder(kTRUE),
205   fRunVertexFinderTracks(kTRUE),
206   fRunHLTTracking(kFALSE),
207   fRunMuonTracking(kFALSE),
208   fRunV0Finder(kTRUE),
209   fRunCascadeFinder(kTRUE),
210   fStopOnError(kTRUE),
211   fWriteAlignmentData(kFALSE),
212   fWriteESDfriend(kFALSE),
213   fFillTriggerESD(kTRUE),
214
215   fCleanESD(kTRUE),
216   fV0DCAmax(3.),
217   fV0CsPmin(0.),
218   fDmax(50.),
219   fZmax(50.),
220
221   fRunLocalReconstruction("ALL"),
222   fRunTracking("ALL"),
223   fFillESD("ALL"),
224   fLoadCDB(""),
225   fUseTrackingErrorsForAlignment(""),
226   fGAliceFileName(gAliceFilename),
227   fRawInput(""),
228   fESDOutput(""),
229   fProofOutputFileName(""),
230   fProofOutputLocation(""),
231   fProofOutputDataset(kFALSE),
232   fProofOutputArchive(""),
233   fEquipIdMap(""),
234   fFirstEvent(0),
235   fLastEvent(-1),
236   fNumberOfEventsPerFile((UInt_t)-1),
237   fOptions(),
238   fLoadAlignFromCDB(kTRUE),
239   fLoadAlignData("ALL"),
240   fUseHLTData(),
241   fRunInfo(NULL),
242   fEventInfo(),
243   fRunScalers(NULL),
244   fCTPTimeParams(NULL),  
245
246   fRunLoader(NULL),
247   fRawReader(NULL),
248   fParentRawReader(NULL),
249
250   fRecoParam(),
251
252   fSPDTrackleter(NULL),
253
254   fDiamondProfileSPD(NULL),
255   fDiamondProfile(NULL),
256   fDiamondProfileTPC(NULL),
257   fListOfCosmicTriggers(NULL),
258   
259   fGRPData(NULL),
260
261   fAlignObjArray(NULL),
262   fCDBUri(),
263   fQARefUri(),
264   fSpecCDBUri(), 
265   fInitCDBCalled(kFALSE),
266   fSetRunNumberFromDataCalled(kFALSE),
267   fQADetectors("ALL"), 
268   fQATasks("ALL"), 
269   fRunQA(kTRUE),  
270   fRunGlobalQA(kTRUE),
271   fSameQACycle(kFALSE),
272   fInitQACalled(kFALSE), 
273   fWriteQAExpertData(kTRUE), 
274   fRunPlaneEff(kFALSE),
275
276   fesd(NULL),
277   fhltesd(NULL),
278   fesdf(NULL),
279   ffile(NULL),
280   ftree(NULL),
281   fhlttree(NULL),
282   ftVertexer(NULL),
283   fIsNewRunLoader(kFALSE),
284   fRunAliEVE(kFALSE),
285   fChain(NULL)
286 {
287 // create reconstruction object with default parameters
288   gGeoManager = NULL;
289   
290   for (Int_t iDet = 0; iDet < kNDetectors; iDet++) {
291     fReconstructor[iDet] = NULL;
292     fLoader[iDet] = NULL;
293     fTracker[iDet] = NULL;
294   }
295   for (Int_t iDet = 0; iDet < AliQAv1::kNDET; iDet++) {
296     fQACycles[iDet] = 999999 ;
297     fQAWriteExpert[iDet] = kFALSE ; 
298   }
299     
300   AliPID pid;
301 }
302
303 //_____________________________________________________________________________
304 AliReconstruction::AliReconstruction(const AliReconstruction& rec) :
305   TSelector(),
306   fRunVertexFinder(rec.fRunVertexFinder),
307   fRunVertexFinderTracks(rec.fRunVertexFinderTracks),
308   fRunHLTTracking(rec.fRunHLTTracking),
309   fRunMuonTracking(rec.fRunMuonTracking),
310   fRunV0Finder(rec.fRunV0Finder),
311   fRunCascadeFinder(rec.fRunCascadeFinder),
312   fStopOnError(rec.fStopOnError),
313   fWriteAlignmentData(rec.fWriteAlignmentData),
314   fWriteESDfriend(rec.fWriteESDfriend),
315   fFillTriggerESD(rec.fFillTriggerESD),
316
317   fCleanESD(rec.fCleanESD),
318   fV0DCAmax(rec.fV0DCAmax),
319   fV0CsPmin(rec.fV0CsPmin),
320   fDmax(rec.fDmax),
321   fZmax(rec.fZmax),
322
323   fRunLocalReconstruction(rec.fRunLocalReconstruction),
324   fRunTracking(rec.fRunTracking),
325   fFillESD(rec.fFillESD),
326   fLoadCDB(rec.fLoadCDB),
327   fUseTrackingErrorsForAlignment(rec.fUseTrackingErrorsForAlignment),
328   fGAliceFileName(rec.fGAliceFileName),
329   fRawInput(rec.fRawInput),
330   fESDOutput(rec.fESDOutput),
331   fProofOutputFileName(rec.fProofOutputFileName),
332   fProofOutputLocation(rec.fProofOutputLocation),
333   fProofOutputDataset(rec.fProofOutputDataset),
334   fProofOutputArchive(rec.fProofOutputArchive),
335   fEquipIdMap(rec.fEquipIdMap),
336   fFirstEvent(rec.fFirstEvent),
337   fLastEvent(rec.fLastEvent),
338   fNumberOfEventsPerFile(rec.fNumberOfEventsPerFile),
339   fOptions(),
340   fLoadAlignFromCDB(rec.fLoadAlignFromCDB),
341   fLoadAlignData(rec.fLoadAlignData),
342   fUseHLTData(rec.fUseHLTData),
343   fRunInfo(NULL),
344   fEventInfo(),
345   fRunScalers(NULL),
346   fCTPTimeParams(NULL),
347
348   fRunLoader(NULL),
349   fRawReader(NULL),
350   fParentRawReader(NULL),
351
352   fRecoParam(rec.fRecoParam),
353
354   fSPDTrackleter(NULL),
355
356   fDiamondProfileSPD(rec.fDiamondProfileSPD),
357   fDiamondProfile(rec.fDiamondProfile),
358   fDiamondProfileTPC(rec.fDiamondProfileTPC),
359   fListOfCosmicTriggers(NULL),
360   
361   fGRPData(NULL),
362
363   fAlignObjArray(rec.fAlignObjArray),
364   fCDBUri(rec.fCDBUri),
365   fQARefUri(rec.fQARefUri),
366   fSpecCDBUri(), 
367   fInitCDBCalled(rec.fInitCDBCalled),
368   fSetRunNumberFromDataCalled(rec.fSetRunNumberFromDataCalled),
369   fQADetectors(rec.fQADetectors), 
370   fQATasks(rec.fQATasks), 
371   fRunQA(rec.fRunQA),  
372   fRunGlobalQA(rec.fRunGlobalQA),
373   fSameQACycle(rec.fSameQACycle),
374   fInitQACalled(rec.fInitQACalled),
375   fWriteQAExpertData(rec.fWriteQAExpertData), 
376   fRunPlaneEff(rec.fRunPlaneEff),
377
378   fesd(NULL),
379   fhltesd(NULL),
380   fesdf(NULL),
381   ffile(NULL),
382   ftree(NULL),
383   fhlttree(NULL),
384   ftVertexer(NULL),
385   fIsNewRunLoader(rec.fIsNewRunLoader),
386   fRunAliEVE(kFALSE),
387   fChain(NULL)
388 {
389 // copy constructor
390
391   for (Int_t i = 0; i < rec.fOptions.GetEntriesFast(); i++) {
392     if (rec.fOptions[i]) fOptions.Add(rec.fOptions[i]->Clone());
393   }
394   for (Int_t iDet = 0; iDet < kNDetectors; iDet++) {
395     fReconstructor[iDet] = NULL;
396     fLoader[iDet] = NULL;
397     fTracker[iDet] = NULL;
398   }  
399   
400   for (Int_t iDet = 0; iDet < AliQAv1::kNDET; iDet++) {
401     fQACycles[iDet] = rec.fQACycles[iDet];
402     fQAWriteExpert[iDet] = rec.fQAWriteExpert[iDet] ; 
403   }
404
405   for (Int_t i = 0; i < rec.fSpecCDBUri.GetEntriesFast(); i++) {
406     if (rec.fSpecCDBUri[i]) fSpecCDBUri.Add(rec.fSpecCDBUri[i]->Clone());
407   }
408 }
409
410 //_____________________________________________________________________________
411 AliReconstruction& AliReconstruction::operator = (const AliReconstruction& rec)
412 {
413 // assignment operator
414 // Used in PROOF mode
415 // Be very careful while modifing it!
416 // Simple rules to follow:
417 // for persistent data members - use their assignment operators
418 // for non-persistent ones - do nothing or take the default values from constructor
419 // TSelector members should not be touched
420   if(&rec == this) return *this;
421
422   fRunVertexFinder       = rec.fRunVertexFinder;
423   fRunVertexFinderTracks = rec.fRunVertexFinderTracks;
424   fRunHLTTracking        = rec.fRunHLTTracking;
425   fRunMuonTracking       = rec.fRunMuonTracking;
426   fRunV0Finder           = rec.fRunV0Finder;
427   fRunCascadeFinder      = rec.fRunCascadeFinder;
428   fStopOnError           = rec.fStopOnError;
429   fWriteAlignmentData    = rec.fWriteAlignmentData;
430   fWriteESDfriend        = rec.fWriteESDfriend;
431   fFillTriggerESD        = rec.fFillTriggerESD;
432
433   fCleanESD  = rec.fCleanESD;
434   fV0DCAmax  = rec.fV0DCAmax;
435   fV0CsPmin  = rec.fV0CsPmin;
436   fDmax      = rec.fDmax;
437   fZmax      = rec.fZmax;
438
439   fRunLocalReconstruction        = rec.fRunLocalReconstruction;
440   fRunTracking                   = rec.fRunTracking;
441   fFillESD                       = rec.fFillESD;
442   fLoadCDB                       = rec.fLoadCDB;
443   fUseTrackingErrorsForAlignment = rec.fUseTrackingErrorsForAlignment;
444   fGAliceFileName                = rec.fGAliceFileName;
445   fRawInput                      = rec.fRawInput;
446   fESDOutput                     = rec.fESDOutput;
447   fProofOutputFileName           = rec.fProofOutputFileName;
448   fProofOutputLocation           = rec.fProofOutputLocation;
449   fProofOutputDataset            = rec.fProofOutputDataset;
450   fProofOutputArchive            = rec.fProofOutputArchive;
451   fEquipIdMap                    = rec.fEquipIdMap;
452   fFirstEvent                    = rec.fFirstEvent;
453   fLastEvent                     = rec.fLastEvent;
454   fNumberOfEventsPerFile         = rec.fNumberOfEventsPerFile;
455
456   for (Int_t i = 0; i < rec.fOptions.GetEntriesFast(); i++) {
457     if (rec.fOptions[i]) fOptions.Add(rec.fOptions[i]->Clone());
458   }
459
460   fLoadAlignFromCDB              = rec.fLoadAlignFromCDB;
461   fLoadAlignData                 = rec.fLoadAlignData;
462   fUseHLTData                    = rec.fUseHLTData;
463
464   delete fRunInfo; fRunInfo = NULL;
465   if (rec.fRunInfo) fRunInfo = new AliRunInfo(*rec.fRunInfo);
466
467   fEventInfo                     = rec.fEventInfo;
468
469   delete fRunScalers; fRunScalers = NULL;
470   if (rec.fRunScalers) fRunScalers = new AliTriggerRunScalers(*rec.fRunScalers); 
471
472   delete fCTPTimeParams; fCTPTimeParams = NULL;
473   if (rec.fCTPTimeParams) fCTPTimeParams = new AliCTPTimeParams(*rec.fCTPTimeParams);
474
475   fRunLoader       = NULL;
476   fRawReader       = NULL;
477   fParentRawReader = NULL;
478
479   fRecoParam = rec.fRecoParam;
480
481   for (Int_t iDet = 0; iDet < kNDetectors; iDet++) {
482     delete fReconstructor[iDet]; fReconstructor[iDet] = NULL;
483     delete fLoader[iDet]; fLoader[iDet] = NULL;
484     delete fTracker[iDet]; fTracker[iDet] = NULL;
485   }
486   
487   for (Int_t iDet = 0; iDet < AliQAv1::kNDET; iDet++) {
488     fQACycles[iDet] = rec.fQACycles[iDet];
489     fQAWriteExpert[iDet] = rec.fQAWriteExpert[iDet] ;
490   } 
491
492   delete fSPDTrackleter; fSPDTrackleter = NULL;
493     
494   delete fDiamondProfileSPD; fDiamondProfileSPD = NULL;
495   if (rec.fDiamondProfileSPD) fDiamondProfileSPD = new AliESDVertex(*rec.fDiamondProfileSPD);
496   delete fDiamondProfile; fDiamondProfile = NULL;
497   if (rec.fDiamondProfile) fDiamondProfile = new AliESDVertex(*rec.fDiamondProfile);
498   delete fDiamondProfileTPC; fDiamondProfileTPC = NULL;
499   if (rec.fDiamondProfileTPC) fDiamondProfileTPC = new AliESDVertex(*rec.fDiamondProfileTPC);
500
501   delete fListOfCosmicTriggers; fListOfCosmicTriggers = NULL;
502   if (rec.fListOfCosmicTriggers) fListOfCosmicTriggers = (THashTable*)((rec.fListOfCosmicTriggers)->Clone());
503
504   delete fGRPData; fGRPData = NULL;
505   //  if (rec.fGRPData) fGRPData = (TMap*)((rec.fGRPData)->Clone());
506   if (rec.fGRPData) fGRPData = (AliGRPObject*)((rec.fGRPData)->Clone());
507
508   delete fAlignObjArray; fAlignObjArray = NULL;
509
510   fCDBUri        = "";
511   fQARefUri      = rec.fQARefUri;
512   fSpecCDBUri.Delete();
513   fInitCDBCalled               = rec.fInitCDBCalled;
514   fSetRunNumberFromDataCalled  = rec.fSetRunNumberFromDataCalled;
515   fQADetectors                 = rec.fQADetectors;
516   fQATasks                     = rec.fQATasks; 
517   fRunQA                       = rec.fRunQA;  
518   fRunGlobalQA                 = rec.fRunGlobalQA;
519   fSameQACycle                 = rec.fSameQACycle;
520   fInitQACalled                = rec.fInitQACalled;
521   fWriteQAExpertData           = rec.fWriteQAExpertData;
522   fRunPlaneEff                 = rec.fRunPlaneEff;
523
524   fesd     = NULL;
525   fhltesd  = NULL;
526   fesdf    = NULL;
527   ffile    = NULL;
528   ftree    = NULL;
529   fhlttree = NULL;
530   ftVertexer = NULL;
531   fIsNewRunLoader = rec.fIsNewRunLoader;
532   fRunAliEVE = kFALSE;
533   fChain = NULL;
534
535   return *this;
536 }
537
538 //_____________________________________________________________________________
539 AliReconstruction::~AliReconstruction()
540 {
541 // clean up
542
543   CleanUp();
544   if (fListOfCosmicTriggers) {
545     fListOfCosmicTriggers->Delete();
546     delete fListOfCosmicTriggers;
547   }
548   delete fGRPData;
549   delete fRunScalers;
550   delete fCTPTimeParams;
551   fOptions.Delete();
552   if (fAlignObjArray) {
553     fAlignObjArray->Delete();
554     delete fAlignObjArray;
555   }
556   fSpecCDBUri.Delete();
557
558   AliCodeTimer::Instance()->Print();
559 }
560
561 //_____________________________________________________________________________
562 void AliReconstruction::InitQA()
563 {
564   //Initialize the QA and start of cycle 
565   AliCodeTimerAuto("",0);
566   
567   if (fInitQACalled) return;
568   fInitQACalled = kTRUE;
569   
570   AliQAManager * qam = AliQAManager::QAManager(AliQAv1::kRECMODE) ; 
571   if (fWriteQAExpertData)
572     qam->SetWriteExpert() ; 
573  
574   if (qam->IsDefaultStorageSet()) {
575     AliWarning("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
576     AliWarning("Default QA reference storage has been already set !");
577     AliWarning(Form("Ignoring the default storage declared in AliReconstruction: %s",fQARefUri.Data()));
578     AliWarning("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
579     fQARefUri = qam->GetDefaultStorage()->GetURI();
580   } else {
581     if (fQARefUri.Length() > 0) {
582         AliDebug(2,"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
583         AliDebug(2, Form("Default QA reference storage is set to: %s", fQARefUri.Data()));
584         AliDebug(2, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
585       } else {
586         fQARefUri="local://$ALICE_ROOT/QAref";
587         AliWarning("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
588         AliWarning("Default QA refeference storage not yet set !!!!");
589         AliWarning(Form("Setting it now to: %s", fQARefUri.Data()));
590         AliWarning("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
591                 
592       }
593     qam->SetDefaultStorage(fQARefUri);
594   }
595   
596   if (fRunQA) {
597   qam->SetActiveDetectors(fQADetectors) ; 
598   for (Int_t det = 0 ; det < AliQAv1::kNDET ; det++) {
599     qam->SetCycleLength(AliQAv1::DETECTORINDEX_t(det), fQACycles[det]) ;  
600     qam->SetWriteExpert(AliQAv1::DETECTORINDEX_t(det)) ;
601   }
602   if (!fRawReader && !fInput && IsInTasks(AliQAv1::kRAWS))
603     fQATasks.ReplaceAll(Form("%d",AliQAv1::kRAWS), "") ;
604   qam->SetTasks(fQATasks) ; 
605   qam->InitQADataMaker(AliCDBManager::Instance()->GetRun()) ; 
606   }
607   if (fRunGlobalQA) {
608     Bool_t sameCycle = kFALSE ;
609     AliQADataMaker *qadm = qam->GetQADataMaker(AliQAv1::kGLOBAL);
610     AliInfo(Form("Initializing the global QA data maker"));
611     if (IsInTasks(AliQAv1::kRECPOINTS)) {
612       qadm->StartOfCycle(AliQAv1::kRECPOINTS, AliCDBManager::Instance()->GetRun(), sameCycle) ; 
613       TObjArray **arr=qadm->Init(AliQAv1::kRECPOINTS);
614       AliTracker::SetResidualsArray(arr);
615       sameCycle = kTRUE ; 
616     }
617     if (IsInTasks(AliQAv1::kESDS)) {
618       qadm->StartOfCycle(AliQAv1::kESDS, AliCDBManager::Instance()->GetRun(), sameCycle) ; 
619       qadm->Init(AliQAv1::kESDS);
620     }
621   }
622     AliSysInfo::AddStamp("InitQA") ; 
623 }
624
625 //_____________________________________________________________________________
626 void AliReconstruction::MergeQA(const char *fileName)
627 {
628   //Initialize the QA and start of cycle 
629   AliCodeTimerAuto("",0) ;
630   AliQAManager::QAManager()->Merge(AliCDBManager::Instance()->GetRun(),fileName) ; 
631   AliSysInfo::AddStamp("MergeQA") ; 
632 }
633   
634 //_____________________________________________________________________________
635 void AliReconstruction::InitCDB()
636 {
637 // activate a default CDB storage
638 // First check if we have any CDB storage set, because it is used 
639 // to retrieve the calibration and alignment constants
640   AliCodeTimerAuto("",0);
641
642   if (fInitCDBCalled) return;
643   fInitCDBCalled = kTRUE;
644
645   AliCDBManager* man = AliCDBManager::Instance();
646   if (man->IsDefaultStorageSet())
647   {
648     AliWarning("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
649     AliWarning("Default CDB storage has been already set !");
650     AliWarning(Form("Ignoring the default storage declared in AliReconstruction: %s",fCDBUri.Data()));
651     AliWarning("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
652     fCDBUri = man->GetDefaultStorage()->GetURI();
653   }
654   else {
655     if (fCDBUri.Length() > 0) 
656     {
657         AliDebug(2,"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
658         AliDebug(2, Form("Default CDB storage is set to: %s", fCDBUri.Data()));
659         AliDebug(2, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
660         man->SetDefaultStorage(fCDBUri);
661     } 
662     else if (!man->GetRaw()){
663         fCDBUri="local://$ALICE_ROOT/OCDB";
664         AliWarning("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
665         AliWarning("Default CDB storage not yet set !!!!");
666         AliWarning(Form("Setting it now to: %s", fCDBUri.Data()));
667         AliWarning("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
668         man->SetDefaultStorage(fCDBUri);
669     }
670     else {    
671         AliWarning("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
672         AliWarning("Default storage will be set after setting the Run Number!!!");
673         AliWarning("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");                    
674     }
675   }
676
677   // Now activate the detector specific CDB storage locations
678   for (Int_t i = 0; i < fSpecCDBUri.GetEntriesFast(); i++) {
679     TObject* obj = fSpecCDBUri[i];
680     if (!obj) continue;
681     AliDebug(2, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
682     AliDebug(2, Form("Specific CDB storage for %s is set to: %s",obj->GetName(),obj->GetTitle()));
683     AliDebug(2, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
684     man->SetSpecificStorage(obj->GetName(), obj->GetTitle());
685   }
686   AliSysInfo::AddStamp("InitCDB");
687 }
688
689 //_____________________________________________________________________________
690 void AliReconstruction::SetDefaultStorage(const char* uri) {
691 // Store the desired default CDB storage location
692 // Activate it later within the Run() method
693
694   fCDBUri = uri;
695
696 }
697
698 //_____________________________________________________________________________
699 void AliReconstruction::SetQARefDefaultStorage(const char* uri) {
700   // Store the desired default CDB storage location
701   // Activate it later within the Run() method
702   
703   fQARefUri = uri;
704   AliQAv1::SetQARefStorage(fQARefUri.Data()) ;
705   
706 }
707 //_____________________________________________________________________________
708 void AliReconstruction::SetSpecificStorage(const char* calibType, const char* uri) {
709 // Store a detector-specific CDB storage location
710 // Activate it later within the Run() method
711
712   AliCDBPath aPath(calibType);
713   if(!aPath.IsValid()){
714         // if calibType is not wildcard but it is a valid detector, add "/*" to make it a valid path
715         for (Int_t iDet = 0; iDet < kNDetectors; iDet++) {
716                 if(!strcmp(calibType, fgkDetectorName[iDet])) {
717                         aPath.SetPath(Form("%s/*", calibType));
718                         AliInfo(Form("Path for specific storage set to %s", aPath.GetPath().Data()));
719                         break;
720                 }
721         }
722         if(!aPath.IsValid()){
723                 AliError(Form("Not a valid path or detector: %s", calibType));
724                 return;
725         }
726   }
727
728 //  // check that calibType refers to a "valid" detector name
729 //  Bool_t isDetector = kFALSE;
730 //  for (Int_t iDet = 0; iDet < kNDetectors; iDet++) {
731 //    TString detName = fgkDetectorName[iDet];
732 //    if(aPath.GetLevel0() == detName) {
733 //      isDetector = kTRUE;
734 //      break;
735 //    }
736 //  }
737 //
738 //  if(!isDetector) {
739 //      AliError(Form("Not a valid detector: %s", aPath.GetLevel0().Data()));
740 //      return;
741 //  }
742
743   TObject* obj = fSpecCDBUri.FindObject(aPath.GetPath().Data());
744   if (obj) fSpecCDBUri.Remove(obj);
745   fSpecCDBUri.Add(new TNamed(aPath.GetPath().Data(), uri));
746
747 }
748
749 //_____________________________________________________________________________
750 Bool_t AliReconstruction::SetRunNumberFromData()
751 {
752   // The method is called in Run() in order
753   // to set a correct run number.
754   // In case of raw data reconstruction the
755   // run number is taken from the raw data header
756
757   if (fSetRunNumberFromDataCalled) return kTRUE;
758   fSetRunNumberFromDataCalled = kTRUE;
759   
760   AliCDBManager* man = AliCDBManager::Instance();
761  
762   if(fRawReader) {
763     if(fRawReader->NextEvent()) {
764       if(man->GetRun() > 0) {
765         AliWarning("Run number is taken from raw-event header! Ignoring settings in AliCDBManager!");
766       } 
767       man->SetRun(fRawReader->GetRunNumber());
768       fRawReader->RewindEvents();
769     }
770     else {
771       if(man->GetRun() > 0) {
772         AliWarning("No raw-data events are found ! Using settings in AliCDBManager !");
773       }
774       else {
775         AliWarning("Neither raw events nor settings in AliCDBManager are found !");
776         return kFALSE;
777       }
778     }
779   }
780   else {
781     AliRunLoader *rl = AliRunLoader::Open(fGAliceFileName.Data());
782     if (!rl) {
783       AliError(Form("No run loader found in file %s", fGAliceFileName.Data()));
784       return kFALSE;
785     }
786     else {
787       rl->LoadHeader();
788       // read run number from gAlice
789       if(rl->GetHeader()) {
790         man->SetRun(rl->GetHeader()->GetRun());
791         rl->UnloadHeader();
792         delete rl;
793       }
794       else {
795         AliError("Neither run-loader header nor RawReader objects are found !");
796         delete rl;
797         return kFALSE;
798       }
799     }
800   }
801
802   man->Print();  
803   
804   return kTRUE;
805 }
806
807 //_____________________________________________________________________________
808 void AliReconstruction::SetCDBLock() {
809   // Set CDB lock: from now on it is forbidden to reset the run number
810   // or the default storage or to activate any further storage!
811   
812   AliCDBManager::Instance()->SetLock(1);
813 }
814
815 //_____________________________________________________________________________
816 Bool_t AliReconstruction::MisalignGeometry(const TString& detectors)
817 {
818   // Read the alignment objects from CDB.
819   // Each detector is supposed to have the
820   // alignment objects in DET/Align/Data CDB path.
821   // All the detector objects are then collected,
822   // sorted by geometry level (starting from ALIC) and
823   // then applied to the TGeo geometry.
824   // Finally an overlaps check is performed.
825
826   // Load alignment data from CDB and fill fAlignObjArray 
827   if(fLoadAlignFromCDB){
828         
829     TString detStr = detectors;
830     TString loadAlObjsListOfDets = "";
831     
832     for (Int_t iDet = 0; iDet < kNDetectors; iDet++) {
833       if(!IsSelected(fgkDetectorName[iDet], detStr)) continue;
834       if(!strcmp(fgkDetectorName[iDet],"HLT")) continue;
835       
836       if(AliGeomManager::GetNalignable(fgkDetectorName[iDet]) != 0)
837       {
838         loadAlObjsListOfDets += fgkDetectorName[iDet];
839         loadAlObjsListOfDets += " ";
840       }
841     } // end loop over detectors
842     
843     if(AliGeomManager::GetNalignable("GRP") != 0)
844       loadAlObjsListOfDets.Prepend("GRP "); //add alignment objects for non-sensitive modules
845     AliGeomManager::ApplyAlignObjsFromCDB(loadAlObjsListOfDets.Data());
846     AliCDBManager::Instance()->UnloadFromCache("*/Align/*");
847   }else{
848     // Check if the array with alignment objects was
849     // provided by the user. If yes, apply the objects
850     // to the present TGeo geometry
851     if (fAlignObjArray) {
852       if (gGeoManager && gGeoManager->IsClosed()) {
853         if (AliGeomManager::ApplyAlignObjsToGeom(*fAlignObjArray) == kFALSE) {
854           AliError("The misalignment of one or more volumes failed!"
855                    "Compare the list of simulated detectors and the list of detector alignment data!");
856           return kFALSE;
857         }
858       }
859       else {
860         AliError("Can't apply the misalignment! gGeoManager doesn't exist or it is still opened!");
861         return kFALSE;
862       }
863     }
864   }
865   
866   if (fAlignObjArray) {
867     fAlignObjArray->Delete();
868     delete fAlignObjArray; fAlignObjArray=NULL;
869   }
870
871   return kTRUE;
872 }
873
874 //_____________________________________________________________________________
875 void AliReconstruction::SetGAliceFile(const char* fileName)
876 {
877 // set the name of the galice file
878
879   fGAliceFileName = fileName;
880 }
881
882 //_____________________________________________________________________________
883 void AliReconstruction::SetInput(const char* input) 
884 {
885   // In case the input string starts with 'mem://', we run in an online mode
886   // and AliRawReaderDateOnline object is created. In all other cases a raw-data
887   // file is assumed. One can give as an input:
888   // mem://: - events taken from DAQ monitoring libs online
889   //  or
890   // mem://<filename> - emulation of the above mode (via DATE monitoring libs)
891   if (input) fRawInput = input;
892 }
893
894 //_____________________________________________________________________________
895 void AliReconstruction::SetOutput(const char* output) 
896 {
897   // Set the output ESD filename
898   // 'output' is a normalt ROOT url
899   // The method is used in case of raw-data reco with PROOF
900   if (output) fESDOutput = output;
901 }
902
903 //_____________________________________________________________________________
904 void AliReconstruction::SetOption(const char* detector, const char* option)
905 {
906 // set options for the reconstruction of a detector
907
908   TObject* obj = fOptions.FindObject(detector);
909   if (obj) fOptions.Remove(obj);
910   fOptions.Add(new TNamed(detector, option));
911 }
912
913 //_____________________________________________________________________________
914 void AliReconstruction::SetRecoParam(const char* detector, AliDetectorRecoParam *par)
915 {
916   // Set custom reconstruction parameters for a given detector
917   // Single set of parameters for all the events
918
919   // First check if the reco-params are global
920   if(!strcmp(detector, "GRP")) {
921     par->SetAsDefault();
922     fRecoParam.AddDetRecoParam(kNDetectors,par);
923     return;
924   }
925
926   for (Int_t iDet = 0; iDet < kNDetectors; iDet++) {
927     if(!strcmp(detector, fgkDetectorName[iDet])) {
928       par->SetAsDefault();
929       fRecoParam.AddDetRecoParam(iDet,par);
930       break;
931     }
932   }
933
934 }
935
936 //_____________________________________________________________________________
937 Bool_t AliReconstruction::InitGRP() {
938   //------------------------------------
939   // Initialization of the GRP entry 
940   //------------------------------------
941   AliCDBEntry* entry = AliCDBManager::Instance()->Get("GRP/GRP/Data");
942
943   if (entry) {
944
945     TMap* m = dynamic_cast<TMap*>(entry->GetObject());  // old GRP entry
946
947     if (m) {
948        AliInfo("Found a TMap in GRP/GRP/Data, converting it into an AliGRPObject");
949        m->Print();
950        fGRPData = new AliGRPObject();
951        fGRPData->ReadValuesFromMap(m);
952     }
953
954     else {
955        AliInfo("Found an AliGRPObject in GRP/GRP/Data, reading it");
956        fGRPData = dynamic_cast<AliGRPObject*>(entry->GetObject());  // new GRP entry
957        entry->SetOwner(0);
958     }
959
960     //    FIX ME: The unloading of GRP entry is temporarily disabled
961     //    because ZDC and VZERO are using it in order to initialize
962     //    their reconstructor objects. In the future one has to think
963     //    of propagating AliRunInfo to the reconstructors.
964     //    AliCDBManager::Instance()->UnloadFromCache("GRP/GRP/Data");
965   }
966
967   if (!fGRPData) {
968      AliError("No GRP entry found in OCDB!");
969      return kFALSE;
970   }
971
972   TString lhcState = fGRPData->GetLHCState();
973   if (lhcState==AliGRPObject::GetInvalidString()) {
974     AliError("GRP/GRP/Data entry:  missing value for the LHC state ! Using UNKNOWN");
975     lhcState = "UNKNOWN";
976   }
977
978   TString beamType = fGRPData->GetBeamType();
979   if (beamType==AliGRPObject::GetInvalidString()) {
980     AliError("GRP/GRP/Data entry:  missing value for the beam type ! Using UNKNOWN");
981     beamType = "UNKNOWN";
982   }
983
984   Float_t beamEnergy = fGRPData->GetBeamEnergy();
985   if (beamEnergy==AliGRPObject::GetInvalidFloat()) {
986     AliError("GRP/GRP/Data entry:  missing value for the beam energy ! Using 0");
987     beamEnergy = 0;
988   }
989
990   TString runType = fGRPData->GetRunType();
991   if (runType==AliGRPObject::GetInvalidString()) {
992     AliError("GRP/GRP/Data entry:  missing value for the run type ! Using UNKNOWN");
993     runType = "UNKNOWN";
994   }
995
996   Int_t activeDetectors = fGRPData->GetDetectorMask();
997   if (activeDetectors==AliGRPObject::GetInvalidUInt()) {
998     AliError("GRP/GRP/Data entry:  missing value for the detector mask ! Using 1074790399");
999     activeDetectors = 1074790399;
1000   }
1001
1002   fRunInfo = new AliRunInfo(lhcState, beamType, beamEnergy, runType, activeDetectors);
1003   fRunInfo->Dump();
1004
1005
1006   // Process the list of active detectors
1007   if (activeDetectors) {
1008     UInt_t detMask = activeDetectors;
1009     fRunLocalReconstruction = MatchDetectorList(fRunLocalReconstruction,detMask);
1010     fRunTracking = MatchDetectorList(fRunTracking,detMask);
1011     fFillESD = MatchDetectorList(fFillESD,detMask);
1012     fQADetectors = MatchDetectorList(fQADetectors,detMask);
1013     fLoadCDB.Form("%s %s %s %s",
1014                   fRunLocalReconstruction.Data(),
1015                   fRunTracking.Data(),
1016                   fFillESD.Data(),
1017                   fQADetectors.Data());
1018     fLoadCDB = MatchDetectorList(fLoadCDB,detMask);
1019     if (!((detMask >> AliDAQ::DetectorID("ITSSPD")) & 0x1) &&
1020         !((detMask >> AliDAQ::DetectorID("ITSSDD")) & 0x1) &&
1021         !((detMask >> AliDAQ::DetectorID("ITSSSD")) & 0x1) ) {
1022       // switch off the vertexer
1023       AliInfo("SPD,SDD,SSD is not in the list of active detectors. Vertexer switched off.");
1024       fRunVertexFinder = kFALSE;
1025     }
1026     if (!((detMask >> AliDAQ::DetectorID("TRG")) & 0x1)) {
1027       // switch off the reading of CTP raw-data payload
1028       if (fFillTriggerESD) {
1029         AliInfo("CTP is not in the list of active detectors. CTP data reading switched off.");
1030         fFillTriggerESD = kFALSE;
1031       }
1032     }
1033   }
1034
1035   AliInfo("===================================================================================");
1036   AliInfo(Form("Running local reconstruction for detectors: %s",fRunLocalReconstruction.Data()));
1037   AliInfo(Form("Running tracking for detectors: %s",fRunTracking.Data()));
1038   AliInfo(Form("Filling ESD for detectors: %s",fFillESD.Data()));
1039   AliInfo(Form("Quality assurance is active for detectors: %s",fQADetectors.Data()));
1040   AliInfo(Form("CDB and reconstruction parameters are loaded for detectors: %s",fLoadCDB.Data()));
1041   AliInfo("===================================================================================");
1042
1043   //*** Dealing with the magnetic field map
1044   if ( TGeoGlobalMagField::Instance()->IsLocked() ) {
1045     if (TGeoGlobalMagField::Instance()->GetField()->TestBit(AliMagF::kOverrideGRP)) {
1046       AliInfo("ExpertMode!!! GRP information will be ignored !");
1047       AliInfo("ExpertMode!!! Running with the externally locked B field !");
1048     }
1049     else {
1050       AliInfo("Destroying existing B field instance!");
1051       delete TGeoGlobalMagField::Instance();
1052     }    
1053   }
1054   if ( !TGeoGlobalMagField::Instance()->IsLocked() ) {
1055     // Construct the field map out of the information retrieved from GRP.
1056     Bool_t ok = kTRUE;
1057     // L3
1058     Float_t l3Current = fGRPData->GetL3Current((AliGRPObject::Stats)0);
1059     if (l3Current == AliGRPObject::GetInvalidFloat()) {
1060       AliError("GRP/GRP/Data entry:  missing value for the L3 current !");
1061       ok = kFALSE;
1062     }
1063     
1064     Char_t l3Polarity = fGRPData->GetL3Polarity();
1065     if (l3Polarity == AliGRPObject::GetInvalidChar()) {
1066       AliError("GRP/GRP/Data entry:  missing value for the L3 polarity !");
1067       ok = kFALSE;
1068     }
1069
1070     // Dipole
1071     Float_t diCurrent = fGRPData->GetDipoleCurrent((AliGRPObject::Stats)0);
1072     if (diCurrent == AliGRPObject::GetInvalidFloat()) {
1073       AliError("GRP/GRP/Data entry:  missing value for the dipole current !");
1074       ok = kFALSE;
1075     }
1076
1077     Char_t diPolarity = fGRPData->GetDipolePolarity();
1078     if (diPolarity == AliGRPObject::GetInvalidChar()) {
1079       AliError("GRP/GRP/Data entry:  missing value for the dipole polarity !");
1080       ok = kFALSE;
1081     }
1082
1083     // read special bits for the polarity convention and map type
1084     Int_t  polConvention = fGRPData->IsPolarityConventionLHC() ? AliMagF::kConvLHC : AliMagF::kConvDCS2008;
1085     Bool_t uniformB = fGRPData->IsUniformBMap();
1086
1087     if (ok) { 
1088       AliMagF* fld = AliMagF::CreateFieldMap(TMath::Abs(l3Current) * (l3Polarity ? -1:1), 
1089                                              TMath::Abs(diCurrent) * (diPolarity ? -1:1), 
1090                                              polConvention,uniformB,beamEnergy, beamType.Data());
1091       if (fld) {
1092         TGeoGlobalMagField::Instance()->SetField( fld );
1093         TGeoGlobalMagField::Instance()->Lock();
1094         AliInfo("Running with the B field constructed out of GRP !");
1095       }
1096       else AliFatal("Failed to create a B field map !");
1097     }
1098     else AliFatal("B field is neither set nor constructed from GRP ! Exitig...");
1099   }
1100   
1101   //*** Get the diamond profiles from OCDB
1102   entry = AliCDBManager::Instance()->Get("GRP/Calib/MeanVertexSPD");
1103   if (entry) {
1104     fDiamondProfileSPD = dynamic_cast<AliESDVertex*> (entry->GetObject());  
1105   } else {
1106      AliError("No SPD diamond profile found in OCDB!");
1107   }
1108
1109   entry = AliCDBManager::Instance()->Get("GRP/Calib/MeanVertex");
1110   if (entry) {
1111     fDiamondProfile = dynamic_cast<AliESDVertex*> (entry->GetObject());  
1112   } else {
1113      AliError("No diamond profile found in OCDB!");
1114   }
1115
1116   entry = AliCDBManager::Instance()->Get("GRP/Calib/MeanVertexTPC");
1117   if (entry) {
1118     fDiamondProfileTPC = dynamic_cast<AliESDVertex*> (entry->GetObject());  
1119   } else {
1120      AliError("No TPC diamond profile found in OCDB!");
1121   }
1122
1123   entry = AliCDBManager::Instance()->Get("GRP/Calib/CosmicTriggers");
1124   if (entry) {
1125     fListOfCosmicTriggers = dynamic_cast<THashTable*>(entry->GetObject());
1126     entry->SetOwner(0);
1127     AliCDBManager::Instance()->UnloadFromCache("GRP/Calib/CosmicTriggers");
1128   }
1129
1130   if (!fListOfCosmicTriggers) {
1131     AliWarning("Can not get list of cosmic triggers from OCDB! Cosmic event specie will be effectively disabled!");
1132   }
1133
1134   return kTRUE;
1135
1136
1137 //_____________________________________________________________________________
1138 Bool_t AliReconstruction::LoadCDB()
1139 {
1140   AliCodeTimerAuto("",0);
1141
1142   AliCDBManager::Instance()->Get("GRP/CTP/Config");
1143
1144   TString detStr = fLoadCDB;
1145   for (Int_t iDet = 0; iDet < kNDetectors; iDet++) {
1146     if (!IsSelected(fgkDetectorName[iDet], detStr)) continue;
1147     AliCDBManager::Instance()->GetAll(Form("%s/Calib/*",fgkDetectorName[iDet]));
1148   }
1149
1150   // Temporary fix - one has to define the correct policy in order
1151   // to load the trigger OCDB entries only for the detectors that
1152   // in the trigger or that are needed in order to put correct
1153   // information in ESD
1154   AliCDBManager::Instance()->GetAll("TRIGGER/*/*");
1155
1156   return kTRUE;
1157 }
1158 //_____________________________________________________________________________
1159 Bool_t AliReconstruction::LoadTriggerScalersCDB()
1160 {
1161   AliCodeTimerAuto("",0);
1162
1163   AliCDBEntry* entry = AliCDBManager::Instance()->Get("GRP/CTP/Scalers");
1164
1165   if (entry) { 
1166    
1167        AliInfo("Found an AliTriggerRunScalers in GRP/CTP/Scalers, reading it");
1168        fRunScalers = dynamic_cast<AliTriggerRunScalers*> (entry->GetObject());
1169        entry->SetOwner(0);
1170        if (fRunScalers->CorrectScalersOverflow() == 0) AliInfo("32bit Trigger counters corrected for overflow");
1171
1172   }
1173   return kTRUE;
1174 }
1175 //_____________________________________________________________________________
1176 Bool_t AliReconstruction::LoadCTPTimeParamsCDB()
1177 {
1178   AliCDBEntry* entry = AliCDBManager::Instance()->Get("GRP/CTP/CTPtiming");
1179
1180   if (entry) {
1181
1182        AliInfo("Found an AliCTPTimeParams in GRP/CTP/CTPtiming, reading it");
1183        fCTPTimeParams = dynamic_cast<AliCTPTimeParams*> (entry->GetObject());
1184        entry->SetOwner(0);
1185        return kTRUE;
1186   }
1187   
1188   return kFALSE; 
1189 }
1190 //_____________________________________________________________________________
1191 Bool_t AliReconstruction::Run(const char* input)
1192 {
1193   // Run Run Run
1194   AliCodeTimerAuto("",0);
1195
1196   InitRun(input);
1197   if (GetAbort() != TSelector::kContinue) return kFALSE;
1198
1199   TChain *chain = NULL;
1200   if (fRawReader && (chain = fRawReader->GetChain())) {
1201     Long64_t nEntries = (fLastEvent < 0) ? (TChain::kBigNumber) : (fLastEvent - fFirstEvent + 1);
1202     // Proof mode
1203     if (gProof) {
1204       // Temporary fix for long raw-data runs (until socket timeout handling in PROOF is revised)
1205       gProof->Exec("gEnv->SetValue(\"Proof.SocketActivityTimeout\",-1)", kTRUE);
1206
1207       if (gGrid)
1208         gProof->Exec("TGrid::Connect(\"alien://\")",kTRUE);
1209
1210       TMessage::EnableSchemaEvolutionForAll(kTRUE);
1211       gProof->Exec("TMessage::EnableSchemaEvolutionForAll(kTRUE)",kTRUE);
1212
1213       gProof->AddInput(this);
1214
1215       if (!ParseOutput()) return kFALSE;
1216
1217       gProof->SetParameter("PROOF_MaxSlavesPerNode", 9999);
1218       chain->SetProof();
1219       chain->Process("AliReconstruction","",nEntries,fFirstEvent);
1220     }
1221     else {
1222       chain->Process(this,"",nEntries,fFirstEvent);
1223     }
1224   }
1225   else {
1226     Begin(NULL);
1227     if (GetAbort() != TSelector::kContinue) return kFALSE;
1228     SlaveBegin(NULL);
1229     if (GetAbort() != TSelector::kContinue) return kFALSE;
1230     //******* The loop over events
1231     AliInfo("Starting looping over events");
1232     Int_t iEvent = 0;
1233     while ((iEvent < fRunLoader->GetNumberOfEvents()) ||
1234            (fRawReader && fRawReader->NextEvent())) {
1235       if (!ProcessEvent(iEvent)) {
1236         Abort("ProcessEvent",TSelector::kAbortFile);
1237         return kFALSE;
1238       }
1239       iEvent++;
1240     }
1241     SlaveTerminate();
1242     if (GetAbort() != TSelector::kContinue) return kFALSE;
1243     Terminate();
1244     if (GetAbort() != TSelector::kContinue) return kFALSE;
1245   }
1246
1247   return kTRUE;
1248 }
1249
1250 //_____________________________________________________________________________
1251 void AliReconstruction::InitRawReader(const char* input)
1252 {
1253   AliCodeTimerAuto("",0);
1254
1255   // Init raw-reader and
1256   // set the input in case of raw data
1257   if (input) fRawInput = input;
1258   fRawReader = AliRawReader::Create(fRawInput.Data());
1259   if (!fRawReader) {
1260     if (fRawInput.IsNull()) {
1261       AliInfo("Reconstruction will run over digits");
1262     }
1263     else {
1264       AliFatal("Can not create raw-data reader ! Exiting..."); 
1265     }
1266   }
1267
1268   if (!fEquipIdMap.IsNull() && fRawReader)
1269     fRawReader->LoadEquipmentIdsMap(fEquipIdMap);
1270
1271   if (!fUseHLTData.IsNull()) {
1272     // create the RawReaderHLT which performs redirection of HLT input data for
1273     // the specified detectors
1274     AliRawReader* pRawReader=AliRawHLTManager::CreateRawReaderHLT(fRawReader, fUseHLTData.Data());
1275     if (pRawReader) {
1276       fParentRawReader=fRawReader;
1277       fRawReader=pRawReader;
1278     } else {
1279       AliError(Form("can not create Raw Reader for HLT input %s", fUseHLTData.Data()));
1280     }
1281   }
1282   AliSysInfo::AddStamp("CreateRawReader");
1283 }
1284
1285 //_____________________________________________________________________________
1286 void AliReconstruction::InitRun(const char* input)
1287 {
1288   // Initialization of raw-reader,
1289   // run number, CDB etc.
1290   AliCodeTimerAuto("",0);
1291   AliSysInfo::AddStamp("Start");
1292
1293   // Initialize raw-reader if any
1294   InitRawReader(input);
1295
1296   // Initialize the CDB storage
1297   InitCDB();
1298
1299   // Set run number in CDBManager (if it is not already set by the user)
1300   if (!SetRunNumberFromData()) {
1301     Abort("SetRunNumberFromData", TSelector::kAbortProcess);
1302     return;
1303   }
1304
1305   // Set CDB lock: from now on it is forbidden to reset the run number
1306   // or the default storage or to activate any further storage!
1307   SetCDBLock();
1308   
1309 }
1310
1311 //_____________________________________________________________________________
1312 void AliReconstruction::Begin(TTree *)
1313 {
1314   // Initialize AlReconstruction before
1315   // going into the event loop
1316   // Should follow the TSelector convention
1317   // i.e. initialize only the object on the client side
1318   AliCodeTimerAuto("",0);
1319
1320   AliReconstruction *reco = NULL;
1321   if (fInput) {
1322     if ((reco = (AliReconstruction*)fInput->FindObject("AliReconstruction"))) {
1323       *this = *reco;
1324     }
1325     AliSysInfo::AddStamp("ReadInputInBegin");
1326   }
1327
1328   // Import ideal TGeo geometry and apply misalignment
1329   if (!gGeoManager) {
1330     TString geom(gSystem->DirName(fGAliceFileName));
1331     geom += "/geometry.root";
1332     AliGeomManager::LoadGeometry(geom.Data());
1333     if (!gGeoManager) {
1334       Abort("LoadGeometry", TSelector::kAbortProcess);
1335       return;
1336     }
1337     AliSysInfo::AddStamp("LoadGeom");
1338     TString detsToCheck=fRunLocalReconstruction;
1339     if(!AliGeomManager::CheckSymNamesLUT(detsToCheck.Data())) {
1340       Abort("CheckSymNamesLUT", TSelector::kAbortProcess);
1341       return;
1342     }
1343     AliSysInfo::AddStamp("CheckGeom");
1344   }
1345
1346   if (!MisalignGeometry(fLoadAlignData)) {
1347     Abort("MisalignGeometry", TSelector::kAbortProcess);
1348     return;
1349   }
1350   AliCDBManager::Instance()->UnloadFromCache("GRP/Geometry/Data");
1351   AliSysInfo::AddStamp("MisalignGeom");
1352
1353   if (!InitGRP()) {
1354     Abort("InitGRP", TSelector::kAbortProcess);
1355     return;
1356   }
1357   AliSysInfo::AddStamp("InitGRP");
1358
1359   if (!LoadCDB()) {
1360     Abort("LoadCDB", TSelector::kAbortProcess);
1361     return;
1362   }
1363   AliSysInfo::AddStamp("LoadCDB");
1364
1365   if (!LoadTriggerScalersCDB()) {
1366     Abort("LoadTriggerScalersCDB", TSelector::kAbortProcess);
1367     return;
1368   }
1369   AliSysInfo::AddStamp("LoadTriggerScalersCDB");
1370
1371   if (!LoadCTPTimeParamsCDB()) {
1372     Abort("LoadCTPTimeParamsCDB", TSelector::kAbortProcess);
1373     return;
1374   }
1375   AliSysInfo::AddStamp("LoadCTPTimeParamsCDB");
1376
1377   // Read the reconstruction parameters from OCDB
1378   if (!InitRecoParams()) {
1379     AliWarning("Not all detectors have correct RecoParam objects initialized");
1380   }
1381   AliSysInfo::AddStamp("InitRecoParams");
1382
1383   if (fInput && gProof) {
1384     if (reco) *reco = *this;
1385
1386     gGeoManager->SetName("Geometry");
1387     gProof->AddInputData(gGeoManager,kTRUE);
1388     gGeoManager = NULL;
1389     gProof->AddInputData(const_cast<TMap*>(AliCDBManager::Instance()->GetEntryCache()),kTRUE);
1390     fInput->Add(new TParameter<Int_t>("RunNumber",AliCDBManager::Instance()->GetRun()));
1391     AliMagF *magFieldMap = (AliMagF*)TGeoGlobalMagField::Instance()->GetField();
1392     magFieldMap->SetName("MagneticFieldMap");
1393     gProof->AddInputData(magFieldMap,kTRUE);
1394   }
1395
1396 }
1397
1398 //_____________________________________________________________________________
1399 void AliReconstruction::SlaveBegin(TTree*)
1400 {
1401   // Initialization related to run-loader,
1402   // vertexer, trackers, recontructors
1403   // In proof mode it is executed on the slave
1404   AliCodeTimerAuto("",0);
1405
1406   TProofOutputFile *outProofFile = NULL;
1407   if (fInput) {
1408     if (AliDebugLevel() > 0) fInput->Print();
1409     if (AliDebugLevel() > 10) fInput->Dump();
1410     if (AliReconstruction *reco = (AliReconstruction*)fInput->FindObject("AliReconstruction")) {
1411       *this = *reco;
1412     }
1413     if (TGeoManager *tgeo = (TGeoManager*)fInput->FindObject("Geometry")) {
1414       gGeoManager = tgeo;
1415       AliGeomManager::SetGeometry(tgeo);
1416     }
1417     if (TMap *entryCache = (TMap*)fInput->FindObject("CDBEntryCache")) {
1418       Int_t runNumber = -1;
1419       if (TProof::GetParameter(fInput,"RunNumber",runNumber) == 0) {
1420         AliCDBManager *man = AliCDBManager::Instance(entryCache,runNumber);
1421         man->SetCacheFlag(kTRUE);
1422         man->SetLock(kTRUE);
1423         man->Print();
1424       }
1425     }
1426     if (AliMagF *map = (AliMagF*)fInput->FindObject("MagneticFieldMap")) {
1427       AliMagF *newMap = new AliMagF(*map);
1428       if (!newMap->LoadParameterization()) {
1429         Abort("AliMagF::LoadParameterization", TSelector::kAbortProcess);
1430         return;
1431       }
1432       TGeoGlobalMagField::Instance()->SetField(newMap);
1433       TGeoGlobalMagField::Instance()->Lock();
1434     }
1435     if (TNamed *outputFileName = (TNamed*)fInput->FindObject("PROOF_OUTPUTFILE"))
1436       fProofOutputFileName = outputFileName->GetTitle();
1437     if (TNamed *outputLocation = (TNamed*)fInput->FindObject("PROOF_OUTPUTFILE_LOCATION"))
1438       fProofOutputLocation = outputLocation->GetTitle();
1439     if (fInput->FindObject("PROOF_OUTPUTFILE_DATASET"))
1440       fProofOutputDataset = kTRUE;
1441     if (TNamed *archiveList = (TNamed*)fInput->FindObject("PROOF_OUTPUTFILE_ARCHIVE"))
1442       fProofOutputArchive = archiveList->GetTitle();
1443     if (!fProofOutputFileName.IsNull() &&
1444         !fProofOutputLocation.IsNull() &&
1445         fProofOutputArchive.IsNull()) {
1446       if (!fProofOutputDataset) {
1447         outProofFile = new TProofOutputFile(fProofOutputFileName.Data(),"M");
1448         outProofFile->SetOutputFileName(Form("%s%s",fProofOutputLocation.Data(),fProofOutputFileName.Data()));
1449       }
1450       else {
1451         outProofFile = new TProofOutputFile(fProofOutputFileName.Data(),"DROV",fProofOutputLocation.Data());
1452       }
1453       if (AliDebugLevel() > 0) outProofFile->Dump();
1454       fOutput->Add(outProofFile);
1455     }
1456     AliSysInfo::AddStamp("ReadInputInSlaveBegin");
1457   }
1458
1459   // get the run loader
1460   if (!InitRunLoader()) {
1461     Abort("InitRunLoader", TSelector::kAbortProcess);
1462     return;
1463   }
1464   AliSysInfo::AddStamp("LoadLoader");
1465  
1466   ftVertexer = new AliVertexerTracks(AliTracker::GetBz());
1467
1468   // get trackers
1469   if (!fRunTracking.IsNull() && !CreateTrackers(fRunTracking)) {
1470     Abort("CreateTrackers", TSelector::kAbortProcess);
1471     return;
1472   }      
1473   AliSysInfo::AddStamp("CreateTrackers");
1474
1475   // create the ESD output file and tree
1476   if (!outProofFile) {
1477     ffile = TFile::Open("AliESDs.root", "RECREATE");
1478     ffile->SetCompressionLevel(2);
1479     if (!ffile->IsOpen()) {
1480       Abort("OpenESDFile", TSelector::kAbortProcess);
1481       return;
1482     }
1483   }
1484   else {
1485     AliInfo(Form("Opening output PROOF file: %s/%s",
1486                  outProofFile->GetDir(), outProofFile->GetFileName()));
1487     if (!(ffile = outProofFile->OpenFile("RECREATE"))) {
1488       Abort(Form("Problems opening output PROOF file: %s/%s",
1489                  outProofFile->GetDir(), outProofFile->GetFileName()),
1490             TSelector::kAbortProcess);
1491       return;
1492     }
1493   }
1494
1495   ftree = new TTree("esdTree", "Tree with ESD objects");
1496   fesd = new AliESDEvent();
1497   fesd->CreateStdContent();
1498   if (fesd->GetESDRun()) ((AliESDRun*)fesd->GetESDRun())->SetBeamEnergyIsSqrtSHalfGeV();
1499   // add a so far non-std object to the ESD, this will
1500   // become part of the std content
1501   fesd->AddObject(new AliESDHLTDecision);
1502
1503   fesd->WriteToTree(ftree);
1504   if (fWriteESDfriend) {
1505     // careful:
1506     // Since we add the branch manually we must 
1507     // book and add it after WriteToTree
1508     // otherwise it is created twice,
1509     // once via writetotree and once here.
1510     // The case for AliESDfriend is now 
1511     // caught also in AlIESDEvent::WriteToTree but 
1512     // be careful when changing the name (AliESDfriend is not 
1513     // a TNamed so we had to hardwire it)
1514     fesdf = new AliESDfriend();
1515     TBranch *br=ftree->Branch("ESDfriend.","AliESDfriend", &fesdf);
1516     br->SetFile("AliESDfriends.root");
1517     fesd->AddObject(fesdf);
1518   }
1519   ftree->GetUserInfo()->Add(fesd);
1520
1521   fhlttree = new TTree("HLTesdTree", "Tree with HLT ESD objects");
1522   fhltesd = new AliESDEvent();
1523   fhltesd->CreateStdContent();
1524   if (fhltesd->GetESDRun()) ((AliESDRun*)fhltesd->GetESDRun())->SetBeamEnergyIsSqrtSHalfGeV();
1525   // read the ESD template from CDB
1526   // HLT is allowed to put non-std content to its ESD, the non-std
1527   // objects need to be created before invocation of WriteToTree in
1528   // order to create all branches. Initialization is done from an
1529   // ESD layout template in CDB
1530   AliCDBManager* man = AliCDBManager::Instance();
1531   AliCDBPath hltESDConfigPath("HLT/ConfigHLT/esdLayout");
1532   AliCDBEntry* hltESDConfig=NULL;
1533   if (man->GetId(hltESDConfigPath)!=NULL &&
1534       (hltESDConfig=man->Get(hltESDConfigPath))!=NULL) {
1535     AliESDEvent* pESDLayout=dynamic_cast<AliESDEvent*>(hltESDConfig->GetObject());
1536     if (pESDLayout) {
1537       // init all internal variables from the list of objects
1538       pESDLayout->GetStdContent();
1539
1540       // copy content and create non-std objects
1541       *fhltesd=*pESDLayout;
1542       fhltesd->Reset();
1543     } else {
1544       AliError(Form("error setting hltEsd layout from %s: invalid object type",
1545                     hltESDConfigPath.GetPath().Data()));
1546     }
1547   }
1548
1549   fhltesd->WriteToTree(fhlttree);
1550   fhlttree->GetUserInfo()->Add(fhltesd);
1551
1552   ProcInfo_t procInfo;
1553   gSystem->GetProcInfo(&procInfo);
1554   AliInfo(Form("Current memory usage %d %d", procInfo.fMemResident, procInfo.fMemVirtual));
1555   
1556   //QA
1557   //Initialize the QA and start of cycle 
1558   if (fRunQA || fRunGlobalQA) 
1559     InitQA() ; 
1560
1561   //Initialize the Plane Efficiency framework
1562   if (fRunPlaneEff && !InitPlaneEff()) {
1563     Abort("InitPlaneEff", TSelector::kAbortProcess);
1564     return;
1565   }
1566
1567   if (strcmp(gProgName,"alieve") == 0)
1568     fRunAliEVE = InitAliEVE();
1569
1570   return;
1571 }
1572
1573 //_____________________________________________________________________________
1574 Bool_t AliReconstruction::Process(Long64_t entry)
1575 {
1576   // run the reconstruction over a single entry
1577   // from the chain with raw data
1578   AliCodeTimerAuto("",0);
1579
1580   TTree *currTree = fChain->GetTree();
1581   AliRawVEvent *event = NULL;
1582   currTree->SetBranchAddress("rawevent",&event);
1583   currTree->GetEntry(entry);
1584   fRawReader = new AliRawReaderRoot(event);
1585   fStatus = ProcessEvent(fRunLoader->GetNumberOfEvents());  
1586   delete fRawReader;
1587   fRawReader = NULL;
1588   delete event;
1589
1590   return fStatus;
1591 }
1592
1593 //_____________________________________________________________________________
1594 void AliReconstruction::Init(TTree *tree)
1595 {
1596   if (tree == 0) {
1597     AliError("The input tree is not found!");
1598     return;
1599   }
1600   fChain = tree;
1601 }
1602
1603 //_____________________________________________________________________________
1604 Bool_t AliReconstruction::ProcessEvent(Int_t iEvent)
1605 {
1606   // run the reconstruction over a single event
1607   // The event loop is steered in Run method
1608
1609   AliCodeTimerAuto("",0);
1610
1611   if (iEvent >= fRunLoader->GetNumberOfEvents()) {
1612     fRunLoader->SetEventNumber(iEvent);
1613     fRunLoader->GetHeader()->Reset(fRawReader->GetRunNumber(), 
1614                                    iEvent, iEvent);
1615     fRunLoader->TreeE()->Fill();
1616     if (fRawReader && fRawReader->UseAutoSaveESD())
1617       fRunLoader->TreeE()->AutoSave("SaveSelf");
1618   }
1619
1620   if ((iEvent < fFirstEvent) || ((fLastEvent >= 0) && (iEvent > fLastEvent))) {
1621     return kTRUE;
1622   }
1623
1624   AliInfo(Form("processing event %d", iEvent));
1625
1626   fRunLoader->GetEvent(iEvent);
1627
1628   // Fill Event-info object
1629   GetEventInfo();
1630   fRecoParam.SetEventSpecie(fRunInfo,fEventInfo,fListOfCosmicTriggers);
1631   AliInfo(Form("Current event specie: %s",fRecoParam.PrintEventSpecie()));
1632
1633   // Set the reco-params
1634   {
1635     TString detStr = fLoadCDB;
1636     for (Int_t iDet = 0; iDet < kNDetectors; iDet++) {
1637       if (!IsSelected(fgkDetectorName[iDet], detStr)) continue;
1638       AliReconstructor *reconstructor = GetReconstructor(iDet);
1639       if (reconstructor && fRecoParam.GetDetRecoParamArray(iDet)) {
1640         const AliDetectorRecoParam *par = fRecoParam.GetDetRecoParam(iDet);
1641         reconstructor->SetRecoParam(par);
1642         reconstructor->SetEventInfo(&fEventInfo);
1643         if (fRunQA) {
1644           AliQAManager::QAManager()->SetRecoParam(iDet, par) ; 
1645           AliQAManager::QAManager()->SetEventSpecie(AliRecoParam::Convert(par->GetEventSpecie())) ;
1646         }
1647       }
1648     }
1649     const AliDetectorRecoParam *grppar = fRecoParam.GetDetRecoParam(kNDetectors);
1650     AliQAManager::QAManager()->SetRecoParam(AliQAv1::kGLOBAL, grppar) ; 
1651     AliQAManager::QAManager()->SetEventSpecie(AliRecoParam::Convert(grppar->GetEventSpecie())) ;
1652   }
1653
1654     // QA on single raw 
1655   if (fRunQA && IsInTasks(AliQAv1::kRAWS)) {
1656     AliQAManager::QAManager()->SetEventSpecie(fRecoParam.GetEventSpecie()) ;
1657     AliQAManager::QAManager()->RunOneEvent(fRawReader) ;  
1658   }
1659     // local single event reconstruction
1660     if (!fRunLocalReconstruction.IsNull()) {
1661       TString detectors=fRunLocalReconstruction;
1662       // run HLT event reconstruction first
1663       // ;-( IsSelected changes the string
1664       if (IsSelected("HLT", detectors) &&
1665           !RunLocalEventReconstruction("HLT")) {
1666         if (fStopOnError) {CleanUp(); return kFALSE;}
1667       }
1668       detectors=fRunLocalReconstruction;
1669       detectors.ReplaceAll("HLT", "");
1670       if (!RunLocalEventReconstruction(detectors)) {
1671         if (fStopOnError) {
1672           CleanUp(); 
1673           return kFALSE;
1674         }
1675       }
1676     }
1677
1678   
1679     // fill Event header information from the RawEventHeader
1680     if (fRawReader){FillRawEventHeaderESD(fesd);}
1681     if (fRawReader){FillRawEventHeaderESD(fhltesd);}
1682
1683     fesd->SetRunNumber(fRunLoader->GetHeader()->GetRun());
1684     fhltesd->SetRunNumber(fRunLoader->GetHeader()->GetRun());
1685     fesd->SetEventNumberInFile(fRunLoader->GetHeader()->GetEventNrInRun());
1686     fhltesd->SetEventNumberInFile(fRunLoader->GetHeader()->GetEventNrInRun());
1687
1688     fesd->SetEventSpecie(fRecoParam.GetEventSpecie());
1689     fhltesd->SetEventSpecie(fRecoParam.GetEventSpecie());
1690     
1691     // Set magnetic field from the tracker
1692     fesd->SetMagneticField(AliTracker::GetBz());
1693     fhltesd->SetMagneticField(AliTracker::GetBz());
1694     //
1695     AliMagF* fld = (AliMagF*)TGeoGlobalMagField::Instance()->GetField();
1696     if (fld) { // set info needed for field initialization
1697       fesd->SetCurrentL3(fld->GetCurrentSol());
1698       fesd->SetCurrentDip(fld->GetCurrentDip());
1699       fesd->SetBeamEnergy(fld->GetBeamEnergy());
1700       fesd->SetBeamType(fld->GetBeamTypeText());
1701       fesd->SetUniformBMap(fld->IsUniform());
1702       fesd->SetBInfoStored();
1703       //
1704       fhltesd->SetCurrentL3(fld->GetCurrentSol());
1705       fhltesd->SetCurrentDip(fld->GetCurrentDip());
1706       fhltesd->SetBeamEnergy(fld->GetBeamEnergy());
1707       fhltesd->SetBeamType(fld->GetBeamTypeText());
1708       fhltesd->SetUniformBMap(fld->IsUniform());
1709       fhltesd->SetBInfoStored();
1710     }
1711     //
1712     // Set most probable pt, for B=0 tracking
1713     // Get the global reco-params. They are atposition 16 inside the array of detectors in fRecoParam
1714     const AliGRPRecoParam *grpRecoParam = dynamic_cast<const AliGRPRecoParam*>(fRecoParam.GetDetRecoParam(kNDetectors));
1715     if (grpRecoParam) AliExternalTrackParam::SetMostProbablePt(grpRecoParam->GetMostProbablePt());
1716     
1717     // Fill raw-data error log into the ESD
1718     if (fRawReader) FillRawDataErrorLog(iEvent,fesd);
1719
1720     // vertex finder
1721     if (fRunVertexFinder) {
1722       if (!RunVertexFinder(fesd)) {
1723         if (fStopOnError) {CleanUp(); return kFALSE;}
1724       }
1725     }
1726
1727     // For Plane Efficiency: run the SPD trackleter
1728     if (fRunPlaneEff && fSPDTrackleter) {
1729       if (!RunSPDTrackleting(fesd)) {
1730         if (fStopOnError) {CleanUp(); return kFALSE;}
1731       }
1732     }
1733
1734     // Muon tracking
1735     if (!fRunTracking.IsNull()) {
1736       if (fRunMuonTracking) {
1737         if (!RunMuonTracking(fesd)) {
1738           if (fStopOnError) {CleanUp(); return kFALSE;}
1739         }
1740       }
1741     }
1742
1743     // barrel tracking
1744     if (!fRunTracking.IsNull()) {
1745       if (!RunTracking(fesd)) {
1746         if (fStopOnError) {CleanUp(); return kFALSE;}
1747       }
1748     }
1749
1750     // fill ESD
1751     if (!fFillESD.IsNull()) {
1752       TString detectors=fFillESD;
1753       // run HLT first and on hltesd
1754       // ;-( IsSelected changes the string
1755       if (IsSelected("HLT", detectors) &&
1756           !FillESD(fhltesd, "HLT")) {
1757         if (fStopOnError) {CleanUp(); return kFALSE;}
1758       }
1759       detectors=fFillESD;
1760       // Temporary fix to avoid problems with HLT that overwrites the offline ESDs
1761       if (detectors.Contains("ALL")) {
1762         detectors="";
1763         for (Int_t idet=0; idet<kNDetectors; ++idet){
1764           detectors += fgkDetectorName[idet];
1765           detectors += " ";
1766         }
1767       }
1768       detectors.ReplaceAll("HLT", "");
1769       if (!FillESD(fesd, detectors)) {
1770         if (fStopOnError) {CleanUp(); return kFALSE;}
1771       }
1772     }
1773  
1774     // combined PID
1775     AliESDpid::MakePID(fesd);
1776
1777     if (fFillTriggerESD) {
1778       if (!FillTriggerESD(fesd)) {
1779         if (fStopOnError) {CleanUp(); return kFALSE;}
1780       }
1781     }
1782     // Always fill scalers
1783     if (!FillTriggerScalers(fesd)) {
1784        if (fStopOnError) {CleanUp(); return kFALSE;}
1785     }
1786     
1787
1788     ffile->cd();
1789
1790     //
1791     // Propagate track to the beam pipe  (if not already done by ITS)
1792     //
1793     const Int_t ntracks = fesd->GetNumberOfTracks();
1794     const Double_t kRadius  = 2.8; //something less than the beam pipe radius
1795
1796     TObjArray trkArray;
1797     UShort_t *selectedIdx=new UShort_t[ntracks];
1798
1799     for (Int_t itrack=0; itrack<ntracks; itrack++){
1800       const Double_t kMaxStep = 1;   //max step over the material
1801       Bool_t ok;
1802
1803       AliESDtrack *track = fesd->GetTrack(itrack);
1804       if (!track) continue;
1805
1806       AliExternalTrackParam *tpcTrack =
1807            (AliExternalTrackParam *)track->GetTPCInnerParam();
1808       ok = kFALSE;
1809       if (tpcTrack)
1810         ok = AliTracker::
1811           PropagateTrackToBxByBz(tpcTrack,kRadius,track->GetMass(),kMaxStep,kFALSE);
1812
1813       if (ok) {
1814         Int_t n=trkArray.GetEntriesFast();
1815         selectedIdx[n]=track->GetID();
1816         trkArray.AddLast(tpcTrack);
1817       }
1818
1819       //Tracks refitted by ITS should already be at the SPD vertex
1820       if (track->IsOn(AliESDtrack::kITSrefit)) continue;
1821
1822       AliTracker::
1823          PropagateTrackToBxByBz(track,kRadius,track->GetMass(),kMaxStep,kFALSE);
1824       Double_t x[3]; track->GetXYZ(x);
1825       Double_t b[3]; AliTracker::GetBxByBz(x,b);
1826       track->RelateToVertexBxByBz(fesd->GetPrimaryVertexSPD(), b, kVeryBig);
1827
1828     }
1829
1830     //
1831     // Improve the reconstructed primary vertex position using the tracks
1832     //
1833     Bool_t runVertexFinderTracks = fRunVertexFinderTracks;
1834     if(fesd->GetPrimaryVertexSPD()) {
1835       TString vtitle = fesd->GetPrimaryVertexSPD()->GetTitle();
1836       if(vtitle.Contains("cosmics")) {
1837         runVertexFinderTracks=kFALSE;
1838       }
1839     }
1840
1841     if (runVertexFinderTracks) {
1842        // TPC + ITS primary vertex
1843        ftVertexer->SetITSMode();
1844        ftVertexer->SetConstraintOff();
1845        // get cuts for vertexer from AliGRPRecoParam
1846        if (grpRecoParam) {
1847          Int_t nCutsVertexer = grpRecoParam->GetVertexerTracksNCuts();
1848          Double_t *cutsVertexer = new Double_t[nCutsVertexer];
1849          grpRecoParam->GetVertexerTracksCutsITS(cutsVertexer);
1850          ftVertexer->SetCuts(cutsVertexer);
1851          delete [] cutsVertexer; cutsVertexer = NULL; 
1852          if(fDiamondProfile && grpRecoParam->GetVertexerTracksConstraintITS())
1853            ftVertexer->SetVtxStart(fDiamondProfile);
1854        }
1855        AliESDVertex *pvtx=ftVertexer->FindPrimaryVertex(fesd);
1856        if (pvtx) {
1857           if (pvtx->GetStatus()) {
1858              fesd->SetPrimaryVertexTracks(pvtx);
1859              for (Int_t i=0; i<ntracks; i++) {
1860                  AliESDtrack *t = fesd->GetTrack(i);
1861                  Double_t x[3]; t->GetXYZ(x);
1862                  Double_t b[3]; AliTracker::GetBxByBz(x,b);
1863                  t->RelateToVertexBxByBz(pvtx, b, kVeryBig);
1864              } 
1865           }
1866           delete pvtx; pvtx=NULL;
1867        }
1868
1869        // TPC-only primary vertex
1870        ftVertexer->SetTPCMode();
1871        ftVertexer->SetConstraintOff();
1872        // get cuts for vertexer from AliGRPRecoParam
1873        if (grpRecoParam) {
1874          Int_t nCutsVertexer = grpRecoParam->GetVertexerTracksNCuts();
1875          Double_t *cutsVertexer = new Double_t[nCutsVertexer];
1876          grpRecoParam->GetVertexerTracksCutsTPC(cutsVertexer);
1877          ftVertexer->SetCuts(cutsVertexer);
1878          delete [] cutsVertexer; cutsVertexer = NULL; 
1879          if(fDiamondProfileTPC && grpRecoParam->GetVertexerTracksConstraintTPC())
1880            ftVertexer->SetVtxStart(fDiamondProfileTPC);
1881        }
1882        pvtx=ftVertexer->FindPrimaryVertex(&trkArray,selectedIdx);
1883        if (pvtx) {
1884           if (pvtx->GetStatus()) {
1885              fesd->SetPrimaryVertexTPC(pvtx);
1886              for (Int_t i=0; i<ntracks; i++) {
1887                  AliESDtrack *t = fesd->GetTrack(i);
1888                  Double_t x[3]; t->GetXYZ(x);
1889                  Double_t b[3]; AliTracker::GetBxByBz(x,b);
1890                  t->RelateToVertexTPCBxByBz(pvtx, b, kVeryBig);
1891              } 
1892           }
1893           delete pvtx; pvtx=NULL;
1894        }
1895
1896     }
1897     delete[] selectedIdx;
1898
1899     if(fDiamondProfile) fesd->SetDiamond(fDiamondProfile);
1900     
1901
1902     if (fRunV0Finder) {
1903        // V0 finding
1904        AliV0vertexer vtxer;
1905        vtxer.Tracks2V0vertices(fesd);
1906
1907        if (fRunCascadeFinder) {
1908           // Cascade finding
1909           AliCascadeVertexer cvtxer;
1910           cvtxer.V0sTracks2CascadeVertices(fesd);
1911        }
1912     }
1913  
1914     // write ESD
1915     if (fCleanESD) CleanESD(fesd);
1916
1917   if (fRunQA && IsInTasks(AliQAv1::kESDS)) {
1918     AliQAManager::QAManager()->SetEventSpecie(fRecoParam.GetEventSpecie()) ;
1919     AliQAManager::QAManager()->RunOneEvent(fesd, fhltesd) ; 
1920   }
1921   if (fRunGlobalQA) {
1922     AliQADataMaker *qadm = AliQAManager::QAManager()->GetQADataMaker(AliQAv1::kGLOBAL);
1923       qadm->SetEventSpecie(fRecoParam.GetEventSpecie()) ;
1924     if (qadm && IsInTasks(AliQAv1::kESDS))
1925       qadm->Exec(AliQAv1::kESDS, fesd);
1926   }
1927
1928   // copy HLT decision from HLTesd to esd
1929   // the most relevant information is stored in a reduced container in the esd,
1930   // while the full information can be found in the HLTesd
1931   TObject* pHLTSrc=fhltesd->FindListObject(AliESDHLTDecision::Name());
1932   TObject* pHLTTgt=fesd->FindListObject(AliESDHLTDecision::Name());
1933   if (pHLTSrc && pHLTTgt) {
1934     pHLTSrc->Copy(*pHLTTgt);
1935   }
1936
1937     if (fWriteESDfriend) {
1938       //      fesdf->~AliESDfriend();
1939       //  new (fesdf) AliESDfriend(); // Reset...
1940       fesd->GetESDfriend(fesdf);
1941     }
1942     ftree->Fill();
1943
1944     // Auto-save the ESD tree in case of prompt reco @P2
1945     if (fRawReader && fRawReader->UseAutoSaveESD()) {
1946       ftree->AutoSave("SaveSelf");
1947       TFile *friendfile = (TFile *)(gROOT->GetListOfFiles()->FindObject("AliESDfriends.root"));
1948       if (friendfile) friendfile->Save();
1949     }
1950
1951     // write HLT ESD
1952     fhlttree->Fill();
1953
1954     // call AliEVE
1955     if (fRunAliEVE) RunAliEVE();
1956
1957     fesd->Reset();
1958     fhltesd->Reset();
1959     if (fWriteESDfriend) {
1960       fesdf->~AliESDfriend();
1961       new (fesdf) AliESDfriend(); // Reset...
1962     }
1963  
1964     ProcInfo_t procInfo;
1965     gSystem->GetProcInfo(&procInfo);
1966     AliInfo(Form("Event %d -> Current memory usage %d %d",iEvent, procInfo.fMemResident, procInfo.fMemVirtual));
1967   
1968     fEventInfo.Reset();
1969     for (Int_t iDet = 0; iDet < kNDetectors; iDet++) {
1970       if (fReconstructor[iDet]) {
1971         fReconstructor[iDet]->SetRecoParam(NULL);
1972         fReconstructor[iDet]->SetEventInfo(NULL);
1973       }
1974       if (fTracker[iDet]) fTracker[iDet]->SetEventInfo(NULL);
1975     }
1976         
1977   if (fRunQA || fRunGlobalQA) 
1978     AliQAManager::QAManager()->Increment() ; 
1979   
1980     return kTRUE;
1981 }
1982
1983 //_____________________________________________________________________________
1984 void AliReconstruction::SlaveTerminate()
1985 {
1986   // Finalize the run on the slave side
1987   // Called after the exit
1988   // from the event loop
1989   AliCodeTimerAuto("",0);
1990
1991   if (fIsNewRunLoader) { // galice.root didn't exist
1992     fRunLoader->WriteHeader("OVERWRITE");
1993     fRunLoader->CdGAFile();
1994     fRunLoader->Write(0, TObject::kOverwrite);
1995   }
1996
1997   const TMap *cdbMap = AliCDBManager::Instance()->GetStorageMap();       
1998   const TList *cdbList = AliCDBManager::Instance()->GetRetrievedIds();   
1999                  
2000    TMap *cdbMapCopy = new TMap(cdbMap->GetEntries());    
2001    cdbMapCopy->SetOwner(1);      
2002    cdbMapCopy->SetName("cdbMap");        
2003    TIter iter(cdbMap->GetTable());       
2004          
2005    TPair* pair = 0;      
2006    while((pair = dynamic_cast<TPair*> (iter.Next()))){   
2007          TObjString* keyStr = dynamic_cast<TObjString*> (pair->Key());   
2008          TObjString* valStr = dynamic_cast<TObjString*> (pair->Value());         
2009          cdbMapCopy->Add(new TObjString(keyStr->GetName()), new TObjString(valStr->GetName()));  
2010    }     
2011          
2012    TList *cdbListCopy = new TList();     
2013    cdbListCopy->SetOwner(1);     
2014    cdbListCopy->SetName("cdbList");      
2015          
2016    TIter iter2(cdbList);         
2017          
2018         AliCDBId* id=0;
2019         while((id = dynamic_cast<AliCDBId*> (iter2.Next()))){    
2020          cdbListCopy->Add(new TObjString(id->ToString().Data()));        
2021    }     
2022          
2023    ftree->GetUserInfo()->Add(cdbMapCopy);        
2024    ftree->GetUserInfo()->Add(cdbListCopy);
2025
2026
2027   ffile->cd();
2028
2029   if (fWriteESDfriend)
2030     ftree->SetBranchStatus("ESDfriend*",0);
2031   // we want to have only one tree version number
2032   ftree->Write(ftree->GetName(),TObject::kOverwrite);
2033   fhlttree->Write(fhlttree->GetName(),TObject::kOverwrite);
2034
2035 // Finish with Plane Efficiency evaluation: before of CleanUp !!!
2036   if (fRunPlaneEff && !FinishPlaneEff()) {
2037    AliWarning("Finish PlaneEff evaluation failed");
2038   }
2039
2040   // End of cycle for the in-loop  
2041
2042   if (fRunQA || fRunGlobalQA) {
2043     AliQAManager::QAManager()->EndOfCycle() ;
2044     if (fInput &&
2045         !fProofOutputLocation.IsNull() &&
2046         fProofOutputArchive.IsNull() &&
2047         !fProofOutputDataset) {
2048       TString qaOutputFile(Form("%sMerged.%s.Data.root",
2049                                 fProofOutputLocation.Data(),
2050                                 AliQAv1::GetQADataFileName()));
2051       TProofOutputFile *qaProofFile = new TProofOutputFile(Form("Merged.%s.Data.root",
2052                                                                 AliQAv1::GetQADataFileName()));
2053       qaProofFile->SetOutputFileName(qaOutputFile.Data());
2054       if (AliDebugLevel() > 0) qaProofFile->Dump();
2055       fOutput->Add(qaProofFile);
2056       MergeQA(qaProofFile->GetFileName());
2057     }
2058     else {
2059       MergeQA();
2060     }
2061   }
2062
2063   gROOT->cd();
2064   CleanUp();
2065
2066   if (fInput) {
2067     if (!fProofOutputFileName.IsNull() &&
2068         !fProofOutputLocation.IsNull() &&
2069         fProofOutputDataset &&
2070         !fProofOutputArchive.IsNull()) {
2071       TProofOutputFile *zipProofFile = new TProofOutputFile(fProofOutputFileName.Data(),
2072                                                             "DROV",
2073                                                             fProofOutputLocation.Data());
2074       if (AliDebugLevel() > 0) zipProofFile->Dump();
2075       fOutput->Add(zipProofFile);
2076       TString fileList(fProofOutputArchive.Data());
2077       fileList.ReplaceAll(","," ");
2078       TString command;
2079 #if ROOT_SVN_REVISION >= 30174
2080       command.Form("zip -n root %s/%s %s",zipProofFile->GetDir(kTRUE),zipProofFile->GetFileName(),fileList.Data());
2081 #else
2082       command.Form("zip -n root %s/%s %s",zipProofFile->GetDir(),zipProofFile->GetFileName(),fileList.Data());
2083 #endif
2084       AliInfo(Form("Executing: %s",command.Data()));
2085       gSystem->Exec(command.Data());
2086     }
2087   }
2088 }
2089     
2090 //_____________________________________________________________________________
2091 void AliReconstruction::Terminate()
2092 {
2093   // Create tags for the events in the ESD tree (the ESD tree is always present)
2094   // In case of empty events the tags will contain dummy values
2095   AliCodeTimerAuto("",0);
2096
2097   // Do not call the ESD tag creator in case of PROOF-based reconstruction
2098   if (!fInput) {
2099     AliESDTagCreator *esdtagCreator = new AliESDTagCreator();
2100     esdtagCreator->CreateESDTags(fFirstEvent,fLastEvent,fGRPData, AliQAv1::Instance()->GetQA(), AliQAv1::Instance()->GetEventSpecies(), AliQAv1::kNDET, AliRecoParam::kNSpecies);
2101     delete esdtagCreator;
2102   }
2103
2104   // Cleanup of CDB manager: cache and active storages!
2105   AliCDBManager::Instance()->ClearCache();
2106 }
2107
2108 //_____________________________________________________________________________
2109 Bool_t AliReconstruction::RunLocalEventReconstruction(const TString& detectors)
2110 {
2111 // run the local reconstruction
2112
2113   static Int_t eventNr=0;
2114   AliCodeTimerAuto("",0)
2115
2116   TString detStr = detectors;
2117   for (Int_t iDet = 0; iDet < kNDetectors; iDet++) {
2118     if (!IsSelected(fgkDetectorName[iDet], detStr)) continue;
2119     AliReconstructor* reconstructor = GetReconstructor(iDet);
2120     if (!reconstructor) continue;
2121     AliLoader* loader = fLoader[iDet];
2122     // Matthias April 2008: temporary fix to run HLT reconstruction
2123     // although the HLT loader is missing
2124     if (strcmp(fgkDetectorName[iDet], "HLT")==0) {
2125       if (fRawReader) {
2126         reconstructor->Reconstruct(fRawReader, NULL);
2127       } else {
2128         TTree* dummy=NULL;
2129         reconstructor->Reconstruct(dummy, NULL);
2130       }
2131       continue;
2132     }
2133     if (!loader) {
2134       AliWarning(Form("No loader is defined for %s!",fgkDetectorName[iDet]));
2135       continue;
2136     }
2137     // conversion of digits
2138     if (fRawReader && reconstructor->HasDigitConversion()) {
2139       AliInfo(Form("converting raw data digits into root objects for %s", 
2140                    fgkDetectorName[iDet]));
2141 //      AliCodeTimerAuto(Form("converting raw data digits into root objects for %s", 
2142 //                            fgkDetectorName[iDet]),0);
2143       loader->LoadDigits("update");
2144       loader->CleanDigits();
2145       loader->MakeDigitsContainer();
2146       TTree* digitsTree = loader->TreeD();
2147       reconstructor->ConvertDigits(fRawReader, digitsTree);
2148       loader->WriteDigits("OVERWRITE");
2149       loader->UnloadDigits();
2150     }
2151     // local reconstruction
2152     AliInfo(Form("running reconstruction for %s", fgkDetectorName[iDet]));
2153     //AliCodeTimerAuto(Form("running reconstruction for %s", fgkDetectorName[iDet]),0);
2154     loader->LoadRecPoints("update");
2155     loader->CleanRecPoints();
2156     loader->MakeRecPointsContainer();
2157     TTree* clustersTree = loader->TreeR();
2158     if (fRawReader && !reconstructor->HasDigitConversion()) {
2159       reconstructor->Reconstruct(fRawReader, clustersTree);
2160     } else {
2161       loader->LoadDigits("read");
2162       TTree* digitsTree = loader->TreeD();
2163       if (!digitsTree) {
2164         AliError(Form("Can't get the %s digits tree", fgkDetectorName[iDet]));
2165         if (fStopOnError) 
2166           return kFALSE;
2167       } else {
2168         reconstructor->Reconstruct(digitsTree, clustersTree);
2169         if (fRunQA && IsInTasks(AliQAv1::kDIGITSR)) {
2170           AliQAManager::QAManager()->SetEventSpecie(fRecoParam.GetEventSpecie()) ;
2171           AliQAManager::QAManager()->RunOneEventInOneDetector(iDet, digitsTree) ; 
2172         }
2173       }
2174       loader->UnloadDigits();
2175     }
2176                 if (fRunQA && IsInTasks(AliQAv1::kRECPOINTS)) {
2177       AliQAManager::QAManager()->SetEventSpecie(fRecoParam.GetEventSpecie()) ;
2178                         AliQAManager::QAManager()->RunOneEventInOneDetector(iDet, clustersTree) ; 
2179     }
2180     loader->WriteRecPoints("OVERWRITE");
2181     loader->UnloadRecPoints();
2182     AliSysInfo::AddStamp(Form("LRec%s_%d",fgkDetectorName[iDet],eventNr), iDet,1,eventNr);
2183   }
2184   IsSelected("CTP", detStr);
2185   if ((detStr.CompareTo("ALL") != 0) && !detStr.IsNull()) {
2186     AliError(Form("the following detectors were not found: %s",
2187                   detStr.Data()));
2188     if (fStopOnError) 
2189       return kFALSE;
2190   }
2191   eventNr++;
2192   return kTRUE;
2193 }
2194 //_____________________________________________________________________________
2195 Bool_t AliReconstruction::RunSPDTrackleting(AliESDEvent*& esd)
2196 {
2197 // run the SPD trackleting (for SPD efficiency purpouses)
2198
2199   AliCodeTimerAuto("",0)
2200
2201   Double_t vtxPos[3] = {0, 0, 0};
2202   Double_t vtxErr[3] = {0.0, 0.0, 0.0};
2203 /*
2204   TArrayF mcVertex(3);
2205   // if(MC)
2206   if (fRunLoader->GetHeader() && fRunLoader->GetHeader()->GenEventHeader()) {
2207     fRunLoader->GetHeader()->GenEventHeader()->PrimaryVertex(mcVertex);
2208     for (Int_t i = 0; i < 3; i++) vtxPos[i] = mcVertex[i];
2209   }
2210 */
2211   const AliESDVertex *vertex = esd->GetVertex();
2212   if(!vertex){
2213     AliWarning("Vertex not found");
2214     return kFALSE;
2215   }
2216   vertex->GetXYZ(vtxPos);
2217   vertex->GetSigmaXYZ(vtxErr);
2218   if (fSPDTrackleter) {
2219     AliInfo("running the SPD Trackleter for Plane Efficiency Evaluation");
2220
2221     // load clusters
2222     fLoader[0]->LoadRecPoints("read");
2223     TTree* tree = fLoader[0]->TreeR();
2224     if (!tree) {
2225       AliError("Can't get the ITS cluster tree");
2226       return kFALSE;
2227     }
2228     fSPDTrackleter->LoadClusters(tree);
2229     fSPDTrackleter->SetVertex(vtxPos, vtxErr);
2230     // run trackleting
2231     if (fSPDTrackleter->Clusters2Tracks(esd) != 0) {
2232       AliError("AliITSTrackleterSPDEff Clusters2Tracks failed");
2233      // fLoader[0]->UnloadRecPoints();
2234       return kFALSE;
2235     }
2236 //fSPDTrackleter->UnloadRecPoints();
2237   } else {
2238     AliWarning("SPDTrackleter not available");
2239     return kFALSE;
2240   }
2241   return kTRUE;
2242 }
2243
2244 //_____________________________________________________________________________
2245 Bool_t AliReconstruction::RunVertexFinder(AliESDEvent*& esd)
2246 {
2247 // run the barrel tracking
2248
2249   AliCodeTimerAuto("",0)
2250
2251   AliVertexer *vertexer = CreateVertexer();
2252   if (!vertexer) return kFALSE;
2253
2254   AliInfo("running the ITS vertex finder");
2255   AliESDVertex* vertex = NULL;
2256   if (fLoader[0]) {
2257     fLoader[0]->LoadRecPoints();
2258     TTree* cltree = fLoader[0]->TreeR();
2259     if (cltree) {
2260       if(fDiamondProfileSPD) vertexer->SetVtxStart(fDiamondProfileSPD);
2261       vertex = vertexer->FindVertexForCurrentEvent(cltree);
2262     }
2263     else {
2264       AliError("Can't get the ITS cluster tree");
2265     }
2266     fLoader[0]->UnloadRecPoints();
2267   }
2268   else {
2269     AliError("Can't get the ITS loader");
2270   }
2271   if(!vertex){
2272     AliWarning("Vertex not found");
2273     vertex = new AliESDVertex();
2274     vertex->SetName("default");
2275   }
2276   else {
2277     vertex->SetName("reconstructed");
2278   }
2279
2280   Double_t vtxPos[3];
2281   Double_t vtxErr[3];
2282   vertex->GetXYZ(vtxPos);
2283   vertex->GetSigmaXYZ(vtxErr);
2284
2285   esd->SetPrimaryVertexSPD(vertex);
2286   AliESDVertex *vpileup = NULL;
2287   Int_t novertices = 0;
2288   vpileup = vertexer->GetAllVertices(novertices);
2289   if(novertices>1){
2290     for (Int_t kk=1; kk<novertices; kk++)esd->AddPileupVertexSPD(&vpileup[kk]);
2291   }
2292   // if SPD multiplicity has been determined, it is stored in the ESD
2293   AliMultiplicity *mult = vertexer->GetMultiplicity();
2294   if(mult)esd->SetMultiplicity(mult);
2295
2296   for (Int_t iDet = 0; iDet < kNDetectors; iDet++) {
2297     if (fTracker[iDet]) fTracker[iDet]->SetVertex(vtxPos, vtxErr);
2298   }  
2299   delete vertex;
2300
2301   delete vertexer;
2302
2303   return kTRUE;
2304 }
2305
2306 //_____________________________________________________________________________
2307 Bool_t AliReconstruction::RunHLTTracking(AliESDEvent*& esd)
2308 {
2309 // run the HLT barrel tracking
2310
2311   AliCodeTimerAuto("",0)
2312
2313   if (!fRunLoader) {
2314     AliError("Missing runLoader!");
2315     return kFALSE;
2316   }
2317
2318   AliInfo("running HLT tracking");
2319
2320   // Get a pointer to the HLT reconstructor
2321   AliReconstructor *reconstructor = GetReconstructor(kNDetectors-1);
2322   if (!reconstructor) return kFALSE;
2323
2324   // TPC + ITS
2325   for (Int_t iDet = 1; iDet >= 0; iDet--) {
2326     TString detName = fgkDetectorName[iDet];
2327     AliDebug(1, Form("%s HLT tracking", detName.Data()));
2328     reconstructor->SetOption(detName.Data());
2329     AliTracker *tracker = reconstructor->CreateTracker();
2330     if (!tracker) {
2331       AliWarning(Form("couldn't create a HLT tracker for %s", detName.Data()));
2332       if (fStopOnError) return kFALSE;
2333       continue;
2334     }
2335     Double_t vtxPos[3];
2336     Double_t vtxErr[3]={0.005,0.005,0.010};
2337     const AliESDVertex *vertex = esd->GetVertex();
2338     vertex->GetXYZ(vtxPos);
2339     tracker->SetVertex(vtxPos,vtxErr);
2340     if(iDet != 1) {
2341       fLoader[iDet]->LoadRecPoints("read");
2342       TTree* tree = fLoader[iDet]->TreeR();
2343       if (!tree) {
2344         AliError(Form("Can't get the %s cluster tree", detName.Data()));
2345         return kFALSE;
2346       }
2347       tracker->LoadClusters(tree);
2348     }
2349     if (tracker->Clusters2Tracks(esd) != 0) {
2350       AliError(Form("HLT %s Clusters2Tracks failed", fgkDetectorName[iDet]));
2351       return kFALSE;
2352     }
2353     if(iDet != 1) {
2354       tracker->UnloadClusters();
2355     }
2356     delete tracker;
2357   }
2358
2359   return kTRUE;
2360 }
2361
2362 //_____________________________________________________________________________
2363 Bool_t AliReconstruction::RunMuonTracking(AliESDEvent*& esd)
2364 {
2365 // run the muon spectrometer tracking
2366
2367   AliCodeTimerAuto("",0)
2368
2369   if (!fRunLoader) {
2370     AliError("Missing runLoader!");
2371     return kFALSE;
2372   }
2373   Int_t iDet = 7; // for MUON
2374
2375   AliInfo("is running...");
2376
2377   // Get a pointer to the MUON reconstructor
2378   AliReconstructor *reconstructor = GetReconstructor(iDet);
2379   if (!reconstructor) return kFALSE;
2380
2381   
2382   TString detName = fgkDetectorName[iDet];
2383   AliDebug(1, Form("%s tracking", detName.Data()));
2384   AliTracker *tracker =  reconstructor->CreateTracker();
2385   if (!tracker) {
2386     AliWarning(Form("couldn't create a tracker for %s", detName.Data()));
2387     return kFALSE;
2388   }
2389      
2390   // read RecPoints
2391   fLoader[iDet]->LoadRecPoints("read");  
2392
2393   tracker->LoadClusters(fLoader[iDet]->TreeR());
2394   
2395   Int_t rv = tracker->Clusters2Tracks(esd);
2396   
2397   fLoader[iDet]->UnloadRecPoints();
2398
2399   tracker->UnloadClusters();
2400   
2401   delete tracker;
2402   
2403   if ( rv )
2404   {
2405     AliError(Form("%s Clusters2Tracks failed", fgkDetectorName[iDet]));
2406     return kFALSE;
2407   }
2408   
2409   return kTRUE;
2410 }
2411
2412
2413 //_____________________________________________________________________________
2414 Bool_t AliReconstruction::RunTracking(AliESDEvent*& esd)
2415 {
2416 // run the barrel tracking
2417   static Int_t eventNr=0;
2418   AliCodeTimerAuto("",0)
2419
2420   AliInfo("running tracking");
2421
2422   // Set the event info which is used
2423   // by the trackers in order to obtain
2424   // information about read-out detectors,
2425   // trigger etc.
2426   AliDebug(1, "Setting event info");
2427   for (Int_t iDet = 0; iDet < kNDetectors; iDet++) {
2428     if (!fTracker[iDet]) continue;
2429     fTracker[iDet]->SetEventInfo(&fEventInfo);
2430   }
2431
2432   //Fill the ESD with the T0 info (will be used by the TOF) 
2433   if (fReconstructor[11] && fLoader[11]) {
2434     fLoader[11]->LoadRecPoints("READ");
2435     TTree *treeR = fLoader[11]->TreeR();
2436     if (treeR) {
2437       GetReconstructor(11)->FillESD((TTree *)NULL,treeR,esd);
2438     }
2439   }
2440
2441   // pass 1: TPC + ITS inwards
2442   for (Int_t iDet = 1; iDet >= 0; iDet--) {
2443     if (!fTracker[iDet]) continue;
2444     AliDebug(1, Form("%s tracking", fgkDetectorName[iDet]));
2445
2446     // load clusters
2447     fLoader[iDet]->LoadRecPoints("read");
2448     AliSysInfo::AddStamp(Form("RLoadCluster%s_%d",fgkDetectorName[iDet],eventNr),iDet,1, eventNr);
2449     TTree* tree = fLoader[iDet]->TreeR();
2450     if (!tree) {
2451       AliError(Form("Can't get the %s cluster tree", fgkDetectorName[iDet]));
2452       return kFALSE;
2453     }
2454     fTracker[iDet]->LoadClusters(tree);
2455     AliSysInfo::AddStamp(Form("TLoadCluster%s_%d",fgkDetectorName[iDet],eventNr), iDet,2, eventNr);
2456     // run tracking
2457     if (fTracker[iDet]->Clusters2Tracks(esd) != 0) {
2458       AliError(Form("%s Clusters2Tracks failed", fgkDetectorName[iDet]));
2459       return kFALSE;
2460     }
2461     // preliminary PID in TPC needed by the ITS tracker
2462     if (iDet == 1) {
2463       GetReconstructor(1)->FillESD((TTree*)NULL, (TTree*)NULL, esd);
2464       AliESDpid::MakePID(esd);
2465     } 
2466     AliSysInfo::AddStamp(Form("Tracking0%s_%d",fgkDetectorName[iDet],eventNr), iDet,3,eventNr);
2467   }
2468
2469   // pass 2: ALL backwards
2470
2471   for (Int_t iDet = 0; iDet < kNDetectors; iDet++) {
2472     if (!fTracker[iDet]) continue;
2473     AliDebug(1, Form("%s back propagation", fgkDetectorName[iDet]));
2474
2475     // load clusters
2476     if (iDet > 1) {     // all except ITS, TPC
2477       TTree* tree = NULL;
2478       fLoader[iDet]->LoadRecPoints("read");
2479       AliSysInfo::AddStamp(Form("RLoadCluster0%s_%d",fgkDetectorName[iDet],eventNr), iDet,1, eventNr);
2480       tree = fLoader[iDet]->TreeR();
2481       if (!tree) {
2482         AliError(Form("Can't get the %s cluster tree", fgkDetectorName[iDet]));
2483         return kFALSE;
2484       }
2485       fTracker[iDet]->LoadClusters(tree); 
2486       AliSysInfo::AddStamp(Form("TLoadCluster0%s_%d",fgkDetectorName[iDet],eventNr), iDet,2, eventNr);
2487     }
2488
2489     // run tracking
2490     if (iDet>1) // start filling residuals for the "outer" detectors
2491       if (fRunGlobalQA) {
2492         AliTracker::SetFillResiduals(fRecoParam.GetEventSpecie(), kTRUE);     
2493         TObjArray ** arr = AliTracker::GetResidualsArray() ; 
2494         if (arr) {
2495           AliRecoParam::EventSpecie_t es=fRecoParam.GetEventSpecie();
2496           TObjArray * elem = arr[AliRecoParam::AConvert(es)];
2497           if ( elem && (! elem->At(0)) ) {
2498             AliQADataMaker *qadm = AliQAManager::QAManager()->GetQADataMaker(AliQAv1::kGLOBAL);
2499             if (qadm) qadm->InitRecPointsForTracker() ; 
2500           }
2501         }
2502       }
2503     if (fTracker[iDet]->PropagateBack(esd) != 0) {
2504       AliError(Form("%s backward propagation failed", fgkDetectorName[iDet]));
2505       //      return kFALSE;
2506     }
2507
2508     // unload clusters
2509     if (iDet > 3) {     // all except ITS, TPC, TRD and TOF
2510       fTracker[iDet]->UnloadClusters();
2511       fLoader[iDet]->UnloadRecPoints();
2512     }
2513     // updated PID in TPC needed by the ITS tracker -MI
2514     if (iDet == 1) {
2515       GetReconstructor(1)->FillESD((TTree*)NULL, (TTree*)NULL, esd);
2516       AliESDpid::MakePID(esd);
2517     }
2518     AliSysInfo::AddStamp(Form("Tracking1%s_%d",fgkDetectorName[iDet],eventNr), iDet,3, eventNr);
2519   }
2520   //stop filling residuals for the "outer" detectors
2521   if (fRunGlobalQA) AliTracker::SetFillResiduals(fRecoParam.GetEventSpecie(), kFALSE);     
2522
2523   // pass 3: TRD + TPC + ITS refit inwards
2524
2525   for (Int_t iDet = 2; iDet >= 0; iDet--) {
2526     if (!fTracker[iDet]) continue;
2527     AliDebug(1, Form("%s inward refit", fgkDetectorName[iDet]));
2528
2529     // run tracking
2530     if (iDet<2) // start filling residuals for TPC and ITS
2531       if (fRunGlobalQA) {
2532         AliTracker::SetFillResiduals(fRecoParam.GetEventSpecie(), kTRUE);     
2533         TObjArray ** arr = AliTracker::GetResidualsArray() ; 
2534         if (arr) {
2535           AliRecoParam::EventSpecie_t es=fRecoParam.GetEventSpecie();
2536           TObjArray * elem = arr[AliRecoParam::AConvert(es)];
2537           if ( elem && (! elem->At(0)) ) {
2538             AliQADataMaker *qadm = AliQAManager::QAManager()->GetQADataMaker(AliQAv1::kGLOBAL);
2539             if (qadm) qadm->InitRecPointsForTracker() ; 
2540           }
2541         }
2542       }
2543     
2544     if (fTracker[iDet]->RefitInward(esd) != 0) {
2545       AliError(Form("%s inward refit failed", fgkDetectorName[iDet]));
2546       //      return kFALSE;
2547     }
2548     // run postprocessing
2549     if (fTracker[iDet]->PostProcess(esd) != 0) {
2550       AliError(Form("%s postprocessing failed", fgkDetectorName[iDet]));
2551       //      return kFALSE;
2552     }
2553     AliSysInfo::AddStamp(Form("Tracking2%s_%d",fgkDetectorName[iDet],eventNr), iDet,3, eventNr);
2554   }
2555
2556   // write space-points to the ESD in case alignment data output
2557   // is switched on
2558   if (fWriteAlignmentData)
2559     WriteAlignmentData(esd);
2560
2561   for (Int_t iDet = 3; iDet >= 0; iDet--) {
2562     if (!fTracker[iDet]) continue;
2563     // unload clusters
2564     fTracker[iDet]->UnloadClusters();
2565     AliSysInfo::AddStamp(Form("TUnloadCluster%s_%d",fgkDetectorName[iDet],eventNr), iDet,4, eventNr);
2566     fLoader[iDet]->UnloadRecPoints();
2567     AliSysInfo::AddStamp(Form("RUnloadCluster%s_%d",fgkDetectorName[iDet],eventNr), iDet,5, eventNr);
2568   }
2569   // stop filling residuals for TPC and ITS
2570   if (fRunGlobalQA) AliTracker::SetFillResiduals(fRecoParam.GetEventSpecie(), kFALSE);     
2571
2572   eventNr++;
2573   return kTRUE;
2574 }
2575
2576 //_____________________________________________________________________________
2577 Bool_t AliReconstruction::CleanESD(AliESDEvent *esd){
2578   //
2579   // Remove the data which are not needed for the physics analysis.
2580   //
2581
2582   Int_t nTracks=esd->GetNumberOfTracks();
2583   Int_t nV0s=esd->GetNumberOfV0s();
2584   AliInfo
2585   (Form("Number of ESD tracks and V0s before cleaning: %d %d",nTracks,nV0s));
2586
2587   Float_t cleanPars[]={fV0DCAmax,fV0CsPmin,fDmax,fZmax};
2588   Bool_t rc=esd->Clean(cleanPars);
2589
2590   nTracks=esd->GetNumberOfTracks();
2591   nV0s=esd->GetNumberOfV0s();
2592   AliInfo
2593   (Form("Number of ESD tracks and V0s after cleaning %d %d",nTracks,nV0s));
2594
2595   return rc;
2596 }
2597
2598 //_____________________________________________________________________________
2599 Bool_t AliReconstruction::FillESD(AliESDEvent*& esd, const TString& detectors)
2600 {
2601 // fill the event summary data
2602
2603   AliCodeTimerAuto("",0)
2604     static Int_t eventNr=0; 
2605   TString detStr = detectors;
2606   
2607   for (Int_t iDet = 0; iDet < kNDetectors; iDet++) {
2608   if (!IsSelected(fgkDetectorName[iDet], detStr)) continue;
2609     AliReconstructor* reconstructor = GetReconstructor(iDet);
2610     if (!reconstructor) continue;
2611     AliDebug(1, Form("filling ESD for %s", fgkDetectorName[iDet]));
2612     TTree* clustersTree = NULL;
2613     if (fLoader[iDet]) {
2614       fLoader[iDet]->LoadRecPoints("read");
2615       clustersTree = fLoader[iDet]->TreeR();
2616       if (!clustersTree) {
2617         AliError(Form("Can't get the %s clusters tree", 
2618                       fgkDetectorName[iDet]));
2619         if (fStopOnError) return kFALSE;
2620       }
2621     }
2622     if (fRawReader && !reconstructor->HasDigitConversion()) {
2623       reconstructor->FillESD(fRawReader, clustersTree, esd);
2624     } else {
2625       TTree* digitsTree = NULL;
2626       if (fLoader[iDet]) {
2627         fLoader[iDet]->LoadDigits("read");
2628         digitsTree = fLoader[iDet]->TreeD();
2629         if (!digitsTree) {
2630           AliError(Form("Can't get the %s digits tree", 
2631                         fgkDetectorName[iDet]));
2632           if (fStopOnError) return kFALSE;
2633         }
2634       }
2635       reconstructor->FillESD(digitsTree, clustersTree, esd);
2636       if (fLoader[iDet]) fLoader[iDet]->UnloadDigits();
2637     }
2638     if (fLoader[iDet]) {
2639       fLoader[iDet]->UnloadRecPoints();
2640     }
2641   }
2642   
2643   IsSelected("CTP", detStr);
2644   if ((detStr.CompareTo("ALL") != 0) && !detStr.IsNull()) {
2645     AliError(Form("the following detectors were not found: %s", 
2646                   detStr.Data()));
2647     if (fStopOnError) return kFALSE;
2648   }
2649   AliSysInfo::AddStamp(Form("FillESD%d",eventNr), 0,1, eventNr);
2650   eventNr++;
2651   return kTRUE;
2652 }
2653
2654 //_____________________________________________________________________________
2655 Bool_t AliReconstruction::FillTriggerESD(AliESDEvent*& esd)
2656 {
2657   // Reads the trigger decision which is
2658   // stored in Trigger.root file and fills
2659   // the corresponding esd entries
2660
2661   AliCodeTimerAuto("",0)
2662   
2663   AliInfo("Filling trigger information into the ESD");
2664
2665   if (fRawReader) {
2666     AliCTPRawStream input(fRawReader);
2667     if (!input.Next()) {
2668       AliWarning("No valid CTP (trigger) DDL raw data is found ! The trigger info is taken from the event header!");
2669     }
2670     else {
2671       if (esd->GetTriggerMask() != input.GetClassMask())
2672         AliError(Form("Invalid trigger pattern found in CTP raw-data: %llx %llx",
2673                       input.GetClassMask(),esd->GetTriggerMask()));
2674       if (esd->GetOrbitNumber() != input.GetOrbitID())
2675         AliError(Form("Invalid orbit id found in CTP raw-data: %x %x",
2676                       input.GetOrbitID(),esd->GetOrbitNumber()));
2677       if (esd->GetBunchCrossNumber() != input.GetBCID())
2678         AliError(Form("Invalid bunch-crossing id found in CTP raw-data: %x %x",
2679                       input.GetBCID(),esd->GetBunchCrossNumber()));
2680       AliESDHeader* esdheader = esd->GetHeader();
2681       esdheader->SetL0TriggerInputs(input.GetL0Inputs());
2682       esdheader->SetL1TriggerInputs(input.GetL1Inputs());
2683       esdheader->SetL2TriggerInputs(input.GetL2Inputs());
2684       // IR
2685       UInt_t orbit=input.GetOrbitID();
2686        for(Int_t i=0 ; i<input.GetNIRs() ; i++ )
2687           if(TMath::Abs(Int_t(orbit-(input.GetIR(i))->GetOrbit()))<=1){
2688              esdheader->AddTriggerIR(input.GetIR(i));
2689           }
2690     }
2691   }
2692   return kTRUE;
2693 }
2694 //_____________________________________________________________________________
2695 Bool_t AliReconstruction::FillTriggerScalers(AliESDEvent*& esd)
2696 {
2697   //Scalers
2698   //fRunScalers->Print();
2699   if(fRunScalers && fRunScalers->CheckRunScalers()){
2700      AliTimeStamp* timestamp = new AliTimeStamp(esd->GetOrbitNumber(), esd->GetPeriodNumber(), esd->GetBunchCrossNumber());
2701      //AliTimeStamp* timestamp = new AliTimeStamp(10308000, 0, (ULong64_t)486238);
2702      AliESDHeader* esdheader = fesd->GetHeader();
2703      for(Int_t i=0;i<50;i++){
2704           if((1ull<<i) & esd->GetTriggerMask()){
2705           AliTriggerScalersESD* scalesd = fRunScalers->GetScalersForEventClass( timestamp, i+1);
2706           if(scalesd)esdheader->SetTriggerScalersRecord(scalesd);
2707         }
2708      }
2709   }
2710   return kTRUE;
2711 }
2712 //_____________________________________________________________________________
2713 Bool_t AliReconstruction::FillRawEventHeaderESD(AliESDEvent*& esd)
2714 {
2715   // 
2716   // Filling information from RawReader Header
2717   // 
2718
2719   if (!fRawReader) return kFALSE;
2720
2721   AliInfo("Filling information from RawReader Header");
2722
2723   esd->SetBunchCrossNumber(fRawReader->GetBCID());
2724   esd->SetOrbitNumber(fRawReader->GetOrbitID());
2725   esd->SetPeriodNumber(fRawReader->GetPeriod());
2726
2727   esd->SetTimeStamp(fRawReader->GetTimestamp());  
2728   esd->SetEventType(fRawReader->GetType());
2729
2730   return kTRUE;
2731 }
2732
2733
2734 //_____________________________________________________________________________
2735 Bool_t AliReconstruction::IsSelected(TString detName, TString& detectors) const
2736 {
2737 // check whether detName is contained in detectors
2738 // if yes, it is removed from detectors
2739
2740   // check if all detectors are selected
2741   if ((detectors.CompareTo("ALL") == 0) ||
2742       detectors.BeginsWith("ALL ") ||
2743       detectors.EndsWith(" ALL") ||
2744       detectors.Contains(" ALL ")) {
2745     detectors = "ALL";
2746     return kTRUE;
2747   }
2748
2749   // search for the given detector
2750   Bool_t result = kFALSE;
2751   if ((detectors.CompareTo(detName) == 0) ||
2752       detectors.BeginsWith(detName+" ") ||
2753       detectors.EndsWith(" "+detName) ||
2754       detectors.Contains(" "+detName+" ")) {
2755     detectors.ReplaceAll(detName, "");
2756     result = kTRUE;
2757   }
2758
2759   // clean up the detectors string
2760   while (detectors.Contains("  ")) detectors.ReplaceAll("  ", " ");
2761   while (detectors.BeginsWith(" ")) detectors.Remove(0, 1);
2762   while (detectors.EndsWith(" ")) detectors.Remove(detectors.Length()-1, 1);
2763
2764   return result;
2765 }
2766
2767 //_____________________________________________________________________________
2768 Bool_t AliReconstruction::InitRunLoader()
2769 {
2770 // get or create the run loader
2771
2772   if (gAlice) delete gAlice;
2773   gAlice = NULL;
2774
2775   TFile *gafile = TFile::Open(fGAliceFileName.Data());
2776   //  if (!gSystem->AccessPathName(fGAliceFileName.Data())) { // galice.root exists
2777   if (gafile) { // galice.root exists
2778     gafile->Close();
2779     delete gafile;
2780
2781     // load all base libraries to get the loader classes
2782     TString libs = gSystem->GetLibraries();
2783     for (Int_t iDet = 0; iDet < kNDetectors; iDet++) {
2784       TString detName = fgkDetectorName[iDet];
2785       if (detName == "HLT") continue;
2786       if (libs.Contains("lib" + detName + "base.so")) continue;
2787       gSystem->Load("lib" + detName + "base.so");
2788     }
2789     fRunLoader = AliRunLoader::Open(fGAliceFileName.Data());
2790     if (!fRunLoader) {
2791       AliError(Form("no run loader found in file %s", fGAliceFileName.Data()));
2792       CleanUp();
2793       return kFALSE;
2794     }
2795
2796     fRunLoader->CdGAFile();
2797     fRunLoader->LoadgAlice();
2798
2799     //PH This is a temporary fix to give access to the kinematics
2800     //PH that is needed for the labels of ITS clusters
2801     fRunLoader->LoadHeader();
2802     fRunLoader->LoadKinematics();
2803
2804   } else {               // galice.root does not exist
2805     if (!fRawReader) {
2806       AliError(Form("the file %s does not exist", fGAliceFileName.Data()));
2807     }
2808     fRunLoader = AliRunLoader::Open(fGAliceFileName.Data(),
2809                                     AliConfig::GetDefaultEventFolderName(),
2810                                     "recreate");
2811     if (!fRunLoader) {
2812       AliError(Form("could not create run loader in file %s", 
2813                     fGAliceFileName.Data()));
2814       CleanUp();
2815       return kFALSE;
2816     }
2817     fIsNewRunLoader = kTRUE;
2818     fRunLoader->MakeTree("E");
2819
2820     if (fNumberOfEventsPerFile > 0)
2821       fRunLoader->SetNumberOfEventsPerFile(fNumberOfEventsPerFile);
2822     else
2823       fRunLoader->SetNumberOfEventsPerFile((UInt_t)-1);
2824   }
2825
2826   return kTRUE;
2827 }
2828
2829 //_____________________________________________________________________________
2830 AliReconstructor* AliReconstruction::GetReconstructor(Int_t iDet)
2831 {
2832 // get the reconstructor object and the loader for a detector
2833
2834   if (fReconstructor[iDet]) {
2835     if (fRecoParam.GetDetRecoParamArray(iDet) && !AliReconstructor::GetRecoParam(iDet)) {
2836       const AliDetectorRecoParam *par = fRecoParam.GetDetRecoParam(iDet);
2837       fReconstructor[iDet]->SetRecoParam(par);
2838       fReconstructor[iDet]->SetRunInfo(fRunInfo);
2839     }
2840     return fReconstructor[iDet];
2841   }
2842
2843   // load the reconstructor object
2844   TPluginManager* pluginManager = gROOT->GetPluginManager();
2845   TString detName = fgkDetectorName[iDet];
2846   TString recName = "Ali" + detName + "Reconstructor";
2847
2848   if (!fIsNewRunLoader && !fRunLoader->GetLoader(detName+"Loader") && (detName != "HLT")) return NULL;
2849
2850   AliReconstructor* reconstructor = NULL;
2851   // first check if a plugin is defined for the reconstructor
2852   TPluginHandler* pluginHandler = 
2853     pluginManager->FindHandler("AliReconstructor", detName);
2854   // if not, add a plugin for it
2855   if (!pluginHandler) {
2856     AliDebug(1, Form("defining plugin for %s", recName.Data()));
2857     TString libs = gSystem->GetLibraries();
2858     if (libs.Contains("lib" + detName + "base.so") ||
2859         (gSystem->Load("lib" + detName + "base.so") >= 0)) {
2860       pluginManager->AddHandler("AliReconstructor", detName, 
2861                                 recName, detName + "rec", recName + "()");
2862     } else {
2863       pluginManager->AddHandler("AliReconstructor", detName, 
2864                                 recName, detName, recName + "()");
2865     }
2866     pluginHandler = pluginManager->FindHandler("AliReconstructor", detName);
2867   }
2868   if (pluginHandler && (pluginHandler->LoadPlugin() == 0)) {
2869     reconstructor = (AliReconstructor*) pluginHandler->ExecPlugin(0);
2870   }
2871   if (reconstructor) {
2872     TObject* obj = fOptions.FindObject(detName.Data());
2873     if (obj) reconstructor->SetOption(obj->GetTitle());
2874     reconstructor->SetRunInfo(fRunInfo);
2875     reconstructor->Init();
2876     fReconstructor[iDet] = reconstructor;
2877   }
2878
2879   // get or create the loader
2880   if (detName != "HLT") {
2881     fLoader[iDet] = fRunLoader->GetLoader(detName + "Loader");
2882     if (!fLoader[iDet]) {
2883       AliConfig::Instance()
2884         ->CreateDetectorFolders(fRunLoader->GetEventFolder(), 
2885                                 detName, detName);
2886       // first check if a plugin is defined for the loader
2887       pluginHandler = 
2888         pluginManager->FindHandler("AliLoader", detName);
2889       // if not, add a plugin for it
2890       if (!pluginHandler) {
2891         TString loaderName = "Ali" + detName + "Loader";
2892         AliDebug(1, Form("defining plugin for %s", loaderName.Data()));
2893         pluginManager->AddHandler("AliLoader", detName, 
2894                                   loaderName, detName + "base", 
2895                                   loaderName + "(const char*, TFolder*)");
2896         pluginHandler = pluginManager->FindHandler("AliLoader", detName);
2897       }
2898       if (pluginHandler && (pluginHandler->LoadPlugin() == 0)) {
2899         fLoader[iDet] = 
2900           (AliLoader*) pluginHandler->ExecPlugin(2, detName.Data(), 
2901                                                  fRunLoader->GetEventFolder());
2902       }
2903       if (!fLoader[iDet]) {   // use default loader
2904         fLoader[iDet] = new AliLoader(detName, fRunLoader->GetEventFolder());
2905       }
2906       if (!fLoader[iDet]) {
2907         AliWarning(Form("couldn't get loader for %s", detName.Data()));
2908         if (fStopOnError) return NULL;
2909       } else {
2910         fRunLoader->AddLoader(fLoader[iDet]);
2911         fRunLoader->CdGAFile();
2912         if (gFile && !gFile->IsWritable()) gFile->ReOpen("UPDATE");
2913         fRunLoader->Write(0, TObject::kOverwrite);
2914       }
2915     }
2916   }
2917       
2918   if (fRecoParam.GetDetRecoParamArray(iDet) && !AliReconstructor::GetRecoParam(iDet)) {
2919     const AliDetectorRecoParam *par = fRecoParam.GetDetRecoParam(iDet);
2920     reconstructor->SetRecoParam(par);
2921     reconstructor->SetRunInfo(fRunInfo);
2922   }
2923   return reconstructor;
2924 }
2925
2926 //_____________________________________________________________________________
2927 AliVertexer* AliReconstruction::CreateVertexer()
2928 {
2929 // create the vertexer
2930 // Please note that the caller is the owner of the
2931 // vertexer
2932
2933   AliVertexer* vertexer = NULL;
2934   AliReconstructor* itsReconstructor = GetReconstructor(0);
2935   if (itsReconstructor && ((fRunLocalReconstruction.Contains("ITS")) || fRunTracking.Contains("ITS"))) {
2936     vertexer = itsReconstructor->CreateVertexer();
2937   }
2938   if (!vertexer) {
2939     AliWarning("couldn't create a vertexer for ITS");
2940   }
2941
2942   return vertexer;
2943 }
2944
2945 //_____________________________________________________________________________
2946 Bool_t AliReconstruction::CreateTrackers(const TString& detectors)
2947 {
2948 // create the trackers
2949         AliInfo("Creating trackers");
2950
2951   TString detStr = detectors;
2952   for (Int_t iDet = 0; iDet < kNDetectors; iDet++) {
2953     if (!IsSelected(fgkDetectorName[iDet], detStr)) continue;
2954     AliReconstructor* reconstructor = GetReconstructor(iDet);
2955     if (!reconstructor) continue;
2956     TString detName = fgkDetectorName[iDet];
2957     if (detName == "HLT") {
2958       fRunHLTTracking = kTRUE;
2959       continue;
2960     }
2961     if (detName == "MUON") {
2962       fRunMuonTracking = kTRUE;
2963       continue;
2964     }
2965
2966
2967     fTracker[iDet] = reconstructor->CreateTracker();
2968     if (!fTracker[iDet] && (iDet < 7)) {
2969       AliWarning(Form("couldn't create a tracker for %s", detName.Data()));
2970       if (fStopOnError) return kFALSE;
2971     }
2972     AliSysInfo::AddStamp(Form("LTracker%s",fgkDetectorName[iDet]), iDet,0);
2973   }
2974
2975   return kTRUE;
2976 }
2977
2978 //_____________________________________________________________________________
2979 void AliReconstruction::CleanUp()
2980 {
2981 // delete trackers and the run loader and close and delete the file
2982
2983   for (Int_t iDet = 0; iDet < kNDetectors; iDet++) {
2984     delete fReconstructor[iDet];
2985     fReconstructor[iDet] = NULL;
2986     fLoader[iDet] = NULL;
2987     delete fTracker[iDet];
2988     fTracker[iDet] = NULL;
2989   }
2990   delete fRunInfo;
2991   fRunInfo = NULL;
2992
2993   delete fSPDTrackleter;
2994   fSPDTrackleter = NULL;
2995
2996   delete ftVertexer;
2997   ftVertexer = NULL;
2998   
2999   delete fRunLoader;
3000   fRunLoader = NULL;
3001   delete fRawReader;
3002   fRawReader = NULL;
3003   delete fParentRawReader;
3004   fParentRawReader=NULL;
3005
3006   if (ffile) {
3007     ffile->Close();
3008     delete ffile;
3009     ffile = NULL;
3010   }
3011
3012   if (AliQAManager::QAManager())
3013     AliQAManager::QAManager()->ShowQA() ; 
3014   AliQAManager::Destroy() ; 
3015   
3016 }
3017
3018 void AliReconstruction::WriteAlignmentData(AliESDEvent* esd)
3019 {
3020   // Write space-points which are then used in the alignment procedures
3021   // For the moment only ITS, TPC, TRD and TOF
3022
3023   Int_t ntracks = esd->GetNumberOfTracks();
3024   for (Int_t itrack = 0; itrack < ntracks; itrack++)
3025     {
3026       AliESDtrack *track = esd->GetTrack(itrack);
3027       Int_t nsp = 0;
3028       Int_t idx[200];
3029       for (Int_t i=0; i<200; ++i) idx[i] = -1; //PH avoid uninitialized values
3030       for (Int_t iDet = 5; iDet >= 0; iDet--) {// TOF, TRD, TPC, ITS clusters
3031           nsp += (iDet==GetDetIndex("TRD")) ? track->GetTRDntracklets():track->GetNcls(iDet);
3032
3033           if (iDet==GetDetIndex("ITS")) { // ITS "extra" clusters
3034              track->GetClusters(iDet,idx);
3035              for (Int_t i=6; i<12; i++) if(idx[i] >= 0) nsp++;
3036           }  
3037       }
3038
3039       if (nsp) {
3040         AliTrackPointArray *sp = new AliTrackPointArray(nsp);
3041         track->SetTrackPointArray(sp);
3042         Int_t isptrack = 0;
3043         for (Int_t iDet = 5; iDet >= 0; iDet--) {
3044           AliTracker *tracker = fTracker[iDet];
3045           if (!tracker) continue;
3046           Int_t nspdet = (iDet==GetDetIndex("TRD")) ? track->GetTRDtracklets(idx):track->GetClusters(iDet,idx);
3047
3048           if (iDet==GetDetIndex("ITS")) // ITS "extra" clusters             
3049              for (Int_t i=6; i<12; i++) if(idx[i] >= 0) nspdet++;
3050
3051           if (nspdet <= 0) continue;
3052           AliTrackPoint p;
3053           Int_t isp = 0;
3054           Int_t isp2 = 0;
3055           while (isp2 < nspdet) {
3056             Bool_t isvalid=kTRUE;
3057
3058             Int_t index=idx[isp++];
3059             if (index < 0) continue;
3060
3061             TString dets = fgkDetectorName[iDet];
3062             if ((fUseTrackingErrorsForAlignment.CompareTo(dets) == 0) ||
3063             fUseTrackingErrorsForAlignment.BeginsWith(dets+" ") ||
3064             fUseTrackingErrorsForAlignment.EndsWith(" "+dets) ||
3065             fUseTrackingErrorsForAlignment.Contains(" "+dets+" ")) {
3066               isvalid = tracker->GetTrackPointTrackingError(index,p,track);
3067             } else {
3068               isvalid = tracker->GetTrackPoint(index,p); 
3069             } 
3070             isp2++;
3071             if (!isvalid) continue;
3072             if (iDet==GetDetIndex("ITS") && (isp-1)>=6) p.SetExtra();
3073             sp->AddPoint(isptrack,&p); isptrack++;
3074           }
3075         }       
3076       }
3077     }
3078 }
3079
3080 //_____________________________________________________________________________
3081 void AliReconstruction::FillRawDataErrorLog(Int_t iEvent, AliESDEvent* esd)
3082 {
3083   // The method reads the raw-data error log
3084   // accumulated within the rawReader.
3085   // It extracts the raw-data errors related to
3086   // the current event and stores them into
3087   // a TClonesArray inside the esd object.
3088
3089   if (!fRawReader) return;
3090
3091   for(Int_t i = 0; i < fRawReader->GetNumberOfErrorLogs(); i++) {
3092
3093     AliRawDataErrorLog *log = fRawReader->GetErrorLog(i);
3094     if (!log) continue;
3095     if (iEvent != log->GetEventNumber()) continue;
3096
3097     esd->AddRawDataErrorLog(log);
3098   }
3099
3100 }
3101
3102 //_____________________________________________________________________________
3103 void AliReconstruction::CheckQA()
3104 {
3105 // check the QA of SIM for this run and remove the detectors 
3106 // with status Fatal
3107   
3108 //      TString newRunLocalReconstruction ; 
3109 //      TString newRunTracking ;
3110 //      TString newFillESD ;
3111 //       
3112 //      for (Int_t iDet = 0; iDet < AliQAv1::kNDET; iDet++) {
3113 //              TString detName(AliQAv1::GetDetName(iDet)) ;
3114 //              AliQAv1 * qa = AliQAv1::Instance(AliQAv1::DETECTORINDEX_t(iDet)) ;       
3115 //      if ( qa->IsSet(AliQAv1::DETECTORINDEX_t(iDet), AliQAv1::kSIM, specie, AliQAv1::kFATAL)) {
3116 //        AliInfo(Form("QA status for %s %s in Hits and/or SDIGITS  and/or Digits was Fatal; No reconstruction performed", 
3117 //                   detName.Data(), AliRecoParam::GetEventSpecieName(es))) ;
3118 //                      } else {
3119 //                      if ( fRunLocalReconstruction.Contains(AliQAv1::GetDetName(iDet)) || 
3120 //                                      fRunLocalReconstruction.Contains("ALL") )  {
3121 //                              newRunLocalReconstruction += detName ; 
3122 //                              newRunLocalReconstruction += " " ;                      
3123 //                      }
3124 //                      if ( fRunTracking.Contains(AliQAv1::GetDetName(iDet)) || 
3125 //                                      fRunTracking.Contains("ALL") )  {
3126 //                              newRunTracking += detName ; 
3127 //                              newRunTracking += " " ;                         
3128 //                      }
3129 //                      if ( fFillESD.Contains(AliQAv1::GetDetName(iDet)) || 
3130 //                                      fFillESD.Contains("ALL") )  {
3131 //                              newFillESD += detName ; 
3132 //                              newFillESD += " " ;                     
3133 //                      }
3134 //              }
3135 //      }
3136 //      fRunLocalReconstruction = newRunLocalReconstruction ; 
3137 //      fRunTracking            = newRunTracking ; 
3138 //      fFillESD                = newFillESD ; 
3139 }
3140
3141 //_____________________________________________________________________________
3142 Int_t AliReconstruction::GetDetIndex(const char* detector)
3143 {
3144   // return the detector index corresponding to detector
3145   Int_t index = -1 ; 
3146   for (index = 0; index < kNDetectors ; index++) {
3147     if ( strcmp(detector, fgkDetectorName[index]) == 0 )
3148         break ; 
3149   }     
3150   return index ; 
3151 }
3152 //_____________________________________________________________________________
3153 Bool_t AliReconstruction::FinishPlaneEff() {
3154  //
3155  // Here execute all the necessary operationis, at the end of the tracking phase,
3156  // in case that evaluation of PlaneEfficiencies was required for some detector.
3157  // E.g., write into a DataBase file the PlaneEfficiency which have been evaluated.
3158  //
3159  // This Preliminary version works only FOR ITS !!!!!
3160  // other detectors (TOF,TRD, etc. have to develop their specific codes)
3161  //
3162  //  Input: none
3163  //  Return: kTRUE if all operations have been done properly, kFALSE otherwise
3164  //
3165  Bool_t ret=kFALSE;
3166  TString detStr = fLoadCDB;
3167  //for (Int_t iDet = 0; iDet < fgkNDetectors; iDet++) {
3168  for (Int_t iDet = 0; iDet < 1; iDet++) { // for the time being only ITS
3169    if (!IsSelected(fgkDetectorName[iDet], detStr)) continue;
3170    if(fTracker[iDet] && fTracker[iDet]->GetPlaneEff()) {
3171       AliPlaneEff *planeeff=fTracker[iDet]->GetPlaneEff();
3172       TString name=planeeff->GetName();
3173       name+=".root";
3174       TFile* pefile = TFile::Open(name, "RECREATE");
3175       ret=(Bool_t)planeeff->Write();
3176       pefile->Close();
3177       if(planeeff->GetCreateHistos()) {
3178         TString hname=planeeff->GetName();
3179         hname+="Histo.root";
3180         ret*=planeeff->WriteHistosToFile(hname,"RECREATE");
3181       }
3182    }
3183    if(fSPDTrackleter) {
3184      AliPlaneEff *planeeff=fSPDTrackleter->GetPlaneEff();
3185       TString name="AliITSPlaneEffSPDtracklet.root";
3186       TFile* pefile = TFile::Open(name, "RECREATE");
3187       ret=(Bool_t)planeeff->Write();
3188       pefile->Close();
3189       AliESDEvent *dummy=NULL;
3190       ret=(Bool_t)fSPDTrackleter->PostProcess(dummy); // take care of writing other files
3191    }
3192  }
3193  return ret;
3194 }
3195 //_____________________________________________________________________________
3196 Bool_t AliReconstruction::InitPlaneEff() {
3197 //
3198  // Here execute all the necessary operations, before of the tracking phase,
3199  // for the evaluation of PlaneEfficiencies, in case required for some detectors.
3200  // E.g., read from a DataBase file a first evaluation of the PlaneEfficiency
3201  // which should be updated/recalculated.
3202  //
3203  // This Preliminary version will work only FOR ITS !!!!!
3204  // other detectors (TOF,TRD, etc. have to develop their specific codes)
3205  //
3206  //  Input: none
3207  //  Return: kTRUE if all operations have been done properly, kFALSE otherwise
3208  //
3209  AliWarning(Form("Implementation of this method not yet completed !! Method return kTRUE"));
3210
3211   fSPDTrackleter = NULL;
3212   TString detStr = fLoadCDB;
3213   if (IsSelected(fgkDetectorName[0], detStr)) {
3214     AliReconstructor* itsReconstructor = GetReconstructor(0);
3215     if (itsReconstructor) {
3216       fSPDTrackleter = itsReconstructor->CreateTrackleter(); // this is NULL unless required in RecoParam
3217     }
3218     if (fSPDTrackleter) {
3219       AliInfo("Trackleter for SPD has been created");
3220     }
3221   }
3222  return kTRUE;
3223 }
3224
3225 //_____________________________________________________________________________
3226 Bool_t AliReconstruction::InitAliEVE()
3227 {
3228   // This method should be called only in case 
3229   // AliReconstruction is run
3230   // within the alieve environment.
3231   // It will initialize AliEVE in a way
3232   // so that it can visualize event processed
3233   // by AliReconstruction.
3234   // The return flag shows whenever the
3235   // AliEVE initialization was successful or not.
3236
3237   TString macroStr;
3238   macroStr.Form("%s/EVE/macros/alieve_online.C",gSystem->ExpandPathName("$ALICE_ROOT"));
3239   AliInfo(Form("Loading AliEVE macro: %s",macroStr.Data()));
3240   if (gROOT->LoadMacro(macroStr.Data()) != 0) return kFALSE;
3241
3242   gROOT->ProcessLine("if (!AliEveEventManager::GetMaster()){new AliEveEventManager();AliEveEventManager::GetMaster()->AddNewEventCommand(\"alieve_online_on_new_event()\");gEve->AddEvent(AliEveEventManager::GetMaster());};");
3243   gROOT->ProcessLine("alieve_online_init()");
3244
3245   return kTRUE;
3246 }
3247   
3248 //_____________________________________________________________________________
3249 void AliReconstruction::RunAliEVE()
3250 {
3251   // Runs AliEVE visualisation of
3252   // the current event.
3253   // Should be executed only after
3254   // successful initialization of AliEVE.
3255
3256   AliInfo("Running AliEVE...");
3257   gROOT->ProcessLine(Form("AliEveEventManager::GetMaster()->SetEvent((AliRunLoader*)0x%lx,(AliRawReader*)0x%lx,(AliESDEvent*)0x%lx,(AliESDfriend*)0x%lx);",fRunLoader,fRawReader,fesd,fesdf));
3258   gSystem->Run();
3259 }
3260
3261 //_____________________________________________________________________________
3262 Bool_t AliReconstruction::SetRunQA(TString detAndAction) 
3263 {
3264         // Allows to run QA for a selected set of detectors
3265         // and a selected set of tasks among RAWS, DIGITSR, RECPOINTS and ESDS
3266         // all selected detectors run the same selected tasks
3267         
3268         if (!detAndAction.Contains(":")) {
3269                 AliError( Form("%s is a wrong syntax, use \"DetectorList:ActionList\" \n", detAndAction.Data()) ) ;
3270                 fRunQA = kFALSE ;
3271                 return kFALSE ;                 
3272         }
3273         Int_t colon = detAndAction.Index(":") ; 
3274         fQADetectors = detAndAction(0, colon) ; 
3275         fQATasks   = detAndAction(colon+1, detAndAction.Sizeof() ) ; 
3276         if (fQATasks.Contains("ALL") ) {
3277                 fQATasks = Form("%d %d %d %d", AliQAv1::kRAWS, AliQAv1::kDIGITSR, AliQAv1::kRECPOINTS, AliQAv1::kESDS) ; 
3278         } else {
3279                 fQATasks.ToUpper() ; 
3280                 TString tempo("") ; 
3281                 if ( fQATasks.Contains("RAW") ) 
3282                         tempo = Form("%d ", AliQAv1::kRAWS) ; 
3283                 if ( fQATasks.Contains("DIGIT") ) 
3284                         tempo += Form("%d ", AliQAv1::kDIGITSR) ; 
3285                 if ( fQATasks.Contains("RECPOINT") ) 
3286                         tempo += Form("%d ", AliQAv1::kRECPOINTS) ; 
3287                 if ( fQATasks.Contains("ESD") ) 
3288                         tempo += Form("%d ", AliQAv1::kESDS) ; 
3289                 fQATasks = tempo ; 
3290                 if (fQATasks.IsNull()) {
3291                         AliInfo("No QA requested\n")  ;
3292                         fRunQA = kFALSE ;
3293                         return kTRUE ; 
3294                 }
3295         }       
3296         TString tempo(fQATasks) ; 
3297         tempo.ReplaceAll(Form("%d", AliQAv1::kRAWS), AliQAv1::GetTaskName(AliQAv1::kRAWS))      ;
3298         tempo.ReplaceAll(Form("%d", AliQAv1::kDIGITSR), AliQAv1::GetTaskName(AliQAv1::kDIGITSR)) ;      
3299         tempo.ReplaceAll(Form("%d", AliQAv1::kRECPOINTS), AliQAv1::GetTaskName(AliQAv1::kRECPOINTS)) ;  
3300         tempo.ReplaceAll(Form("%d", AliQAv1::kESDS), AliQAv1::GetTaskName(AliQAv1::kESDS)) ;    
3301         AliInfo( Form("QA will be done on \"%s\" for \"%s\"\n", fQADetectors.Data(), tempo.Data()) ) ;  
3302         fRunQA = kTRUE ;
3303         return kTRUE; 
3304
3305
3306 //_____________________________________________________________________________
3307 Bool_t AliReconstruction::InitRecoParams() 
3308 {
3309   // The method accesses OCDB and retrieves all
3310   // the available reco-param objects from there.
3311
3312   Bool_t isOK = kTRUE;
3313
3314   if (fRecoParam.GetDetRecoParamArray(kNDetectors)) {
3315     AliInfo("Using custom GRP reconstruction parameters");
3316   }
3317   else {
3318     AliInfo("Loading GRP reconstruction parameter objects");
3319
3320     AliCDBPath path("GRP","Calib","RecoParam");
3321     AliCDBEntry *entry=AliCDBManager::Instance()->Get(path.GetPath());
3322     if(!entry){ 
3323       AliWarning("Couldn't find GRP RecoParam entry in OCDB");
3324       isOK = kFALSE;
3325     }
3326     else {
3327       TObject *recoParamObj = entry->GetObject();
3328       if (dynamic_cast<TObjArray*>(recoParamObj)) {
3329         // GRP has a normal TobjArray of AliDetectorRecoParam objects
3330         // Registering them in AliRecoParam
3331         fRecoParam.AddDetRecoParamArray(kNDetectors,dynamic_cast<TObjArray*>(recoParamObj));
3332       }
3333       else if (dynamic_cast<AliDetectorRecoParam*>(recoParamObj)) {
3334         // GRP has only onse set of reco parameters
3335         // Registering it in AliRecoParam
3336         AliInfo("Single set of GRP reconstruction parameters found");
3337         dynamic_cast<AliDetectorRecoParam*>(recoParamObj)->SetAsDefault();
3338         fRecoParam.AddDetRecoParam(kNDetectors,dynamic_cast<AliDetectorRecoParam*>(recoParamObj));
3339       }
3340       else {
3341         AliError("No valid GRP RecoParam object found in the OCDB");
3342         isOK = kFALSE;
3343       }
3344       entry->SetOwner(0);
3345     }
3346   }
3347
3348   TString detStr = fLoadCDB;
3349   for (Int_t iDet = 0; iDet < kNDetectors; iDet++) {
3350
3351     if (!IsSelected(fgkDetectorName[iDet], detStr)) continue;
3352
3353     if (fRecoParam.GetDetRecoParamArray(iDet)) {
3354       AliInfo(Form("Using custom reconstruction parameters for detector %s",fgkDetectorName[iDet]));
3355       continue;
3356     }
3357
3358     AliInfo(Form("Loading reconstruction parameter objects for detector %s",fgkDetectorName[iDet]));
3359   
3360     AliCDBPath path(fgkDetectorName[iDet],"Calib","RecoParam");
3361     AliCDBEntry *entry=AliCDBManager::Instance()->Get(path.GetPath());
3362     if(!entry){ 
3363       AliWarning(Form("Couldn't find RecoParam entry in OCDB for detector %s",fgkDetectorName[iDet]));
3364       isOK = kFALSE;
3365     }
3366     else {
3367       TObject *recoParamObj = entry->GetObject();
3368       if (dynamic_cast<TObjArray*>(recoParamObj)) {
3369         // The detector has a normal TobjArray of AliDetectorRecoParam objects
3370         // Registering them in AliRecoParam
3371         fRecoParam.AddDetRecoParamArray(iDet,dynamic_cast<TObjArray*>(recoParamObj));
3372       }
3373       else if (dynamic_cast<AliDetectorRecoParam*>(recoParamObj)) {
3374         // The detector has only onse set of reco parameters
3375         // Registering it in AliRecoParam
3376         AliInfo(Form("Single set of reconstruction parameters found for detector %s",fgkDetectorName[iDet]));
3377         dynamic_cast<AliDetectorRecoParam*>(recoParamObj)->SetAsDefault();
3378         fRecoParam.AddDetRecoParam(iDet,dynamic_cast<AliDetectorRecoParam*>(recoParamObj));
3379       }
3380       else {
3381         AliError(Form("No valid RecoParam object found in the OCDB for detector %s",fgkDetectorName[iDet]));
3382         isOK = kFALSE;
3383       }
3384       entry->SetOwner(0);
3385       //      FIX ME: We have to disable the unloading of reco-param CDB
3386       //      entries because QA framework is using them. Has to be fix in
3387       //      a way that the QA takes the objects already constructed in
3388       //      this method.
3389       //      AliCDBManager::Instance()->UnloadFromCache(path.GetPath());
3390     }
3391   }
3392
3393   if (AliDebugLevel() > 0) fRecoParam.Print();
3394
3395   return isOK;
3396 }
3397
3398 //_____________________________________________________________________________
3399 Bool_t AliReconstruction::GetEventInfo() 
3400 {
3401   // Fill the event info object
3402   // ...
3403   AliCodeTimerAuto("",0)
3404
3405   AliCentralTrigger *aCTP = NULL;
3406   if (fRawReader) {
3407     fEventInfo.SetEventType(fRawReader->GetType());
3408
3409     ULong64_t mask = fRawReader->GetClassMask();
3410     fEventInfo.SetTriggerMask(mask);
3411     UInt_t clmask = fRawReader->GetDetectorPattern()[0];
3412     fEventInfo.SetTriggerCluster(AliDAQ::ListOfTriggeredDetectors(clmask));
3413
3414     aCTP = new AliCentralTrigger();
3415     TString configstr("");
3416     if (!aCTP->LoadConfiguration(configstr)) { // Load CTP config from OCDB
3417       AliError("No trigger configuration found in OCDB! The trigger configuration information will not be used!");
3418       delete aCTP;
3419       return kFALSE;
3420     }
3421     aCTP->SetClassMask(mask);
3422     aCTP->SetClusterMask(clmask);
3423   }
3424   else {
3425     fEventInfo.SetEventType(AliRawEventHeaderBase::kPhysicsEvent);
3426
3427     if (fRunLoader && (!fRunLoader->LoadTrigger())) {
3428       aCTP = fRunLoader->GetTrigger();
3429       fEventInfo.SetTriggerMask(aCTP->GetClassMask());
3430       // get inputs from actp - just get
3431       AliESDHeader* esdheader = fesd->GetHeader();
3432       esdheader->SetL0TriggerInputs(aCTP->GetL0TriggerInputs());
3433       esdheader->SetL1TriggerInputs(aCTP->GetL1TriggerInputs());
3434       esdheader->SetL2TriggerInputs(aCTP->GetL2TriggerInputs());
3435       fEventInfo.SetTriggerCluster(AliDAQ::ListOfTriggeredDetectors(aCTP->GetClusterMask()));
3436     }
3437     else {
3438       AliWarning("No trigger can be loaded! The trigger information will not be used!");
3439       return kFALSE;
3440     }
3441   }
3442
3443   AliTriggerConfiguration *config = aCTP->GetConfiguration();
3444   if (!config) {
3445     AliError("No trigger configuration has been found! The trigger configuration information will not be used!");
3446     if (fRawReader) delete aCTP;
3447     return kFALSE;
3448   }
3449
3450   UChar_t clustmask = 0;
3451   TString trclasses;
3452   ULong64_t trmask = fEventInfo.GetTriggerMask();
3453   const TObjArray& classesArray = config->GetClasses();
3454   Int_t nclasses = classesArray.GetEntriesFast();
3455   for( Int_t iclass=0; iclass < nclasses; iclass++ ) {
3456     AliTriggerClass* trclass = (AliTriggerClass*)classesArray.At(iclass);
3457     if (trclass) {
3458       Int_t trindex = TMath::Nint(TMath::Log2(trclass->GetMask()));
3459       fesd->SetTriggerClass(trclass->GetName(),trindex);
3460       if (fRawReader) fRawReader->LoadTriggerClass(trclass->GetName(),trindex);
3461       if (trmask & (1ull << trindex)) {
3462         trclasses += " ";
3463         trclasses += trclass->GetName();
3464         trclasses += " ";
3465         clustmask |= trclass->GetCluster()->GetClusterMask();
3466       }
3467     }
3468   }
3469   fEventInfo.SetTriggerClasses(trclasses);
3470
3471   // Set the information in ESD
3472   fesd->SetTriggerMask(trmask);
3473   fesd->SetTriggerCluster(clustmask);
3474
3475   if (!aCTP->CheckTriggeredDetectors()) {
3476     if (fRawReader) delete aCTP;
3477     return kFALSE;
3478   }    
3479
3480   if (fRawReader) delete aCTP;
3481
3482   // We have to fill also the HLT decision here!!
3483   // ...
3484
3485   return kTRUE;
3486 }
3487
3488 const char *AliReconstruction::MatchDetectorList(const char *detectorList, UInt_t detectorMask)
3489 {
3490   // Match the detector list found in the rec.C or the default 'ALL'
3491   // to the list found in the GRP (stored there by the shuttle PP which
3492   // gets the information from ECS)
3493   static TString resultList;
3494   TString detList = detectorList;
3495
3496   resultList = "";
3497
3498   for(Int_t iDet = 0; iDet < (AliDAQ::kNDetectors-1); iDet++) {
3499     if ((detectorMask >> iDet) & 0x1) {
3500       TString det = AliDAQ::OfflineModuleName(iDet);
3501       if ((detList.CompareTo("ALL") == 0) ||
3502           ((detList.BeginsWith("ALL ") ||
3503             detList.EndsWith(" ALL") ||
3504             detList.Contains(" ALL ")) &&
3505            !(detList.BeginsWith("-"+det+" ") ||
3506              detList.EndsWith(" -"+det) ||
3507              detList.Contains(" -"+det+" "))) ||
3508           (detList.CompareTo(det) == 0) ||
3509           detList.BeginsWith(det+" ") ||
3510           detList.EndsWith(" "+det) ||
3511           detList.Contains( " "+det+" " )) {
3512         if (!resultList.EndsWith(det + " ")) {
3513           resultList += det;
3514           resultList += " ";
3515         }
3516       }        
3517     }
3518   }
3519
3520   // HLT
3521   if ((detectorMask >> AliDAQ::kHLTId) & 0x1) {
3522     TString hltDet = AliDAQ::OfflineModuleName(AliDAQ::kNDetectors-1);
3523     if ((detList.CompareTo("ALL") == 0) ||
3524         ((detList.BeginsWith("ALL ") ||
3525           detList.EndsWith(" ALL") ||
3526           detList.Contains(" ALL ")) &&
3527          !(detList.BeginsWith("-"+hltDet+" ") ||
3528            detList.EndsWith(" -"+hltDet) ||
3529            detList.Contains(" -"+hltDet+" "))) ||
3530         (detList.CompareTo(hltDet) == 0) ||
3531         detList.BeginsWith(hltDet+" ") ||
3532         detList.EndsWith(" "+hltDet) ||
3533         detList.Contains( " "+hltDet+" " )) {
3534       resultList += hltDet;
3535     }
3536   }
3537
3538   return resultList.Data();
3539
3540 }
3541
3542 //______________________________________________________________________________
3543 void AliReconstruction::Abort(const char *method, EAbort what)
3544 {
3545   // Abort processing. If what = kAbortProcess, the Process() loop will be
3546   // aborted. If what = kAbortFile, the current file in a chain will be
3547   // aborted and the processing will continue with the next file, if there
3548   // is no next file then Process() will be aborted. Abort() can also  be
3549   // called from Begin(), SlaveBegin(), Init() and Notify(). After abort
3550   // the SlaveTerminate() and Terminate() are always called. The abort flag
3551   // can be checked in these methods using GetAbort().
3552   //
3553   // The method is overwritten in AliReconstruction for better handling of
3554   // reco specific errors 
3555
3556   if (!fStopOnError) return;
3557
3558   CleanUp();
3559
3560   TString whyMess = method;
3561   whyMess += " failed! Aborting...";
3562
3563   AliError(whyMess.Data());
3564
3565   fAbort = what;
3566   TString mess = "Abort";
3567   if (fAbort == kAbortProcess)
3568     mess = "AbortProcess";
3569   else if (fAbort == kAbortFile)
3570     mess = "AbortFile";
3571
3572   Info(mess, whyMess.Data());
3573 }
3574
3575 //______________________________________________________________________________
3576 Bool_t AliReconstruction::ProcessEvent(void* event)
3577 {
3578   // Method that is used in case the event loop
3579   // is steered from outside, for example by AMORE
3580   // 'event' is a pointer to the DATE event in the memory
3581
3582   if (fRawReader) delete fRawReader;
3583   fRawReader = new AliRawReaderDate(event);
3584   fStatus = ProcessEvent(fRunLoader->GetNumberOfEvents());  
3585   delete fRawReader;
3586   fRawReader = NULL;
3587
3588   return fStatus;
3589 }
3590
3591 //______________________________________________________________________________
3592 Bool_t AliReconstruction::ParseOutput()
3593 {
3594   // The method parses the output file
3595   // location string in order to steer
3596   // properly the selector
3597
3598   TPMERegexp re1("(\\w+\\.zip#\\w+\\.root):([,*\\w+\\.root,*]+)@dataset://(\\w++)");
3599   TPMERegexp re2("(\\w+\\.root)?@?dataset://(\\w++)");
3600
3601   if (re1.Match(fESDOutput) == 4) {
3602     // root archive with output files stored and regustered
3603     // in proof dataset
3604     gProof->AddInput(new TNamed("PROOF_OUTPUTFILE",re1[1].Data()));
3605     gProof->AddInput(new TNamed("PROOF_OUTPUTFILE_LOCATION",re1[3].Data()));
3606     gProof->AddInput(new TNamed("PROOF_OUTPUTFILE_DATASET",""));
3607     gProof->AddInput(new TNamed("PROOF_OUTPUTFILE_ARCHIVE",re1[2].Data()));
3608     AliInfo(Form("%s files will be stored within %s in dataset %s",
3609                  re1[2].Data(),
3610                  re1[1].Data(),
3611                  re1[3].Data()));
3612   }
3613   else if (re2.Match(fESDOutput) == 3) {
3614     // output file stored and registered
3615     // in proof dataset
3616     gProof->AddInput(new TNamed("PROOF_OUTPUTFILE",(re2[1].IsNull()) ? "AliESDs.root" : re2[1].Data()));
3617     gProof->AddInput(new TNamed("PROOF_OUTPUTFILE_LOCATION",re2[2].Data()));
3618     gProof->AddInput(new TNamed("PROOF_OUTPUTFILE_DATASET",""));
3619     AliInfo(Form("%s will be stored in dataset %s",
3620                  (re2[1].IsNull()) ? "AliESDs.root" : re2[1].Data(),
3621                  re2[2].Data()));
3622   }
3623   else {
3624     if (fESDOutput.IsNull()) {
3625       // Output location not given.
3626       // Assuming xrootd has been already started and
3627       // the output file has to be sent back
3628       // to the client machine
3629       TString esdUrl(Form("root://%s/%s/",
3630                           TUrl(gSystem->HostName()).GetHostFQDN(),
3631                           gSystem->pwd()));
3632       gProof->AddInput(new TNamed("PROOF_OUTPUTFILE","AliESDs.root"));
3633       gProof->AddInput(new TNamed("PROOF_OUTPUTFILE_LOCATION",esdUrl.Data()));
3634       AliInfo(Form("AliESDs.root will be stored in %s",
3635                    esdUrl.Data()));
3636     }
3637     else {
3638       // User specified an output location.
3639       // Ones has just to parse it here
3640       TUrl outputUrl(fESDOutput.Data());
3641       TString outputFile(gSystem->BaseName(outputUrl.GetFile()));
3642       gProof->AddInput(new TNamed("PROOF_OUTPUTFILE",outputFile.IsNull() ? "AliESDs.root" : outputFile.Data()));
3643       TString outputLocation(outputUrl.GetUrl());
3644       outputLocation.ReplaceAll(outputFile.Data(),"");
3645       gProof->AddInput(new TNamed("PROOF_OUTPUTFILE_LOCATION",outputLocation.Data()));
3646       AliInfo(Form("%s will be stored in %s",
3647                    outputFile.IsNull() ? "AliESDs.root" : outputFile.Data(),
3648                    outputLocation.Data()));
3649     }
3650   }
3651
3652   return kTRUE;
3653 }