LDMX Software
ReSimulator.cxx
1#include "SimCore/ReSimulator.h"
2
3namespace simcore {
4
6 SimulatorBase::configure(parameters);
8 parameters.getParameter<bool>("resimulate_all_events");
10 care_about_run_ = parameters.getParameter<bool>("care_about_run");
11 auto configured_events{
12 parameters.getParameter<std::vector<framework::config::Parameters>>(
13 "events_to_resimulate", {})};
14 if (configured_events.size() == 0) {
15 EXCEPTION_RAISE(
16 "ReSimNoEvents",
17 "ReSim was configured with resimulate_all_events marked false but "
18 "no event numbers were requested.\n\nDid you forget to configure "
19 "the events_to_resimulate parameter?\n");
20 }
21 for (const auto& run_event : configured_events) {
22 events_to_resimulate_.emplace_back(run_event.getParameter<int>("run"),
23 run_event.getParameter<int>("event"));
24 }
25 }
26}
27
29 /* numEventsBegan_++; */
30 auto& eventHeader{event.getEventHeader()};
31 const auto eventNumber{eventHeader.getEventNumber()};
32 if (skip(event)) {
33 if (verbosity_ > 1) {
34 std::cout << "Skipping event: " << eventNumber
35 << " since it wasn't part of the requested events..."
36 << std::endl;
37 }
38 this->abortEvent(); // get out of processors loop
39 return;
40 }
41 if (verbosity_ > 0) {
42 std::cout << "Resimulating " << eventNumber << std::endl;
43 }
44
45 std::istringstream iss(eventHeader.getStringParameter("eventSeed"));
46 G4Random::restoreFullState(iss);
47 runManager_->ProcessOneEvent(eventNumber);
48 if (verbosity_ > 1) {
49 std::cout << "Finished with event number " << eventNumber << std::endl;
50 }
51 if (runManager_->GetCurrentEvent()->IsAborted()) {
52 runManager_->TerminateOneEvent();
54 [](auto sd) { sd->OnFinishedEvent(); });
55 EXCEPTION_RAISE(
56 "ReSimAbortedEvent",
57 "Resimulation resulted in an aborted event, something is wrong with "
58 "the seed from event " +
59 std::to_string(eventNumber));
60 }
61
62 eventHeader.setEventNumber(++events_resimulated_);
63 updateEventHeader(eventHeader);
64 saveTracks(event);
65
66 saveSDHits(event);
67
68 runManager_->TerminateOneEvent();
69}
70
76 if (resimulate_all_events_) return false;
83 auto found_event_to_resim = std::find_if(
84 std::begin(events_to_resimulate_), std::end(events_to_resimulate_),
85 [&](const std::pair<int, int>& run_event) -> bool {
86 bool runs_match = true;
88 runs_match = (event.getEventHeader().getRun() == run_event.first);
89 return event.getEventNumber() == run_event.second and runs_match;
90 });
91 return (found_event_to_resim == std::end(events_to_resimulate_));
92}
93
94} // namespace simcore
95DECLARE_PRODUCER_NS(simcore, ReSimulator)
#define DECLARE_PRODUCER_NS(NS, CLASS)
Macro which allows the framework to construct a producer given its name during configuration.
void abortEvent()
Abort the event immediately.
Implements an event buffer system for storing event data.
Definition Event.h:41
ldmx::EventHeader & getEventHeader()
Get the event header.
Definition Event.h:58
Class encapsulating parameters for configuring a processor.
Definition Parameters.h:27
T getParameter(const std::string &name) const
Retrieve the parameter of the given name.
Definition Parameters.h:89
int getRun() const
Return the run number.
Definition EventHeader.h:84
void apply(UnaryFunction f) const
Apply the input UnaryFunction to each entry in the inventory.
Definition Factory.h:283
static Factory & get()
get the factory instance
Definition Factory.h:217
void produce(framework::Event &event) override
Run resimulation if the event is part of the requested sets of events to resimulate.
std::vector< std::pair< int, int > > events_to_resimulate_
List of events in the input files that should be resimulated if resimulate_all_events is false.
Definition ReSimulator.h:45
void configure(framework::config::Parameters &parameters) override
Callback for the processor to configure itself from the given set of parameters.
bool care_about_run_
Whether or not we should check the run number when seeing if a specific event should be resimulated.
Definition ReSimulator.h:56
bool skip(framework::Event &event) const
Check if an event should be skipped during resimulation.
bool resimulate_all_events_
Whether to resimulate all events in the input files.
Definition ReSimulator.h:50
std::unique_ptr< RunManager > runManager_
Manager controlling G4 simulation run.
void configure(framework::config::Parameters &parameters) override
Callback for the EventProcessor to configure itself from the given set of parameters.
int verbosity_
Vebosity for the simulation.