Belle II Software  light-2212-foldex
B2A908-ApplyLIDWeights.py
1 #!/usr/bin/env python3
2 
3 
10 
11 # Doxygen should skip this script
12 # @cond
13 
14 """
15 Example to create lepton particle list and apply LeptonID corrections to a MC sample
16 by retrieving lookup tables from the conditions DB.
17 """
18 
19 
20 import argparse
21 
22 
23 def argparser():
24 
25  parser = argparse.ArgumentParser(description=__doc__,
26  formatter_class=argparse.RawTextHelpFormatter)
27 
28  parser.add_argument("--release",
29  type=int,
30  default=5,
31  help="The major release number associated to the corrections that are being applied.\n"
32  "Default: %(default)s.")
33  parser.add_argument("--lid_weights_gt",
34  type=str,
35  default="leptonid_Moriond2022_Official_rel5_v1a",
36  help="Name of conditions DB global tag with recommended lepton ID correction factors.\n"
37  "Default: %(default)s.")
38 
39  return parser
40 
41 
42 def main():
43  """
44  Main entry point allowing external calls.
45  """
46 
47  args = argparser().parse_args()
48 
49  import basf2 as b2
50  import modularAnalysis as ma
51  from variables import variables
52  import variables.utils as vu
53  import variables.collections as vc
54  from stdCharged import stdE, stdMu
55 
56  b2.set_log_level(b2.LogLevel.INFO)
57 
58  # Append the analysis GT. Needed to run the ChargedPidMVA BDT.
59  analysis_gt = ma.getAnalysisGlobaltag()
60  b2.B2INFO(f"Appending analysis GT: {analysis_gt}")
61  b2.conditions.append_globaltag(analysis_gt)
62 
63  path = b2.create_path()
64 
65  # ----------
66  # Add input.
67  # ----------
68 
69  ma.inputMdst(environmentType="default",
70  filename=b2.find_file("mdst13.root", "validation"),
71  entrySequence="0:10000",
72  path=path)
73 
74  # ----------------------------------
75  # Fill example standard lepton list.
76  # ----------------------------------
77 
78  # For electrons, we show the case in which a Bremsstrahlung correction
79  # is applied first to get the 4-momentum right,
80  # and the resulting particle list is passed as input to the stdE list creator.
81  ma.fillParticleList("e+:uncorrected",
82  cut="dr < 2 and abs(dz) < 4", # NB: whichever cut is set here, will be inherited by the std electrons.
83  path=path)
84  ma.fillParticleList("gamma:bremsinput",
85  cut="E < 1.0",
86  path=path)
87  ma.correctBremsBelle(outputListName="e+:corrected",
88  inputListName="e+:uncorrected",
89  gammaListName="gamma:bremsinput",
90  path=path)
91 
92  electrons_fixed09 = "lh_B_fixed09"
93  electrons_wp = "FixedThresh09"
94  electron_id_var, electron_id_weights = stdE(electrons_wp, "likelihood", "binary", args.lid_weights_gt,
95  release=args.release,
96  inputListName="e+:corrected",
97  outputListLabel=electrons_fixed09,
98  path=path)
99 
100  muons_uniform90 = "bdt_G_uniform90"
101  muons_wp = "UniformEff90"
102  muon_id_var, muon_id_weights = stdMu(muons_wp, "bdt", "global", args.lid_weights_gt,
103  release=args.release,
104  outputListLabel=muons_uniform90,
105  path=path)
106 
107  # --------------------------------------------
108  # Add extra cuts on the standard lepton lists.
109  # --------------------------------------------
110 
111  ma.applyCuts(f"e-:{electrons_fixed09}", "[pt > 0.1] and thetaInCDCAcceptance", path=path)
112  ma.applyCuts(f"mu-:{muons_uniform90}", "[pt > 0.1] and thetaInCDCAcceptance", path=path)
113 
114  # --------------------------------------------------
115  # Reconstruct J/psi candidates from the std leptons.
116  # --------------------------------------------------
117 
118  jpsiee = f"J/psi:ee -> e+:{electrons_fixed09} e-:{electrons_fixed09}"
119  jpsimumu = f"J/psi:mumu -> mu+:{muons_uniform90} mu-:{muons_uniform90}"
120 
121  jpsi_cuts = [
122  "[2.8 < M < 3.3]",
123  "[daughterSumOf(charge) == 0]",
124  ]
125  jpsi_cut = " and ".join(jpsi_cuts)
126 
127  ma.reconstructDecay(jpsiee, jpsi_cut, path=path)
128  ma.reconstructDecay(jpsimumu, jpsi_cut, path=path)
129 
130  # ------------------------------------------
131  # Create some example variable aliases
132  # for the mother particle and the daughters.
133  # ------------------------------------------
134 
135  variables_jpsi = []
136  variables_e = []
137  variables_mu = []
138 
139  variables_jpsi += vc.kinematics
140  variables_jpsi += vc.inv_mass
141 
142  variables_e += vc.kinematics
143  variables_mu += vc.kinematics
144 
145  cms_kinematics = vu.create_aliases(vc.kinematics, "useCMSFrame({variable})", "CMS")
146 
147  variables_e += cms_kinematics
148  variables_mu += cms_kinematics
149 
150  lid_e = [electron_id_var] + electron_id_weights
151  variables_e += lid_e
152 
153  lid_mu = [muon_id_var] + muon_id_weights
154  variables_mu += lid_mu
155 
156  aliases_jpsiee = vu.create_aliases_for_selected(
157  variables_jpsi,
158  f"^J/psi:ee -> e+:{electrons_fixed09} e-:{electrons_fixed09}",
159  prefix=["jpsi"])
160  aliases_jpsimumu = vu.create_aliases_for_selected(
161  variables_jpsi,
162  f"^J/psi:mumu -> mu+:{muons_uniform90} mu-:{muons_uniform90}",
163  prefix=["jpsi"])
164 
165  aliases_e = vu.create_aliases_for_selected(
166  variables_e,
167  f"J/psi:ee -> ^e+:{electrons_fixed09} ^e-:{electrons_fixed09}",
168  use_names=True)
169  aliases_mu = vu.create_aliases_for_selected(
170  variables_mu,
171  f"J/psi:mumu -> ^mu+:{muons_uniform90} ^mu-:{muons_uniform90}",
172  use_names=True)
173 
174  variables.printAliases()
175 
176  output_file = "jpsill_LID_weights.root"
177 
178  # Saving variables to ntuple
179  ma.variablesToNtuple(decayString="J/psi:ee",
180  variables=aliases_jpsiee+aliases_e,
181  treename="jpsiee",
182  filename=output_file,
183  path=path)
184  ma.variablesToNtuple(decayString="J/psi:mumu",
185  variables=aliases_jpsimumu+aliases_mu,
186  treename="jpsimumu",
187  filename=output_file,
188  path=path)
189 
190  # Process the events.
191  b2.process(path)
192 
193  # Print out the summary.
194  print(b2.statistics)
195 
196 
197 if __name__ == "__main__":
198 
199  main()
int main(int argc, char **argv)
Run all tests.
Definition: test_main.cc:75