Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
material_mapping_itk.py
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file material_mapping_itk.py
1 #!/usr/bin/env python3
2 import os
3 import argparse
4 from pathlib import Path
5 
6 
7 from acts.examples import (
8  Sequencer,
9  WhiteBoard,
10  AlgorithmContext,
11  ProcessCode,
12  RootMaterialTrackReader,
13  RootMaterialTrackWriter,
14  MaterialMapping,
15  JsonMaterialWriter,
16  JsonFormat,
17 )
18 
19 import acts
20 from acts import (
21  Vector4,
22  UnitConstants as u,
23  SurfaceMaterialMapper,
24  VolumeMaterialMapper,
25  Navigator,
26  Propagator,
27  StraightLineStepper,
28  MaterialMapJsonConverter,
29 )
30 from common import getOpenDataDetectorDirectory
31 from acts.examples.odd import getOpenDataDetector
32 
33 
35  trackingGeometry,
36  decorators,
37  outputDir,
38  inputDir,
39  materialStepsFile,
40  mapName="material-map",
41  mapSurface=True,
42  readCachedSurfaceInformation=False,
43  dumpMaterialTracks=False,
44  s=None,
45 ):
46  s = s or Sequencer(numThreads=1)
47 
48  for decorator in decorators:
49  s.addContextDecorator(decorator)
50 
51  wb = WhiteBoard(acts.logging.INFO)
52 
53  context = AlgorithmContext(0, 0, wb)
54 
55  for decorator in decorators:
56  assert decorator.decorate(context) == ProcessCode.SUCCESS
57 
58  # Read material step information from a ROOT TTRee
59  s.addReader(
60  RootMaterialTrackReader(
61  level=acts.logging.INFO,
62  collection="material-tracks",
63  fileList=[
64  os.path.join(
65  inputDir,
66  materialStepsFile,
67  )
68  ],
69  readCachedSurfaceInformation=readCachedSurfaceInformation,
70  )
71  )
72 
73  stepper = StraightLineStepper()
74 
75  mmAlgCfg = MaterialMapping.Config(context.geoContext, context.magFieldContext)
76  mmAlgCfg.trackingGeometry = trackingGeometry
77  mmAlgCfg.collection = "material-tracks"
78 
79  if mapSurface:
80  navigator = Navigator(
81  trackingGeometry=trackingGeometry,
82  resolveSensitive=False,
83  resolveMaterial=True,
84  resolvePassive=True,
85  )
86  propagator = Propagator(stepper, navigator)
87  mapper = SurfaceMaterialMapper(level=acts.logging.INFO, propagator=propagator)
88  mmAlgCfg.materialSurfaceMapper = mapper
89 
90  jmConverterCfg = MaterialMapJsonConverter.Config(
91  processSensitives=False,
92  processNonMaterial=False,
93  processApproaches=True,
94  processRepresenting=True,
95  processBoundaries=True,
96  processVolumes=True,
97  context=context.geoContext,
98  )
99 
100  jmw = JsonMaterialWriter(
101  level=acts.logging.VERBOSE,
102  converterCfg=jmConverterCfg,
103  fileName=os.path.join(outputDir, mapName),
104  writeFormat=JsonFormat.Json,
105  )
106 
107  if dumpMaterialTracks:
108  s.addWriter(
109  RootMaterialTrackWriter(
110  level=acts.logging.INFO,
111  collection=mmAlgCfg.collection,
112  filePath=os.path.join(
113  outputDir,
114  mapName + "_tracks.root",
115  ),
116  storeSurface=True,
117  storeVolume=True,
118  )
119  )
120 
121  mmAlgCfg.materialWriters = [jmw]
122 
123  s.addAlgorithm(MaterialMapping(level=acts.logging.INFO, config=mmAlgCfg))
124 
125  return s
126 
127 
128 if "__main__" == __name__:
129  p = argparse.ArgumentParser(
130  description="Script to run material mapping on ITk geometry"
131  )
132  p.add_argument(
133  "geo_dir",
134  help="Input directory containing the ITk standalone geometry. Get in touch if you don't have this.",
135  )
136  p.add_argument(
137  "--inputFile",
138  type=str,
139  default="",
140  help="Input file containing material steps.",
141  )
142  p.add_argument(
143  "--material",
144  type=str,
145  default="",
146  help="Geometry file to define layers used in material mapping",
147  )
148 
149  args = p.parse_args()
150 
151  geo_example_dir = Path(args.geo_dir)
152  assert geo_example_dir.exists(), "Detector example input directory missing"
153  assert os.path.exists(
154  args.inputFile
155  ), "Invalid file in --inputFile. Please check your input!"
156  assert os.path.exists(
157  args.material
158  ), "Invalid file path/name in --material. Please check your input!"
159 
160  from acts.examples.itk import buildITkGeometry
161 
162  detector, trackingGeometry, decorators = buildITkGeometry(
163  geo_example_dir, customMaterialFile=args.material
164  )
165 
167  trackingGeometry,
168  decorators,
169  materialStepsFile=args.inputFile,
170  outputDir=os.getcwd(),
171  inputDir=os.getcwd(),
172  readCachedSurfaceInformation=False,
173  ).run()