LDMX Software
tracking::reco::GreedyAmbiguitySolver Class Referencefinal

Minimal example of a processor. More...

#include <GreedyAmbiguitySolver.h>

Classes

struct  State
 

Public Member Functions

 GreedyAmbiguitySolver (const std::string &name, framework::Process &process)
 Constructor.
 
virtual ~GreedyAmbiguitySolver ()=default
 Destructor.
 
void onNewRun (const ldmx::RunHeader &rh) override
 onNewRun is the first function called for each processor after the conditions are fully configured and accessible.
 
void configure (framework::config::Parameters &parameters) override
 Configure the processor using the given user specified parameters.
 
void produce (framework::Event &event) override
 Process the event and put new data products into it.
 
void onProcessEnd () override
 Callback for the EventProcessor to take any necessary action when the processing of events finishes, such as calculating job-summary quantities.
 
- Public Member Functions inherited from tracking::reco::TrackingGeometryUser
 TrackingGeometryUser (const std::string &name, framework::Process &p)
 
- Public Member Functions inherited from framework::Producer
 Producer (const std::string &name, Process &process)
 Class constructor.
 
virtual void process (Event &event) final
 Processing an event for a Producer is calling produce.
 
- Public Member Functions inherited from framework::EventProcessor
 DECLARE_FACTORY (EventProcessor, EventProcessor *, const std::string &, Process &)
 declare that we have a factory for this class
 
 EventProcessor (const std::string &name, Process &process)
 Class constructor.
 
virtual ~EventProcessor ()=default
 Class destructor.
 
virtual void beforeNewRun (ldmx::RunHeader &run_header)
 Callback for Producers to add parameters to the run header before conditions are initialized.
 
virtual void onFileOpen (EventFile &event_file)
 Callback for the EventProcessor to take any necessary action when a new event input ROOT file is opened.
 
virtual void onFileClose (EventFile &event_file)
 Callback for the EventProcessor to take any necessary action when a event input ROOT file is closed.
 
virtual void onProcessStart ()
 Callback for the EventProcessor to take any necessary action when the processing of events starts, such as creating histograms.
 
template<class T >
const T & getCondition (const std::string &condition_name)
 Access a conditions object for the current event.
 
TDirectory * getHistoDirectory ()
 Access/create a directory in the histogram file for this event processor to create histograms and analysis tuples.
 
void setStorageHint (framework::StorageControl::Hint hint)
 Mark the current event as having the given storage control hint from this module_.
 
void setStorageHint (framework::StorageControl::Hint hint, const std::string &purposeString)
 Mark the current event as having the given storage control hint from this module and the given purpose string.
 
int getLogFrequency () const
 Get the current logging frequency from the process.
 
int getRunNumber () const
 Get the run number from the process.
 
std::string getName () const
 Get the processor name.
 
void createHistograms (const std::vector< framework::config::Parameters > &histos)
 Internal function which is used to create histograms passed from the python configuration @parma histos vector of Parameters that configure histograms to create.
 

Private Member Functions

template<typename geometry_t , typename source_link_hash_t , typename source_link_equality_t >
void computeInitialState (std::vector< ldmx::Track > tracks, std::vector< ldmx::Measurement > measurements, State &state, geometry_t &tg, source_link_hash_t &&sourceLinkHash, source_link_equality_t &&sourceLinkEquality) const
 
void resolve (State &state)
 Updates the state iteratively by evicting one track after the other until the final state conditions are met.
 
void removeTrack (State &state, std::size_t iTrack) const
 

Private Attributes

int nevents_ {0}
 
int n_input_tracks_ {0}
 
int n_output_tracks_ {0}
 
double processing_time_ {0.}
 
std::uint32_t maximum_shared_hits_ {1}
 Maximum amount of shared hits per track.
 
std::uint32_t maximum_iterations_ {1000}
 Maximum number of iterations.
 
std::size_t n_meas_min_ {7}
 Minimum number of measurement to form a track.
 
std::string out_trk_collection_ {"TaggerTracksClean"}
 
std::string track_collection_ {"TaggerTracks"}
 
std::string meas_collection_ {"DigiTaggerSimHits"}
 
std::string input_pass_name_ {""}
 

Additional Inherited Members

- Protected Member Functions inherited from tracking::reco::TrackingGeometryUser
const Acts::GeometryContext & geometryContext ()
 
