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