From a84735d15543e543cfd3bbc39381ec8fcceafc6f Mon Sep 17 00:00:00 2001 From: kreczko Date: Fri, 13 Mar 2015 13:58:38 +0000 Subject: [PATCH 1/4] added "fill_area" and "alpha" to global plotting options --- bin/plot | 3 ++- tools/HistSet.py | 12 ++++++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/bin/plot b/bin/plot index 9e882845..60240453 100755 --- a/bin/plot +++ b/bin/plot @@ -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() diff --git a/tools/HistSet.py b/tools/HistSet.py index 7bd50156..d51c0a0d 100644 --- a/tools/HistSet.py +++ b/tools/HistSet.py @@ -27,7 +27,10 @@ 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'] @@ -35,6 +38,11 @@ def plot( self, plot_options = {} ): 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: @@ -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': From b611abda01e023dbaeea1a6da55757bd2bde10d7 Mon Sep 17 00:00:00 2001 From: kreczko Date: Fri, 13 Mar 2015 14:00:11 +0000 Subject: [PATCH 2/4] added get_histogram_ratios & copy_style to tools/hist_utilities --- tools/hist_utilities.py | 44 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/tools/hist_utilities.py b/tools/hist_utilities.py index fba0602d..55558728 100644 --- a/tools/hist_utilities.py +++ b/tools/hist_utilities.py @@ -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 ) @@ -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 ), From 2d5dbb8f5af88debe1c49584b726800b2d57d17c Mon Sep 17 00:00:00 2001 From: kreczko Date: Fri, 13 Mar 2015 14:00:45 +0000 Subject: [PATCH 3/4] A few changes to tools/plotting: - show legend entries as filled even if fill_area is false (shape comparison only) - if len(hists) > 2 show all ratios in the ratio plot w.r.t. 1st hist - spread data points on x axis (shape comparison only) --- tools/plotting.py | 49 ++++++++++++++++++----------------------------- 1 file changed, 19 insertions(+), 30 deletions(-) diff --git a/tools/plotting.py b/tools/plotting.py index af7f3fc2..85ad13cb 100644 --- a/tools/plotting.py +++ b/tools/plotting.py @@ -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 @@ -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: @@ -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 ) @@ -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] ) From 747782bc3a8077b3fa0330be189fca8d8d54b3d8 Mon Sep 17 00:00:00 2001 From: kreczko Date: Fri, 13 Mar 2015 14:09:41 +0000 Subject: [PATCH 4/4] added plot configs for Q^2/matching comparison --- ...T_factorisation_scale_comparison_8TeV.json | 53 +++++++++++++++++++ ...ET_matching_threshold_comparison_8TeV.json | 53 +++++++++++++++++++ 2 files changed, 106 insertions(+) create mode 100644 config/plots/MET_factorisation_scale_comparison_8TeV.json create mode 100644 config/plots/MET_matching_threshold_comparison_8TeV.json diff --git a/config/plots/MET_factorisation_scale_comparison_8TeV.json b/config/plots/MET_factorisation_scale_comparison_8TeV.json new file mode 100644 index 00000000..58fea7b7 --- /dev/null +++ b/config/plots/MET_factorisation_scale_comparison_8TeV.json @@ -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 +} diff --git a/config/plots/MET_matching_threshold_comparison_8TeV.json b/config/plots/MET_matching_threshold_comparison_8TeV.json new file mode 100644 index 00000000..e9a2fcd2 --- /dev/null +++ b/config/plots/MET_matching_threshold_comparison_8TeV.json @@ -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 +}