const Acts::MagneticFieldContext & magneticFieldContext ()
 
const Acts::CalibrationContext & calibrationContext ()
 
const geo::TrackersTrackingGeometry & geometry ()
 
void loadBField (const std::string &path, const BFieldDistortion &distortion={})
 Load the interpolated B-field map from path and cache it.
 
void loadBField (const BFieldDistortion &distortion={})
 Load B-field from the path recorded in the detector GDML.
 
std::shared_ptr< Acts::MagneticFieldProvider > bField () const
 Return the loaded B-field provider.
 
- Protected Member Functions inherited from framework::EventProcessor
void abortEvent ()
 Abort the event immediately.
 
- Static Protected Member Functions inherited from tracking::reco::TrackingGeometryUser
static BFieldDistortion bFieldDistortion (const framework::config::Parameters &parameters)
 Build a BFieldDistortion from processor configuration.
 
- Protected Attributes inherited from framework::EventProcessor
HistogramPool histograms_
 helper object for making and filling histograms
 
NtupleManager & ntuple_ {NtupleManager::getInstance()}
 Manager for any ntuples.
 
logging::logger the_log_
 The logger for this EventProcessor.
 

Detailed Description

Minimal example of a processor.

This processor will loop over all of the ECal hits in an event and print out their details.

Definition at line 38 of file GreedyAmbiguitySolver.h.

Constructor & Destructor Documentation

◆ GreedyAmbiguitySolver()

tracking::reco::GreedyAmbiguitySolver::GreedyAmbiguitySolver ( const std::string & name,
framework::Process & process )

Constructor.

Parameters
nameName for this instance of the class.
processThe Process class associated with EventProcessor, provided by the framework.

Definition at line 16 of file GreedyAmbiguitySolver.cxx.

18 : TrackingGeometryUser(name, process) {}
virtual void process(Event &event) final
Processing an event for a Producer is calling produce.

Member Function Documentation

◆ computeInitialState()

template<typename geometry_t , typename source_link_hash_t , typename source_link_equality_t >
void tracking::reco::GreedyAmbiguitySolver::computeInitialState ( std::vector< ldmx::Track > tracks,
std::vector< ldmx::Measurement > measurements,
State & state,
geometry_t & tg,
source_link_hash_t && sourceLinkHash,
source_link_equality_t && sourceLinkEquality ) const
private
Parameters
tracksThe input track container.
stateAn empty state object which is expected to be default constructed.
sourceLinkHashA functor to acquire a hash from a given source link.
sourceLinkEqualityA functor to check equality of two source links.

Definition at line 48 of file GreedyAmbiguitySolver.cxx.

51 {
52 auto measurement_index_map =
53 std::unordered_map<Acts::SourceLink, std::size_t, source_link_hash_t,
54 source_link_equality_t>(0, sourceLinkHash,
55 sourceLinkEquality);
56
57 // auto tg{geometry()};
58 // Iterate through all input tracks, collect their properties like measurement
59 // count and chi2 and fill the measurement map in order to relate tracks to
60 // each other if they have shared hits_.
61 state.number_of_tracks_ = 0;
62 for (const auto& track : tracks) {
63 // Kick out tracks that do not fulfill our initial requirements
64 if (track.getNhits() < n_meas_min_) {
65 continue;
66 }
67
68 std::vector<std::size_t> measurements;
69 for (auto imeas : track.getMeasurementsIdxs()) {
70 auto meas = meas_coll.at(imeas);
71 const Acts::Surface* hit_surface = tg.getSurface(meas.getLayerID());
72 // Store the index_ source link
73 acts_examples::IndexSourceLink idx_sl(hit_surface->geometryId(), imeas);
74 Acts::SourceLink source_link = Acts::SourceLink(idx_sl);
75
76 auto emplace = measurement_index_map.try_emplace(
77 source_link, measurement_index_map.size());
78 measurements.push_back(emplace.first->second);
79 }
80
81 state.track_tips_.push_back(state.number_of_tracks_);
82 state.track_chi2_.push_back(track.getChi2() / track.getNdf());
83 state.measurements_per_track_.push_back(std::move(measurements));
84 state.selected_tracks_.insert(state.number_of_tracks_);
85
86 ++state.number_of_tracks_;
87 }
88
89 // Now we relate measurements to tracks
90 for (std::size_t i_track = 0; i_track < state.number_of_tracks_; ++i_track) {
91 for (auto i_measurement : state.measurements_per_track_[i_track]) {
92 state.tracks_per_measurement_[i_measurement].insert(i_track);
93 }
94 }
95
96 // Finally, we can accumulate the number of shared measurements per track
97 state.shared_measurements_per_track_ =
98 std::vector<std::size_t>(state.track_tips_.size(), 0);
99 for (std::size_t i_track = 0; i_track < state.number_of_tracks_; ++i_track) {
100 for (auto i_measurement : state.measurements_per_track_[i_track]) {
101 if (state.tracks_per_measurement_[i_measurement].size() > 1) {
102 ++state.shared_measurements_per_track_[i_track];
103 }
104 }
105 }
106}
std::size_t n_meas_min_
Minimum number of measurement to form a track.

