1#include "Recon/OverlayProducer.h"
12 parameters.
get<std::vector<std::string>>(
"calo_collections");
14 parameters.
get<std::vector<std::string>>(
"tracker_collections");
31 ldmx_log(debug) <<
"Got parameters \n \t overlayFileName = "
35 ldmx_log(debug) <<
"\n\t overlayCaloHitCollections = ";
37 ldmx_log(debug) << coll <<
"; ";
40 ldmx_log(debug) <<
"\n\t overlayTrackerHitCollections = ";
42 ldmx_log(debug) << coll <<
"; ";
45 ldmx_log(trace) <<
"\n\t numberOverlaidInteractions = " <<
poisson_mu_
46 <<
"\n\t nEarlierBunchesToSample = " <<
n_earlier_
47 <<
"\n\t nLaterBunchesToSample = " <<
n_later_
63 rndm_ = std::make_unique<TRandom2>(rnss.getSeed(
"OverlayProducer::rndm"));
65 std::make_unique<TRandom2>(rnss.getSeed(
"OverlayProducer::rndmTime"));
72 EXCEPTION_RAISE(
"BadRead",
"Couldn't read to starting offset.");
75 ldmx_log(info) <<
"Starting overlay process with pileup event number "
76 << ev_number <<
" (random event number picked was "
77 << start_event <<
").";
85 std::map<std::string, std::vector<ldmx::SimCalorimeterHit>>
87 std::map<std::string, std::vector<ldmx::SimTrackerHit>>
88 tracker_collection_map;
89 std::map<int, ldmx::SimCalorimeterHit> hit_map;
100 auto needs_contribs_added{
101 coll_name.find(
"Ecal") != std::string::npos ? true :
false};
108 if (!needs_contribs_added) {
112 ldmx_log(debug) <<
"in loop: start of collection " << coll_name
113 <<
"in loop: printing current sim event: ";
114 ldmx_log(debug) <<
"in loop: size of sim hits_ vector " << coll_name
115 <<
" is " << simhits_calo.size();
119 if (needs_contribs_added) {
121 ldmx_log(trace) << simhit;
123 hit_map[simhit.getID()] = simhit;
135 auto simhits_tracker =
140 ldmx_log(debug) <<
"in loop: size of sim hits_ vector " << coll_name
141 <<
" is " << simhits_tracker.size();
143 ldmx_log(debug) <<
"in loop: start of collection " << coll_name
144 <<
"in loop: printing current sim event: ";
147 ldmx_log(trace) << simhit;
165 for (
int bunch_offset{start_bunch}; bunch_offset <= end_bunch;
174 if (bunch_offset == 0) {
184 event.getEventHeader().setIntParameter(
"in_time_pu", n_events_overlay);
188 n_events_overlay -= 1;
193 ldmx_log(debug) <<
"Will overlay " << n_events_overlay
194 <<
" events on the simulated one";
196 for (
int i_ev = 0; i_ev < n_events_overlay; i_ev++) {
204 ldmx_log(error) <<
"Couldn't read next overlay event!";
210 time_offset += bunchtime_offset;
212 ldmx_log(trace) <<
"in overlay loop: overlaying event " <<
"which is "
213 << i_ev + 1 <<
" out of " << n_events_overlay
214 <<
"\n\thit time offset is " << time_offset <<
" ns"
215 <<
"\n\tbunch position offset is " << bunch_offset
216 <<
", leading to a total time offset of "
217 << bunchtime_offset <<
" ns";
224 bool needs_contribs_added =
false;
226 needs_contribs_added =
true;
229 std::vector<ldmx::SimCalorimeterHit> overlay_hits =
233 ldmx_log(debug) <<
"in loop: size of overlay hits_ vector is "
234 << overlay_hits.size();
236 std::string out_coll_name =
239 ldmx_log(trace) <<
"in loop: printing overlay event: ";
242 ldmx_log(trace) << overlay_hit;
244 const float overlay_time = overlay_hit.getTime() + time_offset;
245 overlay_hit.setTime(overlay_time);
247 if (needs_contribs_added) {
249 int overlay_hit_id = overlay_hit.getID();
250 if (hit_map.find(overlay_hit_id) ==
253 hit_map[overlay_hit_id].setID(overlay_hit_id);
254 std::vector<float> hit_pos = overlay_hit.getPosition();
255 hit_map[overlay_hit_id].setPosition(hit_pos[0], hit_pos[1],
261 hit_map[overlay_hit_id].addContrib(
263 overlay_hit.getEdep(), overlay_time);
266 calo_collection_map[out_coll_name].push_back(overlay_hit);
268 ldmx_log(trace) <<
"Adding non-Ecal overlay hit to outhit vector "
273 if (!needs_contribs_added)
274 ldmx_log(debug) <<
"Nhits in overlay collection " << out_coll_name
275 <<
": " << calo_collection_map[out_coll_name].size();
283 auto overlay_tracker_hits{
287 ldmx_log(debug) <<
"in loop: size of overlay hits_ vector is "
288 << overlay_tracker_hits.size();
292 ldmx_log(trace) <<
"in loop: printing overlay event: ";
294 for (
auto &overlay_hit : overlay_tracker_hits) {
295 auto overlay_time{overlay_hit.getTime() + time_offset};
296 overlay_hit.setTime(overlay_time);
297 tracker_collection_map[out_coll_name_tracker].push_back(overlay_hit);
299 ldmx_log(trace) << overlay_hit;
300 ldmx_log(trace) <<
"Adding tracker overlay hit to outhit vector "
301 << out_coll_name_tracker;
304 ldmx_log(debug) <<
"Nhits in overlay collection "
305 << out_coll_name_tracker <<
": "
306 << tracker_collection_map[out_coll_name_tracker].size();
319 ldmx_log(trace) <<
"Hits in hit_map after overlay of "
322 for (
auto &map_hit : hit_map) {
323 ldmx_log(trace) << map_hit.second;
327 calo_collection_map.end()) {
328 ldmx_log(debug) <<
"Adding first hit from hit map as first outhit "
329 "vector to calo_collection_map";
334 .push_back(map_hit.second);
345 for (
auto &[name, coll] : calo_collection_map) {
346 ldmx_log(debug) <<
"Writing " << name <<
" to event bus.";
348 ldmx_log(trace) <<
"List of hits_ added: ";
349 for (
auto &hit : coll) {
350 ldmx_log(trace) << hit;
352 event.add(name, coll);
356 for (
auto &[name, coll] : tracker_collection_map) {
357 ldmx_log(debug) <<
"Writing " << name <<
" to event bus.";
358 ldmx_log(trace) <<
"List of hits_ added: ";
359 for (
auto &hit : coll) {
360 ldmx_log(trace) << hit;
362 event.add(name, coll);
#define DECLARE_PRODUCER(CLASS)
Macro which allows the framework to construct a producer given its name during configuration.
const T & getCondition(const std::string &condition_name)
Access a conditions object for the current event.
Implements an event buffer system for storing event data.
ldmx::EventHeader & getEventHeader()
Get the event header.
const std::vector< ContentType > & getCollection(const std::string &collectionName, const std::string &passName) const
Get a collection (std::vector) of objects from the event bus.
static const std::string CONDITIONS_OBJECT_NAME
Conditions object name.
Class encapsulating parameters for configuring a processor.
const T & get(const std::string &name) const
Retrieve the parameter of the given name.
Stores simulated calorimeter hit information.
Represents a simulated tracker hit in the simulation.
Class to overlay in-time pile-up events from an overlay file.
int start_event_max_
Maximum event number to start overlaying from.
std::string overlay_filename_
Pileup overlay events input file name.
std::unique_ptr< framework::EventFile > overlay_file_
Pileup overlay events input file.
bool do_poisson_in_time_
Let the total number of in-time events be poisson distributed, or fix at the chosen value,...
int n_later_
Number of bunches after the sim event to pull pileup events from.
std::string overlay_passname_
Pileup overlay events input pass name.
bool do_poisson_out_of_time_
Let the total number of out-of-time events be poisson distributed, or fix at the chosen value,...
void onNewRun(const ldmx::RunHeader &) override
At the start of the run, the pileup overlay file is set up, and the starting event number is chosen,...
std::vector< std::string > tracker_collections_
List of SimTrackerHit collection(s) to loop over and add hits from, combining sim and pileup.
framework::config::Parameters params_
The parameters used to configure this producer.
double bunch_spacing_
Spacing in time (in [ns]) between electron bunches.
std::string sim_passname_
To use for finding the sim event bus passengers, mostly a disambiguation.
std::vector< std::string > calo_collections_
List of SimCalorimeterHit collection(s) to loop over and add hits from, combining sim and pileup.
std::unique_ptr< TRandom2 > rndm_time_
Random number generator for pileup event time offset.
std::unique_ptr< TRandom2 > rndm_
Random number generator for number of overlaid events.
void produce(framework::Event &event) override
Based on the list of collections to overlay, and the desired number of events, loop through all relev...
double poisson_mu_
(average) total number of events
void configure(framework::config::Parameters ¶meters) override
Configure the processor with input parameters from the python cofig.
std::string out_coll_postfix_
Postfix to add to the collection name of the overlayed collections.
void onProcessStart() override
At the start of processing, the pileup overlay file is set up.
int overlay_incident_id_
For Ecal, overlay hits should be added as contribs.
double time_sigma_
Width of pileup bunch spread in time (in [ns]), specified as a sigma of a Gaussian distribution.
int start_event_min_
Minimum event number to start overlaying from.
framework::Event overlay_event_
The overlay ldmx event bus.
double time_mean_
Average position in time (in [ns]) of pileup bunches, relative to the sim event.
int n_earlier_
Number of bunches before the sim event to pull pileup events from.