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

Functions

None run (str inFileName="test07_10cm_grid.table", str outFileName="MuonFilterBFieldMap1.txt")
 
None createTextMap (inFileName, outFileName)
 
str formatNumber (float x)
 
None createRootMap (inFileName, outFileName)
 

Function Documentation

◆ createRootMap()

None convertRALMap.createRootMap (   inFileName,
  outFileName 
)

Definition at line 185 of file convertRALMap.py.

185def createRootMap(inFileName, outFileName) -> None:
186 print(f"Create ROOT map {outFileName} from {inFileName}")
187
188 # Define ROOT file and its TTree
189 theFile = ROOT.TFile.Open(outFileName, "recreate")
190
191 rangeTree = ROOT.TTree("Range", "Range")
192 rangeTree.SetDirectory(theFile)
193
194 # Coordinate ranges
195 rStruct = ROOT.rangeStruct()
196 rangeTree.Branch("xMin", ROOT.addressof(rStruct, "xMin"), "xMin/F")
197 rangeTree.Branch("xMax", ROOT.addressof(rStruct, "xMax"), "xMax/F")
198 rangeTree.Branch("dx", ROOT.addressof(rStruct, "dx"), "dx/F")
199 rangeTree.Branch("yMin", ROOT.addressof(rStruct, "yMin"), "yMin/F")
200 rangeTree.Branch("yMax", ROOT.addressof(rStruct, "yMax"), "yMax/F")
201 rangeTree.Branch("dy", ROOT.addressof(rStruct, "dy"), "dy/F")
202 rangeTree.Branch("zMin", ROOT.addressof(rStruct, "zMin"), "zMin/F")
203 rangeTree.Branch("zMax", ROOT.addressof(rStruct, "zMax"), "zMax/F")
204 rangeTree.Branch("dz", ROOT.addressof(rStruct, "dz"), "dz/F")
205
206 dataTree = ROOT.TTree("Data", "Data")
207 dataTree.SetDirectory(theFile)
208
209 # Field components with (x,y,z) coordinate binning ordered such that
210 # z, then y, then x is increased. For the coordinate bin (iX, iY, iZ),
211 # the field bin = (iX*Ny + iY)*Nz + iZ, where Ny and Nz are the number
212 # of y and z bins
213 dStruct = ROOT.dataStruct()
214 dataTree.Branch("Bx", ROOT.addressof(dStruct, "Bx"), "Bx/F")
215 dataTree.Branch("By", ROOT.addressof(dStruct, "By"), "By/F")
216 dataTree.Branch("Bz", ROOT.addressof(dStruct, "Bz"), "Bz/F")
217
218 # Open text file and process the information
219 iLine = 0
220
221 # Number of bins initialised by reading first line
222 Nx = 0
223 Ny = 0
224 Nz = 0
225
226 with open(inFileName) as f:
227 for line in f:
228 iLine += 1
229 sLine = line.split()
230
231 # First line contains ranges
232 if iLine == 1:
233 rStruct.xMin = float(sLine[1])
234 rStruct.xMax = float(sLine[2])
235 rStruct.dx = float(sLine[3])
236 rStruct.yMin = float(sLine[4])
237 rStruct.yMax = float(sLine[5])
238 rStruct.dy = float(sLine[6])
239 rStruct.zMin = float(sLine[7])
240 rStruct.zMax = float(sLine[8])
241 rStruct.dz = float(sLine[9])
242
243 Nx = int(((rStruct.xMax - rStruct.xMin) / rStruct.dx) + 1.0)
244 Ny = int(((rStruct.yMax - rStruct.yMin) / rStruct.dy) + 1.0)
245 Nz = int(((rStruct.zMax - rStruct.zMin) / rStruct.dz) + 1.0)
246
247 print(f"Nx = {Nx}, Ny = {Ny}, Nz = {Nz}")
248
249 rangeTree.Fill()
250
251 elif iLine > 2:
252 # B field components
253 dStruct.Bx = float(sLine[0])
254 dStruct.By = float(sLine[1])
255 dStruct.Bz = float(sLine[2])
256
257 # Also store bin centre coordinates.
258 # Map is ordered in ascending z, y, then x
259 # iBin = iLine - 3
260 # zBin = iBin%Nz
261 # yBin = int((iBin/Nz))%Ny
262 # xBin = int(iBin/Nzy)
263 # dStruct.x = rStruct.dx*(xBin + 0.5) + rStruct.xMin
264 # dStruct.y = rStruct.dy*(yBin + 0.5) + rStruct.yMin
265 # dStruct.z = rStruct.dz*(zBin + 0.5) + rStruct.zMin
266
267 dataTree.Fill()
268
269 theFile.cd()
270 rangeTree.Write()
271 dataTree.Write()
272 theFile.Close()
273
274
int open(const char *, int)
Opens a file descriptor.

◆ createTextMap()

None convertRALMap.createTextMap (   inFileName,
  outFileName 
)

Definition at line 52 of file convertRALMap.py.

