LDMX Software
ecal::EcalPnetVetoProcessor Class Reference

Determines if event is vetoable using ECAL hit information w/ a deep neural network. More...

#include <EcalPnetVetoProcessor.h>

Public Member Functions

 EcalPnetVetoProcessor (const std::string &name, framework::Process &process)
 
void configure (framework::config::Parameters &parameters) override
 Callback for the EventProcessor to configure itself from the given set of parameters.
 
void produce (framework::Event &event) override
 Process the event and put new data products into it.
 
- 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 onNewRun (const ldmx::RunHeader &run_header)
 Callback for the EventProcessor to take any necessary action when the run being processed changes.
 
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.
 
virtual void onProcessEnd ()
 Callback for the EventProcessor to take any necessary action when the processing of events finishes, such as calculating job-summary quantities.
 
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

void makeInputs (const ldmx::EcalGeometry &geom, const std::vector< ldmx::EcalHit > &ecalRecHits, std::array< double, 3 > etraj, std::array< double, 3 > enorm)
 Make inputs to the DNN from ECAL RecHits.
 
std::vector< float > logSoftmax (const std::vector< float > &logits)
 Transform logits to a probability.
 

Private Attributes

float disc_cut_ = -99
 
std::vector< std::vector< float > > data_
 
std::unique_ptr< ldmx::ort::ONNXRuntimert_
 
std::string collection_name_ {"EcalPnetVeto"}
 Name of the collection which will containt the results.
 
std::string rec_coll_name_
 
std::string ecal_rec_hits_passname_
 
std::string ecal_sp_hits_passname_
 
std::string track_pass_name_
 
std::string track_collection_
 
bool recoil_from_tracking_
 

Static Private Attributes

static constexpr unsigned int MAX_NUM_HITS = 300
 Maximum number of hits allowed in ECAL.
 
static constexpr unsigned int N_COORDINATE_DIM = 3
 
static constexpr unsigned int COORDINATE_X_OFFSET = 0
 
static constexpr unsigned int COORDINATE_Y_OFFSET = MAX_NUM_HITS
 
static constexpr unsigned int COORDINATE_Z_OFFSET = 2 * MAX_NUM_HITS
 
static constexpr unsigned int N_FEATURE_DIM = 5
 
static constexpr unsigned int FEATURE_X_OFFSET = 0
 
static constexpr unsigned int FEATURE_Y_OFFSET = MAX_NUM_HITS
 
static constexpr unsigned int FEATURE_Z_OFFSET = 2 * MAX_NUM_HITS
 
static constexpr unsigned int FEATURE_ENERGY_OFFSET = 3 * MAX_NUM_HITS
 
static constexpr unsigned int FEATURE_LAYER_ID_OFFSET = 4 * MAX_NUM_HITS
 
static const std::vector< std::string > INPUT_NAMES
 
static const std::vector< unsigned int > INPUT_SIZES
 

Additional Inherited Members

- 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

Determines if event is vetoable using ECAL hit information w/ a deep neural network.

Definition at line 31 of file EcalPnetVetoProcessor.h.

Constructor & Destructor Documentation

◆ EcalPnetVetoProcessor()

ecal::EcalPnetVetoProcessor::EcalPnetVetoProcessor ( const std::string & name,
framework::Process & process )

Definition at line 10 of file EcalPnetVetoProcessor.cxx.

12 : Producer(name, process) {
13 for (const auto& s : INPUT_SIZES) {
14 data_.emplace_back(s, 0);
15 }
16}
Producer(const std::string &name, Process &process)
Class constructor.
virtual void process(Event &event) final
Processing an event for a Producer is calling produce.

Member Function Documentation

◆ configure()

void ecal::EcalPnetVetoProcessor::configure ( framework::config::Parameters & parameters)
overridevirtual

Callback for the EventProcessor to configure itself from the given set of parameters.

The parameters a processor has access to are the member variables of the python class in the sequence that has className equal to the EventProcessor class name.

For an example, look at MyProcessor.

Parameters
parametersParameters for configuration.

Reimplemented from framework::EventProcessor.

Definition at line 18 of file EcalPnetVetoProcessor.cxx.

19 {
20 disc_cut_ = parameters.get<double>("disc_cut");
21 rt_ = std::make_unique<ldmx::ort::ONNXRuntime>(
22 parameters.get<std::string>("model_path"));
23
24 // Set the collection name as defined in the configuration
25 collection_name_ = parameters.get<std::string>("collection_name");
26
27 rec_coll_name_ = parameters.get<std::string>("rec_coll_name");
28 ecal_rec_hits_passname_ =
29 parameters.get<std::string>("ecal_rec_hits_passname");
30 ecal_sp_hits_passname_ = parameters.get<std::string>("ecal_sp_hits_passname");
31 track_pass_name_ = parameters.get<std::string>("track_pass_name", "");
32 track_collection_ = parameters.get<std::string>("track_collection");
33 recoil_from_tracking_ = parameters.get<bool>("recoil_from_tracking");
34}
std::string collection_name_
Name of the collection which will containt the results.
const T & get(const std::string &name) const
Retrieve the parameter of the given name.
Definition Parameters.h:78

References framework::config::Parameters::get().

◆ logSoftmax()

std::vector< float > ecal::EcalPnetVetoProcessor::logSoftmax ( const std::vector< float > & logits)
private

Transform logits to a probability.

Parameters
logitsVector of logits
Returns
Vector of log softmax probabilities

Definition at line 197 of file EcalPnetVetoProcessor.cxx.

198 {
199 // Find max for numerical stability
200 auto max_val = *std::max_element(logits.begin(), logits.end());
201
202 // Compute shifted exponentials and their sum
203 std::vector<float> exp_vals(logits.size());
204 for (size_t i = 0; i < logits.size(); ++i) {
205 exp_vals[i] = std::exp(logits[i] - max_val);
206 }
207
208 float sum_exp = std::accumulate(exp_vals.begin(), exp_vals.end(), 0.0);
209 float log_sum_exp = max_val + std::log(sum_exp);
210
211 // Compute log_softmax
212 std::vector<float> result(logits.size());
213 for (size_t i = 0; i < logits.size(); ++i) {
214 result[i] = logits[i] - log_sum_exp;
215 }
216
217 return result;
218}

◆ makeInputs()

void ecal::EcalPnetVetoProcessor::makeInputs ( const ldmx::EcalGeometry & geom,
const std::vector< ldmx::EcalHit > & ecalRecHits,
std::array< double, 3 > etraj,
std::array< double, 3 > enorm )
private

Make inputs to the DNN from ECAL RecHits.

Parameters
ecalRecHitsThe EcalHit collection.

Definition at line 148 of file EcalPnetVetoProcessor.cxx.

151 {
152 // clear data
153 for (auto& v : data_) {
154 std::fill(v.begin(), v.end(), 0);
155 }
156
157 // Loop on the rechits
158 unsigned idx = 0;
159 for (const auto& hit : ecal_rec_hits) {
160 if (hit.getEnergy() <= 0) {
161 ldmx_log(warn)
162 << "Hit with zero energy found, should not happen, skipping it.";
163 continue;
164 }
165 ldmx::EcalID id(hit.getID());
166 auto [hit_x, hit_y, hit_z] = geom.getPosition(id);
167
168 double delta_z = hit_z - etraj[2];
169 double etraj_x = etraj[0] + enorm[0] * delta_z;
170 double etraj_y = etraj[1] + enorm[1] * delta_z;
171 data_[0].at(COORDINATE_X_OFFSET + idx) = hit_x - etraj_x;
172 data_[0].at(COORDINATE_Y_OFFSET + idx) = hit_y - etraj_y;
173 data_[0].at(COORDINATE_Z_OFFSET + idx) = hit_z;
174
175 data_[1].at(FEATURE_X_OFFSET + idx) = hit_x - etraj_x;
176 data_[1].at(FEATURE_Y_OFFSET + idx) = hit_y - etraj_y;
177 data_[1].at(FEATURE_Z_OFFSET + idx) = hit_z;
178 data_[1].at(FEATURE_LAYER_ID_OFFSET + idx) = id.layer();
179 data_[1].at(FEATURE_ENERGY_OFFSET + idx) = std::log(hit.getEnergy());
180
181 ++idx;
182 }
183
184 std::stringstream ss;
185 for (unsigned iname = 0; iname < INPUT_NAMES.size(); ++iname) {
186 ss << "=== " << INPUT_NAMES[iname] << " ===";
187 for (unsigned i = 0; i < INPUT_SIZES[iname]; ++i) {
188 ss << data_[iname].at(i) << ", ";
189 if ((i + 1) % MAX_NUM_HITS == 0) {
190 ss << "\n\n";
191 }
192 }
193 }
194 ldmx_log(trace) << ss.str();
195} // end of make inputs
static constexpr unsigned int MAX_NUM_HITS
Maximum number of hits allowed in ECAL.
std::tuple< double, double, double > getPosition(EcalID id) const
Get a cell's position from its ID number.
Extension of DetectorID providing access to ECal layers and cell numbers in a hex grid.
Definition EcalID.h:20

References ldmx::EcalGeometry::getPosition().

◆ produce()

void ecal::EcalPnetVetoProcessor::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 36 of file EcalPnetVetoProcessor.cxx.

36 {
38 // Get the Ecal Geometry
39 const auto& ecal_geometry = getCondition<ldmx::EcalGeometry>(
40 ldmx::EcalGeometry::CONDITIONS_OBJECT_NAME);
41
42 // Get the collection of digitized Ecal hits_ from the event.
43 const auto ecal_rec_hits = event.getCollection<ldmx::EcalHit>(
44 rec_coll_name_, ecal_rec_hits_passname_);
45 auto nhits = std::count_if(
46 ecal_rec_hits.begin(), ecal_rec_hits.end(),
47 [](const ldmx::EcalHit& hit) { return hit.getEnergy() > 0; });
48
49 // check number of hits_
50 ldmx_log(debug) << "nhits = " << nhits << " MAX_NUM_HITS = " << MAX_NUM_HITS;
51 if (nhits < MAX_NUM_HITS) {
52 std::array<double, 3> etraj = {-999., -999., -999.};
53 std::array<double, 3> enorm = {-999., -999., -999.};
54 // Compute electron trajectory}
55 const ldmx::SimTrackerHit* electron_hit = nullptr;
56 // Use Scoring Plane or Tracking
57 if (!recoil_from_tracking_ &&
58 event.exists("EcalScoringPlaneHits", ecal_sp_hits_passname_)) {
59 auto const& ecal_sp_hits = event.getCollection<ldmx::SimTrackerHit>(
60 "EcalScoringPlaneHits", ecal_sp_hits_passname_);
61 double electron_pz_max = -1.0;
62 for (auto const& hit : ecal_sp_hits) {
63 // Look at the electron only
64 if (hit.getPdgID() != 11) continue;
65 double electron_z = hit.getPosition()[2];
66 // Look at the SP in front of the ECAL
67 if (electron_z <= 239.0 || electron_z >= 240.0) continue;
68 double electron_pz = hit.getMomentum()[2];
69 // Find the highest pz electron
70 if (electron_pz > electron_pz_max) {
71 electron_pz_max = electron_pz;
72 electron_hit = &hit;
73 }
74 }
75 // If we found an electron hit at the scoring plane
76 if (electron_hit) {
77 // Get electron hit position/momentum at Ecal surface
78 ldmx_log(debug) << "Electron Found in the Ecal SP!";
79 auto pos = electron_hit->getPosition();
80 auto mom = electron_hit->getMomentum();
81 ldmx_log(debug) << "ECAL SP pos_=(" << pos[0] << "," << pos[1] << ","
82 << pos[2] << ")";
83 ldmx_log(debug) << "ECAL SP mom=(" << mom[0] << "," << mom[1] << ","
84 << mom[2] << ")";
85 etraj = {pos[0], pos[1], pos[2]};
86 double pz = mom[2];
87 if (pz != 0) {
88 // z_-normalized momentum
89 enorm = {mom[0] / pz, mom[1] / pz, 1.0};
90 }
91 }
92 } else if (recoil_from_tracking_) {
93 // Use tracking to get electron hit position/momentum at Ecal surface
94 auto recoil_tracks{event.getCollection<ldmx::Track>(track_collection_,
95 track_pass_name_)};
96 ldmx::TrackStateType ts_at_ecal = ldmx::TrackStateType::AtECAL;
97 auto recoil_track_states_ecal =
98 ecal::trackProp(recoil_tracks, ts_at_ecal, "ecal");
99 if (!recoil_track_states_ecal.empty()) {
100 std::array<double, 3> pos = {recoil_track_states_ecal[0],
101 recoil_track_states_ecal[1],
102 recoil_track_states_ecal[2]};
103 std::array<double, 3> mom = {(recoil_track_states_ecal[3]),
104 (recoil_track_states_ecal[4]),
105 (recoil_track_states_ecal[5])};
106 ldmx_log(debug) << "Electron track pos_=(" << pos[0] << "," << pos[1]
107 << "," << pos[2] << ")";
108 ldmx_log(debug) << "Electron track mom=(" << mom[0] << "," << mom[1]
109 << "," << mom[2] << ")";
110 etraj = pos;
111 double pz = mom[2];
112 if (pz != 0) {
113 enorm = {mom[0] / pz, mom[1] / pz, 1.0};
114 }
115 } else {
116 ldmx_log(info) << " No recoil track at ECAL";
117 }
118 } else {
119 ldmx_log(fatal) << " No electron hit at scoring plane or no tracking";
120 }
121
122 // make inputs
123 makeInputs(ecal_geometry, ecal_rec_hits, etraj, enorm);
124 // run the DNN
125 auto logits = rt_->run(INPUT_NAMES, data_)[0];
126 // make a log softmax of the logits then transform back
127 // to a probability with an exponential
128 auto prob = std::exp((logSoftmax(logits)[1]));
129 result.setDiscValue(prob);
130 } else {
131 result.setDiscValue(-99);
132 }
133
134 ldmx_log(debug) << "ParticleNet disc value = " << result.getDisc();
135
136 result.setVetoResult(result.getDisc() > disc_cut_);
137
138 // If the event passes the veto, keep it. Otherwise, drop the event.
139 if (result.passesVeto()) {
141 } else {
143 }
144
145 event.add(collection_name_, result);
146}
std::vector< float > trackProp(const ldmx::Tracks &tracks, ldmx::TrackStateType ts_type, const std::string &ts_title)
Return a vector of parameters for a propagated recoil track.
Definition EcalHelper.cxx:5
std::vector< float > logSoftmax(const std::vector< float > &logits)
Transform logits to a probability.
void makeInputs(const ldmx::EcalGeometry &geom, const std::vector< ldmx::EcalHit > &ecalRecHits, std::array< double, 3 > etraj, std::array< double, 3 > enorm)
Make inputs to the DNN from ECAL RecHits.
const T & getCondition(const std::string &condition_name)
Access a conditions object for the current event.
void setStorageHint(framework::StorageControl::Hint hint)
Mark the current event as having the given storage control hint from this module_.
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:92
Stores reconstructed hit information from the ECAL.
Definition EcalHit.h:19
bool passesVeto() const
Checks if the event passes the Ecal veto.
Represents a simulated tracker hit in the simulation.
std::vector< float > getPosition() const
Get the XYZ position of the hit [mm].
std::vector< double > getMomentum() const
Get the XYZ momentum of the particle at the position at which the hit took place [MeV].
Implementation of a track object.
Definition Track.h:53
constexpr StorageControl::Hint HINT_SHOULD_DROP
storage control hint alias for backwards compatibility
constexpr StorageControl::Hint HINT_SHOULD_KEEP
storage control hint alias for backwards compatibility

References framework::Event::exists(), ldmx::SimTrackerHit::getMomentum(), ldmx::SimTrackerHit::getPosition(), framework::HINT_SHOULD_DROP, framework::HINT_SHOULD_KEEP, ldmx::EcalVetoResult::passesVeto(), and ecal::trackProp().

Member Data Documentation

◆ collection_name_

std::string ecal::EcalPnetVetoProcessor::collection_name_ {"EcalPnetVeto"}
private

Name of the collection which will containt the results.

Definition at line 79 of file EcalPnetVetoProcessor.h.

79{"EcalPnetVeto"};

◆ COORDINATE_X_OFFSET

unsigned int ecal::EcalPnetVetoProcessor::COORDINATE_X_OFFSET = 0
staticconstexprprivate

Definition at line 60 of file EcalPnetVetoProcessor.h.

◆ COORDINATE_Y_OFFSET

unsigned int ecal::EcalPnetVetoProcessor::COORDINATE_Y_OFFSET = MAX_NUM_HITS
staticconstexprprivate

Definition at line 61 of file EcalPnetVetoProcessor.h.

◆ COORDINATE_Z_OFFSET

unsigned int ecal::EcalPnetVetoProcessor::COORDINATE_Z_OFFSET = 2 * MAX_NUM_HITS
staticconstexprprivate

Definition at line 62 of file EcalPnetVetoProcessor.h.

◆ data_

std::vector<std::vector<float> > ecal::EcalPnetVetoProcessor::data_
private

Definition at line 75 of file EcalPnetVetoProcessor.h.

◆ disc_cut_

float ecal::EcalPnetVetoProcessor::disc_cut_ = -99
private

Definition at line 74 of file EcalPnetVetoProcessor.h.

◆ ecal_rec_hits_passname_

std::string ecal::EcalPnetVetoProcessor::ecal_rec_hits_passname_
private

Definition at line 82 of file EcalPnetVetoProcessor.h.

◆ ecal_sp_hits_passname_

std::string ecal::EcalPnetVetoProcessor::ecal_sp_hits_passname_
private

Definition at line 83 of file EcalPnetVetoProcessor.h.

◆ FEATURE_ENERGY_OFFSET

unsigned int ecal::EcalPnetVetoProcessor::FEATURE_ENERGY_OFFSET = 3 * MAX_NUM_HITS
staticconstexprprivate

Definition at line 68 of file EcalPnetVetoProcessor.h.

◆ FEATURE_LAYER_ID_OFFSET

unsigned int ecal::EcalPnetVetoProcessor::FEATURE_LAYER_ID_OFFSET = 4 * MAX_NUM_HITS
staticconstexprprivate

Definition at line 69 of file EcalPnetVetoProcessor.h.

◆ FEATURE_X_OFFSET

unsigned int ecal::EcalPnetVetoProcessor::FEATURE_X_OFFSET = 0
staticconstexprprivate

Definition at line 65 of file EcalPnetVetoProcessor.h.

◆ FEATURE_Y_OFFSET

unsigned int ecal::EcalPnetVetoProcessor::FEATURE_Y_OFFSET = MAX_NUM_HITS
staticconstexprprivate

Definition at line 66 of file EcalPnetVetoProcessor.h.

◆ FEATURE_Z_OFFSET

unsigned int ecal::EcalPnetVetoProcessor::FEATURE_Z_OFFSET = 2 * MAX_NUM_HITS
staticconstexprprivate

Definition at line 67 of file EcalPnetVetoProcessor.h.

◆ INPUT_NAMES

const std::vector< std::string > ecal::EcalPnetVetoProcessor::INPUT_NAMES
staticprivate
Initial value:
{"points",
"features"}

Definition at line 71 of file EcalPnetVetoProcessor.h.

◆ INPUT_SIZES

const std::vector< unsigned int > ecal::EcalPnetVetoProcessor::INPUT_SIZES
staticprivate
Initial value:
{
N_COORDINATE_DIM * MAX_NUM_HITS, N_FEATURE_DIM* MAX_NUM_HITS}

Definition at line 72 of file EcalPnetVetoProcessor.h.

◆ MAX_NUM_HITS

unsigned int ecal::EcalPnetVetoProcessor::MAX_NUM_HITS = 300
staticconstexprprivate

Maximum number of hits allowed in ECAL.

Events with more hits will be marked as BKG directly without running ParticleNet.

Definition at line 57 of file EcalPnetVetoProcessor.h.

◆ N_COORDINATE_DIM

unsigned int ecal::EcalPnetVetoProcessor::N_COORDINATE_DIM = 3
staticconstexprprivate

Definition at line 59 of file EcalPnetVetoProcessor.h.

◆ N_FEATURE_DIM

unsigned int ecal::EcalPnetVetoProcessor::N_FEATURE_DIM = 5
staticconstexprprivate

Definition at line 64 of file EcalPnetVetoProcessor.h.

◆ rec_coll_name_

std::string ecal::EcalPnetVetoProcessor::rec_coll_name_
private

Definition at line 81 of file EcalPnetVetoProcessor.h.

◆ recoil_from_tracking_

bool ecal::EcalPnetVetoProcessor::recoil_from_tracking_
private

Definition at line 86 of file EcalPnetVetoProcessor.h.

◆ rt_

std::unique_ptr<ldmx::ort::ONNXRuntime> ecal::EcalPnetVetoProcessor::rt_
private

Definition at line 76 of file EcalPnetVetoProcessor.h.

◆ track_collection_

std::string ecal::EcalPnetVetoProcessor::track_collection_
private

Definition at line 85 of file EcalPnetVetoProcessor.h.

◆ track_pass_name_

std::string ecal::EcalPnetVetoProcessor::track_pass_name_
private

Definition at line 84 of file EcalPnetVetoProcessor.h.


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