Belle II Software  release-08-00-04
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 
28  """Collects variables of interest for the V0Validation and the v0ValidationCreatePlots script."""
29 
30  def __init__(self):
31  """Initialize the harvester.
32  Defines over which StoreArray is iterated and the output file.
33  """
34  HarvestingModule.__init__(self, foreach="MCParticles", output_file_name="../V0ValidationHarvested.root")
35 
36  def pick(self, mc_particle):
37  """Selects all MCParticles which are KShort AND decay to Pi+Pi-.
38 
39  :param mc_particle: Belle2::MCParticle.
40  :return: True if the MCParticle is a KShort decaying to two charged pions.
41  """
42  if abs(mc_particle.getPDG()) != 310:
43  return False
44  daughters = mc_particle.getDaughters()
45  return len(daughters) == 2 and abs(daughters[0].getPDG()) == 211 and abs(daughters[1].getPDG()) == 211
46 
47  def peel(self, mc):
48  """Selects MCTrue variables of interest for all KShort in the sample. If the KShort has a related reconstructed
49  V0, these values are written out too. Variables of interest are:
50  R: Radial (in xy) distance to origin.
51  Theta: Theta Angle of decay vertex.
52  Phi: Phi Angle of decay vertex.
53  P: Momentum of the KShort.
54  M: Invariant mass of the KShort.
55  Chi2: Chi2 of vertex fit.
56  isFound: True if MCParticle has a related V0.
57 
58  If the MCParticle has no related V0, the variables are filled with NaN's.
59 
60  :param mc: Belle2::MCParticle
61  :return: dict with the variables of interest.
62  """
63  mc_vertex = mc.getDecayVertex()
64  mc_perp = mc_vertex.Rho()
65  mc_theta = mc_vertex.Theta()
66  mc_phi = mc_vertex.Phi()
67  mc_m = mc.getMass()
68  mc_p = mc.getMomentum().R()
69 
70  v0 = mc.getRelated("V0ValidationVertexs")
71 
72  if v0:
73  v0_vertex = v0.getVertexPosition()
74  v0_perp = v0_vertex.Rho()
75  v0_theta = v0_vertex.Theta()
76  v0_phi = v0_vertex.Phi()
77  v0_m = v0.getFittedInvariantMass()
78  v0_p = v0.getFittedMomentum()
79  v0_chi2 = v0.getVertexChi2()
80 
81  return {
82  "R": v0_perp,
83  "R_MC": mc_perp,
84  "THETA": v0_theta,
85  "THETA_MC": mc_theta,
86  "PHI": v0_phi,
87  "PHI_MC": mc_phi,
88  "P": v0_p,
89  "P_MC": mc_p,
90  "M": v0_m,
91  "M_MC": mc_m,
92  "CHI2": v0_chi2,
93  "FOUND": True
94  }
95 
96  else:
97  return {
98  "R": numpy.NaN,
99  "R_MC": mc_perp,
100  "THETA": numpy.NaN,
101  "THETA_MC": mc_theta,
102  "PHI": numpy.NaN,
103  "PHI_MC": mc_phi,
104  "P": numpy.NaN,
105  "P_MC": mc_p,
106  "M": numpy.NaN,
107  "M_MC": mc_m,
108  "CHI2": numpy.NaN,
109  "FOUND": False
110  }
111 
112 
114  save_tree = SaveTreeRefiner()
115 
116 
117 basf2.set_random_seed(1337)
118 path = basf2.create_path()
119 
120 path.add_module('RootInput', inputFileName='../KShortGenSimNoBkg.root')
121 path.add_module('Gearbox')
122 
123 add_tracking_reconstruction(path)
124 
125 # Set options for V0 Validation
126 for module in path.modules():
127  if module.name() == "V0Finder":
128  module.param("Validation", True)
129 path.add_module('MCV0Matcher', V0ColName='V0ValidationVertexs')
130 path.add_module(V0Harvester())
131 
132 path.add_module("Progress")
133 
134 basf2.process(path)
135 print(basf2.statistics)