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

Constructor & Destructor Documentation

◆ __init__()

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

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

Member Function Documentation

◆ __str__()

def plotting.AggregateBins.__str__ (   self)

◆ create()

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

Definition at line 989 of file plotting.py.

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

References plotting._getOrCreateObject(), plotting.AggregateBins._histoName, plotting.AggregateBins._ignoreMissingBins, plotting.AggregateBins._mapping, plotting.AggregateBins._minExistingBins, FP420HitsObject._name, TrackerHitsObject._name, PGeometricDet::Item._name, TrackingRecHitAlgorithm._name, Logger._name, hcaldqm::DQModule._name, citk::IsolationConeDefinitionBase._name, DrellYanValidation._name, WValidation._name, hcaldqm::flag::Flag._name, hcaldqm::quantity::Quantity._name, HistoParams< T >._name, CutApplicatorBase._name, HistoParams< TH2F >._name, HistoParams< TProfile2D >._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, plotting.AggregateBins._scale, plotting._th1ToOrderedDict(), print(), FastTimerService_cff.range, and ComparisonHelper.zip().

Member Data Documentation

◆ _histoName

plotting.AggregateBins._histoName
private

Definition at line 973 of file plotting.py.

Referenced by plotting.AggregateBins.create().

◆ _ignoreMissingBins

plotting.AggregateBins._ignoreMissingBins
private

Definition at line 978 of file plotting.py.

Referenced by plotting.AggregateBins.create().

◆ _mapping

plotting.AggregateBins._mapping
private

Definition at line 974 of file plotting.py.

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

◆ _minExistingBins

plotting.AggregateBins._minExistingBins
private

Definition at line 979 of file plotting.py.

Referenced by plotting.AggregateBins.create().

◆ _name

plotting.AggregateBins._name
private

◆ _normalizeTo

plotting.AggregateBins._normalizeTo
private

Definition at line 975 of file plotting.py.

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

◆ _originalOrder

plotting.AggregateBins._originalOrder
private

Definition at line 980 of file plotting.py.

Referenced by plotting.AggregateBins.create().

◆ _renameBin

plotting.AggregateBins._renameBin
private

Definition at line 977 of file plotting.py.

Referenced by plotting.AggregateBins.create().

◆ _reorder

plotting.AggregateBins._reorder
private

Definition at line 981 of file plotting.py.

Referenced by plotting.AggregateBins.create().

◆ _scale

plotting.AggregateBins._scale
private

Definition at line 976 of file plotting.py.

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

FastTimerService_cff.range
range
Definition: FastTimerService_cff.py:34
beamerCreator.create
def create(alignables, pedeDump, additionalData, outputFile, config)
Definition: beamerCreator.py:44
print
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:46
Exception
TriggerAnalyzer.__str__
def __str__(self)
Definition: TriggerAnalyzer.py:103
ComparisonHelper::zip
OutputIterator zip(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp)
Definition: L1TStage2CaloLayer1.h:41
plotting._getOrCreateObject
def _getOrCreateObject(tdirectory, nameOrCreator)
Definition: plotting.py:58
format
plotting._th1ToOrderedDict
def _th1ToOrderedDict(th1, renameBin=None)
Definition: plotting.py:101
AlignmentPI::index
index
Definition: AlignmentPayloadInspectorHelper.h:46