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

Functions

None run (str inFileName="FieldTest.txt", str rootFileName="BFieldTest.root", float cmScale=1.0, bool storeCoords=True)
None createRootMap (inFileName, rootFileName, cmScale, storeCoords)
 findRanges (inFileName, cmScale)

Function Documentation

◆ createRootMap()

None convertMap.createRootMap ( inFileName,
rootFileName,
cmScale,
storeCoords )

Definition at line 62 of file convertMap.py.

62def createRootMap(inFileName, rootFileName, cmScale, storeCoords) -> None:
63 print(f"Create map {rootFileName} from {inFileName} using cmScale = {cmScale}")
64 if storeCoords is True:
65 print(f"We will also store the x,y,z field coordinates in {rootFileName}")
66
67 rangeInfo = findRanges(inFileName, cmScale)
68
69 # Define ROOT file and its TTree
70 theFile = ROOT.TFile.Open(rootFileName, "recreate")
71
72 rangeTree = ROOT.TTree("Range", "Range")
73 rangeTree.SetDirectory(theFile)
74
75 # Coordinate ranges
76 rStruct = ROOT.rangeStruct()
77 rangeTree.Branch("xMin", ROOT.addressof(rStruct, "xMin"), "xMin/F")
78 rangeTree.Branch("xMax", ROOT.addressof(rStruct, "xMax"), "xMax/F")
79 rangeTree.Branch("dx", ROOT.addressof(rStruct, "dx"), "dx/F")
80 rangeTree.Branch("yMin", ROOT.addressof(rStruct, "yMin"), "yMin/F")
81 rangeTree.Branch("yMax", ROOT.addressof(rStruct, "yMax"), "yMax/F")
82 rangeTree.Branch("dy", ROOT.addressof(rStruct, "dy"), "dy/F")
83 rangeTree.Branch("zMin", ROOT.addressof(rStruct, "zMin"), "zMin/F")
84 rangeTree.Branch("zMax", ROOT.addressof(rStruct, "zMax"), "zMax/F")
85 rangeTree.Branch("dz", ROOT.addressof(rStruct, "dz"), "dz/F")
86
87 rStruct.xMin = rangeInfo["xMin"]
88 rStruct.xMax = rangeInfo["xMax"]
89 rStruct.dx = rangeInfo["dx"]
90 rStruct.yMin = rangeInfo["yMin"]
91 rStruct.yMax = rangeInfo["yMax"]
92 rStruct.dy = rangeInfo["dy"]
93 rStruct.zMin = rangeInfo["zMin"]
94 rStruct.zMax = rangeInfo["zMax"]
95 rStruct.dz = rangeInfo["dz"]
96
97 # Centre the field map on the local origin (cm)
98 x0 = 0.5 * (rStruct.xMin + rStruct.xMax)
99 y0 = 0.5 * (rStruct.yMin + rStruct.yMax)
100 z0 = 0.5 * (rStruct.zMin + rStruct.zMax)
101
102 # Use this if we don't want to centre the field map
103 # x0 = 0.0
104 # y0 = 0.0
105 # z0 = 0.0
106
107 print(f"Centering field map using coordinate shift {x0} {y0} {z0} cm")
108
109 # Center coordinate range limits (cm)
110 rStruct.xMin = rStruct.xMin - x0
111 rStruct.xMax = rStruct.xMax - x0
112
113 rStruct.yMin = rStruct.yMin - y0
114 rStruct.yMax = rStruct.yMax - y0
115
116 rStruct.zMin = rStruct.zMin - z0
117 rStruct.zMax = rStruct.zMax - z0
118
119 print(f"x range = {rStruct.xMin} to {rStruct.xMax}")
120 print(f"y range = {rStruct.yMin} to {rStruct.yMax}")
121 print(f"z range = {rStruct.zMin} to {rStruct.zMax}")
122
123 # Fill info into range tree
124 rangeTree.Fill()
125
126 # Store field data components
127 dataTree = ROOT.TTree("Data", "Data")
128 dataTree.SetDirectory(theFile)
129
130 # Field components with (x,y,z) coordinate binning ordered such that
131 # z, then y, then x is increased. For the coordinate bin (iX, iY, iZ),
132 # the field bin = (iX*Ny + iY)*Nz + iZ, where Ny and Nz are the number
133 # of y and z bins
134 dStruct = ROOT.dataStruct()
135 if storeCoords is True:
136 dataTree.Branch("x", ROOT.addressof(dStruct, "x"), "x/F")
137 dataTree.Branch("y", ROOT.addressof(dStruct, "y"), "y/F")
138 dataTree.Branch("z", ROOT.addressof(dStruct, "z"), "z/F")
139
140 dataTree.Branch("Bx", ROOT.addressof(dStruct, "Bx"), "Bx/F")
141 dataTree.Branch("By", ROOT.addressof(dStruct, "By"), "By/F")
142 dataTree.Branch("Bz", ROOT.addressof(dStruct, "Bz"), "Bz/F")
143
144 # Reopen the file and store the information in the ROOT file
145 with open(inFileName) as f:
146 # Read each line
147 for line in f:
148 # Ignore comment lines which begin with "#"
149 if "#" not in line:
150 sLine = line.split()
151
152 # Bin centre coordinates with origin shift (all in cm)
153 if storeCoords is True:
154 dStruct.x = float(sLine[0]) * cmScale - x0
155 dStruct.y = float(sLine[1]) * cmScale - y0
156 dStruct.z = float(sLine[2]) * cmScale - z0
157
158 # B field components (Tesla)
159 dStruct.Bx = float(sLine[3])
160 dStruct.By = float(sLine[4])
161 dStruct.Bz = float(sLine[5])
162
163 dataTree.Fill()
164
165 theFile.cd()
166 rangeTree.Write()
167 dataTree.Write()
168 theFile.Close()
169
170

