Skip to content

Commit 9e301ff

Browse files
committed
Merge pull request #206 from kreczko/latex-checks
[MRG] Latex checks
2 parents c4864b1 + e6ff8e7 commit 9e301ff

File tree

9 files changed

+174
-73
lines changed

9 files changed

+174
-73
lines changed

config/latex_labels.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818

1919
measurements_latex = {'unfolded': 'unfolded',
2020
'measured': 'measured',
21-
'MADGRAPH': '$t\\bar{t}$ (MADPGRAPH+Pythia)',
22-
'MADGRAPH_ptreweight': '$t\\bar{t}$ (MADPGRAPH+$p_\mathrm{T}^\mathrm{reweight}$)',
21+
'MADGRAPH': '$t\\bar{t}$ (MADGRAPH+Pythia)',
22+
'MADGRAPH_ptreweight': '$t\\bar{t}$ (MADGRAPH+$p_\mathrm{T}^\mathrm{reweight}$)',
2323
'MCATNLO': '$t\\bar{t}$ (MC@NLO+Herwig)',
2424
'POWHEG_PYTHIA': '$t\\bar{t}$ (POWHEG+Pythia)',
2525
'POWHEG_HERWIG': '$t\\bar{t}$ (POWHEG+Herwig)',
@@ -99,8 +99,8 @@
9999
'angle_bl' : r'angle$(b,l)$',
100100
}
101101

