Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion bin/plot
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ labels = []
plot_options = {}
global_options = ['files', 'file-aliases', 'histograms', 'labels', 'plot_type', 'output_folder',
'output_format', 'command', 'data_index', 'normalise',
'show_ratio', 'show_stat_errors_on_mc', 'colours', 'name_prefix']
'show_ratio', 'show_stat_errors_on_mc', 'colours', 'name_prefix',
'fill_area', 'alpha']

def main():
options, input_values_sets, json_input_files = parse_options()
Expand Down
53 changes: 53 additions & 0 deletions config/plots/MET_factorisation_scale_comparison_8TeV.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"command": "compare-files",
"files": [
"/hdfs/TopQuarkGroup/results/histogramfiles/AN-14-071_6th_draft/8TeV/unfolding/unfolding_TTJets_8TeV_asymmetric.root",
"/hdfs/TopQuarkGroup/results/histogramfiles/AN-14-071_6th_draft/8TeV/unfolding/unfolding_TTJets_8TeV_scaledown_asymmetric.root",
"/hdfs/TopQuarkGroup/results/histogramfiles/AN-14-071_6th_draft/8TeV/unfolding/unfolding_TTJets_8TeV_scaleup_asymmetric.root"
],
"file-aliases": [
"TTJets_central",
"TTJets_scaledown",
"TTJets_scaleup"
],
"histograms": [
"unfolding_MET_analyser_electron_channel_patType1CorrectedPFMet/truth",
"unfolding_MET_analyser_electron_channel_patType1CorrectedPFMet/measured",
"unfolding_MET_analyser_electron_channel_patType1CorrectedPFMet/fake",
"unfolding_MET_analyser_muon_channel_patType1CorrectedPFMet/truth",
"unfolding_MET_analyser_muon_channel_patType1CorrectedPFMet/measured",
"unfolding_MET_analyser_muon_channel_patType1CorrectedPFMet/fake"
],
"labels": [
"central",
"scale down",
"scale up"
],
"output_folder": "plots/8TeV/TTJet_comparison",
"output_format": ["pdf"],
"name_prefix": "comparison_scale_",
"plot_type": "shape_comparison",
"ratio_y_limits": [
[0.8, 1.2]
],
"title": [
"Comparison of TTJets MC (central, $Q^2$ up/down) $\\sqrt{s}$ = 8 TeV"
],
"x_axis_title": [
"$E_T^{\\text{miss}}$ [GeV]"
],
"x_limits": [
[0, 300]
],
"y_axis_title": [
"normalised to unit area"
],
"y_limits": [
[0, 0.4]
],
"rebin" : [
[0.0, 27.0, 52.0, 87.0, 130.0, 172.0, 300]
],
"colours": ["green", "red", "blue"],
"fill_area": false
}
53 changes: 53 additions & 0 deletions config/plots/MET_matching_threshold_comparison_8TeV.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"command": "compare-files",
"files": [
"/hdfs/TopQuarkGroup/results/histogramfiles/AN-14-071_6th_draft/8TeV/unfolding/unfolding_TTJets_8TeV_asymmetric.root",
"/hdfs/TopQuarkGroup/results/histogramfiles/AN-14-071_6th_draft/8TeV/unfolding/unfolding_TTJets_8TeV_matchingdown_asymmetric.root",
"/hdfs/TopQuarkGroup/results/histogramfiles/AN-14-071_6th_draft/8TeV/unfolding/unfolding_TTJets_8TeV_matchingup_asymmetric.root"
],
"file-aliases": [
"TTJets_central",
"TTJets_matchingdown",
"TTJets_matchingup"
],
"histograms": [
"unfolding_MET_analyser_electron_channel_patType1CorrectedPFMet/truth",
"unfolding_MET_analyser_electron_channel_patType1CorrectedPFMet/measured",
"unfolding_MET_analyser_electron_channel_patType1CorrectedPFMet/fake",
"unfolding_MET_analyser_muon_channel_patType1CorrectedPFMet/truth",
"unfolding_MET_analyser_muon_channel_patType1CorrectedPFMet/measured",
"unfolding_MET_analyser_muon_channel_patType1CorrectedPFMet/fake"
],
"labels": [
"central",
"matching down",
"matching up"
],
"output_folder": "plots/8TeV/TTJet_comparison",
"output_format": ["pdf"],
"name_prefix": "comparison_matching_",
"plot_type": "shape_comparison",
"ratio_y_limits": [
[0.8, 1.2]
],
"title": [
"Comparison of TTJets MC (central, matchin up/down) $\\sqrt{s}$ = 8 TeV"
],
"x_axis_title": [
"$E_T^{\\text{miss}}$ [GeV]"
],
"x_limits": [
[0, 300]
],
"y_axis_title": [
"normalised to unit area"
],
"y_limits": [
[0, 0.4]
],
"rebin" : [
[0.0, 27.0, 52.0, 87.0, 130.0, 172.0, 300]
],
"colours": ["green", "red", "blue"],
"fill_area": false
}
12 changes: 10 additions & 2 deletions tools/HistSet.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,22 @@ def plot( self, plot_options = {} ):
'''
Plots the stored histograms based on the plot options
'''
# defaults
file_name = self.name
alpha = 0.5
fill_area = True
if plot_options.has_key('output_file'):
file_name = plot_options['output_file']
output_format = plot_options['output_format']
output_folder = plot_options['output_folder']

