Belle II Software light-2406-ragdoll
B2A908-ApplyLIDWeights.py
1#!/usr/bin/env python3
2
3
10
11"""
12Example to create lepton particle list and apply LeptonID corrections to a MC sample
13by retrieving lookup tables from the conditions DB.
14"""
15
16# Doxygen should skip this script
17# @cond
18
19import argparse
20
21
22def argparser():
23
24 parser = argparse.ArgumentParser(description=__doc__,
25 formatter_class=argparse.RawTextHelpFormatter)
26
27 parser.add_argument("--release",
28 type=int,
29 default=5,
30 help="The major release number associated to the corrections that are being applied.\n"
31 "Default: %(default)s.")
32 parser.add_argument("--global_tag_append",
33 type=str,
34 nargs="+",
35 default=['analysis_tools_light-2302-genetta'],
36 help="List of names of conditions DB global tag(s) to append on top of GT replay.\n"
37 "NB: these GTs will have lowest priority over GT replay.\n"
38 "The order of the sequence passed determines the priority of the GTs, w/ the highest coming first.\n"
39 "Pass a space-separated list of names.\n"
40 "Default: %(default)s.")
41 parser.add_argument("--lid_weights_gt",
42 type=str,
43 default="leptonid_Moriond2022_Official_rel5_v1a",
44 help="Name of conditions DB global tag with recommended lepton ID correction factors.\n"
45 "Default: %(default)s.")
46
47 return parser
48
49
50def main():
51 """
52 Main entry point allowing external calls.
53 """
54
55 args = argparser().parse_args()
56
57 import basf2 as b2
58 import modularAnalysis as ma
59 from variables import variables as vm
60 import variables.utils as vu
61 import variables.collections as vc
62 from stdCharged import stdE, stdMu
63
64 b2.set_log_level(b2.LogLevel.INFO)
65
66 for tag in args.global_tag_append:
67 b2.conditions.append_globaltag(tag)
68 print(f"Appending GTs:\n{args.global_tag_append}")
69
70 path = b2.create_path()
71
72 # ----------
73 # Add input.
74 # ----------
75
76 ma.inputMdst(environmentType="default",
77 filename=b2.find_file("mdst14.root", "validation"),
78 entrySequence="0:10000",
79 path=path)
80
81 # ----------------------------------------------
82 # Define preselected particle lists for leptons.
83 # ----------------------------------------------
84
85 # For electrons, we show the case in which a Bremsstrahlung correction
86 # is applied first to get the 4-momentum right,
87 # and the resulting particle list is passed as input to the stdE list creator.
88 ma.fillParticleList("e+:uncorrected",
89 cut="dr < 2 and abs(dz) < 4", # NB: whichever cut is set here, will be inherited by the std electrons.
90 path=path)
91 ma.fillParticleList("gamma:bremsinput",
92 cut="E < 1.0",
93 path=path)
94 ma.correctBremsBelle(outputListName="e+:corrected",
95 inputListName="e+:uncorrected",
96 gammaListName="gamma:bremsinput",
97 path=path)
98
99 ma.fillParticleList("mu+:presel",
100 cut="dr < 2 and abs(dz) < 4", # NB: whichever cut is set here, will be inherited by the std muons.
101 path=path)
102
103 # -------------------------------------------------------------
104 # Calculate track isolation variables on the preselected lists.
105 # NB: this must be done *before* the sdtLep lists are defined!
106 # -------------------------------------------------------------
107
108 # Reference list for isolation variables' calculation.
109 ma.fillParticleList("pi+:ref", "inCDCAcceptance", path=path)
110
111 # Define alias for isolation score. Needed to get the correct bin in the payload.
112 vm.addAlias("minET2ETIsoScore", "minET2ETIsoScore(pi+:ref, 1, CDC, TOP, ARICH, ECL, KLM)")
113
114 _ = ma.calculateTrackIsolation("e+:corrected",
115 path,
116 *["CDC", "TOP", "ARICH", "ECL", "KLM"],
117 reference_list_name="pi+:ref")
118
119 _ = ma.calculateTrackIsolation("mu+:presel",
120 path,
121 *["CDC", "TOP", "ARICH", "ECL", "KLM"],
122 reference_list_name="pi+:ref")
123
124 # ----------------------------------
125 # Fill example standard lepton list.
126 # ----------------------------------
127
128 electrons_fixed09 = "lh_B_fixed09"
129 electrons_wp = "FixedThresh09"
130 electron_id_var, electron_id_weights = stdE(electrons_wp, "likelihood", "binary", args.lid_weights_gt,
131 release=args.release,
132 inputListName="e+:corrected",
133 outputListLabel=electrons_fixed09,
134 path=path)
135
136 muons_uniform90 = "bdt_G_uniform90"
137 muons_wp = "UniformEff90"
138 muon_id_var, muon_id_weights = stdMu(muons_wp, "bdt", "global", args.lid_weights_gt,
139 release=args.release,
140 inputListName="mu+:presel",
141 outputListLabel=muons_uniform90,
142 path=path)
143
144 # --------------------------------------------
145 # Add extra cuts on the standard lepton lists.
146 # --------------------------------------------
147
148 ma.applyCuts(f"e-:{electrons_fixed09}", "[pt > 0.1] and thetaInCDCAcceptance", path=path)
149 ma.applyCuts(f"mu-:{muons_uniform90}", "[pt > 0.1] and thetaInCDCAcceptance", path=path)
150
151 # --------------------------------------------------
152 # Reconstruct J/psi candidates from the std leptons.
153 # --------------------------------------------------
154
155 jpsiee = f"J/psi:ee -> e+:{electrons_fixed09} e-:{electrons_fixed09}"
156 jpsimumu = f"J/psi:mumu -> mu+:{muons_uniform90} mu-:{muons_uniform90}"
157
158 jpsi_cuts = [
159 "[2.8 < M < 3.3]",
160 "[daughterSumOf(charge) == 0]",
161 ]
162 jpsi_cut = " and ".join(jpsi_cuts)
163
164 ma.reconstructDecay(jpsiee, jpsi_cut, path=path)
165 ma.reconstructDecay(jpsimumu, jpsi_cut, path=path)
166
167 # ------------------------------------------
168 # Create some example variable aliases
169 # for the mother particle and the daughters.
170 # ------------------------------------------
171
172 variables_jpsi = []
173 variables_e = []
174 variables_mu = []
175
176 variables_jpsi += vc.kinematics
177 variables_jpsi += vc.inv_mass
178
179 variables_e += (vc.kinematics + ["theta", "charge", "minET2ETIsoScore"])
180 variables_mu += (vc.kinematics + ["theta", "charge", "minET2ETIsoScore"])
181
182 cms_kinematics = vu.create_aliases(vc.kinematics, "useCMSFrame({variable})", "CMS")
183
184 variables_e += cms_kinematics
185 variables_mu += cms_kinematics
186
187 lid_e = [electron_id_var] + electron_id_weights
188 variables_e += lid_e
189
190 lid_mu = [muon_id_var] + muon_id_weights
191 variables_mu += lid_mu
192
193 aliases_jpsiee = vu.create_aliases_for_selected(
194 variables_jpsi,
195 f"^J/psi:ee -> e+:{electrons_fixed09} e-:{electrons_fixed09}",
196 prefix=["jpsi"])
197 aliases_jpsimumu = vu.create_aliases_for_selected(
198 variables_jpsi,
199 f"^J/psi:mumu -> mu+:{muons_uniform90} mu-:{muons_uniform90}",
200 prefix=["jpsi"])
201
202 aliases_e = vu.create_aliases_for_selected(
203 variables_e,
204 f"J/psi:ee -> ^e+:{electrons_fixed09} ^e-:{electrons_fixed09}",
205 use_names=True)
206 aliases_mu = vu.create_aliases_for_selected(
207 variables_mu,
208 f"J/psi:mumu -> ^mu+:{muons_uniform90} ^mu-:{muons_uniform90}",
209 use_names=True)
210
211 vm.printAliases()
212
213 output_file = "jpsill_LID_weights.root"
214
215 # Saving variables to ntuple
216 ma.variablesToNtuple(decayString="J/psi:ee",
217 variables=aliases_jpsiee+aliases_e,
218 treename="jpsiee",
219 filename=output_file,
220 path=path)
221 ma.variablesToNtuple(decayString="J/psi:mumu",
222 variables=aliases_jpsimumu+aliases_mu,
223 treename="jpsimumu",
224 filename=output_file,
225 path=path)
226
227 # Process the events.
228 b2.process(path)
229
230 # Print out the summary.
231 print(b2.statistics)
232
233
234if __name__ == "__main__":
235
236 main()
237
238# @endcond
Definition: main.py:1