Belle II Software  release-08-01-10
klm_trig_linear_fit.cc
1 /**************************************************************************
2  * basf2 (Belle II Analysis Software Framework) *
3  * Author: The Belle II Collaboration *
4  * *
5  * See git log for contributors and copyright holders. *
6  * This file is licensed under LGPL-3.0, see LICENSE.md. *
7  **************************************************************************/
8 
9 
10 
11 #include "trg/klm/modules/klmtrigger/klm_trig_linear_fit.h"
12 #include "trg/klm/modules/klmtrigger/group_helper.h"
13 #include "trg/klm/modules/klmtrigger/KLMAxis.h"
14 
15 
16 
17 AXIS_NAME(x_pos, int);
18 AXIS_NAME(y_pos, int);
19 
20 using namespace Belle2::group_helper::KLM_Coordinates_n;
21 using namespace Belle2::group_helper::KLM_Generic;
22 using namespace Belle2::group_helper;
23 namespace Belle2 {
32  template <typename Container_T>
33  auto operator()(const Container_T& container) const
34  {
35  int64_t sumX = 0, sumY = 0, sumXX = 0, sumXY = 0, sumYY = 0;
36  for (const auto& e : container) {
37  sumX += x_pos(e);
38  sumXX += x_pos(e) * x_pos(e);
39 
40  sumY += y_pos(e);
41  sumYY += y_pos(e) * y_pos(e);
42 
43  sumXY += x_pos(e) * y_pos(e);
44  }
45  auto nHits = Nhits_t(container.size());
46  int64_t denom = sumXX * nHits - sumX * sumX;
47  if (denom == 0) {
48  return std::tuple(slopeXY_t(1e100), interceptXY_t(1e100), ipXY_t(1e100), chisqXY_t(1e100), nHits);
49  }
50  auto slopeXY = slopeXY_t((double)(sumXY * nHits - sumX * sumY) / (double)denom);
51  auto interceptXY = interceptXY_t((double)(sumXX * sumY - sumX * sumXY) / (double)denom);
52 
53  auto ipXY = ipXY_t(interceptXY * interceptXY * (1.0 - slopeXY * slopeXY));
54 
55  auto chisqXY = chisqXY_t(slopeXY * slopeXY * sumXX
56  + interceptXY * interceptXY * nHits
57  + sumYY
58  + 2.0 * slopeXY * interceptXY * sumX
59  - 2.0 * slopeXY * sumXY
60  - 2.0 * interceptXY * sumY);
61 
62  return std::tuple(slopeXY, interceptXY, ipXY, chisqXY, nHits) ;
63  }
64  };
65  constexpr Linear_fit_of_Hits_t Linear_fit_of_Hits;
66 
67 
68 
69 
70 
71 
72 
73 
74  std::vector<KLM_trig_linear_fit> klm_trig_linear_fit(std::vector<Belle2::group_helper::KLM_Digit_compact>& hits,
75  const std::vector<Belle2::group_helper::KLM_geo_fit_t>& geometry)
76  {
77  sort(hits);
78 
79  auto hits_w_geo_fit = vec_join(
80  hits,
81  geometry,
82  comparators::on_common_args,
83  [&](const auto & e1, const auto & e2) {
84  return std::tuple(
85  event_nr(e1),
86  Subdetector(e1),
87  section(e1),
88  sector(e1),
89  plane(e1),
90  layer(e1),
91  strip(e1),
92  x_pos(layer(e1) * geofit::slopeX(e2) + geofit::offsetX(e2)),
93  y_pos(strip(e1) * geofit::slopeY(e2) + geofit::offsetY(e2))
94  );
95  }
96 
97  );
98 
99  sort(hits_w_geo_fit);
100  auto linear_fited = group<event_nr, Subdetector, section, sector, plane>::apply(hits_w_geo_fit,
101  Linear_fit_of_Hits
102  );
103  sort(linear_fited);
104  return linear_fited;
105 
106  }
107 
109 }
Abstract base class for different kinds of events.