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)
 
def __str__ (self)
 
def create (self, tdirectory)
 

Private Attributes

 _histoName
 
 _ignoreMissingBins
 
 _mapping
 
 _minExistingBins
 
 _name
 
 _normalizeTo
 
 _originalOrder
 
 _renameBin
 
 _scale
 

Detailed Description

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

Definition at line 787 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 
)
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)

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

789  def __init__(self, name, histoName, mapping, normalizeTo=None, scale=None, renameBin=None, ignoreMissingBins=False, minExistingBins=None, originalOrder=False):
790  """Constructor.
791 
792  Arguments:
793  name -- String for the name of the resulting histogram
794  histoName -- String for the name of the source histogram
795  mapping -- Dictionary for mapping the bins (see below)
796 
797  Keyword arguments:
798  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.
799  scale -- Optional number for scaling the histogram (passed to ROOT.TH1.Scale())
800  renameBin -- Optional function (string -> string) to rename the bins of the input histogram
801  originalOrder -- Boolean for using the order of bins in the histogram (default False)
802 
803  Mapping structure (mapping):
804 
805  Dictionary (you probably want to use collections.OrderedDict)
806  should be a mapping from the destination bin label to a list
807  of source bin labels ("dst -> [src]").
808  """
809  self._name = name
810  self._histoName = histoName
811  self._mapping = mapping
812  self._normalizeTo = normalizeTo
813  self._scale = scale
814  self._renameBin = renameBin
815  self._ignoreMissingBins = ignoreMissingBins
816  self._minExistingBins = minExistingBins
817  self._originalOrder = originalOrder
818 
def __init__(self, name, histoName, mapping, normalizeTo=None, scale=None, renameBin=None, ignoreMissingBins=False, minExistingBins=None, originalOrder=False)
Definition: plotting.py:789

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 823 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, L1TMuon::PtAssignmentUnit._name, L1TMuon::PtRefinementUnit._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, CutApplicatorBase._name, ElectronMVAEstimatorRun2Phys14NonTrig._name, PhotonMVAEstimatorRun2Spring15NonTrig._name, PhotonMVAEstimatorRun2Phys14NonTrig._name, GeometricDetExtra._name, ElectronMVAEstimatorRun2Spring15Trig._name, ElectronMVAEstimatorRun2Spring15NonTrig._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, plotting.AggregateBins._normalizeTo, plotting.AggregateBins._originalOrder, plotting.AggregateBins._renameBin, Vispa.Gui.VispaWidget.VispaWidget._scale, plotting.AggregateBins._scale, plotting._th1ToOrderedDict(), ALCARECOTkAlBeamHalo_cff.filter, diffTreeTool.index, and ComparisonHelper.zip().

823  def create(self, tdirectory):
824  """Create and return the histogram from a TDirectory"""
825  th1 = _getOrCreateObject(tdirectory, self._histoName)
826  if th1 is None:
827  return None
828 
829  binLabels = [""]*len(self._mapping)
830  binValues = [None]*len(self._mapping)
831 
832  # TH1 can't really be used as a map/dict, so convert it here:
833  values = _th1ToOrderedDict(th1, self._renameBin)
834 
835  binIndexOrder = [] # for reordering bins if self._originalOrder is True
836  for i, (key, labels) in enumerate(self._mapping.iteritems()):
837  sumTime = 0.
838  sumErrorSq = 0.
839  nsum = 0
840  for l in labels:
841  try:
842  sumTime += values[l][0]
843  sumErrorSq += values[l][1]**2
844  nsum += 1
845  except KeyError:
846  pass
847 
848  if nsum > 0:
849  binValues[i] = (sumTime, math.sqrt(sumErrorSq))
850  binLabels[i] = key
851 
852  ivalue = len(values)+1
853  if len(labels) > 0:
854  # first label doesn't necessarily exist (especially for
855  # the iteration timing plots), so let's test them all
856  for lab in labels:
857  if lab in values:
858  ivalue = values.keys().index(lab)
859  break
860  binIndexOrder.append( (ivalue, i) )
861 
862  if self._originalOrder:
863  binIndexOrder.sort(key=lambda t: t[0])
864  tmpVal = []
865  tmpLab = []
866  for i in xrange(0, len(binValues)):
867  fromIndex = binIndexOrder[i][1]
868  tmpVal.append(binValues[fromIndex])
869  tmpLab.append(binLabels[fromIndex])
870  binValues = tmpVal
871  binLabels = tmpLab
872 
873  if self._minExistingBins is not None and (len(binValues)-binValues.count(None)) < self._minExistingBins:
874  return None
875 
876  if self._ignoreMissingBins:
877  for i, val in enumerate(binValues):
878  if val is None:
879  binLabels[i] = None
880  binValues = filter(lambda v: v is not None, binValues)
881  binLabels = filter(lambda v: v is not None, binLabels)
882  if len(binValues) == 0:
883  return None
884 
885  result = ROOT.TH1F(self._name, self._name, len(binValues), 0, len(binValues))
886  for i, (value, label) in enumerate(zip(binValues, binLabels)):
887  if value is not None:
888  result.SetBinContent(i+1, value[0])
889  result.SetBinError(i+1, value[1])
890  result.GetXaxis().SetBinLabel(i+1, label)
891 
892  if self._normalizeTo is not None:
893  bin = th1.GetXaxis().FindBin(self._normalizeTo)
894  if bin <= 0:
895  print "Trying to normalize {name} to {binlabel}, which does not exist".format(name=self._name, binlabel=self._normalizeTo)
896  sys.exit(1)
897  value = th1.GetBinContent(bin)
898  if value != 0:
899  result.Scale(1/value)
900 
901  if self._scale is not None:
902  result.Scale(self._scale)
903 
904  return result
905 
def _getOrCreateObject(tdirectory, nameOrCreator)
Definition: plotting.py:53
OutputIterator zip(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp)
def create(self, tdirectory)
Definition: plotting.py:823
def _th1ToOrderedDict(th1, renameBin=None)
Definition: plotting.py:96

Member Data Documentation

plotting.AggregateBins._histoName
private

Definition at line 810 of file plotting.py.

Referenced by plotting.AggregateBins.create().

plotting.AggregateBins._ignoreMissingBins
private

Definition at line 815 of file plotting.py.

Referenced by plotting.AggregateBins.create().

plotting.AggregateBins._mapping
private

Definition at line 811 of file plotting.py.

Referenced by plotting.AggregateBins.create().

plotting.AggregateBins._minExistingBins
private

Definition at line 816 of file plotting.py.

Referenced by plotting.AggregateBins.create().

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

Definition at line 812 of file plotting.py.

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

plotting.AggregateBins._originalOrder
private

Definition at line 817 of file plotting.py.

Referenced by plotting.AggregateBins.create().

plotting.AggregateBins._renameBin
private

Definition at line 814 of file plotting.py.

Referenced by plotting.AggregateBins.create().

plotting.AggregateBins._scale
private

Definition at line 813 of file plotting.py.

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