1#include "Tracking/dqm/TrackingRecoDQM.h"
3namespace tracking::dqm {
6 track_collection_ = parameters.
get<std::string>(
"track_collection");
7 truth_collection_ = parameters.
get<std::string>(
"truth_collection");
8 measurement_collection_ =
9 parameters.
get<std::string>(
"measurement_collection");
10 measurement_passname_ = parameters.
get<std::string>(
"measurement_passname");
12 ecal_sp_events_passname_ =
13 parameters.
get<std::string>(
"ecal_sp_events_passname");
14 ecal_sp_passname_ = parameters.
get<std::string>(
"ecal_sp_passname");
15 target_sp_events_passname_ =
16 parameters.
get<std::string>(
"target_sp_events_passname");
17 target_sp_passname_ = parameters.
get<std::string>(
"target_sp_passname");
18 truth_passname_ = parameters.
get<std::string>(
"truth_passname");
19 truth_events_passname_ = parameters.
get<std::string>(
"truth_events_passname");
20 track_passname_ = parameters.
get<std::string>(
"track_passname");
21 track_collection_events_passname_ =
22 parameters.
get<std::string>(
"track_collection_events_passname");
24 title_ = parameters.
get<std::string>(
"title",
"tagger_trk_");
25 track_prob_cut_ = parameters.
get<
double>(
"trackProb_cut", 0.5);
26 subdetector_ = parameters.
get<std::string>(
"subdetector",
"Tagger");
27 track_states_ = parameters.
get<std::vector<std::string>>(
"trackStates", {});
29 pidmap_[-321] = PIDBins::kminus;
30 pidmap_[321] = PIDBins::kplus;
31 pidmap_[-211] = PIDBins::piminus;
32 pidmap_[211] = PIDBins::piplus;
33 pidmap_[11] = PIDBins::electron;
34 pidmap_[-11] = PIDBins::positron;
35 pidmap_[2212] = PIDBins::proton;
36 pidmap_[-2212] = PIDBins::antiproton;
40 ldmx_log(trace) <<
"DQM Reading in:" << track_collection_;
42 if (!event.
exists(track_collection_, track_collection_events_passname_)) {
43 ldmx_log(error) <<
"TrackCollection " << track_collection_
44 <<
" with pass = " << track_collection_events_passname_
50 event.getCollection<
ldmx::Track>(track_collection_, track_passname_)};
52 if (!event.
exists(measurement_collection_, measurement_passname_)) {
53 ldmx_log(error) <<
"Measurement collection " << measurement_collection_
54 <<
" with pass = " << measurement_passname_
60 measurement_collection_, measurement_passname_)};
63 if (event.
exists(truth_collection_, truth_events_passname_)) {
64 truth_track_collection_ = std::make_shared<ldmx::Tracks>(
66 do_truth_comparison_ =
true;
70 if (event.
exists(
"EcalScoringPlaneHits", ecal_sp_events_passname_)) {
71 ecal_scoring_hits_ = std::make_shared<std::vector<ldmx::SimTrackerHit>>(
76 if (event.
exists(
"TargetScoringPlaneHits", target_sp_events_passname_)) {
77 target_scoring_hits_ = std::make_shared<std::vector<ldmx::SimTrackerHit>>(
79 target_sp_passname_));
82 ldmx_log(debug) <<
"Do truth comparison::" << do_truth_comparison_;
84 if (do_truth_comparison_) {
85 sortTracks(tracks, unique_tracks_, duplicate_tracks_, fake_tracks_);
87 unique_tracks_ = tracks;
90 ldmx_log(debug) <<
"Filling histograms for " << tracks.size() <<
" tracks";
95 if (!unique_tracks_.empty()) {
96 ldmx_log(debug) <<
"Track Monitoring on " << unique_tracks_.size()
98 trackMonitoring(unique_tracks_, measurements, title_,
true,
99 do_truth_comparison_);
103 if (!duplicate_tracks_.empty()) {
104 ldmx_log(debug) <<
"Track Monitoring on " << duplicate_tracks_.size()
106 trackMonitoring(duplicate_tracks_, measurements, title_ +
"dup_",
false,
109 if (!fake_tracks_.empty()) {
110 ldmx_log(debug) <<
"Track Monitoring on " << fake_tracks_.size()
112 trackMonitoring(fake_tracks_, measurements, title_ +
"fake_",
false,
false);
116 ldmx_log(trace) <<
"Track Extrapolation to Ecal Monitoring";
117 if (std::find(track_states_.begin(), track_states_.end(),
"target") !=
118 track_states_.end()) {
122 if (std::find(track_states_.begin(), track_states_.end(),
"ecal") !=
123 track_states_.end()) {
127 if (std::find(track_states_.begin(), track_states_.end(),
"beamOrigin") !=
128 track_states_.end()) {
134 if (do_truth_comparison_) {
135 ldmx_log(trace) <<
"Technical Efficiency plots";
136 efficiencyPlots(tracks, measurements, title_);
142 ldmx_log(trace) <<
"Clear the vectors";
143 unique_tracks_.clear();
144 duplicate_tracks_.clear();
145 fake_tracks_.clear();
152void TrackingRecoDQM::efficiencyPlots(
153 const std::vector<ldmx::Track>& tracks,
154 const std::vector<ldmx::Measurement>& measurements,
155 const std::string& title) {
158 histograms_.
fill(title +
"truth_N_tracks", truth_track_collection_->size());
159 for (
auto& truth_trk : *(truth_track_collection_)) {
160 auto truth_phi = truth_trk.getPhi();
161 auto truth_d0 = truth_trk.getD0();
162 auto truth_z0 = truth_trk.getZ0();
163 auto truth_theta = truth_trk.getTheta();
164 auto truth_qop = truth_trk.getQoP();
165 auto truth_p = 1. / abs(truth_trk.getQoP());
166 auto truth_n_hits = truth_trk.getNhits();
168 std::vector<double> truth_mom = truth_trk.getMomentum();
173 std::sqrt(truth_mom[1] * truth_mom[1] + truth_mom[2] * truth_mom[2]);
175 auto truth_beam_angle = std::atan2(truth_pt_beam, truth_mom[0]);
186 if (pidmap_.count(truth_trk.getPdgID()) != 0) {
191 if (pidmap_[truth_trk.getPdgID()] == PIDBins::kminus) {
195 if (pidmap_[truth_trk.getPdgID()] == PIDBins::kplus) {
199 if (pidmap_[truth_trk.getPdgID()] == PIDBins::piminus) {
203 if (pidmap_[truth_trk.getPdgID()] == PIDBins::piplus) {
207 if (pidmap_[truth_trk.getPdgID()] == PIDBins::electron) {
211 if (pidmap_[truth_trk.getPdgID()] == PIDBins::positron) {
215 if (pidmap_[truth_trk.getPdgID()] == PIDBins::proton) {
222 for (
auto& track : tracks) {
226 auto it = std::find_if(truth_track_collection_->begin(),
227 truth_track_collection_->end(),
229 return tt.getTrackID() == track.getTrackID();
232 double track_truth_prob = track.getTruthProb();
234 if (it != truth_track_collection_->end() &&
235 track_truth_prob >= track_prob_cut_)
239 if (!truth_trk)
return;
241 auto truth_phi = truth_trk->getPhi();
242 auto truth_d0 = truth_trk->getD0();
243 auto truth_z0 = truth_trk->getZ0();
244 auto truth_theta = truth_trk->getTheta();
245 auto truth_qop = truth_trk->getQoP();
246 auto truth_p = 1. / abs(truth_trk->getQoP());
247 std::vector<double> truth_mom = truth_trk->getMomentum();
252 std::sqrt(truth_mom[1] * truth_mom[1] + truth_mom[2] * truth_mom[2]);
254 auto truth_beam_angle = std::atan2(truth_pt_beam, truth_mom[0]);
266 auto dedx_measurements = track.getDedxMeasurements();
267 auto measurement_idxs = track.getMeasurementsIdxs();
268 for (
size_t i = 0; i < measurement_idxs.size(); ++i) {
270 measurements.at(measurement_idxs[i]).getLayer());
272 if (i < dedx_measurements.size()) {
274 dedx_measurements[i]);
280 if (pidmap_.count(truth_trk->getPdgID()) != 0) {
285 if (pidmap_[truth_trk->getPdgID()] == PIDBins::kminus) {
289 if (pidmap_[truth_trk->getPdgID()] == PIDBins::kplus) {
293 if (pidmap_[truth_trk->getPdgID()] == PIDBins::piminus) {
297 if (pidmap_[truth_trk->getPdgID()] == PIDBins::piplus) {
301 if (pidmap_[truth_trk->getPdgID()] == PIDBins::electron) {
305 if (pidmap_[truth_trk->getPdgID()] == PIDBins::positron) {
309 if (pidmap_[truth_trk->getPdgID()] == PIDBins::proton) {
317void TrackingRecoDQM::trackMonitoring(
318 const std::vector<ldmx::Track>& tracks,
319 const std::vector<ldmx::Measurement>& measurements,
const std::string title,
320 const bool& doDetail,
const bool& doTruth) {
321 for (
auto& track : tracks) {
323 auto trk_d0 = track.getD0();
324 auto trk_z0 = track.getZ0();
325 auto trk_qop = track.getQoP();
326 auto trk_theta = track.getTheta();
327 auto trk_phi = track.getPhi();
328 auto trk_p = 1. / abs(trk_qop);
329 auto dedx_measurements = track.getDedxMeasurements();
330 auto measurement_idxs = track.getMeasurementsIdxs();
331 for (
size_t i = 0; i < measurement_idxs.size(); ++i) {
333 measurements.at(measurement_idxs[i]).getLayer());
335 if (i < dedx_measurements.size()) {
340 std::vector<double> trk_mom = track.getMomentum();
344 std::sqrt(trk_mom[0] * trk_mom[0] + trk_mom[1] * trk_mom[1]);
348 std::sqrt(trk_mom[1] * trk_mom[1] + trk_mom[2] * trk_mom[2]);
351 Acts::BoundSquareMatrix cov =
352 tracking::sim::utils::unpackCov(track.getPerigeeCov());
354 double sigmad0 = sqrt(
355 cov(Acts::BoundIndices::eBoundLoc0, Acts::BoundIndices::eBoundLoc0));
356 double sigmaz0 = sqrt(
357 cov(Acts::BoundIndices::eBoundLoc1, Acts::BoundIndices::eBoundLoc1));
359 sqrt(cov(Acts::BoundIndices::eBoundPhi, Acts::BoundIndices::eBoundPhi));
360 double sigmatheta = sqrt(
361 cov(Acts::BoundIndices::eBoundTheta, Acts::BoundIndices::eBoundTheta));
362 double sigmaqop = sqrt(cov(Acts::BoundIndices::eBoundQOverP,
363 Acts::BoundIndices::eBoundQOverP));
364 double sigmap = (1. / trk_qop) * (1. / trk_qop) * sigmaqop;
385 track.getChi2() / track.getNdf());
396 double p = std::abs(1. / trk_qop);
398 histograms_.
fill(title +
"z0_err_vs_p", std::abs(1. / trk_qop), sigmaz0);
399 histograms_.
fill(title +
"p_err_vs_p", std::abs(1. / trk_qop), sigmap);
401 if (track.getNhits() == 8)
403 else if (track.getNhits() == 9)
405 else if (track.getNhits() == 10)
413 auto it = std::find_if(truth_track_collection_->begin(),
414 truth_track_collection_->end(),
416 return tt.getTrackID() == track.getTrackID();
419 double track_truth_prob = track.getTruthProb();
421 if (it != truth_track_collection_->end() &&
422 track_truth_prob >= track_prob_cut_)
427 auto truth_d0 = truth_trk->getD0();
428 auto truth_z0 = truth_trk->getZ0();
429 auto truth_phi = truth_trk->getPhi();
430 auto truth_theta = truth_trk->getTheta();
431 auto truth_qop = truth_trk->getQoP();
432 auto truth_p = 1. / abs(truth_trk->getQoP());
433 std::vector<double> truth_mom = truth_trk->getMomentum();
436 double truth_pt_beam = std::sqrt(truth_mom[1] * truth_mom[1] +
437 truth_mom[2] * truth_mom[2]);
446 double res_d0 = trk_d0 - truth_d0;
447 double res_z0 = trk_z0 - truth_z0;
448 double res_phi = trk_phi - truth_phi;
449 double res_theta = trk_theta - truth_theta;
450 double res_qop = trk_qop - truth_qop;
451 double res_p = trk_p - truth_p;
452 double res_pt_beam = pt_beam - truth_pt_beam;
462 double pull_d0 = res_d0 / sigmad0;
463 double pull_z0 = res_z0 / sigmaz0;
464 double pull_phi = res_phi / sigmaphi;
465 double pull_theta = res_theta / sigmatheta;
466 double pull_qop = res_qop / sigmaqop;
467 double pull_p = res_p / sigmap;
492 if (track.getNhits() == 8)
494 else if (track.getNhits() == 9)
496 else if (track.getNhits() == 10)
509 ldmx::TrackStateType ts_type,
510 const std::string& ts_title) {
511 for (
auto& track : tracks) {
515 auto it = std::find_if(truth_track_collection_->begin(),
516 truth_track_collection_->end(),
518 return tt.getTrackID() == track.getTrackID();
521 double track_truth_prob = track.getTruthProb();
523 if (it != truth_track_collection_->end() &&
524 track_truth_prob >= track_prob_cut_)
528 if (!truth_trk)
continue;
532 auto trk_ts = track.getTrackState(ts_type);
533 auto truth_ts = truth_trk->getTrackState(ts_type);
535 if (!trk_ts.has_value())
continue;
537 if (!truth_ts.has_value())
continue;
542 ldmx_log(debug) <<
"Unpacking covariance matrix";
543 Acts::BoundSquareMatrix cov =
544 tracking::sim::utils::unpackCov(target_state.cov_);
546 [[maybe_unused]]
double sigmaloc0 = sqrt(
547 cov(Acts::BoundIndices::eBoundLoc0, Acts::BoundIndices::eBoundLoc0));
548 [[maybe_unused]]
double sigmaloc1 = sqrt(
549 cov(Acts::BoundIndices::eBoundLoc1, Acts::BoundIndices::eBoundLoc1));
550 [[maybe_unused]]
double sigmaphi =
551 sqrt(cov(Acts::BoundIndices::eBoundPhi, Acts::BoundIndices::eBoundPhi));
552 [[maybe_unused]]
double sigmatheta = sqrt(
553 cov(Acts::BoundIndices::eBoundTheta, Acts::BoundIndices::eBoundTheta));
554 [[maybe_unused]]
double sigmaqop = sqrt(cov(
555 Acts::BoundIndices::eBoundQOverP, Acts::BoundIndices::eBoundQOverP));
557 double trk_qop = track.getQoP();
558 double trk_p = 1. / abs(trk_qop);
560 double track_state_loc0 = target_state.params_[0];
561 double track_state_loc1 = target_state.params_[1];
562 [[maybe_unused]]
double track_state_phi = target_state.params_[2];
563 [[maybe_unused]]
double track_state_theta = target_state.params_[3];
564 [[maybe_unused]]
double track_state_p = target_state.params_[4];
566 double truth_state_loc0 = truth_target_state.params_[0];
567 double truth_state_loc1 = truth_target_state.params_[1];
568 [[maybe_unused]]
double truth_state_phi = truth_target_state.params_[2];
569 [[maybe_unused]]
double truth_state_theta = truth_target_state.params_[3];
570 [[maybe_unused]]
double truth_state_p = truth_target_state.params_[4];
573 if (target_state.params_.size() < 5)
continue;
575 histograms_.
fill(title_ +
"trk_" + ts_title +
"_loc0", track_state_loc0);
576 histograms_.
fill(title_ +
"trk_" + ts_title +
"_loc1", track_state_loc1);
582 track_state_loc0 - truth_state_loc0);
584 track_state_loc1 - truth_state_loc1);
588 (track_state_loc0 - truth_state_loc0) / sigmaloc0);
590 (track_state_loc1 - truth_state_loc1) / sigmaloc1);
596 track.getNhits(), track_state_loc0 - truth_state_loc0);
598 track.getNhits(), track_state_loc1 - truth_state_loc1);
603 (track_state_loc0 - truth_state_loc0) / sigmaloc0);
606 (track_state_loc1 - truth_state_loc1) / sigmaloc1);
610 track_state_loc0 - truth_state_loc0);
612 track_state_loc1 - truth_state_loc1);
616 (track_state_loc0 - truth_state_loc0) / sigmaloc0);
618 (track_state_loc1 - truth_state_loc1) / sigmaloc1);
623void TrackingRecoDQM::sortTracks(
const std::vector<ldmx::Track>& tracks,
624 std::vector<ldmx::Track>& uniqueTracks,
625 std::vector<ldmx::Track>& duplicateTracks,
626 std::vector<ldmx::Track>& fakeTracks) {
628 std::vector<ldmx::Track> sorted_tracks = tracks;
631 std::sort(sorted_tracks.begin(), sorted_tracks.end(),
633 return t1.getTrackID() < t2.getTrackID();
637 for (
size_t i = 0; i < sorted_tracks.size(); i++) {
638 if (sorted_tracks[i].getTruthProb() < track_prob_cut_)
639 fakeTracks.push_back(sorted_tracks[i]);
643 if (uniqueTracks.size() == 0 ||
644 sorted_tracks[i].getTrackID() != sorted_tracks[i - 1].getTrackID()) {
645 uniqueTracks.push_back(sorted_tracks[i]);
651 else if (sorted_tracks[i].getTruthProb() >
652 uniqueTracks.back().getTruthProb()) {
653 duplicateTracks.push_back(uniqueTracks.back());
654 uniqueTracks.back() = sorted_tracks[i];
658 duplicateTracks.push_back(sorted_tracks[i]);
665 if (uniqueTracks.size() + duplicateTracks.size() + fakeTracks.size() !=
667 std::cerr <<
"Error: unique and duplicate tracks vectors do not add up to "
668 "original tracks vector";
673 ldmx_log(trace) <<
"Unique tracks:";
675 ldmx_log(trace) <<
"\tTrack ID: " << track.getTrackID()
676 <<
", Truth Prob: " << track.getTruthProb();
678 ldmx_log(trace) <<
"Duplicate tracks:";
680 ldmx_log(trace) <<
"\tTrack ID: " << track.getTrackID()
681 <<
", Truth Prob: " << track.getTruthProb();
683 ldmx_log(trace) <<
"Fake tracks:";
685 ldmx_log(trace) <<
"\tTrack ID: " << track.getTrackID()
686 <<
", Truth Prob: " << track.getTruthProb();
#define DECLARE_ANALYZER(CLASS)
Macro which allows the framework to construct an analyzer given its name during configuration.
HistogramPool histograms_
helper object for making and filling histograms
Implements an event buffer system for storing event data.
bool exists(const std::string &name, const std::string &passName, bool unique=true) const
Check for the existence of an object or collection with the given name and pass name in the event.
const std::vector< ContentType > & getCollection(const std::string &collectionName, const std::string &passName) const
Get a collection (std::vector) of objects from the event bus.
void fill(const std::string &name, const T &val)
Fill a 1D histogram.
Class encapsulating parameters for configuring a processor.
const T & get(const std::string &name) const
Retrieve the parameter of the given name.
Represents a simulated tracker hit in the simulation.
Implementation of a track object.
void onProcessEnd() override
Callback for the EventProcessor to take any necessary action when the processing of events finishes,...
void configure(framework::config::Parameters ¶meters) override
Configure the analyzer using the given user specified parameters.
void analyze(const framework::Event &event) override
Process the event and make histograms or summaries.
void trackStateMonitoring(const ldmx::Tracks &tracks, ldmx::TrackStateType ts_type, const std::string &ts_title)
Monitoring plots for tracks extrapolated to the ECAL Scoring plane.