◆ findRanges()

convertMap.findRanges ( inFileName,
cmScale )

Definition at line 171 of file convertMap.py.

171def findRanges(inFileName, cmScale):
172 # First read the data file to find the binning and coordinate ranges.
173 # Store the unique (ordered) x, y and z values so we can then find the
174 # bin widths, min/max limits and central offset
175
176 xArray = []
177 yArray = []
178 zArray = []
179
180 with open(inFileName) as f:
181 # Read each line
182 for line in f:
183 # Ignore comment lines which begin with "#"
184 if "#" not in line:
185 sLine = line.split()
186
187 x = float(sLine[0]) * cmScale
188 y = float(sLine[1]) * cmScale
189 z = float(sLine[2]) * cmScale
190
191 if x not in xArray:
192 xArray.append(x)
193
194 if y not in yArray:
195 yArray.append(y)
196
197 if z not in zArray:
198 zArray.append(z)
199
200 Nx = len(xArray)
201 Ny = len(yArray)
202 Nz = len(zArray)
203
204 xMin = 0.0
205 xMax = 0.0
206 dx = 0.0
207 yMin = 0.0
208 yMax = 0.0
209 dy = 0.0
210 zMin = 0.0
211 zMax = 0.0
212 dz = 0.0
213
214 if Nx > 0:
215 xMin = xArray[0]
216 Nx1 = Nx - 1
217 xMax = xArray[Nx1]
218 dx = (xMax - xMin) / (Nx1 * 1.0)
219
220 if Ny > 0:
221 yMin = yArray[0]
222 Ny1 = Ny - 1
223 yMax = yArray[Ny1]
224 dy = (yMax - yMin) / (Ny1 * 1.0)
225
226 if Nz > 0:
227 zMin = zArray[0]
228 Nz1 = Nz - 1
229 zMax = zArray[Nz1]
230 dz = (zMax - zMin) / (Nz1 * 1.0)
231
232 rangeInfo = {
233 "Nx": Nx,
234 "xMin": xMin,
235 "xMax": xMax,
236 "dx": dx,
237 "Ny": Ny,
238 "yMin": yMin,
239 "yMax": yMax,
240 "dy": dy,
241 "Nz": Nz,
242 "zMin": zMin,
243 "zMax": zMax,
244 "dz": dz,
245 }
246
247 # print 'rangeInfo = {0}'.format(rangeInfo)
248
249 return rangeInfo
250
251

◆ run()

None convertMap.run ( str inFileName = "FieldTest.txt",
str rootFileName = "BFieldTest.root",
float cmScale = 1.0,
bool storeCoords = True )

Definition at line 53 of file convertMap.py.

58) -> None:
59 createRootMap(inFileName, rootFileName, cmScale, storeCoords)
60
61