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::TrackersTrackingGeometrygeometry ()
 
void loadBField (const std::string &path, const std::vector< double > &map_offset={0., 0., 0.})
 Load the interpolated B-field map from path and cache it.
 
void loadBField (const std::vector< double > &map_offset={0., 0., 0.})
 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.
 
- Protected Attributes inherited from framework::EventProcessor
HistogramPool histograms_
 helper object for making and filling histograms
 
NtupleManagerntuple_ {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 49 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 13 of file GreedyAmbiguitySolver.cxx.

15 : 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 45 of file GreedyAmbiguitySolver.cxx.

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

167 {
168 out_trk_collection_ =
169 parameters.get<std::string>("out_trk_collection", "TaggerTracksClean");
170
171 track_collection_ =
172 parameters.get<std::string>("track_collection", "TaggerTracks");
173
174 meas_collection_ =
175 parameters.get<std::string>("meas_collection", "DigiTaggerSimHits");
176 input_pass_name_ = parameters.get<std::string>("input_pass_name");
177 n_meas_min_ = parameters.get<int>("n_measurements_min", 5);
178 maximum_shared_hits_ = parameters.get<int>("maximum_shared_hits", 1);
179}
const T & get(const std::string &name) const
Retrieve the parameter of the given name.
Definition Parameters.h:78
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 164 of file GreedyAmbiguitySolver.cxx.

164{}

◆ 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 230 of file GreedyAmbiguitySolver.cxx.

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

◆ 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 181 of file GreedyAmbiguitySolver.cxx.

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

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

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 105 of file GreedyAmbiguitySolver.cxx.

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

111{""};

◆ maximum_iterations_

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

Maximum number of iterations.

Definition at line 100 of file GreedyAmbiguitySolver.h.

100{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 98 of file GreedyAmbiguitySolver.h.

98{1};

Referenced by configure(), and resolve().

◆ meas_collection_

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

Definition at line 109 of file GreedyAmbiguitySolver.h.

109{"DigiTaggerSimHits"};

◆ n_input_tracks_

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

Definition at line 93 of file GreedyAmbiguitySolver.h.

93{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 103 of file GreedyAmbiguitySolver.h.

103{7};

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

◆ n_output_tracks_

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

Definition at line 94 of file GreedyAmbiguitySolver.h.

94{0};

◆ nevents_

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

Definition at line 92 of file GreedyAmbiguitySolver.h.

92{0};

◆ out_trk_collection_

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

Definition at line 105 of file GreedyAmbiguitySolver.h.

105{"TaggerTracksClean"};

◆ processing_time_

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

Definition at line 95 of file GreedyAmbiguitySolver.h.

95{0.};

◆ track_collection_

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

Definition at line 107 of file GreedyAmbiguitySolver.h.

107{"TaggerTracks"};

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