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

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

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 984 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, ElectronMVAEstimatorRun2Phys14NonTrig._name, CutApplicatorBase._name, PhotonMVAEstimatorRun2Spring15NonTrig._name, PhotonMVAEstimatorRun2Phys14NonTrig._name, ElectronMVAEstimatorRun2Spring15Trig._name, ElectronMVAEstimatorRun2Spring15NonTrig._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(), ALCARECOTkAlBeamHalo_cff.filter, and ComparisonHelper.zip().

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

Member Data Documentation

plotting.AggregateBins._histoName
private

Definition at line 968 of file plotting.py.

Referenced by plotting.AggregateBins.create().

plotting.AggregateBins._ignoreMissingBins
private

Definition at line 973 of file plotting.py.

Referenced by plotting.AggregateBins.create().

plotting.AggregateBins._mapping
private

Definition at line 969 of file plotting.py.

Referenced by plotting.AggregateBins.create().

plotting.AggregateBins._minExistingBins
private

Definition at line 974 of file plotting.py.

Referenced by plotting.AggregateBins.create().

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

Definition at line 970 of file plotting.py.

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

plotting.AggregateBins._originalOrder
private

Definition at line 975 of file plotting.py.

Referenced by plotting.AggregateBins.create().

plotting.AggregateBins._renameBin
private

Definition at line 972 of file plotting.py.

Referenced by plotting.AggregateBins.create().

plotting.AggregateBins._reorder
private

Definition at line 976 of file plotting.py.

Referenced by plotting.AggregateBins.create().

plotting.AggregateBins._scale
private

Definition at line 971 of file plotting.py.

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