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