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 945 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 947 of file plotting.py.

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

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 985 of file plotting.py.

References plotting._getOrCreateObject(), plotting.AggregateBins._histoName, plotting.AggregateBins._ignoreMissingBins, plotting.AggregateBins._mapping, plotting.AggregateBins._minExistingBins, FP420HitsObject._name, TrackerHitsObject._name, PGeometricDet::Item._name, TrackingRecHitAlgorithm._name, LikelihoodSpecies._name, LikelihoodPdfProduct._name, LikelihoodPdf._name, citk::IsolationConeDefinitionBase._name, Logger._name, hcaldqm::DQModule._name, DrellYanValidation._name, WValidation._name, HistoParams< T >._name, hcaldqm::flag::Flag._name, hcaldqm::quantity::Quantity._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().

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

Member Data Documentation

plotting.AggregateBins._histoName
private

Definition at line 969 of file plotting.py.

Referenced by plotting.AggregateBins.create().

plotting.AggregateBins._ignoreMissingBins
private

Definition at line 974 of file plotting.py.

Referenced by plotting.AggregateBins.create().

plotting.AggregateBins._mapping
private

Definition at line 970 of file plotting.py.

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

plotting.AggregateBins._minExistingBins
private

Definition at line 975 of file plotting.py.

Referenced by plotting.AggregateBins.create().

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

Definition at line 971 of file plotting.py.

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

plotting.AggregateBins._originalOrder
private

Definition at line 976 of file plotting.py.

Referenced by plotting.AggregateBins.create().

plotting.AggregateBins._renameBin
private

Definition at line 973 of file plotting.py.

Referenced by plotting.AggregateBins.create().

plotting.AggregateBins._reorder
private

Definition at line 977 of file plotting.py.

Referenced by plotting.AggregateBins.create().

plotting.AggregateBins._scale
private

Definition at line 972 of file plotting.py.

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