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