102-
typical_systematics_latex = {"typical_systematics_electron": "Trigger efficiency \& electron selection",
103-
"typical_systematics_muon": "Trigger efficiency \& muon selection",
102+
typical_systematics_latex = {"typical_systematics_electron": "Electron trigger efficiency \& electron selection",
103+
"typical_systematics_muon": "Muon trigger efficiency \& muon selection",
104104
"typical_systematics_btagging": "btagging",
105105
"typical_systematics_JES": "Jet Energy Scale",
106106
"typical_systematics_JER": "Jet Energy Resolution",

setup.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ echo "Installing matplotlib"
7777
export LDFLAGS="$LDFLAGS -L$VO_CMS_SW_DIR/$SCRAM_ARCH/external/freetype/2.4.7/lib -L$VO_CMS_SW_DIR/$SCRAM_ARCH/external/libpng/1.2.46/lib"
7878
export CFLAGS="$CFLAGS -I$VO_CMS_SW_DIR/$SCRAM_ARCH/external/freetype/2.4.7/include -I$VO_CMS_SW_DIR/$SCRAM_ARCH/external/freetype/2.4.7/include/freetype2 -I$VO_CMS_SW_DIR/$SCRAM_ARCH/external/libpng/1.2.46/include"
7979
#pip install -e $base/external/matplotlib
80-
pip install matplotlib==1.3.1 &> /dev/null
80+
pip install matplotlib==1.4.3 &> /dev/null
8181
success $? matplotlib
8282
echo "Installing rootpy"
8383
pip install -e $base/external/rootpy &> /dev/null

src/cross_section_measurement/04_make_plots_matplotlib.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,10 @@
2222
import matplotlib.gridspec as gridspec
2323
from matplotlib.ticker import MultipleLocator
2424
from config import CMS
25-
from matplotlib import rc, rcParams
26-
rc( 'font', **CMS.font )
27-
rc( 'text', usetex = True )
28-
rcParams['text.latex.preamble'] = [
29-
r'\usepackage{siunitx}', # i need upright \micro symbols, but you need...
30-
r'\sisetup{detect-all}', # ...this to force siunitx to actually use your fonts
31-
r'\usepackage{helvet}', # set the normal font here
32-
r'\usepackage{sansmath}', # load up the sansmath so that math -> helvet
33-
r'\sansmath' # <- tricky! -- gotta actually tell tex to use!
34-
]
25+
from tools.latex import setup_matplotlib
26+
# latex, font, etc
27+
setup_matplotlib()
28+
3529
import matplotlib.patches as mpatches
3630

3731
def read_xsection_measurement_results( category, channel ):
@@ -653,7 +647,7 @@ def get_unit_string(fit_variable):
653647
if met_type == 'PFMETJetEnDown':
654648
met_type = 'patPFMetJetEnDown'
655649

656-
if not channel == 'combined':
650+
if not channel == 'combined' and options.additional_plots:
657651
#Don't make additional plots for e.g. generator systematics, mass systematics, k value systematics and pdf systematics because they are now done \
658652
#in the unfolding process with BLT unfolding files.
659653
if category in ttbar_generator_systematics or category in ttbar_mass_systematics or category in kValue_systematics or category in pdf_uncertainties:

src/cross_section_measurement/make_control_plots.py

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,9 @@
88
make_control_region_comparison
99
from tools.hist_utilities import prepare_histograms, clean_control_region
1010
from tools.ROOT_utils import get_histograms_from_files, set_root_defaults
11-
from matplotlib import rc, rcParams
12-
from config import CMS
13-
rc( 'font', **CMS.font )
14-
rc( 'text', usetex = True )
15-
rcParams['text.latex.preamble'] = [
16-
r'\usepackage{siunitx}', # i need upright \micro symbols, but you need...
17-
r'\sisetup{detect-all}', # ...this to force siunitx to actually use your fonts
18-
r'\usepackage{helvet}', # set the normal font here
19-
r'\usepackage{sansmath}', # load up the sansmath so that math -> helvet
20-
r'\sansmath' # <- tricky! -- gotta actually tell tex to use!
21-
]
11+
from tools.latex import setup_matplotlib
12+
# latex, font, etc
13+
setup_matplotlib()
2214

2315
def get_fitted_normalisation( variable, channel ):
2416
'''
@@ -106,7 +98,6 @@ def make_plot( channel, x_axis_title, y_axis_title,
10698
signal_region,
10799
name_prefix, x_limits,
108100
qcd_data_region_btag = '',
109-
qcd_data_region = '',
110101
use_qcd_data_region = True,
111102
y_limits = [],
112103
y_max_scale = 1.2,
@@ -120,19 +111,20 @@ def make_plot( channel, x_axis_title, y_axis_title,
120111
global output_folder, measurement_config, category, normalise_to_fit
121112
global preliminary, norm_variable, sum_bins, b_tag_bin, histogram_files
122113

114+
qcd_data_region = ''
123115
title = title_template % ( measurement_config.new_luminosity / 1000., measurement_config.centre_of_mass_energy )
124116
normalisation = None
125117
if channel == 'electron':
126118
histogram_files['data'] = measurement_config.data_file_electron
127119
histogram_files['QCD'] = measurement_config.electron_QCD_MC_category_templates[category]
128120
normalisation = normalisations_electron[norm_variable]
129-
if qcd_data_region_btag and qcd_data_region == '':
121+
if use_qcd_data_region:
130122
qcd_data_region = 'QCDConversions'
131123
if channel == 'muon':
132124
histogram_files['data'] = measurement_config.data_file_muon
133125
histogram_files['QCD'] = measurement_config.muon_QCD_MC_category_templates[category]
134126
normalisation = normalisations_muon[norm_variable]
135-
if qcd_data_region_btag and qcd_data_region == '':
127+
if use_qcd_data_region:
136128
qcd_data_region = 'QCD non iso mu+jets ge3j'
137129

138130
multi = isinstance( signal_region, list )
@@ -164,31 +156,39 @@ def make_plot( channel, x_axis_title, y_axis_title,
164156
else:
165157
histograms = get_histograms_from_files( [signal_region], histogram_files )
166158

159+
signal_region_hists = {}
160+
inclusive_control_region_hists = {}
161+
for sample in histograms.keys():
162+
signal_region_hists[sample] = histograms[sample][signal_region]
163+
if use_qcd_data_region:
164+
inclusive_control_region_hists[sample] = histograms[sample][qcd_control_region]
165+
167166
if normalise_to_fit:
168-
prepare_histograms( histograms, rebin = rebin,
167+
# only scale signal region to fit (results are invalid for control region)
168+
prepare_histograms( signal_region_hists, rebin = rebin,
169169
scale_factor = measurement_config.luminosity_scale,
170170
normalisation = normalisation )
171171
else:
172-
prepare_histograms( histograms, rebin = rebin,
172+
prepare_histograms( signal_region_hists, rebin = rebin,
173+
scale_factor = measurement_config.luminosity_scale )
174+
prepare_histograms( inclusive_control_region_hists, rebin = rebin,
173175
scale_factor = measurement_config.luminosity_scale )
174176
qcd_from_data = None
175177
if use_qcd_data_region:
176-
inclusive_control_region_hists = {}
177-
for sample in histograms.keys():
178-
inclusive_control_region_hists[sample] = histograms[sample][qcd_control_region]
179178
qcd_from_data = clean_control_region( inclusive_control_region_hists,
180179
subtract = ['TTJet', 'V+Jets', 'SingleTop'] )
181180
else:
182-
qcd_from_data = histograms['QCD'][signal_region]
181+
qcd_from_data = signal_region_hists['QCD']
183182

184183
n_qcd_predicted_mc = histograms['QCD'][signal_region].Integral()
185184
n_qcd_control_region = qcd_from_data.Integral()
186185
if not n_qcd_control_region == 0:
187186
qcd_from_data.Scale( 1.0 / n_qcd_control_region * n_qcd_predicted_mc )
188187

189-
histograms_to_draw = [histograms['data'][signal_region], qcd_from_data,
190-
histograms['V+Jets'][signal_region],
191-
histograms['SingleTop'][signal_region], histograms['TTJet'][signal_region]]
188+
histograms_to_draw = [signal_region_hists['data'], qcd_from_data,
189+
signal_region_hists['V+Jets'],
190+
signal_region_hists['SingleTop'],
191+
signal_region_hists['TTJet']]
192192
histogram_lables = ['data', 'QCD', 'V+Jets', 'Single-Top', samples_latex['TTJet']]
193193
histogram_colors = ['black', 'yellow', 'green', 'magenta', 'red']
194194

@@ -643,7 +643,7 @@ def make_plot( channel, x_axis_title, y_axis_title,
643643
rebin = 2,
644644
legend_location = ( 0.95, 0.78 ),
645645
cms_logo_location = 'right',
646-
ratio_y_limits = [0.5, 1.5],
646+
ratio_y_limits = [0.25, 1.75],
647647
)
648648
tmp = 'TTbar_plus_X_analysis/MuPlusJets/Ref selection/Binned_MT_Analysis/MT_with_patType1CorrectedPFMet_bin_%s/angle_bl_' + b_tag_bin
649649
regions = [tmp % bin_i for bin_i in variable_bins_ROOT['MT']]
@@ -670,6 +670,7 @@ def make_plot( channel, x_axis_title, y_axis_title,
670670
y_axis_title = 'Normalised events/(10 GeV)',
671671
signal_region = 'TTbar_plus_X_analysis/EPlusJets/Ref selection/bjet_invariant_mass_' + b_tag_bin,
672672
qcd_data_region_btag = '',
673+
use_qcd_data_region = False,
673674
name_prefix = 'EPlusJets_BJets_invmass_',
674675
x_limits = [0, 800],
675676
rebin = 10,
@@ -681,6 +682,7 @@ def make_plot( channel, x_axis_title, y_axis_title,
681682
y_axis_title = 'Normalised events/(10 GeV)',
682683
signal_region = 'TTbar_plus_X_analysis/MuPlusJets/Ref selection/bjet_invariant_mass_' + b_tag_bin,
683684
qcd_data_region_btag = '',
685+
use_qcd_data_region = False,
684686
name_prefix = 'MuPlusJets_BJets_invmass_',
685687
x_limits = [0, 800],
686688
rebin = 10,
@@ -697,6 +699,7 @@ def make_plot( channel, x_axis_title, y_axis_title,
697699
y_axis_title = 'Events',
698700
signal_region = 'TTbar_plus_X_analysis/EPlusJets/Ref selection/N_BJets',
699701
qcd_data_region_btag = '',
702+
use_qcd_data_region = False,
700703
name_prefix = 'EPlusJets_N_BJets',
701704
x_limits = [1.5, 7.5],
702705
rebin = 1,
@@ -708,6 +711,7 @@ def make_plot( channel, x_axis_title, y_axis_title,
708711
y_axis_title = 'Events',
709712
signal_region = 'TTbar_plus_X_analysis/MuPlusJets/Ref selection/N_BJets',
710713
qcd_data_region_btag = '',
714+
use_qcd_data_region = False,
711715
name_prefix = 'MuPlusJets_N_BJets',
712716
x_limits = [1.5, 7.5],
713717
rebin = 1,
@@ -721,6 +725,7 @@ def make_plot( channel, x_axis_title, y_axis_title,
721725
y_axis_title = 'Events',
722726
signal_region = 'TTbar_plus_X_analysis/EPlusJets/Ref selection/N_BJets_reweighted',
723727
qcd_data_region_btag = '',
728+
use_qcd_data_region = False,
724729
name_prefix = 'EPlusJets_N_BJets_reweighted',
725730
x_limits = [1.5, 7.5],
726731
rebin = 1,
@@ -732,6 +737,7 @@ def make_plot( channel, x_axis_title, y_axis_title,
732737
y_axis_title = 'Events',
733738
signal_region = 'TTbar_plus_X_analysis/MuPlusJets/Ref selection/N_BJets',
734739
qcd_data_region_btag = '',
740+
use_qcd_data_region = False,
735741
name_prefix = 'MuPlusJets_N_BJets',
736742
x_limits = [1.5, 7.5],
737743
rebin = 1,
@@ -748,6 +754,7 @@ def make_plot( channel, x_axis_title, y_axis_title,
748754
y_axis_title = 'Events',
749755
signal_region = 'TTbar_plus_X_analysis/EPlusJets/Ref selection/Jets/N_Jets_' + b_tag_bin,
750756
qcd_data_region_btag = '',
757+
use_qcd_data_region = False,
751758
name_prefix = 'EPlusJets_N_Jets_',
752759
x_limits = [3.5, 9.5],
753760
rebin = 1,
@@ -759,6 +766,7 @@ def make_plot( channel, x_axis_title, y_axis_title,
759766
y_axis_title = 'Events',
760767
signal_region = 'TTbar_plus_X_analysis/MuPlusJets/Ref selection/Jets/N_Jets_' + b_tag_bin,
761768
qcd_data_region_btag = '',
769+
use_qcd_data_region = False,
762770
name_prefix = 'MuPlusJets_N_Jets_',
763771
x_limits = [3.5, 9.5],
764772
rebin = 1,
@@ -775,6 +783,7 @@ def make_plot( channel, x_axis_title, y_axis_title,
775783
y_axis_title = 'arbitrary units',
776784
signal_region = 'TTbar_plus_X_analysis/EPlusJets/Ref selection/Vertices/nVertex',
777785
qcd_data_region_btag = '',
786+
use_qcd_data_region = False,
778787
name_prefix = 'EPlusJets_nVertex_',
779788
x_limits = [0, 50],
780789
rebin = 1,
@@ -787,6 +796,7 @@ def make_plot( channel, x_axis_title, y_axis_title,
787796
y_axis_title = 'arbitrary units',
788797
signal_region = 'TTbar_plus_X_analysis/MuPlusJets/Ref selection/Vertices/nVertex',
789798
qcd_data_region_btag = '',
799+
use_qcd_data_region = False,
790800
name_prefix = 'MuPlusJets_nVertex_',
791801
x_limits = [0, 50],
792802
rebin = 1,
@@ -800,6 +810,7 @@ def make_plot( channel, x_axis_title, y_axis_title,
800810
y_axis_title = 'arbitrary units',
801811
signal_region = 'TTbar_plus_X_analysis/EPlusJets/Ref selection/Vertices/nVertex_reweighted',
802812
qcd_data_region_btag = '',
813+
use_qcd_data_region = False,
803814
name_prefix = 'EPlusJets_nVertex_reweighted_',
804815
x_limits = [0, 50],
805816
rebin = 1,
@@ -812,6 +823,7 @@ def make_plot( channel, x_axis_title, y_axis_title,
812823
y_axis_title = 'arbitrary units',
813824
signal_region = 'TTbar_plus_X_analysis/MuPlusJets/Ref selection/Vertices/nVertex_reweighted',
814825
qcd_data_region_btag = '',
826+
use_qcd_data_region = False,
815827
name_prefix = 'MuPlusJets_nVertex_reweighted_',
816828
x_limits = [0, 50],
817829
rebin = 1,

src/cross_section_measurement/make_fit_variable_plots.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,9 @@
1515
from config.latex_labels import b_tag_bins_latex, samples_latex, channel_latex
1616
from config.variable_binning import variable_bins_ROOT, fit_variable_bin_edges, bin_edges
1717
from config import XSectionConfig
18-
from config import CMS
19-
from matplotlib import rc, rcParams
20-
rc( 'font', **CMS.font )
21-
rc( 'text', usetex = True )
22-
rcParams['text.latex.preamble'] = [
23-
r'\usepackage{siunitx}', # i need upright \micro symbols, but you need...
24-
r'\sisetup{detect-all}', # ...this to force siunitx to actually use your fonts
25-
r'\usepackage{helvet}', # set the normal font here
26-
r'\usepackage{sansmath}', # load up the sansmath so that math -> helvet
27-
r'\sansmath' # <- tricky! -- gotta actually tell tex to use!
28-
]
18+
from tools.latex import setup_matplotlib
19+
# latex, font, etc
20+
setup_matplotlib()
2921

3022
common_fit_variables = ['M3', 'M_bl', 'angle_bl']
3123
electron_fit_variables = copy( common_fit_variables )

test/tools_plotting.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'''
2+
Created on 5 May 2015
3+
4+
@author: kreczko
5+
'''
6+
from tools.plotting import get_best_max_y
7+
from tools.hist_utilities import value_errors_tuplelist_to_graph
8+
from tools.hist_utilities import value_error_tuplelist_to_hist
9+
10+
data_h = [( 3, 1 ),
11+
( 2, 1 ),
12+
( 1, 1 ),
13+
]
14+
data_g = [( 3, 1, 1 ),
15+
( 2, 1, 1 ),
16+
( 1, 1, 1 ),
17+
]
18+
bin_edges = [0, 1, 2, 3]
19+
20+
def test_get_max_y_hist():
21+
h = value_error_tuplelist_to_hist(data_h, bin_edges)
22+
max_y = get_best_max_y([h])
23+
assert max_y == 3 + 1
24+
25+
def test_get_max_y_graph():
26+
g = value_errors_tuplelist_to_graph(data_g, bin_edges)
27+
max_y = get_best_max_y([g])
28+
assert max_y == 3 + 1

tools/hist_utilities.py

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -95,25 +95,38 @@ def scale_histogram_errors( histogram, total_error ):
9595
def prepare_histograms( histograms, rebin = 1, scale_factor = 1.,
9696
normalisation = {}, exclude_from_scaling = ['data'] ):
9797
for sample, histogram_dict in histograms.iteritems():
98+
# check if this is a simple dict
99+
if histogram_dict.__class__.__name__ == 'Hist':
100+
h = histogram_dict
101+
scale = 1.
102+
norm = None
103+
if not sample in exclude_from_scaling:
104+
scale = scale_factor
105+
if sample in normalisation.keys():
106+
norm = normalisation[sample]
107+
scale_and_rebin_histogram( histogram = h, scale_factor = scale,
108+
normalisation = norm, rebin = rebin )
109+
continue
110+
# otherwise go a level deeper
98111
for _, histogram in histogram_dict.iteritems():
99-
histogram.Rebin( rebin )
112+
scale = 1.
113+
norm = None
100114
if not sample in exclude_from_scaling:
101-
histogram.Scale( scale_factor )
102-
if normalisation != {} and histogram.Integral() != 0:
103-
# TODO: this can be simplyfied and generalised
104-
# by using normalisation.keys() + for loop
105-
if sample == 'TTJet':
106-
histogram.Scale( normalisation['TTJet'][0] / histogram.Integral() )
107-
scale_histogram_errors( histogram, normalisation['TTJet'][1] )
108-
if sample == 'SingleTop':
109-
histogram.Scale( normalisation['SingleTop'][0] / histogram.Integral() )
110-
scale_histogram_errors( histogram, normalisation['SingleTop'][1] )
111-
if sample == 'V+Jets':
112-
histogram.Scale( normalisation['V+Jets'][0] / histogram.Integral() )
113-
scale_histogram_errors( histogram, normalisation['V+Jets'][1] )
114-
if sample == 'QCD':
115-
histogram.Scale( normalisation['QCD'][0] / histogram.Integral() )
116-
scale_histogram_errors( histogram, normalisation['QCD'][1] )
115+
scale = scale_factor
116+
if sample in normalisation.keys():
117+
norm = normalisation[sample]
118+
scale_and_rebin_histogram( histogram = histogram,
119+
scale_factor = scale,
120+
normalisation = norm, rebin = rebin )
121+
122+
def scale_and_rebin_histogram(histogram, scale_factor,
123+
normalisation = None,
124+
rebin = 1):
125+
histogram.Rebin( rebin )
126+
histogram.Scale( scale_factor )
127+
if not normalisation is None and histogram.Integral() != 0:
128+
histogram.Scale( normalisation[0] / histogram.Integral() )
129+
scale_histogram_errors( histogram, normalisation[1] )
117130

118131
def rebin_asymmetric( histogram, bins ):
119132
bin_array = array( 'd', bins )

0 commit comments

Comments
 (0)