EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
binning.py
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file binning.py
1 import array
2 import math
3 
4 import ROOT
5 
6 class Bins(object):
7  '''
8  Base class for generating bin boundaries in some range.
9  '''
10  def __init__(self, n, lower, upper):
11  '''
12  Initialise with the number of ranges for which
13  to generate bin edges, in the range [lower, upper].
14  '''
15  self.n = n
16  self.lower = lower
17  self.upper = upper
18 
19 
20 class BinsLog10(Bins):
21  '''
22  Generator class for bins with equal intervals in log10.
23  '''
24  def __init__(self, n, lower, upper):
25  super(BinsLog10, self).__init__(n, lower, upper)
26  self.width = math.log10(self.upper / self.lower) / self.n
27  # Redefine lower and upper as log10 equivalents
28  self.lower = math.log10(self.lower)
29  self.upper = math.log10(self.upper)
30 
31  def edges(self):
32  '''
33  Generator function for (n+1) bin edges.
34  '''
35  for i in range(0, self.n + 1):
36  yield math.pow(10., self.lower + i * self.width)
37 
38  def lower_edges(self):
39  '''
40  Generator function for n bin lower edges.
41  '''
42  for i in range(0, self.n):
43  yield math.pow(10., self.lower + i * self.width)
44 
45  def upper_edges(self):
46  '''
47  Generator function for n bin upper edges.
48  '''
49  for i in range(1, self.n + 1):
50  yield math.pow(10., self.lower + i * self.width)
51 
52 
53 def bin_log10(obj):
54  '''
55  Rebins an axis such that bins span equal intervals in log_10(x).
56  '''
57  if isinstance(obj, ROOT.TAxis):
58  binner = BinsLog10(obj.GetNbins(), obj.GetXmin(), obj.GetXmax())
59  # Need array('d') to pass to TAxis.Set() in place of Double_t*
60  edgearray = array.array('d', [x for x in binner.edges()])
61  obj.Set(obj.GetNbins(), edgearray)
62  elif isinstance(obj, ROOT.TH1):
63  axes = [obj.GetXaxis(), obj.GetYaxis(), obj.GetZaxis()]
64  for i in range(0, obj.GetDimension()):
65  bin_log10(axes[i])