52def createTextMap(inFileName, outFileName) -> None:
53 print(f"Creating text map {outFileName} from {inFileName}")
54
55 tmpFileName = "tmpFile.txt"
56
57 iLine = 0
58 xMin = 0.0
59 xMax = 0.0
60 dx = 0.0
61 yMin = 0.0
62 yMax = 0.0
63 dy = 0.0
64 zMin = 0.0
65 zMax = 0.0
66 dz = 0.0
67
68 # Offsets (in cm)
69 # ox = 0.0
70 # oy = 0.0
71 # oz = 0.0
72
73 iLine = 0
74 # Convert metres to centimetres
75 m2cm = 100.0
76
77 # For finding the delta bin widths
78 xOld = 0.0
79 yOld = 0.0
80 zOld = 0.0
81 gotdx = 0
82 gotdy = 0
83 gotdz = 0
84
85 firstDataLine = 9
86
87 with open(inFileName) as inFile, open(tmpFileName, "w") as tmpFile:
88 for inLine in inFile:
89 iLine += 1
90 # Skip the first few lines
91 if iLine >= firstDataLine:
92 words = inLine.split()
93 # print 'words = {0}'.format(words)
94 # Convert the x,y,z co-ords to centimetres
95 x = float(words[0]) * m2cm
96 y = float(words[1]) * m2cm
97 z = float(words[2]) * m2cm
98 Bx = float(words[3])
99 By = float(words[4])
100 Bz = float(words[5])
101
102 BxWord = formatNumber(Bx)
103 ByWord = formatNumber(By)
104 BzWord = formatNumber(Bz)
105
106 # Write out the new line. Just print out the B field components, since we
107 # can infer x,y,z co-ords from the ordering
108 newLine = f"{BxWord} {ByWord} {BzWord}\n"
109 # newLine = '{0:.0f} {1:.0f} {2:.0f} {3:.3e} {4:.3e} {5:.3e}\n'.format(x,y,z,Bx,By,Bz)
110 tmpFile.write(newLine)
111
112 # Keep track of the min/max values
113 if iLine == firstDataLine:
114 xMin = x
115 xMax = x
116 xOld = x
117 yMin = y
118 yMax = y
119 yOld = y
120 zMin = z
121 zMax = z
122 zOld = z
123
124 if x < xMin:
125 xMin = x
126 if x > xMax:
127 xMax = x
128 if y < yMin:
129 yMin = y
130 if y > yMax:
131 yMax = y
132 if z < zMin:
133 zMin = z
134 if z > zMax:
135 zMax = z
136
137 if gotdx == 0 and x != xOld:
138 dx = x - xOld
139 gotdx = 1
140 if gotdy == 0 and y != yOld:
141 dy = y - yOld
142 gotdy = 1
143 if gotdz == 0 and z != zOld:
144 dz = z - zOld
145 gotdz = 1
146
147 print(f"dx = {dx}, dy = {dy}, dz = {dz}")
148 print(f"x = {xMin} to {xMax}, y = {yMin} to {yMax}, z = {zMin} to {zMax}")
149
150 # Write out the map containing the coordinate ranges and offsets etc
151 with open(tmpFileName) as tmpFile2, open(outFileName, "w") as outFile:
152 outLine = (
153 f"CLimits {xMin:.0f} {xMax:.0f} {dx:.0f} {yMin:.0f} {yMax:.0f} {dy:.0f} {zMin:.0f} {zMax:.0f} {dz:.0f}\n"
154 )
155 outFile.write(outLine)
156
157 # outLine = 'Offsets {0:.0f} {1:.0f} {2:.0f}\n'.format(ox, oy, oz)
158 # outFile.write(outLine)
159
160 # Write a line showing the variable names (for file readability)
161 outLine = "Bx(T) By(T) Bz(T)\n"
162 # outLine = 'x(cm) y(cm) z(cm) Bx(T) By(T) Bz(T)\n'
163 outFile.write(outLine)
164
165 # Copy the tmp file data
166 for tLine in tmpFile2:
167 outFile.write(tLine)
168
169

◆ formatNumber()

str convertRALMap.formatNumber ( float  x)

Definition at line 170 of file convertRALMap.py.

170def formatNumber(x: float) -> str:
171 # To save disk space, reduce the precision of the field value
172 # as we go below various thresholds
173
174 # Let the general precision be 0.01 mT. Anything below this
175 # is set to zero.
176 xWord = f"{x:.5f}"
177
178 if abs(x) < 1e-5:
179 # Set to zero
180 xWord = "0"
181
182 return xWord
183
184

◆ run()

None convertRALMap.run ( str   inFileName = "test07_10cm_grid.table",
str   outFileName = "MuonFilterBFieldMap1.txt" 
)

Definition at line 43 of file convertRALMap.py.

43def run(inFileName: str = "test07_10cm_grid.table", outFileName: str = "MuonFilterBFieldMap1.txt") -> None:
44 # Text format
45 createTextMap(inFileName, outFileName)
46
47 # Also create ROOT file based on the restructured text output
48 rootFileName = outFileName.replace(".txt", ".root")
49 createRootMap(outFileName, rootFileName)
50
51