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

Functions

None run (inFileName="FieldTest.txt", rootFileName="BFieldTest.root", float|int cmScale=1.0, bool storeCoords=False)
 
None createRootMap (inFileName, rootFileName, cmScale, storeCoords)
 
def findRanges (inFileName, cmScale)
 

Function Documentation

◆ createRootMap()

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

Definition at line 59 of file convertNoisyMap.py.

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

◆ findRanges()

def convertNoisyMap.findRanges (   inFileName,
  cmScale 
)

Definition at line 174 of file convertNoisyMap.py.

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

◆ run()

None convertNoisyMap.run (   inFileName = "FieldTest.txt",
  rootFileName = "BFieldTest.root",
float | int   cmScale = 1.0,
bool   storeCoords = False 
)

Definition at line 53 of file convertNoisyMap.py.

55) -> None:
56 createRootMap(inFileName, rootFileName, cmScale, storeCoords)
57
58