plot_type = plot_options['plot_type']

if plot_options.has_key('fill_area'):
fill_area = plot_options['fill_area']
if plot_options.has_key('alpha'):
alpha = plot_options['alpha']

histogram_properties = Histogram_properties(plot_options)
histogram_properties.name = file_name
if plot_options.has_key('rebin') and plot_options['rebin'] > 1:
Expand All @@ -55,9 +63,9 @@ def plot( self, plot_options = {} ):
names = self.labels,
colours = colours,
histogram_properties = histogram_properties,
fill_area = True,
fill_area = fill_area,
make_ratio = True,
alpha = 0.5,
alpha = alpha,
save_folder = output_folder,
save_as = output_format )
elif plot_type == 'data_mc_comparison':
Expand Down
44 changes: 44 additions & 0 deletions tools/hist_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ def spread_x( histograms, bin_edges ):
graphs = []
for histogram, x_coordinates in zip( histograms, x_locations ):
g = Graph( histogram )
copy_style(copy_from = histogram, copy_to = g)
for i, ( x, y ) in enumerate( zip( x_coordinates, histogram.y() ) ):
g.SetPoint( i, x, y )

Expand Down Expand Up @@ -339,6 +340,49 @@ def get_fit_results_histogram( data_path = 'data/absolute_eta_M3_angle_bl',
h_data = value_error_tuplelist_to_hist( fit_data, bin_edges )
return h_data

def get_histogram_ratios(nominator, denominators, normalise_ratio_to_errors = False):
ratios = []
for denom in denominators:
ratio = nominator.Clone()
if normalise_ratio_to_errors:
# TODO
# this is a preliminary feature, use with care
for bin_i in range( 1, nominator.nbins() ):
x_i = nominator[bin_i].value
x_i_error = nominator[bin_i].error
y_i = denom[bin_i].value
y_i_error = denom[bin_i].error
numerator = x_i - y_i
denominator = pow( pow( x_i_error, 2 ) + pow( y_i_error, 2 ), 0.5 )
if denominator == 0:
ratio.SetBinContent(bin_i, 0.)
ratio.SetBinError(bin_i, 0.)
else:
ratio.SetBinContent(bin_i, numerator/denominator)
ratio.SetBinError(bin_i, denominator)
else:
ratio.Divide( denom )
if len(denominators) > 1:
ratio.linecolor = denom.linecolor
ratio.fillcolor = denom.fillcolor
ratios.append(ratio)
return ratios

def copy_style(copy_from, copy_to):
# colours
copy_to.linecolor = copy_from.linecolor
copy_to.markercolor = copy_from.markercolor
copy_to.fillcolor = copy_from.fillcolor
# style
copy_to.markerstyle = copy_from.markerstyle
copy_to.linestyle = copy_from.linestyle
copy_to.fillstyle = copy_from.fillstyle
# size
copy_to.markersize = copy_from.markersize
copy_to.linesize = copy_from.markersize
# legend
copy_to.legendstyle = copy_from.legendstyle

if __name__ == '__main__':
value_error_tuplelist = [( 0.006480446927374301, 0.0004647547547401945 ),
( 0.012830288388947605, 0.0010071677178938234 ),
Expand Down
49 changes: 19 additions & 30 deletions tools/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
'''
import matplotlib as mpl
from tools.file_utilities import make_folder_if_not_exists
from tkFont import names
from tools.hist_utilities import get_histogram_ratios, spread_x
mpl.use('agg')
import matplotlib.pyplot as plt
import rootpy.plotting.root2matplotlib as rplt
Expand Down Expand Up @@ -291,13 +293,13 @@ def make_shape_comparison_plot( shapes = [],
if integral > 0:
shape.Sumw2()
shape.Scale( 1 / integral )
shape.fillcolor = colour
shape.linecolor = colour
shape.markercolor = colour
shape.legendstyle = 'F'
if fill_area:
shape.fillcolor = colour
shape.fillstyle = 'solid'
shape.legendstyle = 'F'
else:
shape.linecolor = colour
shape.legendstyle = 'F'
shape.linewidth = 5

if not histogram_properties.y_limits:
Expand All @@ -319,39 +321,24 @@ def make_shape_comparison_plot( shapes = [],
handles, labels = axes.get_legend_handles_labels()
for i,name in enumerate(names):
labels.insert(i, name)

# always fill legends
if not fill_area:
for handle in handles:
handle.set_fill(True)
handle.set_facecolor(handle.get_edgecolor())

plt.legend( handles, labels, numpoints = 1, loc = histogram_properties.legend_location,
prop = CMS.legend_properties, ncol = histogram_properties.legend_columns ).set_zorder(102)
#add error bars
for shape in shapes_:
rplt.errorbar( shape, axes = axes, alpha = alpha)
graphs = spread_x(shapes_, list(shapes_[0].xedges()))
for graph in graphs:
rplt.errorbar( graph, axes = axes, xerr = False,)

adjust_axis_limits(axes, histogram_properties)
if make_ratio:
plt.setp( axes.get_xticklabels(), visible = False )

ratio = shapes_[0].Clone( 'ratio' )
if normalise_ratio_to_errors:
# TODO
# this is a preliminary feature, use with care
for bin_i in range( 1, shapes_[0].nbins() ):
x_i = shapes_[0][bin_i].value
x_i_error = shapes_[0][bin_i].error
y_i = shapes_[1][bin_i].value
y_i_error = shapes_[1][bin_i].error
numerator = x_i - y_i
denominator = pow( pow( x_i_error, 2 ) + pow( y_i_error, 2 ), 0.5 )
if denominator == 0:
ratio.SetBinContent(bin_i, 0.)
ratio.SetBinError(bin_i, 0.)
else:
ratio.SetBinContent(bin_i, numerator/denominator)
ratio.SetBinError(bin_i, denominator)
else:
ratio.Divide( shapes_[1] )

ratio.SetMarkerSize( 3 )

ratios = get_histogram_ratios(shapes_[0], shapes_[1:], normalise_ratio_to_errors)
ax1 = plt.subplot( gs[1] )
ax1.minorticks_on()
ax1.grid( True, 'major', linewidth = 1 )
Expand All @@ -360,7 +347,9 @@ def make_shape_comparison_plot( shapes = [],
plt.ylabel( r'$\frac{1-2}{\sqrt{(\sigma_1)^2 + (\sigma_2)^2}}$', CMS.y_axis_title )
else:
plt.ylabel( '(1)/(2)', CMS.y_axis_title )
rplt.errorbar( ratio, xerr = True, emptybins = False, axes = ax1 )
for ratio in ratios:
ratio.SetMarkerSize( 2 )
rplt.errorbar( ratio, xerr = True, emptybins = False, axes = ax1 )
if len( histogram_properties.x_limits ) == 2:
ax1.set_xlim( xmin = histogram_properties.x_limits[0],
xmax = histogram_properties.x_limits[1] )
Expand Down