test
CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
List of all members | Public Member Functions | Private Attributes
plotting.AggregateBins Class Reference

Public Member Functions

def __init__
 
def __str__
 
def create
 

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

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

Member Function Documentation

def plotting.AggregateBins.__str__ (   self)
String representation, returns the name

Definition at line 801 of file plotting.py.

References 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, and plotting.AggregateBins._name.

802  def __str__(self):
803  """String representation, returns the name"""
804  return self._name
def plotting.AggregateBins.create (   self,
  tdirectory 
)
Create and return the histogram from a TDirectory

Definition at line 805 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, ElectronMVAEstimatorRun2Spring15Trig._name, GeometricDetExtra._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(), alcazmumu_cfi.filter, cmsHarvester.index, and ComparisonHelper.zip().

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

Member Data Documentation

plotting.AggregateBins._histoName
private

Definition at line 792 of file plotting.py.

Referenced by plotting.AggregateBins.create().

plotting.AggregateBins._ignoreMissingBins
private

Definition at line 797 of file plotting.py.

Referenced by plotting.AggregateBins.create().

plotting.AggregateBins._mapping
private

Definition at line 793 of file plotting.py.

Referenced by plotting.AggregateBins.create().

plotting.AggregateBins._minExistingBins
private

Definition at line 798 of file plotting.py.

Referenced by plotting.AggregateBins.create().

plotting.AggregateBins._name
private

Definition at line 791 of file plotting.py.

Referenced by plotting.AggregateBins.__str__(), plotting.AggregateHistos.__str__(), plotting.ROC.__str__(), trackingPlots.TimePerTrackPlot.__str__(), plotting.PlotGroup._drawSeparate(), plotting.PlotGroup._save(), plotting.AggregateBins.create(), plotting.AggregateHistos.create(), trackingPlots.TimePerTrackPlot.create(), plotting.Plot.create(), plotting.PlotGroup.draw(), plotting.Plot.getName(), plotting.PlotGroup.getName(), plotting.PlotterFolder.getName(), plotting.PlotterItem.getName(), plotting.PlotterFolder.getSelectionNameIterator(), validation.SimpleSample.name(), trackingPlots.Iteration.name(), plotting.PlotterItem.readDirs(), and plotting.PlotGroup.remove().

plotting.AggregateBins._normalizeTo
private

Definition at line 794 of file plotting.py.

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

plotting.AggregateBins._originalOrder
private

Definition at line 799 of file plotting.py.

Referenced by plotting.AggregateBins.create().

plotting.AggregateBins._renameBin
private

Definition at line 796 of file plotting.py.

Referenced by plotting.AggregateBins.create().

plotting.AggregateBins._scale
private

Definition at line 795 of file plotting.py.

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