This module implements python classes which inherit from and extend the functionality of the ROOT canvas classes.
Bases: rootpy.core.Object, rootpy.plotting.canvas.PadMixin, TCanvas
Bases: rootpy.core.Object, rootpy.plotting.canvas.PadMixin, TPad
This module contains base classes defining core funcionality
Bases: object
This is a mixin to provide additional attributes for plottable classes and to override ROOT TAttXXX and Draw methods.
mode may be ‘root’, ‘mpl’, or None to return the ROOT, matplotlib, or input value.
mode may be ‘root’, ‘mpl’, or None to return the ROOT, matplotlib, or input value.
mode may be ‘root’, ‘mpl’, or None to return the ROOT, matplotlib, or input value.
mode may be ‘root’, ‘mpl’, or None to return the ROOT, matplotlib, or input value.
mode may be ‘root’, ‘mpl’, or None to return the ROOT, matplotlib, or input value.
mode may be ‘root’, ‘mpl’, or None to return the ROOT, matplotlib, or input value.
color may be any color understood by ROOT or matplotlib.
Set all color attributes with one method call.
For full documentation of accepted color arguments, see rootpy.plotting.style.Color.
color may be any color understood by ROOT or matplotlib.
For full documentation of accepted color arguments, see rootpy.plotting.style.Color.
style may be any fill style understood by ROOT or matplotlib.
For full documentation of accepted style arguments, see rootpy.plotting.style.FillStyle.
color may be any color understood by ROOT or matplotlib.
For full documentation of accepted color arguments, see rootpy.plotting.style.Color.
style may be any line style understood by ROOT or matplotlib.
For full documentation of accepted style arguments, see rootpy.plotting.style.LineStyle.
color may be any color understood by ROOT or matplotlib.
For full documentation of accepted color arguments, see rootpy.plotting.style.Color.
style may be any marker style understood by ROOT or matplotlib.
For full documentation of accepted style arguments, see rootpy.plotting.style.MarkerStyle.
Bases: rootpy.plotting.core.Plottable, rootpy.core.NamelessConstructorObject, TGraphAsymmErrors
Remove points which lie outside of [x1, x2]. If x1 and/or x2 is below/above the current lowest/highest x-coordinates, additional points are added to the graph using a linear interpolation
Bases: rootpy.plotting.core.Plottable, rootpy.core.Object, TEfficiency
Bases: rootpy.plotting.hist._Hist
Returns a 1-dimensional Hist object which inherits from the associated ROOT.TH1* class (where * is C, S, I, F, or D depending on the type keyword argument)
Bases: rootpy.plotting.hist._Hist2D
Returns a 2-dimensional Hist object which inherits from the associated ROOT.TH1* class (where * is C, S, I, F, or D depending on the type keyword argument)
Bases: rootpy.plotting.hist._Hist3D
Returns a 3-dimensional Hist object which inherits from the associated ROOT.TH1* class (where * is C, S, I, F, or D depending on the type keyword argument)
Bases: rootpy.plotting.core.Plottable, rootpy.core.Object, THStack
will implement matplotlib.pyplot-like module here
Bases: rootpy.plotting.core.Plottable, TEllipse
Bases: rootpy.plotting.style._StyleContainer
Container for grouping together ROOT and matplotlib colors.
The color argument to the constructor can be a ROOT TColor or color index. If matplotlib is available, it can also accept an RGB or RGBA sequence, or a string in any of several forms:
- a letter from the set ‘rgbcmykw’
- a hex color string, like ‘#00FFFF’
- a standard name, like ‘aqua’
- a float, like ‘0.4’, indicating gray on a 0-1 scale
if color is RGBA, the A will simply be discarded.
Examples:
>>> color = Color(2)
>>> color()
2
>>> color('mpl')
(1.0, 0.0, 0.0)
>>> color = Color('blue')
>>> color('root')
4
>>> color('mpl')
(0.0, 0.0, 1.0)
>>> color = Color('0.25')
>>> color('mpl')
(0.25, 0.25, 0.25)
>>> color('root')
924
Bases: rootpy.plotting.style._StyleContainer
Container for grouping together ROOT and matplotlib fill styles.
The style argument to the constructor may be a ROOT fill style, a matplotlib fill style, or one of the following descriptions: ‘solid’ ‘hollow’
For an input value of ‘solid’, the matplotlib hatch value will be set to None, which is the same value as for ‘hollow’. The root2matplotlib functions will all check the ROOT value to see whether to make the fill solid or hollow.
Examples:
>>> style = FillStyle('hollow')
>>> style('root')
0
>>> print style('mpl')
None
Bases: rootpy.plotting.style._StyleContainer
Container for grouping together ROOT and matplotlib line styles.
The style argument to the constructor may be a ROOT line style, a matplotlib line style, or one of the following descriptions: ‘longdashdot’ ‘dashdot’ ‘dotted’ ‘longdashdotdotdot’ ‘solid’ ‘dashed’ ‘longdash’ ‘verylongdashdot’ ‘verylongdash’ ‘longdashdotdot’
Examples:
>>> style = LineStyle('verylongdashdot')
>>> style('root')
10
>>> style('mpl')
'dashdot'
Bases: rootpy.plotting.style._StyleContainer
Container for grouping together ROOT and matplotlib marker styles.
The style argument to the constructor may be a ROOT marker style, a matplotlib marker style, or one of the following descriptions: ‘square’ ‘triangle’ ‘largedot’ ‘opencircle’ ‘triangleup’ ‘opencross’ ‘opensquare’ ‘triangledown’ ‘cross’ ‘openstar’ ‘diamond’ ‘opendiamond’ ‘dot’ ‘mediumdot’ ‘opentriangle’ ‘fullstar’ ‘circle’ ‘star’ ‘smalldot’
Examples:
>>> style = MarkerStyle('opentriangle')
>>> style('root')
26
>>> style('mpl')
'^'
Convert color to a TColor if mode=’root’ or to (r,g,b) if ‘mpl’.
The color argument can be a ROOT TColor or color index, an RGB or RGBA sequence or a string in any of several forms:
- a letter from the set ‘rgbcmykw’
- a hex color string, like ‘#00FFFF’
- a standard name, like ‘aqua’
- a float, like ‘0.4’, indicating gray on a 0-1 scale
if arg is RGBA, the transparency value will be ignored.
Convert inputstyle to ROOT or matplotlib format.
Output format is determined by mode (‘root’ or ‘mpl’). The inputstyle may be a ROOT fill style, a matplotlib hatch style, ‘hollow’, or ‘solid’.
Convert inputstyle to ROOT or matplotlib format.
Output format is determined by mode (‘root’ or ‘mpl’). The inputstyle may be a ROOT line style, a matplotlib line style, or a description such as ‘solid’ or ‘dotted’.
Convert inputstyle to ROOT or matplotlib format.
Output format is determined by mode (‘root’ or ‘mpl’). The inputstyle may be a ROOT marker style, a matplotlib marker style, or a description such as ‘star’ or ‘square’.
These classes wrap Directories and perform automatic actions to Histograms retrieved from them. The different views can be composited and layered.
Summary of views:
One has a ROOT file with the following content:
zjets/mutau_mass
zz/mutau_mass
wz/mutau_mass
data_2010/mutau_mass
data_2011/mutau_mass
and wants to do the following:
This example can be tested by running:
python -m rootpy.plotting.views
>>> # Mock up the example test case
>>> import rootpy.io as io
>>> # We have to keep these, to make sure PyROOT doesn't garbage collect them
>>> keep = []
>>> zjets_dir = io.Directory('zjets', 'Zjets directory')
>>> zz_dir = io.Directory('zz', 'ZZ directory')
>>> wz_dir = io.Directory('wz', 'WZ directory')
>>> data2010_dir = io.Directory('data2010', 'data2010 directory')
>>> data2011_dir = io.Directory('data2011', 'data2011 directory')
>>> # Make the Zjets case
>>> _ = zjets_dir.cd()
>>> zjets_hist = ROOT.TH1F("mutau_mass", "Mu-Tau mass", 100, 0, 100)
>>> zjets_hist.FillRandom('gaus', 5000)
>>> keep.append(zjets_hist)
>>> # Make the ZZ case
>>> _ = zz_dir.cd()
>>> zz_hist = ROOT.TH1F("mutau_mass", "Mu-Tau mass", 100, 0, 100)
>>> zz_hist.FillRandom('gaus', 5000)
>>> keep.append(zz_hist)
>>> # Make the WZ case
>>> _ = wz_dir.cd()
>>> wz_hist = ROOT.TH1F("mutau_mass", "Mu-Tau mass", 100, 0, 100)
>>> wz_hist.FillRandom('gaus', 5000)
>>> keep.append(wz_hist)
>>> # Make the 2010 data case
>>> _ = data2010_dir.cd()
>>> data2010_hist = ROOT.TH1F("mutau_mass", "Mu-Tau mass", 100, 0, 100)
>>> data2010_hist.FillRandom('gaus', 30)
>>> keep.append(data2010_hist)
>>> # Make the 2011 data case
>>> _ = data2011_dir.cd()
>>> data2011_hist = ROOT.TH1F("mutau_mass", "Mu-Tau mass", 100, 0, 100)
>>> data2011_hist.FillRandom('gaus', 51)
>>> keep.append(data2011_hist)
We can merge the two data periods into a single case using a SumView.
>>> data = SumView(data2010_dir, data2011_dir)
>>> data_hist = data.Get("mutau_mass")
>>> data_hist.Integral()
81.0
>>> data_hist.Integral() == data2010_hist.Integral() + data2011_hist.Integral()
True
The simulated results (Z & diboson) can be scaled to the expected integrated luminosity using ScaleViews.
>>> zjets = ScaleView(zjets_dir, 0.01)
>>> zjets_hist = zjets.Get("mutau_mass")
>>> abs(zjets_hist.Integral() - 50.0) < 1e-5
True
>>> # Scale the diboson contribution
>>> zz = ScaleView(zz_dir, 0.001)
>>> wz = ScaleView(wz_dir, 0.003)
The dibosons individually are tiny, let’s put them together using a SumView. Note that this operation nests two ScaleViews into a SumView.
>>> dibosons = SumView(zz, wz)
>>> # We expect 5000*0.001 + 5000*0.003 = 20 events
>>> dibosons_hist = dibosons.Get("mutau_mass")
>>> abs(dibosons_hist.Integral() - 20) < 1e-4
True
A style view automatically applies a style to retrieved Plottable objects. The style is specified using the same arguments as the Plottable.decorate. Let’s make the Z background red and the diboson background blue.
>>> zjets = StyleView(zjets, fillcolor=ROOT.EColor.kRed)
>>> dibosons = StyleView(dibosons, fillcolor=ROOT.EColor.kBlue)
>>> zjets_hist = zjets.Get("mutau_mass")
>>> zjets_hist.GetFillColor() == ROOT.EColor.kRed
True
>>> dibosons_hist = dibosons.Get("mutau_mass")
>>> dibosons_hist.GetFillColor() == ROOT.EColor.kBlue
True
The StackView combines multiple items into a HistStack. In our example we stack the SM backgrounds to compare to the data.
>>> sm_bkg = StackView(zjets, dibosons)
>>> sm_bkg_stack = sm_bkg.Get("mutau_mass")
>>> '%0.0f' % sm_bkg_stack.Integral()
'70'
Looks like we have an excess of 11 events - must be the Higgs.
The normalization view renormalizes histograms to a given value (default 1.0). Here is an example of using the NormalizeView to compare the Z and diboson shapes.
>>> z_shape = NormalizeView(zjets)
>>> z_shape_hist = z_shape.Get("mutau_mass")
>>> abs(1 - z_shape_hist.Integral()) < 1e-5
True
>>> # Let's compare the shapes using a HistStack, using the "nostack" option.
>>> diboson_shape = NormalizeView(dibosons)
>>> shape_comparison = StackView(z_shape, diboson_shape)
>>> # To draw the comparison:
>>> # shape_comparison.Get("mutau_mass").Draw('nostack')
FunctorView allows you to apply an arbitrary transformation to the object. Here we show how you can change the axis range for all histograms in a directory.
>>> rebin = lambda x: x.Rebin(2)
>>> zjets_rebinned = FunctorView(zjets, rebin)
>>> zjets.Get("mutau_mass").GetNbinsX()
100
>>> zjets_rebinned.Get("mutau_mass").GetNbinsX()
50
The functor doesn’t have to return a histogram.
>>> mean_getter = lambda x: x.GetMean()
>>> mean = zjets.Get("mutau_mass").GetMean()
>>> zjets_mean = FunctorView(zjets, mean_getter)
>>> zjets_mean.Get("mutau_mass") == mean
True
MultiFunctorView is similar except that it operates on a group of histograms. The functor should take one argument, a generator of the sub-objects.
Here’s an example to get the integral of the biggest histogram in a set:
>>> biggest_histo = lambda objects: max(y.Integral() for y in objects)
>>> biggest = MultiFunctorView(biggest_histo, zjets, dibosons)
>>> biggest.Get("mutau_mass") == zjets.Get("mutau_mass").Integral()
True
If you’d like to “cd” into a lower subdirectory, while still maintaining the same view, use a SubdirectoryView.
>>> basedir = io.Directory('base', 'base directory')
>>> _ = basedir.cd()
>>> subdir1 = io.Directory('subdir1', 'subdir directory in 1')
>>> _ = subdir1.cd()
>>> hist = ROOT.TH1F("mutau_mass", "Mu-Tau mass", 100, 0, 100)
>>> hist.FillRandom('gaus', 2000)
>>> keep.append(hist)
>>> _ = basedir.cd()
>>> subdir2 = io.Directory('subdir2', 'subdir directory 2')
>>> _ = subdir2.cd()
>>> hist = ROOT.TH1F("mutau_mass", "Mu-Tau mass", 100, 0, 100)
>>> hist.FillRandom('gaus', 5000)
>>> keep.append(hist)
Subdirectory views work on top of other views.
>>> baseview = ScaleView(basedir, 0.1)
>>> subdir1view = SubdirectoryView(baseview, 'subdir1')
>>> subdir2view = SubdirectoryView(baseview, 'subdir2')
>>> histo1 = subdir1view.Get('mutau_mass')
>>> histo2 = subdir2view.Get('mutau_mass')
>>> exp_histo1 = baseview.Get("subdir1/mutau_mass")
>>> exp_histo2 = baseview.Get("subdir2/mutau_mass")
>>> def equivalent(h1, h2):
... return (abs(h1.GetMean() - h2.GetMean()) < 1e-4 and
... abs(h1.GetRMS() - h2.GetRMS()) < 1e-4 and
... abs(h1.Integral() - h2.Integral()) < 1e-4)
>>> equivalent(exp_histo1, histo1)
True
>>> equivalent(exp_histo2, histo2)
True
>>> equivalent(histo1, histo2)
False
Bases: rootpy.plotting.views._FolderView
Apply an arbitrary function to the output histogram.
The histogram is always cloned before it is passed to the function.
Bases: rootpy.plotting.views._MultiFolderView
Apply an arbitrary function to the output histograms.
The function must take one argument, a generator of objects.
Bases: rootpy.plotting.views.ScaleView
Normalize histograms to a constant value
Bases: rootpy.plotting.views._FolderView
Does some magic to the path
User should supply a functor which transforms the path argument passed to Get(...)
Bases: rootpy.plotting.views._FolderView
View of a folder which applies a scaling factor to histograms.
Bases: rootpy.plotting.views._MultiFolderView
Build a HistStack from the input histograms
The default draw option that histograms will use is “hist”.
One can override this for all histograms by passing a string. Individual behavior can be controlled by passing a list of draw options, corresponding to the input directories. In this case the option for all histograms must be specified.
The name and title of the HistStack is taken from the first histogram in the list.
Normally the histograms will be added to the stack in the order of the constructor. Optionally, one can add them in order of ascending integral by passing the kwarg sorted=True.
Bases: rootpy.plotting.views._FolderView
View of a folder which applies a style to Plottable objects.
The kwargs are passed to Plottable.decorate
Bases: rootpy.plotting.views.PathModifierView
Add some base directories to the path of Get()
<subdir> is the directory you want to ‘cd’ too.