Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
itk_seeding.py
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file itk_seeding.py
1 #!/usr/bin/env python3
2 import os
3 import argparse
4 import tempfile
5 
6 import pathlib, acts
7 
8 from acts.examples import (
9  CsvSpacePointReader,
10  TrackParamsEstimationAlgorithm,
11  SeedingPerformanceWriter,
12 )
13 from acts.examples.reconstruction import (
14  addSeeding,
15  addStandardSeeding,
16  SeedingAlgorithm,
17 )
18 
19 from acts.examples.itk import itkSeedingAlgConfig, InputSpacePointsType
20 
21 u = acts.UnitConstants
22 rnd = acts.examples.RandomNumbers(seed=42)
23 
24 
25 def runITkSeedingFromCsv(detector, trackingGeometry, field, outputDir):
26 
27  # create temporary file with pixel SPs and run the seeding
28  with tempfile.TemporaryDirectory() as tmpdirname:
29  temp = open(tmpdirname + "/event000000000-spacepoints_pixel.csv", "w+t")
30  print(
31  "created temporary file: "
32  + tmpdirname
33  + "/event000000000-spacepoints_pixel.csv"
34  )
35  temp.write(
36  "measurement_id,sp_type,module_idhash,sp_x,sp_y,sp_z,sp_radius,sp_covr,sp_covz,sp_topHalfStripLength,sp_bottomHalfStripLength,sp_topStripDirection[0],sp_topStripDirection[1],sp_topStripDirection[2],sp_bottomStripDirection[0],sp_bottomStripDirection[1],sp_bottomStripDirection[2],sp_stripCenterDistance[0],sp_stripCenterDistance[1],sp_stripCenterDistance[2],sp_topStripCenterPosition[0],sp_topStripCenterPosition[1],sp_topStripCenterPosition[2]\n 1,0,3139,32.67557144165039,-5.311902523040771,-47.65000152587891,33.10452270507812,0.05999999865889549,0.02999880164861679,0,0,0,0,0,0,0,0,0,0,0,0,0,0\n 2,0,3422,95.14442443847656,-15.46361255645752,-52.125,96.39286804199219,0.05999999865889549,0.01687432639300823,0,0,0,0,0,0,0,0,0,0,0,0,0,0\n 3,0,3650,102.8257064819336,-16.71612739562988,-52.67499923706055,104.1755981445312,0.05999999865889549,0.001875000074505806,0,0,0,0,0,0,0,0,0,0,0,0,0,0\n 4,0,4223,159.4266204833984,-25.91166687011719,-56.75,161.5186157226562,0.05999999865889549,0.02999880164861679,0,0,0,0,0,0,0,0,0,0,0,0,0,0\n 5,0,5015,224.07958984375,-36.37123107910156,-61.40000152587891,227.0121765136719,0.05999999865889549,0.007499700412154198,0,0,0,0,0,0,0,0,0,0,0,0,0,0\n 6,0,6023,284.1485595703125,-46.0638542175293,-65.72499847412109,287.8580932617188,0.05999999865889549,0.001875000074505806,0,0,0,0,0,0,0,0,0,0,0,0,0,0"
37  )
38  temp.read()
39 
40  s = acts.examples.Sequencer(events=1, numThreads=-1, logLevel=acts.logging.INFO)
41 
42  # Read input space points from input csv files
43  evReader = CsvSpacePointReader(
44  level=acts.logging.INFO,
45  inputStem="spacepoints",
46  inputCollection="pixel",
47  inputDir=os.path.dirname(temp.name),
48  outputSpacePoints="PixelSpacePoint",
49  extendCollection=False,
50  )
51 
52  # add csv reader
53  s.addReader(evReader)
54  spacePoints = evReader.config.outputSpacePoints
55 
56  # run seeding
57  inputSeeds = addStandardSeeding(
58  s,
59  spacePoints,
61  InputSpacePointsType.PixelSpacePoints
62  ),
63  )
64 
65  prototracks = "seed-prototracks"
66  s.addAlgorithm(
67  acts.examples.SeedsToPrototracks(
68  level=acts.logging.INFO,
69  inputSeeds=inputSeeds,
70  outputProtoTracks=prototracks,
71  )
72  )
73 
74  # estimate seeding performance
75  parEstimateAlg = TrackParamsEstimationAlgorithm(
76  level=acts.logging.INFO,
77  inputSeeds=inputSeeds,
78  outputTrackParameters="estimatedparameters",
79  trackingGeometry=trackingGeometry,
80  magneticField=field,
81  )
82  s.addAlgorithm(parEstimateAlg)
83 
84  s.run()
85 
86  # create temporary file with strips SPs and run the seeding
87  with tempfile.TemporaryDirectory() as tmpdirname:
88  temp = open(tmpdirname + "/event000000000-spacepoints_strip.csv", "w+t")
89  print(
90  "created temporary file: "
91  + tmpdirname
92  + "/event000000000-spacepoints_strip.csv"
93  )
94  temp.write(
95  "measurement_id,sp_type,module_idhash,sp_x,sp_y,sp_z,sp_radius,sp_covr,sp_covz,sp_topHalfStripLength,sp_bottomHalfStripLength,sp_topStripDirection[0],sp_topStripDirection[1],sp_topStripDirection[2],sp_bottomStripDirection[0],sp_bottomStripDirection[1],sp_bottomStripDirection[2],sp_stripCenterDistance[0],sp_stripCenterDistance[1],sp_stripCenterDistance[2],sp_topStripCenterPosition[0],sp_topStripCenterPosition[1],sp_topStripCenterPosition[2]\n 0,1,0,386.77178955078125,-62.579288482666015625,-72.66841888427734375,391.801727294921875,0.100000001490116119384765625,5.11999988555908203125,12.08999919891357421875,12.08999919891357421875,-0.00864744372665882110595703125,-0.02451671846210956573486328125,0.999662101268768310546875,0.00864744372665882110595703125,0.02451671846210956573486328125,0.999662101268768310546875,-6.43960094451904296875,1.04346692562103271484375,23.157070159912109375,386.6771240234375,-62.847682952880859375,-61.724697113037109375\n 1,1,0,543.9947509765625,-87.7279205322265625,-82.09113311767578125,551.02313232421875,0.100000001490116119384765625,5.11999988555908203125,12.08999919891357421875,12.08999919891357421875,-0.0073835677467286586761474609375,-0.024926505982875823974609375,0.999662101268768310546875,0.0073835677467286586761474609375,0.024926505982875823974609375,0.999662101268768310546875,-6.34883975982666015625,1.17108881473541259765625,-2.3926274776458740234375,544.0279541015625,-87.6157989501953125,-86.58742523193359375\n 2,1,0,544.00189208984375,-87.70365142822265625,-83.064239501953125,551.02630615234375,0.100000001490116119384765625,5.11999988555908203125,12.08999919891357421875,12.08999919891357421875,-0.0073835677467286586761474609375,-0.024926505982875823974609375,0.999662101268768310546875,0.011192028410732746124267578125,0.02346457540988922119140625,0.999662101268768310546875,-24.83704376220703125,4.08867168426513671875,-0.0274789035320281982421875,544.0279541015625,-87.6157989501953125,-86.58742523193359375\n 3,1,0,562.2547607421875,-90.650543212890625,-83.99970245361328125,569.5155029296875,0.100000001490116119384765625,5.11999988555908203125,12.08999919891357421875,12.08999919891357421875,-0.011192028410732746124267578125,-0.02346457540988922119140625,0.999662101268768310546875,0.0073835677467286586761474609375,0.024926505982875823974609375,0.999662101268768310546875,11.8808460235595703125,-1.85768926143646240234375,-0.0588833652436733245849609375,562.25762939453125,-90.6445770263671875,-84.2536773681640625\n 4,1,0,562.26605224609375,-90.62686920166015625,-85.00818634033203125,569.52288818359375,0.100000001490116119384765625,5.11999988555908203125,12.08999919891357421875,12.08999919891357421875,-0.011192028410732746124267578125,-0.02346457540988922119140625,0.999662101268768310546875,0.011192028410732746124267578125,0.02346457540988922119140625,0.999662101268768310546875,-6.60735797882080078125,1.05989348888397216796875,2.3062651157379150390625,562.25762939453125,-90.6445770263671875,-84.2536773681640625\n 5,1,0,750.5875244140625,-120.5648040771484375,-98.9385986328125,760.2088623046875,0.100000001490116119384765625,5.11999988555908203125,24.1799983978271484375,24.1799983978271484375,-0.009588238783180713653564453125,-0.02416430227458477020263671875,0.999662101268768310546875,0.009588238783180713653564453125,0.02416430227458477020263671875,0.999662101268768310546875,-6.041371822357177734375,2.1813886165618896484375,0.3670396506786346435546875,750.81573486328125,-119.98968505859375,-122.7309112548828125\n 6,1,0,979.34979248046875,-156.5580291748046875,-114.63397979736328125,991.78448486328125,0.100000001490116119384765625,5.11999988555908203125,24.1799983978271484375,24.1799983978271484375,-0.00824898667633533477783203125,-0.02465364150702953338623046875,0.999662101268768310546875,0.00824898667633533477783203125,0.02465364150702953338623046875,0.999662101268768310546875,-6.2955722808837890625,1.417438507080078125,-1.45441913604736328125,979.4241943359375,-156.335723876953125,-123.64752960205078125"
96  )
97  temp.read()
98 
99  s = acts.examples.Sequencer(events=1, numThreads=-1, logLevel=acts.logging.INFO)
100 
101  # Read input space points from input csv files
102  evReader = CsvSpacePointReader(
103  level=acts.logging.INFO,
104  inputStem="spacepoints",
105  inputCollection="strip",
106  inputDir=os.path.dirname(temp.name),
107  outputSpacePoints="StripSpacePoint",
108  extendCollection=True,
109  )
110 
111  # add csv reader
112  s.addReader(evReader)
113  spacePoints = evReader.config.outputSpacePoints
114 
115  # run seeding
116  inputSeeds = addStandardSeeding(
117  s,
118  spacePoints,
120  InputSpacePointsType.StripSpacePoints
121  ),
122  )
123 
124  prototracks = "seed-prototracks"
125  s.addAlgorithm(
126  acts.examples.SeedsToPrototracks(
127  level=acts.logging.INFO,
128  inputSeeds=inputSeeds,
129  outputProtoTracks=prototracks,
130  )
131  )
132 
133  # estimate seeding performance
134  parEstimateAlg = TrackParamsEstimationAlgorithm(
135  level=acts.logging.INFO,
136  inputSeeds=inputSeeds,
137  outputTrackParameters="estimatedparameters",
138  trackingGeometry=trackingGeometry,
139  magneticField=field,
140  )
141  s.addAlgorithm(parEstimateAlg)
142 
143  s.run()
144 
145 
146 if "__main__" == __name__:
147  p = argparse.ArgumentParser(
148  description="Example script to run ITk seed finding based on CSV spacepoints",
149  )
150 
151  geo_dir = pathlib.Path("acts-itk")
152  outputDir = pathlib.Path.cwd() / "itk_output"
153 
154  detector, trackingGeometry, decorators = acts.examples.itk.buildITkGeometry(geo_dir)
155  field = acts.examples.MagneticFieldMapXyz(
156  str(geo_dir / "bfield/ATLAS-BField-xyz.root")
157  )
158 
159  runITkSeedingFromCsv(detector, trackingGeometry, field, outputDir)