Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
itk.py
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file itk.py
1 #!/usr/bin/env python3
2 import sys
3 from pathlib import Path
4 import argparse
5 
6 from acts.examples import (
7  WhiteBoard,
8  AlgorithmContext,
9  ProcessCode,
10  CsvTrackingGeometryWriter,
11  ObjTrackingGeometryWriter,
12  JsonSurfacesWriter,
13  JsonMaterialWriter,
14  JsonFormat,
15 )
16 
17 import acts
18 
19 from acts import MaterialMapJsonConverter, UnitConstants as u
20 
21 
22 def runITk(
23  trackingGeometry,
24  decorators,
25  outputDir: Path,
26  events=1,
27  outputObj=True,
28  outputCsv=False,
29  outputJson=False,
30 ):
31 
32  for ievt in range(events):
33  eventStore = WhiteBoard(name=f"EventStore#{ievt}", level=acts.logging.INFO)
34  ialg = 0
35 
36  context = AlgorithmContext(ialg, ievt, eventStore)
37 
38  for cdr in decorators:
39  r = cdr.decorate(context)
40  if r != ProcessCode.SUCCESS:
41  raise RuntimeError("Failed to decorate event context")
42 
43  if outputCsv:
44  csv_dir = outputDir / "csv"
45  csv_dir.mkdir(exist_ok=True)
46  writer = CsvTrackingGeometryWriter(
47  level=acts.logging.INFO,
48  trackingGeometry=trackingGeometry,
49  outputDir=str(csv_dir),
50  writePerEvent=True,
51  )
52  writer.write(context)
53 
54  if outputObj:
55  obj_dir = outputDir / "obj"
56  obj_dir.mkdir(exist_ok=True)
57  writer = ObjTrackingGeometryWriter(
58  level=acts.logging.INFO,
59  outputDir=str(obj_dir),
60  )
61  writer.write(context, trackingGeometry)
62 
63  if outputJson:
64  json_dir = outputDir / "json"
65  json_dir.mkdir(exist_ok=True)
66  writer = JsonSurfacesWriter(
67  level=acts.logging.INFO,
68  trackingGeometry=trackingGeometry,
69  outputDir=str(json_dir),
70  writePerEvent=True,
71  writeSensitive=True,
72  )
73  writer.write(context)
74 
75  jmConverterCfg = MaterialMapJsonConverter.Config(
76  processSensitives=True,
77  processApproaches=True,
78  processRepresenting=True,
79  processBoundaries=True,
80  processVolumes=True,
81  processNonMaterial=True,
82  context=context.geoContext,
83  )
84 
85  jmw = JsonMaterialWriter(
86  level=acts.logging.VERBOSE,
87  converterCfg=jmConverterCfg,
88  fileName=str(json_dir / "material-map"),
89  writeFormat=JsonFormat.Json,
90  )
91 
92  jmw.write(trackingGeometry)
93 
94 
95 if "__main__" == __name__:
96  p = argparse.ArgumentParser(
97  description="Example script to construct the ITk geometry and write it out to CSV and OBJ formats"
98  )
99  p.add_argument(
100  "geo_dir",
101  help="Input directory containing the ITk standalone geometry. Get in touch if you don't have this.",
102  )
103  p.add_argument(
104  "--output-dir",
105  default=Path.cwd(),
106  type=Path,
107  help="Directory to write outputs to",
108  )
109  p.add_argument(
110  "--output-csv", action="store_true", help="Write geometry in CSV format."
111  )
112  p.add_argument(
113  "--output-obj", action="store_true", help="Write geometry in OBJ format."
114  )
115  p.add_argument(
116  "--output-json",
117  action="store_true",
118  help="Write geometry and material in JSON format.",
119  )
120  p.add_argument(
121  "--no-material", action="store_true", help="Decorate material to the geometry"
122  )
123 
124  args = p.parse_args()
125  args.output_dir.mkdir(exist_ok=True, parents=True)
126 
127  geo_example_dir = Path(args.geo_dir)
128  assert geo_example_dir.exists(), "Detector example input directory missing"
129  from acts.examples.itk import buildITkGeometry
130 
131  detector, trackingGeometry, decorators = buildITkGeometry(
132  geo_example_dir,
133  material=not args.no_material,
134  )
135 
136  runITk(
137  trackingGeometry=trackingGeometry,
138  decorators=decorators,
139  outputDir=args.output_dir,
140  outputCsv=args.output_csv,
141  outputObj=args.output_obj,
142  outputJson=args.output_json,
143  )