forked from cms-tau-pog/TauFW
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot_differential.py
executable file
·209 lines (194 loc) · 11.9 KB
/
plot_differential.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#! /usr/bin/env python
# Author: Izaak Neutelings (April 2022)
# Description:
# Simple plotting script for in (double) differential bins of tau ID WP, pt & DM
# Example of plots: https://indico.cern.ch/event/1104863/
# Instructions:
# ./plot.py -c mutau -y 2018
# ./plot.py -c mutau -y 2018 -S baseline -V m_vis
from config.samples import *
from TauFW.Plotter.plot.string import filtervars
from TauFW.Plotter.plot.utils import LOG as PLOG
import yaml
def plot(sampleset,setup,parallel=True,tag="",extratext="",outdir="plots",era="",
varfilter=None,selfilter=None,fraction=False,pdf=False,exts=['png']):
"""Test plotting of SampleSet class for data/MC comparison."""
LOG.header("plot")
channel = setup["channel"]
if 'baselineCuts' in setup:
baseline = setup['baselineCuts']
else:
raise IOError("No baseline selection for channel %r defined!"%(channel))
baseline = baseline.replace("VSjet_2>=32","VSjet_2>=16")
noVSjet = baseline.replace(" && idDeepTau2017v2p1VSjet_2>=16","")
zttregion = "%s && mt_1<60 && dzeta>-25 && abs(deta_ll)<1.5"%(baseline) # && nbtag==0
selections = [
Sel('baseline, no DeepTauVSjet',noVSjet,only=["DeepTau","^pt_[12]","^eta_[12]","^m_2","dm"]),
Sel("baseline",baseline),
#Sel("baseline, pt > 50 GeV",baseline+" && pt_1>50"),
#Sel("mt<60 GeV, dzeta>-25 GeV, |deta|<1.5",zttregion,fname="zttregion"),
#Sel("0b",baseline+" && nbtag==0",weight="btagweight"),
#Sel(">=1b",baseline+" && nbtag>=1",weight="btagweight"),
]
# DIFFERENTIAL pt/DM bins for TauID measurement
wps = [
('Medium',"idDeepTau2017v2p1VSjet_2>=16"),
#('VVVLoose && !Medium',"idDeepTau2017v2p1VSjet_2>=1 && idDeepTau2017v2p1VSjet_2<16")
#('0b, Medium',"idDeepTau2017v2p1VSjet_2>=16 && nbtag==0"),
#('0b, VVVLoose && !Medium',"idDeepTau2017v2p1VSjet_2>=1 && idDeepTau2017v2p1VSjet_2<16 && nbtag==0")
#('>=1b, Medium',"idDeepTau2017v2p1VSjet_2>=16 && nbtag>=1"),
#('>=1b, VVVLoose && !Medium',"idDeepTau2017v2p1VSjet_2>=1 && idDeepTau2017v2p1VSjet_2<16 && nbtag>=1")
]
pts = [20,30,40,50,70]
dms = [0,1,10,11]
for wp, wpcut in wps:
wpname = wp.replace(" && !","-not")
basecut = baseline.replace("idDeepTau2017v2p1VSjet_2>=16",wpcut) #+" && nbtag==0"
#for dm in dms:
# name_ = "%s_dm%s"%(wpname,dm)
# tit_ = "%s, DM%s"%(wp,dm)
# cut_ = "%s && dm_2==%s"%(basecut,dm)
# selections.append(Sel(name_,tit_,cut_,only=['m_vis','m_2'])) # DM bins
for i, ptlow in enumerate(pts):
if i<len(pts)-1: # ptlow < pt < ptup
ptup = pts[i+1]
name = "%s_pt%d-%d"%(wpname,ptlow,ptup)
tit = "%s, %d < pt < %d GeV"%(wp,ptlow,ptup)
cut = "%s && %s<pt_2 && pt_2<%s"%(basecut,ptlow,ptup)
else: # pt > ptlow (no upper pt cut)
name = "%s_pt%d-Inf"%(wpname,ptlow)
tit = "%s, pt > %d GeV"%(wp,ptlow)
cut = "%s && pt_2>%s"%(basecut,ptlow)
selections.append(Sel(name,tit,cut,only=['m_vis','^m_2','mapRecoDM'])) # pt bins
#for dm in dms:
# name_ = "%s_dm%s"%(name,dm)
# tit_ = "%s, DM%s"%(tit,dm)
# cut_ = "%s && dm_2==%s"%(cut,dm)
# selections.append(Sel(name_,tit_,cut_,only=['m_vis','^m_2'])) # pt-DM bins
# VARIABLES
variables = [
Var('pt_1', "Muon pt", 40, 0, 120, ctitle={'etau':"Electron pt",'tautau':"Leading tau_h pt",'mumu':"Leading muon pt",'emu':"Electron pt"},cbins={"nbtag\w*>":(40,0,200)}),
Var('pt_2', "tau_h pt", 40, 0, 120, ctitle={'tautau':"Subleading tau_h pt",'mumu':"Subleading muon pt",'emu':"Muon pt"},cbins={"nbtag\w*>":(40,0,200)}),
Var('eta_1', "Muon eta", 30, -3, 3, ctitle={'etau':"Electron eta",'tautau':"Leading tau_h eta",'mumu':"Leading muon eta",'emu':"Electron eta"},ymargin=1.7,pos='T',ncols=2),
Var('eta_2', "tau_h eta", 30, -3, 3, ctitle={'etau':"Electron eta",'tautau':"Subleading tau_h eta",'mumu':"Subleading muon eta",'emu':"Muon eta"},ymargin=1.7,pos='T',ncols=2),
Var('mt_1', "mt(mu,MET)", 40, 0, 200, ctitle={'etau':"mt(mu,MET)",'tautau':"mt(tau,MET)",'emu':"mt(e,MET)"},cbins={"nbtag\w*>":(50,0,250)}),
Var("jpt_1", 29, 10, 300, veto=[r"njets\w*==0"]),
Var("jpt_2", 29, 10, 300, veto=[r"njets\w*==0"]),
Var("jeta_1", 53, -5.4, 5.2, ymargin=1.6,pos='T',ncols=2,veto=[r"njets\w*==0"]),
Var("jeta_2", 53, -5.4, 5.2, ymargin=1.6,pos='T',ncols=2,veto=[r"njets\w*==0"]),
Var('npv', 40, 0, 80),
Var('njets', 8, 0, 8),
Var('nbtag', "Number of b jets (Medium, pt > 30 GeV)", 8, 0, 8),
Var('met', 50, 0, 150,cbins={"nbtag\w*>":(50,0,250)}),
#Var('genmet', 50, 0, 150, fname="$VAR_log", logyrange=4, data=False, logy=True, ncols=2, pos='TT'),
Var('pt_ll', "p_{T}(mutau_h)", 25, 0, 200, ctitle={'etau':"p_{T}(etau_h)",'tautau':"p_{T}(tau_htau_h)",'emu':"p_{T}(emu)"}),
Var('dR_ll', "DR(mutau_h)", 30, 0, 6.0, ctitle={'etau':"DR(etau_h)",'tautau':"DR(tau_htau_h)",'emu':"DR(emu)"}),
Var('deta_ll', "deta(mutau_h)", 20, 0, 6.0, ctitle={'etau':"deta(etau_h)",'tautau':"deta(tautau)",'emu':"deta(emu)"},logy=True,pos='TRR',cbins={"abs(deta_ll)<":(10,0,3)}), #, ymargin=8, logyrange=2.6
Var('dzeta', 56, -180, 100, pos='L;y=0.87',units='GeV',cbins={"nbtag\w*>":(35,-220,130)}),
]
if 'tau' in channel: # mutau, etau, tautau
loadmacro("python/macros/mapDecayModes.C") # for mapRecoDM
dmlabels = ["h^{#pm}","h^{#pm}h^{0}","h^{#pm}h^{#mp}h^{#pm}","h^{#pm}h^{#mp}h^{#pm}h^{0}","Other"]
variables += [
Var('m_vis', 40, 0, 200, fname="mvis",ctitle={'mumu':"m_mumu",'emu':"m_emu"},cbins={"pt_\d>":(50,0,250),"nbtag\w*>":(60,0,300)},cpos={"pt_\d>[1678]0":'LL;y=0.88'}),
Var('m_vis', 20, 0, 200, fname="mvis_coarse",ctitle={'mumu':"m_mumu",'emu':"m_emu"},cbins={"pt_\d>":(25,0,250),"nbtag\w*>":(30,0,300)},cpos={"pt_\d>[1678]0":'LL;y=0.88'}),
Var("m_2", 30, 0, 3, title="m_tau",veto=["njet","nbtag","dm_2==0"]),
Var("dm_2", 14, 0, 14, fname="dm_2",title="Reconstructed tau_h decay mode",veto="dm_2==",position="TMC",ymargin=1.2),
Var("mapRecoDM(dm_2)", 5, 0, 5, fname="dm_2_label",title="Reconstructed tau_h decay mode",veto="dm_2==",position="TT",labels=dmlabels,ymargin=1.2),
#Var("pzetavis", 50, 0, 200 ),
Var('rawDeepTau2017v2p1VSjet_2', "rawDeepTau2017v2p1VSjet", 100, 0.0, 1, ncols=2,pos='L;y=0.87',logy=True,ymargin=1.9,cymargin={'VSjet':1.5},cbins={"VSjet_2>":(60,0.4,1)}),
Var('rawDeepTau2017v2p1VSjet_2', "rawDeepTau2017v2p1VSjet", 20, 0.80, 1, fname="$VAR_zoom",ncols=2,pos='L;y=0.85'),
Var('rawDeepTau2017v2p1VSe_2', "rawDeepTau2017v2p1VSe", 30, 0.70, 1, fname="$VAR_zoom",ncols=2,logy=True,logyrange=4,pos='L;y=0.85'),
Var('rawDeepTau2017v2p1VSmu_2', "rawDeepTau2017v2p1VSmu", 20, 0.80, 1, fname="$VAR_zoom",ncols=2,logy=True,logyrange=5,pos='L;y=0.85'),
]
elif 'mumu' in channel:
variables += [
Var('m_ll', "m_mumu", 40, 0, 200, fname="$VAR", cbins={"m_vis>200":(40,200,1000)}), # alias: m_ll alias of m_vis
Var('m_ll', "m_mumu", 40, 0, 200, fname="$VAR_log", logy=True, ymin=1e2, cbins={"m_vis>200":(40,200,1000)} ),
Var('m_ll', "m_mumu", 40, 70, 110, fname="$VAR_Zmass", veto=["m_vis>200"] ),
Var('m_ll', "m_mumu", 1, 70, 110, fname="$VAR_1bin", veto=["m_vis>200"] ),
]
variables = filtervars(variables,varfilter) # filter variable list with -V/--var flag
# PLOT
outdir = ensuredir(repkey(outdir,CHANNEL=channel,ERA=era))
if pdf and 'pdf' not in exts: # extensions for output image file
exts = exts+['pdf']
for selection in selections:
print(">>> Selection %r: %r"%(selection.title,selection.selection))
stacks = sampleset.getstack(variables,selection,method='QCD_OSSS',parallel=parallel)
fname = "%s/$VAR_%s-%s-%s$TAG"%(outdir,channel.replace('mu','m').replace('tau','t'),selection.filename,era)
text = "%s: %s"%(channel.replace('mu',"#mu").replace('tau',"#tau_{h}"),selection.title)
if extratext:
text += ("" if '\n' in extratext[:3] else ", ") + extratext
for stack, variable in stacks.items():
#position = "" #variable.position or 'topright'
stack.draw(fraction=fraction)
stack.drawlegend() #position)
stack.drawtext(text)
stack.saveas(fname,ext=exts,tag=tag)
stack.close()
def main(args):
configs = args.configs
eras = args.eras
parallel = args.parallel
varfilter = args.varfilter
selfilter = args.selfilter
notauidsf = args.notauidsf
extratext = args.text
tag = args.tag
fraction = args.fraction
pdf = args.pdf
exts = args.exts
outdir = "plots/$ERA"
fname = "$PICODIR/$SAMPLE_$CHANNEL$TAG.root"
if notauidsf:
tag += "_noIDSF"
# LOOP over configs / channels
for config in configs:
if not config.endswith(".yml"): # config = channel name
config = "config/setup_%s.yml"%(config) # assume this file name pattern
print(">>> Using configuration file: %s"%config)
with open(config, 'r') as file:
setup = yaml.safe_load(file)
tag_ = setup.get('tag',"")+tag
for era in eras:
setera(era) # set era for plot style and lumi-xsec normalization
addsfs = [ ] #"getTauIDSF(dm_2,genmatch_2)"]
rmsfs = [ ] if (setup['channel']=='mumu' or not notauidsf) else ['idweight_2','ltfweight_2'] # remove tau ID SFs
split = ['DY'] if 'tau' in setup['channel'] else [ ] # split these backgrounds into tau components
sampleset = getsampleset(setup['channel'],era,fname=fname,rmsf=rmsfs,addsf=addsfs,split=split)
plot(sampleset,setup,parallel=parallel,tag=tag_,extratext=extratext,outdir=outdir,era=era,
varfilter=varfilter,selfilter=selfilter,fraction=fraction,pdf=pdf,exts=exts)
sampleset.close()
if __name__ == "__main__":
from argparse import ArgumentParser, RawTextHelpFormatter
eras = ['2016','2017','2018','UL2016_preVFP','UL2016_postVFP','UL2017','UL2018']
description = """Simple plotting script for pico analysis tuples"""
parser = ArgumentParser(prog="plot",description=description,epilog="Good luck!",formatter_class=RawTextHelpFormatter)
parser.add_argument('-y', '--era', dest='eras', nargs='*', choices=eras, default=['2017'],
help="set era" )
parser.add_argument('-c', '--config', dest='configs', type=str, nargs='+', default=['config/setup_mutau.yml'],
help="set config file containing sample & fit setup, default=%(default)r" )
parser.add_argument('-V', '--var', dest='varfilter', nargs='+',
help="only plot the variables passing this filter (glob patterns allowed)" )
parser.add_argument('-S', '--sel', dest='selfilter', nargs='+',
help="only plot the selection passing this filter (glob patterns allowed)" )
parser.add_argument('-s', '--serial', dest='parallel', action='store_false',
help="run Tree::MultiDraw serial instead of in parallel" )
parser.add_argument('-F', '--fraction',dest='fraction', action='store_true',
help="include fraction stack in ratio plot" )
parser.add_argument('-p', '--pdf', dest='pdf', action='store_true',
help="create pdf version of each plot" )
parser.add_argument('-e', '--ext', dest='exts', nargs='+', default=['png'],
help="extension for output image files" )
parser.add_argument('-r', '--nosf', dest='notauidsf', action='store_true',
help="remove DeepTau ID SF" )
parser.add_argument('-t', '--tag', default="", help="extra tag for output" )
parser.add_argument('-T', '--text', default="", help="extra text on plot" )
parser.add_argument('-v', '--verbose', dest='verbosity', type=int, nargs='?', const=1, default=0,
help="set verbosity" )
args = parser.parse_args()
LOG.verbosity = args.verbosity
PLOG.verbosity = args.verbosity
main(args)
print("\n>>> Done.")