Belle II Software development
v0ValidationGenerateSample.py
1#!/usr/bin/env python3
2
3
10
11"""
12<header>
13 <contact>software-tracking@belle2.org</contact>
14 <input>KShortGenSimNoBkg.root</input>
15 <output>V0ValidationSample.root, V0ValidationHarvested.root</output>
16 <description>This module generates events for the V0 validation.</description>
17</header>
18"""
19
20import basf2
21from tracking import add_tracking_reconstruction
22from tracking.harvest.harvesting import HarvestingModule
23from tracking.harvest.refiners import SaveTreeRefiner
24import numpy
25
26ACTIVE = True
27
28
29def run():
30 """
31 Validate Kshort reconstruction efficiency and collect all the necessary data.
32 """
33 class V0Harvester(HarvestingModule):
34 """Collects variables of interest for the V0Validation and the v0ValidationCreatePlots script."""
35
36 def __init__(self):
37 """Initialize the harvester.
38 Defines over which StoreArray is iterated and the output file.
39 """
40 HarvestingModule.__init__(self, foreach="MCParticles", output_file_name="../V0ValidationHarvested.root")
41
42 def pick(self, mc_particle):
43 """Selects all MCParticles which are KShort AND decay to Pi+Pi-.
44
45 :param mc_particle: Belle2::MCParticle.
46 :return: True if the MCParticle is a KShort decaying to two charged pions.
47 """
48 if abs(mc_particle.getPDG()) != 310:
49 return False
50 daughters = mc_particle.getDaughters()
51 return len(daughters) == 2 and abs(daughters[0].getPDG()) == 211 and abs(daughters[1].getPDG()) == 211
52
53 def peel(self, mc):
54 """Selects MCTrue variables of interest for all KShort in the sample. If the KShort has a related reconstructed
55 V0, these values are written out too. Variables of interest are:
56 R: Radial (in xy) distance to origin.
57 Theta: Theta Angle of decay vertex.
58 Phi: Phi Angle of decay vertex.
59 P: Momentum of the KShort.
60 M: Invariant mass of the KShort.
61 Chi2: Chi2 of vertex fit.
62 isFound: True if MCParticle has a related V0.
63
64 If the MCParticle has no related V0, the variables are filled with NaN's.
65 :param mc: Belle2::MCParticle
66 :return: dict with the variables of interest.
67 """
68 mc_vertex = mc.getDecayVertex()
69 mc_perp = mc_vertex.Rho()
70 mc_theta = mc_vertex.Theta()
71 mc_phi = mc_vertex.Phi()
72 mc_m = mc.getMass()
73 mc_p = mc.getMomentum().R()
74
75 v0 = mc.getRelated("V0ValidationVertexs")
76
77 if v0:
78 v0_vertex = v0.getVertexPosition()
79 v0_perp = v0_vertex.Rho()
80 v0_theta = v0_vertex.Theta()
81 v0_phi = v0_vertex.Phi()
82 v0_m = v0.getFittedInvariantMass()
83 v0_p = v0.getFittedMomentum()
84 v0_chi2 = v0.getVertexChi2()
85
86 return {
87 "R": v0_perp,
88 "R_MC": mc_perp,
89 "THETA": v0_theta,
90 "THETA_MC": mc_theta,
91 "PHI": v0_phi,
92 "PHI_MC": mc_phi,
93 "P": v0_p,
94 "P_MC": mc_p,
95 "M": v0_m,
96 "M_MC": mc_m,
97 "CHI2": v0_chi2,
98 "FOUND": True
99 }
100
101 else:
102 return {
103 "R": numpy.NaN,
104 "R_MC": mc_perp,
105 "THETA": numpy.NaN,
106 "THETA_MC": mc_theta,
107 "PHI": numpy.NaN,
108 "PHI_MC": mc_phi,
109 "P": numpy.NaN,
110 "P_MC": mc_p,
111 "M": numpy.NaN,
112 "M_MC": mc_m,
113 "CHI2": numpy.NaN,
114 "FOUND": False
115 }
116
117
119 save_tree = SaveTreeRefiner()
120
121 basf2.set_random_seed(1337)
122 path = basf2.create_path()
123
124 path.add_module('RootInput', inputFileName='../KShortGenSimNoBkg.root')
125 path.add_module('Gearbox')
126
127 add_tracking_reconstruction(path)
128
129 # Set options for V0 Validation
130 for module in path.modules():
131 if module.name() == "V0Finder":
132 module.param("Validation", True)
133 path.add_module('MCV0Matcher', V0ColName='V0ValidationVertexs')
134 path.add_module(V0Harvester())
135
136 path.add_module("Progress")
137
138 basf2.process(path)
139 print(basf2.statistics)
140
141
142if __name__ == '__main__':
143 if ACTIVE:
144 run()
145 else:
146 print("This validation deactivated and thus basf2 is not executed.\n"
147 "If you want to run this validation, please set the 'ACTIVE' flag above to 'True'.\n"
148 "Exiting.")
149