]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/STEER/AliReconstruction.cxx
Complete implementation of pools, see #88914. From rev. 53550,53557,53568 (Ruben)
[u/mrichter/AliRoot.git] / STEER / 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 #include <TUrl.h>
133 #include <TRandom.h>
134
135 #include "AliAlignObj.h"
136 #include "AliAnalysisManager.h"
137 #include "AliAnalysisDataContainer.h"
138 #include "AliCDBEntry.h"
139 #include "AliCDBManager.h"
140 #include "AliCDBStorage.h"
141 #include "AliCTPRawStream.h"
142 #include "AliCascadeVertexer.h"
143 #include "AliCentralTrigger.h"
144 #include "AliCodeTimer.h"
145 #include "AliDAQ.h"
146 #include "AliDetectorRecoParam.h"
147 #include "AliESDCaloCells.h"
148 #include "AliESDCaloCluster.h"
149 #include "AliESDEvent.h"
150 #include "AliESDMuonTrack.h"
151 #include "AliESDPmdTrack.h"
152 #include "AliESDTagCreator.h"
153 #include "AliESDVertex.h"
154 #include "AliESDcascade.h"
155 #include "AliESDfriend.h"
156 #include "AliESDkink.h"
157 #include "AliESDpid.h"
158 #include "AliESDtrack.h"
159 #include "AliESDtrack.h"
160 #include "AliEventInfo.h"
161 #include "AliGRPObject.h"
162 #include "AliGRPRecoParam.h"
163 #include "AliGenEventHeader.h"
164 #include "AliGeomManager.h"
165 #include "AliGlobalQADataMaker.h" 
166 #include "AliHeader.h"
167 #include "AliLog.h"
168 #include "AliMagF.h"
169 #include "AliMultiplicity.h"
170 #include "AliPID.h"
171 #include "AliPlaneEff.h"
172 #include "AliQAv1.h"
173 #include "AliQADataMakerRec.h" 
174 #include "AliQAManager.h"
175 #include "AliRawVEvent.h"
176 #include "AliRawEventHeaderBase.h"
177 #include "AliRawHLTManager.h"
178 #include "AliRawReaderDate.h"
179 #include "AliRawReaderFile.h"
180 #include "AliRawReaderRoot.h"
181 #include "AliRecoInputHandler.h"
182 #include "AliReconstruction.h"
183 #include "AliReconstructor.h"
184 #include "AliRun.h"
185 #include "AliRunInfo.h"
186 #include "AliRunLoader.h"
187 #include "AliSysInfo.h" // memory snapshots
188 #include "AliTrackPointArray.h"
189 #include "AliTracker.h"
190 #include "AliTriggerClass.h"
191 #include "AliTriggerCluster.h"
192 #include "AliTriggerIR.h"
193 #include "AliTriggerConfiguration.h"
194 #include "AliV0vertexer.h"
195 #include "AliVertexer.h"
196 #include "AliTrackleter.h"
197 #include "AliVertexerTracks.h"
198 #include "AliTriggerRunScalers.h"
199 #include "AliCTPTimeParams.h" 
200 #include "AliESDHLTDecision.h"
201 #include "AliTriggerInput.h"
202 #include "AliLHCData.h"
203 #include "AliPoolN.h"
204 #include "AliClonesPool.h"
205 #include "AliPoolsSet.h"
206 #include "ARVersion.h"
207 #include <RVersion.h>
208 #include <unistd.h>
209 #include <sys/resource.h>
210 ClassImp(AliReconstruction)
211
212 //_____________________________________________________________________________
213 const char* AliReconstruction::fgkStopEvFName = "_stopEvent_";
214 const char* AliReconstruction::fgkDetectorName[AliReconstruction::kNDetectors] = {"ITS", "TPC", "TRD", "TOF", "PHOS", "HMPID", "EMCAL", "MUON", "FMD", "ZDC", "PMD", "T0", "VZERO", "ACORDE"
215 // #ifdef MFT_UPGRADE
216 //                                                                                   , "MFT"
217 // #endif 
218                                                                                   , "MFT"    // AU
219                                                                                   , "HLT"
220 };
221
222 //_____________________________________________________________________________
223 AliReconstruction::AliReconstruction(const char* gAliceFilename) :
224   TSelector(),
225   fRunVertexFinder(kTRUE),
226   fRunVertexFinderTracks(kTRUE),
227   fRunHLTTracking(kFALSE),
228   fRunMuonTracking(kFALSE),
229   fRunV0Finder(kTRUE),
230   fRunCascadeFinder(kTRUE),
231   fRunMultFinder(kTRUE),
232   fStopOnError(kTRUE),
233   fWriteAlignmentData(kFALSE),
234   fWriteESDfriend(kFALSE),
235   fFillTriggerESD(kTRUE),
236
237   fCleanESD(kTRUE),
238   fV0DCAmax(3.),
239   fV0CsPmin(0.),
240   fDmax(50.),
241   fZmax(50.),
242
243   fRunLocalReconstruction("ALL"),
244   fRunTracking("ALL"),
245   fFillESD("ALL"),
246   fDeleteRecPoints(""),
247   fDeleteDigits(""),
248   fLoadCDB(""),
249   fUseTrackingErrorsForAlignment(""),
250   fGAliceFileName(gAliceFilename),
251   fRawInput(""),
252   fESDOutput(""),
253   fProofOutputFileName(""),
254   fProofOutputLocation(""),
255   fProofOutputDataset(kFALSE),
256   fProofOutputArchive(""),
257   fEquipIdMap(""),
258   fFirstEvent(0),
259   fLastEvent(-1),
260   fNumberOfEventsPerFile((UInt_t)-1),
261   fFractionFriends(0.04),
262   fOptions(),
263   fLoadAlignFromCDB(kTRUE),
264   fLoadAlignData("ALL"),
265   fUseHLTData(),
266   fRunInfo(NULL),
267   fEventInfo(),
268   fRunScalers(NULL),
269   fCTPTimeParams(NULL),  
270   fCTPTimeAlign(NULL),  
271
272   fRunLoader(NULL),
273   fRawReader(NULL),
274   fParentRawReader(NULL),
275
276   fRecoParam(),
277
278   fSPDTrackleter(NULL),
279
280   fDiamondProfileSPD(NULL),
281   fDiamondProfile(NULL),
282   fDiamondProfileTPC(NULL),
283   fListOfCosmicTriggers(NULL),
284   
285   fGRPData(NULL),
286
287   fAlignObjArray(NULL),
288   fCDBUri(),
289   fQARefUri(),
290   fSpecCDBUri(), 
291   fInitCDBCalled(kFALSE),
292   fFromCDBSnapshot(kFALSE),
293   fSnapshotFileName(""),
294   fSetRunNumberFromDataCalled(kFALSE),
295   fQADetectors("ALL"), 
296   fQATasks("ALL"), 
297   fRunQA(kTRUE),  
298   fRunGlobalQA(kTRUE),
299   fSameQACycle(kFALSE),
300   fInitQACalled(kFALSE), 
301   fWriteQAExpertData(kTRUE), 
302   fRunPlaneEff(kFALSE),
303
304   fesd(NULL),
305   fhltesd(NULL),
306   fesdf(NULL),
307   ffile(NULL),
308   ffileF(NULL),
309   ftree(NULL),
310   ftreeF(NULL),
311   fhlttree(NULL),
312   ftVertexer(NULL),
313   fIsNewRunLoader(kFALSE),
314   fRunAliEVE(kFALSE),
315   fChain(NULL),
316   fNall(0),
317   fNspecie(0),
318   fSspecie(0),
319   fNhighPt(0),
320   fShighPt(0),
321   fUpgradeModule(""),
322   fAnalysisMacro(),
323   fAnalysis(0),
324   fRecoHandler(0),
325   fDeclTriggerClasses(""),
326   fPools(0),
327   fStopped(kFALSE),
328   fMaxRSS(0),
329   fMaxVMEM(0)
330 {
331 // create reconstruction object with default parameters
332   gGeoManager = NULL;
333   
334   for (Int_t iDet = 0; iDet < kNDetectors; iDet++) {
335     fReconstructor[iDet] = NULL;
336     fUpgradeMask[iDet]=kFALSE;
337     fLoader[iDet] = NULL;
338     fTracker[iDet] = NULL;
339   }
340   for (Int_t iDet = 0; iDet < AliQAv1::kNDET; iDet++) {
341     fQACycles[iDet] = 999999 ;
342     fQAWriteExpert[iDet] = kFALSE ; 
343   }
344   fBeamInt[0][0]=fBeamInt[0][1]=fBeamInt[1][0]=fBeamInt[1][1] = -1;
345
346   AliPID pid;
347 }
348
349 //_____________________________________________________________________________
350 AliReconstruction::AliReconstruction(const AliReconstruction& rec) :
351   TSelector(),
352   fRunVertexFinder(rec.fRunVertexFinder),
353   fRunVertexFinderTracks(rec.fRunVertexFinderTracks),
354   fRunHLTTracking(rec.fRunHLTTracking),
355   fRunMuonTracking(rec.fRunMuonTracking),
356   fRunV0Finder(rec.fRunV0Finder),
357   fRunCascadeFinder(rec.fRunCascadeFinder),
358   fRunMultFinder(rec.fRunMultFinder),
359   fStopOnError(rec.fStopOnError),
360   fWriteAlignmentData(rec.fWriteAlignmentData),
361   fWriteESDfriend(rec.fWriteESDfriend),
362   fFillTriggerESD(rec.fFillTriggerESD),
363
364   fCleanESD(rec.fCleanESD),
365   fV0DCAmax(rec.fV0DCAmax),
366   fV0CsPmin(rec.fV0CsPmin),
367   fDmax(rec.fDmax),
368   fZmax(rec.fZmax),
369
370   fRunLocalReconstruction(rec.fRunLocalReconstruction),
371   fRunTracking(rec.fRunTracking),
372   fFillESD(rec.fFillESD),
373   fDeleteRecPoints(""),
374   fDeleteDigits(""),
375   fLoadCDB(rec.fLoadCDB),
376   fUseTrackingErrorsForAlignment(rec.fUseTrackingErrorsForAlignment),
377   fGAliceFileName(rec.fGAliceFileName),
378   fRawInput(rec.fRawInput),
379   fESDOutput(rec.fESDOutput),
380   fProofOutputFileName(rec.fProofOutputFileName),
381   fProofOutputLocation(rec.fProofOutputLocation),
382   fProofOutputDataset(rec.fProofOutputDataset),
383   fProofOutputArchive(rec.fProofOutputArchive),
384   fEquipIdMap(rec.fEquipIdMap),
385   fFirstEvent(rec.fFirstEvent),
386   fLastEvent(rec.fLastEvent),
387   fNumberOfEventsPerFile(rec.fNumberOfEventsPerFile),
388   fFractionFriends(rec.fFractionFriends),
389   fOptions(),
390   fLoadAlignFromCDB(rec.fLoadAlignFromCDB),
391   fLoadAlignData(rec.fLoadAlignData),
392   fUseHLTData(rec.fUseHLTData),
393   fRunInfo(NULL),
394   fEventInfo(),
395   fRunScalers(NULL),
396   fCTPTimeParams(NULL),
397   fCTPTimeAlign(NULL),
398
399   fRunLoader(NULL),
400   fRawReader(NULL),
401   fParentRawReader(NULL),
402
403   fRecoParam(rec.fRecoParam),
404
405   fSPDTrackleter(NULL),
406
407   fDiamondProfileSPD(rec.fDiamondProfileSPD),
408   fDiamondProfile(rec.fDiamondProfile),
409   fDiamondProfileTPC(rec.fDiamondProfileTPC),
410   fListOfCosmicTriggers(NULL),
411   
412   fGRPData(NULL),
413
414   fAlignObjArray(rec.fAlignObjArray),
415   fCDBUri(rec.fCDBUri),
416   fQARefUri(rec.fQARefUri),
417   fSpecCDBUri(), 
418   fInitCDBCalled(rec.fInitCDBCalled),
419   fFromCDBSnapshot(rec.fFromCDBSnapshot),
420   fSnapshotFileName(rec.fSnapshotFileName),
421   fSetRunNumberFromDataCalled(rec.fSetRunNumberFromDataCalled),
422   fQADetectors(rec.fQADetectors), 
423   fQATasks(rec.fQATasks), 
424   fRunQA(rec.fRunQA),  
425   fRunGlobalQA(rec.fRunGlobalQA),
426   fSameQACycle(rec.fSameQACycle),
427   fInitQACalled(rec.fInitQACalled),
428   fWriteQAExpertData(rec.fWriteQAExpertData), 
429   fRunPlaneEff(rec.fRunPlaneEff),
430
431   fesd(NULL),
432   fhltesd(NULL),
433   fesdf(NULL),
434   ffile(NULL),
435   ffileF(NULL),
436   ftree(NULL),
437   ftreeF(NULL),
438   fhlttree(NULL),
439   ftVertexer(NULL),
440   fIsNewRunLoader(rec.fIsNewRunLoader),
441   fRunAliEVE(kFALSE),
442   fChain(NULL),
443   fNall(0),
444   fNspecie(0),
445   fSspecie(0),
446   fNhighPt(0),
447   fShighPt(0),
448   fUpgradeModule(""),
449   fAnalysisMacro(rec.fAnalysisMacro),
450   fAnalysis(0),
451   fRecoHandler(0),
452   fDeclTriggerClasses(rec.fDeclTriggerClasses),
453   fPools(0),
454   fStopped(kFALSE),
455   fMaxRSS(0),
456   fMaxVMEM(0)
457 {
458 // copy constructor
459
460   for (Int_t i = 0; i < rec.fOptions.GetEntriesFast(); i++) {
461     if (rec.fOptions[i]) fOptions.Add(rec.fOptions[i]->Clone());
462   }
463   for (Int_t iDet = 0; iDet < kNDetectors; iDet++) {
464     fReconstructor[iDet] = NULL;
465     fUpgradeMask[iDet] = kFALSE;
466     fLoader[iDet] = NULL;
467     fTracker[iDet] = NULL;
468   }  
469   
470   for (Int_t iDet = 0; iDet < AliQAv1::kNDET; iDet++) {
471     fQACycles[iDet] = rec.fQACycles[iDet];
472     fQAWriteExpert[iDet] = rec.fQAWriteExpert[iDet] ; 
473   }
474
475   for (Int_t i = 0; i < rec.fSpecCDBUri.GetEntriesFast(); i++) {
476     if (rec.fSpecCDBUri[i]) fSpecCDBUri.Add(rec.fSpecCDBUri[i]->Clone());
477   }
478
479   for (int i=2;i--;) for (int j=2;j--;) fBeamInt[i][j] = rec.fBeamInt[i][j];
480
481 }
482
483 //_____________________________________________________________________________
484 AliReconstruction& AliReconstruction::operator = (const AliReconstruction& rec)
485 {
486 // assignment operator
487 // Used in PROOF mode
488 // Be very careful while modifing it!
489 // Simple rules to follow:
490 // for persistent data members - use their assignment operators
491 // for non-persistent ones - do nothing or take the default values from constructor
492 // TSelector members should not be touched
493   if(&rec == this) return *this;
494
495   fRunVertexFinder       = rec.fRunVertexFinder;
496   fRunVertexFinderTracks = rec.fRunVertexFinderTracks;
497   fRunHLTTracking        = rec.fRunHLTTracking;
498   fRunMuonTracking       = rec.fRunMuonTracking;
499   fRunV0Finder           = rec.fRunV0Finder;
500   fRunCascadeFinder      = rec.fRunCascadeFinder;
501   fRunMultFinder         = rec.fRunMultFinder;
502   fStopOnError           = rec.fStopOnError;
503   fWriteAlignmentData    = rec.fWriteAlignmentData;
504   fWriteESDfriend        = rec.fWriteESDfriend;
505   fFillTriggerESD        = rec.fFillTriggerESD;
506
507   fCleanESD  = rec.fCleanESD;
508   fV0DCAmax  = rec.fV0DCAmax;
509   fV0CsPmin  = rec.fV0CsPmin;
510   fDmax      = rec.fDmax;
511   fZmax      = rec.fZmax;
512
513   fRunLocalReconstruction        = rec.fRunLocalReconstruction;
514   fRunTracking                   = rec.fRunTracking;
515   fFillESD                       = rec.fFillESD;
516   fDeleteRecPoints               = rec.fDeleteRecPoints;
517   fDeleteDigits                  = rec.fDeleteDigits;
518   fLoadCDB                       = rec.fLoadCDB;
519   fUseTrackingErrorsForAlignment = rec.fUseTrackingErrorsForAlignment;
520   fGAliceFileName                = rec.fGAliceFileName;
521   fRawInput                      = rec.fRawInput;
522   fESDOutput                     = rec.fESDOutput;
523   fProofOutputFileName           = rec.fProofOutputFileName;
524   fProofOutputLocation           = rec.fProofOutputLocation;
525   fProofOutputDataset            = rec.fProofOutputDataset;
526   fProofOutputArchive            = rec.fProofOutputArchive;
527   fEquipIdMap                    = rec.fEquipIdMap;
528   fFirstEvent                    = rec.fFirstEvent;
529   fLastEvent                     = rec.fLastEvent;
530   fNumberOfEventsPerFile         = rec.fNumberOfEventsPerFile;
531   fFractionFriends               = rec.fFractionFriends;
532
533   for (Int_t i = 0; i < rec.fOptions.GetEntriesFast(); i++) {
534     if (rec.fOptions[i]) fOptions.Add(rec.fOptions[i]->Clone());
535   }
536
537   fLoadAlignFromCDB              = rec.fLoadAlignFromCDB;
538   fLoadAlignData                 = rec.fLoadAlignData;
539   fUseHLTData                    = rec.fUseHLTData;
540
541   delete fRunInfo; fRunInfo = NULL;
542   if (rec.fRunInfo) fRunInfo = new AliRunInfo(*rec.fRunInfo);
543
544   fEventInfo                     = rec.fEventInfo;
545
546   delete fRunScalers; fRunScalers = NULL;
547   if (rec.fRunScalers) fRunScalers = new AliTriggerRunScalers(*rec.fRunScalers); 
548
549   delete fCTPTimeParams; fCTPTimeParams = NULL;
550   if (rec.fCTPTimeParams) fCTPTimeParams = new AliCTPTimeParams(*rec.fCTPTimeParams);
551   delete fCTPTimeAlign; fCTPTimeAlign = NULL;
552   if (rec.fCTPTimeAlign) fCTPTimeAlign = new AliCTPTimeParams(*rec.fCTPTimeAlign);
553
554   fRunLoader       = NULL;
555   fRawReader       = NULL;
556   fParentRawReader = NULL;
557
558   fRecoParam = rec.fRecoParam;
559
560   for (Int_t iDet = 0; iDet < kNDetectors; iDet++) {
561     fUpgradeMask[iDet] = kFALSE;
562     delete fReconstructor[iDet]; fReconstructor[iDet] = NULL;
563     delete fLoader[iDet]; fLoader[iDet] = NULL;
564     delete fTracker[iDet]; fTracker[iDet] = NULL;
565   }
566   
567   for (Int_t iDet = 0; iDet < AliQAv1::kNDET; iDet++) {
568     fQACycles[iDet] = rec.fQACycles[iDet];
569     fQAWriteExpert[iDet] = rec.fQAWriteExpert[iDet] ;
570   } 
571
572   delete fSPDTrackleter; fSPDTrackleter = NULL;
573     
574   delete fDiamondProfileSPD; fDiamondProfileSPD = NULL;
575   if (rec.fDiamondProfileSPD) fDiamondProfileSPD = new AliESDVertex(*rec.fDiamondProfileSPD);
576   delete fDiamondProfile; fDiamondProfile = NULL;
577   if (rec.fDiamondProfile) fDiamondProfile = new AliESDVertex(*rec.fDiamondProfile);
578   delete fDiamondProfileTPC; fDiamondProfileTPC = NULL;
579   if (rec.fDiamondProfileTPC) fDiamondProfileTPC = new AliESDVertex(*rec.fDiamondProfileTPC);
580
581   delete fListOfCosmicTriggers; fListOfCosmicTriggers = NULL;
582   if (rec.fListOfCosmicTriggers) fListOfCosmicTriggers = (THashTable*)((rec.fListOfCosmicTriggers)->Clone());
583
584   delete fGRPData; fGRPData = NULL;
585   //  if (rec.fGRPData) fGRPData = (TMap*)((rec.fGRPData)->Clone());
586   if (rec.fGRPData) fGRPData = (AliGRPObject*)((rec.fGRPData)->Clone());
587
588   delete fAlignObjArray; fAlignObjArray = NULL;
589
590   fCDBUri        = "";
591   fQARefUri      = rec.fQARefUri;
592   fSpecCDBUri.Delete();
593   fInitCDBCalled               = rec.fInitCDBCalled;
594   fFromCDBSnapshot             = rec.fFromCDBSnapshot;
595   fSnapshotFileName            = rec.fSnapshotFileName;
596   fSetRunNumberFromDataCalled  = rec.fSetRunNumberFromDataCalled;
597   fQADetectors                 = rec.fQADetectors;
598   fQATasks                     = rec.fQATasks; 
599   fRunQA                       = rec.fRunQA;  
600   fRunGlobalQA                 = rec.fRunGlobalQA;
601   fSameQACycle                 = rec.fSameQACycle;
602   fInitQACalled                = rec.fInitQACalled;
603   fWriteQAExpertData           = rec.fWriteQAExpertData;
604   fRunPlaneEff                 = rec.fRunPlaneEff;
605   for (int i=2;i--;) for (int j=2;j--;) fBeamInt[i][j] = rec.fBeamInt[i][j];
606   fesd     = NULL;
607   fhltesd  = NULL;
608   fesdf    = NULL;
609   ffile    = NULL;
610   ffileF   = NULL;
611   ftree    = NULL;
612   ftreeF   = NULL;
613   fhlttree = NULL;
614   ftVertexer = NULL;
615   fIsNewRunLoader = rec.fIsNewRunLoader;
616   fRunAliEVE = kFALSE;
617   fChain = NULL;
618   fNall = 0;
619   fNspecie = 0;
620   fSspecie = 0;
621   fNhighPt = 0;
622   fShighPt = 0;
623   fUpgradeModule="";
624   fAnalysisMacro = rec.fAnalysisMacro;
625   fAnalysis = 0;
626   fRecoHandler = 0;
627   fDeclTriggerClasses = rec.fDeclTriggerClasses;
628
629   return *this;
630 }
631
632 //_____________________________________________________________________________
633 AliReconstruction::~AliReconstruction()
634 {
635 // clean up
636
637   CleanUp();
638   if (fListOfCosmicTriggers) {
639     fListOfCosmicTriggers->Delete();
640     delete fListOfCosmicTriggers;
641   }
642   delete fGRPData;
643   delete fRunScalers;
644   delete fCTPTimeParams;
645   delete fCTPTimeAlign;
646   fOptions.Delete();
647   if (fAlignObjArray) {
648     fAlignObjArray->Delete();
649     delete fAlignObjArray;
650   }
651   fSpecCDBUri.Delete();
652   AliCodeTimer::Instance()->Print();
653 }
654
655 //_____________________________________________________________________________
656 void AliReconstruction::InitQA()
657 {
658   //Initialize the QA and start of cycle 
659   AliCodeTimerAuto("",0);
660   
661   if (fInitQACalled) return;
662   fInitQACalled = kTRUE;
663   
664   if (fGRPData) AliQADataMaker::SetCloningRequest( fGRPData->GetQATrigClasses(), fGRPData->GetQACloningRequest());
665
666
667   AliQAManager * qam = AliQAManager::QAManager(AliQAv1::kRECMODE) ; 
668   if (fWriteQAExpertData)
669     qam->SetWriteExpert() ; 
670  
671   if (qam->IsDefaultStorageSet()) {
672     AliWarning("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
673     AliWarning("Default QA reference storage has been already set !");
674     AliWarning(Form("Ignoring the default storage declared in AliReconstruction: %s",fQARefUri.Data()));
675     AliWarning("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
676     fQARefUri = qam->GetDefaultStorage()->GetURI();
677   } else {
678     if (fQARefUri.Length() > 0) {
679         AliDebug(2,"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
680         AliDebug(2, Form("Default QA reference storage is set to: %s", fQARefUri.Data()));
681         AliDebug(2, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
682       } else {
683         fQARefUri="local://$ALICE_ROOT/QAref";
684         AliWarning("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
685         AliWarning("Default QA refeference storage not yet set !!!!");
686         AliWarning(Form("Setting it now to: %s", fQARefUri.Data()));
687         AliWarning("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
688                 
689       }
690     qam->SetDefaultStorage(fQARefUri);
691   }
692   
693   if (fRunQA) {
694   qam->SetActiveDetectors(fQADetectors) ; 
695   for (Int_t det = 0 ; det < AliQAv1::kNDET ; det++) {
696     qam->SetCycleLength(AliQAv1::DETECTORINDEX_t(det), fQACycles[det]) ;  
697     qam->SetWriteExpert(AliQAv1::DETECTORINDEX_t(det)) ;
698   }
699   if (!fRawReader && !fInput && IsInTasks(AliQAv1::kRAWS))
700     fQATasks.ReplaceAll(Form("%d",AliQAv1::kRAWS), "") ;
701   qam->SetTasks(fQATasks) ; 
702   qam->InitQADataMaker(AliCDBManager::Instance()->GetRun()) ; 
703   }
704   if (fRunGlobalQA) {
705     Bool_t sameCycle = kFALSE ;
706     AliQADataMaker *qadm = qam->GetQADataMaker(AliQAv1::kGLOBAL);
707     AliInfo(Form("Initializing the global QA data maker"));
708     if (IsInTasks(AliQAv1::kRECPOINTS)) {
709       qadm->StartOfCycle(AliQAv1::kRECPOINTS, AliCDBManager::Instance()->GetRun(), sameCycle) ; 
710       TObjArray **arr=qadm->Init(AliQAv1::kRECPOINTS);
711       AliTracker::SetResidualsArray(arr);
712       sameCycle = kTRUE ; 
713     }
714     if (IsInTasks(AliQAv1::kESDS)) {
715       qadm->StartOfCycle(AliQAv1::kESDS, AliCDBManager::Instance()->GetRun(), sameCycle) ; 
716       qadm->Init(AliQAv1::kESDS);
717     }
718   }
719     AliSysInfo::AddStamp("InitQA") ; 
720 }
721
722 //_____________________________________________________________________________
723 void AliReconstruction::MergeQA(const char *fileName)
724 {
725   //Initialize the QA and start of cycle 
726   AliCodeTimerAuto("",0) ;
727   AliQAManager::QAManager()->Merge(AliCDBManager::Instance()->GetRun(),fileName) ; 
728   AliSysInfo::AddStamp("MergeQA") ; 
729 }
730   
731 //_____________________________________________________________________________
732 void AliReconstruction::InitCDB()
733 {
734 // activate a default CDB storage
735 // First check if we have any CDB storage set, because it is used 
736 // to retrieve the calibration and alignment constants
737   AliCodeTimerAuto("",0);
738
739   if (fInitCDBCalled) return;
740   fInitCDBCalled = kTRUE;
741
742   AliCDBManager* man = AliCDBManager::Instance();
743   if (man->IsDefaultStorageSet())
744   {
745     AliWarning("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
746     AliWarning("Default CDB storage has been already set !");
747     AliWarning(Form("Ignoring the default storage declared in AliReconstruction: %s",fCDBUri.Data()));
748     AliWarning("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
749     fCDBUri = man->GetDefaultStorage()->GetURI();
750   }
751   else {
752     if (fCDBUri.Length() > 0) 
753     {
754         AliDebug(2,"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
755         AliDebug(2, Form("Default CDB storage is set to: %s", fCDBUri.Data()));
756         AliDebug(2, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
757         man->SetDefaultStorage(fCDBUri);
758     } 
759     else if (!man->GetRaw()){
760         fCDBUri="local://$ALICE_ROOT/OCDB";
761         AliWarning("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
762         AliWarning("Default CDB storage not yet set !!!!");
763         AliWarning(Form("Setting it now to: %s", fCDBUri.Data()));
764         AliWarning("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
765         man->SetDefaultStorage(fCDBUri);
766     }
767     else {    
768         AliWarning("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
769         AliWarning("Default storage will be set after setting the Run Number!!!");
770         AliWarning("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");                    
771     }
772   }
773
774   // Now activate the detector specific CDB storage locations
775   for (Int_t i = 0; i < fSpecCDBUri.GetEntriesFast(); i++) {
776     TObject* obj = fSpecCDBUri[i];
777     if (!obj) continue;
778     AliDebug(2, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
779     AliDebug(2, Form("Specific CDB storage for %s is set to: %s",obj->GetName(),obj->GetTitle()));
780     AliDebug(2, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
781     man->SetSpecificStorage(obj->GetName(), obj->GetTitle());
782   }
783   AliSysInfo::AddStamp("InitCDB");
784 }
785
786 //_____________________________________________________________________________
787 void AliReconstruction::SetDefaultStorage(const char* uri) {
788 // Store the desired default CDB storage location
789 // Activate it later within the Run() method
790
791   fCDBUri = uri;
792
793 }
794
795 //_____________________________________________________________________________
796 void AliReconstruction::SetQARefDefaultStorage(const char* uri) {
797   // Store the desired default CDB storage location
798   // Activate it later within the Run() method
799   
800   fQARefUri = uri;
801   AliQAv1::SetQARefStorage(fQARefUri.Data()) ;
802   
803 }
804 //_____________________________________________________________________________
805 void AliReconstruction::SetSpecificStorage(const char* calibType, const char* uri) {
806 // Store a detector-specific CDB storage location
807 // Activate it later within the Run() method
808
809   AliCDBPath aPath(calibType);
810   if(!aPath.IsValid()){
811         // if calibType is not wildcard but it is a valid detector, add "/*" to make it a valid path
812         for (Int_t iDet = 0; iDet < kNDetectors; iDet++) {
813                 if(!strcmp(calibType, fgkDetectorName[iDet])) {
814                         aPath.SetPath(Form("%s/*", calibType));
815                         AliInfo(Form("Path for specific storage set to %s", aPath.GetPath().Data()));
816                         break;
817                 }
818         }
819         if(!aPath.IsValid()){
820                 AliError(Form("Not a valid path or detector: %s", calibType));
821                 return;
822         }
823   }
824
825 //  // check that calibType refers to a "valid" detector name
826 //  Bool_t isDetector = kFALSE;
827 //  for (Int_t iDet = 0; iDet < kNDetectors; iDet++) {
828 //    TString detName = fgkDetectorName[iDet];
829 //    if(aPath.GetLevel0() == detName) {
830 //      isDetector = kTRUE;
831 //      break;
832 //    }
833 //  }
834 //
835 //  if(!isDetector) {
836 //      AliError(Form("Not a valid detector: %s", aPath.GetLevel0().Data()));
837 //      return;
838 //  }
839
840   TObject* obj = fSpecCDBUri.FindObject(aPath.GetPath().Data());
841   if (obj) fSpecCDBUri.Remove(obj);
842   fSpecCDBUri.Add(new TNamed(aPath.GetPath().Data(), uri));
843
844 }
845
846 //_____________________________________________________________________________
847 Bool_t AliReconstruction::SetRunNumberFromData()
848 {
849   // The method is called in Run() in order
850   // to set a correct run number.
851   // In case of raw data reconstruction the
852   // run number is taken from the raw data header
853
854   if (fSetRunNumberFromDataCalled) return kTRUE;
855   fSetRunNumberFromDataCalled = kTRUE;
856   
857   AliCDBManager* man = AliCDBManager::Instance();
858  
859   if(fRawReader) {
860     if(fRawReader->NextEvent()) {
861       if(man->GetRun() > 0) {
862         AliWarning("Run number is taken from raw-event header! Ignoring settings in AliCDBManager!");
863       } 
864       man->SetRun(fRawReader->GetRunNumber());
865       fRawReader->RewindEvents();
866     }
867     else {
868       if(man->GetRun() > 0) {
869         AliWarning("No raw-data events are found ! Using settings in AliCDBManager !");
870       }
871       else {
872         AliWarning("Neither raw events nor settings in AliCDBManager are found !");
873         return kFALSE;
874       }
875     }
876   }
877   else {
878     AliRunLoader *rl = AliRunLoader::Open(fGAliceFileName.Data());
879     if (!rl) {
880       AliError(Form("No run loader found in file %s", fGAliceFileName.Data()));
881       return kFALSE;
882     }
883     else {
884       rl->LoadHeader();
885       // read run number from gAlice
886       if(rl->GetHeader()) {
887         man->SetRun(rl->GetHeader()->GetRun());
888         rl->UnloadHeader();
889         delete rl;
890       }
891       else {
892         AliError("Neither run-loader header nor RawReader objects are found !");
893         delete rl;
894         return kFALSE;
895       }
896     }
897   }
898
899   man->Print();  
900   
901   return kTRUE;
902 }
903
904 //_____________________________________________________________________________
905 void AliReconstruction::SetCDBLock() {
906   // Set CDB lock: from now on it is forbidden to reset the run number
907   // or the default storage or to activate any further storage!
908   
909   AliCDBManager::Instance()->SetLock(1);
910 }
911
912 //_____________________________________________________________________________
913 void AliReconstruction::MatchUpgradeDetector() {
914   // Translates detector name in a boolean.
915   // The boolean is used in GetReconstructor to load the 
916   // upgrade reconstructor instead of the standard one.
917    for(Int_t iDet = 0; iDet < kNDetectors; iDet++) {
918     if(fUpgradeModule.Contains(fgkDetectorName[iDet])) fUpgradeMask[iDet]=kTRUE;
919    }
920 }
921 //_____________________________________________________________________________
922 Bool_t AliReconstruction::MisalignGeometry(const TString& detectors)
923 {
924   // Read the alignment objects from CDB.
925   // Each detector is supposed to have the
926   // alignment objects in DET/Align/Data CDB path.
927   // All the detector objects are then collected,
928   // sorted by geometry level (starting from ALIC) and
929   // then applied to the TGeo geometry.
930   // Finally an overlaps check is performed.
931
932   // Load alignment data from CDB and fill fAlignObjArray 
933   if(fLoadAlignFromCDB){
934         
935     TString detStr = detectors;
936     TString loadAlObjsListOfDets = "";
937     
938     for (Int_t iDet = 0; iDet < kNDetectors; iDet++) {
939       if(!IsSelected(fgkDetectorName[iDet], detStr)) continue;
940       if(!strcmp(fgkDetectorName[iDet],"HLT")) continue;
941       
942       if(AliGeomManager::GetNalignable(fgkDetectorName[iDet]) != 0)
943       {
944         loadAlObjsListOfDets += fgkDetectorName[iDet];
945         loadAlObjsListOfDets += " ";
946       }
947     } // end loop over detectors
948     
949     if(AliGeomManager::GetNalignable("GRP") != 0)
950       loadAlObjsListOfDets.Prepend("GRP "); //add alignment objects for non-sensitive modules
951     AliGeomManager::ApplyAlignObjsFromCDB(loadAlObjsListOfDets.Data());
952   }else{
953     // Check if the array with alignment objects was
954     // provided by the user. If yes, apply the objects
955     // to the present TGeo geometry
956     if (fAlignObjArray) {
957       if (gGeoManager && gGeoManager->IsClosed()) {
958         if (AliGeomManager::ApplyAlignObjsToGeom(*fAlignObjArray) == kFALSE) {
959           AliError("The misalignment of one or more volumes failed!"
960                    "Compare the list of simulated detectors and the list of detector alignment data!");
961           return kFALSE;
962         }
963       }
964       else {
965         AliError("Can't apply the misalignment! gGeoManager doesn't exist or it is still opened!");
966         return kFALSE;
967       }
968     }
969   }
970   
971   if (fAlignObjArray) {
972     fAlignObjArray->Delete();
973     delete fAlignObjArray; fAlignObjArray=NULL;
974   }
975
976   return kTRUE;
977 }
978
979 //_____________________________________________________________________________
980 void AliReconstruction::SetGAliceFile(const char* fileName)
981 {
982 // set the name of the galice file
983
984   fGAliceFileName = fileName;
985 }
986
987 //_____________________________________________________________________________
988 void AliReconstruction::SetInput(const char* input) 
989 {
990   // In case the input string starts with 'mem://', we run in an online mode
991   // and AliRawReaderDateOnline object is created. In all other cases a raw-data
992   // file is assumed. One can give as an input:
993   // mem://: - events taken from DAQ monitoring libs online
994   //  or
995   // mem://<filename> - emulation of the above mode (via DATE monitoring libs)
996   if (input) fRawInput = input;
997 }
998
999 //_____________________________________________________________________________
1000 void AliReconstruction::SetOutput(const char* output) 
1001 {
1002   // Set the output ESD filename
1003   // 'output' is a normalt ROOT url
1004   // The method is used in case of raw-data reco with PROOF
1005   if (output) fESDOutput = output;
1006 }
1007
1008 //_____________________________________________________________________________
1009 void AliReconstruction::SetOption(const char* detector, const char* option)
1010 {
1011 // set options for the reconstruction of a detector
1012
1013   TObject* obj = fOptions.FindObject(detector);
1014   if (obj) fOptions.Remove(obj);
1015   fOptions.Add(new TNamed(detector, option));
1016 }
1017
1018 //_____________________________________________________________________________
1019 void AliReconstruction::SetRecoParam(const char* detector, AliDetectorRecoParam *par)
1020 {
1021   // Set custom reconstruction parameters for a given detector
1022   // Single set of parameters for all the events
1023
1024   // First check if the reco-params are global
1025   if(!strcmp(detector, "GRP")) {
1026     par->SetAsDefault();
1027     fRecoParam.AddDetRecoParam(kNDetectors,par);
1028     return;
1029   }
1030
1031   for (Int_t iDet = 0; iDet < kNDetectors; iDet++) {
1032     if(!strcmp(detector, fgkDetectorName[iDet])) {
1033       par->SetAsDefault();
1034       fRecoParam.AddDetRecoParam(iDet,par);
1035       break;
1036     }
1037   }
1038
1039 }
1040
1041 //_____________________________________________________________________________
1042 Bool_t AliReconstruction::InitGRP() {
1043   //------------------------------------
1044   // Initialization of the GRP entry 
1045   //------------------------------------
1046   AliCDBEntry* entry = AliCDBManager::Instance()->Get("GRP/GRP/Data");
1047
1048   if (entry) {
1049
1050     TMap* m = dynamic_cast<TMap*>(entry->GetObject());  // old GRP entry
1051
1052     if (m) {
1053        AliInfo("Found a TMap in GRP/GRP/Data, converting it into an AliGRPObject");
1054        m->Print();
1055        fGRPData = new AliGRPObject();
1056        fGRPData->ReadValuesFromMap(m);
1057     }
1058
1059     else {
1060        AliInfo("Found an AliGRPObject in GRP/GRP/Data, reading it");
1061        fGRPData = dynamic_cast<AliGRPObject*>(entry->GetObject());  // new GRP entry
1062        entry->SetOwner(0);
1063     }
1064
1065     //    FIX ME: The unloading of GRP entry is temporarily disabled
1066     //    because ZDC and VZERO are using it in order to initialize
1067     //    their reconstructor objects. In the future one has to think
1068     //    of propagating AliRunInfo to the reconstructors.
1069     //    AliCDBManager::Instance()->UnloadFromCache("GRP/GRP/Data");
1070   }
1071
1072   if (!fGRPData) {
1073      AliError("No GRP entry found in OCDB!");
1074      return kFALSE;
1075   }
1076
1077   TString lhcState = fGRPData->GetLHCState();
1078   if (lhcState==AliGRPObject::GetInvalidString()) {
1079     AliError("GRP/GRP/Data entry:  missing value for the LHC state ! Using UNKNOWN");
1080     lhcState = "UNKNOWN";
1081   }
1082
1083   TString beamType = fGRPData->GetBeamType();
1084   if (beamType==AliGRPObject::GetInvalidString()) {
1085     AliError("GRP/GRP/Data entry:  missing value for the beam type ! Using UNKNOWN");
1086     beamType = "UNKNOWN";
1087   }
1088
1089   Float_t beamEnergy = fGRPData->GetBeamEnergy();
1090   if (beamEnergy==AliGRPObject::GetInvalidFloat()) {
1091     AliError("GRP/GRP/Data entry:  missing value for the beam energy ! Using 0");
1092     beamEnergy = 0;
1093   }
1094
1095   TString runType = fGRPData->GetRunType();
1096   if (runType==AliGRPObject::GetInvalidString()) {
1097     AliError("GRP/GRP/Data entry:  missing value for the run type ! Using UNKNOWN");
1098     runType = "UNKNOWN";
1099   }
1100
1101   Int_t activeDetectors = fGRPData->GetDetectorMask();
1102   if (activeDetectors==AliGRPObject::GetInvalidUInt()) {
1103     AliError("GRP/GRP/Data entry:  missing value for the detector mask ! Using 1074790399");
1104     activeDetectors = 1074790399;
1105   }
1106   AliDebug(1, Form("activeDetectors = %d", activeDetectors));
1107
1108   fRunInfo = new AliRunInfo(lhcState, beamType, beamEnergy, runType, activeDetectors);
1109   fRunInfo->Dump();
1110
1111
1112   // Process the list of active detectors
1113   if (activeDetectors) {
1114     UInt_t detMask = activeDetectors;
1115     AliDebug(1, Form("Detector List = %s", fRunLocalReconstruction.Data()));
1116     fRunLocalReconstruction = MatchDetectorList(fRunLocalReconstruction,detMask);
1117     AliDebug(1, Form("Detector List = %s", fRunLocalReconstruction.Data()));
1118     fRunTracking = MatchDetectorList(fRunTracking,detMask);
1119     fFillESD = MatchDetectorList(fFillESD,detMask);
1120     fQADetectors = MatchDetectorList(fQADetectors,detMask);
1121     fDeleteRecPoints = MatchDetectorList(fDeleteRecPoints,detMask);
1122     fDeleteDigits    = MatchDetectorList(fDeleteDigits,detMask);
1123     fLoadCDB.Form("%s %s %s %s",
1124                   fRunLocalReconstruction.Data(),
1125                   fRunTracking.Data(),
1126                   fFillESD.Data(),
1127                   fQADetectors.Data());
1128     fLoadCDB = MatchDetectorList(fLoadCDB,detMask);
1129     if (!((detMask >> AliDAQ::DetectorID("ITSSPD")) & 0x1) &&
1130         !((detMask >> AliDAQ::DetectorID("ITSSDD")) & 0x1) &&
1131         !((detMask >> AliDAQ::DetectorID("ITSSSD")) & 0x1) ) {
1132       // switch off the vertexer
1133       AliInfo("SPD,SDD,SSD is not in the list of active detectors. Vertexer and Trackleter are switched off.");
1134       fRunVertexFinder = kFALSE;
1135       fRunMultFinder = kFALSE;
1136     }
1137     if (!((detMask >> AliDAQ::DetectorID("TRG")) & 0x1)) {
1138       // switch off the reading of CTP raw-data payload
1139       if (fFillTriggerESD) {
1140         AliInfo("CTP is not in the list of active detectors. CTP data reading switched off.");
1141         fFillTriggerESD = kFALSE;
1142       }
1143     }
1144   }
1145
1146   AliInfo("===================================================================================");
1147   AliInfo(Form("Running local reconstruction for detectors: %s",fRunLocalReconstruction.Data()));
1148   AliInfo(Form("Running tracking for detectors: %s",fRunTracking.Data()));
1149   AliInfo(Form("Filling ESD for detectors: %s",fFillESD.Data()));
1150   AliInfo(Form("Quality assurance is active for detectors: %s",fQADetectors.Data()));
1151   AliInfo(Form("CDB and reconstruction parameters are loaded for detectors: %s",fLoadCDB.Data()));
1152   AliInfo("===================================================================================");
1153
1154   //*** Dealing with the magnetic field map
1155   if ( TGeoGlobalMagField::Instance()->IsLocked() ) {
1156     if (TGeoGlobalMagField::Instance()->GetField()->TestBit(AliMagF::kOverrideGRP)) {
1157       AliInfo("ExpertMode!!! GRP information will be ignored !");
1158       AliInfo("ExpertMode!!! Running with the externally locked B field !");
1159     }
1160     else {
1161       AliInfo("Destroying existing B field instance!");
1162       delete TGeoGlobalMagField::Instance();
1163     }    
1164   }
1165   if ( !TGeoGlobalMagField::Instance()->IsLocked() ) {
1166     // Construct the field map out of the information retrieved from GRP.
1167     Bool_t ok = kTRUE;
1168     // L3
1169     Float_t l3Current = fGRPData->GetL3Current((AliGRPObject::Stats)0);
1170     if (l3Current == AliGRPObject::GetInvalidFloat()) {
1171       AliError("GRP/GRP/Data entry:  missing value for the L3 current !");
1172       ok = kFALSE;
1173     }
1174     
1175     Char_t l3Polarity = fGRPData->GetL3Polarity();
1176     if (l3Polarity == AliGRPObject::GetInvalidChar()) {
1177       AliError("GRP/GRP/Data entry:  missing value for the L3 polarity !");
1178       ok = kFALSE;
1179     }
1180
1181     // Dipole
1182     Float_t diCurrent = fGRPData->GetDipoleCurrent((AliGRPObject::Stats)0);
1183     if (diCurrent == AliGRPObject::GetInvalidFloat()) {
1184       AliError("GRP/GRP/Data entry:  missing value for the dipole current !");
1185       ok = kFALSE;
1186     }
1187
1188     Char_t diPolarity = fGRPData->GetDipolePolarity();
1189     if (diPolarity == AliGRPObject::GetInvalidChar()) {
1190       AliError("GRP/GRP/Data entry:  missing value for the dipole polarity !");
1191       ok = kFALSE;
1192     }
1193
1194     // read special bits for the polarity convention and map type
1195     Int_t  polConvention = fGRPData->IsPolarityConventionLHC() ? AliMagF::kConvLHC : AliMagF::kConvDCS2008;
1196     Bool_t uniformB = fGRPData->IsUniformBMap();
1197
1198     if (ok) { 
1199       AliMagF* fld = AliMagF::CreateFieldMap(TMath::Abs(l3Current) * (l3Polarity ? -1:1), 
1200                                              TMath::Abs(diCurrent) * (diPolarity ? -1:1), 
1201                                              polConvention,uniformB,beamEnergy, beamType.Data());
1202       if (fld) {
1203         TGeoGlobalMagField::Instance()->SetField( fld );
1204         TGeoGlobalMagField::Instance()->Lock();
1205         AliInfo("Running with the B field constructed out of GRP !");
1206       }
1207       else AliFatal("Failed to create a B field map !");
1208     }
1209     else AliFatal("B field is neither set nor constructed from GRP ! Exitig...");
1210   }
1211   
1212   //*** Get the diamond profiles from OCDB
1213   entry = AliCDBManager::Instance()->Get("GRP/Calib/MeanVertexSPD");
1214   if (entry) {
1215     fDiamondProfileSPD = dynamic_cast<AliESDVertex*> (entry->GetObject());  
1216   } else {
1217      AliError("No SPD diamond profile found in OCDB!");
1218   }
1219
1220   entry = AliCDBManager::Instance()->Get("GRP/Calib/MeanVertex");
1221   if (entry) {
1222     fDiamondProfile = dynamic_cast<AliESDVertex*> (entry->GetObject());  
1223   } else {
1224      AliError("No diamond profile found in OCDB!");
1225   }
1226
1227   entry = AliCDBManager::Instance()->Get("GRP/Calib/MeanVertexTPC");
1228   if (entry) {
1229     fDiamondProfileTPC = dynamic_cast<AliESDVertex*> (entry->GetObject());  
1230   } else {
1231      AliError("No TPC diamond profile found in OCDB!");
1232   }
1233
1234   entry = AliCDBManager::Instance()->Get("GRP/Calib/CosmicTriggers");
1235   if (entry) {
1236     fListOfCosmicTriggers = dynamic_cast<THashTable*>(entry->GetObject());
1237     entry->SetOwner(0);
1238   }
1239
1240   if (!fListOfCosmicTriggers) {
1241     AliWarning("Can not get list of cosmic triggers from OCDB! Cosmic event specie will be effectively disabled!");
1242   }
1243
1244   return kTRUE;
1245
1246
1247 //_____________________________________________________________________________
1248 Bool_t AliReconstruction::LoadCDB()
1249 {
1250   // Load CDB entries for all active detectors.
1251   // By default we load all the entries in <det>/Calib
1252   // folder.
1253
1254   AliCodeTimerAuto("",0);
1255
1256   AliCDBManager::Instance()->Get("GRP/CTP/Config");
1257
1258   AliCDBManager::Instance()->Get("GRP/Calib/LHCClockPhase");
1259
1260   TString detStr = fLoadCDB;
1261   for (Int_t iDet = 0; iDet < kNDetectors; iDet++) {
1262     if (!IsSelected(fgkDetectorName[iDet], detStr)) continue;
1263     AliCDBManager::Instance()->GetAll(Form("%s/Calib/*",fgkDetectorName[iDet]));
1264     AliCDBManager::Instance()->GetAll(Form("%s/Trigger/*",fgkDetectorName[iDet]));
1265   }
1266
1267   // Temporary fix - one has to define the correct policy in order
1268   // to load the trigger OCDB entries only for the detectors that
1269   // in the trigger or that are needed in order to put correct
1270   // information in ESD
1271   AliCDBManager::Instance()->GetAll("TRIGGER/*/*");
1272
1273   return kTRUE;
1274 }
1275 //_____________________________________________________________________________
1276 Bool_t AliReconstruction::LoadTriggerScalersCDB()
1277 {
1278   // Load CTP scalers from OCDB.
1279   // The scalers are checked for consistency.
1280
1281   AliCodeTimerAuto("",0);
1282
1283   AliCDBEntry* entry = AliCDBManager::Instance()->Get("GRP/CTP/Scalers");
1284
1285   if (entry) { 
1286    
1287        AliInfo("Found an AliTriggerRunScalers in GRP/CTP/Scalers, reading it");
1288        fRunScalers = dynamic_cast<AliTriggerRunScalers*> (entry->GetObject());
1289        entry->SetOwner(0);
1290        if (fRunScalers && (fRunScalers->CorrectScalersOverflow() == 0)) AliInfo("32bit Trigger counters corrected for overflow");
1291
1292   }
1293   return kTRUE;
1294 }
1295 //_____________________________________________________________________________
1296 Bool_t AliReconstruction::LoadCTPTimeParamsCDB()
1297 {
1298   // Load CTP timing information (alignment)
1299   // from OCDB.
1300
1301   AliCDBEntry* entry = AliCDBManager::Instance()->Get("GRP/CTP/CTPtiming");
1302   if (!entry) return kFALSE;
1303
1304   AliInfo("Found an AliCTPTimeParams in GRP/CTP/CTPtiming, reading it");
1305   fCTPTimeParams = dynamic_cast<AliCTPTimeParams*> (entry->GetObject());
1306   entry->SetOwner(0);
1307
1308   AliCDBEntry* entry2 = AliCDBManager::Instance()->Get("GRP/CTP/TimeAlign");
1309   if (!entry2) return kFALSE;
1310
1311   AliInfo("Found an AliCTPTimeParams in GRP/CTP/TimeAlign, reading it");
1312   fCTPTimeAlign = dynamic_cast<AliCTPTimeParams*> (entry2->GetObject());
1313   entry2->SetOwner(0);
1314
1315   return kTRUE;
1316 }
1317
1318 //_____________________________________________________________________________
1319 Bool_t AliReconstruction::ReadIntensityInfoCDB()
1320 {
1321   // Load LHC DIP data
1322   AliCDBEntry* entry    = AliCDBManager::Instance()->Get("GRP/GRP/LHCData");
1323   AliCDBEntry* entryCTP = AliCDBManager::Instance()->Get("GRP/CTP/Config");
1324   //
1325   if (!entry || !entryCTP) {
1326     AliError(Form("Failed to extract CDB objects GRP/GRP/LHCData: %p or GRP/CTP/Config: %p",entry,entryCTP));
1327     return kFALSE;
1328   }
1329   // extract BC masks
1330   enum {kA,kB,kC,kE,kNMasks};
1331   AliTriggerConfiguration* conf = (AliTriggerConfiguration*)entryCTP->GetObject();
1332   const TObjArray& clArr = conf->GetClasses();
1333   TObjArray masks(kNMasks);
1334   TIter next(&clArr);
1335   AliTriggerClass* trClass = 0;
1336   int nFound = 0;
1337   masks.SetOwner(kFALSE);
1338   //
1339   while ( (trClass=(AliTriggerClass*)next()) ) {
1340     TString trName = trClass->GetName();
1341     int ind = trName.Index("-"); // prefix in front of A,B,C,E
1342     if (ind<1) continue;   // anomaly
1343     //
1344     trName = trName.Data() + ind;
1345     AliTriggerBCMask* bcMask = trClass->GetBCMask();
1346     if (!bcMask) continue;
1347     UInt_t which = 0;
1348     if      (trName.BeginsWith("-A-"))  which |= 0x1<<kA;
1349     else if (trName.BeginsWith("-B-"))  which |= 0x1<<kB;
1350     else if (trName.BeginsWith("-C-"))  which |= 0x1<<kC;
1351     else if (trName.BeginsWith("-E-"))  which |= 0x1<<kE;
1352     else if (trName.BeginsWith("-AC-")) which |= (0x1<<kA) | (0x1<<kC);
1353     else if (trName.BeginsWith("-CA-")) which |= (0x1<<kA) | (0x1<<kC);
1354     else { AliWarning(Form("Unknown trigger type %s\n",trClass->GetName())); continue;}
1355     //
1356     for (int ip=kNMasks;ip--;) {
1357       if ( !(which&(0x1<<ip)) || masks[ip] ) continue; // does not match or already done
1358       masks[ip] = (TObject*)bcMask;
1359       nFound++;
1360     }
1361     if (nFound==kNMasks) break;
1362   }  
1363   //  
1364   AliInfo("Reading mean bunch intensities from GRP/GRP/LHCData");
1365   AliLHCData* dipData = dynamic_cast<AliLHCData*> (entry->GetObject());
1366   //
1367   for (int ib=2;ib--;) {
1368     double intI,intNI;
1369     if (dipData && (dipData->GetMeanIntensity(ib,intI,intNI,&masks)>=0)) {
1370       fBeamInt[ib][0] = intI;
1371       fBeamInt[ib][1] = intNI;  
1372       AliInfo(Form("Mean intensity for beam %d: Interacting:%.2e Non-Interacting:%.2e",ib,intI,intNI));
1373     }
1374   }
1375   return kTRUE;
1376   //
1377 }
1378
1379
1380 //_____________________________________________________________________________
1381 Bool_t AliReconstruction::Run(const char* input)
1382 {
1383   // Run Run Run
1384   AliCodeTimerAuto("",0);
1385
1386   InitRun(input);
1387   if (GetAbort() != TSelector::kContinue) return kFALSE;
1388
1389   TChain *chain = NULL;
1390   if (fRawReader && (chain = fRawReader->GetChain())) {
1391     Long64_t nEntries = (fLastEvent < 0) ? (TChain::kBigNumber) : (fLastEvent - fFirstEvent + 1);
1392     // Proof mode
1393     if (gProof) {
1394       // Temporary fix for long raw-data runs (until socket timeout handling in PROOF is revised)
1395       gProof->Exec("gEnv->SetValue(\"Proof.SocketActivityTimeout\",-1)", kTRUE);
1396
1397       if (gGrid)
1398         gProof->Exec("TGrid::Connect(\"alien://\")",kTRUE);
1399
1400       TMessage::EnableSchemaEvolutionForAll(kTRUE);
1401       gProof->Exec("TMessage::EnableSchemaEvolutionForAll(kTRUE)",kTRUE);
1402
1403       gProof->AddInput(this);
1404
1405       if (!ParseOutput()) return kFALSE;
1406
1407       gProof->SetParameter("PROOF_MaxSlavesPerNode", 9999);
1408       chain->SetProof();
1409       chain->Process("AliReconstruction","",nEntries,fFirstEvent);
1410     }
1411     else {
1412       chain->Process(this,"",nEntries,fFirstEvent);
1413     }
1414   }
1415   else {
1416     Begin(NULL);
1417     if (GetAbort() != TSelector::kContinue) return kFALSE;
1418     SlaveBegin(NULL);
1419     if (GetAbort() != TSelector::kContinue) return kFALSE;
1420     //******* The loop over events
1421     AliInfo("Starting looping over events");
1422     Int_t iEvent = 0;
1423     while ((iEvent < fRunLoader->GetNumberOfEvents()) ||
1424            (fRawReader && fRawReader->NextEvent())) {
1425       //
1426       // check if process has enough resources 
1427       if (!HasEnoughResources(iEvent)) break;
1428       if (!ProcessEvent(iEvent)) {
1429         Abort("ProcessEvent",TSelector::kAbortFile);
1430         return kFALSE;
1431       }
1432       iEvent++;
1433     }
1434     SlaveTerminate();
1435     if (GetAbort() != TSelector::kContinue) return kFALSE;
1436     Terminate();
1437     if (GetAbort() != TSelector::kContinue) return kFALSE;
1438   }
1439
1440   return kTRUE;
1441 }
1442
1443 //_____________________________________________________________________________
1444 void AliReconstruction::InitRawReader(const char* input)
1445 {
1446   // Init raw-reader and
1447   // set the input in case of raw data
1448
1449   AliCodeTimerAuto("",0);
1450
1451   if (input) fRawInput = input;
1452   fRawReader = AliRawReader::Create(fRawInput.Data());
1453   if (!fRawReader) {
1454     if (fRawInput.IsNull()) {
1455       AliInfo("Reconstruction will run over digits");
1456     }
1457     else {
1458       AliFatal("Can not create raw-data reader ! Exiting..."); 
1459     }
1460   }
1461
1462   if (!fEquipIdMap.IsNull() && fRawReader)
1463     fRawReader->LoadEquipmentIdsMap(fEquipIdMap);
1464
1465   if (!fUseHLTData.IsNull()) {
1466     // create the RawReaderHLT which performs redirection of HLT input data for
1467     // the specified detectors
1468     AliRawReader* pRawReader=AliRawHLTManager::CreateRawReaderHLT(fRawReader, fUseHLTData.Data());
1469     if (pRawReader) {
1470       fParentRawReader=fRawReader;
1471       fRawReader=pRawReader;
1472     } else {
1473       AliError(Form("can not create Raw Reader for HLT input %s", fUseHLTData.Data()));
1474     }
1475   }
1476   AliSysInfo::AddStamp("CreateRawReader");
1477 }
1478
1479 //_____________________________________________________________________________
1480 void AliReconstruction::InitRun(const char* input)
1481 {
1482   // Initialization of raw-reader,
1483   // run number, CDB etc.
1484   AliCodeTimerAuto("",0);
1485   AliSysInfo::AddStamp("Start");
1486
1487   // Initialize raw-reader if any
1488   InitRawReader(input);
1489
1490   // Initialize the CDB storage
1491   InitCDB();
1492
1493   // Set run number in CDBManager (if it is not already set by the user)
1494   if (!SetRunNumberFromData()) {
1495     Abort("SetRunNumberFromData", TSelector::kAbortProcess);
1496     return;
1497   }
1498
1499   // Set CDB lock: from now on it is forbidden to reset the run number
1500   // or the default storage or to activate any further storage!
1501   SetCDBLock();
1502   
1503 }
1504
1505 //_____________________________________________________________________________
1506 void AliReconstruction::Begin(TTree *)
1507 {
1508   // Initialize AlReconstruction before
1509   // going into the event loop
1510   // Should follow the TSelector convention
1511   // i.e. initialize only the object on the client side
1512   AliCodeTimerAuto("",0);
1513
1514   AliReconstruction *reco = NULL;
1515   if (fInput) {
1516     if ((reco = (AliReconstruction*)fInput->FindObject("AliReconstruction"))) {
1517       *this = *reco;
1518     }
1519     AliSysInfo::AddStamp("ReadInputInBegin");
1520   }
1521
1522   // Import ideal TGeo geometry and apply misalignment
1523   if (!gGeoManager) {
1524     TString geom(gSystem->DirName(fGAliceFileName));
1525     geom += "/geometry.root";
1526     AliGeomManager::LoadGeometry(geom.Data());
1527     if (!gGeoManager) {
1528       Abort("LoadGeometry", TSelector::kAbortProcess);
1529       return;
1530     }
1531     AliSysInfo::AddStamp("LoadGeom");
1532     TString detsToCheck=fRunLocalReconstruction;
1533     if(!AliGeomManager::CheckSymNamesLUT(detsToCheck.Data())) {
1534       Abort("CheckSymNamesLUT", TSelector::kAbortProcess);
1535       return;
1536     }
1537     AliSysInfo::AddStamp("CheckGeom");
1538   }
1539
1540   Bool_t loadedFromSnapshot=kFALSE;
1541   Bool_t toCDBSnapshot=kFALSE;
1542   TString snapshotFileOut(""); // we could use fSnapshotFileName if we are not interested
1543   // in reading from and writing to a snapshot file at the same time
1544   if(TString(gSystem->Getenv("OCDB_SNAPSHOT_CREATE")) == TString("kTRUE")){
1545       toCDBSnapshot=kTRUE;
1546       //fFromCDBSnapshot=kFALSE;
1547       TString snapshotFile(gSystem->Getenv("OCDB_SNAPSHOT_FILENAME"));
1548       if(!(snapshotFile.IsNull() || snapshotFile.IsWhitespace()))
1549           snapshotFileOut = snapshotFile;
1550       else
1551           snapshotFileOut="OCDB.root";
1552   }
1553   if(fFromCDBSnapshot){
1554       AliDebug(2,"Initializing from a CDB snapshot");
1555       loadedFromSnapshot = AliCDBManager::Instance()->InitFromSnapshot(fSnapshotFileName.Data());
1556   }
1557
1558   if (!MisalignGeometry(fLoadAlignData)) {
1559     Abort("MisalignGeometry", TSelector::kAbortProcess);
1560     return;
1561   }
1562   AliCDBManager::Instance()->UnloadFromCache("GRP/Geometry/Data");
1563   if(!toCDBSnapshot) AliCDBManager::Instance()->UnloadFromCache("*/Align/*");
1564   AliSysInfo::AddStamp("MisalignGeom");
1565
1566   if (!InitGRP()) {
1567     Abort("InitGRP", TSelector::kAbortProcess);
1568     return;
1569   }
1570   AliSysInfo::AddStamp("InitGRP");
1571   if(!toCDBSnapshot) AliCDBManager::Instance()->UnloadFromCache("GRP/Calib/CosmicTriggers");
1572
1573   if(!loadedFromSnapshot){
1574       if (!LoadCDB()) {
1575           Abort("LoadCDB", TSelector::kAbortProcess);
1576           return;
1577       }
1578       AliSysInfo::AddStamp("LoadCDB");
1579   }
1580
1581   if (!LoadTriggerScalersCDB()) {
1582     Abort("LoadTriggerScalersCDB", TSelector::kAbortProcess);
1583     return;
1584   }
1585   AliSysInfo::AddStamp("LoadTriggerScalersCDB");
1586
1587   if (!LoadCTPTimeParamsCDB()) {
1588     Abort("LoadCTPTimeParamsCDB", TSelector::kAbortProcess);
1589     return;
1590   }
1591   AliSysInfo::AddStamp("LoadCTPTimeParamsCDB");
1592
1593   if (!ReadIntensityInfoCDB()) {
1594     Abort("ReadIntensityInfoCDB", TSelector::kAbortProcess);
1595     return;
1596   }
1597   AliSysInfo::AddStamp("ReadIntensityInfoCDB");
1598
1599   // Read the reconstruction parameters from OCDB
1600   if (!InitRecoParams()) {
1601     AliWarning("Not all detectors have correct RecoParam objects initialized");
1602   }
1603   AliSysInfo::AddStamp("InitRecoParams");
1604
1605   if(toCDBSnapshot)
1606       AliCDBManager::Instance()->DumpToSnapshotFile(snapshotFileOut.Data());
1607   AliCDBManager::Instance()->UnloadFromCache("*/Align/*");
1608   AliCDBManager::Instance()->UnloadFromCache("GRP/Calib/CosmicTriggers");
1609
1610   if (fInput && gProof) {
1611     if (reco) *reco = *this;
1612
1613     gGeoManager->SetName("Geometry");
1614     gProof->AddInputData(gGeoManager,kTRUE);
1615     gGeoManager = NULL;
1616     gProof->AddInputData(const_cast<TMap*>(AliCDBManager::Instance()->GetEntryCache()),kTRUE);
1617     fInput->Add(new TParameter<Int_t>("RunNumber",AliCDBManager::Instance()->GetRun()));
1618     AliMagF *magFieldMap = (AliMagF*)TGeoGlobalMagField::Instance()->GetField();
1619     magFieldMap->SetName("MagneticFieldMap");
1620     gProof->AddInputData(magFieldMap,kTRUE);
1621     if (fAnalysis) {
1622       fAnalysis->SetName("Analysis");
1623       gProof->AddInputData(fAnalysis,kTRUE);
1624     }  
1625   }
1626
1627 }
1628
1629 //_____________________________________________________________________________
1630 void AliReconstruction::SlaveBegin(TTree*)
1631 {
1632   // Initialization related to run-loader,
1633   // vertexer, trackers, recontructors
1634   // In proof mode it is executed on the slave
1635   AliCodeTimerAuto("",0);
1636
1637   TProofOutputFile *outProofFile = NULL;
1638   if (fInput) {
1639     if (AliDebugLevel() > 0) fInput->Print();
1640     if (AliDebugLevel() > 10) fInput->Dump();
1641     if (AliReconstruction *reco = (AliReconstruction*)fInput->FindObject("AliReconstruction")) {
1642       *this = *reco;
1643     }
1644     if (TGeoManager *tgeo = (TGeoManager*)fInput->FindObject("Geometry")) {
1645       gGeoManager = tgeo;
1646       AliGeomManager::SetGeometry(tgeo);
1647     }
1648     if (TMap *entryCache = (TMap*)fInput->FindObject("CDBEntryCache")) {
1649       Int_t runNumber = -1;
1650       if (TProof::GetParameter(fInput,"RunNumber",runNumber) == 0) {
1651         AliCDBManager *man = AliCDBManager::Instance(entryCache,runNumber);
1652         man->SetCacheFlag(kTRUE);
1653         man->SetLock(kTRUE);
1654         man->Print();
1655       }
1656     }
1657     if (AliMagF *map = (AliMagF*)fInput->FindObject("MagneticFieldMap")) {
1658       AliMagF *newMap = new AliMagF(*map);
1659       if (!newMap->LoadParameterization()) {
1660         Abort("AliMagF::LoadParameterization", TSelector::kAbortProcess);
1661         return;
1662       }
1663       TGeoGlobalMagField::Instance()->SetField(newMap);
1664       TGeoGlobalMagField::Instance()->Lock();
1665     }
1666     if (!fAnalysis) {
1667        // Attempt to get the analysis manager from the input list
1668        fAnalysis = (AliAnalysisManager*)fInput->FindObject("Analysis");
1669        if (fAnalysis) AliInfo("==== Analysis manager retrieved from input list ====");
1670     }   
1671     if (TNamed *outputFileName = (TNamed*)fInput->FindObject("PROOF_OUTPUTFILE"))
1672       fProofOutputFileName = outputFileName->GetTitle();
1673     if (TNamed *outputLocation = (TNamed*)fInput->FindObject("PROOF_OUTPUTFILE_LOCATION"))
1674       fProofOutputLocation = outputLocation->GetTitle();
1675     if (fInput->FindObject("PROOF_OUTPUTFILE_DATASET"))
1676       fProofOutputDataset = kTRUE;
1677     if (TNamed *archiveList = (TNamed*)fInput->FindObject("PROOF_OUTPUTFILE_ARCHIVE"))
1678       fProofOutputArchive = archiveList->GetTitle();
1679     if (!fProofOutputFileName.IsNull() &&
1680         !fProofOutputLocation.IsNull() &&
1681         fProofOutputArchive.IsNull()) {
1682       if (!fProofOutputDataset) {
1683         outProofFile = new TProofOutputFile(fProofOutputFileName.Data(),"M");
1684         outProofFile->SetOutputFileName(Form("%s%s",fProofOutputLocation.Data(),fProofOutputFileName.Data()));
1685       }
1686       else {
1687         outProofFile = new TProofOutputFile(fProofOutputFileName.Data(),"DROV",fProofOutputLocation.Data());
1688       }
1689       if (AliDebugLevel() > 0) outProofFile->Dump();
1690       fOutput->Add(outProofFile);
1691     }
1692     AliSysInfo::AddStamp("ReadInputInSlaveBegin");
1693   }
1694   // Check if analysis was requested in the reconstruction event loop
1695   if (!fAnalysis) {
1696     // Attempt to connect in-memory singleton
1697     fAnalysis = AliAnalysisManager::GetAnalysisManager();
1698     if (fAnalysis) AliInfo(Form("==== Analysis manager <%s> found in memory ====", fAnalysis->GetName()));
1699     // Check if an analysis macro was specified
1700     if (!fAnalysis && !fAnalysisMacro.IsNull()) {
1701       // Run specified analysis macro
1702       gROOT->ProcessLine(Form(".x %s",fAnalysisMacro.Data()));
1703       fAnalysis = AliAnalysisManager::GetAnalysisManager();
1704       if (!fAnalysis) AliError(Form("No analysis manager produced by analysis macro %s", fAnalysisMacro.Data()));
1705       else AliInfo(Form("==== Analysis manager <%s> produced by analysis macro <%s> ====", 
1706                         fAnalysis->GetName(), fAnalysisMacro.Data()));
1707     }
1708   }
1709   
1710   // get the run loader
1711   if (!InitRunLoader()) {
1712     Abort("InitRunLoader", TSelector::kAbortProcess);
1713     return;
1714   }
1715   AliSysInfo::AddStamp("LoadLoader");
1716  
1717   ftVertexer = new AliVertexerTracks(AliTracker::GetBz());
1718
1719   // get trackers
1720   if (!fRunTracking.IsNull() && !CreateTrackers(fRunTracking)) {
1721     Abort("CreateTrackers", TSelector::kAbortProcess);
1722     return;
1723   }      
1724   AliSysInfo::AddStamp("CreateTrackers");
1725
1726   // create the ESD output file and tree
1727   if (!outProofFile) {
1728     ffile = TFile::Open("AliESDs.root", "RECREATE");
1729     ffile->SetCompressionLevel(2);
1730     if (!ffile->IsOpen()) {
1731       Abort("OpenESDFile", TSelector::kAbortProcess);
1732       return;
1733     }
1734   }
1735   else {
1736     AliInfo(Form("Opening output PROOF file: %s/%s",
1737                  outProofFile->GetDir(), outProofFile->GetFileName()));
1738     if (!(ffile = outProofFile->OpenFile("RECREATE"))) {
1739       Abort(Form("Problems opening output PROOF file: %s/%s",
1740                  outProofFile->GetDir(), outProofFile->GetFileName()),
1741             TSelector::kAbortProcess);
1742       return;
1743     }
1744   }
1745
1746   ftree = new TTree("esdTree", "Tree with ESD objects");
1747   ftree->SetAutoFlush(1);
1748   fesd = new AliESDEvent();
1749   fesd->CreateStdContent();
1750   // add a so far non-std object to the ESD, this will
1751   // become part of the std content
1752   fesd->AddObject(new AliESDHLTDecision);
1753  
1754   const Int_t kBufsize = 32000/4; // default value in TTree::Branch()
1755   fesd->WriteToTree(ftree);
1756   if (fWriteESDfriend) {
1757     ffileF = TFile::Open("AliESDfriends.root", "RECREATE");
1758     ftreeF = new TTree("esdFriendTree", "Tree with ESD Friend objects");
1759     ftreeF->SetAutoFlush(1);
1760     fesdf  = new AliESDfriend();
1761     ftreeF->Branch("ESDfriend.","AliESDfriend", &fesdf, kBufsize);
1762     fesd->AddObject(fesdf);
1763     ffile->cd();
1764   }
1765   ftree->GetUserInfo()->Add(fesd);
1766
1767   fhlttree = new TTree("HLTesdTree", "Tree with HLT ESD objects");
1768   fhlttree->SetAutoFlush(1);
1769   fhltesd = new AliESDEvent();
1770   fhltesd->CreateStdContent();
1771   // read the ESD template from CDB
1772   // HLT is allowed to put non-std content to its ESD, the non-std
1773   // objects need to be created before invocation of WriteToTree in
1774   // order to create all branches. Initialization is done from an
1775   // ESD layout template in CDB
1776   AliCDBManager* man = AliCDBManager::Instance();
1777   AliCDBPath hltESDConfigPath("HLT/ConfigHLT/esdLayout");
1778   AliCDBEntry* hltESDConfig=NULL;
1779   if (man->GetId(hltESDConfigPath)!=NULL &&
1780       (hltESDConfig=man->Get(hltESDConfigPath))!=NULL) {
1781     AliESDEvent* pESDLayout=dynamic_cast<AliESDEvent*>(hltESDConfig->GetObject());
1782     if (pESDLayout) {
1783       // init all internal variables from the list of objects
1784       pESDLayout->GetStdContent();
1785
1786       // copy content and create non-std objects
1787       *fhltesd=*pESDLayout;
1788       fhltesd->Reset();
1789     } else {
1790       AliError(Form("error setting hltEsd layout from %s: invalid object type",
1791                     hltESDConfigPath.GetPath().Data()));
1792     }
1793   }
1794
1795   fhltesd->WriteToTree(fhlttree);
1796   fhlttree->GetUserInfo()->Add(fhltesd);
1797
1798   ProcInfo_t procInfo;
1799   gSystem->GetProcInfo(&procInfo);
1800   AliInfo(Form("Current memory usage %ld %ld", procInfo.fMemResident, procInfo.fMemVirtual));
1801   
1802   //QA
1803   //Initialize the QA and start of cycle 
1804   if (fRunQA || fRunGlobalQA) 
1805     InitQA() ; 
1806
1807   //Initialize the Plane Efficiency framework
1808   if (fRunPlaneEff && !InitPlaneEff()) {
1809     Abort("InitPlaneEff", TSelector::kAbortProcess);
1810     return;
1811   }
1812
1813   if (strcmp(gProgName,"alieve") == 0)
1814     fRunAliEVE = InitAliEVE();
1815   // If we have an analysis manager, connect the AliRecoInputHandler here  
1816   if (fAnalysis) {
1817     if (!dynamic_cast<AliRecoInputHandler*>(fAnalysis->GetInputEventHandler())) {
1818        AliError("Analysis manager used in reconstruction should use AliRecoInputHandler - \
1819                  \n  ->Replacing with AliRecoInputHandler instance.");
1820        delete fAnalysis->GetInputEventHandler();
1821     }
1822     // Set the event and other data pointers
1823     fRecoHandler = new AliRecoInputHandler();
1824     fRecoHandler->Init(ftree, "LOCAL");
1825     fRecoHandler->SetEvent(fesd);
1826     fRecoHandler->SetESDfriend(fesdf);
1827     fRecoHandler->SetHLTEvent(fhltesd);
1828     fRecoHandler->SetHLTTree(fhlttree);
1829     fAnalysis->SetInputEventHandler(fRecoHandler);
1830     // Enter external loop mode
1831     fAnalysis->SetExternalLoop(kTRUE);
1832     // Initialize analysis
1833     fAnalysis->StartAnalysis("local", (TTree*)0);
1834     // Connect ESD tree with the input container
1835     fAnalysis->GetCommonInputContainer()->SetData(ftree);
1836   }  
1837   return;
1838 }
1839
1840 //_____________________________________________________________________________
1841 Bool_t AliReconstruction::Process(Long64_t entry)
1842 {
1843   // run the reconstruction over a single entry
1844   // from the chain with raw data
1845   AliCodeTimerAuto("",0);
1846
1847   TTree *currTree = fChain->GetTree();
1848   AliRawVEvent *event = NULL;
1849   currTree->SetBranchAddress("rawevent",&event);
1850   currTree->GetEntry(entry);
1851   fRawReader = new AliRawReaderRoot(event);
1852   // check if process has enough resources 
1853   if (!HasEnoughResources(entry)) return kFALSE;
1854   fStatus = ProcessEvent(fRunLoader->GetNumberOfEvents());
1855   delete fRawReader;
1856   fRawReader = NULL;
1857   delete event;
1858
1859   return fStatus;
1860 }
1861
1862 //_____________________________________________________________________________
1863 void AliReconstruction::Init(TTree *tree)
1864 {
1865   // Implementation of TSelector::Init()
1866   // method
1867   if (tree == 0) {
1868     AliError("The input tree is not found!");
1869     return;
1870   }
1871   fChain = tree;
1872 }
1873
1874 //_____________________________________________________________________________
1875 Bool_t AliReconstruction::ProcessEvent(Int_t iEvent)
1876 {
1877   // run the reconstruction over a single event
1878   // The event loop is steered in Run method
1879
1880
1881   static Long_t oldMres=0;
1882   static Long_t oldMvir=0;
1883   static Float_t oldCPU=0;
1884   static Long_t aveDMres=0;
1885   static Long_t aveDMvir=0;
1886   static Float_t aveDCPU=0;
1887
1888   AliCodeTimerAuto("",0);
1889
1890   AliESDpid pid;
1891   
1892   AliSysInfo::AddStamp(Form("StartEv_%d",iEvent), 0,0,iEvent);
1893
1894   if (iEvent >= fRunLoader->GetNumberOfEvents()) {
1895     fRunLoader->SetEventNumber(iEvent);
1896     if (fRawReader)
1897       fRunLoader->GetHeader()->Reset(fRawReader->GetRunNumber(), 
1898                                      iEvent, iEvent);
1899     fRunLoader->TreeE()->Fill();
1900
1901     if (fRawReader && fRawReader->UseAutoSaveESD())
1902       fRunLoader->TreeE()->AutoSave("SaveSelf");
1903   }
1904
1905   if ((iEvent < fFirstEvent) || ((fLastEvent >= 0) && (iEvent > fLastEvent))) {
1906     return kTRUE;
1907   }
1908   //
1909   InitPools();   // make sure the pools are ready
1910   //
1911   fRunLoader->GetEvent(iEvent);
1912
1913   // Fill Event-info object
1914   GetEventInfo();
1915   fRecoParam.SetEventSpecie(fRunInfo,fEventInfo,fListOfCosmicTriggers);
1916   
1917   ProcInfo_t procInfo;
1918   if(iEvent==fFirstEvent) {
1919     gSystem->GetProcInfo(&procInfo);
1920     oldMres=procInfo.fMemResident;
1921     oldMvir=procInfo.fMemVirtual;
1922     oldCPU=procInfo.fCpuUser+procInfo.fCpuSys;
1923   }
1924   AliInfo(Form("================================= Processing event %d of type %-10s ==================================", iEvent,fRecoParam.PrintEventSpecie()));
1925
1926   AliSysInfo::AddStamp(Form("StartReco_%d",iEvent), 0,0,iEvent);
1927
1928   // Set the reco-params
1929   {
1930     TString detStr = fLoadCDB;
1931     for (Int_t iDet = 0; iDet < kNDetectors; iDet++) {
1932       if (!IsSelected(fgkDetectorName[iDet], detStr)) continue;
1933       AliReconstructor *reconstructor = GetReconstructor(iDet);
1934       if (reconstructor && fRecoParam.GetDetRecoParamArray(iDet)) {
1935         const AliDetectorRecoParam *par = fRecoParam.GetDetRecoParam(iDet);
1936         reconstructor->SetRecoParam(par);
1937         reconstructor->GetPidSettings(&pid);
1938         reconstructor->SetEventInfo(&fEventInfo);
1939         if (fRunQA) {
1940           AliQAManager::QAManager()->SetEventInfo(&fEventInfo) ;
1941           AliQAManager::QAManager()->SetRecoParam(iDet, par) ; 
1942           if (par) AliQAManager::QAManager()->SetEventSpecie(AliRecoParam::Convert(par->GetEventSpecie())) ;
1943         }
1944       }
1945     }
1946     //
1947     if (fRunQA || fRunGlobalQA) AliQADataMaker::SetEventTrigClasses(fEventInfo.GetTriggerClasses()); // RS: select which histo clones are to be filled
1948     //
1949     if (fRunQA) {
1950       const AliDetectorRecoParam *grppar = fRecoParam.GetDetRecoParam(kNDetectors);
1951       AliQAManager::QAManager()->SetRecoParam(AliQAv1::kGLOBAL, grppar) ; 
1952       AliQAManager::QAManager()->SetEventSpecie(AliRecoParam::Convert(grppar->GetEventSpecie())) ;
1953     }
1954   }
1955
1956     // QA on single raw 
1957   if (fRunQA && IsInTasks(AliQAv1::kRAWS)) {
1958     AliQAManager::QAManager()->SetEventSpecie(fRecoParam.GetEventSpecie()) ;
1959     AliQAManager::QAManager()->RunOneEvent(fRawReader) ;  
1960     AliSysInfo::AddStamp(Form("RawQA_%d",iEvent), 0,0,iEvent);
1961   }
1962     // local single event reconstruction
1963     if (!fRunLocalReconstruction.IsNull()) {
1964       TString detectors=fRunLocalReconstruction;
1965       // run HLT event reconstruction first
1966       // ;-( IsSelected changes the string
1967       if (IsSelected("HLT", detectors) &&
1968           !RunLocalEventReconstruction("HLT")) {
1969         if (fStopOnError) {CleanUp(); return kFALSE;}
1970       }
1971       detectors=fRunLocalReconstruction;
1972       detectors.ReplaceAll("HLT", "");
1973       if (!RunLocalEventReconstruction(detectors)) {
1974         if (fStopOnError) {
1975           CleanUp(); 
1976           return kFALSE;
1977         }
1978       }
1979     }
1980
1981   
1982     // fill Event header information from the RawEventHeader
1983     if (fRawReader){FillRawEventHeaderESD(fesd);}
1984     if (fRawReader){FillRawEventHeaderESD(fhltesd);}
1985
1986     fesd->SetRunNumber(fRunLoader->GetHeader()->GetRun());
1987     fhltesd->SetRunNumber(fRunLoader->GetHeader()->GetRun());
1988     
1989     ((AliESDRun*)fesd->GetESDRun())->SetDetectorsInDAQ(fRunInfo->GetDetectorMask());
1990     ((AliESDRun*)fhltesd->GetESDRun())->SetDetectorsInDAQ(fRunInfo->GetDetectorMask());
1991     ((AliESDRun*)fesd->GetESDRun())->SetDetectorsInReco(AliDAQ::DetectorPatternOffline(fFillESD.Data()));
1992     ((AliESDRun*)fhltesd->GetESDRun())->SetDetectorsInReco(AliDAQ::DetectorPatternOffline(fFillESD.Data()));
1993
1994     fesd->SetEventNumberInFile(fRunLoader->GetHeader()->GetEventNrInRun());
1995     fhltesd->SetEventNumberInFile(fRunLoader->GetHeader()->GetEventNrInRun());
1996
1997     fesd->SetEventSpecie(fRecoParam.GetEventSpecie());
1998     fhltesd->SetEventSpecie(fRecoParam.GetEventSpecie());
1999     
2000     // Set magnetic field from the tracker
2001     fesd->SetMagneticField(AliTracker::GetBz());
2002     fhltesd->SetMagneticField(AliTracker::GetBz());
2003     //
2004     AliESDRun *esdRun,*esdRunH;
2005     esdRun  = (AliESDRun*)fesd->GetESDRun();
2006     esdRunH = (AliESDRun*)fhltesd->GetESDRun();
2007     esdRun->SetBeamEnergyIsSqrtSHalfGeV();
2008     esdRunH->SetBeamEnergyIsSqrtSHalfGeV();
2009     //
2010     for (int ib=2;ib--;) for (int it=2;it--;) {
2011         esdRun->SetMeanIntensity(ib,it, fBeamInt[ib][it]); 
2012         esdRunH->SetMeanIntensity(ib,it, fBeamInt[ib][it]); 
2013       }
2014     //
2015     fesd->SetBeamEnergy(fGRPData->GetBeamEnergy());
2016     fesd->SetBeamType(fGRPData->GetBeamType().Data());
2017     fesd->SetBeamParticle(fGRPData->GetSingleBeamType(0).Atoi(),0);
2018     fesd->SetBeamParticle(fGRPData->GetSingleBeamType(1).Atoi(),1);
2019     fhltesd->SetBeamEnergy(fGRPData->GetBeamEnergy());
2020     fhltesd->SetBeamType(fGRPData->GetBeamType().Data());
2021     fhltesd->SetBeamParticle(fGRPData->GetSingleBeamType(0).Atoi(),0);
2022     fhltesd->SetBeamParticle(fGRPData->GetSingleBeamType(1).Atoi(),1);
2023     //
2024     AliMagF* fld = (AliMagF*)TGeoGlobalMagField::Instance()->GetField();
2025     if (fld) { // set info needed for field initialization
2026       fesd->SetCurrentL3(fld->GetCurrentSol());
2027       fesd->SetCurrentDip(fld->GetCurrentDip());
2028       fesd->SetUniformBMap(fld->IsUniform());
2029       fesd->SetBInfoStored();
2030       //
2031       fhltesd->SetCurrentL3(fld->GetCurrentSol());
2032       fhltesd->SetCurrentDip(fld->GetCurrentDip());
2033       fhltesd->SetUniformBMap(fld->IsUniform());
2034       fhltesd->SetBInfoStored();
2035     }
2036     //
2037     // Set most probable pt, for B=0 tracking
2038     // Get the global reco-params. They are atposition 16 inside the array of detectors in fRecoParam
2039     const AliGRPRecoParam *grpRecoParam = dynamic_cast<const AliGRPRecoParam*>(fRecoParam.GetDetRecoParam(kNDetectors));
2040     if (grpRecoParam) AliExternalTrackParam::SetMostProbablePt(grpRecoParam->GetMostProbablePt());
2041     
2042     // Fill raw-data error log into the ESD
2043     if (fRawReader) FillRawDataErrorLog(iEvent,fesd);
2044
2045     AliSysInfo::AddStamp(Form("FillHeadErrs_%d",iEvent), 0,0,iEvent);
2046
2047     // vertex finder
2048     if (fRunVertexFinder) {
2049       if (!RunVertexFinder(fesd)) {
2050         if (fStopOnError) {CleanUp(); return kFALSE;}
2051       }
2052       AliSysInfo::AddStamp(Form("VtxFinder_%d",iEvent), 0,0,iEvent);
2053     }
2054
2055     // For Plane Efficiency: run the SPD trackleter
2056     if (fRunPlaneEff && fSPDTrackleter) {
2057       if (!RunSPDTrackleting(fesd)) {
2058         if (fStopOnError) {CleanUp(); return kFALSE;}
2059       }
2060       AliSysInfo::AddStamp(Form("TrackletEff_%d",iEvent), 0,0,iEvent);
2061     }
2062
2063     // Muon tracking
2064     if (!fRunTracking.IsNull()) {
2065       if (fRunMuonTracking) {
2066         if (!RunMuonTracking(fesd)) {
2067           if (fStopOnError) {CleanUp(); return kFALSE;}
2068         }
2069       }
2070       AliSysInfo::AddStamp(Form("TrackingMUON_%d",iEvent), 0,0,iEvent);      
2071     }
2072
2073     // barrel tracking
2074     if (!fRunTracking.IsNull()) {
2075       if (!RunTracking(fesd,pid)) {
2076         if (fStopOnError) {CleanUp(); return kFALSE;}
2077       }
2078     }
2079
2080     // fill ESD
2081     if (!fFillESD.IsNull()) {
2082       TString detectors=fFillESD;
2083       // run HLT first and on hltesd
2084       // ;-( IsSelected changes the string
2085       if (IsSelected("HLT", detectors) &&
2086           !FillESD(fhltesd, "HLT")) {
2087         if (fStopOnError) {CleanUp(); return kFALSE;}
2088       }
2089       detectors=fFillESD;
2090       // Temporary fix to avoid problems with HLT that overwrites the offline ESDs
2091       if (detectors.Contains("ALL")) {
2092         detectors="";
2093         for (Int_t idet=0; idet<kNDetectors; ++idet){
2094           detectors += fgkDetectorName[idet];
2095           detectors += " ";
2096         }
2097       }
2098       detectors.ReplaceAll("HLT", "");
2099       if (!FillESD(fesd, detectors)) {
2100         if (fStopOnError) {CleanUp(); return kFALSE;}
2101       }
2102     }
2103     
2104
2105     ffile->cd();
2106
2107     //
2108     // Propagate track to the beam pipe  (if not already done by ITS)
2109     //
2110     const Int_t ntracks = fesd->GetNumberOfTracks();
2111     const Double_t kRadius  = 2.8; //something less than the beam pipe radius
2112
2113     TObjArray trkArray;
2114     UShort_t selectedIdx[ntracks];
2115
2116     for (Int_t itrack=0; itrack<ntracks; itrack++){
2117       const Double_t kMaxStep = 1;   //max step over the material
2118       Bool_t ok;
2119
2120       AliESDtrack *track = fesd->GetTrack(itrack);
2121       if (!track) continue;
2122
2123       AliExternalTrackParam *tpcTrack =
2124            (AliExternalTrackParam *)track->GetTPCInnerParam();
2125       ok = kFALSE;
2126       if (tpcTrack)
2127         ok = AliTracker::
2128           PropagateTrackToBxByBz(tpcTrack,kRadius,track->GetMass(),kMaxStep,kFALSE);
2129
2130       if (ok) {
2131         Int_t n=trkArray.GetEntriesFast();
2132         selectedIdx[n]=track->GetID();
2133         trkArray.AddLast(tpcTrack);
2134       }
2135
2136       //Tracks refitted by ITS should already be at the SPD vertex
2137       if (track->IsOn(AliESDtrack::kITSrefit)) continue;
2138
2139       AliTracker::
2140          PropagateTrackToBxByBz(track,kRadius,track->GetMass(),kMaxStep,kFALSE);
2141       Double_t x[3]; track->GetXYZ(x);
2142       Double_t b[3]; AliTracker::GetBxByBz(x,b);
2143       track->RelateToVertexBxByBz(fesd->GetPrimaryVertexSPD(), b, kVeryBig);
2144
2145     }
2146     AliSysInfo::AddStamp(Form("RelToSPDVtx_%d",iEvent), 0,0,iEvent);      
2147     //
2148     // Improve the reconstructed primary vertex position using the tracks
2149     //
2150     Bool_t runVertexFinderTracks = fRunVertexFinderTracks;
2151     if(fesd->GetPrimaryVertexSPD()) {
2152       TString vtitle = fesd->GetPrimaryVertexSPD()->GetTitle();
2153       if(vtitle.Contains("cosmics")) {
2154         runVertexFinderTracks=kFALSE;
2155       }
2156     }
2157
2158     if (runVertexFinderTracks) {
2159        // TPC + ITS primary vertex
2160        ftVertexer->SetITSMode();
2161        ftVertexer->SetConstraintOff();
2162        // get cuts for vertexer from AliGRPRecoParam
2163        Bool_t constrSPD=kFALSE;
2164        if (grpRecoParam) {
2165          Int_t nCutsVertexer = grpRecoParam->GetVertexerTracksNCuts();
2166          Double_t *cutsVertexer = new Double_t[nCutsVertexer];
2167          grpRecoParam->GetVertexerTracksCutsITS(cutsVertexer,nCutsVertexer);
2168          ftVertexer->SetCuts(cutsVertexer,nCutsVertexer);
2169          delete [] cutsVertexer; cutsVertexer = NULL; 
2170          if(grpRecoParam->GetVertexerTracksConstraintITS()) { 
2171            if(fDiamondProfile && fDiamondProfile->GetXRes()<kRadius){
2172              ftVertexer->SetVtxStart(fDiamondProfile); // apply constraint only if sigmax is smaller than the beam pipe radius 
2173            }else{
2174              if(fDiamondProfileSPD && fDiamondProfileSPD->GetXRes()<kRadius){
2175                ftVertexer->SetVtxStart(fDiamondProfileSPD);
2176                constrSPD=kTRUE;
2177              }
2178            }
2179          } 
2180        }
2181        AliESDVertex *pvtx=ftVertexer->FindPrimaryVertex(fesd);
2182        if (pvtx) {
2183          if(constrSPD){
2184            TString title=pvtx->GetTitle();
2185            title.Append("SPD");
2186            pvtx->SetTitle(title);
2187          }
2188           if (pvtx->GetStatus()) {
2189              fesd->SetPrimaryVertexTracks(pvtx);
2190              for (Int_t i=0; i<ntracks; i++) {
2191                  AliESDtrack *t = fesd->GetTrack(i);
2192                  Double_t x[3]; t->GetXYZ(x);
2193                  Double_t b[3]; AliTracker::GetBxByBz(x,b);
2194                  t->RelateToVertexBxByBz(pvtx, b, kVeryBig);
2195              } 
2196           }
2197           delete pvtx; pvtx=NULL;
2198        }
2199        AliSysInfo::AddStamp(Form("VtxTrk_%d",iEvent), 0,0,iEvent);      
2200
2201        // TPC-only primary vertex
2202        ftVertexer->SetTPCMode();
2203        ftVertexer->SetConstraintOff();
2204        // get cuts for vertexer from AliGRPRecoParam
2205        if (grpRecoParam) {
2206          Int_t nCutsVertexer = grpRecoParam->GetVertexerTracksNCuts();
2207          Double_t *cutsVertexer = new Double_t[nCutsVertexer];
2208          grpRecoParam->GetVertexerTracksCutsTPC(cutsVertexer,nCutsVertexer);
2209          ftVertexer->SetCuts(cutsVertexer,nCutsVertexer);
2210          delete [] cutsVertexer; cutsVertexer = NULL; 
2211          if(fDiamondProfileTPC && grpRecoParam->GetVertexerTracksConstraintTPC()) { 
2212            if(fDiamondProfileTPC->GetXRes()<kRadius) ftVertexer->SetVtxStart(fDiamondProfileTPC); // apply constraint only if sigmax is smaller than the beam pipe radius 
2213          } 
2214        }
2215        pvtx=ftVertexer->FindPrimaryVertex(&trkArray,selectedIdx);
2216        if (pvtx) {
2217           if (pvtx->GetStatus()) {
2218              fesd->SetPrimaryVertexTPC(pvtx);
2219              for (Int_t i=0; i<ntracks; i++) {
2220                  AliESDtrack *t = fesd->GetTrack(i);
2221                  Double_t x[3]; t->GetXYZ(x);
2222                  Double_t b[3]; AliTracker::GetBxByBz(x,b);
2223                  t->RelateToVertexTPCBxByBz(pvtx, b, kVeryBig);
2224              } 
2225           }
2226           delete pvtx; pvtx=NULL;
2227        }
2228        AliSysInfo::AddStamp(Form("VtxTPC_%d",iEvent), 0,0,iEvent);      
2229
2230     }
2231     
2232     if(fDiamondProfile && fDiamondProfile->GetXRes()<kRadius) fesd->SetDiamond(fDiamondProfile);
2233     else fesd->SetDiamond(fDiamondProfileSPD);
2234
2235     if (fRunV0Finder) {
2236        // V0 finding
2237        AliV0vertexer vtxer;
2238        // get cuts for V0vertexer from AliGRPRecoParam
2239        if (grpRecoParam) {
2240          Int_t nCutsV0vertexer = grpRecoParam->GetVertexerV0NCuts();
2241          Double_t cutsV0vertexer[nCutsV0vertexer];
2242          grpRecoParam->GetVertexerV0Cuts(cutsV0vertexer);
2243          vtxer.SetCuts(cutsV0vertexer);
2244        }
2245        vtxer.Tracks2V0vertices(fesd);
2246        AliSysInfo::AddStamp(Form("V0Finder_%d",iEvent), 0,0,iEvent); 
2247
2248        if (fRunCascadeFinder) {
2249           // Cascade finding
2250           AliCascadeVertexer cvtxer;
2251           // get cuts for CascadeVertexer from AliGRPRecoParam
2252           if (grpRecoParam) {
2253             Int_t nCutsCascadeVertexer = grpRecoParam->GetVertexerCascadeNCuts();
2254             Double_t cutsCascadeVertexer[nCutsCascadeVertexer];
2255             grpRecoParam->GetVertexerCascadeCuts(cutsCascadeVertexer);
2256             cvtxer.SetCuts(cutsCascadeVertexer);
2257           }
2258           cvtxer.V0sTracks2CascadeVertices(fesd);
2259           AliSysInfo::AddStamp(Form("CascadeFinder_%d",iEvent), 0,0,iEvent); 
2260        }
2261     }
2262
2263     // AdC+FN
2264     if (fReconstructor[3])
2265       GetReconstructor(3)->FillEventTimeWithTOF(fesd,&pid);
2266
2267     // combined PID
2268     pid.MakePID(fesd);
2269
2270     if (fFillTriggerESD) {
2271       if (!FillTriggerESD(fesd)) {
2272         if (fStopOnError) {CleanUp(); return kFALSE;}
2273       }
2274     }
2275     // Always fill scalers
2276     if (!FillTriggerScalers(fesd)) {
2277        if (fStopOnError) {CleanUp(); return kFALSE;}
2278     }
2279
2280     AliSysInfo::AddStamp(Form("FillVaria_%d",iEvent), 0,0,iEvent); 
2281
2282     // write ESD
2283     if (fCleanESD) {
2284       CleanESD(fesd);
2285       AliSysInfo::AddStamp(Form("CleanESD_%d",iEvent), 0,0,iEvent); 
2286     }
2287     // 
2288     // RS run updated trackleter: since we want to mark the clusters used by tracks and also mark the 
2289     // tracks interpreted as primary, this step should be done in the very end, when full 
2290     // ESD info is available (particulalry, V0s)
2291     // vertex finder
2292     if (fRunMultFinder) {
2293       if (!RunMultFinder(fesd)) {
2294         if (fStopOnError) {CleanUp(); return kFALSE;}
2295       }
2296       AliSysInfo::AddStamp(Form("MultFinder_%d",iEvent), 0,0,iEvent); 
2297     }
2298
2299   if (fRunQA && IsInTasks(AliQAv1::kESDS)) {
2300     AliQAManager::QAManager()->SetEventSpecie(fRecoParam.GetEventSpecie()) ;
2301     AliQAManager::QAManager()->RunOneEvent(fesd, fhltesd) ; 
2302     AliSysInfo::AddStamp(Form("RunQA_%d",iEvent), 0,0,iEvent); 
2303   }
2304   if (fRunGlobalQA) {
2305     AliQADataMaker *qadm = AliQAManager::QAManager()->GetQADataMaker(AliQAv1::kGLOBAL);
2306     if (qadm)
2307       qadm->SetEventSpecie(fRecoParam.GetEventSpecie()) ;
2308     if (qadm && IsInTasks(AliQAv1::kESDS))
2309       qadm->Exec(AliQAv1::kESDS, fesd);
2310     AliSysInfo::AddStamp(Form("RunGlobQA_%d",iEvent), 0,0,iEvent);     
2311   }
2312
2313   // copy HLT decision from HLTesd to esd
2314   // the most relevant information is stored in a reduced container in the esd,
2315   // while the full information can be found in the HLTesd
2316   TObject* pHLTSrc=fhltesd->FindListObject(AliESDHLTDecision::Name());
2317   TObject* pHLTTgt=fesd->FindListObject(AliESDHLTDecision::Name());
2318   if (pHLTSrc && pHLTTgt) {
2319     pHLTSrc->Copy(*pHLTTgt);
2320   }
2321   //
2322   fPools->Print(); // RS:tmp
2323   // Perform analysis of this event if requested
2324   // RS: Should be done before WriteESDfriend, since the latter may clean the esdfriend
2325   if (fAnalysis) {
2326     fRecoHandler->BeginEvent(iEvent);
2327     fAnalysis->ExecAnalysis();
2328     fRecoHandler->FinishEvent();
2329     AliSysInfo::AddStamp(Form("Analysis_%d",iEvent), 0,0,iEvent);     
2330   }  
2331   //
2332   // RS there is no point in filling the esdFriend object if it is not going to be written
2333   if (fWriteESDfriend) {
2334     WriteESDfriend();
2335     AliSysInfo::AddStamp(Form("WriteFriend_%d",iEvent), 0,0,iEvent);     
2336   }
2337   //
2338   ftree->Fill();
2339   //
2340   AliSysInfo::AddStamp(Form("ESDFill_%d",iEvent), 0,0,iEvent);     
2341   if (fWriteESDfriend) {
2342     // Auto-save the ESD tree in case of prompt reco @P2
2343     if (fRawReader && fRawReader->UseAutoSaveESD()) {
2344       ftree->AutoSave("SaveSelf");
2345       if (fWriteESDfriend) ftreeF->AutoSave("SaveSelf");
2346     }
2347   }
2348     // write HLT ESD
2349     fhlttree->Fill();
2350
2351     AliSysInfo::AddStamp(Form("WriteESDs_%d",iEvent), 0,0,iEvent);     
2352
2353     // call AliEVE
2354     if (fRunAliEVE) RunAliEVE();
2355     //
2356     fesd->Reset();
2357     fhltesd->Reset();
2358     if (fWriteESDfriend) {
2359       fesdf->Clear();
2360       //      fesdf->~AliESDfriend();
2361       //      new (fesdf) AliESDfriend(); // Reset...
2362     }
2363  
2364     gSystem->GetProcInfo(&procInfo);
2365     Long_t dMres=(procInfo.fMemResident-oldMres)/1024;
2366     Long_t dMvir=(procInfo.fMemVirtual-oldMvir)/1024;
2367     Float_t dCPU=procInfo.fCpuUser+procInfo.fCpuSys-oldCPU;
2368     aveDMres+=(dMres-aveDMres)/(iEvent-fFirstEvent+1);
2369     aveDMvir+=(dMvir-aveDMvir)/(iEvent-fFirstEvent+1);
2370     aveDCPU+=(dCPU-aveDCPU)/(iEvent-fFirstEvent+1);
2371     AliInfo(Form("======================= End Event %d: Res %ld(%3ld <%3ld>) Vir %ld(%3ld <%3ld>) CPU %5.2f <%5.2f> ===================",
2372                  iEvent, procInfo.fMemResident/1024, dMres, aveDMres, procInfo.fMemVirtual/1024, dMvir, aveDMvir, dCPU, aveDCPU));
2373     oldMres=procInfo.fMemResident;
2374     oldMvir=procInfo.fMemVirtual;
2375     oldCPU=procInfo.fCpuUser+procInfo.fCpuSys;
2376   
2377     fEventInfo.Reset();
2378     for (Int_t iDet = 0; iDet < kNDetectors; iDet++) {
2379       if (fReconstructor[iDet]) {
2380         fReconstructor[iDet]->SetRecoParam(NULL);
2381         fReconstructor[iDet]->SetEventInfo(NULL);
2382       }
2383       if (fTracker[iDet]) fTracker[iDet]->SetEventInfo(NULL);
2384     }
2385         
2386   if (fRunQA || fRunGlobalQA) 
2387     AliQAManager::QAManager()->Increment() ; 
2388   //
2389   fPools->Print();
2390   fPools->ResetPools();
2391   DeleteRecPoints(fDeleteRecPoints);
2392   DeleteDigits(fDeleteDigits);
2393   return kTRUE;
2394 }
2395
2396 //_____________________________________________________________________________
2397 void AliReconstruction::SlaveTerminate()
2398 {
2399   // Finalize the run on the slave side
2400   // Called after the exit
2401   // from the event loop
2402   AliCodeTimerAuto("",0);
2403   // If analysis was done during reconstruction, we need to call SlaveTerminate for it
2404   if (fAnalysis) {
2405      fAnalysis->PackOutput(fOutput);
2406      fAnalysis->SetSkipTerminate(kTRUE);
2407      fAnalysis->Terminate();
2408   }   
2409
2410   if (fIsNewRunLoader) { // galice.root didn't exist
2411     fRunLoader->WriteHeader("OVERWRITE");
2412     fRunLoader->WriteTrigger("OVERWRITE");
2413     fRunLoader->CdGAFile();
2414     fRunLoader->Write(0, TObject::kOverwrite);
2415   }
2416
2417   const TMap *cdbMap = AliCDBManager::Instance()->GetStorageMap();       
2418   const TList *cdbList = AliCDBManager::Instance()->GetRetrievedIds();   
2419                  
2420    TMap *cdbMapCopy = new TMap(cdbMap->GetEntries());    
2421    cdbMapCopy->SetOwner(1);      
2422    cdbMapCopy->SetName("cdbMap");        
2423    TIter iter(cdbMap->GetTable());       
2424          
2425    TPair* pair = 0;      
2426    while((pair = dynamic_cast<TPair*> (iter.Next()))){   
2427          TObjString* keyStr = dynamic_cast<TObjString*> (pair->Key());   
2428          TObjString* valStr = dynamic_cast<TObjString*> (pair->Value());
2429          if (keyStr && valStr)
2430            cdbMapCopy->Add(new TObjString(keyStr->GetName()), new TObjString(valStr->GetName()));        
2431    }     
2432          
2433    TList *cdbListCopy = new TList();     
2434    cdbListCopy->SetOwner(1);     
2435    cdbListCopy->SetName("cdbList");      
2436          
2437    TIter iter2(cdbList);         
2438          
2439         AliCDBId* id=0;
2440         while((id = dynamic_cast<AliCDBId*> (iter2.Next()))){    
2441          cdbListCopy->Add(new TObjString(id->ToString().Data()));        
2442    }     
2443          
2444    ftree->GetUserInfo()->Add(cdbMapCopy);        
2445    ftree->GetUserInfo()->Add(cdbListCopy);
2446
2447    // Add the AliRoot version that created this file
2448    TString sVersion("aliroot ");
2449    sVersion += ALIROOT_SVN_BRANCH;
2450    sVersion += ":";
2451    sVersion += ALIROOT_SVN_REVISION;
2452    sVersion += "; root ";
2453    sVersion += ROOT_SVN_BRANCH;
2454    sVersion += ":";
2455    sVersion += ROOT_SVN_REVISION;
2456    sVersion += "; metadata ";
2457    sVersion += gSystem->Getenv("PRODUCTION_METADATA");
2458                     
2459
2460    TNamed * alirootVersion = new TNamed("alirootVersion",sVersion.Data());
2461    ftree->GetUserInfo()->Add(alirootVersion); // The list becomes owner of alirootVersion
2462
2463   ffile->cd();
2464
2465   // we want to have only one tree version number
2466   ftree->Write(ftree->GetName(),TObject::kOverwrite);
2467   fhlttree->Write(fhlttree->GetName(),TObject::kOverwrite);
2468
2469   if (fWriteESDfriend) {
2470     ffileF->cd();
2471     ftreeF->Write(ftreeF->GetName(),TObject::kOverwrite);
2472   }
2473
2474 // Finish with Plane Efficiency evaluation: before of CleanUp !!!
2475   if (fRunPlaneEff && !FinishPlaneEff()) {
2476    AliWarning("Finish PlaneEff evaluation failed");
2477   }
2478
2479   for (Int_t iDet = 0; iDet < kNDetectors; iDet++) {
2480     if (fReconstructor[iDet]) fReconstructor[iDet]->Terminate();
2481   }
2482   // End of cycle for the in-loop  
2483
2484   if (fRunQA || fRunGlobalQA) {
2485     AliQAManager::QAManager()->EndOfCycle() ;
2486     if (fInput &&
2487         !fProofOutputLocation.IsNull() &&
2488         fProofOutputArchive.IsNull() &&
2489         !fProofOutputDataset) {
2490       TString qaOutputFile(Form("%sMerged.%s.Data.root",
2491                                 fProofOutputLocation.Data(),
2492                                 AliQAv1::GetQADataFileName()));
2493       TProofOutputFile *qaProofFile = new TProofOutputFile(Form("Merged.%s.Data.root",
2494                                                                 AliQAv1::GetQADataFileName()));
2495       qaProofFile->SetOutputFileName(qaOutputFile.Data());
2496       if (AliDebugLevel() > 0) qaProofFile->Dump();
2497       fOutput->Add(qaProofFile);
2498       MergeQA(qaProofFile->GetFileName());
2499     }
2500     else {
2501       MergeQA();
2502     }
2503   }
2504
2505   gROOT->cd();
2506   CleanUp();
2507
2508   if (fInput) {
2509     if (!fProofOutputFileName.IsNull() &&
2510         !fProofOutputLocation.IsNull() &&
2511         fProofOutputDataset &&
2512         !fProofOutputArchive.IsNull()) {
2513       TProofOutputFile *zipProofFile = new TProofOutputFile(fProofOutputFileName.Data(),
2514                                                             "DROV",
2515                                                             fProofOutputLocation.Data());
2516       if (AliDebugLevel() > 0) zipProofFile->Dump();
2517       fOutput->Add(zipProofFile);
2518       TString fileList(fProofOutputArchive.Data());
2519       fileList.ReplaceAll(","," ");
2520       TString command;
2521 #if ROOT_SVN_REVISION >= 30174
2522       command.Form("zip -n root %s/%s %s",zipProofFile->GetDir(kTRUE),zipProofFile->GetFileName(),fileList.Data());
2523 #else
2524       command.Form("zip -n root %s/%s %s",zipProofFile->GetDir(),zipProofFile->GetFileName(),fileList.Data());
2525 #endif
2526       AliInfo(Form("Executing: %s",command.Data()));
2527       gSystem->Exec(command.Data());
2528     }
2529   }
2530 }
2531     
2532 //_____________________________________________________________________________
2533 void AliReconstruction::Terminate()
2534 {
2535   // Create tags for the events in the ESD tree (the ESD tree is always present)
2536   // In case of empty events the tags will contain dummy values
2537   AliCodeTimerAuto("",0);
2538
2539   // 
2540   // or for the files stopped due to the limited resources
2541   if (!fInput) {
2542     AliESDTagCreator *esdtagCreator = new AliESDTagCreator();
2543     esdtagCreator->CreateESDTags(fFirstEvent,fLastEvent,fGRPData, AliQAv1::Instance()->GetQA(), AliQAv1::Instance()->GetEventSpecies(), AliQAv1::kNDET, AliRecoParam::kNSpecies);
2544     delete esdtagCreator;
2545   }
2546
2547   // Cleanup of CDB manager: cache and active storages!
2548   AliCDBManager::Instance()->ClearCache();
2549 }
2550
2551 //_____________________________________________________________________________
2552 Bool_t AliReconstruction::RunLocalEventReconstruction(const TString& detectors)
2553 {
2554 // run the local reconstruction
2555
2556   static Int_t eventNr=0;
2557   AliCodeTimerAuto("",0)
2558
2559   TString detStr = detectors;
2560   // execute HLT reconstruction first since other detector reconstruction
2561   // might depend on HLT data
2562   // key 'HLT' is removed from detStr by IsSelected
2563   if (!IsSelected("HLT", detStr)) {
2564     AliReconstructor* reconstructor = GetReconstructor(kNDetectors-1);
2565     if (reconstructor) {
2566       // there is no AliLoader for HLT, see
2567       // https://savannah.cern.ch/bugs/?35473
2568       AliInfo("running reconstruction for HLT");
2569       if (fRawReader) {
2570         AliInfo("reconstructor->Reconstruct(fRawReader, NULL)");
2571         reconstructor->Reconstruct(fRawReader, NULL);
2572       } 
2573       else {
2574         AliInfo("reconstructor->Reconstruct(dummy, NULL)");
2575         TTree* dummy=NULL;
2576         reconstructor->Reconstruct(dummy, NULL);
2577       }
2578     }
2579     AliSysInfo::AddStamp(Form("LRecHLT_%d",eventNr), -1,1,eventNr);
2580   }
2581
2582   AliInfo(Form("kNDetectors = %d",kNDetectors));
2583
2584   for (Int_t iDet = 0; iDet < kNDetectors; iDet++) {
2585     if (!IsSelected(fgkDetectorName[iDet], detStr)) continue;
2586     AliDebug(1, Form("Detector: %s", fgkDetectorName[iDet]));
2587     AliReconstructor* reconstructor = GetReconstructor(iDet);
2588     if (!reconstructor) continue;
2589     AliLoader* loader = fLoader[iDet];
2590     if (!loader) {
2591       AliWarning(Form("No loader is defined for %s!",fgkDetectorName[iDet]));
2592       continue;
2593     }
2594     // conversion of digits
2595     if (fRawReader && reconstructor->HasDigitConversion()) {
2596       AliInfo(Form("converting raw data digits into root objects for %s", 
2597                    fgkDetectorName[iDet]));
2598 //      AliCodeTimerAuto(Form("converting raw data digits into root objects for %s", 
2599 //                            fgkDetectorName[iDet]),0);
2600       loader->LoadDigits("update");
2601       loader->CleanDigits();
2602       loader->MakeDigitsContainer();
2603       TTree* digitsTree = loader->TreeD();
2604       reconstructor->ConvertDigits(fRawReader, digitsTree);
2605       loader->WriteDigits("OVERWRITE");
2606       loader->UnloadDigits();
2607       AliSysInfo::AddStamp(Form("LRecRW2Root%s_%d",fgkDetectorName[iDet],eventNr), iDet,1,eventNr);
2608     }
2609     // local reconstruction
2610     AliInfo(Form("running reconstruction for %s", fgkDetectorName[iDet]));
2611     //AliCodeTimerAuto(Form("running reconstruction for %s", fgkDetectorName[iDet]),0);
2612     AliDebug(1, "Loading Rec Points");
2613     loader->LoadRecPoints("update");
2614     AliDebug(1, "Cleaning Rec Points");
2615     loader->CleanRecPoints();
2616     AliDebug(1, "Making Rec Points Container");
2617     loader->MakeRecPointsContainer();
2618     TTree* clustersTree = loader->TreeR();
2619     if (fRawReader && !reconstructor->HasDigitConversion()) {
2620       reconstructor->Reconstruct(fRawReader, clustersTree);
2621       AliSysInfo::AddStamp(Form("LRecRec%s_%d",fgkDetectorName[iDet],eventNr), iDet,1,eventNr);
2622     } 
2623     else {
2624       AliDebug(1, "Loading Digits");
2625       loader->LoadDigits("read");
2626       TTree* digitsTree = loader->TreeD();
2627       AliDebug(1, Form("Digits Tree = %p",digitsTree));
2628       if (!digitsTree) {
2629         AliError(Form("Can't get the %s digits tree", fgkDetectorName[iDet]));
2630         if (fStopOnError) 
2631           return kFALSE;
2632       } 
2633       else {
2634         AliDebug(1, "Digits -> Clusters");
2635         reconstructor->Reconstruct(digitsTree, clustersTree);
2636         if (fRunQA && IsInTasks(AliQAv1::kDIGITSR)) {
2637           AliQAManager::QAManager()->SetEventSpecie(fRecoParam.GetEventSpecie()) ;
2638           AliQAManager::QAManager()->RunOneEventInOneDetector(iDet, digitsTree) ; 
2639         }
2640         AliSysInfo::AddStamp(Form("LRecD2C%s_%d",fgkDetectorName[iDet],eventNr), iDet,1,eventNr);
2641       }
2642       loader->UnloadDigits();
2643     }
2644     if (fRunQA && IsInTasks(AliQAv1::kRECPOINTS)) {
2645       AliQAManager::QAManager()->SetEventSpecie(fRecoParam.GetEventSpecie()) ;
2646       AliQAManager::QAManager()->RunOneEventInOneDetector(iDet, clustersTree) ; 
2647     }
2648     loader->WriteRecPoints("OVERWRITE");
2649     loader->UnloadRecPoints();
2650     AliSysInfo::AddStamp(Form("LRec%s_%d",fgkDetectorName[iDet],eventNr), iDet,1,eventNr);
2651   }
2652   if (!IsSelected("CTP", detStr)) AliDebug(10,"No CTP");
2653   if ((detStr.CompareTo("ALL") != 0) && !detStr.IsNull()) {
2654     AliError(Form("the following detectors were not found: %s",
2655                   detStr.Data()));
2656     if (fStopOnError) 
2657       return kFALSE;
2658   }
2659   eventNr++;
2660   return kTRUE;
2661 }
2662 //_____________________________________________________________________________
2663 Bool_t AliReconstruction::RunSPDTrackleting(AliESDEvent*& esd)
2664 {
2665 // run the SPD trackleting (for SPD efficiency purpouses)
2666
2667   AliCodeTimerAuto("",0)
2668
2669   Double_t vtxPos[3] = {0, 0, 0};
2670   Double_t vtxErr[3] = {0.0, 0.0, 0.0};
2671 /*
2672   TArrayF m
2673 /
2674 cVertex(3);
2675   // if(MC)
2676   if (fRunLoader->GetHeader() && fRunLoader->GetHeader()->GenEventHeader()) {
2677     fRunLoader->GetHeader()->GenEventHeader()->PrimaryVertex(mcVertex);
2678     for (Int_t i = 0; i < 3; i++) vtxPos[i] = mcVertex[i];
2679   }
2680 */
2681   const AliESDVertex *vertex = esd->GetVertex();
2682   if(!vertex){
2683     AliWarning("Vertex not found");
2684     return kFALSE;
2685   }
2686   vertex->GetXYZ(vtxPos);
2687   vertex->GetSigmaXYZ(vtxErr);
2688   if (fSPDTrackleter) {
2689     AliInfo("running the SPD Trackleter for Plane Efficiency Evaluation");
2690
2691     // load clusters
2692     fLoader[0]->LoadRecPoints("read");
2693     TTree* tree = fLoader[0]->TreeR();
2694     if (!tree) {
2695       AliError("Can't get the ITS cluster tree");
2696       return kFALSE;
2697     }
2698     fSPDTrackleter->LoadClusters(tree);
2699     fSPDTrackleter->SetVertex(vtxPos, vtxErr);
2700     // run trackleting
2701     if (fSPDTrackleter->Clusters2Tracks(esd) != 0) {
2702       AliWarning("AliITSTrackleterSPDEff Clusters2Tracks failed");
2703      // fLoader[0]->UnloadRecPoints();
2704       return kFALSE;
2705     }
2706 //fSPDTrackleter->UnloadRecPoints();
2707   } else {
2708     AliWarning("SPDTrackleter not available");
2709     return kFALSE;
2710   }
2711   return kTRUE;
2712 }
2713
2714 //_____________________________________________________________________________
2715 Bool_t AliReconstruction::RunVertexFinder(AliESDEvent*& esd)
2716 {
2717 // run the barrel tracking
2718
2719   AliCodeTimerAuto("",0)
2720
2721   AliVertexer *vertexer = CreateVertexer();
2722   if (!vertexer) return kFALSE;
2723
2724   AliInfo(Form("running the ITS vertex finder: %s",vertexer->ClassName()));
2725   AliESDVertex* vertex = NULL;
2726   if (fLoader[0]) {
2727     fLoader[0]->LoadRecPoints();
2728     TTree* cltree = fLoader[0]->TreeR();
2729     if (cltree) {
2730       if(fDiamondProfileSPD) vertexer->SetVtxStart(fDiamondProfileSPD);
2731       vertex = vertexer->FindVertexForCurrentEvent(cltree);
2732     }
2733     else {
2734       AliError("Can't get the ITS cluster tree");
2735     }
2736     fLoader[0]->UnloadRecPoints();
2737   }
2738   else {
2739     AliError("Can't get the ITS loader");
2740   }
2741   if(!vertex){
2742     AliWarning("Vertex not found");
2743     vertex = new AliESDVertex();
2744     vertex->SetName("default");
2745   }
2746   else {
2747     vertex->SetName("reconstructed");
2748   }
2749
2750   Double_t vtxPos[3];
2751   Double_t vtxErr[3];
2752   vertex->GetXYZ(vtxPos);
2753   vertex->GetSigmaXYZ(vtxErr);
2754
2755   esd->SetPrimaryVertexSPD(vertex);
2756   AliESDVertex *vpileup = NULL;
2757   Int_t novertices = 0;
2758   vpileup = vertexer->GetAllVertices(novertices);
2759   if(novertices>1){
2760     for (Int_t kk=1; kk<novertices; kk++)esd->AddPileupVertexSPD(&vpileup[kk]);
2761   }
2762   /*
2763   // if SPD multiplicity has been determined, it is stored in the ESD
2764   AliMultiplicity *mult = vertexer->GetMultiplicity();
2765   if(mult)esd->SetMultiplicity(mult);
2766   */
2767   for (Int_t iDet = 0; iDet < kNDetectors; iDet++) {
2768     if (fTracker[iDet]) fTracker[iDet]->SetVertex(vtxPos, vtxErr);
2769   }  
2770   delete vertex;
2771
2772   delete vertexer;
2773
2774   return kTRUE;
2775 }
2776
2777 //_____________________________________________________________________________
2778 Bool_t AliReconstruction::RunMultFinder(AliESDEvent*& esd)
2779 {
2780   // run the trackleter for multiplicity study
2781
2782   AliCodeTimerAuto("",0)
2783
2784   AliTrackleter *trackleter = CreateMultFinder();
2785   if (!trackleter) return kFALSE;
2786
2787   AliInfo(Form("running the ITS multiplicity finder: %s",trackleter->ClassName()));
2788
2789   if (fLoader[0]) {
2790     fLoader[0]->LoadRecPoints();
2791     TTree* cltree = fLoader[0]->TreeR();
2792     if (cltree) {
2793       trackleter->Reconstruct(esd,cltree);
2794       AliMultiplicity *mult = trackleter->GetMultiplicity();
2795       if(mult) esd->SetMultiplicity(mult);
2796     }
2797     else {
2798       AliError("Can't get the ITS cluster tree");
2799     }
2800     fLoader[0]->UnloadRecPoints();
2801   }
2802   else {
2803     AliError("Can't get the ITS loader");
2804   }
2805
2806   delete trackleter;
2807
2808   return kTRUE;
2809 }
2810
2811 //_____________________________________________________________________________
2812 Bool_t AliReconstruction::RunHLTTracking(AliESDEvent*& esd)
2813 {
2814 // run the HLT barrel tracking
2815
2816   AliCodeTimerAuto("",0)
2817
2818   if (!fRunLoader) {
2819     AliError("Missing runLoader!");
2820     return kFALSE;
2821   }
2822
2823   AliInfo("running HLT tracking");
2824
2825   // Get a pointer to the HLT reconstructor
2826   AliReconstructor *reconstructor = GetReconstructor(kNDetectors-1);
2827   if (!reconstructor) return kFALSE;
2828
2829   // TPC + ITS
2830   for (Int_t iDet = 1; iDet >= 0; iDet--) {
2831     TString detName = fgkDetectorName[iDet];
2832     AliDebug(1, Form("%s HLT tracking", detName.Data()));
2833     reconstructor->SetOption(detName.Data());
2834     AliTracker *tracker = reconstructor->CreateTracker();
2835     if (!tracker) {
2836       AliWarning(Form("couldn't create a HLT tracker for %s", detName.Data()));
2837       if (fStopOnError) return kFALSE;
2838       continue;
2839     }
2840     Double_t vtxPos[3];
2841     Double_t vtxErr[3]={0.005,0.005,0.010};
2842     const AliESDVertex *vertex = esd->GetVertex();
2843     vertex->GetXYZ(vtxPos);
2844     tracker->SetVertex(vtxPos,vtxErr);
2845     if(iDet != 1) {
2846       fLoader[iDet]->LoadRecPoints("read");
2847       TTree* tree = fLoader[iDet]->TreeR();
2848       if (!tree) {
2849         AliError(Form("Can't get the %s cluster tree", detName.Data()));
2850         return kFALSE;
2851       }
2852       tracker->LoadClusters(tree);
2853     }
2854     if (tracker->Clusters2Tracks(esd) != 0) {
2855       AliError(Form("HLT %s Clusters2Tracks failed", fgkDetectorName[iDet]));
2856       return kFALSE;
2857     }
2858     if(iDet != 1) {
2859       tracker->UnloadClusters();
2860     }
2861     delete tracker;
2862   }
2863
2864   return kTRUE;
2865 }
2866
2867 //_____________________________________________________________________________
2868 Bool_t AliReconstruction::RunMuonTracking(AliESDEvent*& esd)
2869 {
2870 // run the muon spectrometer tracking
2871
2872   AliCodeTimerAuto("",0)
2873
2874   if (!fRunLoader) {
2875     AliError("Missing runLoader!");
2876     return kFALSE;
2877   }
2878   Int_t iDet =  GetDetIndex("MUON"); // for MUON
2879
2880   // Get a pointer to the MUON reconstructor
2881   AliReconstructor *reconstructor = GetReconstructor(iDet);
2882   if (!reconstructor) return kFALSE;
2883
2884   
2885   TString detName = fgkDetectorName[iDet];
2886   AliDebug(1, Form("%s tracking", detName.Data()));
2887   AliTracker *tracker =  reconstructor->CreateTracker();
2888   if (!tracker) {
2889     AliWarning(Form("couldn't create a tracker for %s", detName.Data()));
2890     return kFALSE;
2891   }
2892   tracker->SetPools(fPools);
2893      
2894   // read RecPoints
2895   fLoader[iDet]->LoadRecPoints("read");  
2896
2897   tracker->LoadClusters(fLoader[iDet]->TreeR());
2898   
2899   Int_t rv = tracker->Clusters2Tracks(esd);
2900   
2901   fLoader[iDet]->UnloadRecPoints();
2902
2903   tracker->UnloadClusters();
2904   
2905   if ( rv )
2906   {
2907     AliError(Form("%s Clusters2Tracks failed", fgkDetectorName[iDet]));
2908     return kFALSE;
2909   }
2910   
2911   return kTRUE;
2912 }
2913
2914
2915 //_____________________________________________________________________________
2916 Bool_t AliReconstruction::RunTracking(AliESDEvent*& esd,AliESDpid &PID)
2917 {
2918 // run the barrel tracking
2919   static Int_t eventNr=0;
2920   AliCodeTimerAuto("",0)
2921
2922   AliInfo("running tracking");
2923
2924   // Set the event info which is used
2925   // by the trackers in order to obtain
2926   // information about read-out detectors,
2927   // trigger etc.
2928   AliDebug(1, "Setting event info");
2929   for (Int_t iDet = 0; iDet < kNDetectors; iDet++) {
2930     if (!fTracker[iDet]) continue;
2931     fTracker[iDet]->SetEventInfo(&fEventInfo);
2932     fTracker[iDet]->SetPools(fPools);
2933   }
2934
2935   //Fill the ESD with the T0 info (will be used by the TOF) 
2936   if (fReconstructor[11] && fLoader[11]) {
2937     fLoader[11]->LoadRecPoints("READ");
2938     TTree *treeR = fLoader[11]->TreeR();
2939     if (treeR) {
2940       GetReconstructor(11)->FillESD((TTree *)NULL,treeR,esd);
2941     }
2942   }
2943
2944   // pass 1: TPC + ITS inwards
2945   for (Int_t iDet = 1; iDet >= 0; iDet--) {
2946     if (!fTracker[iDet]) continue;
2947     AliDebug(1, Form("%s tracking", fgkDetectorName[iDet]));
2948
2949     // load clusters
2950     fLoader[iDet]->LoadRecPoints("read");
2951     AliSysInfo::AddStamp(Form("RLoadCluster%s_%d",fgkDetectorName[iDet],eventNr),iDet,1, eventNr);
2952     TTree* tree = fLoader[iDet]->TreeR();
2953     if (!tree) {
2954       AliError(Form("Can't get the %s cluster tree", fgkDetectorName[iDet]));
2955       return kFALSE;
2956     }
2957     fTracker[iDet]->LoadClusters(tree);
2958     AliSysInfo::AddStamp(Form("TLoadCluster%s_%d",fgkDetectorName[iDet],eventNr), iDet,2, eventNr);
2959     // run tracking
2960     if (fTracker[iDet]->Clusters2Tracks(esd) != 0) {
2961       AliError(Form("%s Clusters2Tracks failed", fgkDetectorName[iDet]));
2962       return kFALSE;
2963     }
2964     AliSysInfo::AddStamp(Form("Tracking0%s_%d",fgkDetectorName[iDet],eventNr), iDet,3,eventNr);
2965     // preliminary PID in TPC needed by the ITS tracker
2966     if (iDet == 1) {
2967       GetReconstructor(1)->FillESD((TTree*)NULL, (TTree*)NULL, esd);
2968       PID.MakePID(esd,kTRUE);
2969       AliSysInfo::AddStamp(Form("MakePID0%s_%d",fgkDetectorName[iDet],eventNr), iDet,4,eventNr);
2970     } 
2971   }
2972
2973   // pass 2: ALL backwards
2974
2975   for (Int_t iDet = 0; iDet < kNDetectors; iDet++) {
2976     if (!fTracker[iDet]) continue;
2977     AliDebug(1, Form("%s back propagation", fgkDetectorName[iDet]));
2978
2979     // load clusters
2980     if (iDet > 1) {     // all except ITS, TPC
2981       TTree* tree = NULL;
2982       fLoader[iDet]->LoadRecPoints("read");
2983       AliSysInfo::AddStamp(Form("RLoadCluster0%s_%d",fgkDetectorName[iDet],eventNr), iDet,1, eventNr);
2984       tree = fLoader[iDet]->TreeR();
2985       if (!tree) {
2986         AliError(Form("Can't get the %s cluster tree", fgkDetectorName[iDet]));
2987         return kFALSE;
2988       }
2989       fTracker[iDet]->LoadClusters(tree); 
2990       AliSysInfo::AddStamp(Form("TLoadCluster0%s_%d",fgkDetectorName[iDet],eventNr), iDet,2, eventNr);
2991     }
2992
2993     // run tracking
2994     if (iDet>1) // start filling residuals for the "outer" detectors
2995       if (fRunGlobalQA) {
2996         AliTracker::SetFillResiduals(fRecoParam.GetEventSpecie(), kTRUE);     
2997         TObjArray ** arr = AliTracker::GetResidualsArray() ; 
2998         if (arr) {
2999           AliRecoParam::EventSpecie_t es=fRecoParam.GetEventSpecie();
3000           TObjArray * elem = arr[AliRecoParam::AConvert(es)];
3001           if ( elem && (! elem->At(0)) ) {
3002             AliQADataMaker *qadm = AliQAManager::QAManager()->GetQADataMaker(AliQAv1::kGLOBAL);
3003             if (qadm) qadm->InitRecPointsForTracker() ; 
3004           }
3005         }
3006         //      AliSysInfo::AddStamp(Form("QAInitResid%s_%d",fgkDetectorName[iDet],eventNr), iDet,0, eventNr);
3007       }
3008     if (fTracker[iDet]->PropagateBack(esd) != 0) {
3009       AliError(Form("%s backward propagation failed", fgkDetectorName[iDet]));
3010       //      return kFALSE;
3011     }
3012     AliSysInfo::AddStamp(Form("Tracking1%s_%d",fgkDetectorName[iDet],eventNr), iDet,3, eventNr);
3013
3014     // unload clusters
3015     if (iDet > 3) {     // all except ITS, TPC, TRD and TOF
3016       fTracker[iDet]->UnloadClusters();
3017       fLoader[iDet]->UnloadRecPoints();
3018     }
3019     // updated PID in TPC needed by the ITS tracker -MI
3020     if (iDet == 1) {
3021       //GetReconstructor(1)->FillESD((TTree*)NULL, (TTree*)NULL, esd);
3022       //AliESDpid::MakePID(esd);
3023       PID.MakePID(esd,kTRUE);
3024       AliSysInfo::AddStamp(Form("MakePID1%s_%d",fgkDetectorName[iDet],eventNr), iDet,4,eventNr);
3025     }
3026
3027   }
3028   //stop filling residuals for the "outer" detectors
3029   if (fRunGlobalQA) AliTracker::SetFillResiduals(fRecoParam.GetEventSpecie(), kFALSE);     
3030
3031   // pass 3: TRD + TPC + ITS refit inwards
3032
3033   for (Int_t iDet = 2; iDet >= 0; iDet--) {
3034     if (!fTracker[iDet]) continue;
3035     AliDebug(1, Form("%s inward refit", fgkDetectorName[iDet]));
3036
3037     // run tracking
3038     if (iDet<2) // start filling residuals for TPC and ITS
3039       if (fRunGlobalQA) {
3040         AliTracker::SetFillResiduals(fRecoParam.GetEventSpecie(), kTRUE);     
3041         TObjArray ** arr = AliTracker::GetResidualsArray() ; 
3042         if (arr) {
3043           AliRecoParam::EventSpecie_t es=fRecoParam.GetEventSpecie();
3044           TObjArray * elem = arr[AliRecoParam::AConvert(es)];
3045           if ( elem && (! elem->At(0)) ) {
3046             AliQADataMaker *qadm = AliQAManager::QAManager()->GetQADataMaker(AliQAv1::kGLOBAL);
3047             if (qadm) qadm->InitRecPointsForTracker() ; 
3048           }
3049         }
3050       }
3051     
3052     if (fTracker[iDet]->RefitInward(esd) != 0) {
3053       AliError(Form("%s inward refit failed", fgkDetectorName[iDet]));
3054       //      return kFALSE;
3055     }
3056     // run postprocessing
3057     if (fTracker[iDet]->PostProcess(esd) != 0) {
3058       AliError(Form("%s postprocessing failed", fgkDetectorName[iDet]));
3059       //      return kFALSE;
3060     }
3061     AliSysInfo::AddStamp(Form("Tracking2%s_%d",fgkDetectorName[iDet],eventNr), iDet,3, eventNr);
3062   }
3063
3064   // write space-points to the ESD in case alignment data output
3065   // is switched on
3066   if (fWriteAlignmentData) {
3067     WriteAlignmentData(esd);
3068     AliSysInfo::AddStamp(Form("WrtAlignData_%d",eventNr), 0,0, eventNr);
3069   }
3070   
3071   for (Int_t iDet = 3; iDet >= 0; iDet--) {
3072     if (!fTracker[iDet]) continue;
3073     // unload clusters
3074     fTracker[iDet]->UnloadClusters();
3075     AliSysInfo::AddStamp(Form("TUnloadCluster%s_%d",fgkDetectorName[iDet],eventNr), iDet,4, eventNr);
3076     fLoader[iDet]->UnloadRecPoints();
3077     AliSysInfo::AddStamp(Form("RUnloadCluster%s_%d",fgkDetectorName[iDet],eventNr), iDet,5, eventNr);
3078   }
3079   // stop filling residuals for TPC and ITS
3080   if (fRunGlobalQA) AliTracker::SetFillResiduals(fRecoParam.GetEventSpecie(), kFALSE);     
3081
3082   eventNr++;
3083   return kTRUE;
3084 }
3085
3086 //_____________________________________________________________________________
3087 Bool_t AliReconstruction::CleanESD(AliESDEvent *esd){
3088   //
3089   // Remove the data which are not needed for the physics analysis.
3090   //
3091
3092   Int_t nTracks=esd->GetNumberOfTracks();
3093   Int_t nV0s=esd->GetNumberOfV0s();
3094   AliInfo
3095   (Form("Number of ESD tracks and V0s before cleaning: %d %d",nTracks,nV0s));
3096
3097   Float_t cleanPars[]={fV0DCAmax,fV0CsPmin,fDmax,fZmax};
3098   Bool_t rc=esd->Clean(cleanPars);
3099
3100   nTracks=esd->GetNumberOfTracks();
3101   nV0s=esd->GetNumberOfV0s();
3102   AliInfo
3103   (Form("Number of ESD tracks and V0s after cleaning %d %d",nTracks,nV0s));
3104
3105   return rc;
3106 }
3107
3108 //_____________________________________________________________________________
3109 Bool_t AliReconstruction::FillESD(AliESDEvent*& esd, const TString& detectors)
3110 {
3111 // fill the event summary data
3112
3113   AliCodeTimerAuto("",0)
3114     static Int_t eventNr=0; 
3115   TString detStr = detectors;
3116   
3117   AliSysInfo::AddStamp(Form("FillESDb%d",eventNr), -19,-19, eventNr);
3118   for (Int_t iDet = 0; iDet < kNDetectors; iDet++) {
3119   if (!IsSelected(fgkDetectorName[iDet], detStr)) continue;
3120     AliReconstructor* reconstructor = GetReconstructor(iDet);
3121     if (!reconstructor) continue;
3122     AliDebug(1, Form("filling ESD for %s", fgkDetectorName[iDet]));
3123     TTree* clustersTree = NULL;
3124     if (fLoader[iDet]) {
3125       fLoader[iDet]->LoadRecPoints("read");
3126       clustersTree = fLoader[iDet]->TreeR();
3127       if (!clustersTree) {
3128         AliError(Form("Can't get the %s clusters tree", 
3129                       fgkDetectorName[iDet]));
3130         if (fStopOnError) return kFALSE;
3131       }
3132     }
3133     if (fRawReader && !reconstructor->HasDigitConversion()) {
3134       reconstructor->FillESD(fRawReader, clustersTree, esd);
3135     } else {
3136       TTree* digitsTree = NULL;
3137       if (fLoader[iDet]) {
3138         fLoader[iDet]->LoadDigits("read");
3139         digitsTree = fLoader[iDet]->TreeD();
3140         if (!digitsTree) {
3141           AliError(Form("Can't get the %s digits tree", 
3142                         fgkDetectorName[iDet]));
3143           if (fStopOnError) return kFALSE;
3144         }
3145       }
3146       reconstructor->FillESD(digitsTree, clustersTree, esd);
3147       if (fLoader[iDet]) fLoader[iDet]->UnloadDigits();
3148     }
3149     if (fLoader[iDet]) {
3150       fLoader[iDet]->UnloadRecPoints();
3151     }
3152   }
3153   
3154   if (!IsSelected("CTP", detStr)) AliDebug(10,"No CTP");
3155   if ((detStr.CompareTo("ALL") != 0) && !detStr.IsNull()) {
3156     AliError(Form("the following detectors were not found: %s", 
3157                   detStr.Data()));
3158     if (fStopOnError) return kFALSE;
3159   }
3160   AliSysInfo::AddStamp(Form("FillESDe%d",eventNr), -20,-20, eventNr);
3161   eventNr++;
3162   return kTRUE;
3163 }
3164
3165 //_____________________________________________________________________________
3166 Bool_t AliReconstruction::FillTriggerESD(AliESDEvent*& esd)
3167 {
3168   // Reads the trigger decision which is
3169   // stored in Trigger.root file and fills
3170   // the corresponding esd entries
3171
3172   AliCodeTimerAuto("",0)
3173   
3174   AliInfo("Filling trigger information into the ESD");
3175
3176   if (fRawReader) {
3177     AliCTPRawStream input(fRawReader);
3178     if (!input.Next()) {
3179       AliWarning("No valid CTP (trigger) DDL raw data is found ! The trigger info is taken from the event header!");
3180     }
3181     else {
3182       if (esd->GetTriggerMask() != input.GetClassMask())
3183         AliError(Form("Invalid trigger pattern found in CTP raw-data: %llx %llx",
3184                       input.GetClassMask(),esd->GetTriggerMask()));
3185       if (esd->GetOrbitNumber() != input.GetOrbitID())
3186         AliError(Form("Invalid orbit id found in CTP raw-data: %x %x",
3187                       input.GetOrbitID(),esd->GetOrbitNumber()));
3188       if (esd->GetBunchCrossNumber() != input.GetBCID())
3189         AliError(Form("Invalid bunch-crossing id found in CTP raw-data: %x %x",
3190                       input.GetBCID(),esd->GetBunchCrossNumber()));
3191       AliESDHeader* esdheader = esd->GetHeader();
3192       esdheader->SetL0TriggerInputs(input.GetL0Inputs());
3193       esdheader->SetL1TriggerInputs(input.GetL1Inputs());
3194       esdheader->SetL2TriggerInputs(input.GetL2Inputs());
3195       // IR
3196       //      UInt_t orbit=input.GetOrbitID();
3197       for(Int_t i=0 ; i<input.GetNIRs() ; i++ ) {
3198         esdheader->AddTriggerIR(input.GetIR(i));
3199       }
3200        AliCentralTrigger* rlCTP = fRunLoader->GetTrigger();
3201        rlCTP->SetL0TriggerInputs(input.GetL0Inputs());
3202        rlCTP->SetL1TriggerInputs(input.GetL1Inputs());
3203        rlCTP->SetL2TriggerInputs(input.GetL2Inputs());
3204     }
3205     if (fIsNewRunLoader) fRunLoader->TreeCT()->Fill();
3206   }
3207   return kTRUE;
3208 }
3209 //_____________________________________________________________________________
3210 Bool_t AliReconstruction::FillTriggerScalers(AliESDEvent*& esd)
3211 {
3212   //Scalers
3213   //fRunScalers->Print();
3214   if(fRunScalers && fRunScalers->CheckRunScalers()){
3215      AliTimeStamp* timestamp = new AliTimeStamp(esd->GetOrbitNumber(), esd->GetPeriodNumber(), esd->GetBunchCrossNumber());
3216      //AliTimeStamp* timestamp = new AliTimeStamp(10308000, 0, (ULong64_t)486238);
3217      AliESDHeader* esdheader = fesd->GetHeader();
3218      for(Int_t i=0;i<50;i++){
3219           if((1ull<<i) & esd->GetTriggerMask()){
3220           AliTriggerScalersESD* scalesd = fRunScalers->GetScalersForEventClass( timestamp, i+1);
3221           if(scalesd)esdheader->SetTriggerScalersRecord(scalesd);
3222         }
3223      }
3224      const AliTriggerScalersRecordESD* scalrecEvent = fRunScalers->GetScalersDeltaForEvent( timestamp);
3225      const AliTriggerScalersRecordESD* scalrecRun = fRunScalers->GetScalersDeltaForRun();
3226      if (scalrecEvent) esdheader->SetTriggerScalersDeltaEvent(scalrecEvent);
3227      if (scalrecRun) esdheader->SetTriggerScalersDeltaRun(scalrecRun);
3228   }
3229   return kTRUE;
3230 }
3231 //_____________________________________________________________________________
3232 Bool_t AliReconstruction::FillRawEventHeaderESD(AliESDEvent*& esd)
3233 {
3234   // 
3235   // Filling information from RawReader Header
3236   // 
3237
3238   if (!fRawReader) return kFALSE;
3239
3240   AliInfo("Filling information from RawReader Header");
3241
3242   esd->SetBunchCrossNumber(fRawReader->GetBCID());
3243   esd->SetOrbitNumber(fRawReader->GetOrbitID());
3244   esd->SetPeriodNumber(fRawReader->GetPeriod());
3245
3246   esd->SetTimeStamp(fRawReader->GetTimestamp());  
3247   esd->SetEventType(fRawReader->GetType());
3248
3249   return kTRUE;
3250 }
3251
3252
3253 //_____________________________________________________________________________
3254 Bool_t AliReconstruction::IsSelected(TString detName, TString& detectors) const
3255 {
3256 // check whether detName is contained in detectors
3257 // if yes, it is removed from detectors
3258
3259   // check if all detectors are selected
3260   if ((detectors.CompareTo("ALL") == 0) ||
3261       detectors.BeginsWith("ALL ") ||
3262       detectors.EndsWith(" ALL") ||
3263       detectors.Contains(" ALL ")) {
3264     detectors = "ALL";
3265     return kTRUE;
3266   }
3267
3268   // search for the given detector
3269   Bool_t result = kFALSE;
3270   if ((detectors.CompareTo(detName) == 0) ||
3271       detectors.BeginsWith(detName+" ") ||
3272       detectors.EndsWith(" "+detName) ||
3273       detectors.Contains(" "+detName+" ")) {
3274     detectors.ReplaceAll(detName, "");
3275     result = kTRUE;
3276   }
3277
3278   // clean up the detectors string
3279   while (detectors.Contains("  ")) detectors.ReplaceAll("  ", " ");
3280   while (detectors.BeginsWith(" ")) detectors.Remove(0, 1);
3281   while (detectors.EndsWith(" ")) detectors.Remove(detectors.Length()-1, 1);
3282
3283   return result;
3284 }
3285
3286 //_____________________________________________________________________________
3287 Bool_t AliReconstruction::InitRunLoader()
3288 {
3289 // get or create the run loader
3290
3291   if (gAlice) delete gAlice;
3292   gAlice = NULL;
3293
3294   TFile *gafile = TFile::Open(fGAliceFileName.Data());
3295   //  if (!gSystem->AccessPathName(fGAliceFileName.Data())) { // galice.root exists
3296   if (gafile) { // galice.root exists
3297     gafile->Close();
3298     delete gafile;
3299
3300     // load all base libraries to get the loader classes
3301     TString libs = gSystem->GetLibraries();
3302     for (Int_t iDet = 0; iDet < kNDetectors; iDet++) {
3303       TString detName = fgkDetectorName[iDet];
3304       if (detName == "HLT") continue;
3305       if (libs.Contains("lib" + detName + "base.so")) continue;
3306       gSystem->Load("lib" + detName + "base.so");
3307     }
3308     fRunLoader = AliRunLoader::Open(fGAliceFileName.Data());
3309     if (!fRunLoader) {
3310       AliError(Form("no run loader found in file %s", fGAliceFileName.Data()));
3311       CleanUp();
3312       return kFALSE;
3313     }
3314
3315     fRunLoader->CdGAFile();
3316     fRunLoader->LoadgAlice();
3317
3318     //PH This is a temporary fix to give access to the kinematics
3319     //PH that is needed for the labels of ITS clusters
3320     fRunLoader->LoadHeader();
3321     fRunLoader->LoadKinematics();
3322
3323   } else {               // galice.root does not exist
3324     if (!fRawReader) {
3325       AliError(Form("the file %s does not exist", fGAliceFileName.Data()));
3326     }
3327     fRunLoader = AliRunLoader::Open(fGAliceFileName.Data(),
3328                                     AliConfig::GetDefaultEventFolderName(),
3329                                     "recreate");
3330     if (!fRunLoader) {
3331       AliError(Form("could not create run loader in file %s", 
3332                     fGAliceFileName.Data()));
3333       CleanUp();
3334       return kFALSE;
3335     }
3336     fIsNewRunLoader = kTRUE;
3337     fRunLoader->MakeTree("E");
3338     fRunLoader->MakeTree("GG");
3339
3340     if (fNumberOfEventsPerFile > 0)
3341       fRunLoader->SetNumberOfEventsPerFile(fNumberOfEventsPerFile);
3342     else
3343       fRunLoader->SetNumberOfEventsPerFile((UInt_t)-1);
3344   }
3345
3346   return kTRUE;
3347 }
3348
3349 //_____________________________________________________________________________
3350 AliReconstructor* AliReconstruction::GetReconstructor(Int_t iDet)
3351 {
3352 // get the reconstructor object and the loader for a detector
3353
3354   if (fReconstructor[iDet]) {
3355     if (fRecoParam.GetDetRecoParamArray(iDet) && !AliReconstructor::GetRecoParam(iDet)) {
3356       const AliDetectorRecoParam *par = fRecoParam.GetDetRecoParam(iDet);
3357       fReconstructor[iDet]->SetRecoParam(par);
3358       fReconstructor[iDet]->SetRunInfo(fRunInfo);
3359     }
3360     return fReconstructor[iDet];
3361   }
3362
3363   // load the reconstructor object
3364   TPluginManager* pluginManager = gROOT->GetPluginManager();
3365   TString detName = fgkDetectorName[iDet];
3366   TString recName = "Ali" + detName + "Reconstructor";
3367
3368   if (!fIsNewRunLoader && !fRunLoader->GetLoader(detName+"Loader") && (detName != "HLT")) return NULL;
3369
3370   AliReconstructor* reconstructor = NULL;
3371   // first check if a plugin is defined for the reconstructor
3372   TPluginHandler* pluginHandler = 
3373     pluginManager->FindHandler("AliReconstructor", detName);
3374   // if not, add a plugin for it
3375   if (!pluginHandler) {
3376     AliDebug(1, Form("defining plugin for %s", recName.Data()));
3377     TString libs = gSystem->GetLibraries();
3378     if (libs.Contains("lib" + detName + "base.so") ||
3379         (gSystem->Load("lib" + detName + "base.so") >= 0)) {
3380       pluginManager->AddHandler("AliReconstructor", detName, 
3381                                 recName, detName + "rec", recName + "()");
3382     } else {
3383       pluginManager->AddHandler("AliReconstructor", detName, 
3384                                 recName, detName, recName + "()");
3385     }
3386     pluginHandler = pluginManager->FindHandler("AliReconstructor", detName);
3387   }
3388   if (pluginHandler && (pluginHandler->LoadPlugin() == 0)) {
3389     reconstructor = (AliReconstructor*) pluginHandler->ExecPlugin(0);
3390   }
3391
3392    // check if the upgrade reconstructor should be used instead of the standard one
3393   if(fUpgradeMask[iDet]) {
3394     if(reconstructor) delete reconstructor;
3395     TClass *cl = new TClass(Form("Ali%sUpgradeReconstructor",fgkDetectorName[iDet]));
3396     reconstructor = (AliReconstructor*)(cl->New());
3397    }
3398
3399   if (reconstructor) {
3400     TObject* obj = fOptions.FindObject(detName.Data());
3401     if (obj) reconstructor->SetOption(obj->GetTitle());
3402     reconstructor->SetRunInfo(fRunInfo);
3403     reconstructor->Init();
3404     fReconstructor[iDet] = reconstructor;
3405   }
3406
3407   // get or create the loader
3408   if (detName != "HLT") {
3409     fLoader[iDet] = fRunLoader->GetLoader(detName + "Loader");
3410     if (!fLoader[iDet]) {
3411       AliConfig::Instance()
3412         ->CreateDetectorFolders(fRunLoader->GetEventFolder(), 
3413                                 detName, detName);
3414       // first check if a plugin is defined for the loader
3415       pluginHandler = 
3416         pluginManager->FindHandler("AliLoader", detName);
3417       // if not, add a plugin for it
3418       if (!pluginHandler) {
3419         TString loaderName = "Ali" + detName + "Loader";
3420         AliDebug(1, Form("defining plugin for %s", loaderName.Data()));
3421         pluginManager->AddHandler("AliLoader", detName, 
3422                                   loaderName, detName + "base", 
3423                                   loaderName + "(const char*, TFolder*)");
3424         pluginHandler = pluginManager->FindHandler("AliLoader", detName);
3425       }
3426       if (pluginHandler && (pluginHandler->LoadPlugin() == 0)) {
3427         fLoader[iDet] = 
3428           (AliLoader*) pluginHandler->ExecPlugin(2, detName.Data(), 
3429                                                  fRunLoader->GetEventFolder());
3430       }
3431       if (!fLoader[iDet]) {   // use default loader
3432         fLoader[iDet] = new AliLoader(detName, fRunLoader->GetEventFolder());
3433       }
3434       if (!fLoader[iDet]) {
3435         AliWarning(Form("couldn't get loader for %s", detName.Data()));
3436         if (fStopOnError) return NULL;
3437       } else {
3438         fRunLoader->AddLoader(fLoader[iDet]);
3439         fRunLoader->CdGAFile();
3440         if (gFile && !gFile->IsWritable()) gFile->ReOpen("UPDATE");
3441         fRunLoader->Write(0, TObject::kOverwrite);
3442       }
3443     }
3444   }
3445       
3446   if (fRecoParam.GetDetRecoParamArray(iDet) && !AliReconstructor::GetRecoParam(iDet)) {
3447     const AliDetectorRecoParam *par = fRecoParam.GetDetRecoParam(iDet);
3448     if (reconstructor) {
3449       reconstructor->SetRecoParam(par);
3450       reconstructor->SetRunInfo(fRunInfo);
3451     }
3452   }
3453   return reconstructor;
3454 }
3455
3456 //_____________________________________________________________________________
3457 AliVertexer* AliReconstruction::CreateVertexer()
3458 {
3459 // create the vertexer
3460 // Please note that the caller is the owner of the
3461 // vertexer
3462
3463   AliVertexer* vertexer = NULL;
3464   AliReconstructor* itsReconstructor = GetReconstructor(0);
3465   if (itsReconstructor && ((fRunLocalReconstruction.Contains("ITS")) || 
3466                            fRunTracking.Contains("ITS") || fFillESD.Contains("ITS") )) {
3467     vertexer = itsReconstructor->CreateVertexer();
3468   }
3469   if (!vertexer) {
3470     AliWarning("couldn't create a vertexer for ITS");
3471   }
3472
3473   return vertexer;
3474 }
3475
3476 //_____________________________________________________________________________
3477 AliTrackleter* AliReconstruction::CreateMultFinder()
3478 {
3479 // create the ITS trackleter for mult. estimation
3480 // Please note that the caller is the owner of the
3481 // trackleter
3482
3483   AliTrackleter* trackleter = NULL;
3484   AliReconstructor* itsReconstructor = GetReconstructor(0);
3485   if (itsReconstructor && ((fRunLocalReconstruction.Contains("ITS")) || 
3486                            fRunTracking.Contains("ITS") || fFillESD.Contains("ITS") )) {
3487     trackleter = itsReconstructor->CreateMultFinder();
3488   }
3489   else {
3490     AliWarning("ITS is not in reconstruction, switching off RunMultFinder");
3491     fRunMultFinder = kFALSE;
3492   }
3493
3494   return trackleter;
3495 }
3496
3497 //_____________________________________________________________________________
3498 Bool_t AliReconstruction::CreateTrackers(const TString& detectors)
3499 {
3500 // create the trackers
3501         AliInfo("Creating trackers");
3502
3503   TString detStr = detectors;
3504   for (Int_t iDet = 0; iDet < kNDetectors; iDet++) {
3505     if (!IsSelected(fgkDetectorName[iDet], detStr)) continue;
3506     AliReconstructor* reconstructor = GetReconstructor(iDet);
3507     if (!reconstructor) continue;
3508     TString detName = fgkDetectorName[iDet];
3509     if (detName == "HLT") {
3510       fRunHLTTracking = kTRUE;
3511       continue;
3512     }
3513     if (detName == "MUON") {
3514       fRunMuonTracking = kTRUE;
3515       continue;
3516     }
3517
3518     fTracker[iDet] = reconstructor->CreateTracker();
3519     if (!fTracker[iDet] && (iDet < 7)) {
3520       AliWarning(Form("couldn't create a tracker for %s", detName.Data()));
3521       if (fStopOnError) return kFALSE;
3522     }
3523     AliSysInfo::AddStamp(Form("LTracker%s",fgkDetectorName[iDet]), iDet,0);
3524   }
3525
3526   return kTRUE;
3527 }
3528
3529 //_____________________________________________________________________________
3530 void AliReconstruction::CleanUp()
3531 {
3532 // delete trackers and the run loader and close and delete the file
3533 /*
3534   for (Int_t iDet = 0; iDet < kNDetectors; iDet++) {
3535     delete fReconstructor[iDet];
3536     fReconstructor[iDet] = NULL;
3537     fLoader[iDet] = NULL;
3538     delete fTracker[iDet];
3539     fTracker[iDet] = NULL;
3540   }
3541 */
3542   if (fPools)  {
3543     if (fesd)    fesd->Reset();
3544     if (fhltesd) fhltesd->Reset();
3545     if (fesdf)   fesdf->Clear();
3546     fPools->ResetPools();
3547     AliTrackPointArray::SetPool(0);
3548     AliESDtrack::SetPools(0);
3549     AliESDfriendTrack::SetPools(0);
3550     delete fPools; fPools = 0;
3551   }
3552   //
3553
3554   delete fRunInfo;
3555   fRunInfo = NULL;
3556
3557   delete fSPDTrackleter;
3558   fSPDTrackleter = NULL;
3559
3560   delete ftVertexer;
3561   ftVertexer = NULL;
3562   
3563   delete fRunLoader;
3564   fRunLoader = NULL;
3565   delete fRawReader;
3566   fRawReader = NULL;
3567   delete fParentRawReader;
3568   fParentRawReader=NULL;
3569
3570   if (ffile) {
3571     ffile->Close();
3572     delete ffile;
3573     ffile = NULL;
3574   }
3575
3576   if (AliQAManager::QAManager())
3577     AliQAManager::QAManager()->ShowQA() ; 
3578   //  AliQAManager::Destroy() ;
3579   delete fAnalysis; 
3580   fAnalysis = NULL;
3581   //
3582 }
3583
3584 void AliReconstruction::WriteAlignmentData(AliESDEvent* esd)
3585 {
3586   // Write space-points which are then used in the alignment procedures
3587   // For the moment only ITS, TPC, TRD and TOF
3588   AliClonesPool* poolTPA = fPools ? fPools->GetPoolTrPoints() : 0;
3589
3590   Int_t ntracks = esd->GetNumberOfTracks();
3591   for (Int_t itrack = 0; itrack < ntracks; itrack++)
3592     {
3593       AliESDtrack *track = esd->GetTrack(itrack);
3594       Int_t nsp = 0;
3595       Int_t idx[200];
3596       for (Int_t i=0; i<200; ++i) idx[i] = -1; //PH avoid uninitialized values
3597       for (Int_t iDet = 5; iDet >= 0; iDet--) {// TOF, TRD, TPC, ITS clusters
3598           nsp += (iDet==GetDetIndex("TRD")) ? track->GetTRDntracklets():track->GetNcls(iDet);
3599
3600           if (iDet==GetDetIndex("ITS")) { // ITS "extra" clusters
3601              track->GetClusters(iDet,idx);
3602              for (Int_t i=6; i<12; i++) if(idx[i] >= 0) nsp++;
3603           }  
3604       }
3605
3606       if (nsp) {
3607         AliTrackPointArray *sp = 0;
3608         if (poolTPA) {sp = new(poolTPA->NextFreeSlot()) AliTrackPointArray(nsp); poolTPA->RegisterClone(sp);}
3609         else          sp = new AliTrackPointArray(nsp); 
3610         track->SetTrackPointArray(sp);
3611         Int_t isptrack = 0;
3612         for (Int_t iDet = 5; iDet >= 0; iDet--) {
3613           AliTracker *tracker = fTracker[iDet];
3614           if (!tracker) continue;
3615           Int_t nspdet = (iDet==GetDetIndex("TRD")) ? track->GetTRDtracklets(idx):track->GetClusters(iDet,idx);
3616
3617           if (iDet==GetDetIndex("ITS")) // ITS "extra" clusters             
3618              for (Int_t i=6; i<12; i++) if(idx[i] >= 0) nspdet++;
3619
3620           if (nspdet <= 0) continue;
3621           AliTrackPoint p;
3622           Int_t isp = 0;
3623           Int_t isp2 = 0;
3624           while (isp2 < nspdet) {
3625             Bool_t isvalid=kTRUE;
3626
3627             Int_t index=idx[isp++];
3628             if (index < 0) continue;
3629
3630             TString dets = fgkDetectorName[iDet];
3631             if ((fUseTrackingErrorsForAlignment.CompareTo(dets) == 0) ||
3632             fUseTrackingErrorsForAlignment.BeginsWith(dets+" ") ||
3633             fUseTrackingErrorsForAlignment.EndsWith(" "+dets) ||
3634             fUseTrackingErrorsForAlignment.Contains(" "+dets+" ")) {
3635               isvalid = tracker->GetTrackPointTrackingError(index,p,track);
3636             } else {
3637               isvalid = tracker->GetTrackPoint(index,p); 
3638             } 
3639             isp2++;
3640             if (!isvalid) continue;
3641             if (iDet==GetDetIndex("ITS") && (isp-1)>=6) p.SetExtra();
3642             sp->AddPoint(isptrack,&p); isptrack++;
3643           }
3644         }       
3645       }
3646     }
3647 }
3648
3649 //_____________________________________________________________________________
3650 void AliReconstruction::FillRawDataErrorLog(Int_t iEvent, AliESDEvent* esd)
3651 {
3652   // The method reads the raw-data error log
3653   // accumulated within the rawReader.
3654   // It extracts the raw-data errors related to
3655   // the current event and stores them into
3656   // a TClonesArray inside the esd object.
3657
3658   if (!fRawReader) return;
3659
3660   for(Int_t i = 0; i < fRawReader->GetNumberOfErrorLogs(); i++) {
3661
3662     AliRawDataErrorLog *log = fRawReader->GetErrorLog(i);
3663     if (!log) continue;
3664     if (iEvent != log->GetEventNumber()) continue;
3665
3666     esd->AddRawDataErrorLog(log);
3667   }
3668
3669 }
3670
3671 //_____________________________________________________________________________
3672 // void AliReconstruction::CheckQA()
3673 // {
3674 // check the QA of SIM for this run and remove the detectors 
3675 // with status Fatal
3676   
3677 //      TString newRunLocalReconstruction ; 
3678 //      TString newRunTracking ;
3679 //      TString newFillESD ;
3680 //       
3681 //      for (Int_t iDet = 0; iDet < AliQAv1::kNDET; iDet++) {
3682 //              TString detName(AliQAv1::GetDetName(iDet)) ;
3683 //              AliQAv1 * qa = AliQAv1::Instance(AliQAv1::DETECTORINDEX_t(iDet)) ;       
3684 //      if ( qa->IsSet(AliQAv1::DETECTORINDEX_t(iDet), AliQAv1::kSIM, specie, AliQAv1::kFATAL)) {
3685 //        AliInfo(Form("QA status for %s %s in Hits and/or SDIGITS  and/or Digits was Fatal; No reconstruction performed", 
3686 //                   detName.Data(), AliRecoParam::GetEventSpecieName(es))) ;
3687 //                      } else {
3688 //                      if ( fRunLocalReconstruction.Contains(AliQAv1::GetDetName(iDet)) || 
3689 //                                      fRunLocalReconstruction.Contains("ALL") )  {
3690 //                              newRunLocalReconstruction += detName ; 
3691 //                              newRunLocalReconstruction += " " ;                      
3692 //                      }
3693 //                      if ( fRunTracking.Contains(AliQAv1::GetDetName(iDet)) || 
3694 //                                      fRunTracking.Contains("ALL") )  {
3695 //                              newRunTracking += detName ; 
3696 //                              newRunTracking += " " ;                         
3697 //                      }
3698 //                      if ( fFillESD.Contains(AliQAv1::GetDetName(iDet)) || 
3699 //                                      fFillESD.Contains("ALL") )  {
3700 //                              newFillESD += detName ; 
3701 //                              newFillESD += " " ;                     
3702 //                      }
3703 //              }
3704 //      }
3705 //      fRunLocalReconstruction = newRunLocalReconstruction ; 
3706 //      fRunTracking            = newRunTracking ; 
3707 //      fFillESD                = newFillESD ; 
3708 // }
3709
3710 //_____________________________________________________________________________
3711 Int_t AliReconstruction::GetDetIndex(const char* detector)
3712 {
3713   // return the detector index corresponding to detector
3714   Int_t index = -1 ; 
3715   for (index = 0; index < kNDetectors ; index++) {
3716     if ( strcmp(detector, fgkDetectorName[index]) == 0 )
3717         break ; 
3718   }     
3719   return index ; 
3720 }
3721 //_____________________________________________________________________________
3722 Bool_t AliReconstruction::FinishPlaneEff() {
3723  //
3724  // Here execute all the necessary operationis, at the end of the tracking phase,
3725  // in case that evaluation of PlaneEfficiencies was required for some detector.
3726  // E.g., write into a DataBase file the PlaneEfficiency which have been evaluated.
3727  //
3728  // This Preliminary version works only FOR ITS !!!!!
3729  // other detectors (TOF,TRD, etc. have to develop their specific codes)
3730  //
3731  //  Input: none
3732  //  Return: kTRUE if all operations have been done properly, kFALSE otherwise
3733  //
3734  Bool_t ret=kFALSE;
3735  TString detStr = fLoadCDB;
3736  //for (Int_t iDet = 0; iDet < fgkNDetectors; iDet++) {
3737  for (Int_t iDet = 0; iDet < 1; iDet++) { // for the time being only ITS
3738    if (!IsSelected(fgkDetectorName[iDet], detStr)) continue;
3739    if(fTracker[iDet] && fTracker[iDet]->GetPlaneEff()) {
3740       AliPlaneEff *planeeff=fTracker[iDet]->GetPlaneEff();
3741       TString name=planeeff->GetName();
3742       name+=".root";
3743       TFile* pefile = TFile::Open(name, "RECREATE");
3744       ret=(Bool_t)planeeff->Write();
3745       pefile->Close();
3746       if(planeeff->GetCreateHistos()) {
3747         TString hname=planeeff->GetName();
3748         hname+="Histo.root";
3749         ret*=planeeff->WriteHistosToFile(hname,"RECREATE");
3750       }
3751    }
3752    if(fSPDTrackleter) {
3753      AliPlaneEff *planeeff=fSPDTrackleter->GetPlaneEff();
3754       TString name="AliITSPlaneEffSPDtracklet.root";
3755       TFile* pefile = TFile::Open(name, "RECREATE");
3756       ret=(Bool_t)planeeff->Write();
3757       pefile->Close();
3758       AliESDEvent *dummy=NULL;
3759       ret=(Bool_t)fSPDTrackleter->PostProcess(dummy); // take care of writing other files
3760    }
3761  }
3762  return ret;
3763 }
3764 //_____________________________________________________________________________
3765 Bool_t AliReconstruction::InitPlaneEff() {
3766 //
3767  // Here execute all the necessary operations, before of the tracking phase,
3768  // for the evaluation of PlaneEfficiencies, in case required for some detectors.
3769  // E.g., read from a DataBase file a first evaluation of the PlaneEfficiency
3770  // which should be updated/recalculated.
3771  //
3772  // This Preliminary version will work only FOR ITS !!!!!
3773  // other detectors (TOF,TRD, etc. have to develop their specific codes)
3774  //
3775  //  Input: none
3776  //  Return: kTRUE if all operations have been done properly, kFALSE otherwise
3777  //
3778
3779   fSPDTrackleter = NULL;
3780   TString detStr = fLoadCDB;
3781   if (IsSelected(fgkDetectorName[0], detStr)) {
3782     AliReconstructor* itsReconstructor = GetReconstructor(0);
3783     if (itsReconstructor) {
3784       fSPDTrackleter = itsReconstructor->CreateTrackleter(); // this is NULL unless required in RecoParam
3785     }
3786     if (fSPDTrackleter) {
3787       AliInfo("Trackleter for SPD has been created");
3788     }
3789   }
3790  return kTRUE;
3791 }
3792
3793 //_____________________________________________________________________________
3794 Bool_t AliReconstruction::InitAliEVE()
3795 {
3796   // This method should be called only in case 
3797   // AliReconstruction is run
3798   // within the alieve environment.
3799   // It will initialize AliEVE in a way
3800   // so that it can visualize event processed
3801   // by AliReconstruction.
3802   // The return flag shows whenever the
3803   // AliEVE initialization was successful or not.
3804
3805   TString macroStr(gSystem->Getenv("ALIEVE_ONLINE_MACRO"));
3806
3807   if (macroStr.IsNull())
3808     macroStr.Form("%s/EVE/macros/alieve_online.C",gSystem->ExpandPathName("$ALICE_ROOT"));
3809
3810   AliInfo(Form("Loading AliEVE macro: %s",macroStr.Data()));
3811
3812   if (gROOT->LoadMacro(macroStr.Data()) != 0) return kFALSE;
3813
3814   gROOT->ProcessLine("if (!AliEveEventManager::GetMaster()){new AliEveEventManager();AliEveEventManager::GetMaster()->AddNewEventCommand(\"alieve_online_on_new_event()\");gEve->AddEvent(AliEveEventManager::GetMaster());};");
3815   gROOT->ProcessLine("alieve_online_init()");
3816
3817   return kTRUE;
3818 }
3819   
3820 //_____________________________________________________________________________
3821 void AliReconstruction::RunAliEVE()
3822 {
3823   // Runs AliEVE visualisation of
3824   // the current event.
3825   // Should be executed only after
3826   // successful initialization of AliEVE.
3827
3828   AliInfo("Running AliEVE...");
3829   gROOT->ProcessLine(Form("AliEveEventManager::GetMaster()->SetEvent((AliRunLoader*)%p,(AliRawReader*)%p,(AliESDEvent*)%p,(AliESDfriend*)%p);",fRunLoader,fRawReader,fesd,fesdf));
3830   gSystem->Run();
3831 }
3832
3833 //_____________________________________________________________________________
3834 Bool_t AliReconstruction::SetRunQA(TString detAndAction) 
3835 {
3836         // Allows to run QA for a selected set of detectors
3837         // and a selected set of tasks among RAWS, DIGITSR, RECPOINTS and ESDS
3838         // all selected detectors run the same selected tasks
3839         
3840         if (!detAndAction.Contains(":")) {
3841                 AliError( Form("%s is a wrong syntax, use \"DetectorList:ActionList\" \n", detAndAction.Data()) ) ;
3842                 fRunQA = kFALSE ;
3843                 return kFALSE ;                 
3844         }
3845         Int_t colon = detAndAction.Index(":") ; 
3846         fQADetectors = detAndAction(0, colon) ; 
3847         fQATasks   = detAndAction(colon+1, detAndAction.Sizeof() ) ; 
3848         if (fQATasks.Contains("ALL") ) {
3849                 fQATasks = Form("%d %d %d %d", AliQAv1::kRAWS, AliQAv1::kDIGITSR, AliQAv1::kRECPOINTS, AliQAv1::kESDS) ; 
3850         } else {
3851                 fQATasks.ToUpper() ; 
3852                 TString tempo("") ; 
3853                 if ( fQATasks.Contains("RAW") ) 
3854                         tempo = Form("%d ", AliQAv1::kRAWS) ; 
3855                 if ( fQATasks.Contains("DIGIT") ) 
3856                         tempo += Form("%d ", AliQAv1::kDIGITSR) ; 
3857                 if ( fQATasks.Contains("RECPOINT") ) 
3858                         tempo += Form("%d ", AliQAv1::kRECPOINTS) ; 
3859                 if ( fQATasks.Contains("ESD") ) 
3860                         tempo += Form("%d ", AliQAv1::kESDS) ; 
3861                 fQATasks = tempo ; 
3862                 if (fQATasks.IsNull()) {
3863                         AliInfo("No QA requested\n")  ;
3864                         fRunQA = kFALSE ;
3865                         return kTRUE ; 
3866                 }
3867         }       
3868         TString tempo(fQATasks) ; 
3869         tempo.ReplaceAll(Form("%d", AliQAv1::kRAWS), AliQAv1::GetTaskName(AliQAv1::kRAWS))      ;
3870         tempo.ReplaceAll(Form("%d", AliQAv1::kDIGITSR), AliQAv1::GetTaskName(AliQAv1::kDIGITSR)) ;      
3871         tempo.ReplaceAll(Form("%d", AliQAv1::kRECPOINTS), AliQAv1::GetTaskName(AliQAv1::kRECPOINTS)) ;  
3872         tempo.ReplaceAll(Form("%d", AliQAv1::kESDS), AliQAv1::GetTaskName(AliQAv1::kESDS)) ;    
3873         AliInfo( Form("QA will be done on \"%s\" for \"%s\"\n", fQADetectors.Data(), tempo.Data()) ) ;  
3874         fRunQA = kTRUE ;
3875         return kTRUE; 
3876
3877
3878 //_____________________________________________________________________________
3879 Bool_t AliReconstruction::InitRecoParams() 
3880 {
3881   // The method accesses OCDB and retrieves all
3882   // the available reco-param objects from there.
3883
3884   Bool_t isOK = kTRUE;
3885
3886   if (fRecoParam.GetDetRecoParamArray(kNDetectors)) {
3887     AliInfo("Using custom GRP reconstruction parameters");
3888   }
3889   else {
3890     AliInfo("Loading GRP reconstruction parameter objects");
3891
3892     AliCDBPath path("GRP","Calib","RecoParam");
3893     AliCDBEntry *entry=AliCDBManager::Instance()->Get(path.GetPath());
3894     if(!entry){ 
3895       AliWarning("Couldn't find GRP RecoParam entry in OCDB");
3896       isOK = kFALSE;
3897     }
3898     else {
3899       TObject *recoParamObj = entry->GetObject();
3900       if (dynamic_cast<TObjArray*>(recoParamObj)) {
3901         // GRP has a normal TobjArray of AliDetectorRecoParam objects
3902         // Registering them in AliRecoParam
3903         fRecoParam.AddDetRecoParamArray(kNDetectors,dynamic_cast<TObjArray*>(recoParamObj));
3904       }
3905       else if (dynamic_cast<AliDetectorRecoParam*>(recoParamObj)) {
3906         // GRP has only onse set of reco parameters
3907         // Registering it in AliRecoParam
3908         AliInfo("Single set of GRP reconstruction parameters found");
3909         dynamic_cast<AliDetectorRecoParam*>(recoParamObj)->SetAsDefault();
3910         fRecoParam.AddDetRecoParam(kNDetectors,dynamic_cast<AliDetectorRecoParam*>(recoParamObj));
3911       }
3912       else {
3913         AliError("No valid GRP RecoParam object found in the OCDB");
3914         isOK = kFALSE;
3915       }
3916       entry->SetOwner(0);
3917     }
3918   }
3919
3920   TString detStr = fLoadCDB;
3921   for (Int_t iDet = 0; iDet < kNDetectors; iDet++) {
3922
3923     if (!IsSelected(fgkDetectorName[iDet], detStr)) continue;
3924
3925     if (fRecoParam.GetDetRecoParamArray(iDet)) {
3926       AliInfo(Form("Using custom reconstruction parameters for detector %s",fgkDetectorName[iDet]));
3927       continue;
3928     }
3929
3930     AliInfo(Form("Loading reconstruction parameter objects for detector %s",fgkDetectorName[iDet]));
3931   
3932     AliCDBPath path(fgkDetectorName[iDet],"Calib","RecoParam");
3933     AliCDBEntry *entry=AliCDBManager::Instance()->Get(path.GetPath());
3934     if(!entry){ 
3935       AliWarning(Form("Couldn't find RecoParam entry in OCDB for detector %s",fgkDetectorName[iDet]));
3936       isOK = kFALSE;
3937     }
3938     else {
3939       TObject *recoParamObj = entry->GetObject();
3940       if (dynamic_cast<TObjArray*>(recoParamObj)) {
3941         // The detector has a normal TobjArray of AliDetectorRecoParam objects
3942         // Registering them in AliRecoParam
3943         fRecoParam.AddDetRecoParamArray(iDet,dynamic_cast<TObjArray*>(recoParamObj));
3944       }
3945       else if (dynamic_cast<AliDetectorRecoParam*>(recoParamObj)) {
3946         // The detector has only onse set of reco parameters
3947         // Registering it in AliRecoParam
3948         AliInfo(Form("Single set of reconstruction parameters found for detector %s",fgkDetectorName[iDet]));
3949         dynamic_cast<AliDetectorRecoParam*>(recoParamObj)->SetAsDefault();
3950         fRecoParam.AddDetRecoParam(iDet,dynamic_cast<AliDetectorRecoParam*>(recoParamObj));
3951       }
3952       else {
3953         AliError(Form("No valid RecoParam object found in the OCDB for detector %s",fgkDetectorName[iDet]));
3954         isOK = kFALSE;
3955       }
3956       entry->SetOwner(0);
3957       //      FIX ME: We have to disable the unloading of reco-param CDB
3958       //      entries because QA framework is using them. Has to be fix in
3959       //      a way that the QA takes the objects already constructed in
3960       //      this method.
3961       //      AliCDBManager::Instance()->UnloadFromCache(path.GetPath());
3962     }
3963   }
3964
3965   if (AliDebugLevel() > 0) fRecoParam.Print();
3966
3967   return isOK;
3968 }
3969
3970 //_____________________________________________________________________________
3971 Bool_t AliReconstruction::GetEventInfo() 
3972 {
3973   // Fill the event info object
3974   // ...
3975   AliCodeTimerAuto("",0)
3976
3977   AliCentralTrigger *aCTP = NULL;
3978   if (fRawReader) {
3979     fEventInfo.SetEventType(fRawReader->GetType());
3980
3981     ULong64_t mask = fRawReader->GetClassMask();
3982     fEventInfo.SetTriggerMask(mask);
3983     UInt_t clmask = fRawReader->GetDetectorPattern()[0];
3984     fEventInfo.SetTriggerCluster(AliDAQ::ListOfTriggeredDetectors(clmask));
3985
3986     aCTP = new AliCentralTrigger();
3987     TString configstr("");
3988     if (!aCTP->LoadConfiguration(configstr)) { // Load CTP config from OCDB
3989       AliError("No trigger configuration found in OCDB! The trigger configuration information will not be used!");
3990       delete aCTP;
3991       return kFALSE;
3992     }
3993     aCTP->SetClassMask(mask);
3994     aCTP->SetClusterMask(clmask);
3995
3996     AliCentralTrigger* rlCTP = fRunLoader->GetTrigger();
3997     rlCTP->SetClassMask(mask);
3998     rlCTP->SetClusterMask(clmask);
3999   }
4000   else {
4001     fEventInfo.SetEventType(AliRawEventHeaderBase::kPhysicsEvent);
4002
4003     if (fRunLoader && (!fRunLoader->LoadTrigger())) {
4004       aCTP = fRunLoader->GetTrigger();
4005       fEventInfo.SetTriggerMask(aCTP->GetClassMask());
4006       // get inputs from actp - just get
4007       AliESDHeader* esdheader = fesd->GetHeader();
4008       esdheader->SetL0TriggerInputs(aCTP->GetL0TriggerInputs());
4009       esdheader->SetL1TriggerInputs(aCTP->GetL1TriggerInputs());
4010       esdheader->SetL2TriggerInputs(aCTP->GetL2TriggerInputs());
4011       fEventInfo.SetTriggerCluster(AliDAQ::ListOfTriggeredDetectors(aCTP->GetClusterMask()));
4012     }
4013     else {
4014       AliWarning("No trigger can be loaded! The trigger information will not be used!");
4015       return kFALSE;
4016     }
4017   }
4018
4019   AliTriggerConfiguration *config = aCTP->GetConfiguration();
4020   if (!config) {
4021     AliError("No trigger configuration has been found! The trigger configuration information will not be used!");
4022     if (fRawReader) delete aCTP;
4023     return kFALSE;
4024   }
4025
4026   UChar_t clustmask = 0;
4027   TString trclasses;
4028   ULong64_t trmask = fEventInfo.GetTriggerMask();
4029   const TObjArray& classesArray = config->GetClasses();
4030   Int_t nclasses = classesArray.GetEntriesFast();
4031   for( Int_t iclass=0; iclass < nclasses; iclass++ ) {
4032     AliTriggerClass* trclass = (AliTriggerClass*)classesArray.At(iclass);
4033     if (trclass && trclass->GetMask()>0) {
4034       Int_t trindex = TMath::Nint(TMath::Log2(trclass->GetMask()));
4035       fesd->SetTriggerClass(trclass->GetName(),trindex);
4036       if (fRawReader) fRawReader->LoadTriggerClass(trclass->GetName(),trindex);
4037       if (trmask & (1ull << trindex)) {
4038         trclasses += " ";
4039         trclasses += trclass->GetName();
4040         trclasses += " ";
4041         clustmask |= trclass->GetCluster()->GetClusterMask();
4042       }
4043     }
4044   }
4045   fEventInfo.SetTriggerClasses(trclasses);
4046   // Now put the declared trigger classes (not present in the run)
4047   // to 0/false in the event selection
4048   if (!fDeclTriggerClasses.IsNull()) {
4049     TObjArray *tokens = fDeclTriggerClasses.Tokenize(" ");
4050     Int_t ntokens = tokens->GetEntriesFast();
4051     for (Int_t itoken = 0; itoken < ntokens; ++itoken) {
4052       if (fRawReader) fRawReader->LoadTriggerClass((((TObjString*)tokens->At(itoken))->String()).Data(),-1);
4053     }
4054     delete tokens;
4055   }
4056
4057   // Write names of active trigger inputs in ESD Header
4058   const TObjArray& inputsArray = config->GetInputs(); 
4059   Int_t ninputs = inputsArray.GetEntriesFast();
4060   for( Int_t iinput=0; iinput < ninputs; iinput++ ) {
4061     AliTriggerInput* trginput = (AliTriggerInput*)inputsArray.At(iinput);
4062     if (trginput && trginput->GetMask()>0) {
4063       Int_t inputIndex = (Int_t)TMath::Nint(TMath::Log2(trginput->GetMask()));
4064       AliESDHeader* headeresd = fesd->GetHeader();
4065       Int_t trglevel = (Int_t)trginput->GetLevel();
4066       if (trglevel == 0) headeresd->SetActiveTriggerInputs(trginput->GetInputName(), inputIndex);
4067       if (trglevel == 1) headeresd->SetActiveTriggerInputs(trginput->GetInputName(), inputIndex+24);
4068       if (trglevel == 2) headeresd->SetActiveTriggerInputs(trginput->GetInputName(), inputIndex+48);
4069     }
4070   }
4071
4072   // Set the information in ESD
4073   fesd->SetTriggerMask(trmask);
4074   fesd->SetTriggerCluster(clustmask);
4075
4076   if (!aCTP->CheckTriggeredDetectors()) {
4077     if (fRawReader) delete aCTP;
4078     return kFALSE;
4079   }    
4080
4081   if (fRawReader) delete aCTP;
4082
4083   // We have to fill also the HLT decision here!!
4084   // ...
4085
4086   return kTRUE;
4087 }
4088
4089 const char *AliReconstruction::MatchDetectorList(const char *detectorList, UInt_t detectorMask)
4090 {
4091   // Match the detector list found in the rec.C or the default 'ALL'
4092   // to the list found in the GRP (stored there by the shuttle PP which
4093   // gets the information from ECS)
4094   static TString resultList;
4095   TString detList = detectorList;
4096
4097   resultList = "";
4098
4099   for(Int_t iDet = 0; iDet < (AliDAQ::kNDetectors-1); iDet++) {
4100     if ((detectorMask >> iDet) & 0x1) {
4101       TString det = AliDAQ::OfflineModuleName(iDet);
4102       if ((detList.CompareTo("ALL") == 0) ||
4103           ((detList.BeginsWith("ALL ") ||
4104             detList.EndsWith(" ALL") ||
4105             detList.Contains(" ALL ")) &&
4106            !(detList.BeginsWith("-"+det+" ") ||
4107              detList.EndsWith(" -"+det) ||
4108              detList.Contains(" -"+det+" "))) ||
4109           (detList.CompareTo(det) == 0) ||
4110           detList.BeginsWith(det+" ") ||
4111           detList.EndsWith(" "+det) ||
4112           detList.Contains( " "+det+" " )) {
4113         if (!resultList.EndsWith(det + " ")) {
4114           resultList += det;
4115           resultList += " ";
4116         }
4117       }        
4118     }
4119   }
4120
4121   // HLT
4122   if ((detectorMask >> AliDAQ::kHLTId) & 0x1) {
4123     TString hltDet = AliDAQ::OfflineModuleName(AliDAQ::kNDetectors-1);
4124     if ((detList.CompareTo("ALL") == 0) ||
4125         ((detList.BeginsWith("ALL ") ||
4126           detList.EndsWith(" ALL") ||
4127           detList.Contains(" ALL ")) &&
4128          !(detList.BeginsWith("-"+hltDet+" ") ||
4129            detList.EndsWith(" -"+hltDet) ||
4130            detList.Contains(" -"+hltDet+" "))) ||
4131         (detList.CompareTo(hltDet) == 0) ||
4132         detList.BeginsWith(hltDet+" ") ||
4133         detList.EndsWith(" "+hltDet) ||
4134         detList.Contains( " "+hltDet+" " )) {
4135       resultList += hltDet;
4136     }
4137   }
4138
4139   return resultList.Data();
4140
4141 }
4142
4143 //______________________________________________________________________________
4144 void AliReconstruction::Abort(const char *method, EAbort what)
4145 {
4146   // Abort processing. If what = kAbortProcess, the Process() loop will be
4147   // aborted. If what = kAbortFile, the current file in a chain will be
4148   // aborted and the processing will continue with the next file, if there
4149   // is no next file then Process() will be aborted. Abort() can also  be
4150   // called from Begin(), SlaveBegin(), Init() and Notify(). After abort
4151   // the SlaveTerminate() and Terminate() are always called. The abort flag
4152   // can be checked in these methods using GetAbort().
4153   //
4154   // The method is overwritten in AliReconstruction for better handling of
4155   // reco specific errors 
4156
4157   if (!fStopOnError) return;
4158
4159   CleanUp();
4160
4161   TString whyMess = method;
4162   whyMess += " failed! Aborting...";
4163
4164   AliError(whyMess.Data());
4165
4166   fAbort = what;
4167   TString mess = "Abort";
4168   if (fAbort == kAbortProcess)
4169     mess = "AbortProcess";
4170   else if (fAbort == kAbortFile)
4171     mess = "AbortFile";
4172
4173   Info(mess.Data(), "%s", whyMess.Data());
4174 }
4175
4176 //______________________________________________________________________________
4177 Bool_t AliReconstruction::ProcessEvent(void* event)
4178 {
4179   // Method that is used in case the event loop
4180   // is steered from outside, for example by AMORE
4181   // 'event' is a pointer to the DATE event in the memory
4182
4183   if (fRawReader) delete fRawReader;
4184   fRawReader = new AliRawReaderDate(event);
4185   fStatus = ProcessEvent(fRunLoader->GetNumberOfEvents());  
4186   delete fRawReader;
4187   fRawReader = NULL;
4188
4189   return fStatus;
4190 }
4191
4192 //______________________________________________________________________________
4193 Bool_t AliReconstruction::ParseOutput()
4194 {
4195   // The method parses the output file
4196   // location string in order to steer
4197   // properly the selector
4198
4199   TPMERegexp re1("(\\w+\\.zip#\\w+\\.root):([,*\\w+\\.root,*]+)@dataset://(\\w++)");
4200   TPMERegexp re2("(\\w+\\.root)?@?dataset://(\\w++)");
4201
4202   if (re1.Match(fESDOutput) == 4) {
4203     // root archive with output files stored and regustered
4204     // in proof dataset
4205     gProof->AddInput(new TNamed("PROOF_OUTPUTFILE",re1[1].Data()));
4206     gProof->AddInput(new TNamed("PROOF_OUTPUTFILE_LOCATION",re1[3].Data()));
4207     gProof->AddInput(new TNamed("PROOF_OUTPUTFILE_DATASET",""));
4208     gProof->AddInput(new TNamed("PROOF_OUTPUTFILE_ARCHIVE",re1[2].Data()));
4209     AliInfo(Form("%s files will be stored within %s in dataset %s",
4210                  re1[2].Data(),
4211                  re1[1].Data(),
4212                  re1[3].Data()));
4213   }
4214   else if (re2.Match(fESDOutput) == 3) {
4215     // output file stored and registered
4216     // in proof dataset
4217     gProof->AddInput(new TNamed("PROOF_OUTPUTFILE",(re2[1].IsNull()) ? "AliESDs.root" : re2[1].Data()));
4218     gProof->AddInput(new TNamed("PROOF_OUTPUTFILE_LOCATION",re2[2].Data()));
4219     gProof->AddInput(new TNamed("PROOF_OUTPUTFILE_DATASET",""));
4220     AliInfo(Form("%s will be stored in dataset %s",
4221                  (re2[1].IsNull()) ? "AliESDs.root" : re2[1].Data(),
4222                  re2[2].Data()));
4223   }
4224   else {
4225     if (fESDOutput.IsNull()) {
4226       // Output location not given.
4227       // Assuming xrootd has been already started and
4228       // the output file has to be sent back
4229       // to the client machine
4230       TString esdUrl(Form("root://%s/%s/",
4231                           TUrl(gSystem->HostName()).GetHostFQDN(),
4232                           gSystem->pwd()));
4233       gProof->AddInput(new TNamed("PROOF_OUTPUTFILE","AliESDs.root"));
4234       gProof->AddInput(new TNamed("PROOF_OUTPUTFILE_LOCATION",esdUrl.Data()));
4235       AliInfo(Form("AliESDs.root will be stored in %s",
4236                    esdUrl.Data()));
4237     }
4238     else {
4239       // User specified an output location.
4240       // Ones has just to parse it here
4241       TUrl outputUrl(fESDOutput.Data());
4242       TString outputFile(gSystem->BaseName(outputUrl.GetFile()));
4243       gProof->AddInput(new TNamed("PROOF_OUTPUTFILE",outputFile.IsNull() ? "AliESDs.root" : outputFile.Data()));
4244       TString outputLocation(outputUrl.GetUrl());
4245       outputLocation.ReplaceAll(outputFile.Data(),"");
4246       gProof->AddInput(new TNamed("PROOF_OUTPUTFILE_LOCATION",outputLocation.Data()));
4247       AliInfo(Form("%s will be stored in %s",
4248                    outputFile.IsNull() ? "AliESDs.root" : outputFile.Data(),
4249                    outputLocation.Data()));
4250     }
4251   }
4252
4253   return kTRUE;
4254 }
4255
4256 //______________________________________________________________________________
4257 Bool_t AliReconstruction::IsHighPt() const {
4258   // Selection of events containing "high" pT tracks
4259   // If at least one track is found within 1.5 and 100 GeV (pT)
4260   // that was reconstructed by both ITS and TPC, the event is accepted
4261
4262   // Track cuts
4263   const Double_t pTmin = 1.5;
4264   const Double_t pTmax = 100;
4265   ULong_t mask = 0;
4266   mask |= (AliESDtrack::kITSrefit);
4267   mask |= (AliESDtrack::kTPCrefit);
4268   const Double_t pTminCosmic = 5.;
4269   const Double_t pTmaxCosmic = 100;
4270   ULong_t maskCosmic = 0;
4271   Int_t cosmicCount=0;
4272   maskCosmic |= (AliESDtrack::kTPCrefit);
4273
4274   Bool_t isOK = kFALSE;
4275
4276   if (fesd && fesd->GetEventType()==AliRawEventHeaderBase::kPhysicsEvent) {
4277     // Check if this ia a physics event (code 7)
4278     Int_t ntrk = fesd->GetNumberOfTracks();
4279     for (Int_t itrk=0; itrk<ntrk; ++itrk) {
4280           
4281       AliESDtrack * trk = fesd->GetTrack(itrk);
4282       if (trk 
4283           && trk->Pt() > pTmin 
4284           && trk->Pt() < pTmax
4285           && (trk->GetStatus() & mask) == mask ) {
4286         
4287         isOK = kTRUE;
4288         break;
4289       }
4290       if (trk 
4291           && trk->GetInnerParam()
4292           && trk->GetInnerParam()->Pt() > pTminCosmic 
4293           && trk->GetInnerParam()->Pt() < pTmaxCosmic
4294           && (trk->GetStatus() & maskCosmic) == maskCosmic ) {
4295         
4296         cosmicCount++;
4297         break;
4298       }
4299     }
4300     if (cosmicCount>1) isOK=kTRUE;
4301   }
4302   return isOK;
4303 }
4304
4305 //______________________________________________________________________________
4306 Bool_t AliReconstruction::IsCosmicOrCalibSpecie() const {
4307   // Select cosmic or calibration events
4308
4309   Bool_t isOK = kFALSE;
4310
4311   if (fesd && fesd->GetEventType()==AliRawEventHeaderBase::kPhysicsEvent) {
4312       // Check if this ia a physics event (code 7)
4313       
4314       UInt_t specie = fesd->GetEventSpecie();
4315       if (specie==AliRecoParam::kCosmic || specie==AliRecoParam::kCalib) {
4316         isOK = kTRUE;
4317       }
4318   }
4319   return isOK;
4320 }
4321
4322 //______________________________________________________________________________
4323 void AliReconstruction::WriteESDfriend() {
4324   // Fill the ESD friend in the tree. The required fraction of ESD friends is stored
4325   // in fFractionFriends. We select events where we store the ESD friends according
4326   // to the following algorithm:
4327   // 1. Store all Cosmic or Calibration events within the required fraction
4328   // 2. Sample "high Pt" events within the remaining fraction after step 1.
4329   // 3. Sample randomly events if we still have remaining slot
4330   //
4331   static int iEvent = 0;
4332   //
4333   fNall++;
4334   Bool_t isSelected = kFALSE;
4335   //
4336   // Store all friends for B field OFF 
4337   if (TMath::Abs(AliTrackerBase::GetBz())<0.5) isSelected=kTRUE;
4338
4339   if (IsCosmicOrCalibSpecie()) { // Selection of calib or cosmic events
4340     fNspecie++;
4341
4342     isSelected = kTRUE;
4343     fSspecie++;
4344   }
4345   
4346   Double_t remainingFraction = fFractionFriends;
4347   remainingFraction -= ((Double_t)(fSspecie)/(Double_t)(fNall));
4348   
4349   if (IsHighPt())  { // Selection of "high Pt" events
4350     fNhighPt++;
4351     Double_t curentHighPtFraction = ((Double_t)(fNhighPt+1))/((Double_t)(fNall+1));
4352     // "Bayesian" estimate supposing that without events all the events are of the required type
4353     
4354     if (!isSelected) {
4355       Double_t rnd = gRandom->Rndm()*curentHighPtFraction;
4356       if (rnd<remainingFraction) {
4357         isSelected = kTRUE;
4358         fShighPt++;
4359       }
4360     }
4361   }
4362   remainingFraction -= ((Double_t)(fShighPt)/(Double_t)(fNall));
4363   
4364   // Random selection to fill the remaining fraction (if any)
4365   if (!isSelected) {
4366     Double_t rnd = gRandom->Rndm();
4367     if (rnd<remainingFraction) {        
4368       isSelected = kTRUE;
4369     }
4370   }
4371   //
4372   if (!isSelected) {
4373     fesdf->Clear();
4374     fesdf->SetSkipBit(kTRUE);
4375     fesd->DetachFriends();   // remove friend tracks
4376   }
4377   else fesd->GetESDfriend(fesdf); // move friend tracks to AliESDfriens object
4378   //
4379   AliSysInfo::AddStamp(Form("WriteFriend%d_%d",isSelected,iEvent), 0,0,iEvent); 
4380   //
4381   ftreeF->Fill();
4382   AliSysInfo::AddStamp(Form("WriteFrFill%d_%d",isSelected,iEvent), 0,0,iEvent); 
4383   //
4384   iEvent++;
4385 }
4386
4387 //___________________________________________________________________
4388 void AliReconstruction::InitPools()
4389 {
4390   // create general purpose pools
4391   if (!fPools) {
4392     fPools = new AliPoolsSet();
4393     fPools->InitPools(); // this will initialize only obligatory pools
4394     //
4395     if (fWriteAlignmentData) {
4396       AliClonesPool* pta = new AliClonesPool("AliTrackPointArray",5000);
4397       fPools->SetPool(pta,AliPoolsSet::kPoolTrPoints);
4398     }
4399     AliTrackPointArray::SetPool(fPools->GetPoolN());
4400     AliESDtrack::SetPools(fPools);
4401     AliESDfriendTrack::SetPools(fPools);    
4402   }
4403   //
4404   fPools->ResetPools();
4405   //
4406 }
4407
4408 //_________________________________________________________________
4409 void AliReconstruction::DeleteDigits(const TString& detectors)
4410 {
4411   // delete requested digit files produced at current event
4412   static int iEvent = 0;
4413   if (detectors.IsNull()) return;
4414   TString detStr = detectors;
4415   AliInfo(Form("Deleting Digits: %s",detectors.Data()));
4416
4417   for (Int_t iDet = 0; iDet < kNDetectors; iDet++) {
4418     gSystem->Exec(Form("if [ -e %s.Digits.root ]; then\nrm %s.Digits.root\nfi",
4419                        fgkDetectorName[iDet],fgkDetectorName[iDet]));
4420   }
4421   AliSysInfo::AddStamp(Form("DelDigits_%d",iEvent), 0,0,iEvent);
4422   iEvent++;
4423 }
4424
4425 //_________________________________________________________________
4426 void AliReconstruction::DeleteRecPoints(const TString& detectors)
4427 {
4428   // delete requested recpoint files produced at current event
4429   static int iEvent = 0;
4430   if (detectors.IsNull()) return;
4431   TString detStr = detectors;
4432   AliInfo(Form("Deleting Recpoints: %s",detectors.Data()));
4433   //
4434   for (Int_t iDet = 0; iDet < kNDetectors; iDet++) {
4435     gSystem->Exec(Form("if [ -e %s.RecPoints.root ]; then\nrm %s.RecPoints.root\nfi",
4436                        fgkDetectorName[iDet],fgkDetectorName[iDet]));
4437   }
4438   AliSysInfo::AddStamp(Form("DelRecPoints_%d",iEvent), 0,0,iEvent);
4439   iEvent++;
4440 }
4441
4442 //_________________________________________________________________
4443 void AliReconstruction::SetStopOnResourcesExcess(Int_t vRSS,Int_t vVMEM)
4444 {
4445   // require checking the resources left and stopping on excess
4446   // if 0  : no check is done
4447   // if >0 : stop reconstruction if exceeds this value
4448   // if <0 : use as margin to system limits
4449   //
4450   const int kKB2MB = 1024;
4451   const int kInfMem = 9999999;
4452   //
4453   struct rlimit r;
4454   int pgSize = getpagesize();
4455   //
4456   if (vRSS>0) {
4457     fMaxRSS = vRSS;
4458     AliInfo(Form("Setting max. RSS usage to user value %d MB",fMaxRSS));
4459   }
4460   else if (vRSS<0) {
4461     getrlimit(RLIMIT_RSS,&r);
4462     fMaxRSS = r.rlim_max==RLIM_INFINITY ? kInfMem : int(r.rlim_max*pgSize/kKB2MB/kKB2MB) + vRSS;
4463     AliInfo(Form("Setting max. RSS usage to system hard limit %d%s MB (%d margin)",fMaxRSS,r.rlim_max==RLIM_INFINITY ? "(inf)":"",-vRSS));
4464   }
4465   else {AliInfo("No check on RSS memory usage will be applied");}
4466   //
4467   if (vVMEM>0) {
4468     fMaxVMEM = vVMEM;
4469     AliInfo(Form("Setting max. VMEM usage to user value %d MB",fMaxVMEM));
4470   }
4471   else if (vVMEM<0) {
4472     getrlimit(RLIMIT_AS,&r);
4473     fMaxVMEM = r.rlim_max==RLIM_INFINITY ? kInfMem : int(r.rlim_max*pgSize/kKB2MB/kKB2MB) + vVMEM;
4474     AliInfo(Form("Setting max. VMEM usage to system hard limit %d%s MB (%d margin)",fMaxVMEM,r.rlim_max==RLIM_INFINITY ? "(inf)":"",-vVMEM));
4475   }
4476   else {AliInfo("No check on RSS memory usage will be applied");}
4477   //  
4478 }
4479
4480 //_________________________________________________________________
4481 Bool_t AliReconstruction::HasEnoughResources(int ev)
4482 {
4483   // check if process consumed more than allowed resources
4484   const int kKB2MB = 1024;
4485   Bool_t res = kTRUE;
4486   if (!fMaxRSS && !fMaxVMEM) return res;
4487   //
4488   ProcInfo_t procInfo;
4489   gSystem->GetProcInfo(&procInfo);
4490   if (procInfo.fMemResident/kKB2MB > fMaxRSS)  res = kFALSE;
4491   if (procInfo.fMemVirtual/kKB2MB  > fMaxVMEM) res = kFALSE;  
4492   //
4493   if (!res) {
4494     AliInfo(Form("Job exceeded allowed limits: RSS:%d (%d) VMEM:%d (%d), will stop",
4495                  int(procInfo.fMemResident/kKB2MB),fMaxRSS,
4496                  int(procInfo.fMemVirtual/kKB2MB) ,fMaxVMEM));
4497     //
4498     gSystem->Exec(Form("if [ -e %s ]; then\nrm %s\nfi\necho %d > %s",fgkStopEvFName,fgkStopEvFName,ev,fgkStopEvFName));
4499     fStopped = kTRUE;
4500   }
4501   return res;
4502 }