161 """Read the field as an ``(Nx, Ny, Nz, 3)`` array of Tesla.
163 ``order`` says how the flat entries map onto the grid: ``xslow`` is the
164 canonical ``(iX*Ny + iY)*Nz + iZ``, ``yslow`` its x/y transpose.
166 if order
not in ORDERS:
167 raise ValueError(f
"Unknown data order {order!r}, expected one of {ORDERS}")
169 field = np.stack(
_columns(path, FIELD_BRANCHES), axis=-1)
170 if len(field) != rng.N:
171 raise ValueError(f
"{path}: expected {rng.N} entries but found {len(field)}")
173 return field.reshape(rng.Nx, rng.Ny, rng.Nz, 3)
174 return field.reshape(rng.Ny, rng.Nx, rng.Nz, 3).transpose(1, 0, 2, 3)
190 """Per-axis deviation of the stored coordinates from ``order``, in bins."""
191 nodes = rng.node_indices(np.arange(rng.N), order)
192 limits = ((rng.xMin, rng.dx), (rng.yMin, rng.dy), (rng.zMin, rng.dz))
194 np.abs(stored - (lo + node * step)).max() / step
for stored, node, (lo, step)
in zip(coords, nodes, limits)
234 """Slices selecting the middle of the map, away from coils and yoke."""
235 xs, ys, zs = rng.axis_values()
237 def middle(values, fraction):
238 centre = 0.5 * (values[0] + values[-1])
239 half = fraction * 0.5 * (values[-1] - values[0])
240 (inside,) = np.nonzero(np.abs(values - centre) <= half)
245 nearest = int(np.abs(values - centre).argmin())
246 return slice(nearest, nearest + 1)
247 return slice(inside[0], inside[-1] + 1)
250 middle(xs, xy_fraction),
251 middle(ys, xy_fraction),
252 middle(zs, z_fraction),
257 """RMS of div B over the core of the map, and the local gradient scale.
259 A physical field has div B = 0, so an ordering that scrambles neighbouring
260 nodes shows up as a large residual. The gradient scale is the RMS of the
261 three diagonal derivatives, which is what the residual should be compared
262 against: it is the size div B would have if the derivatives did not cancel.
265 terms = [np.gradient(field[..., axis], step, axis=axis)
for axis, step
in enumerate((rng.dx, rng.dy, rng.dz))]
266 dBx, dBy, dBz = (term[region]
for term
in terms)
267 divergence = dBx + dBy + dBz
268 scale = sum(float((term**2).mean())
for term
in (dBx, dBy, dBz))
269 return float(np.sqrt((divergence**2).mean())), float(np.sqrt(scale))
279 """Write a field map in canonical order, with matching node coordinates.
281 ``order`` is the order the *input* is in. The output is always
282 ``(iX*Ny + iY)*Nz + iZ`` with freshly generated ``x``, ``y`` and ``z``
283 branches, so ``ShipBFieldMap`` can verify it.
287 if os.path.exists(out_path)
and os.path.samefile(in_path, out_path):
288 raise ValueError(f
"{out_path} is the input; write the repacked map somewhere else")
291 field =
read_field(in_path, order=order, rng=rng).reshape(rng.N, 3)
293 xs, ys, zs = rng.axis_values()
294 x, y, z = (grid.ravel()
for grid
in np.meshgrid(xs, ys, zs, indexing=
"ij"))
297 "x": x.astype(np.float32),
298 "y": y.astype(np.float32),
299 "z": z.astype(np.float32),
301 columns.update({name: field[:, i].astype(np.float32)
for i, name
in enumerate(FIELD_BRANCHES)})
302 ROOT.RDF.FromNumpy(columns).Snapshot(
"Data", str(out_path))
305 source = ROOT.TFile.Open(str(in_path))
306 destination = ROOT.TFile.Open(str(out_path),
"UPDATE")
308 source.Get(
"Range").CloneTree(-1,
"fast").Write()