References n_meas_min_.

Referenced by produce().

◆ configure()

void tracking::reco::GreedyAmbiguitySolver::configure ( framework::config::Parameters & parameters)
overridevirtual

Configure the processor using the given user specified parameters.

The user specified parameters that are availabed are defined in the python configuration class. Look at the my_processor.py module of the EventProc python for the python structure.

Parameters
parametersSet of parameters used to configure this processor.

Reimplemented from framework::EventProcessor.

Definition at line 169 of file GreedyAmbiguitySolver.cxx.

170 {
171 out_trk_collection_ =
172 parameters.get<std::string>("out_trk_collection", "TaggerTracksClean");
173
174 track_collection_ =
175 parameters.get<std::string>("track_collection", "TaggerTracks");
176
177 meas_collection_ =
178 parameters.get<std::string>("meas_collection", "DigiTaggerSimHits");
179 input_pass_name_ = parameters.get<std::string>("input_pass_name");
180 n_meas_min_ = parameters.get<int>("n_measurements_min", 5);
181 maximum_shared_hits_ = parameters.get<int>("maximum_shared_hits", 1);
182}
const T & get(const std::string &name) const
Retrieve the parameter of the given name.
Definition Parameters.h:75
std::uint32_t maximum_shared_hits_
Maximum amount of shared hits per track.

References framework::config::Parameters::get(), maximum_shared_hits_, and n_meas_min_.

◆ onNewRun()

void tracking::reco::GreedyAmbiguitySolver::onNewRun ( const ldmx::RunHeader & rh)
overridevirtual

onNewRun is the first function called for each processor after the conditions are fully configured and accessible.

This is where you could create single-processors, multi-event calculation objects.

Reimplemented from framework::EventProcessor.

Definition at line 167 of file GreedyAmbiguitySolver.cxx.

167{}

◆ onProcessEnd()

void tracking::reco::GreedyAmbiguitySolver::onProcessEnd ( )
overridevirtual

Callback for the EventProcessor to take any necessary action when the processing of events finishes, such as calculating job-summary quantities.

Reimplemented from framework::EventProcessor.

Definition at line 233 of file GreedyAmbiguitySolver.cxx.

233 {
234 double avg_in =
235 nevents_ > 0 ? static_cast<double>(n_input_tracks_) / nevents_ : 0.;
236 double avg_out =
237 nevents_ > 0 ? static_cast<double>(n_output_tracks_) / nevents_ : 0.;
238 double retention =
239 n_input_tracks_ > 0 ? 100.0 * n_output_tracks_ / n_input_tracks_ : 0.;
240 ldmx_log(info) << "--------------------------------- ";
241 ldmx_log(info) << "GAS: " << n_output_tracks_ << " output tracks / "
242 << n_input_tracks_ << " input tracks";
243 ldmx_log(info) << "AVG Time/Event: " << std::fixed << std::setprecision(1)
244 << processing_time_ / nevents_ << " ms";
245 ldmx_log(info) << "AVG tracks in/event: " << std::fixed
246 << std::setprecision(1) << avg_in;
247 ldmx_log(info) << "AVG tracks out/event: " << std::fixed
248 << std::setprecision(1) << avg_out << " (" << std::fixed
249 << std::setprecision(1) << retention << "% retained)";
250}

◆ produce()

void tracking::reco::GreedyAmbiguitySolver::produce ( framework::Event & event)
overridevirtual

Process the event and put new data products into it.

Parameters
eventThe event to process.

Implements framework::Producer.

