Belle II Software  release-08-01-10
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 
20 import basf2
21 from tracking import add_tracking_reconstruction
22 from tracking.harvest.harvesting import HarvestingModule
23 from tracking.harvest.refiners import SaveTreeRefiner
24 import numpy
25 
26 ACTIVE = True
27 
28 
29 def 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 
66  :param mc: Belle2::MCParticle
67  :return: dict with the variables of interest.
68  """
69  mc_vertex = mc.getDecayVertex()
70  mc_perp = mc_vertex.Rho()
71  mc_theta = mc_vertex.Theta()
72  mc_phi = mc_vertex.Phi()
73  mc_m = mc.getMass()
74  mc_p = mc.getMomentum().R()
75 
76  v0 = mc.getRelated("V0ValidationVertexs")
77 
78  if v0:
79  v0_vertex = v0.getVertexPosition()
80  v0_perp = v0_vertex.Rho()
81  v0_theta = v0_vertex.Theta()
82  v0_phi = v0_vertex.Phi()
83  v0_m = v0.getFittedInvariantMass()
84  v0_p = v0.getFittedMomentum()
85  v0_chi2 = v0.getVertexChi2()
86 
87  return {
88  "R": v0_perp,
89  "R_MC": mc_perp,
90  "THETA": v0_theta,
91  "THETA_MC": mc_theta,
92  "PHI": v0_phi,
93  "PHI_MC": mc_phi,
94  "P": v0_p,
95  "P_MC": mc_p,
96  "M": v0_m,
97  "M_MC": mc_m,
98  "CHI2": v0_chi2,
99  "FOUND": True
100  }
101 
102  else:
103  return {
104  "R": numpy.NaN,
105  "R_MC": mc_perp,
106  "THETA": numpy.NaN,
107  "THETA_MC": mc_theta,
108  "PHI": numpy.NaN,
109  "PHI_MC": mc_phi,
110  "P": numpy.NaN,
111  "P_MC": mc_p,
112  "M": numpy.NaN,
113  "M_MC": mc_m,
114  "CHI2": numpy.NaN,
115  "FOUND": False
116  }
117 
118 
120  save_tree = SaveTreeRefiner()
121 
122  basf2.set_random_seed(1337)
123  path = basf2.create_path()
124 
125  path.add_module('RootInput', inputFileName='../KShortGenSimNoBkg.root')
126  path.add_module('Gearbox')
127 
128  add_tracking_reconstruction(path)
129 
130  # Set options for V0 Validation
131  for module in path.modules():
132  if module.name() == "V0Finder":
133  module.param("Validation", True)
134  path.add_module('MCV0Matcher', V0ColName='V0ValidationVertexs')
135  path.add_module(V0Harvester())
136 
137  path.add_module("Progress")
138 
139  basf2.process(path)
140  print(basf2.statistics)
141 
142 
143 if __name__ == '__main__':
144  if ACTIVE:
145  run()
146  else:
147  print("This validation deactivated and thus basf2 is not executed.\n"
148  "If you want to run this validation, please set the 'ACTIVE' flag above to 'True'.\n"
149  "Exiting.")