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