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_coll_name_ = parameters.
get<std::string>(
"ecal_sp_coll_name");
13 ecal_sp_passname_ = parameters.
get<std::string>(
"ecal_sp_passname");
14 target_sp_coll_name_ = parameters.
get<std::string>(
"target_sp_coll_name");
15 target_sp_passname_ = parameters.
get<std::string>(
"target_sp_passname");
16 truth_passname_ = parameters.
get<std::string>(
"truth_passname");
17 truth_events_passname_ = parameters.
get<std::string>(
"truth_events_passname");
18 track_passname_ = parameters.
get<std::string>(
"track_passname");
19 track_collection_events_passname_ =
20 parameters.
get<std::string>(
"track_collection_events_passname");
22 title_ = parameters.
get<std::string>(
"title",
"tagger_trk_");
23 track_prob_cut_ = parameters.
get<
double>(
"trackProb_cut", 0.5);
24 subdetector_ = parameters.
get<std::string>(
"subdetector",
"Tagger");
25 track_states_ = parameters.
get<std::vector<std::string>>(
"trackStates", {});
27 pidmap_[-321] = PIDBins::kminus;
28 pidmap_[321] = PIDBins::kplus;
29 pidmap_[-211] = PIDBins::piminus;
30 pidmap_[211] = PIDBins::piplus;
31 pidmap_[11] = PIDBins::electron;
32 pidmap_[-11] = PIDBins::positron;
33 pidmap_[2212] = PIDBins::proton;
34 pidmap_[-2212] = PIDBins::antiproton;
38 ldmx_log(trace) <<
"DQM Reading in:" << track_collection_;
40 if (!event.
exists(track_collection_, track_collection_events_passname_)) {
41 ldmx_log(error) <<
"TrackCollection " << track_collection_
42 <<
" with pass = " << track_collection_events_passname_
48 event.getCollection<
ldmx::Track>(track_collection_, track_passname_)};
50 if (!event.
exists(measurement_collection_, measurement_passname_)) {
51 ldmx_log(error) <<
"Measurement collection " << measurement_collection_
52 <<
" with pass = " << measurement_passname_
58 measurement_collection_, measurement_passname_)};
61 if (event.
exists(truth_collection_, truth_events_passname_)) {
62 truth_track_collection_ = std::make_shared<ldmx::Tracks>(
64 do_truth_comparison_ =
true;
68 if (event.
exists(ecal_sp_coll_name_, ecal_sp_passname_)) {
69 ecal_scoring_hits_ = std::make_shared<std::vector<ldmx::SimTrackerHit>>(
74 if (event.
exists(target_sp_coll_name_, target_sp_passname_)) {
75 target_scoring_hits_ = std::make_shared<std::vector<ldmx::SimTrackerHit>>(
77 target_sp_passname_));
80 ldmx_log(debug) <<
"Do truth comparison::" << do_truth_comparison_;
82 if (do_truth_comparison_) {
83 sortTracks(tracks, unique_tracks_, duplicate_tracks_, fake_tracks_);
85 unique_tracks_ = tracks;
88 ldmx_log(debug) <<
"Filling histograms for " << tracks.size() <<
" tracks";
93 if (!unique_tracks_.empty()) {
94 ldmx_log(debug) <<
"Track Monitoring on " << unique_tracks_.size()
96 trackMonitoring(unique_tracks_, measurements, title_,
true,
97 do_truth_comparison_);
101 if (!duplicate_tracks_.empty()) {
102 ldmx_log(debug) <<
"Track Monitoring on " << duplicate_tracks_.size()
104 trackMonitoring(duplicate_tracks_, measurements, title_ +
"dup_",
false,
107 if (!fake_tracks_.empty()) {
108 ldmx_log(debug) <<
"Track Monitoring on " << fake_tracks_.size()
110 trackMonitoring(fake_tracks_, measurements, title_ +
"fake_",
false,
false);
114 ldmx_log(trace) <<
"Track Extrapolation to Ecal Monitoring";
115 if (std::find(track_states_.begin(), track_states_.end(),
"target") !=
116 track_states_.end()) {
120 if (std::find(track_states_.begin(), track_states_.end(),
"ecal") !=
121 track_states_.end()) {
125 if (std::find(track_states_.begin(), track_states_.end(),
"beamOrigin") !=
126 track_states_.end()) {
132 if (do_truth_comparison_) {
133 ldmx_log(trace) <<
"Technical Efficiency plots";
134 efficiencyPlots(tracks, measurements, title_);
140 ldmx_log(trace) <<
"Clear the vectors";
141 unique_tracks_.clear();
142 duplicate_tracks_.clear();
143 fake_tracks_.clear();
150void TrackingRecoDQM::efficiencyPlots(
151 const std::vector<ldmx::Track>& tracks,
152 const std::vector<ldmx::Measurement>& measurements,
153 const std::string& title) {
156 histograms_.
fill(title +
"truth_N_tracks", truth_track_collection_->size());
157 for (
auto& truth_trk : *(truth_track_collection_)) {
158 auto truth_phi = truth_trk.getPhi();
159 auto truth_d0 = truth_trk.getD0();
160 auto truth_z0 = truth_trk.getZ0();
161 auto truth_theta = truth_trk.getTheta();
162 auto truth_qop = truth_trk.getQoP();
163 auto truth_p = 1. / abs(truth_trk.getQoP());
164 auto truth_n_hits = truth_trk.getNhits();
166 std::vector<double> truth_mom = truth_trk.getMomentum();
171 std::sqrt(truth_mom[1] * truth_mom[1] + truth_mom[2] * truth_mom[2]);
173 auto truth_beam_angle = std::atan2(truth_pt_beam, truth_mom[0]);
184 if (pidmap_.count(truth_trk.getPdgID()) != 0) {
189 if (pidmap_[truth_trk.getPdgID()] == PIDBins::kminus) {
193 if (pidmap_[truth_trk.getPdgID()] == PIDBins::kplus) {
197 if (pidmap_[truth_trk.getPdgID()] == PIDBins::piminus) {
201 if (pidmap_[truth_trk.getPdgID()] == PIDBins::piplus) {
205 if (pidmap_[truth_trk.getPdgID()] == PIDBins::electron) {
209 if (pidmap_[truth_trk.getPdgID()] == PIDBins::positron) {
213 if (pidmap_[truth_trk.getPdgID()] == PIDBins::proton) {
220 for (
auto& track : tracks) {
224 auto it = std::find_if(truth_track_collection_->begin(),
225 truth_track_collection_->end(),
227 return tt.getTrackID() == track.getTrackID();
230 double track_truth_prob = track.getTruthProb();
232 if (it != truth_track_collection_->end() &&
233 track_truth_prob >= track_prob_cut_)
237 if (!truth_trk)
return;
239 auto truth_phi = truth_trk->getPhi();
240 auto truth_d0 = truth_trk->getD0();
241 auto truth_z0 = truth_trk->getZ0();
242 auto truth_theta = truth_trk->getTheta();
243 auto truth_qop = truth_trk->getQoP();
244 auto truth_p = 1. / abs(truth_trk->getQoP());
245 std::vector<double> truth_mom = truth_trk->getMomentum();
250 std::sqrt(truth_mom[1] * truth_mom[1] + truth_mom[2] * truth_mom[2]);
252 auto truth_beam_angle = std::atan2(truth_pt_beam, truth_mom[0]);
264 auto dedx_measurements = track.getDedxMeasurements();
265 auto measurement_idxs = track.getMeasurementsIdxs();
266 for (
size_t i = 0; i < measurement_idxs.size(); ++i) {
268 measurements.at(measurement_idxs[i]).getLayer());
270 if (i < dedx_measurements.size()) {
272 dedx_measurements[i]);
278 if (pidmap_.count(truth_trk->getPdgID()) != 0) {
283 if (pidmap_[truth_trk->getPdgID()] == PIDBins::kminus) {
287 if (pidmap_[truth_trk->getPdgID()] == PIDBins::kplus) {
291 if (pidmap_[truth_trk->getPdgID()] == PIDBins::piminus) {
295 if (pidmap_[truth_trk->getPdgID()] == PIDBins::piplus) {
299 if (pidmap_[truth_trk->getPdgID()] == PIDBins::electron) {
303 if (pidmap_[truth_trk->getPdgID()] == PIDBins::positron) {
307 if (pidmap_[truth_trk->getPdgID()] == PIDBins::proton) {
315void TrackingRecoDQM::trackMonitoring(
316 const std::vector<ldmx::Track>& tracks,
317 const std::vector<ldmx::Measurement>& measurements,
const std::string title,
318 const bool& doDetail,
const bool& doTruth) {
319 for (
auto& track : tracks) {
321 auto trk_d0 = track.getD0();
322 auto trk_z0 = track.getZ0();
323 auto trk_qop = track.getQoP();
324 auto trk_theta = track.getTheta();
325 auto trk_phi = track.getPhi();
326 auto trk_p = 1. / abs(trk_qop);
327 auto dedx_measurements = track.getDedxMeasurements();
328 auto measurement_idxs = track.getMeasurementsIdxs();
329 for (
size_t i = 0; i < measurement_idxs.size(); ++i) {
331 measurements.at(measurement_idxs[i]).getLayer());
333 if (i < dedx_measurements.size()) {
338 std::vector<double> trk_mom = track.getMomentum();
342 std::sqrt(trk_mom[0] * trk_mom[0] + trk_mom[1] * trk_mom[1]);
346 std::sqrt(trk_mom[1] * trk_mom[1] + trk_mom[2] * trk_mom[2]);
349 Acts::BoundSquareMatrix cov =
350 tracking::sim::utils::unpackCov(track.getPerigeeCov());
352 double sigmad0 = sqrt(
353 cov(Acts::BoundIndices::eBoundLoc0, Acts::BoundIndices::eBoundLoc0));
354 double sigmaz0 = sqrt(
355 cov(Acts::BoundIndices::eBoundLoc1, Acts::BoundIndices::eBoundLoc1));
357 sqrt(cov(Acts::BoundIndices::eBoundPhi, Acts::BoundIndices::eBoundPhi));
358 double sigmatheta = sqrt(
359 cov(Acts::BoundIndices::eBoundTheta, Acts::BoundIndices::eBoundTheta));
360 double sigmaqop = sqrt(cov(Acts::BoundIndices::eBoundQOverP,
361 Acts::BoundIndices::eBoundQOverP));
362 double sigmap = (1. / trk_qop) * (1. / trk_qop) * sigmaqop;
383 track.getChi2() / track.getNdf());
394 double p = std::abs(1. / trk_qop);
396 histograms_.
fill(title +
"z0_err_vs_p", std::abs(1. / trk_qop), sigmaz0);
397 histograms_.
fill(title +
"p_err_vs_p", std::abs(1. / trk_qop), sigmap);
399 if (track.getNhits() == 8)
401 else if (track.getNhits() == 9)
403 else if (track.getNhits() == 10)
411 auto it = std::find_if(truth_track_collection_->begin(),
412 truth_track_collection_->end(),
414 return tt.getTrackID() == track.getTrackID();
417 double track_truth_prob = track.getTruthProb();
419 if (it != truth_track_collection_->end() &&
420 track_truth_prob >= track_prob_cut_)
425 auto truth_d0 = truth_trk->getD0();
426 auto truth_z0 = truth_trk->getZ0();
427 auto truth_phi = truth_trk->getPhi();
428 auto truth_theta = truth_trk->getTheta();
429 auto truth_qop = truth_trk->getQoP();
430 auto truth_p = 1. / abs(truth_trk->getQoP());
431 std::vector<double> truth_mom = truth_trk->getMomentum();
434 double truth_pt_beam = std::sqrt(truth_mom[1] * truth_mom[1] +
435 truth_mom[2] * truth_mom[2]);
444 double res_d0 = trk_d0 - truth_d0;
445 double res_z0 = trk_z0 - truth_z0;
446 double res_phi = trk_phi - truth_phi;
447 double res_theta = trk_theta - truth_theta;
448 double res_qop = trk_qop - truth_qop;
449 double res_p = trk_p - truth_p;
450 double res_pt_beam = pt_beam - truth_pt_beam;
460 double pull_d0 = res_d0 / sigmad0;
461 double pull_z0 = res_z0 / sigmaz0;
462 double pull_phi = res_phi / sigmaphi;
463 double pull_theta = res_theta / sigmatheta;
464 double pull_qop = res_qop / sigmaqop;
465 double pull_p = res_p / sigmap;
490 if (track.getNhits() == 8)
492 else if (track.getNhits() == 9)
494 else if (track.getNhits() == 10)
507 ldmx::TrackStateType ts_type,
508 const std::string& ts_title) {
509 for (
auto& track : tracks) {
513 auto it = std::find_if(truth_track_collection_->begin(),
514 truth_track_collection_->end(),
516 return tt.getTrackID() == track.getTrackID();
519 double track_truth_prob = track.getTruthProb();
521 if (it != truth_track_collection_->end() &&
522 track_truth_prob >= track_prob_cut_)
526 if (!truth_trk)
continue;
530 auto trk_ts = track.getTrackState(ts_type);
531 auto truth_ts = truth_trk->getTrackState(ts_type);
533 if (!trk_ts.has_value())
continue;
535 if (!truth_ts.has_value())
continue;
540 ldmx_log(debug) <<
"Unpacking covariance matrix";
541 Acts::BoundSquareMatrix cov =
542 tracking::sim::utils::unpackCov(target_state.cov_);
544 [[maybe_unused]]
double sigmaloc0 = sqrt(
545 cov(Acts::BoundIndices::eBoundLoc0, Acts::BoundIndices::eBoundLoc0));
546 [[maybe_unused]]
double sigmaloc1 = sqrt(
547 cov(Acts::BoundIndices::eBoundLoc1, Acts::BoundIndices::eBoundLoc1));
548 [[maybe_unused]]
double sigmaphi =
549 sqrt(cov(Acts::BoundIndices::eBoundPhi, Acts::BoundIndices::eBoundPhi));
550 [[maybe_unused]]
double sigmatheta = sqrt(
551 cov(Acts::BoundIndices::eBoundTheta, Acts::BoundIndices::eBoundTheta));
552 [[maybe_unused]]
double sigmaqop = sqrt(cov(
553 Acts::BoundIndices::eBoundQOverP, Acts::BoundIndices::eBoundQOverP));
555 double trk_qop = track.getQoP();
556 double trk_p = 1. / abs(trk_qop);
558 double track_state_loc0 = target_state.params_[0];
559 double track_state_loc1 = target_state.params_[1];
560 [[maybe_unused]]
double track_state_phi = target_state.params_[2];
561 [[maybe_unused]]
double track_state_theta = target_state.params_[3];
562 [[maybe_unused]]
double track_state_p = target_state.params_[4];
564 double truth_state_loc0 = truth_target_state.params_[0];
565 double truth_state_loc1 = truth_target_state.params_[1];
566 [[maybe_unused]]
double truth_state_phi = truth_target_state.params_[2];
567 [[maybe_unused]]
double truth_state_theta = truth_target_state.params_[3];
568 [[maybe_unused]]
double truth_state_p = truth_target_state.params_[4];
571 if (target_state.params_.size() < 5)
continue;
573 histograms_.
fill(title_ +
"trk_" + ts_title +
"_loc0", track_state_loc0);
574 histograms_.
fill(title_ +
"trk_" + ts_title +
"_loc1", track_state_loc1);
580 track_state_loc0 - truth_state_loc0);
582 track_state_loc1 - truth_state_loc1);
586 (track_state_loc0 - truth_state_loc0) / sigmaloc0);
588 (track_state_loc1 - truth_state_loc1) / sigmaloc1);
594 track.getNhits(), track_state_loc0 - truth_state_loc0);
596 track.getNhits(), track_state_loc1 - truth_state_loc1);
601 (track_state_loc0 - truth_state_loc0) / sigmaloc0);
604 (track_state_loc1 - truth_state_loc1) / sigmaloc1);
608 track_state_loc0 - truth_state_loc0);
610 track_state_loc1 - truth_state_loc1);
614 (track_state_loc0 - truth_state_loc0) / sigmaloc0);
616 (track_state_loc1 - truth_state_loc1) / sigmaloc1);
621void TrackingRecoDQM::sortTracks(
const std::vector<ldmx::Track>& tracks,
622 std::vector<ldmx::Track>& uniqueTracks,
623 std::vector<ldmx::Track>& duplicateTracks,
624 std::vector<ldmx::Track>& fakeTracks) {
626 std::vector<ldmx::Track> sorted_tracks = tracks;
629 std::sort(sorted_tracks.begin(), sorted_tracks.end(),
631 return t1.getTrackID() < t2.getTrackID();
635 for (
size_t i = 0; i < sorted_tracks.size(); i++) {
636 if (sorted_tracks[i].getTruthProb() < track_prob_cut_)
637 fakeTracks.push_back(sorted_tracks[i]);
641 if (uniqueTracks.size() == 0 ||
642 sorted_tracks[i].getTrackID() != sorted_tracks[i - 1].getTrackID()) {
643 uniqueTracks.push_back(sorted_tracks[i]);
649 else if (sorted_tracks[i].getTruthProb() >
650 uniqueTracks.back().getTruthProb()) {
651 duplicateTracks.push_back(uniqueTracks.back());
652 uniqueTracks.back() = sorted_tracks[i];
656 duplicateTracks.push_back(sorted_tracks[i]);
663 if (uniqueTracks.size() + duplicateTracks.size() + fakeTracks.size() !=
665 std::cerr <<
"Error: unique and duplicate tracks vectors do not add up to "
666 "original tracks vector";
671 ldmx_log(trace) <<
"Unique tracks:";
673 ldmx_log(trace) <<
"\tTrack ID: " << track.getTrackID()
674 <<
", Truth Prob: " << track.getTruthProb();
676 ldmx_log(trace) <<
"Duplicate tracks:";
678 ldmx_log(trace) <<
"\tTrack ID: " << track.getTrackID()
679 <<
", Truth Prob: " << track.getTruthProb();
681 ldmx_log(trace) <<
"Fake tracks:";
683 ldmx_log(trace) <<
"\tTrack ID: " << track.getTrackID()
684 <<
", 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.