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
 
 _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 332 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 
)
Constructor.

Arguments:
name      -- String for the name of the resulting histogram
histoName -- String for the name of the source histogram
mapping   -- Dictionary or list 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

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]").

If the mapping is a list, it should only contain the source
bin labels. In this case, the resulting histogram contains a
subset of the bins of the source histogram.

Definition at line 334 of file plotting.py.

335  def __init__(self, name, histoName, mapping, normalizeTo=None, scale=None, renameBin=None, ignoreMissingBins=False, minExistingBins=None):
336  """Constructor.
337 
338  Arguments:
339  name -- String for the name of the resulting histogram
340  histoName -- String for the name of the source histogram
341  mapping -- Dictionary or list for mapping the bins (see below)
342 
343  Keyword arguments:
344  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.
345  scale -- Optional number for scaling the histogram (passed to ROOT.TH1.Scale())
346  renameBin -- Optional function (string -> string) to rename the bins of the input histogram
347 
348  Mapping structure (mapping):
349 
350  Dictionary (you probably want to use collections.OrderedDict)
351  should be a mapping from the destination bin label to a list
352  of source bin labels ("dst -> [src]").
353 
354  If the mapping is a list, it should only contain the source
355  bin labels. In this case, the resulting histogram contains a
356  subset of the bins of the source histogram.
357  """
358  self._name = name
359  self._histoName = histoName
360  self._mapping = mapping
361  self._normalizeTo = normalizeTo
362  self._scale = scale
363  self._renameBin = renameBin
364  self._ignoreMissingBins = ignoreMissingBins
365  self._minExistingBins = minExistingBins

Member Function Documentation

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

Definition at line 366 of file plotting.py.

References FP420HitsObject._name, TrackerHitsObject._name, PGeometricDet::Item._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, plotting.Subtract._name, plotting.FakeDuplicate._name, plotting.AggregateBins._name, Vispa.Views.PropertyView.Property._name, and SequenceTypes.SequencePlaceholder._name.

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

Definition at line 370 of file plotting.py.

References plotting._getOrCreateObject(), plotting.AggregateBins._histoName, plotting.AggregateBins._ignoreMissingBins, plotting.AggregateBins._mapping, plotting.AggregateBins._minExistingBins, TrackerHitsObject._name, FP420HitsObject._name, PGeometricDet::Item._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, plotting.Subtract._name, plotting.FakeDuplicate._name, plotting.AggregateBins._name, Vispa.Views.PropertyView.Property._name, SequenceTypes.SequencePlaceholder._name, plotting.AggregateBins._normalizeTo, plotting.AggregateBins._renameBin, plotting.AggregateBins._scale, Vispa.Gui.VispaWidget.VispaWidget._scale, alcazmumu_cfi.filter, and ComparisonHelper.zip().

371  def create(self, tdirectory):
372  """Create and return the histogram from a TDirectory"""
373  th1 = _getOrCreateObject(tdirectory, self._histoName)
374  if th1 is None:
375  return None
376 
377  binLabels = [""]*len(self._mapping)
378  binValues = [None]*len(self._mapping)
379 
380  # TH1 can't really be used as a map/dict, so convert it here:
381  values = {}
382  for i in xrange(1, th1.GetNbinsX()+1):
383  binLabel = th1.GetXaxis().GetBinLabel(i)
384  if self._renameBin is not None:
385  binLabel = self._renameBin(binLabel)
386  values[binLabel] = (th1.GetBinContent(i), th1.GetBinError(i))
387 
388  if isinstance(self._mapping, list):
389  for i, label in enumerate(self._mapping):
390  try:
391  binValues[i] = values[label]
392  except KeyError:
393  pass
394  binLabels[i] = label
395  else:
396  for i, (key, labels) in enumerate(self._mapping.iteritems()):
397  sumTime = 0.
398  sumErrorSq = 0.
399  nsum = 0
400  for l in labels:
401  try:
402  sumTime += values[l][0]
403  sumErrorSq += values[l][1]**2
404  nsum += 1
405  except KeyError:
406  pass
407 
408  if nsum > 0:
409  binValues[i] = (sumTime, math.sqrt(sumErrorSq))
410  binLabels[i] = key
411 
412  if self._minExistingBins is not None and (len(binValues)-binValues.count(None)) < self._minExistingBins:
413  return None
414 
415  if self._ignoreMissingBins:
416  for i, val in enumerate(binValues):
417  if val is None:
418  binLabels[i] = None
419  binValues = filter(lambda v: v is not None, binValues)
420  binLabels = filter(lambda v: v is not None, binLabels)
421  if len(binValues) == 0:
422  return None
423 
424  result = ROOT.TH1F(self._name, self._name, len(binValues), 0, len(binValues))
425  for i, (value, label) in enumerate(zip(binValues, binLabels)):
426  if value is not None:
427  result.SetBinContent(i+1, value[0])
428  result.SetBinError(i+1, value[1])
429  result.GetXaxis().SetBinLabel(i+1, label)
430 
431  if self._normalizeTo is not None:
432  bin = th1.GetXaxis().FindBin(self._normalizeTo)
433  if bin <= 0:
434  print "Trying to normalize {name} to {binlabel}, which does not exist".format(name=self._name, binlabel=self._normalizeTo)
435  sys.exit(1)
436  value = th1.GetBinContent(bin)
437  if value != 0:
438  result.Scale(1/value)
439 
440  if self._scale is not None:
441  result.Scale(self._scale)
442 
443  return result
OutputIterator zip(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp)
def _getOrCreateObject
Definition: plotting.py:22

Member Data Documentation

plotting.AggregateBins._histoName
private

Definition at line 358 of file plotting.py.

Referenced by plotting.AggregateBins.create().

plotting.AggregateBins._ignoreMissingBins
private

Definition at line 363 of file plotting.py.

Referenced by plotting.AggregateBins.create().

plotting.AggregateBins._mapping
private

Definition at line 359 of file plotting.py.

Referenced by plotting.AggregateBins.create().

plotting.AggregateBins._minExistingBins
private

Definition at line 364 of file plotting.py.

Referenced by plotting.AggregateBins.create().

plotting.AggregateBins._name
private

Definition at line 357 of file plotting.py.

Referenced by plotting.AggregateBins.__str__(), plotting.AggregateHistos.__str__(), plotting.ROC.__str__(), plotting.PlotGroup._drawSeparate(), plotting.PlotGroup._save(), plotting.AggregateBins.create(), plotting.AggregateHistos.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 360 of file plotting.py.

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

plotting.AggregateBins._renameBin
private

Definition at line 362 of file plotting.py.

Referenced by plotting.AggregateBins.create().

plotting.AggregateBins._scale
private

Definition at line 361 of file plotting.py.

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