LDMX Software
EcalRecProducer.cxx
Go to the documentation of this file.
1
8
9#include <algorithm>
10#include <cmath>
11
13#include "DetDescr/EcalID.h"
14#include "Ecal/EcalReconConditions.h"
15#include "Ecal/Event/EcalHit.h"
19#include "Tools/PulseFitter.h"
20
21namespace ecal {
22
23EcalRecProducer::EcalRecProducer(const std::string& name,
24 framework::Process& process)
25 : Producer(name, process) {}
26
28 // collection names
29 digi_coll_name_ = ps.get<std::string>("digi_coll_name");
30 digi_pass_name_ = ps.get<std::string>("digi_pass_name");
31 sim_hit_coll_name_ = ps.get<std::string>("sim_hit_coll_name");
32 sim_hit_pass_name_ = ps.get<std::string>("sim_hit_pass_name");
33 rec_hit_coll_name_ = ps.get<std::string>("rec_hit_coll_name");
34
35 layer_weights_ = ps.get<std::vector<double>>("layer_weights");
37 ps.get<double>("second_order_energy_correction");
38
39 mip_si_energy_ = ps.get<double>("mip_si_energy");
40 clock_cycle_ = ps.get<double>("clock_cycle");
41 charge_per_mip_ = ps.get<double>("charge_per_mip");
42
43 amplitude_method_ = ps.get<std::string>("amplitude_method", "soi");
44 fit_n_samples_ = ps.get<int>("fit_n_samples", 0);
45 fit_fixed_t0_ = ps.get<bool>("fit_fixed_t0", false);
46 fit_t0_offset_ = ps.get<double>("fit_t0_offset", 0.);
47 fit_zs_threshold_ = ps.get<double>("fit_zs_threshold", 0.);
48 phase_resolution_ = ps.get<double>("phase_resolution", 0.);
49 phase_name_ = ps.get<std::string>("phase_name", "ecal_digi_phase");
50 if (amplitude_method_ == "fit") {
51 auto shape = ps.get<std::vector<double>>("pulse_shape");
52 if (shape.size() != 5) {
53 EXCEPTION_RAISE("InvalidConfig", "pulse_shape needs 5 parameters.");
54 }
55 pulse_shape_ = std::make_unique<ldmx::HgcrocShape>(
56 shape[0], shape[1], shape[2], shape[3], shape[4]);
57 } else if (amplitude_method_ != "soi" and amplitude_method_ != "max") {
58 EXCEPTION_RAISE("InvalidConfig", "Unknown amplitude_method '" +
60 "', use soi, max or fit.");
61 }
62}
63
65 if (fit_fixed_t0_) {
68 rng_.seed(rseed.getSeed(getName() + "::phase"));
69 }
70}
71
73 // Get the Ecal Geometry
74 const auto& geometry = getCondition<ldmx::EcalGeometry>(
75 ldmx::EcalGeometry::CONDITIONS_OBJECT_NAME);
76
77 // Safety check: ensure layer_weights_ covers all geometry layers.
78 // A mismatch here typically means an outdated geometry configuration
79 // (e.g. v14) is being used with layer weights for a different geometry
80 // version (e.g. v15).
81 if (geometry.getNumLayers() > static_cast<int>(layer_weights_.size())) {
82 EXCEPTION_RAISE(
83 "InvalidConfig",
84 "The number of layers in the Ecal geometry (" +
85 std::to_string(geometry.getNumLayers()) +
86 ") exceeds the number of configured layer_weights (" +
87 std::to_string(layer_weights_.size()) +
88 "). This is likely caused by a mismatch between the geometry "
89 "version and the reconstruction configuration. Please ensure "
90 "that the correct geometry is being used.");
91 }
92
93 // Get the reconstruction parameters
94 EcalReconConditions the_conditions(
97
98 std::vector<ldmx::EcalHit> ecal_rec_hits;
99 auto ecal_digis = event.getObject<ldmx::HgcrocDigiCollection>(
101 const int i_soi = ecal_digis.getSampleOfInterestIndex();
102
103 // one measured phase per event, shared by all channels
104 double t0_fixed{0.};
105 if (fit_fixed_t0_) {
106 t0_fixed =
107 event.getEventHeader().getFloatParameter(phase_name_) + fit_t0_offset_;
108 if (phase_resolution_ > 0.) {
109 t0_fixed += std::normal_distribution<double>(0., phase_resolution_)(rng_);
110 }
111 }
112 // loop through digis
113 for (auto digi : ecal_digis) {
114 // ID from first digi sample
115 // assuming rest of samples have same ID
116 ldmx::EcalID id(digi.id());
117
118 // ID to real space position
119 auto [x_, y_, z_] = geometry.getPosition(id);
120
121 // the TOT measurement is in the sample where the pulse fell back below
122 // threshold, which is the SOI only for in-time hits (iss #1944)
123 int i_tot = digi.totSampleIndex();
124
125 // get the estimated charge deposited from digi samples
126 double charge(0.);
127 // TOA with respect to the 25ns clock window that measured it
128 double hit_time(0.);
129
130 if (i_tot >= 0) {
131 // TOT - number of clock ticks that pulse was over threshold
132 // this is related to the amplitude of the pulse approximately through a
133 // linear drain rate the amplitude of the pulse is related to the energy
134 // deposited
135 auto tot_sample = digi.at(i_tot);
136
137 // convert the time over threshold into a total energy deposited in the
138 // silicon
139 // (time over threshold [ns] - pedestal) * gain
140 charge = (tot_sample.tot() - the_conditions.totPedestal(id)) *
141 the_conditions.totGain(id);
142
143 // TOA is measured from the start of its own sample
144 hit_time = (i_tot - i_soi) * clock_cycle_ +
145 tot_sample.toa() * (clock_cycle_ / 1024);
146
147 ldmx_log(trace) << "Recon { TOA: " << hit_time << " ns } ";
148 ldmx_log(trace) << "TOT Mode (sample " << i_tot << ") -> "
149 << tot_sample.tot() << "TDC -> " << charge << " fC";
150 } else {
151 hit_time = digi.soi().toa() * (clock_cycle_ / 1024); // ns
152 ldmx_log(trace) << "Recon { TOA: " << hit_time << " ns } ";
153
154 // ADC mode of readout
155 // ADC - voltage measurement at a specific time of the pulse
156 // Pulse Shape:
157 // p[0]/(1.0+exp(p[1](t-p[2]+p[3]-p[4])))/(1.0+exp(p[5]*(t-p[6]+p[3]-p[4])))
158 // p[0] = amplitude to be fit (TBD)
159 // p[1] = -0.345 shape parameter - rate of up slope
160 // p[2] = 70.6547 shape parameter - time of up slope relative to shape
161 // fit p[3] = 77.732 shape parameter - time of peak relative to shape fit
162 // p[4] = peak time to be fit (TBD)
163 // p[5] = 0.140068 shape parameter - rate of down slope
164 // p[6] = 87.7649 shape paramter - time of down slope relative to shape
165 // fit
166 // These measurements can be used to fit the pulse shape if TOT is not
167 // available. For now, we simply take the measurement of the SOI as the
168 // peak amplitude.
169
170 const double pedestal = the_conditions.adcPedestal(id);
171 if (amplitude_method_ == "soi") {
172 charge = (digi.soi().adcT() - pedestal) * the_conditions.adcGain(id);
173 } else {
174 std::vector<short> adc;
175 // zero-suppressed samples come as raw 0, leave them out of the fit
176 std::vector<bool> kept;
177 for (unsigned int i{0}; i < digi.size(); i++) {
178 adc.push_back(static_cast<short>(digi.at(i).adcT()));
179 kept.push_back(digi.at(i).raw() != 0);
180 }
181 int i_max = std::max_element(adc.begin(), adc.end()) - adc.begin();
182 if (amplitude_method_ == "max") {
183 charge = (adc[i_max] - pedestal) * the_conditions.adcGain(id);
184 // TOA is measured from the start of its own sample
185 hit_time = (i_max - i_soi) * clock_cycle_ +
186 digi.at(i_max).toa() * (clock_cycle_ / 1024);
187 } else {
188 // window starts one sample before the peak to catch the rise
189 const int n_adc = static_cast<int>(adc.size());
190 const int n =
191 fit_n_samples_ > 0 ? std::min(fit_n_samples_, n_adc) : n_adc;
192 int i_peak = i_max;
193 if (fit_fixed_t0_) {
194 i_peak =
195 i_soi + static_cast<int>(std::lround(t0_fixed / clock_cycle_));
196 }
197 const int first = std::clamp(i_peak - 1, 0, n_adc - n);
198 std::vector<short> window(adc.begin() + first,
199 adc.begin() + first + n);
200 std::vector<bool> window_kept(kept.begin() + first,
201 kept.begin() + first + n);
202 // peak within half a clock of the window, else noise tails fake
203 // pulses
204 ldmx::PulseFitter fitter(
205 *pulse_shape_, (first - i_soi) * clock_cycle_, clock_cycle_,
206 pedestal, 1., (first - i_soi - 0.5) * clock_cycle_,
207 (first + n - 1 - i_soi + 0.5) * clock_cycle_, 0.25);
209 auto r = fit_fixed_t0_
210 ? fitter.fitFixed(window, t0_fixed, window_kept)
211 : fitter.fit(window, window_kept);
212 charge = r.converged ? r.amplitude * the_conditions.adcGain(id) : 0.;
213 hit_time = r.t0;
214 }
215 }
216
217 ldmx_log(trace) << "ADC Mode -> " << charge << " fC";
218 }
219
232 if (charge <= 0) continue;
233
234 double num_mips_equivalent = charge / charge_per_mip_;
235 double energy_deposited_in_si = num_mips_equivalent * mip_si_energy_;
236
237 ldmx_log(trace) << " -> " << num_mips_equivalent << " equiv MIPs -> "
238 << energy_deposited_in_si << " MeV";
239
240 // incorporate layer_ weights
241 double reconstructed_energy =
242 (num_mips_equivalent *
244 id.layer()) // energy lost in non-sensitive layers
245 + energy_deposited_in_si // energy deposited in Si itself
246 ) *
248
249 // copy over information to rec hit structure in new collection
250 ldmx::EcalHit rec_hit;
251 rec_hit.setID(id.raw());
252 rec_hit.setXPos(x_);
253 rec_hit.setYPos(y_);
254 rec_hit.setZPos(z_);
255 rec_hit.setAmplitude(energy_deposited_in_si);
256 rec_hit.setEnergy(reconstructed_energy);
257 rec_hit.setTime(hit_time);
258
259 ecal_rec_hits.push_back(rec_hit);
260 }
261
263 // ecal sim hits_ exist ==> label which hits_ are real and which are pure
264 // noise
265 auto ecal_sim_hits{event.getCollection<ldmx::SimCalorimeterHit>(
267 std::set<int> real_hits;
268 for (auto const& sim_hit : ecal_sim_hits) real_hits.insert(sim_hit.getID());
269 for (auto& hit : ecal_rec_hits)
270 hit.setNoise(real_hits.find(hit.getID()) == real_hits.end());
271 }
272
273 // add collection to event bus
274 event.add(rec_hit_coll_name_, ecal_rec_hits);
275}
276
277} // namespace ecal
278
Class that translates raw positions of ECal module hits into cells in a hexagonal readout.
Class that defines an ECal detector ID with a cell number.
Class that performs basic ECal digitization.
#define DECLARE_PRODUCER(CLASS)
Macro which allows the framework to construct a producer given its name during configuration.
Class that represents a digitized hit in a calorimeter cell readout by an HGCROC.
Conditions object for random number seeds.
Class which stores simulated calorimeter hit information.
Performs basic ECal reconstruction.
double fit_zs_threshold_
ZS threshold [ADC over pedestal] for suppressed samples in the fit.
std::string phase_name_
event header float parameter holding the event phase [ns]
double mip_si_energy_
Energy [MeV] deposited by a MIP in Si 0.5mm thick.
std::string sim_hit_coll_name_
simhit collection name
void onNewRun(const ldmx::RunHeader &) override
Seed the phase smearing.
std::string amplitude_method_
ADC mode amplitude: soi, max or fit.
bool fit_fixed_t0_
fit with t0 = event phase + offset, e.g. phase from HCal TOA
double fit_t0_offset_
offset [ns] between the event phase and the pulse t0
double phase_resolution_
Gaussian smearing [ns] of the event phase.
std::vector< double > layer_weights_
Layer Weights to use for this reconstruction.
double second_order_energy_correction_
Second Order Energy Correction to use for this reconstruction.
double charge_per_mip_
Number of electrons generated by average MIP in Si 0.5mm thick.
std::string rec_hit_coll_name_
output hit collection name
EcalRecProducer(const std::string &name, framework::Process &process)
Constructor.
int fit_n_samples_
number of samples in the fit, starting one before the max, <= 0 for all
std::string digi_pass_name_
Digi Pass Name to use as input.
void produce(framework::Event &event) override
Produce EcalHits and put them into the event bus using the EcalDigis as input.
double clock_cycle_
Length of clock cycle [ns].
std::string sim_hit_pass_name_
simhit pass name
void configure(framework::config::Parameters &) override
Grabs configure parameters from the python config file.
std::unique_ptr< ldmx::HgcrocShape > pulse_shape_
HGCROC pulse template, only for amplitude_method fit.
std::string digi_coll_name_
Digi Collection Name to use as input.
Class to wrap around an double table of conditions.
double adcPedestal(const ldmx::EcalID &id) const
get the ADC pedestal
double adcGain(const ldmx::EcalID &id) const
get the ADC gain
double totPedestal(const ldmx::EcalID &id) const
get the TOT pedestal
static const std::string CONDITIONS_NAME
the name of the EcalReconConditions table (must match python registration name)
double totGain(const ldmx::EcalID &id) const
get the TOT gain
const T & getCondition(const std::string &condition_name)
Access a conditions object for the current event.
std::string getName() const
Get the processor name.
Implements an event buffer system for storing event data.
Definition Event.h:40
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
Class which represents the process under execution.
Definition Process.h:34
static const std::string CONDITIONS_OBJECT_NAME
Conditions object name.
Class encapsulating parameters for configuring a processor.
Definition Parameters.h:26
const T & get(const std::string &name) const
Retrieve the parameter of the given name.
Definition Parameters.h:75
void setYPos(float ypos)
Set the Y position of the hit [mm].
void setID(int id)
Set the detector ID.
void setZPos(float zpos)
Set the Z position of the hit [mm].
void setXPos(float xpos)
Set the X position of the hit [mm].
void setTime(float time)
Set the time of the hit [ns].
void setAmplitude(float amplitude)
Set the amplitude of the hit, which is proportional to the signal in the calorimeter cell without sam...
void setEnergy(float energy)
Set the calorimetric energy of the hit, corrected for sampling factors [MeV].
Stores reconstructed hit information from the ECAL.
Definition EcalHit.h:19
Extension of DetectorID providing access to ECal layers and cell numbers in a hex grid.
Definition EcalID.h:20
Represents a collection of the digi hits readout by an HGCROC.
unsigned int getSampleOfInterestIndex() const
Get index of sample of interest.
Fits a pulse shape to the ADC samples of a single readout channel to extract hit amplitude and time.
Definition PulseFitter.h:41
FitResult fitFixed(const std::vector< short > &samples, double T, const std::vector< bool > &used={}) const
Amplitude only fit with the hit time T known, e.g.
FitResult fit(const std::vector< short > &samples, const std::vector< bool > &used={}) const
Fit the pulse to the given ADC sample vector.
void setZSThreshold(double thr_adc)
Zero-suppression threshold [ADC over pedestal] for the samples left out by the mask: a model above it...
Definition PulseFitter.h:96
Run-specific configuration and data stored in its own output TTree alongside the event TTree in the o...
Definition RunHeader.h:68
Stores simulated calorimeter hit information.
bool converged
False if no above-pedestal samples found.
Definition PulseFitter.h:52