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