16from matplotlib.backends.backend_pdf
import PdfPages
17from matplotlib
import pyplot
as pl
19from basf2
import Path, process
23import matplotlib
as mpl
32stepsU = (1800, -400., 500.)
33stepsV = (1600, -400., 400.)
34fieldtype =
"MagneticField"
35filename = f
"fieldmap-{scan_type}.pdf"
57fieldmap_file = os.path.splitext(filename)[0] +
"-fieldmap.root"
58material_file = os.path.splitext(filename)[0] +
"-material.root"
62 material_plane = [0, 0, -0.1, 1, 0, 0, 0, 1, 0]
63elif scan_type ==
"zx":
64 material_plane = [0, -0.1, 0, 0, 0, 1, 1, 0, 0]
65elif scan_type ==
"zy":
66 material_plane = [-0.1, 0, 0, 0, 0, 1, 0, 1, 0]
70main.add_module(
'EventInfoSetter')
71main.add_module(
'Gearbox')
72geometry = main.add_module(
'Geometry')
73main.add_module(
'FullSim')
75if fieldtype !=
"MagneticField":
78 "excludedComponents": [
"MagneticField"],
79 "additionalComponents": [fieldtype],
83main.add_module(
'CreateFieldMap', **{
84 'filename': fieldmap_file,
95main.add_module(
'MaterialScan', **{
96 'Filename': material_file,
99 'planar.plane':
'custom',
100 'planar.custom': material_plane,
101 'planar.maxDepth': 0.2,
102 'planar.nU': stepsU[0],
103 'planar.minU': stepsU[1],
104 'planar.maxU': stepsU[2],
105 'planar.nV': stepsV[0],
106 'planar.minV': stepsV[1],
107 'planar.maxV': stepsV[2],
108 'planar.splitByMaterials':
False,
109 'planar.ignored': [
'Air',
'Vacuum',
'G4_AIR',
'ColdAir'],
120def get_hist_data(hist):
121 """Obtain the data and extent of a ROOT TH2 as a numpy array"""
122 nbinsX = hist.GetNbinsX()
123 nbinsY = hist.GetNbinsY()
124 data = np.frombuffer(hist.GetArray(), count=hist.GetSize())
126 data = data.reshape(nbinsY + 2, nbinsX + 2)[1:-1, 1:-1]
128 xmin = hist.GetXaxis().GetXmin()
129 xmax = hist.GetXaxis().GetXmax()
130 ymin = hist.GetYaxis().GetXmin()
131 ymax = hist.GetYaxis().GetXmax()
133 return data, (xmin, xmax, ymin, ymax)
136def plotMap(axes, data, extent, cmap, **kwargs):
137 """Plot either material or field map"""
138 return axes.imshow(data, extent=extent, origin=
"lower", cmap=cmap,
139 aspect=
"auto", interpolation=
"nearest", **kwargs)
143rfieldmap_file = root.TFile(fieldmap_file)
144rmaterial_file = root.TFile(material_file)
146mat_hist = rmaterial_file.Get(
"Planar/All_Regions_x0")
147mat_data, mat_extent = get_hist_data(mat_hist)
149cmap_fieldmap = mpl.cm.get_cmap(
'seismic')
150cmap_material = mpl.cm.get_cmap(
'binary')
152pdf = PdfPages(filename)
153for component
in [
"Bx",
"By",
"Bz",
"B"]:
154 print(f
"Create {component} fieldmap plot")
155 h = rfieldmap_file.Get(component)
156 data, extent = get_hist_data(h)
159 field_max = max(np.abs(data).max(), 0.1)
161 a = f.add_subplot(111)
163 lognorm = mpl.colors.LogNorm()
164 plotMap(a, mat_data, mat_extent, cmap_material, norm=lognorm)
167 m = plotMap(a, data, extent, cmap_fieldmap, alpha=0.9,
168 vmin=-field_max, vmax=field_max)
171 if len(component) == 1:
172 cb.set_label(
r"$B/{\rm T}$")
174 cb.set_label(
r"$B_%s/{\rm T}$" % component[1])
176 if show_contours
is not None and len(show_contours) > 0:
178 x, xstep = np.linspace(extent[0], extent[1], h.GetNbinsX() + 1,
181 y, ystep = np.linspace(extent[2], extent[3], h.GetNbinsY() + 1,
184 x, y = np.meshgrid(x[:-1], y[:-1])
186 cs = a.contour(x, y, data, levels=show_contours, colors=contour_colors,
187 linewidths=contour_width)
188 a.clabel(cs, fmt=
"%.2f T", fontsize=6)
191 a.set_title(h.GetTitle())
192 a.set_xlabel(
r"$%s/{\rm cm}$" % scan_type[0])
193 a.set_ylabel(
r"$%s/{\rm cm}$" % scan_type[1])