Belle II Software  release-08-01-10
BasicTrackVarSet.test.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 #include <gtest/gtest.h>
9 #include <framework/utilities/TestHelpers.h>
10 #include <tracking/trackFindingCDC/testFixtures/TrackFindingCDCTestWithTopology.h>
11 
12 #include <tracking/trackFindingCDC/filters/track/BasicTrackVarSet.h>
13 #include <tracking/trackFindingCDC/eventdata/tracks/CDCTrack.h>
14 
15 #include <cdc/dataobjects/CDCHit.h>
16 #include <cdc/dataobjects/WireID.h>
17 #include <tracking/trackFindingCDC/eventdata/hits/CDCRLWireHit.h>
18 #include <tracking/trackFindingCDC/eventdata/hits/CDCWireHit.h>
19 #include <tracking/trackFindingCDC/topology/CDCWireTopology.h>
20 #include <tracking/trackFindingCDC/geometry/Vector3D.h>
21 
22 #include <vector>
23 
24 using namespace Belle2;
25 using namespace TrackFindingCDC;
26 
27 TEST(BasicTrackVarSetCalculation, test_empty_track)
28 {
29  // Define mean variables to be 0 for empty track. Default of boost accumulator library is NAN, but
30  // that the BDT cannot train on. Should be converted to 0 via the toFinite function.
31 
32  // create an empty track containing no hits
33  const CDCTrack emptyTrack;
34  // extract variables from emtpy track
35  BasicTrackVarSet trackVarSet;
36 
37  // returnvalue should be false
38  bool returnvalue = trackVarSet.extract(&emptyTrack);
39  EXPECT_EQ(false, returnvalue);
40 
41  // for empty tracks the extraction calculations are not performed
42  // thus all variables should be 0
43  EXPECT_EQ(0, *trackVarSet.find("size"));
44  EXPECT_EQ(0, *trackVarSet.find("pt"));
45 
46  EXPECT_EQ(0, *trackVarSet.find("drift_length_mean"));
47  EXPECT_EQ(0, *trackVarSet.find("drift_length_sum"));
48  EXPECT_EQ(0, *trackVarSet.find("drift_length_min"));
49  EXPECT_EQ(0, *trackVarSet.find("drift_length_max"));
50  EXPECT_EQ(0, *trackVarSet.find("drift_length_variance"));
51 
52  EXPECT_EQ(0, *trackVarSet.find("adc_mean"));
53  EXPECT_EQ(0, *trackVarSet.find("adc_sum"));
54  EXPECT_EQ(0, *trackVarSet.find("adc_min"));
55  EXPECT_EQ(0, *trackVarSet.find("adc_max"));
56  EXPECT_EQ(0, *trackVarSet.find("adc_variance"));
57 
58  EXPECT_EQ(0, *trackVarSet.find("empty_s_mean"));
59  EXPECT_EQ(0, *trackVarSet.find("empty_s_sum"));
60  EXPECT_EQ(0, *trackVarSet.find("empty_s_min"));
61  EXPECT_EQ(0, *trackVarSet.find("empty_s_max"));
62  EXPECT_EQ(0, *trackVarSet.find("empty_s_variance"));
63  EXPECT_EQ(0, *trackVarSet.find("s_range"));
64 }
65 
66 TEST_F(TrackFindingCDCTestWithTopology, basicTrackVarSet_test_one_hit_track)
67 {
68  // create a dummy track with only one hit
69  const CDCWireTopology& wireTopology = CDCWireTopology::getInstance();
70 
71  const WireID& aWireID = wireTopology.getWire(0, 0, 0).getWireID();
72  double driftLength = 2.0;
73  unsigned short adcCount = 512.0;
74  double arcLength2D = 0.3;
75  const CDCHit aHit(128, adcCount, aWireID);
76  const CDCWireHit aWireHit(&aHit, driftLength);
77  const CDCRLWireHit aRLWireHit(&aWireHit, ERightLeft::c_Unknown);
78  double some_arbitrary_z_coord = 0.0;
79  const Vector3D aRecoPos(aRLWireHit.getRefPos2D(), some_arbitrary_z_coord);
80  const CDCRecoHit3D aCDCHit(aRLWireHit, aRecoPos, arcLength2D);
81  const std::vector<CDCRecoHit3D> aHitVector{aCDCHit};
82  const CDCTrack track(aHitVector);
83 
84  // extrack variables from track
85  BasicTrackVarSet trackVarSet;
86 
87  trackVarSet.extract(&track);
88 
89  EXPECT_EQ(1, *trackVarSet.find("size"));
90 
91  EXPECT_EQ(driftLength, *trackVarSet.find("drift_length_mean"));
92  EXPECT_EQ(driftLength, *trackVarSet.find("drift_length_sum"));
93  EXPECT_EQ(driftLength, *trackVarSet.find("drift_length_min"));
94  EXPECT_EQ(driftLength, *trackVarSet.find("drift_length_max"));
95  // variance not calculatable, define it to be -1
96  EXPECT_EQ(-1, *trackVarSet.find("drift_length_variance"));
97 
98  EXPECT_EQ(adcCount, *trackVarSet.find("adc_mean"));
99  EXPECT_EQ(adcCount, *trackVarSet.find("adc_sum"));
100  EXPECT_EQ(adcCount, *trackVarSet.find("adc_min"));
101  EXPECT_EQ(adcCount, *trackVarSet.find("adc_max"));
102  // variance not calculatable, define it to be -1
103  EXPECT_EQ(-1, *trackVarSet.find("adc_variance"));
104 
105  // with only one arc length (s), there are no gaps in s, thus empty_s variables empty
106  EXPECT_EQ(0, *trackVarSet.find("empty_s_mean"));
107  EXPECT_EQ(0, *trackVarSet.find("empty_s_sum"));
108  EXPECT_EQ(0, *trackVarSet.find("empty_s_min"));
109  EXPECT_EQ(0, *trackVarSet.find("empty_s_max"));
110  EXPECT_EQ(0, *trackVarSet.find("s_range"));
111  // variance not calculatable, define it to be -1
112  EXPECT_EQ(-1, *trackVarSet.find("empty_s_variance"));
113 }
114 
115 TEST_F(TrackFindingCDCTestWithTopology, basicTrackVarSet_test_two_hit_track)
116 {
117 
118  // create a dummy track with only one hit
119  const CDCWireTopology& wireTopology = CDCWireTopology::getInstance();
120 
121  const WireID& aWireID = wireTopology.getWire(0, 0, 0).getWireID();
122  const WireID& bWireID = wireTopology.getWire(0, 0, 1).getWireID();
123  std::vector<WireID> wireIDs = {aWireID, bWireID};
124 
125  std::vector<double> drift_lengths = {2.0, 3.0};
126  // large ADC counts might cause errors in calculation due to short data type
127  // for example when squaring to get the variance
128  std::vector<unsigned short> adc_counts = {30, 500};
129  std::vector<double> arc_length_2Ds = {2.0, -3.0};
130 
131  std::vector<CDCHit> cdcHits;
132  std::vector<CDCWireHit> cdcWireHits;
133  std::vector<CDCRecoHit3D> cdcRecoHits;
134 
135  for (size_t i = 0; i < drift_lengths.size(); i++) {
136  cdcHits.emplace_back(128, adc_counts.at(i), wireIDs.at(i)); // store in vector to avoid nullptrs
137  cdcWireHits.emplace_back(&cdcHits.at(i), drift_lengths.at(i));
138  CDCRLWireHit aRLWireHit(&cdcWireHits.at(i), ERightLeft::c_Unknown);
139  Vector3D aRecoPos(aRLWireHit.getRefPos2D(), 0.0);
140  cdcRecoHits.emplace_back(aRLWireHit, aRecoPos, arc_length_2Ds.at(i));
141  }
142 
143  const CDCTrack track(cdcRecoHits);
144 
145  // extrack variables from track
146  BasicTrackVarSet trackVarSet;
147  trackVarSet.extract(&track);
148 
149  EXPECT_EQ(2, *trackVarSet.find("size"));
150 
151  EXPECT_EQ(2.5, *trackVarSet.find("drift_length_mean"));
152  EXPECT_EQ(5.0, *trackVarSet.find("drift_length_sum"));
153  EXPECT_EQ(2.0, *trackVarSet.find("drift_length_min"));
154  EXPECT_EQ(3.0, *trackVarSet.find("drift_length_max"));
155  // variance from python: np.sqrt(np.var([2, 3])*2/(2-1))
156  EXPECT_NEAR(0.7071, *trackVarSet.find("drift_length_variance"), 1e-4);
157 
158  EXPECT_EQ(265, *trackVarSet.find("adc_mean"));
159  EXPECT_EQ(530, *trackVarSet.find("adc_sum"));
160  EXPECT_EQ(30, *trackVarSet.find("adc_min"));
161  EXPECT_EQ(500, *trackVarSet.find("adc_max"));
162  EXPECT_NEAR(332.34, *trackVarSet.find("adc_variance"), 1e-2);
163 
164  // for empty_s, there is only one hit gap with two hits
165  double only_empty_s = -5.0;
166  EXPECT_EQ(only_empty_s, *trackVarSet.find("empty_s_mean"));
167  EXPECT_EQ(only_empty_s, *trackVarSet.find("empty_s_sum"));
168  EXPECT_EQ(only_empty_s, *trackVarSet.find("empty_s_min"));
169  EXPECT_EQ(only_empty_s, *trackVarSet.find("empty_s_max"));
170  EXPECT_EQ(only_empty_s, *trackVarSet.find("s_range"));
171  // variance not calculatable
172  EXPECT_EQ(-1, *trackVarSet.find("empty_s_variance"));
173 }
174 
175 TEST_F(TrackFindingCDCTestWithTopology, basicTrackVarSet_test_empty_s_for_three_hit_track)
176 {
177  // Just test the empty_s calculations also with three hits, which result in two empty_s values
178  const CDCWireTopology& wireTopology = CDCWireTopology::getInstance();
179 
180  const WireID& aWireID = wireTopology.getWire(0, 0, 0).getWireID();
181  const WireID& bWireID = wireTopology.getWire(0, 0, 1).getWireID();
182  const WireID& cWireID = wireTopology.getWire(0, 0, 2).getWireID();
183  std::vector<WireID> wireIDs = {aWireID, bWireID, cWireID};
184 
185  // -> empty_s hit gaps = {2, 3}
186  std::vector<double> arc_length_2Ds = { -2.0, 0.0, 3.0};
187 
188  std::vector<CDCHit> cdcHits;
189  cdcHits.reserve(wireIDs.size());
190  std::vector<CDCWireHit> cdcWireHits;
191  cdcWireHits.reserve(wireIDs.size());
192  std::vector<CDCRecoHit3D> cdcRecoHits;
193  cdcRecoHits.reserve(wireIDs.size());
194 
195  for (const WireID& wireID : wireIDs) {
196  cdcHits.emplace_back(128, 0, wireID);
197  }
198  for (const CDCHit& cdcHit : cdcHits) {
199  cdcWireHits.emplace_back(&cdcHit, 0);
200  }
201  for (std::size_t i = 0; i < cdcWireHits.size(); i++) {
202  const CDCRLWireHit aRLWireHit(&cdcWireHits.at(i), ERightLeft::c_Unknown);
203  const Vector3D aRecoPos(aRLWireHit.getRefPos2D(), 0.0);
204  cdcRecoHits.emplace_back(aRLWireHit, aRecoPos, arc_length_2Ds.at(i));
205  }
206 
207  const CDCTrack track(cdcRecoHits);
208 
209  // extrack variables from track
210  BasicTrackVarSet trackVarSet;
211  trackVarSet.extract(&track);
212 
213  EXPECT_EQ(3, *trackVarSet.find("size"));
214 
215  EXPECT_EQ(2.5, *trackVarSet.find("empty_s_mean"));
216  EXPECT_EQ(5.0, *trackVarSet.find("empty_s_sum"));
217  EXPECT_EQ(2.0, *trackVarSet.find("empty_s_min"));
218  EXPECT_EQ(3.0, *trackVarSet.find("empty_s_max"));
219  EXPECT_NEAR(0.7071, *trackVarSet.find("empty_s_variance"), 1e-4);
220 }
Class containing the result of the unpacker in raw data and the result of the digitizer in simulation...
Definition: CDCHit.h:40
Class to compute floating point variables from a track which can be recorded as a flat TNtuple or ser...
bool extract(const CDCTrack *track) override
Generate and assign the contained variables.
Class representing an oriented hit wire including a hypotheses whether the causing track passes left ...
Definition: CDCRLWireHit.h:41
Class representing a three dimensional reconstructed hit.
Definition: CDCRecoHit3D.h:52
Class representing a sequence of three dimensional reconstructed hits.
Definition: CDCTrack.h:41
Class representing a hit wire in the central drift chamber.
Definition: CDCWireHit.h:55
Class representating the sense wire arrangement in the whole of the central drift chamber.
static CDCWireTopology & getInstance()
Getter for the singleton instance of the wire topology.
const CDCWire & getWire(const WireID &wireId) const
Getter for wire getter by wireID object.
const WireID & getWireID() const
Getter for the wire id.
Definition: CDCWire.h:122
This class provides the declaration of the common test fixture to all test of the track finding in th...
MayBePtr< Float_t > find(const std::string &varName) override
Pointer to the variable with the given name.
Definition: VarSet.h:64
A three dimensional vector.
Definition: Vector3D.h:33
Class to identify a wire inside the CDC.
Definition: WireID.h:34
TEST_F(GlobalLabelTest, LargeNumberOfTimeDependentParameters)
Test large number of time-dep params for registration and retrieval.
Definition: globalLabel.cc:72
TEST(TestgetDetectorRegion, TestgetDetectorRegion)
Test Constructors.
Abstract base class for different kinds of events.