Definition at line 184 of file GreedyAmbiguitySolver.cxx.

184 {
185 auto t_start = std::chrono::high_resolution_clock::now();
186
187 GreedyAmbiguitySolver::State state;
188 std::vector<ldmx::Track> out_tracks;
189
190 auto tg{geometry()};
191
192 if (!event.exists(track_collection_, input_pass_name_)) {
193 ldmx_log(debug) << "Track collection not found, exiting";
194 ++nevents_;
195 return;
196 }
197 const auto& tracks =
198 event.getCollection<ldmx::Track>(track_collection_, input_pass_name_);
199
200 if (!event.exists(meas_collection_, input_pass_name_)) {
201 ldmx_log(debug) << "Measurement collection not found, exiting";
202 ++nevents_;
203 return;
204 }
205 const auto& measurements = event.getCollection<ldmx::Measurement>(
206 meas_collection_, input_pass_name_);
207
208 n_input_tracks_ += static_cast<int>(tracks.size());
209
210 computeInitialState(tracks, measurements, state, tg,
211 tracking::sim::utils::sourceLinkHash,
212 tracking::sim::utils::sourceLinkEquality);
213 resolve(state);
214
215 for (auto i_track : state.selected_tracks_) {
216 auto clean_trk = tracks[state.track_tips_.at(i_track)];
217 if ((clean_trk.getNhits() > n_meas_min_) &&
218 (std::abs(1. / clean_trk.getQoP()) > 0.05)) {
219 out_tracks.push_back(clean_trk);
220 }
221 }
222
223 n_output_tracks_ += static_cast<int>(out_tracks.size());
224
225 event.add(out_trk_collection_, out_tracks);
226
227 auto t_end = std::chrono::high_resolution_clock::now();
228 processing_time_ +=
229 std::chrono::duration<double, std::milli>(t_end - t_start).count();
230 ++nevents_;
231}
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.
Definition Event.cxx:107
Implementation of a track object.
Definition Track.h:54
void computeInitialState(std::vector< ldmx::Track > tracks, std::vector< ldmx::Measurement > measurements, State &state, geometry_t &tg, source_link_hash_t &&sourceLinkHash, source_link_equality_t &&sourceLinkEquality) const
void resolve(State &state)
Updates the state iteratively by evicting one track after the other until the final state conditions ...

References computeInitialState(), framework::Event::exists(), n_meas_min_, and resolve().

◆ removeTrack()

void tracking::reco::GreedyAmbiguitySolver::removeTrack ( State & state,
std::size_t iTrack ) const
private
Parameters
stateA state object that was previously filled by the initialization.
iTrack

Definition at line 34 of file GreedyAmbiguitySolver.cxx.

35 {
36 for (auto i_measurement : state.measurements_per_track_[iTrack]) {
37 state.tracks_per_measurement_[i_measurement].erase(iTrack);
38 if (state.tracks_per_measurement_[i_measurement].size() == 1) {
39 auto j_track = *state.tracks_per_measurement_[i_measurement].begin();
40 --state.shared_measurements_per_track_[j_track];
41 }
42 }
43 state.selected_tracks_.erase(iTrack);
44}

Referenced by resolve().

◆ resolve()

void tracking::reco::GreedyAmbiguitySolver::resolve ( State & state)
private

Updates the state iteratively by evicting one track after the other until the final state conditions are met.

Parameters
stateA state object that was previously filled by the initialization.

Compares two tracks based on the number of shared measurements in order to decide if we already met the final state.

Compares two tracks in order to find the one which should be evicted. First we compare the relative amount of shared measurements. If that is indecisive we use the chi2.

Helper to calculate the relative amount of shared measurements.

Definition at line 108 of file GreedyAmbiguitySolver.cxx.

