Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
vertex_fitting.py
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file vertex_fitting.py
1 #!/usr/bin/env python3
2 from pathlib import Path
3 from typing import Optional
4 
5 import acts
6 from acts.examples import (
7  Sequencer,
8  ParticleSelector,
9  ParticleSmearing,
10  TrackParameterSelector,
11 )
12 from acts.examples.simulation import addPythia8
13 from acts.examples.reconstruction import (
14  addVertexFitting,
15  VertexFinder,
16 )
17 
18 u = acts.UnitConstants
19 
20 
22  field,
23  outputDir: Path,
24  outputRoot: bool = True,
25  inputParticlePath: Optional[Path] = None,
26  inputTrackSummary: Path = None,
27  vertexFinder: VertexFinder = VertexFinder.Truth,
28  s=None,
29 ):
30  s = s or Sequencer(events=100, numThreads=-1)
31 
32  logger = acts.logging.getLogger("VertexFittingExample")
33 
34  rnd = acts.examples.RandomNumbers(seed=42)
35 
36  inputParticles = "particles_input"
37  if inputParticlePath is None:
38  logger.info("Generating particles using Pythia8")
39  addPythia8(s, rnd)
40  else:
41  logger.info("Reading particles from %s", inputParticlePath.resolve())
42  assert inputParticlePath.exists()
43  s.addReader(
44  acts.examples.RootParticleReader(
45  level=acts.logging.INFO,
46  filePath=str(inputParticlePath.resolve()),
47  particleCollection=inputParticles,
48  orderedEvents=False,
49  )
50  )
51 
52  selectedParticles = "particles_selected"
53  ptclSelector = ParticleSelector(
54  level=acts.logging.INFO,
55  inputParticles=inputParticles,
56  outputParticles=selectedParticles,
57  removeNeutral=True,
58  absEtaMax=2.5,
59  rhoMax=4.0 * u.mm,
60  ptMin=500 * u.MeV,
61  )
62  s.addAlgorithm(ptclSelector)
63 
64  trackParameters = "fittedTrackParameters"
65 
66  if inputTrackSummary is None or inputParticlePath is None:
67  logger.info("Using smeared particles")
68 
69  ptclSmearing = ParticleSmearing(
70  level=acts.logging.INFO,
71  inputParticles=selectedParticles,
72  outputTrackParameters=trackParameters,
73  randomNumbers=rnd,
74  )
75  s.addAlgorithm(ptclSmearing)
76  associatedParticles = selectedParticles
77  else:
78  logger.info("Reading track summary from %s", inputTrackSummary.resolve())
79  assert inputTrackSummary.exists()
80  associatedParticles = "associatedTruthParticles"
81  trackSummaryReader = acts.examples.RootTrajectorySummaryReader(
82  level=acts.logging.VERBOSE,
83  outputTracks=trackParameters,
84  outputParticles=associatedParticles,
85  filePath=str(inputTrackSummary.resolve()),
86  orderedEvents=False,
87  )
88  s.addReader(trackSummaryReader)
89 
90  trackParamSelector = TrackParameterSelector(
91  level=acts.logging.INFO,
92  inputTrackParameters=trackSummaryReader.config.outputTracks,
93  outputTrackParameters="selectedTrackParameters",
94  absEtaMax=2.5,
95  loc0Max=4.0 * u.mm, # rho max
96  ptMin=500 * u.MeV,
97  )
98  s.addAlgorithm(trackParamSelector)
99  trackParameters = trackParamSelector.config.outputTrackParameters
100 
101  logger.info("Using vertex finder: %s", vertexFinder.name)
102 
104  s,
105  field,
106  trackParameters=trackParameters,
107  associatedParticles=associatedParticles,
108  trajectories=None,
109  vertexFinder=vertexFinder,
110  outputDirRoot=outputDir if outputRoot else None,
111  )
112 
113  return s
114 
115 
116 if "__main__" == __name__:
117  detector, trackingGeometry, decorators = acts.examples.GenericDetector.create()
118 
119  field = acts.ConstantBField(acts.Vector3(0, 0, 2 * u.T))
120 
121  inputParticlePath = Path("particles.root")
122  if not inputParticlePath.exists():
123  inputParticlePath = None
124 
125  inputTrackSummary = None
126  for p in ("tracksummary_fitter.root", "tracksummary_ckf.root"):
127  p = Path(p)
128  if p.exists():
129  inputTrackSummary = p
130  break
131 
133  field,
134  vertexFinder=VertexFinder.Truth,
135  inputParticlePath=inputParticlePath,
136  inputTrackSummary=inputTrackSummary,
137  outputDir=Path.cwd(),
138  ).run()