-
Notifications
You must be signed in to change notification settings - Fork 41
/
plot_CMSPODAS23.py
executable file
·161 lines (141 loc) · 8.91 KB
/
plot_CMSPODAS23.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
#! /usr/bin/env python
# Author: Izaak Neutelings (August 2020)
# Description: Simple plotting script for pico analysis tuples
# Instructions:
# ./plot.py -y 2018 -c mutau
# ./plot.py -y 2018 -c config/setup_mutau.yml
# ./plot.py -y 2018 -c mutau -S baseline -V m_vis
from config.samples_v10 import *
from TauFW.Plotter.plot.string import filtervars
from TauFW.Plotter.plot.utils import LOG as PLOG
from TauFW.Plotter.plot.Plot import Plot, deletehist
import yaml
def plot(sampleset,setup,parallel=True,tag="",extratext="",outdir="plots",era="",
varfilter=None,selfilter=None,fraction=False,pdf=False):
"""Test plotting of SampleSet class for data/MC comparison."""
LOG.header("plot")
channel = setup["channel"]
if 'baselineCuts' in setup: # baseline pre-selections
baseline = setup['baselineCuts']
else:
raise IOError("No baseline selection for channel %r defined!"%(channel))
selections = [ # plot these selections
Sel('baseline',baseline)
]
if 'regions' in setup: # add extra regions on top of baseline
for region in setup['regions']:
skwargs = setup['regions'][region].copy() # extra key-word options
assert 'definition' in skwargs
selstr = setup['baselineCuts']+" && "+skwargs.pop('definition')
selections.append(Sel(region,selstr,**skwargs))
selections = filtervars(selections,selfilter) # filter variable list with -S/--sel flag
# VARIABLES
variables = [
Var('pt_1', "Muon pt", 50, 0, 500, 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", 50, 0, 500, 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, -10, 10, 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, -10, 10, 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)", 50, 0, 500, ctitle={'etau':"mt(mu,MET)",'tautau':"mt(tau,MET)",'emu':"mt(e,MET)"},cbins={"nbtag\w*>":(50,0,250)}),
Var('njets', 10, 0, 10),
Var('nbtag', "Number of b jets (Medium, pt > 30 GeV)", 10, 0, 10),
Var('met', 50, 0, 150,cbins={"nbtag\w*>":(50,0,250)}),
]
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={'mutau':"m_{#mu#tau}",'etau':"m_{e#tau}"},logy=False, cbins={"pt_\d>":(50,0,250),"nbtag\w*>":(60,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('rawDeepTau2018v2p5VSe_2', "rawDeepTau2018v2p5VSe", 80, 0.20, 1, ymin = 1e2, fname="$VAR_zoom",ncols=2,logy=True,logyrange=4,pos='L;y=0.85'),
Var('rawDeepTau2018v2p5VSmu_2', "rawDeepTau2018v2p5VSmu", 25, 0.90, 1, ymin = 1e1, fname="$VAR_zoom",ncols=2,logy=True,logyrange=5,pos='L;y=0.85'),
Var('rawDeepTau2017v2p1VSjet_2', "rawDeepTau2017v2p1VSjet", 50, 0.00, 1, ymin = 1e1, fname="$VAR_allRange", ncols=2,pos='L;y=0.85',logy=True,ymargin=1.5),
Var('rawDeepTau2018v2p5VSjet_2', "rawDeepTau2018v2p5VSjet", 50, 0.00, 1, ymin = 1e1, fname="$VAR_allRange", ncols=2,pos='L;y=0.85',logy=True,ymargin=1.5),
]
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))
exts = ['png','pdf'] if pdf else ['png'] # extensions
for selection in selections:
print(">>> Selection %r: %r"%(selection.title,selection.selection))
stacks = sampleset.getstack(variables,selection,method='QCD_OSSS',scale=1, 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.iteritems():
for stack, variable in stacks.items(): # python 3
#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
fraction = args.fraction
pdf = args.pdf
outdir = "plots/$ERA/$CHANNEL"
fname = "$PICODIR/$SAMPLE_$CHANNEL$TAG.root"
#fname = "/nfs/user/pmastra/DeepTau2p5/analysis/$ERA/$CHANNEL/$GROUP/$SAMPLE_$CHANNEL$TAG.root"
# 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',"")+args.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)
sampleset.close()
if __name__ == "__main__":
from argparse import ArgumentParser, RawTextHelpFormatter
eras = ['2016','2017','2018','UL2016_preVFP','UL2016_postVFP','UL2017','UL2018','2022_preEE','2022_postEE','UL2018_v10']
description = """Simple plotting script for pico analysis tuples"""
parser = ArgumentParser(prog="plot",description=description,epilog="Good luck!")
parser.add_argument('-y', '--era', dest='eras', nargs='*', choices=eras, default=['2017'],
help="set era" )
parser.add_argument('-c', '--config', '--channel',
dest='configs', type=str, nargs='+', default=['config/setup_mutau_PODAS.yml'], action='store',
help="config file(s) containing channel setup for samples and selections, 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('-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, action='store',
help="set verbosity" )
args = parser.parse_args()
LOG.verbosity = args.verbosity
PLOG.verbosity = args.verbosity
main(args)
print("\n>>> Done.")