108 {
111 auto shared_measurements_comperator = [&state](std::size_t a, std::size_t b) {
112 return state.shared_measurements_per_track_[a] <
113 state.shared_measurements_per_track_[b];
114 };
115
119 auto track_comperator = [&state](std::size_t a, std::size_t b) {
121 auto relative_shared_measurements = [&state](std::size_t i) {
122 return 1.0 * state.shared_measurements_per_track_[i] /
123 state.measurements_per_track_[i].size();
124 };
125
126 if (relative_shared_measurements(a) != relative_shared_measurements(b)) {
127 return relative_shared_measurements(a) < relative_shared_measurements(b);
128 }
129 return state.track_chi2_[a] < state.track_chi2_[b];
130 };
131
132 for (std::size_t i = 0; i < maximum_iterations_; ++i) {
133 // Lazy out if there is nothing to filter on.
134 if (state.selected_tracks_.empty()) {
135 ldmx_log(trace) << "No tracks left - exit loop";
136 break;
137 }
138
139 // Find the maximum amount of shared measurements per track to decide if we
140 // are done or not.
141 auto maximum_shared_measurements = *std::max_element(
142 state.selected_tracks_.begin(), state.selected_tracks_.end(),
143 shared_measurements_comperator);
144 // ldmx_log(debug) <<
145 // "maximum shared measurements "
146 // << state.sharedMeasurementsPerTrack[maximumSharedMeasurements];
147 if (state.shared_measurements_per_track_[maximum_shared_measurements] <
149 break;
150 }
151
152 // Find the "worst" track by comparing them to each other
153 auto bad_track =
154 *std::max_element(state.selected_tracks_.begin(),
155 state.selected_tracks_.end(), track_comperator);
156 ldmx_log(trace) << "Remove track " << bad_track << ", nMeas = "
157 << state.measurements_per_track_[bad_track].size()
158 << ", nShared = "
159 << state.shared_measurements_per_track_[bad_track]
160 << ", chi2 =" << state.track_chi2_[bad_track];
161 removeTrack(state, bad_track);
162 }
163}
void removeTrack(State &state, std::size_t iTrack) const
std::uint32_t maximum_iterations_
Maximum number of iterations.

References maximum_iterations_, maximum_shared_hits_, and removeTrack().

Referenced by produce().

Member Data Documentation

◆ input_pass_name_

std::string tracking::reco::GreedyAmbiguitySolver::input_pass_name_ {""}
private

Definition at line 100 of file GreedyAmbiguitySolver.h.

100{""};

◆ maximum_iterations_

std::uint32_t tracking::reco::GreedyAmbiguitySolver::maximum_iterations_ {1000}
private

Maximum number of iterations.

Definition at line 89 of file GreedyAmbiguitySolver.h.

89{1000};

Referenced by resolve().

◆ maximum_shared_hits_

std::uint32_t tracking::reco::GreedyAmbiguitySolver::maximum_shared_hits_ {1}
private

Maximum amount of shared hits per track.

Definition at line 87 of file GreedyAmbiguitySolver.h.

87{1};

Referenced by configure(), and resolve().

◆ meas_collection_

std::string tracking::reco::GreedyAmbiguitySolver::meas_collection_ {"DigiTaggerSimHits"}
private

Definition at line 98 of file GreedyAmbiguitySolver.h.

98{"DigiTaggerSimHits"};

◆ n_input_tracks_

int tracking::reco::GreedyAmbiguitySolver::n_input_tracks_ {0}
private

Definition at line 82 of file GreedyAmbiguitySolver.h.

82{0};

◆ n_meas_min_

std::size_t tracking::reco::GreedyAmbiguitySolver::n_meas_min_ {7}
private

Minimum number of measurement to form a track.

Definition at line 92 of file GreedyAmbiguitySolver.h.

92{7};

Referenced by computeInitialState(), configure(), and produce().

◆ n_output_tracks_

int tracking::reco::GreedyAmbiguitySolver::n_output_tracks_ {0}
private

Definition at line 83 of file GreedyAmbiguitySolver.h.

83{0};

◆ nevents_

int tracking::reco::GreedyAmbiguitySolver::nevents_ {0}
private

Definition at line 81 of file GreedyAmbiguitySolver.h.

81{0};

◆ out_trk_collection_

std::string tracking::reco::GreedyAmbiguitySolver::out_trk_collection_ {"TaggerTracksClean"}
private

Definition at line 94 of file GreedyAmbiguitySolver.h.

94{"TaggerTracksClean"};

◆ processing_time_

double tracking::reco::GreedyAmbiguitySolver::processing_time_ {0.}
private

Definition at line 84 of file GreedyAmbiguitySolver.h.

84{0.};

◆ track_collection_

std::string tracking::reco::GreedyAmbiguitySolver::track_collection_ {"TaggerTracks"}
private

Definition at line 96 of file GreedyAmbiguitySolver.h.

96{"TaggerTracks"};

The documentation for this class was generated from the following files: