LDMX Software
OverlayProducer.cxx
1#include "Recon/OverlayProducer.h"
2
6
7namespace recon {
8
10 params_ = parameters;
11
12 ldmx_log(debug) << "Running configure() ";
13
14 // name of file containing events to be overlaid, and a list of collections to
15 // overlay
16 overlayFileName_ = parameters.getParameter<std::string>("overlayFileName");
17 caloCollections_ = parameters.getParameter<std::vector<std::string>>(
18 "overlayCaloHitCollections");
19 trackerCollections_ = parameters.getParameter<std::vector<std::string>>(
20 "overlayTrackerHitCollections");
21 simPassName_ = parameters.getParameter<std::string>("passName");
22 overlayPassName_ = parameters.getParameter<std::string>("overlayPassName");
23 // overlay specifics:
24 poissonMu_ = parameters.getParameter<double>("totalNumberOfInteractions");
25 doPoissonIT_ = parameters.getParameter<bool>("doPoissonIntime");
26 doPoissonOOT_ = parameters.getParameter<bool>("doPoissonOutoftime");
27 timeSigma_ = parameters.getParameter<double>("timeSpread");
28 timeMean_ = parameters.getParameter<double>("timeMean");
29 nEarlier_ = parameters.getParameter<int>("nEarlierBunchesToSample");
30 nLater_ = parameters.getParameter<int>("nLaterBunchesToSample");
31 bunchSpacing_ = parameters.getParameter<double>("bunchSpacing");
32 verbosity_ = parameters.getParameter<int>("verbosity");
33
35 if (verbosity_) {
36 ldmx_log(info) << "Got parameters \n \t overlayFileName = "
38 << "\n\t sim pass name = " << simPassName_
39 << "\n\t overlay pass name = " << overlayPassName_
40 << "\n\t overlayCaloHitCollections = ";
41 for (const auto &coll : caloCollections_) ldmx_log(info) << coll << "; ";
42
43 ldmx_log(info) << "\n\t overlayTrackerHitCollections = ";
44 for (const std::string &coll : trackerCollections_)
45 ldmx_log(info) << coll << "; ";
46
47 ldmx_log(info) << "\n\t numberOverlaidInteractions = " << poissonMu_
48 << "\n\t nEarlierBunchesToSample = " << nEarlier_
49 << "\n\t nLaterBunchesToSample = " << nLater_
50 << "\n\t bunchSpacing = " << bunchSpacing_
51 << "\n\t doPoissonIntime = " << doPoissonIT_
52 << "\n\t doPoissonOutoftime = " << doPoissonOOT_
53 << "\n\t timeSpread = " << timeSigma_
54 << "\n\t timeMean = " << timeMean_
55 << "\n\t verbosity = " << verbosity_;
56 }
57 return;
58}
59
62 if (rndm_.get() == nullptr) {
63 // not been seeded yet, get it from RNSS
66 rndm_ = std::make_unique<TRandom2>(rnss.getSeed("OverlayProducer::rndm"));
67 }
68 // TAV: These boundaries should be configurable
69 int start_event = rndm_->Uniform(20., 4570);
70 // EventFile::skipToEvent handles actual number of events in file
71 int evNb = overlayFile_->skipToEvent(start_event);
72 if (evNb < 0) {
73 EXCEPTION_RAISE("BadRead", "Couldn't read to starting offset.");
74 }
76 ldmx_log(info) << "Starting overlay process with pileup event number " << evNb
77 << " (random event number picked was " << start_event << ").";
78}
79
81 // event is the incoming, simulated event/"hard" process
82 // overlayEvent_ is the overlay producer's own event.
83 if (verbosity_ > 1) {
84 ldmx_log(info) << "produce() starts on simulation event "
85 << event.getEventHeader().getEventNumber();
86 }
87
88 if (rndmTime_.get() == nullptr) {
89 // not been seeded yet, get it from RNSS
92 rndmTime_ =
93 std::make_unique<TRandom2>(rnss.getSeed("OverlayProducer::rndmTime"));
94 }
95
96 // using nextEvent to loop, we need to loop over overlay events and in an
97 // inner loop, loop over collections, and store them. after all pileup events
98 // have been added, the vector of collections is iterated over and added to
99 // the event bus.
100 std::map<std::string, std::vector<ldmx::SimCalorimeterHit>> caloCollectionMap;
101 std::map<std::string, std::vector<ldmx::SimTrackerHit>> trackerCollectionMap;
102 std::map<int, ldmx::SimCalorimeterHit> hitMap;
103
104 // start by copying over all the collections from the sim event
105
106 /* ----------- first do the SimCalorimeterHits ----------- */
107
108 // get the calo hits collections that we want to overlay, by looping over
109 // the list of collections passed to the producer : caloCollections_
110 for (const auto &collName : caloCollections_) {
111 // for now, Ecal and only Ecal uses contribs instead of multiple
112 // SimHitsCalo per channel, meaning, it requires special treatment
113 auto needsContribsAdded{collName.find("Ecal") != std::string::npos ? true
114 : false};
115
116 // start out by just copying the sim hits, unaltered.
117 auto simHitsCalo =
118 event.getCollection<ldmx::SimCalorimeterHit>(collName, simPassName_);
119 // but don't copy ecal hits immediately: for them, wait until overlay
120 // contribs have been added. then add everything through the hitmap
121 // TAV: the string "Overlay" should be configurable too
122 if (!needsContribsAdded) {
123 caloCollectionMap[collName + "Overlay"] = simHitsCalo;
124 }
125
126 if (verbosity_ > 2) {
127 ldmx_log(debug) << "in loop: start of collection " << collName
128 << "in loop: printing current sim event: ";
129 }
130 ldmx_log(debug) << "in loop: size of sim hits vector " << collName << " is "
131 << simHitsCalo.size();
132
133 // we don't need to touch the hard process sim hits, really... but we
134 // might need the simhits in the hit map.
135 if (needsContribsAdded || verbosity_ > 2) {
136 for (const ldmx::SimCalorimeterHit &simHit : simHitsCalo) {
137 if (verbosity_ > 2) simHit.Print();
138
139 if (needsContribsAdded) {
140 // this copies the hit, its ID and its coordinates directly
141 hitMap[simHit.getID()] = simHit;
142 }
143
144 } // over calo simhit collection
145 } // if needContribs or very verbose
146
147 } // over calo collections for sim event
148
149 /* ----------- now do the same with SimTrackerHits! ----------- */
150
151 // get the SimTrackerHit collections that we want to overlay, by looping
152 // over the list of collections passed to the producer : trackerCollections_
153 for (const auto &collName : trackerCollections_) {
154 auto simHitsTracker =
155 event.getCollection<ldmx::SimTrackerHit>(collName, simPassName_);
156 trackerCollectionMap[collName + "Overlay"] = simHitsTracker;
157
158 // the rest is printouts for debugging
159 ldmx_log(debug) << "in loop: size of sim hits vector " << collName << " is "
160 << simHitsTracker.size();
161
162 if (verbosity_ > 2) {
163 ldmx_log(debug) << "in loop: start of collection " << collName
164 << "in loop: printing current sim event: ";
165
166 for (const ldmx::SimTrackerHit &simHit : simHitsTracker) simHit.Print();
167 } // if high verbosity
168 } // over tracker collections for sim event
169
170 /* ----------- now do the pileup overlay ----------- */
171
172 // we could shift these by a random number, effectively placing the
173 // sim event at random positions in the interval, preserving the
174 // overall interval length
175 // int simBunch= (int)rndmTime_->Uniform(
176 // -(nEarlier_+1) , nLater_+1); // +1 to get
177 // inclusive interval
178 int startBunch = -nEarlier_;
179 int endBunch = nLater_;
180
181 // TODO -- figure out if we should also randomly shift the time of the sim
182 // event (likely only needed if time bias gets picked up by BDT or ML by way
183 // of pulse behaviour)
184 for (int bunchOffset{startBunch}; bunchOffset <= endBunch; bunchOffset++) {
185 // sample a poisson distribution, or use mu as fixed number of overlay
186 // events
187 int nEvsOverlay =
188 doPoissonOOT_ ? rndm_->Poisson(poissonMu_) : (int)poissonMu_;
189
190 // special case: in-time pileup at bunch 0
191 if (bunchOffset == 0) {
192 if (!doPoissonIT_)
193 nEvsOverlay = (int)poissonMu_; // fix it to the average
194 else if (doPoissonIT_ && !doPoissonOOT_) // then we haven't set this yet
195 nEvsOverlay = rndm_->Poisson(poissonMu_);
196
197 // paticularly useful in the poisson fluctuated case
198 event.getEventHeader().setIntParameter("inTimePU", nEvsOverlay);
199
200 // the total number of events is nPU + 1 (it includes the sim event)
201 nEvsOverlay -= 1; // in any case, subtract the sim event from nOverlay
202 if (verbosity_ > 2) {
203 ldmx_log(debug) << "will overlay " << nEvsOverlay
204 << " events on the simulated one";
205 }
206 }
207
208 float bunchTimeOffset = bunchSpacing_ * bunchOffset;
209
210 for (int iEv = 0; iEv < nEvsOverlay; iEv++) {
217 if (!overlayFile_->nextEvent()) {
218 ldmx_log(error) << "At sim event "
219 << event.getEventHeader().getEventNumber()
220 << ": couldn't read next overlay event!";
221 return;
222 }
223
224 // a pileup event wide time offset to be applied to all its hits.
225 float timeOffset = rndmTime_->Gaus(timeMean_, timeSigma_);
226 timeOffset += bunchTimeOffset;
227
228 if (verbosity_ > 2) {
229 ldmx_log(debug) << "in overlay loop: overlaying event "
231 << "which is " << iEv + 1 << " out of " << nEvsOverlay
232 << "\n\thit time offset is " << timeOffset << " ns"
233 << "\n\tbunch position offset is " << bunchOffset
234 << ", leading to a total time offset of "
235 << bunchTimeOffset << " ns";
236 }
237
238 /* ----------- first do the SimCalorimeterHits overlay ----------- */
239
240 // again get the calo hits collections that we want to overlay
241 for (uint iColl = 0; iColl < caloCollections_.size(); iColl++) {
242 // for now, Ecal and only Ecal uses contribs
243 bool needsContribsAdded = false;
244 if (strstr(caloCollections_[iColl].c_str(), "Ecal"))
245 needsContribsAdded = true;
246
247 std::vector<ldmx::SimCalorimeterHit> overlayHits =
250
251 ldmx_log(debug) << "in loop: size of overlay hits vector is "
252 << overlayHits.size();
253
254 std::string outCollName = caloCollections_[iColl] + "Overlay";
255
256 if (verbosity_ > 2) {
257 ldmx_log(debug) << "in loop: printing overlay event: ";
258 }
259
260 for (ldmx::SimCalorimeterHit &overlayHit : overlayHits) {
261 if (verbosity_ > 2) overlayHit.Print();
262
263 const float overlayTime = overlayHit.getTime() + timeOffset;
264 overlayHit.setTime(overlayTime);
265
266 if (needsContribsAdded) { // special treatment for (for now only)
267 // ecal
268 int overlayHitID = overlayHit.getID();
269 if (hitMap.find(overlayHitID) ==
270 hitMap.end()) { // there wasn't already a simhit in this id
271 hitMap[overlayHitID] = ldmx::SimCalorimeterHit();
272 hitMap[overlayHitID].setID(overlayHitID);
273 std::vector<float> hitPos = overlayHit.getPosition();
274 hitMap[overlayHitID].setPosition(hitPos[0], hitPos[1], hitPos[2]);
275 }
276 // add the overlay hit (as a) contrib
277 // incidentID = -1000, trackID = -1000, pdgCode = 0 <-- these are
278 // set in the header for now but could be parameters
279 hitMap[overlayHitID].addContrib(overlayIncidentID_, overlayTrackID_,
280 overlayPdgCode_,
281 overlayHit.getEdep(), overlayTime);
282 } // if add overlay as contribs
283 else {
284 caloCollectionMap[outCollName].push_back(overlayHit);
285 if (verbosity_ > 2)
286 ldmx_log(debug) << "Adding non-Ecal overlay hit to outhit vector "
287 << outCollName;
288 }
289 } // over overlay calo simhit collection
290
291 if (!needsContribsAdded)
292 ldmx_log(debug) << "Nhits in overlay collection " << outCollName
293 << ": " << caloCollectionMap[outCollName].size();
294
295 } // over caloCollections
296
297 /* ----------- now do simtracker hits overlay ----------- */
298
299 // get the SimTrackerHit collections that we want to overlay
300 for (const auto &coll : trackerCollections_) {
301 auto overlayTrackerHits{
304
305 ldmx_log(debug) << "in loop: size of overlay hits vector is "
306 << overlayTrackerHits.size();
307
308 auto outCollName{coll + "Overlay"};
309
310 if (verbosity_ > 2) {
311 ldmx_log(debug) << "in loop: printing overlay event: ";
312 }
313
314 for (auto &overlayHit : overlayTrackerHits) {
315 auto overlayTime{overlayHit.getTime() + timeOffset};
316 overlayHit.setTime(overlayTime);
317 trackerCollectionMap[outCollName].push_back(overlayHit);
318
319 if (verbosity_ > 2) {
320 overlayHit.Print();
321 ldmx_log(debug) << "Adding tracker overlay hit to outhit vector "
322 << outCollName;
323 } // verbose
324 } // over overlay tracker simhit collection
325
326 ldmx_log(debug) << "Nhits in overlay collection " << outCollName << ": "
327 << trackerCollectionMap[outCollName].size();
328
329 } // over trackerCollections
330
331 } // over overlay events
332 } // over bunches
333
334 // after all events are done, the ecal hitmap is final and can be written to
335 // the event output
336 for (uint iColl = 0; iColl < caloCollections_.size(); iColl++) {
337 // loop through collection names to find the right collection name
338 // add overlaid ecal hits as contribs/from hitmap rather than as copied
339 // simhits
340 if (strstr(caloCollections_[iColl].c_str(), "Ecal")) {
341 if (verbosity_ > 2)
342 ldmx_log(debug) << "Hits in hitmap after overlay of "
343 << caloCollections_[iColl] << "Overlay :";
344
345 for (auto &mapHit : hitMap) {
346 if (verbosity_ > 2) mapHit.second.Print();
347
348 if (caloCollectionMap.find(caloCollections_[iColl] + "Overlay") ==
349 caloCollectionMap.end()) {
350 ldmx_log(debug) << "Adding first hit from hit map as first outhit "
351 "vector to caloCollectionMap";
352 caloCollectionMap[caloCollections_[iColl] + "Overlay"] = {
353 mapHit.second};
354 } else
355 caloCollectionMap[caloCollections_[iColl] + "Overlay"].push_back(
356 mapHit.second);
357 }
358 break; // for now we only have one hitMap: for Ecal. so no need looking
359 // further after we got a match
360 } // isEcal
361 } // second loop over collections, to collect hits from hitmap
362
363 // done collecting hits.
364
365 // this should be added to the sim file, so to "event"
366 // once for each hit type
367 for (auto &[name, coll] : caloCollectionMap) {
368 ldmx_log(debug) << "Writing " << name << " to event bus.";
369 if (verbosity_ > 2) {
370 ldmx_log(debug) << "List of hits added: ";
371 for (auto &hit : coll) hit.Print();
372 }
373 event.add(name, coll);
374 }
375 for (auto &[name, coll] : trackerCollectionMap) {
376 ldmx_log(debug) << "Writing " << name << " to event bus.";
377 if (verbosity_ > 2) {
378 ldmx_log(debug) << "List of hits added: ";
379 for (auto &hit : coll) hit.Print();
380 }
381 event.add(name, coll);
382 }
383 return;
384}
385
387 if (verbosity_ > 2) {
388 ldmx_log(debug) << "onProcessStart() ";
389 }
390
391 // replace by this line once the corresponding tweak to EventFile is ready:
393 std::make_unique<framework::EventFile>(params_, overlayFileName_, true);
394 overlayFile_->setupEvent(&overlayEvent_);
395 // we update the iterator at the end of each event. so do this once here to
396 // grab the first event in the processor
397
398 if (verbosity_ > 2) {
399 ldmx_log(debug) << "onProcessStart () successful. Used input file: "
400 << overlayFile_->getFileName();
401 ldmx_log(debug) << "onProcessStart () successful. Got event info: ";
402 overlayFile_->getEvent()->Print();
403 }
404
405 return;
406}
407
408} // namespace recon
409
410DECLARE_PRODUCER_NS(recon, OverlayProducer)
#define DECLARE_PRODUCER_NS(NS, CLASS)
Macro which allows the framework to construct a producer given its name during configuration.
Conditions object for random number seeds.
Class which stores simulated calorimeter hit information.
Class which encapsulates information from a hit in a simulated tracking detector.
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
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:388
ldmx::EventHeader & getEventHeader()
Get the event header.
Definition Event.h:59
static const std::string CONDITIONS_OBJECT_NAME
Conditions object name.
Class encapsulating parameters for configuring a processor.
Definition Parameters.h:29
void setEventNumber(int eventNumber)
Set the event number.
int getEventNumber() const
Return the event number.
Definition EventHeader.h:78
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.
void Print() const
Print a description of this object.
std::string simPassName_
To use for finding the sim event bus passengers, mostly a disambiguation.
std::unique_ptr< framework::EventFile > overlayFile_
Pileup overlay events input file.
std::string overlayPassName_
Pileup overlay events input pass name.
std::string overlayFileName_
Pileup overlay events input file name.
std::vector< std::string > caloCollections_
List of SimCalorimeterHit collection(s) to loop over and add hits from, combining sim and pileup.
int verbosity_
Local control of processor verbosity.
int overlayIncidentID_
For Ecal, overlay hits should be added as contribs.
std::unique_ptr< TRandom2 > rndmTime_
Random number generator for pileup event time offset.
int nEarlier_
Number of bunches before the sim event to pull pileup events from.
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,...
double bunchSpacing_
Spacing in time (in [ns]) between electron bunches.
double timeMean_
Average position in time (in [ns]) of pileup bunches, relative to the sim event.
framework::config::Parameters params_
The parameters used to configure this producer.
double poissonMu_
(average) total number of events
framework::Event overlayEvent_
The overlay ldmx event bus.
int nLater_
Number of bunches after the sim event to pull pileup events from.
bool doPoissonIT_
Let the total number of in-time events be poisson distributed, or fix at the chosen value,...
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 timeSigma_
Width of pileup bunch spread in time (in [ns]), specified as a sigma of a Gaussian distribution.
void configure(framework::config::Parameters &parameters) override
Configure the processor with input parameters from the python cofig.
bool doPoissonOOT_
Let the total number of out-of-time events be poisson distributed, or fix at the chosen value,...
void onProcessStart() override
At the start of processing, the pileup overlay file is set up.
std::vector< std::string > trackerCollections_
List of SimTrackerHit collection(s) to loop over and add hits from, combining sim and pileup.