24 """Constructor for a two dimensional ROOT histogram TH2Poly setup with hexagonal bins.
26 In contrast to TH2Poly.Honeycomb this does not leave any space
in the histogram range uncovered
27 and allows
for different scales
in the x
and y direction.
32 The name of the histogram to identify the object
in ROOT.
34 Title of the histogram
35 n_x_bins : float
or int
36 Number of hexagons
in the x direction to cover the whole range
38 The lower bound of the histogram
in the x direction.
40 The upper bound of the histogram
in the x direction.
41 n_y_bins : float
or int
42 Number of hexagons
in the y direction to cover the whole range
44 The lower bound of the histogram
in the y direction.
46 The upper bound of the histogram
in the x direction.
47 y_orientation : bool, optional
48 Switch whether the hexagones should be primarily aligned along the y coordinate
49 instead of the x coordinate, which
is the default
54 Two dimensional histogram populated
with
58 hex_histogram = ROOT.TH2Poly(name,
69 unit_radius_hex_xs = np.array([math.sin(2.0 * pi * i / 6.0)
for i
in range(-2, 4)])
70 unit_radius_hex_ys = np.array([math.cos(2.0 * pi * i / 6.0)
for i
in range(-2, 4)])
73 unit_radius_hex_xs -= unit_radius_hex_xs[0]
74 unit_radius_hex_ys -= unit_radius_hex_ys[0]
77 hex_x_width = unit_radius_hex_xs[3] - unit_radius_hex_ys[0]
78 hex_y_width = unit_radius_hex_ys[2] - unit_radius_hex_ys[0]
79 hex_y_protrusion = unit_radius_hex_ys[2] - unit_radius_hex_ys[1]
81 unit_width_hex_xs = unit_radius_hex_xs / hex_x_width
82 unit_width_hex_ys = unit_radius_hex_ys / hex_y_width
86 n_x_bins, n_y_bins = n_y_bins, n_x_bins
87 x_lower_bound, y_lower_bound = y_lower_bound, x_lower_bound
88 x_upper_bound, y_upper_bound = y_upper_bound, x_upper_bound
91 n_y_bins_protrusion_correction = hex_y_protrusion / hex_y_width
93 base_x_bin_width = float(x_upper_bound - x_lower_bound) / n_x_bins
94 base_y_bin_width = float(y_upper_bound - y_lower_bound) / (n_y_bins - n_y_bins_protrusion_correction)
96 base_hex_xs = unit_width_hex_xs * base_x_bin_width
97 base_hex_ys = unit_width_hex_ys * base_y_bin_width
98 base_y_protrusion = base_hex_ys[2] - base_hex_ys[1]
101 x_bin_edges = np.linspace(x_lower_bound, x_upper_bound, n_x_bins + 1)
102 even_x_lower_bin_edges = x_bin_edges[:-1]
104 odd_x_lower_bin_edges = x_bin_edges - base_x_bin_width / 2.0
106 y_bin_edges = np.linspace(y_lower_bound, y_upper_bound + base_y_protrusion, n_y_bins + 1)
107 y_lower_bin_edges = y_bin_edges[:-1]
110 for i_y_bin, y_lower_bin_edge
in enumerate(y_lower_bin_edges):
112 x_lower_bin_edges = odd_x_lower_bin_edges
114 x_lower_bin_edges = even_x_lower_bin_edges
116 for x_lower_bin_edge
in x_lower_bin_edges:
117 hex_xs = base_hex_xs + x_lower_bin_edge
118 hex_ys = base_hex_ys + y_lower_bin_edge
122 hex_histogram.AddBin(6, hex_ys, hex_xs)
124 hex_histogram.AddBin(6, hex_xs, hex_ys)
132 normal_distributed_x_values = np.random.randn(n_data)
133 normal_distributed_y_values = 2.0 * np.random.randn(n_data)
134 weights = np.ones_like(normal_distributed_y_values)
144 hex_histogram = TH2Hex(
"hex_normal",
145 "Two dimensional normal distribution in a hexagonal binning",
154 hex_histogram.FillN(n_data, normal_distributed_x_values, normal_distributed_y_values, weights)
165 ROOT.gStyle.SetPalette(root_palette[
"rainbow"])
167 hex_histogram.Draw(
"colz")
171if __name__ ==
"__main__":