CMS 3D CMS Logo

List of all members | Public Member Functions | Private Attributes
plotting.AggregateBins Class Reference

Public Member Functions

def __init__ (self, name, histoName, mapping, normalizeTo=None, scale=None, renameBin=None, ignoreMissingBins=False, minExistingBins=None, originalOrder=False, reorder=None)
 
def __str__ (self)
 
def create (self, tdirectory)
 

Private Attributes

 _histoName
 
 _ignoreMissingBins
 
 _mapping
 
 _minExistingBins
 
 _name
 
 _normalizeTo
 
 _originalOrder
 
 _renameBin
 
 _reorder
 
 _scale
 

Detailed Description

Class to create a histogram by aggregating bins of another histogram to a bin of the resulting histogram.

Definition at line 947 of file plotting.py.

Constructor & Destructor Documentation

def plotting.AggregateBins.__init__ (   self,
  name,
  histoName,
  mapping,
  normalizeTo = None,
  scale = None,
  renameBin = None,
  ignoreMissingBins = False,
  minExistingBins = None,
  originalOrder = False,
  reorder = None 
)
Constructor.

Arguments:
name      -- String for the name of the resulting histogram
histoName -- String for the name of the source histogram
mapping   -- Dictionary for mapping the bins (see below)

Keyword arguments:
normalizeTo -- Optional string of a bin label in the source histogram. If given, all bins of the resulting histogram are divided by the value of this bin.
scale       -- Optional number for scaling the histogram (passed to ROOT.TH1.Scale())
renameBin   -- Optional function (string -> string) to rename the bins of the input histogram
originalOrder -- Boolean for using the order of bins in the histogram (default False)
reorder     -- Optional function to reorder the bins

Mapping structure (mapping):

Dictionary (you probably want to use collections.OrderedDict)
should be a mapping from the destination bin label to a list
of source bin labels ("dst -> [src]").

Definition at line 949 of file plotting.py.

949  def __init__(self, name, histoName, mapping, normalizeTo=None, scale=None, renameBin=None, ignoreMissingBins=False, minExistingBins=None, originalOrder=False, reorder=None):
950  """Constructor.
951 
952  Arguments:
953  name -- String for the name of the resulting histogram
954  histoName -- String for the name of the source histogram
955  mapping -- Dictionary for mapping the bins (see below)
956 
957  Keyword arguments:
958  normalizeTo -- Optional string of a bin label in the source histogram. If given, all bins of the resulting histogram are divided by the value of this bin.
959  scale -- Optional number for scaling the histogram (passed to ROOT.TH1.Scale())
960  renameBin -- Optional function (string -> string) to rename the bins of the input histogram
961  originalOrder -- Boolean for using the order of bins in the histogram (default False)
962  reorder -- Optional function to reorder the bins
963 
964  Mapping structure (mapping):
965 
966  Dictionary (you probably want to use collections.OrderedDict)
967  should be a mapping from the destination bin label to a list
968  of source bin labels ("dst -> [src]").
969  """
970  self._name = name
971  self._histoName = histoName
972  self._mapping = mapping
973  self._normalizeTo = normalizeTo
974  self._scale = scale
975  self._renameBin = renameBin
976  self._ignoreMissingBins = ignoreMissingBins
977  self._minExistingBins = minExistingBins
978  self._originalOrder = originalOrder
979  self._reorder = reorder
980  if self._originalOrder and self._reorder is not None:
981  raise Exception("reorder is not None and originalOrder is True, please set only one of them")
982 
def __init__(self, name, histoName, mapping, normalizeTo=None, scale=None, renameBin=None, ignoreMissingBins=False, minExistingBins=None, originalOrder=False, reorder=None)
Definition: plotting.py:949

Member Function Documentation

def plotting.AggregateBins.__str__ (   self)
def plotting.AggregateBins.create (   self,
  tdirectory 
)
Create and return the histogram from a TDirectory

Definition at line 987 of file plotting.py.

References plotting._getOrCreateObject(), plotting.AggregateBins._histoName, plotting.AggregateBins._ignoreMissingBins, plotting.AggregateBins._mapping, plotting.AggregateBins._minExistingBins, TrackerHitsObject._name, FP420HitsObject._name, PGeometricDet::Item._name, TrackingRecHitAlgorithm._name, LikelihoodSpecies._name, LikelihoodPdfProduct._name, Logger._name, hcaldqm::DQModule._name, LikelihoodPdf._name, citk::IsolationConeDefinitionBase._name, DrellYanValidation._name, WValidation._name, hcaldqm::flag::Flag._name, hcaldqm::quantity::Quantity._name, HistoParams< T >._name, GeometricTimingDetExtra._name, CutApplicatorBase._name, GeometricDetExtra._name, HistoParams< TH2F >._name, HistoParams< TProfile2D >._name, Vispa.Views.PropertyView.Property._name, SequenceTypes.SequencePlaceholder._name, plotting.Subtract._name, plotting.Transform._name, plotting.FakeDuplicate._name, plotting.CutEfficiency._name, plotting.AggregateBins._name, SequenceTypes.TaskPlaceholder._name, plotting.AggregateBins._normalizeTo, plotting.AggregateBins._originalOrder, plotting.AggregateBins._renameBin, plotting.AggregateBins._reorder, Vispa.Gui.VispaWidget.VispaWidget._scale, plotting.AggregateBins._scale, plotting._th1ToOrderedDict(), edm.print(), and ComparisonHelper.zip().

