Belle II Software  release-05-01-25
riemannFitToy.py
1 import numpy as np
2 import numpy.random
3 
4 import matplotlib as mpl
5 mpl.use('Agg')
6 import matplotlib.pyplot as plt
7 
8 import basf2
9 
10 from ROOT import gSystem
11 gSystem.Load('libframework')
12 gSystem.Load('libtracking')
13 gSystem.Load('libtracking_trackFindingCDC')
14 
15 from ROOT import Belle2 # make Belle2 namespace available
16 
17 from tracking.validation.utilities import prob
18 
19 import logging
20 
21 
22 def get_logger():
23  return logging.getLogger(__name__)
24 
25 CONTACT = "oliver.frost@desy.de"
26 
27 n_toys = 10000
28 
29 radius = 1
30 center_x = 1
31 center_y = 1
32 center_phi = np.arctan2(center_y, center_x)
33 
34 n_points = 50
35 
36 # Position variance
37 pos_var = 0.0001
38 
39 # Reconstruction variance
40 pos_var = 0.0001
41 
42 chi2s = []
43 curvature_estimates = []
44 curvature_variances = []
45 
46 
47 def main():
48  for i_toy in range(n_toys):
49  chi_random = False
50  if chi_random:
51  chis = np.random.uniform(0, np.pi, n_points)
52  else:
53  # equally spaced points
54  chis = np.linspace(0, np.pi, n_points)
55 
56  ls = np.random.normal(0, 10 * np.sqrt(pos_var), n_points)
57  dls = np.random.normal(0, np.sqrt(pos_var), n_points)
58 
59  xs = (radius + ls + dls) * np.cos(chis + center_phi - np.pi) + center_x
60  ys = (radius + ls + dls) * np.sin(chis + center_phi - np.pi) + center_y
61 
63 
64  for x, y, l in zip(xs, ys, ls):
65  weight = 1 / pos_var
66  observations.fill(x, y, l, weight)
67 
69  trajectory = fitter.fit(observations)
70 
71  circle = trajectory.getGlobalCircle()
72  if circle.curvature() < 0:
73  circle.reverse()
74 
75  curvature_estimates.append(circle.curvature())
76  curvature_variances.append(trajectory.getLocalVariance(0))
77  chi2s.append(trajectory.getChi2())
78 
79  circle_points = [circle.atArcLength(chi * radius) for chi in np.linspace(0, np.pi, 50)]
80  circle_xs = [p.x() for p in circle_points]
81  circle_ys = [p.y() for p in circle_points]
82 
83  continue
84  plt.plot(circle_xs, circle_ys)
85  plt.scatter(xs, ys)
86 
87  plt.show()
88 
89  curvature_residuals = np.array(curvature_estimates) - 1 / radius
90 
91  plt.hist(curvature_residuals, bins=100)
92  plt.title("Curvature residual")
93  plt.show()
94 
95  plt.hist(curvature_residuals / np.sqrt(curvature_variances), bins=100, normed=True)
96 
97  normal_xs = np.linspace(-4, 4, 50)
98  normal_ys = np.exp(-normal_xs * normal_xs / 2) / np.sqrt(2 * np.pi)
99  plt.plot(normal_xs, normal_ys)
100  plt.title("Curvature pull")
101  plt.show()
102 
103  plt.hist(chi2s, bins=100)
104  plt.show()
105 
106  ndfs = n_points - 3
107  p_values = prob(chi2s, ndfs)
108  plt.hist(p_values, bins=100, normed=True)
109  plt.plot([0, 1], [1, 1])
110  plt.show()
111 
112 
113 if __name__ == "__main__":
114  main()
main
int main(int argc, char **argv)
Run all tests.
Definition: test_main.cc:77
Belle2::TrackFindingCDC::CDCObservations2D
Class serving as a storage of observed drift circles to present to the Riemann fitter.
Definition: CDCObservations2D.h:53
Belle2::TrackFindingCDC::CDCRiemannFitter::getFitter
static const CDCRiemannFitter & getFitter()
Static getter for a general Riemann fitter.
Definition: CDCRiemannFitter.cc:22
tracking.validation.utilities
Definition: utilities.py:1