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

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

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 988 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, Logger._name, hcaldqm::DQModule._name, citk::IsolationConeDefinitionBase._name, WValidation._name, DrellYanValidation._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._TaskBasePlaceholder._name, plotting.AggregateBins._normalizeTo, plotting.AggregateBins._originalOrder, plotting.AggregateBins._renameBin, plotting.AggregateBins._reorder, plotting.AggregateBins._scale, plotting._th1ToOrderedDict(), mps_monitormerge.items, print(), FastTimerService_cff.range, and ComparisonHelper.zip().

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

Member Data Documentation

◆ _histoName

plotting.AggregateBins._histoName
private

Definition at line 972 of file plotting.py.

Referenced by plotting.AggregateBins.create().

◆ _ignoreMissingBins

plotting.AggregateBins._ignoreMissingBins
private

Definition at line 977 of file plotting.py.

Referenced by plotting.AggregateBins.create().

◆ _mapping

plotting.AggregateBins._mapping
private

Definition at line 973 of file plotting.py.

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

◆ _minExistingBins

plotting.AggregateBins._minExistingBins
private

Definition at line 978 of file plotting.py.

Referenced by plotting.AggregateBins.create().

◆ _name

plotting.AggregateBins._name
private

◆ _normalizeTo

plotting.AggregateBins._normalizeTo
private

Definition at line 974 of file plotting.py.

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

◆ _originalOrder

plotting.AggregateBins._originalOrder
private

Definition at line 979 of file plotting.py.

Referenced by plotting.AggregateBins.create().

◆ _renameBin

plotting.AggregateBins._renameBin
private

Definition at line 976 of file plotting.py.

Referenced by plotting.AggregateBins.create().

◆ _reorder

plotting.AggregateBins._reorder
private

Definition at line 980 of file plotting.py.

Referenced by plotting.AggregateBins.create().

◆ _scale

plotting.AggregateBins._scale
private

Definition at line 975 of file plotting.py.

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