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