ATLAS Offline Software
Classes | Functions | Variables
python.histsampling Namespace Reference

Classes

class  TH1
 
class  TH2
 

Functions

def load_hist (*args)
 
def get_sampling_vars (h)
 
def get_random_bin (globalbins, cheights)
 
def get_random_x (h, globalbins, cheights, globalbin_to_axisbin)
 
def get_random_xy (h2, globalbins, cheights, globalbin_to_axisbin)
 

Variables

string __author__ = "Andy Buckley <andy.buckley@cern.ch>"
 

Function Documentation

◆ get_random_bin()

def python.histsampling.get_random_bin (   globalbins,
  cheights 
)
Choose a random bin from the cumulative distribution list of nbins+1 entries.

TODO: Search more efficiently (lin and log guesses, then lin search or
binary split depending on vector size).

Definition at line 59 of file histsampling.py.

59 def get_random_bin(globalbins, cheights):
60  """
61  Choose a random bin from the cumulative distribution list of nbins+1 entries.
62 
63  TODO: Search more efficiently (lin and log guesses, then lin search or
64  binary split depending on vector size).
65  """
66  assert len(cheights) == len(globalbins)+1
67  randomheight = random.uniform(0, cheights[-1])
68  for i, iglobal in enumerate(globalbins):
69  if randomheight >= cheights[i] and randomheight < cheights[i+1]:
70  return iglobal
71  raise Exception("Sample fell outside range of cumulative distribution?!?!")
72 
73 

◆ get_random_x()

def python.histsampling.get_random_x (   h,
  globalbins,
  cheights,
  globalbin_to_axisbin 
)
Choose a random bin via get_random_bin, then pick a uniform random x
point in that bin (without any attempt at estimating the in-bin distribution).

Definition at line 74 of file histsampling.py.

74 def get_random_x(h, globalbins, cheights, globalbin_to_axisbin):
75  """
76  Choose a random bin via get_random_bin, then pick a uniform random x
77  point in that bin (without any attempt at estimating the in-bin distribution).
78  """
79  irand = get_random_bin(globalbins, cheights)
80  axisids = globalbin_to_axisbin.get(irand)
81  assert axisids is not None
82  xrand = random.uniform(h.GetXaxis().GetBinLowEdge(axisids[0]), h.GetXaxis().GetBinUpEdge(axisids[0]))
83  return xrand
84 
85 

◆ get_random_xy()

def python.histsampling.get_random_xy (   h2,
  globalbins,
  cheights,
  globalbin_to_axisbin 
)
Choose a random bin via get_random_bin, then pick a uniform random x,y
point in that bin (without any attempt at estimating the in-bin distribution).

Definition at line 86 of file histsampling.py.

86 def get_random_xy(h2, globalbins, cheights, globalbin_to_axisbin):
87  """
88  Choose a random bin via get_random_bin, then pick a uniform random x,y
89  point in that bin (without any attempt at estimating the in-bin distribution).
90  """
91  irand = get_random_bin(globalbins, cheights)
92  axisids = globalbin_to_axisbin.get(irand)
93  assert axisids is not None
94  xrand = random.uniform(h2.GetXaxis().GetBinLowEdge(axisids[0]), h2.GetXaxis().GetBinUpEdge(axisids[0]))
95  yrand = random.uniform(h2.GetYaxis().GetBinLowEdge(axisids[1]), h2.GetYaxis().GetBinUpEdge(axisids[1]))
96  return xrand, yrand
97 
98 

◆ get_sampling_vars()

def python.histsampling.get_sampling_vars (   h)
Get the following from a histogram h, since the ROOT API sucks:
* list of global bin IDs (not even contiguous for 2D, gee thanks ROOT)
* dict mapping global bin IDs to a tuple of axis bin IDs
* list of nbins+1 cumulative bin values, in the same order as globalbins

Definition at line 33 of file histsampling.py.

33 def get_sampling_vars(h):
34  """
35  Get the following from a histogram h, since the ROOT API sucks:
36  * list of global bin IDs (not even contiguous for 2D, gee thanks ROOT)
37  * dict mapping global bin IDs to a tuple of axis bin IDs
38  * list of nbins+1 cumulative bin values, in the same order as globalbins
39  """
40  globalbin_to_axisbin = {} # for reverse axis bin lookup to get edges
41  globalbins = [] # because they aren't easily predicted, nor contiguous
42  cheights = [0] # cumulative "histogram" from which to uniformly sample
43  if issubclass(type(h), ROOT.TH1):
44  for ix in range(1, h.GetNbinsX()+1):
45  iglobal = h.GetBin(ix)
46  globalbins.append(iglobal)
47  globalbin_to_axisbin[iglobal] = (ix,)
48  cheights.append(cheights[-1] + h.GetBinContent(iglobal))
49  elif issubclass(type(h), ROOT.TH2):
50  for ix in range(1, h.GetNbinsX()+1):
51  for iy in range(1, h.GetNbinsY()+1):
52  iglobal = h.GetBin(ix, iy)
53  globalbins.append(iglobal)
54  globalbin_to_axisbin[iglobal] = (ix, iy)
55  cheights.append(cheights[-1] + h.GetBinContent(iglobal))
56  return globalbins, globalbin_to_axisbin, cheights
57 
58 

◆ load_hist()

def python.histsampling.load_hist ( args)
Load a histogram from a filename/TFile and histo name. If a single arg is
provided, it has to be a histo object and will be cloned before return.

Definition at line 13 of file histsampling.py.

13 def load_hist(*args):
14  """
15  Load a histogram from a filename/TFile and histo name. If a single arg is
16  provided, it has to be a histo object and will be cloned before return.
17  """
18  h = None
19  if len(args) == 1 and issubclass(type(args[0]), ROOT.TH1):
20  h = args[0].Clone()
21  elif len(args) == 2:
22  if isinstance(args[0], str) and isinstance(args[1], str) :
23  f = ROOT.TFile.Open(args[0])
24  h = copy.deepcopy(f.Get(args[1]).Clone())
25  #f.Close()
26  elif type(args[0]) is ROOT.TFile and type(args[1]) is str:
27  h = args[0].Get(args[1]).Clone()
28  if h is None:
29  raise Exception("Error in histogram loading from " + args)
30  return h
31 
32 

Variable Documentation

◆ __author__

string python.histsampling.__author__ = "Andy Buckley <andy.buckley@cern.ch>"
private

Definition at line 8 of file histsampling.py.

Get
T * Get(TFile &f, const std::string &n, const std::string &dir="", const chainmap_t *chainmap=0, std::vector< std::string > *saved=0)
get a histogram given a path, and an optional initial directory if histogram is not found,...
Definition: comparitor.cxx:178
plotBeamSpotVxVal.range
range
Definition: plotBeamSpotVxVal.py:195
python.histsampling.get_random_x
def get_random_x(h, globalbins, cheights, globalbin_to_axisbin)
Definition: histsampling.py:74
python.histsampling.get_random_bin
def get_random_bin(globalbins, cheights)
Definition: histsampling.py:59
python.histsampling.get_random_xy
def get_random_xy(h2, globalbins, cheights, globalbin_to_axisbin)
Definition: histsampling.py:86
python.histsampling.load_hist
def load_hist(*args)
Definition: histsampling.py:13
python.CaloScaleNoiseConfig.type
type
Definition: CaloScaleNoiseConfig.py:78
python.histsampling.get_sampling_vars
def get_sampling_vars(h)
Definition: histsampling.py:33