FairShip
Loading...
Searching...
No Matches
convertMisisMap Namespace Reference

Functions

None run (str inFileName="BFieldTest.txt", str rootFileName="BFieldTest.root")
None createRootMap (inFileName, rootFileName)

Function Documentation

◆ createRootMap()

None convertMisisMap.createRootMap ( inFileName,
rootFileName )

Definition at line 48 of file convertMisisMap.py.

48def createRootMap(inFileName, rootFileName) -> None:
49 print(f"Create ROOT map {rootFileName} from {inFileName}")
50
51 # Define ROOT file and its TTree
52 theFile = ROOT.TFile.Open(rootFileName, "recreate")
53
54 rangeTree = ROOT.TTree("Range", "Range")
55 rangeTree.SetDirectory(theFile)
56
57 # Coordinate ranges
58 rStruct = ROOT.rangeStruct()
59 rangeTree.Branch("xMin", ROOT.addressof(rStruct, "xMin"), "xMin/F")
60 rangeTree.Branch("xMax", ROOT.addressof(rStruct, "xMax"), "xMax/F")
61 rangeTree.Branch("dx", ROOT.addressof(rStruct, "dx"), "dx/F")
62 rangeTree.Branch("yMin", ROOT.addressof(rStruct, "yMin"), "yMin/F")
63 rangeTree.Branch("yMax", ROOT.addressof(rStruct, "yMax"), "yMax/F")
64 rangeTree.Branch("dy", ROOT.addressof(rStruct, "dy"), "dy/F")
65 rangeTree.Branch("zMin", ROOT.addressof(rStruct, "zMin"), "zMin/F")
66 rangeTree.Branch("zMax", ROOT.addressof(rStruct, "zMax"), "zMax/F")
67 rangeTree.Branch("dz", ROOT.addressof(rStruct, "dz"), "dz/F")
68
69 dataTree = ROOT.TTree("Data", "Data")
70 dataTree.SetDirectory(theFile)
71
72 # Field components with (x,y,z) coordinate binning ordered such that
73 # z, then y, then x is increased. For the coordinate bin (iX, iY, iZ),
74 # the field bin = (iX*Ny + iY)*Nz + iZ, where Ny and Nz are the number
75 # of y and z bins
76 dStruct = ROOT.dataStruct()
77 dataTree.Branch("Bx", ROOT.addressof(dStruct, "Bx"), "Bx/F")
78 dataTree.Branch("By", ROOT.addressof(dStruct, "By"), "By/F")
79 dataTree.Branch("Bz", ROOT.addressof(dStruct, "Bz"), "Bz/F")
80
81 # mm to cm conversion
82 mm2cm = 0.1
83
84 # Open text file and process the information
85 iLine = 0
86
87 # Number of bins initialised by reading first line
88 Nx = 0
89 Ny = 0
90 Nz = 0
91
92 # Field centering coordinates
93 x0 = 0.0
94 y0 = 0.0
95 z0 = 0.0
96
97 with open(inFileName) as f:
98 for line in f:
99 iLine += 1
100
101 # First line contains ranges
102 if iLine == 1:
103 # Remove extraneous, unneeded symbols in the line
104 line = line.replace("[", "")
105 line = line.replace("]", "")
106 line = line.replace("mm", "")
107 sLine = line.split()
108
109 # Bin info line assumed to be formatted as:
110 # Grid Output Min: xMin yMin zMin Max: xMax yMax zMax Grid Size: dx dy dz
111 # These coordinate limits are in mm, but the actual data lines use m
112
113 print(f"sLine = {sLine}")
114 # For each value, convert from mm to cm
115 rStruct.xMin = float(sLine[3]) * mm2cm
116 rStruct.xMax = float(sLine[7]) * mm2cm
117 rStruct.dx = float(sLine[12]) * mm2cm
118 rStruct.yMin = float(sLine[4]) * mm2cm
119 rStruct.yMax = float(sLine[8]) * mm2cm
120 rStruct.dy = float(sLine[13]) * mm2cm
121 rStruct.zMin = float(sLine[5]) * mm2cm
122 rStruct.zMax = float(sLine[9]) * mm2cm
123 rStruct.dz = float(sLine[14]) * mm2cm
124
125 Nx = int(((rStruct.xMax - rStruct.xMin) / rStruct.dx) + 1.0)
126 Ny = int(((rStruct.yMax - rStruct.yMin) / rStruct.dy) + 1.0)
127 Nz = int(((rStruct.zMax - rStruct.zMin) / rStruct.dz) + 1.0)
128
129 print(f"Nx = {Nx}, Ny = {Ny}, Nz = {Nz}")
130
131 # Centre the field map on the local origin (cm)
132 x0 = 0.5 * (rStruct.xMin + rStruct.xMax)
133 y0 = 0.5 * (rStruct.yMin + rStruct.yMax)
134 z0 = 0.5 * (rStruct.zMin + rStruct.zMax)
135
136 print(f"Centering field map using coordinate shift {x0} {y0} {z0} cm")
137
138 # Center coordinate range limits (cm)
139 rStruct.xMin = rStruct.xMin - x0
140 rStruct.xMax = rStruct.xMax - x0
141
142 rStruct.yMin = rStruct.yMin - y0
143 rStruct.yMax = rStruct.yMax - y0
144
145 rStruct.zMin = rStruct.zMin - z0
146 rStruct.zMax = rStruct.zMax - z0
147
148 # Fill info into range tree
149 rangeTree.Fill()
150
151 # Field data values start from line 3
152 elif iLine > 2:
153 sLine = line.split()
154
155 # B field components (Tesla)
156 dStruct.Bx = float(sLine[3])
157 dStruct.By = float(sLine[4])
158 dStruct.Bz = float(sLine[5])
159
160 dataTree.Fill()
161
162 theFile.cd()
163 rangeTree.Write()
164 dataTree.Write()
165 theFile.Close()
166
167

◆ run()

None convertMisisMap.run ( str inFileName = "BFieldTest.txt",
str rootFileName = "BFieldTest.root" )

Definition at line 44 of file convertMisisMap.py.

44def run(inFileName: str = "BFieldTest.txt", rootFileName: str = "BFieldTest.root") -> None:
45 createRootMap(inFileName, rootFileName)
46
47