987  def create(self, tdirectory):
988  """Create and return the histogram from a TDirectory"""
989  th1 = _getOrCreateObject(tdirectory, self._histoName)
990  if th1 is None:
991  return None
992 
993  binLabels = [""]*len(self._mapping)
994  binValues = [None]*len(self._mapping)
995 
996  # TH1 can't really be used as a map/dict, so convert it here:
997  values = _th1ToOrderedDict(th1, self._renameBin)
998 
999  binIndexOrder = [] # for reordering bins if self._originalOrder is True
1000  for i, (key, labels) in enumerate(six.iteritems(self._mapping)):
1001  sumTime = 0.
1002  sumErrorSq = 0.
1003  nsum = 0
1004  for l in labels:
1005  try:
1006  sumTime += values[l][0]
1007  sumErrorSq += values[l][1]**2
1008  nsum += 1
1009  except KeyError:
1010  pass
1011 
1012  if nsum > 0:
1013  binValues[i] = (sumTime, math.sqrt(sumErrorSq))
1014  binLabels[i] = key
1015 
1016  ivalue = len(values)+1
1017  if len(labels) > 0:
1018  # first label doesn't necessarily exist (especially for
1019  # the iteration timing plots), so let's test them all
1020  for lab in labels:
1021  if lab in values:
1022  ivalue = values.keys().index(lab)
1023  break
1024  binIndexOrder.append( (ivalue, i) )
1025 
1026  if self._originalOrder:
1027  binIndexOrder.sort(key=lambda t: t[0])
1028  tmpVal = []
1029  tmpLab = []
1030  for i in range(0, len(binValues)):
1031  fromIndex = binIndexOrder[i][1]
1032  tmpVal.append(binValues[fromIndex])
1033  tmpLab.append(binLabels[fromIndex])
1034  binValues = tmpVal
1035  binLabels = tmpLab
1036  if self._reorder is not None:
1037  order = self._reorder(tdirectory, binLabels)
1038  binValues = [binValues[i] for i in order]
1039  binLabels = [binLabels[i] for i in order]
1040 
1041  if self._minExistingBins is not None and (len(binValues)-binValues.count(None)) < self._minExistingBins:
1042  return None
1043 
1044  if self._ignoreMissingBins:
1045  for i, val in enumerate(binValues):
1046  if val is None:
1047  binLabels[i] = None
1048  binValues = [v for v in binValues if v is not None]
1049  binLabels = [v for v in binLabels if v is not None]
1050  if len(binValues) == 0:
1051  return None
1052 
1053  result = ROOT.TH1F(self._name, self._name, len(binValues), 0, len(binValues))
1054  for i, (value, label) in enumerate(zip(binValues, binLabels)):
1055  if value is not None:
1056  result.SetBinContent(i+1, value[0])
1057  result.SetBinError(i+1, value[1])
1058  result.GetXaxis().SetBinLabel(i+1, label)
1059 
1060  if self._normalizeTo is not None:
1061  bin = th1.GetXaxis().FindBin(self._normalizeTo)
1062  if bin <= 0:
1063  print("Trying to normalize {name} to {binlabel}, which does not exist".format(name=self._name, binlabel=self._normalizeTo))
1064  sys.exit(1)
1065  value = th1.GetBinContent(bin)
1066  if value != 0:
1067  result.Scale(1/value)
1068 
1069  if self._scale is not None:
1070  result.Scale(self._scale)
1071 
1072  return result
1073 
def _getOrCreateObject(tdirectory, nameOrCreator)
Definition: plotting.py:58
S & print(S &os, JobReport::InputFile const &f)
Definition: JobReport.cc:66
OutputIterator zip(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp)
def create(self, tdirectory)
Definition: plotting.py:987
def _th1ToOrderedDict(th1, renameBin=None)
Definition: plotting.py:101

Member Data Documentation

plotting.AggregateBins._histoName
private

Definition at line 971 of file plotting.py.

Referenced by plotting.AggregateBins.create().

plotting.AggregateBins._ignoreMissingBins
private

Definition at line 976 of file plotting.py.

Referenced by plotting.AggregateBins.create().

plotting.AggregateBins._mapping
private

Definition at line 972 of file plotting.py.

Referenced by plotting.AggregateBins.create(), and plotting.AggregateHistos.create().

plotting.AggregateBins._minExistingBins
private

Definition at line 977 of file plotting.py.

Referenced by plotting.AggregateBins.create().

plotting.AggregateBins._name
private
plotting.AggregateBins._normalizeTo
private

Definition at line 973 of file plotting.py.

Referenced by plotting.AggregateBins.create(), and plotting.AggregateHistos.create().

plotting.AggregateBins._originalOrder
private

Definition at line 978 of file plotting.py.

Referenced by plotting.AggregateBins.create().

plotting.AggregateBins._renameBin
private

Definition at line 975 of file plotting.py.

Referenced by plotting.AggregateBins.create().

plotting.AggregateBins._reorder
private

Definition at line 979 of file plotting.py.

Referenced by plotting.AggregateBins.create().

plotting.AggregateBins._scale
private

Definition at line 974 of file plotting.py.

Referenced by plotting.AggregateBins.create(), and plotting.Plot.create().