1#include "Recon/OverlayProducer.h"
12 parameters.
get<std::vector<std::string>>(
"calo_collections");
14 parameters.
get<std::vector<std::string>>(
"tracker_collections");
16 parameters.
get<std::vector<std::string>>(
"contrib_collections");
33 ldmx_log(debug) <<
"Got parameters \n \t overlayFileName = "
37 ldmx_log(debug) <<
"\n\t overlayCaloHitCollections = ";
39 ldmx_log(debug) << coll <<
"; ";
42 ldmx_log(debug) <<
"\n\t overlayTrackerHitCollections = ";
44 ldmx_log(debug) << coll <<
"; ";
47 ldmx_log(trace) <<
"\n\t numberOverlaidInteractions = " <<
poisson_mu_
48 <<
"\n\t nEarlierBunchesToSample = " <<
n_earlier_
49 <<
"\n\t nLaterBunchesToSample = " <<
n_later_
63 EXCEPTION_RAISE(
"CollNameMismatch",
64 "The contrib-using collection " + coll_name +
65 " does not match any SimCalorimeterHit collection in "
66 "'calo_collections_'! Please check your spelling");
77 rndm_ = std::make_unique<TRandom2>(rnss.getSeed(
"OverlayProducer::rndm"));
79 std::make_unique<TRandom2>(rnss.getSeed(
"OverlayProducer::rndmTime"));
86 EXCEPTION_RAISE(
"BadRead",
"Couldn't read to starting offset.");
89 ldmx_log(info) <<
"Starting overlay process with pileup event number "
90 << ev_number <<
" (random event number picked was "
91 << start_event <<
").";
99 std::map<std::string, std::vector<ldmx::SimCalorimeterHit>>
101 std::map<std::string, std::vector<ldmx::SimTrackerHit>>
102 tracker_collection_map;
103 std::map<std::string, std::map<int, ldmx::SimCalorimeterHit>> hit_map;
125 if (!needs_contribs_added) {
129 ldmx_log(debug) <<
"in loop: start of collection " << coll_name
130 <<
"in loop: printing current sim event: ";
131 ldmx_log(debug) <<
"in loop: size of sim hits_ vector " << coll_name
132 <<
" is " << simhits_calo.size();
136 if (needs_contribs_added) {
137 ldmx_log(trace) <<
"Collection " << coll_name <<
" needs contribs added";
139 ldmx_log(trace) << simhit;
153 auto simhits_tracker =
158 ldmx_log(debug) <<
"in loop: size of sim hits_ vector " << coll_name
159 <<
" is " << simhits_tracker.size();
161 ldmx_log(debug) <<
"in loop: start of collection " << coll_name
162 <<
"in loop: printing current sim event: ";
165 ldmx_log(trace) << simhit;
183 for (
int bunch_offset{start_bunch}; bunch_offset <= end_bunch;
192 if (bunch_offset == 0) {
202 event.getEventHeader().setIntParameter(
"in_time_pu", n_events_overlay);
206 n_events_overlay -= 1;
211 ldmx_log(debug) <<
"Will overlay " << n_events_overlay
212 <<
" events on the simulated one";
214 for (
int i_ev = 0; i_ev < n_events_overlay; i_ev++) {
222 ldmx_log(error) <<
"Couldn't read next overlay event!";
228 time_offset += bunchtime_offset;
230 ldmx_log(trace) <<
"in overlay loop: overlaying event " <<
"which is "
231 << i_ev + 1 <<
" out of " << n_events_overlay
232 <<
"\n\thit time offset is " << time_offset <<
" ns"
233 <<
"\n\tbunch position offset is " << bunch_offset
234 <<
", leading to a total time offset of "
235 << bunchtime_offset <<
" ns";
243 bool needs_contribs_added{
249 std::vector<ldmx::SimCalorimeterHit> overlay_hits =
253 ldmx_log(debug) <<
"in loop: size of overlay hits_ vector is "
254 << overlay_hits.size();
256 std::string out_coll_name =
259 ldmx_log(trace) <<
"in loop: printing overlay event: ";
262 ldmx_log(trace) << overlay_hit;
264 const float overlay_time = overlay_hit.getTime() + time_offset;
265 overlay_hit.setTime(overlay_time);
267 if (needs_contribs_added) {
269 auto &this_coll_hit_map{
271 int overlay_hit_id = overlay_hit.getID();
272 if (this_coll_hit_map.find(overlay_hit_id) ==
276 this_coll_hit_map[overlay_hit_id].setID(overlay_hit_id);
277 std::vector<float> hit_pos = overlay_hit.getPosition();
278 this_coll_hit_map[overlay_hit_id].setPosition(
279 hit_pos[0], hit_pos[1], hit_pos[2]);
284 this_coll_hit_map[overlay_hit_id].addContrib(
286 overlay_hit.getEdep(), overlay_time);
289 calo_collection_map[out_coll_name].push_back(overlay_hit);
291 ldmx_log(trace) <<
"Adding non-Ecal overlay hit to outhit vector "
296 if (!needs_contribs_added)
297 ldmx_log(debug) <<
"Nhits in overlay collection " << out_coll_name
298 <<
": " << calo_collection_map[out_coll_name].size();
306 auto overlay_tracker_hits{
310 ldmx_log(debug) <<
"in loop: size of overlay hits_ vector is "
311 << overlay_tracker_hits.size();
315 ldmx_log(trace) <<
"in loop: printing overlay event: ";
317 for (
auto &overlay_hit : overlay_tracker_hits) {
318 auto overlay_time{overlay_hit.getTime() + time_offset};
319 overlay_hit.setTime(overlay_time);
320 auto overlay_track_id{overlay_hit.getTrackID()};
321 overlay_hit.setTrackID(overlay_track_id);
322 tracker_collection_map[out_coll_name_tracker].push_back(overlay_hit);
324 ldmx_log(trace) << overlay_hit;
325 ldmx_log(trace) <<
"Adding tracker overlay hit to outhit vector "
326 << out_coll_name_tracker;
329 ldmx_log(debug) <<
"Nhits in overlay collection "
330 << out_coll_name_tracker <<
": "
331 << tracker_collection_map[out_coll_name_tracker].size();
343 ldmx_log(trace) <<
"Hits in hit_map after overlay of "
348 ldmx_log(trace) << map_hit.second;
352 calo_collection_map.end()) {
353 ldmx_log(debug) <<
"Adding first hit from hit map as first outhit "
354 "vector to calo_collection_map";
359 .push_back(map_hit.second);
368 for (
auto &[name, coll] : calo_collection_map) {
369 ldmx_log(debug) <<
"Writing " << name <<
" to event bus.";
371 ldmx_log(trace) <<
"List of hits_ added: ";
372 for (
auto &hit : coll) {
373 ldmx_log(trace) << hit;
375 event.add(name, coll);
379 for (
auto &[name, coll] : tracker_collection_map) {
380 ldmx_log(debug) <<
"Writing " << name <<
" to event bus.";
381 ldmx_log(trace) <<
"List of hits_ added: ";
382 for (
auto &hit : coll) {
383 ldmx_log(trace) << hit;
385 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.
std::vector< std::string > contrib_collections_
List of SimCalorimeterHit collections which keep track of hit contribs.
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.