LDMX Software
OverlayProducer.cxx
1#include "Recon/OverlayProducer.h"
2
3namespace recon {
4
6 params_ = parameters;
7
8 // name of file containing events to be overlaid, and a list of collections to
9 // overlay
10 overlay_filename_ = parameters.get<std::string>("overlay_filename");
12 parameters.get<std::vector<std::string>>("calo_collections");
14 parameters.get<std::vector<std::string>>("tracker_collections");
15 sim_passname_ = parameters.get<std::string>("sim_passname");
16 overlay_passname_ = parameters.get<std::string>("overlay_passname");
17 out_coll_postfix_ = parameters.get<std::string>("out_coll_postfix");
18 // overlay specifics:
19 poisson_mu_ = parameters.get<double>("poisson_mu");
20 do_poisson_in_time_ = parameters.get<bool>("do_poisson_in_time");
21 do_poisson_out_of_time_ = parameters.get<bool>("do_poisson_out_of_time");
22 time_sigma_ = parameters.get<double>("time_sigma");
23 time_mean_ = parameters.get<double>("time_mean");
24 n_earlier_ = parameters.get<int>("n_earlier");
25 n_later_ = parameters.get<int>("n_later");
26 bunch_spacing_ = parameters.get<double>("bunch_spacing");
27 start_event_min_ = parameters.get<int>("start_event_min");
28 start_event_max_ = parameters.get<int>("start_event_max");
29
31 ldmx_log(debug) << "Got parameters \n \t overlayFileName = "
33 << "\n\t sim pass name = " << sim_passname_
34 << "\n\t overlay pass name = " << overlay_passname_;
35 ldmx_log(debug) << "\n\t overlayCaloHitCollections = ";
36 for (const auto &coll : calo_collections_) {
37 ldmx_log(debug) << coll << "; ";
38 }
39
40 ldmx_log(debug) << "\n\t overlayTrackerHitCollections = ";
41 for (const std::string &coll : tracker_collections_) {
42 ldmx_log(debug) << coll << "; ";
43 }
44
45 ldmx_log(trace) << "\n\t numberOverlaidInteractions = " << poisson_mu_
46 << "\n\t nEarlierBunchesToSample = " << n_earlier_
47 << "\n\t nLaterBunchesToSample = " << n_later_
48 << "\n\t bunchSpacing = " << bunch_spacing_
49 << "\n\t doPoissonIntime = " << do_poisson_in_time_
50 << "\n\t doPoissonOutoftime = " << do_poisson_out_of_time_
51 << "\n\t timeSpread = " << time_sigma_
52 << "\n\t timeMean = " << time_mean_
53 << "\n\t startEventMin = " << start_event_min_
54 << "\n\t startEventMax = " << start_event_max_;
55
56 return;
57} // end configure()
58
63 rndm_ = std::make_unique<TRandom2>(rnss.getSeed("OverlayProducer::rndm"));
65 std::make_unique<TRandom2>(rnss.getSeed("OverlayProducer::rndmTime"));
66
67 // Pick a random event from the Pileup file
68 int start_event = rndm_->Uniform(start_event_min_, start_event_max_);
69 // EventFile::skipToEvent handles actual number of events in file
70 int ev_number = overlay_file_->skipToEvent(start_event);
71 if (ev_number < 0) {
72 EXCEPTION_RAISE("BadRead", "Couldn't read to starting offset.");
73 }
75 ldmx_log(info) << "Starting overlay process with pileup event number "
76 << ev_number << " (random event number picked was "
77 << start_event << ").";
78} // end onNewRun()
79
81 // using nextEvent to loop, we need to loop over overlay events and in an
82 // inner loop, loop over collections, and store them. after all pileup events
83 // have been added, the vector of collections is iterated over and added to
84 // the event bus.
85 std::map<std::string, std::vector<ldmx::SimCalorimeterHit>>
86 calo_collection_map;
87 std::map<std::string, std::vector<ldmx::SimTrackerHit>>
88 tracker_collection_map;
89 std::map<int, ldmx::SimCalorimeterHit> hit_map;
90
91 // start by copying over all the collections from the sim event
92
93 /* ----------- first do the SimCalorimeterHits ----------- */
94
95 // get the calo hits_ collections that we want to overlay, by looping over
96 // the list of collections passed to the producer : calo_collections_
97 for (const auto &coll_name : calo_collections_) {
98 // for now, Ecal and only Ecal uses contribs instead of multiple
99 // simhits_calo per channel, meaning, it requires special treatment
100 auto needs_contribs_added{
101 coll_name.find("Ecal") != std::string::npos ? true : false};
102
103 // start out by just copying the sim hits_, unaltered.
104 auto simhits_calo =
105 event.getCollection<ldmx::SimCalorimeterHit>(coll_name, sim_passname_);
106 // but don't copy ecal hits_ immediately: for them, wait until overlay
107 // contribs have been added. then add everything through the hit_map
108 if (!needs_contribs_added) {
109 calo_collection_map[coll_name + out_coll_postfix_] = simhits_calo;
110 }
111
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();
116
117 // we don't need to touch the hard process sim hits_, really... but we
118 // might need the simhits in the hit map.
119 if (needs_contribs_added) {
120 for (const ldmx::SimCalorimeterHit &simhit : simhits_calo) {
121 ldmx_log(trace) << simhit;
122 // this copies the hit, its ID and its coordinates directly
123 hit_map[simhit.getID()] = simhit;
124
125 } // over calo simhit collection
126 } // if needContribs
127
128 } // over calo collections for sim event
129
130 /* ----------- now do the same with SimTrackerHits! ----------- */
131
132 // get the SimTrackerHit collections that we want to overlay, by looping
133 // over the list of collections passed to the producer : tracker_collections_
134 for (const auto &coll_name : tracker_collections_) {
135 auto simhits_tracker =
136 event.getCollection<ldmx::SimTrackerHit>(coll_name, sim_passname_);
137 tracker_collection_map[coll_name + out_coll_postfix_] = simhits_tracker;
138
139 // the rest is printouts for debugging
140 ldmx_log(debug) << "in loop: size of sim hits_ vector " << coll_name
141 << " is " << simhits_tracker.size();
142
143 ldmx_log(debug) << "in loop: start of collection " << coll_name
144 << "in loop: printing current sim event: ";
145
146 for (const ldmx::SimTrackerHit &simhit : simhits_tracker) {
147 ldmx_log(trace) << simhit;
148 }
149 } // over tracker collections for sim event
150
151 /* ----------- now do the pileup overlay ----------- */
152
153 // we could shift these by a random number, effectively placing the
154 // sim event at random positions in the interval, preserving the
155 // overall interval length
156 // int simBunch= static_cast<int>(rndm_time_->Uniform(
157 // -(n_earlier_+1) , n_later_+1)); // +1 to get
158 // inclusive interval
159 int start_bunch = -n_earlier_;
160 int end_bunch = n_later_;
161
162 // TODO -- figure out if we should also randomly shift the time of the sim
163 // event (likely only needed if time bias gets picked up by BDT or ML by way
164 // of pulse behaviour)
165 for (int bunch_offset{start_bunch}; bunch_offset <= end_bunch;
166 bunch_offset++) {
167 // sample a poisson distribution, or use mu as fixed number of overlay
168 // events
169 int n_events_overlay = do_poisson_out_of_time_
170 ? rndm_->Poisson(poisson_mu_)
171 : static_cast<int>(poisson_mu_);
172
173 // special case: in-time pileup at bunch 0
174 if (bunch_offset == 0) {
175 if (!do_poisson_in_time_) {
176 // fix it to the average
177 n_events_overlay = static_cast<int>(poisson_mu_);
179 // then we haven't set this yet
180 n_events_overlay = rndm_->Poisson(poisson_mu_);
181 }
182
183 // paticularly useful in the poisson fluctuated case
184 event.getEventHeader().setIntParameter("in_time_pu", n_events_overlay);
185
186 // the total number of events is nPU + 1 (it includes the sim event)
187 // in any case, subtract the sim event from nOverlay
188 n_events_overlay -= 1;
189
190 } // end if bunch_offset == 0
191
192 float bunchtime_offset = bunch_spacing_ * bunch_offset;
193 ldmx_log(debug) << "Will overlay " << n_events_overlay
194 << " events on the simulated one";
195
196 for (int i_ev = 0; i_ev < n_events_overlay; i_ev++) {
203 if (!overlay_file_->nextEvent()) {
204 ldmx_log(error) << "Couldn't read next overlay event!";
205 return;
206 }
207
208 // a pileup event wide time offset to be applied to all its hits_.
209 float time_offset = rndm_time_->Gaus(time_mean_, time_sigma_);
210 time_offset += bunchtime_offset;
211
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";
218
219 /* ----------- first do the SimCalorimeterHits overlay ----------- */
220
221 // again get the calo hits_ collections that we want to overlay
222 for (uint i_coll = 0; i_coll < calo_collections_.size(); i_coll++) {
223 // for now, Ecal and only Ecal uses contribs
224 bool needs_contribs_added = false;
225 if (strstr(calo_collections_[i_coll].c_str(), "Ecal")) {
226 needs_contribs_added = true;
227 }
228
229 std::vector<ldmx::SimCalorimeterHit> overlay_hits =
232
233 ldmx_log(debug) << "in loop: size of overlay hits_ vector is "
234 << overlay_hits.size();
235
236 std::string out_coll_name =
238
239 ldmx_log(trace) << "in loop: printing overlay event: ";
240
241 for (ldmx::SimCalorimeterHit &overlay_hit : overlay_hits) {
242 ldmx_log(trace) << overlay_hit;
243
244 const float overlay_time = overlay_hit.getTime() + time_offset;
245 overlay_hit.setTime(overlay_time);
246
247 if (needs_contribs_added) { // special treatment for (for now only)
248 // ecal
249 int overlay_hit_id = overlay_hit.getID();
250 if (hit_map.find(overlay_hit_id) ==
251 hit_map.end()) { // there wasn't already a simhit in this id
252 hit_map[overlay_hit_id] = ldmx::SimCalorimeterHit();
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],
256 hit_pos[2]);
257 }
258 // add the overlay hit (as a) contrib
259 // incidentID = -1000, trackID = -1000, pdgCode = 0 <-- these are
260 // set in the header for now but could be parameters
261 hit_map[overlay_hit_id].addContrib(
262 overlay_incident_id_, overlay_track_id_, overlay_pdg_code_,
263 overlay_hit.getEdep(), overlay_time);
264 } // if add overlay as contribs
265 else {
266 calo_collection_map[out_coll_name].push_back(overlay_hit);
267
268 ldmx_log(trace) << "Adding non-Ecal overlay hit to outhit vector "
269 << out_coll_name;
270 } // end else !needs_contribs_added
271 } // over overlay calo simhit collection
272
273 if (!needs_contribs_added)
274 ldmx_log(debug) << "Nhits in overlay collection " << out_coll_name
275 << ": " << calo_collection_map[out_coll_name].size();
276
277 } // over caloCollections
278
279 /* ----------- now do simtracker hits_ overlay ----------- */
280
281 // get the SimTrackerHit collections that we want to overlay
282 for (const auto &coll : tracker_collections_) {
283 auto overlay_tracker_hits{
285 coll, overlay_passname_)};
286
287 ldmx_log(debug) << "in loop: size of overlay hits_ vector is "
288 << overlay_tracker_hits.size();
289
290 std::string out_coll_name_tracker{coll + out_coll_postfix_};
291
292 ldmx_log(trace) << "in loop: printing overlay event: ";
293
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);
298
299 ldmx_log(trace) << overlay_hit;
300 ldmx_log(trace) << "Adding tracker overlay hit to outhit vector "
301 << out_coll_name_tracker;
302 } // over overlay tracker simhit collection
303
304 ldmx_log(debug) << "Nhits in overlay collection "
305 << out_coll_name_tracker << ": "
306 << tracker_collection_map[out_coll_name_tracker].size();
307
308 } // over trackerCollections
309 } // over overlay events
310 } // over bunches
311
312 // after all events are done, the ecal hit_map is final and can be written
313 // to the event output
314 for (uint i_coll = 0; i_coll < calo_collections_.size(); i_coll++) {
315 // loop through collection names to find the right collection name
316 // add overlaid ecal hits_ as contribs/from hit_map rather than as copied
317 // simhits
318 if (strstr(calo_collections_[i_coll].c_str(), "Ecal")) {
319 ldmx_log(trace) << "Hits in hit_map after overlay of "
320 << calo_collections_[i_coll] << "Overlay :";
321
322 for (auto &map_hit : hit_map) {
323 ldmx_log(trace) << map_hit.second;
324
325 if (calo_collection_map.find(calo_collections_[i_coll] +
327 calo_collection_map.end()) {
328 ldmx_log(debug) << "Adding first hit from hit map as first outhit "
329 "vector to calo_collection_map";
330 calo_collection_map[calo_collections_[i_coll] + out_coll_postfix_] = {
331 map_hit.second};
332 } else {
333 calo_collection_map[calo_collections_[i_coll] + out_coll_postfix_]
334 .push_back(map_hit.second);
335 }
336 } // over hit_map
337 break; // for now we only have one hit_map: for Ecal. so no need
338 // looking further after we got a match
339 } // isEcal
340 } // second loop over collections, to collect hits_ from hit_map
341
342 // done collecting hits_.
343
344 // now we can write the calo collections to the event bus
345 for (auto &[name, coll] : calo_collection_map) {
346 ldmx_log(debug) << "Writing " << name << " to event bus.";
347
348 ldmx_log(trace) << "List of hits_ added: ";
349 for (auto &hit : coll) {
350 ldmx_log(trace) << hit;
351 }
352 event.add(name, coll);
353 }
354
355 // and now for the tracker hits_
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;
361 }
362 event.add(name, coll);
363 }
364 return;
365} // end produce()
366
368 // replace by this line once the corresponding tweak to EventFile is ready:
370 std::make_unique<framework::EventFile>(params_, overlay_filename_, true);
371 overlay_file_->setupEvent(&overlay_event_);
372 // we update the iterator at the end of each event. so do this once here to
373 // grab the first event in the processor
374 // ldmx_log(trace) << "Used input file: "
375 // << overlay_file_->getFileName() << " Got event info: " <<
376 // overlay_file_->getEvent()->Print();
377
378 return;
379} // end onProcessStart
380
381} // namespace recon
#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.
Definition Event.h:42
ldmx::EventHeader & getEventHeader()
Get the event header.
Definition Event.h:59
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.
Definition Event.h:400
static const std::string CONDITIONS_OBJECT_NAME
Conditions object name.
Class encapsulating parameters for configuring a processor.
Definition Parameters.h:29
const T & get(const std::string &name) const
Retrieve the parameter of the given name.
Definition Parameters.h:78
void setEventNumber(int eventNumber)
Set the event number.
Run-specific configuration and data stored in its own output TTree alongside the event TTree in the o...
Definition RunHeader.h:57
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 &parameters) 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.