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