LDMX Software
trigger::TrigClusterNNProducer Class Reference

Scores each event with the ECal trigger MLP and stores the output. More...

#include <TrigClusterNNProducer.h>

Public Member Functions

 TrigClusterNNProducer (const std::string &name, framework::Process &process)
 
void configure (framework::config::Parameters &ps) 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.
 
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 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.
 
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.
 

Static Public Member Functions

static void fillInputs (const TrigCaloClusterCollection &clusters, const TrigEnergySumCollection &ecal_sums, const TrigEnergySumCollection &hcal_sums, std::vector< float > &inputs)
 Fill flattened (MAX_CLUSTERS, N_FEATURES) model input, (index = slot * N_FEATURES + feature).
 

Static Public Attributes

static constexpr std::size_t MAX_CLUSTERS = 3
 
static constexpr std::size_t N_FEATURES = 9
 
static constexpr std::size_t N_CLASSES = 9
 
static constexpr std::size_t FEAT_X = 0
 feature index within one cluster slot of the model input
 
static constexpr std::size_t FEAT_Y = 1
 
static constexpr std::size_t FEAT_Z = 2
 
static constexpr std::size_t FEAT_E = 3
 
static constexpr std::size_t FEAT_DEPTH = 4
 
static constexpr std::size_t FEAT_N_TP = 5
 
static constexpr std::size_t FEAT_N_CLUSTERS = 6
 
static constexpr std::size_t FEAT_ECAL_SUM_E = 7
 
static constexpr std::size_t FEAT_HCAL_SUM_ADC = 8
 
static constexpr std::size_t BKG_CLASS = 0
 class index of the single-electron background in the model output
 

Private Attributes

std::string cluster_coll_name_
 
std::string ecal_sum_coll_name_
 
std::string hcal_sum_coll_name_
 
std::string input_pass_
 pass name shared by all three input collections
 
std::string score_coll_name_
 
ldmx::ort::FloatArrays data_
 model input buffer, reused every event
 
std::unique_ptr< ldmx::ort::ONNXRuntime > rt_
 
int nevents_ {0}
 
double processing_time_ {0.}
 
double inference_time_ {0.}
 

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
 
NtupleManager & ntuple_ {NtupleManager::getInstance()}
 Manager for any ntuples.
 
logging::logger the_log_
 The logger for this EventProcessor.
 

Detailed Description

Scores each event with the ECal trigger MLP and stores the output.

The model input is the top-3 (energy-sorted) ECal trigger clusters times 9 features: x, y, z, e, depth, n_tp of the cluster, then the event-level n_clusters (before truncation), the sum of the ECal trigger sums (MeV) and the sum of the HCal back-layer trigger sums (ADC), repeated in every slot.

Required upstream sequence with default parameters: EcalTrigPrimDigiProducer, HcalTrigPrimDigiProducer, EcalTPSelector, TrigHcalEnergySum and TrigEcalClusterProducer.

Output: a TrigClusterNNScore with the raw logits and P(bkg).

Definition at line 40 of file TrigClusterNNProducer.h.

Constructor & Destructor Documentation

◆ TrigClusterNNProducer()

trigger::TrigClusterNNProducer::TrigClusterNNProducer ( const std::string & name,
framework::Process & process )

Definition at line 36 of file TrigClusterNNProducer.cxx.

39 data_{std::vector<float>(MAX_CLUSTERS * N_FEATURES, 0.f)} {}
Base class for a module which produces a data product.
virtual void process(Event &event) final
Processing an event for a Producer is calling produce.
ldmx::ort::FloatArrays data_
model input buffer, reused every event

Member Function Documentation

◆ configure()

void trigger::TrigClusterNNProducer::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 class_name equal to the EventProcessor class name.

For an example, look at MyProcessor.

Parameters
parametersParameters for configuration.

Reimplemented from framework::EventProcessor.

Definition at line 41 of file TrigClusterNNProducer.cxx.

41 {
42 cluster_coll_name_ = ps.get<std::string>("cluster_coll_name");
43 ecal_sum_coll_name_ = ps.get<std::string>("ecal_sum_coll_name");
44 hcal_sum_coll_name_ = ps.get<std::string>("hcal_sum_coll_name");
45 input_pass_ = ps.get<std::string>("input_pass");
46 score_coll_name_ = ps.get<std::string>("score_coll_name");
47
48 const auto model_path{ps.get<std::string>("model_path")};
49 if (model_path.empty()) {
50 EXCEPTION_RAISE("NoModel",
51 "model_path is empty. Pass the path to "
52 "ecal_nn_trigger.onnx.");
53 }
54 rt_ = std::make_unique<ldmx::ort::ONNXRuntime>(model_path);
55
56 // the batch dimension is reported as -1 by the wrapper
57 const std::vector<int64_t> expected_shape{-1,
58 static_cast<int64_t>(N_CLASSES)};
59 const auto& outputs = rt_->getOutputNames();
60 if (std::find(outputs.begin(), outputs.end(), "logits") == outputs.end() or
61 rt_->getOutputShape("logits") != expected_shape) {
62 EXCEPTION_RAISE("BadModel", "Model '" + model_path +
63 "' does not have a 'logits' output of "
64 "shape (batch, " +
65 std::to_string(N_CLASSES) + ").");
66 }
67}
std::string input_pass_
pass name shared by all three input collections

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

◆ fillInputs()

void trigger::TrigClusterNNProducer::fillInputs ( const TrigCaloClusterCollection & clusters,
const TrigEnergySumCollection & ecal_sums,
const TrigEnergySumCollection & hcal_sums,
std::vector< float > & inputs )
static

Fill flattened (MAX_CLUSTERS, N_FEATURES) model input, (index = slot * N_FEATURES + feature).

Unused cluster slots are zero.

Parameters
[in]clustersECal trigger clusters, in collection order
[in]ecal_sumsECal trigger energy sums (energy() is used)
[in]hcal_sumsHCal back-layer trigger sums (hwEnergy() is used)
[out]inputsresized to MAX_CLUSTERS * N_FEATURES and filled

Definition at line 113 of file TrigClusterNNProducer.cxx.

116 {
117 inputs.assign(MAX_CLUSTERS * N_FEATURES, 0.f);
118
119 // sort cluster indices by descending energy
120 std::vector<std::size_t> order(clusters.size());
121 std::iota(order.begin(), order.end(), 0);
122 std::stable_sort(order.begin(), order.end(),
123 [&clusters](const std::size_t lhs, const std::size_t rhs) {
124 return clusters[lhs].e() > clusters[rhs].e();
125 });
126
127 double ecal_sum_e = 0.;
128 for (const auto& sum : ecal_sums) ecal_sum_e += sum.energy();
129 // the HCal sums carry hardware (ADC) energy
130 int hcal_sum_adc = 0;
131 for (const auto& sum : hcal_sums) hcal_sum_adc += sum.hwEnergy();
132
133 for (std::size_t slot = 0; slot < MAX_CLUSTERS; ++slot) {
134 float* feats = &inputs[slot * N_FEATURES];
135 if (slot < order.size()) {
136 const auto& cluster = clusters[order[slot]];
137 feats[FEAT_X] = cluster.x();
138 feats[FEAT_Y] = cluster.y();
139 feats[FEAT_Z] = cluster.z();
140 feats[FEAT_E] = cluster.e();
141 feats[FEAT_DEPTH] = cluster.depth();
142 feats[FEAT_N_TP] = cluster.nTP();
143 }
144 // event-level features are filled in every slot
145 feats[FEAT_N_CLUSTERS] = clusters.size();
146 feats[FEAT_ECAL_SUM_E] = ecal_sum_e;
147 feats[FEAT_HCAL_SUM_ADC] = hcal_sum_adc;
148 }
149}
static constexpr std::size_t FEAT_X
feature index within one cluster slot of the model input

◆ onProcessEnd()

void trigger::TrigClusterNNProducer::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 106 of file TrigClusterNNProducer.cxx.

106 {
107 if (nevents_ == 0) return;
108 ldmx_log(info) << "AVG Time/Event: total " << std::fixed
109 << std::setprecision(3) << processing_time_ / nevents_
110 << " ms, inference " << inference_time_ / nevents_ << " ms";
111}

◆ produce()

void trigger::TrigClusterNNProducer::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 69 of file TrigClusterNNProducer.cxx.

69 {
70 const auto start = std::chrono::high_resolution_clock::now();
71 nevents_++;
72
73 // throws an exception if an input is missing or found in more than
74 // one pass; an empty input is valid and the event is still scored
75 const auto& clusters{
76 event.getCollection<TrigCaloCluster>(cluster_coll_name_, input_pass_)};
77 const auto& ecal_sums{
78 event.getCollection<TrigEnergySum>(ecal_sum_coll_name_, input_pass_)};
79 const auto& hcal_sums{
80 event.getCollection<TrigEnergySum>(hcal_sum_coll_name_, input_pass_)};
81
82 fillInputs(clusters, ecal_sums, hcal_sums, data_[0]);
83 const auto inference_start = std::chrono::high_resolution_clock::now();
84 const auto logits{rt_->run({"x"}, data_, {"logits"})[0]};
85 inference_time_ +=
86 std::chrono::duration<double, std::milli>(
87 std::chrono::high_resolution_clock::now() - inference_start)
88 .count();
89
90 // P(bkg) in double: 1 - P(bkg) saturates to 1 in float
91 const double p_bkg = std::exp(logSoftmax(logits)[BKG_CLASS]);
92
93 ldmx_log(debug) << "n_clusters = " << clusters.size()
94 << ", P(bkg) = " << p_bkg;
95
96 TrigClusterNNScore score;
97 score.setLogits(logits);
98 score.setPBkg(p_bkg);
99 event.add(score_coll_name_, score);
100
101 processing_time_ += std::chrono::duration<double, std::milli>(
102 std::chrono::high_resolution_clock::now() - start)
103 .count();
104}
static void fillInputs(const TrigCaloClusterCollection &clusters, const TrigEnergySumCollection &ecal_sums, const TrigEnergySumCollection &hcal_sums, std::vector< float > &inputs)
Fill flattened (MAX_CLUSTERS, N_FEATURES) model input, (index = slot * N_FEATURES + feature).
static constexpr std::size_t BKG_CLASS
class index of the single-electron background in the model output

Member Data Documentation

◆ BKG_CLASS

std::size_t trigger::TrigClusterNNProducer::BKG_CLASS = 0
staticconstexpr

class index of the single-electron background in the model output

Definition at line 79 of file TrigClusterNNProducer.h.

◆ cluster_coll_name_

std::string trigger::TrigClusterNNProducer::cluster_coll_name_
private

Definition at line 82 of file TrigClusterNNProducer.h.

◆ data_

ldmx::ort::FloatArrays trigger::TrigClusterNNProducer::data_
private

model input buffer, reused every event

Definition at line 90 of file TrigClusterNNProducer.h.

◆ ecal_sum_coll_name_

std::string trigger::TrigClusterNNProducer::ecal_sum_coll_name_
private

Definition at line 83 of file TrigClusterNNProducer.h.

◆ FEAT_DEPTH

std::size_t trigger::TrigClusterNNProducer::FEAT_DEPTH = 4
staticconstexpr

Definition at line 71 of file TrigClusterNNProducer.h.

◆ FEAT_E

std::size_t trigger::TrigClusterNNProducer::FEAT_E = 3
staticconstexpr

Definition at line 70 of file TrigClusterNNProducer.h.

◆ FEAT_ECAL_SUM_E

std::size_t trigger::TrigClusterNNProducer::FEAT_ECAL_SUM_E = 7
staticconstexpr

Definition at line 74 of file TrigClusterNNProducer.h.

◆ FEAT_HCAL_SUM_ADC

std::size_t trigger::TrigClusterNNProducer::FEAT_HCAL_SUM_ADC = 8
staticconstexpr

Definition at line 75 of file TrigClusterNNProducer.h.

◆ FEAT_N_CLUSTERS

std::size_t trigger::TrigClusterNNProducer::FEAT_N_CLUSTERS = 6
staticconstexpr

Definition at line 73 of file TrigClusterNNProducer.h.

◆ FEAT_N_TP

std::size_t trigger::TrigClusterNNProducer::FEAT_N_TP = 5
staticconstexpr

Definition at line 72 of file TrigClusterNNProducer.h.

◆ FEAT_X

std::size_t trigger::TrigClusterNNProducer::FEAT_X = 0
staticconstexpr

feature index within one cluster slot of the model input

Definition at line 67 of file TrigClusterNNProducer.h.

◆ FEAT_Y

std::size_t trigger::TrigClusterNNProducer::FEAT_Y = 1
staticconstexpr

Definition at line 68 of file TrigClusterNNProducer.h.

◆ FEAT_Z

std::size_t trigger::TrigClusterNNProducer::FEAT_Z = 2
staticconstexpr

Definition at line 69 of file TrigClusterNNProducer.h.

◆ hcal_sum_coll_name_

std::string trigger::TrigClusterNNProducer::hcal_sum_coll_name_
private

Definition at line 84 of file TrigClusterNNProducer.h.

◆ inference_time_

double trigger::TrigClusterNNProducer::inference_time_ {0.}
private

Definition at line 95 of file TrigClusterNNProducer.h.

95{0.};

◆ input_pass_

std::string trigger::TrigClusterNNProducer::input_pass_
private

pass name shared by all three input collections

Definition at line 86 of file TrigClusterNNProducer.h.

◆ MAX_CLUSTERS

std::size_t trigger::TrigClusterNNProducer::MAX_CLUSTERS = 3
staticconstexpr

Definition at line 62 of file TrigClusterNNProducer.h.

◆ N_CLASSES

std::size_t trigger::TrigClusterNNProducer::N_CLASSES = 9
staticconstexpr

Definition at line 64 of file TrigClusterNNProducer.h.

◆ N_FEATURES

std::size_t trigger::TrigClusterNNProducer::N_FEATURES = 9
staticconstexpr

Definition at line 63 of file TrigClusterNNProducer.h.

◆ nevents_

int trigger::TrigClusterNNProducer::nevents_ {0}
private

Definition at line 93 of file TrigClusterNNProducer.h.

93{0};

◆ processing_time_

double trigger::TrigClusterNNProducer::processing_time_ {0.}
private

Definition at line 94 of file TrigClusterNNProducer.h.

94{0.};

◆ rt_

std::unique_ptr<ldmx::ort::ONNXRuntime> trigger::TrigClusterNNProducer::rt_
private

Definition at line 91 of file TrigClusterNNProducer.h.

◆ score_coll_name_

std::string trigger::TrigClusterNNProducer::score_coll_name_
private

Definition at line 87 of file TrigClusterNNProducer.h.


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