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

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

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 815 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().

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

Member Data Documentation

plotting.AggregateBins._histoName
private

Definition at line 802 of file plotting.py.

Referenced by plotting.AggregateBins.create().

plotting.AggregateBins._ignoreMissingBins
private

Definition at line 807 of file plotting.py.

Referenced by plotting.AggregateBins.create().

plotting.AggregateBins._mapping
private

Definition at line 803 of file plotting.py.

Referenced by plotting.AggregateBins.create().

plotting.AggregateBins._minExistingBins
private

Definition at line 808 of file plotting.py.

Referenced by plotting.AggregateBins.create().

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

Definition at line 804 of file plotting.py.

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

plotting.AggregateBins._originalOrder
private

Definition at line 809 of file plotting.py.

Referenced by plotting.AggregateBins.create().

plotting.AggregateBins._renameBin
private

Definition at line 806 of file plotting.py.

Referenced by plotting.AggregateBins.create().

plotting.AggregateBins._scale
private

Definition at line 805 of file plotting.py.

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