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
 
 _mapping
 
 _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 241 of file plotting.py.

Constructor & Destructor Documentation

def plotting.AggregateBins.__init__ (   self,
  name,
  histoName,
  mapping,
  normalizeTo = None,
  scale = None,
  renameBin = 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 243 of file plotting.py.

244  def __init__(self, name, histoName, mapping, normalizeTo=None, scale=None, renameBin=None):
245  """Constructor.
246 
247  Arguments:
248  name -- String for the name of the resulting histogram
249  histoName -- String for the name of the source histogram
250  mapping -- Dictionary or list for mapping the bins (see below)
251 
252  Keyword arguments:
253  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.
254  scale -- Optional number for scaling the histogram (passed to ROOT.TH1.Scale())
255  renameBin -- Optional function (string -> string) to rename the bins of the input histogram
256 
257  Mapping structure (mapping):
258 
259  Dictionary (you probably want to use collections.OrderedDict)
260  should be a mapping from the destination bin label to a list
261  of source bin labels ("dst -> [src]").
262 
263  If the mapping is a list, it should only contain the source
264  bin labels. In this case, the resulting histogram contains a
265  subset of the bins of the source histogram.
266  """
267  self._name = name
268  self._histoName = histoName
269  self._mapping = mapping
270  self._normalizeTo = normalizeTo
271  self._scale = scale
272  self._renameBin = renameBin

Member Function Documentation

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

Definition at line 273 of file plotting.py.

References FP420HitsObject._name, TrackerHitsObject._name, PGeometricDet::Item._name, LikelihoodSpecies._name, LikelihoodPdfProduct._name, LikelihoodPdf._name, citk::IsolationConeDefinitionBase._name, DrellYanValidation._name, WValidation._name, HistoParams< T >._name, CutApplicatorBase._name, ElectronMVAEstimatorRun2Phys14NonTrig._name, PhotonMVAEstimatorRun2Spring15NonTrig._name, PhotonMVAEstimatorRun2Phys14NonTrig._name, GeometricDetExtra._name, ElectronMVAEstimatorRun2Spring15Trig._name, ElectronMVAEstimatorRun2Spring15NonTrig._name, HistoParams< TH2F >._name, plotting.Subtract._name, HistoParams< TProfile2D >._name, plotting.FakeDuplicate._name, plotting.AggregateBins._name, Vispa.Views.PropertyView.Property._name, and SequenceTypes.SequencePlaceholder._name.

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

Definition at line 277 of file plotting.py.

References plotting._getOrCreateObject(), plotting.AggregateBins._histoName, plotting.AggregateBins._mapping, FP420HitsObject._name, TrackerHitsObject._name, PGeometricDet::Item._name, LikelihoodSpecies._name, LikelihoodPdfProduct._name, LikelihoodPdf._name, citk::IsolationConeDefinitionBase._name, DrellYanValidation._name, WValidation._name, HistoParams< T >._name, CutApplicatorBase._name, ElectronMVAEstimatorRun2Phys14NonTrig._name, PhotonMVAEstimatorRun2Spring15NonTrig._name, PhotonMVAEstimatorRun2Phys14NonTrig._name, GeometricDetExtra._name, ElectronMVAEstimatorRun2Spring15Trig._name, ElectronMVAEstimatorRun2Spring15NonTrig._name, HistoParams< TH2F >._name, plotting.Subtract._name, HistoParams< TProfile2D >._name, plotting.FakeDuplicate._name, plotting.AggregateBins._name, Vispa.Views.PropertyView.Property._name, SequenceTypes.SequencePlaceholder._name, plotting.AggregateBins._normalizeTo, plotting.AggregateBins._renameBin, plotting.AggregateBins._scale, and Vispa.Gui.VispaWidget.VispaWidget._scale.

278  def create(self, tdirectory):
279  """Create and return the histogram from a TDirectory"""
280  th1 = _getOrCreateObject(tdirectory, self._histoName)
281  if th1 is None:
282  return None
283 
284  result = ROOT.TH1F(self._name, self._name, len(self._mapping), 0, len(self._mapping))
285 
286  # TH1 can't really be used as a map/dict, so convert it here:
287  values = {}
288  for i in xrange(1, th1.GetNbinsX()+1):
289  binLabel = th1.GetXaxis().GetBinLabel(i)
290  if self._renameBin is not None:
291  binLabel = self._renameBin(binLabel)
292  values[binLabel] = (th1.GetBinContent(i), th1.GetBinError(i))
293 
294  if isinstance(self._mapping, list):
295  for i, label in enumerate(self._mapping):
296  try:
297  result.SetBinContent(i+1, values[label][0])
298  result.SetBinError(i+1, values[label][1])
299  except KeyError:
300  pass
301  result.GetXaxis().SetBinLabel(i+1, label)
302  else:
303  for i, (key, labels) in enumerate(self._mapping.iteritems()):
304  sumTime = 0
305  sumErrorSq = 0
306  for l in labels:
307  try:
308  sumTime += values[l][0]
309  sumErrorSq += values[l][1]**2
310  except KeyError:
311  pass
312  result.SetBinContent(i+1, sumTime)
313  result.SetBinError(i+1, math.sqrt(sumErrorSq))
314  result.GetXaxis().SetBinLabel(i+1, key)
315 
316  if self._normalizeTo is not None:
317  bin = th1.GetXaxis().FindBin(self._normalizeTo)
318  if bin <= 0:
319  print "Trying to normalize {name} to {binlabel}, which does not exist".format(name=self._name, binlabel=self._normalizeTo)
320  sys.exit(1)
321  value = th1.GetBinContent(bin)
322  if value != 0:
323  result.Scale(1/value)
324 
325  if self._scale is not None:
326  result.Scale(self._scale)
327 
328  return result
def _getOrCreateObject
Definition: plotting.py:16

Member Data Documentation

plotting.AggregateBins._histoName
private

Definition at line 267 of file plotting.py.

Referenced by plotting.AggregateBins.create().

plotting.AggregateBins._mapping
private

Definition at line 268 of file plotting.py.

Referenced by plotting.AggregateBins.create().

plotting.AggregateBins._name
private

Definition at line 266 of file plotting.py.

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

plotting.AggregateBins._normalizeTo
private

Definition at line 269 of file plotting.py.

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

plotting.AggregateBins._renameBin
private

Definition at line 271 of file plotting.py.

Referenced by plotting.AggregateBins.create().

plotting.AggregateBins._scale
private

Definition at line 270 of file plotting.py.

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