Belle II Software  release-06-02-00
039_various_additions.py
1 #!/usr/bin/env python3
2 
3 import sys
4 import basf2 as b2
5 import modularAnalysis as ma
6 import stdV0s
7 from variables import variables as vm # shorthand for VariableManager
8 import variables.collections as vc
9 import variables.utils as vu
10 
11 # get input file number from the command line
12 filenumber = sys.argv[1]
13 
14 # create path
15 main = b2.Path()
16 
17 # load input data from mdst/udst file
18 ma.inputMdstList(
19  environmentType="default",
20  filelist=[b2.find_file(f"starterkit/2021/1111540100_eph3_BGx0_{filenumber}.root", "examples")],
21  path=main,
22 )
23 
24 # fill final state particle lists
25 ma.fillParticleList(
26  "e+:uncorrected",
27  "electronID > 0.1 and dr < 0.5 and abs(dz) < 2 and thetaInCDCAcceptance",
28  path=main,
29 )
30 stdV0s.stdKshorts(path=main)
31 
32 # apply Bremsstrahlung correction to electrons
33 vm.addAlias(
34  "goodFWDGamma", "passesCut(clusterReg == 1 and clusterE > 0.075)"
35 )
36 vm.addAlias(
37  "goodBRLGamma", "passesCut(clusterReg == 2 and clusterE > 0.05)"
38 )
39 vm.addAlias(
40  "goodBWDGamma", "passesCut(clusterReg == 3 and clusterE > 0.1)"
41 )
42 vm.addAlias(
43  "goodGamma", "passesCut(goodFWDGamma or goodBRLGamma or goodBWDGamma)"
44 )
45 ma.fillParticleList("gamma:brems", "goodGamma", path=main)
46 ma.correctBrems("e+:corrected", "e+:uncorrected", "gamma:brems", path=main)
47 vm.addAlias("isBremsCorrected", "extraInfo(bremsCorrected)")
48 
49 # combine final state particles to form composite particles
50 ma.reconstructDecay(
51  "J/psi:ee -> e+:corrected e-:corrected ?addbrems",
52  cut="dM < 0.11",
53  path=main,
54 )
55 
56 # combine J/psi and KS candidates to form B0 candidates
57 ma.reconstructDecay(
58  "B0 -> J/psi:ee K_S0:merged",
59  cut="Mbc > 5.2 and abs(deltaE) < 0.15",
60  path=main,
61 )
62 
63 # match reconstructed with MC particles
64 ma.matchMCTruth("B0", path=main)
65 
66 # build the rest of the event
67 ma.buildRestOfEvent("B0", fillWithMostLikely=True, path=main)
68 track_based_cuts = "thetaInCDCAcceptance and pt > 0.075 and dr < 5 and abs(dz) < 10"
69 ecl_based_cuts = "thetaInCDCAcceptance and E > 0.05"
70 roe_mask = ("my_mask", track_based_cuts, ecl_based_cuts)
71 ma.appendROEMasks("B0", [roe_mask], path=main)
72 
73 # perform best candidate selection
74 b2.set_random_seed("Belle II StarterKit")
75 ma.rankByHighest("B0", variable="random", numBest=1, path=main)
76 
77 # Create list of variables to save into the output file
78 b_vars = []
79 
80 standard_vars = vc.kinematics + vc.mc_kinematics + vc.mc_truth
81 b_vars += vc.deltae_mbc
82 b_vars += standard_vars
83 
84 # ROE variables
85 roe_kinematics = ["roeE()", "roeM()", "roeP()", "roeMbc()", "roeDeltae()"]
86 roe_multiplicities = [
87  "nROE_Charged()",
88  "nROE_Photons()",
89  "nROE_NeutralHadrons()",
90 ]
91 b_vars += roe_kinematics + roe_multiplicities
92 # Let's also add a version of the ROE variables that includes the mask:
93 for roe_variable in roe_kinematics + roe_multiplicities:
94  # e.g. instead of 'roeE()' (no mask) we want 'roeE(my_mask)'
95  roe_variable_with_mask = roe_variable.replace("()", "(my_mask)")
96  b_vars.append(roe_variable_with_mask)
97 
98 # Variables for final states (electrons, positrons, pions)
99 fs_vars = vc.pid + vc.track + vc.track_hits + standard_vars
100 b_vars += vu.create_aliases_for_selected(
101  fs_vars + ["isBremsCorrected"],
102  "B0 -> [J/psi -> ^e+ ^e-] K_S0",
103  prefix=["ep", "em"],
104 )
105 b_vars += vu.create_aliases_for_selected(
106  fs_vars, "B0 -> J/psi [K_S0 -> ^pi+ ^pi-]", prefix=["pip", "pim"]
107 )
108 # Variables for J/Psi, KS
109 jpsi_ks_vars = vc.inv_mass + standard_vars
110 b_vars += vu.create_aliases_for_selected(jpsi_ks_vars, "B0 -> ^J/psi ^K_S0")
111 # Add the J/Psi mass calculated with uncorrected electrons:
112 vm.addAlias(
113  "Jpsi_M_uncorrected", "daughter(0, daughterCombination(M,0:0,1:0))"
114 )
115 b_vars += ["Jpsi_M_uncorrected"]
116 # Also add kinematic variables boosted to the center of mass frame (CMS)
117 # for all particles
118 cmskinematics = vu.create_aliases(
119  vc.kinematics, "useCMSFrame({variable})", "CMS"
120 )
121 b_vars += vu.create_aliases_for_selected(
122  cmskinematics, "^B0 -> [^J/psi -> ^e+ ^e-] [^K_S0 -> ^pi+ ^pi-]"
123 )
124 
125 vm.addAlias(
126  "withBremsCorrection",
127  "passesCut(passesCut(ep_isBremsCorrected == 1) or passesCut(em_isBremsCorrected == 1))",
128 )
129 b_vars += ["withBremsCorrection"]
130 
131 # Save variables to an output file (ntuple)
132 ma.variablesToNtuple(
133  "B0",
134  variables=b_vars,
135  filename="Bd2JpsiKS.root",
136  treename="tree",
137  path=main,
138 )
139 
140 # Start the event loop (actually start processing things)
141 b2.process(main)
142 
143 # print out the summary
144 print(b2.statistics)
def stdKshorts(prioritiseV0=True, fitter='TreeFit', path=None)
Definition: stdV0s.py:17