Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
bfield_writing.py
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file bfield_writing.py
1 #!/usr/bin/env python3
2 from pathlib import Path
3 
4 import acts
5 import acts.examples
6 
7 u = acts.UnitConstants
8 
9 
10 def RootBFieldWrite(bField, fileName, treeName="solenoid", level=acts.logging.VERBOSE):
11  cfg = acts.examples.RootBFieldWriter.Config()
12  cfg.bField = bField
13  cfg.gridType = acts.examples.RootBFieldWriter.GridType.rz
14  cfg.fileName = str(fileName)
15  cfg.treeName = treeName
16  acts.examples.RootBFieldWriter.run(cfg, level)
17  return cfg
18 
19 
20 def CsvBFieldWrite(bField, fileName, level=acts.logging.VERBOSE):
21  cfg = acts.examples.CsvBFieldWriter.ConfigRzGrid()
22  cfg.bField = bField
23  cfg.fileName = str(fileName)
24  acts.examples.CsvBFieldWriter.runRzGrid(cfg, level)
25  return cfg
26 
27 
28 def runBFieldWriting(outputDir: Path, rewrites: int = 0):
29  solenoid = acts.SolenoidBField(
30  radius=1200 * u.mm, length=6000 * u.mm, bMagCenter=2 * u.T, nCoils=1194
31  )
32  field = acts.solenoidFieldMap(
33  rlim=(0, 1200 * u.mm),
34  zlim=(-5000 * u.mm, 5000 * u.mm),
35  nbins=(10, 10),
36  field=solenoid,
37  )
38 
39  print("Solenoid ready")
40 
41  cfg = RootBFieldWrite(field, outputDir / "solenoid.root")
42  CsvBFieldWrite(field, outputDir / "solenoid.csv")
43 
44  for i in range(rewrites):
45  print(f"Now read back {cfg.fileName}")
46 
47  field2 = acts.examples.MagneticFieldMapRz(cfg.fileName, tree="solenoid")
48  cfg2 = RootBFieldWrite(field2, outputDir / f"solenoid{i+2}.root")
49  CsvBFieldWrite(field2, outputDir / f"solenoid{i+2}.csv")
50  cfg = cfg2
51 
52  print("Done")
53 
54 
55 if "__main__" == __name__:
56  runBFieldWriting(Path.cwd())