LDMX Software
LinearTruthTracking.cxx
1#include "Tracking/Reco/LinearTruthTracking.h"
2
3#include <iostream>
4
5#include "Ecal/Event/EcalHit.h"
6#include "Eigen/Dense"
7
8namespace tracking {
9namespace reco {
10
12 framework::Process& process)
13 : TrackingGeometryUser(name, process) {}
14
16 // Output seed name
17 out_trk_collection_ = parameters.get<std::string>("out_trk_collection",
18 "LinearRecoilTruthTracks");
19
20 // Input strip hits_
22 parameters.get<std::string>("input_hits_collection", "RecoilSimHits");
24 parameters.get<std::string>("input_rec_hits_collection", "EcalRecHits");
25
26 input_pass_name_ = parameters.get<std::string>("input_pass_name");
27
28 ecal_first_layer_z_threshold_ =
29 parameters.get<double>("ecal_first_layer_z_threshold");
30} // configure
31
33 auto start = std::chrono::high_resolution_clock::now();
34 std::vector<ldmx::StraightTrack> straight_truth_tracks;
35 n_events_++;
36
37 const auto& recoil_hits = event.getCollection<ldmx::SimTrackerHit>(
38 input_hits_collection_, input_pass_name_);
39 const auto& ecal_rec_hit = event.getCollection<ldmx::EcalHit>(
40 input_rec_hits_collection_, input_pass_name_);
41
42 std::vector<std::array<double, 3>> first_layer_ecal_rec_hits;
43
44 for (const auto& x_ecal : ecal_rec_hit) {
45 if (x_ecal.getZPos() < ecal_first_layer_z_threshold_) {
46 first_layer_ecal_rec_hits.push_back(
47 {x_ecal.getZPos(), x_ecal.getXPos(), x_ecal.getYPos()});
48 } // if first layer_ of Ecal
49 } // for positions in ecalRecHit
50
51 // Check if we would fit empty seeds.
52 if (recoil_hits.size() < 2) {
53 n_missing_++;
54 n_truth_ += straight_truth_tracks.size();
55 event.add(out_trk_collection_, straight_truth_tracks);
56 return;
57 } // if not enough hits_
58
59 std::vector<ldmx::SimTrackerHit> truth_points;
60
61 // Find only recoil electron out of the hit collection
62 for (const auto& point : recoil_hits) {
63 if (point.getTrackID() == 1) {
64 truth_points.push_back(point);
65 } // if trackID == 1
66 } // for point in hit collection
67
68 // should only find 1 track (recoil e-) or 0 track (no recoil e-)
69 if (truth_points.size() > 1) {
70 straight_truth_tracks.push_back(
71 truthTracker(truth_points, first_layer_ecal_rec_hits));
72 } // if we have truth points
73 else {
74 n_empty_++;
75 n_truth_ += straight_truth_tracks.size();
76 return;
77 } // else no truth points
78
79 n_truth_ += straight_truth_tracks.size();
80 event.add(out_trk_collection_, straight_truth_tracks);
81
82 auto end = std::chrono::high_resolution_clock::now();
83 auto diff = end - start;
84 processing_time_ += std::chrono::duration<double, std::milli>(diff).count();
85
86 first_layer_ecal_rec_hits.clear();
87 straight_truth_tracks.clear();
88
89} // produce
90
91ldmx::StraightTrack LinearTruthTracking::truthTracker(
92 const std::vector<ldmx::SimTrackerHit>& points,
93 std::vector<std::array<double, 3>>& ecal_points) {
95
96 // We have pre-selected hits_ corresponding to the Recoil e-, so we only fit
97 // these (no RecHit)
98 auto [m_x, b_x, m_y, b_y] = fit3DLine(points);
99
100 trk.setSlopeX(m_x);
101 trk.setInterceptX(b_x);
102 trk.setSlopeY(m_y);
103 trk.setInterceptY(b_y);
104 trk.setTheta(std::atan2(m_y, std::sqrt(1 + m_x * m_x)));
105 trk.setPhi(std::atan2(m_x, 1.0));
106 trk.setTargetLocation(0.0, b_x, b_y);
107 trk.setNhits(points.size());
108 trk.setNdf(points.size() - 2);
109
110 trk.setTrackID(points[0].getTrackID());
111 trk.setPdgID(points[0].getPdgID());
112 trk.setTruthProb(1.0);
113
114 trk.setAllTruthSensorPoints(points);
115
116 trk.setChi2(globalChiSquare(points, m_x, m_y, b_x, b_y));
117
118 // Z position from the first point in ecalPoints
119 if (ecal_points.size() > 0) {
120 double ecal_first_layer_z = ecal_points[0][0];
121
122 // Extrapolate track to first layer_ of Ecal
123 std::array<double, 3> extrapolated_point = {ecal_first_layer_z,
124 m_x * ecal_first_layer_z + b_x,
125 m_y * ecal_first_layer_z + b_y};
126
127 const std::array<double, 3>* closest_rec_hit = nullptr;
128 double min_distance = std::numeric_limits<double>::max();
129
130 // Loop through ecalPoints to find the closest recHit to our track
131 for (const auto& ecal_rec_hit : ecal_points) {
132 double temp_distance =
133 calculateDistance(extrapolated_point, ecal_rec_hit);
134
135 if (temp_distance < min_distance) {
136 min_distance = temp_distance;
137 closest_rec_hit = &ecal_rec_hit;
138 } // if compare min distance
139 } // for recHits
140
141 if (closest_rec_hit != nullptr) {
142 trk.setFirstLayerEcalRecHit(*closest_rec_hit);
143 trk.setDistancetoRecHit(min_distance);
144 trk.setEcalLayer1Location(extrapolated_point);
145 } // if set trk info
146 } // make sure there are ecalPoints to work with
147
148 return trk;
149} // truthTracker
150
152 ldmx_log(info) << "AVG Time / Event: " << std::fixed << std::setprecision(1)
153 << processing_time_ / n_events_ << " ms";
154 ldmx_log(info) << "Number of Events without enough seed points: "
155 << n_missing_;
156 ldmx_log(info) << "Number of Events with only 1 trackID == 1 point: "
157 << n_empty_;
158 ldmx_log(info) << "Total Truth Tracks / Event: " << n_truth_ << "/"
159 << n_events_;
160} // onProcessEnd
161
162double LinearTruthTracking::calculateDistance(
163 const std::array<double, 3>& point1, const std::array<double, 3>& point2) {
164 return sqrt(pow(point1[1] - point2[1], 2) + pow(point1[2] - point2[2], 2));
165} // calculateDistance
166
167std::tuple<double, double, double, double> LinearTruthTracking::fit3DLine(
168 const std::vector<ldmx::SimTrackerHit>& points) {
169 std::vector<double> z_vals, x_vals, y_vals;
170
171 for (const auto& point : points) {
172 const auto& position = point.getPosition();
173 x_vals.push_back(position[0]);
174 y_vals.push_back(position[1]);
175 z_vals.push_back(position[2]);
176 }
177
178 size_t num_points = points.size();
179 Eigen::MatrixXd a_mat(num_points, 2);
180 Eigen::VectorXd x_vec(num_points), y_vec(num_points);
181
182 for (size_t i = 0; i < num_points; ++i) {
183 a_mat(i, 0) = z_vals[i];
184 a_mat(i, 1) = 1;
185 x_vec(i) = x_vals[i];
186 y_vec(i) = y_vals[i];
187 }
188
189 Eigen::Matrix2d at_a = a_mat.transpose() * a_mat;
190 Eigen::Vector2d at_x = a_mat.transpose() * x_vec;
191 Eigen::Vector2d at_y = a_mat.transpose() * y_vec;
192
193 Eigen::Vector2d x_params = at_a.ldlt().solve(at_x);
194 Eigen::Vector2d y_params = at_a.ldlt().solve(at_y);
195
196 // Return (mx, bx, my, by)
197 return {x_params(0), x_params(1), y_params(0), y_params(1)};
198}
199
200double LinearTruthTracking::globalChiSquare(
201 const std::vector<ldmx::SimTrackerHit>& points, double m_x, double m_y,
202 double b_x, double b_y) {
203 double chi2_x = 0, chi2_y = 0;
204
205 for (const auto& point : points) {
206 const auto& position = point.getPosition();
207 double x_pos = position[0];
208 double y_pos = position[1];
209 double z_pos = position[2];
210
211 double x_fit = m_x * z_pos + b_x;
212 double y_fit = m_y * z_pos + b_y;
213
214 chi2_x += std::pow((x_pos - x_fit), 2);
215 chi2_y += std::pow((y_pos - y_fit), 2);
216 }
217
218 return chi2_x + chi2_y;
219} // globalChiSquare
220
221} // namespace reco
222} // namespace tracking
223
#define DECLARE_PRODUCER(CLASS)
Macro which allows the framework to construct a producer given its name during configuration.
Implements an event buffer system for storing event data.
Definition Event.h:40
Class which represents the process under execution.
Definition Process.h:34
Class encapsulating parameters for configuring a processor.
Definition Parameters.h:26
const T & get(const std::string &name) const
Retrieve the parameter of the given name.
Definition Parameters.h:75
Stores reconstructed hit information from the ECAL.
Definition EcalHit.h:19
Represents a simulated tracker hit in the simulation.
std::string input_rec_hits_collection_
The name of the tagger Tracks (only for Recoil Seeding)
void produce(framework::Event &event) override
Run the processor and create a collection of results which indicate if a charge particle can be found...
std::string input_hits_collection_
The name of the input hits collection to use in finding seeds..
LinearTruthTracking(const std::string &name, framework::Process &process)
Constructor.
std::string out_trk_collection_
The name of the output collection of seeds to be stored.
void onProcessEnd() override
Output event statistics.
void configure(framework::config::Parameters &parameters) override
Configure the processor using the given user specified parameters.
a helper base class providing some methods to shorten access to common conditions used within the tra...
The measurement calibrator can be a function or a class/struct able to retrieve the sim hits containe...