Skip to content
This repository was archived by the owner on May 6, 2024. It is now read-only.

Commit 447d577

Browse files
refactor to move config deducing into python
the translation of different format of runs provided by the user into the actual data structure used during processing is done within python now so it can be more transparent in error
1 parent 9f605fb commit 447d577

3 files changed

Lines changed: 59 additions & 62 deletions

File tree

include/SimCore/ReSimulator.h

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,43 +27,33 @@ class ReSimulator : public SimulatorBase {
2727

2828
private:
2929
/**
30-
* List of event numbers in the input files that should be resimulated if
30+
* List of events in the input files that should be resimulated if
3131
* `resimulate_all_events` is false.
3232
*
33-
* @note: If an event number in `events_to_resimulate_` is not part of the
33+
* Each event is identified uniquely by its run number and event number.
34+
*
35+
* @note: If an event in `events_to_resimulate_` is not part of the
3436
* input file, it will be ignored.
3537
*/
36-
std::vector<int> events_to_resimulate_;
38+
std::vector<std::pair<int,int>> events_to_resimulate_;
3739

3840
/**
39-
* List of run numbers in the input files that should be resimulated if
40-
* `resimulate_all_events` is false.
41-
*
42-
* @note: If a run number in `runs_to_resimulate_` is not part of the
43-
* input file, it will be ignored.
44-
*
45-
* The list of runs to resimulate comes in one of three forms:
46-
* 1. Empty: If no runs are given, we only match based off the event number.
47-
* 2. Single Entry: If only one run is given, then we require the run number
48-
* to be that entry for all events requested to resimulate.
49-
* 3. More than one entry: In this case, we require the length of this list
50-
* to be the same as the length of the events to resimulate so that we
51-
* only resimulate events where the event/run *pair* matches a corresponding
52-
* *pair* in the events and runs to resimulate lists.
41+
* Whether to resimulate all events in the input files
5342
*/
54-
std::vector<int> runs_to_resimulate_;
43+
bool resimulate_all_events_;
5544

5645
/**
57-
* Whether to resimulate all events in the input files
46+
* Whether or not we should check the run number when seeing
47+
* if a specific event should be resimulated
5848
*/
59-
bool resimulate_all_events;
49+
bool care_about_run_;
6050

6151
/*
6252
* How many events have already been resimulated. This determines the event
6353
* number in the output file, since more than one input file can be used.
6454
*
6555
*/
66-
int events_resimulated = 0;
56+
int events_resimulated_ = 0;
6757
};
6858
} // namespace simcore
6959

python/simulator.py.in

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,25 @@ with several helpful member functions.
66

77
from @PYTHON_PACKAGE_NAME@.Framework.ldmxcfg import Producer
88

9+
class _EventToReSim:
10+
"""A class to hold the information identifying a specific event we wish to re-simulate
11+
12+
This is an internal class used by simulator.resimulate in order to pass the event
13+
identification to the ReSimulator class
14+
15+
Attributes
16+
----------
17+
run: int
18+
run number of the event to re-sim, -1 if we don't care about the run
19+
event: int
20+
event number to re-sim, required
21+
"""
22+
23+
def __init__(self, event, run = -1):
24+
self.event = event
25+
self.run = run
26+
27+
928
class simulator(Producer):
1029
"""A instance of the simulation configuration
1130

@@ -177,23 +196,25 @@ class simulator(Producer):
177196
resimulator.className = 'simcore::ReSimulator'
178197
if which_events is None:
179198
resimulator.resimulate_all_events = True
199+
resimulator.care_about_run = False
180200
resimulator.events_to_resimulate = [ ]
181-
resimulator.runs_to_resimulate = [ ]
182201
elif isinstance(which_events, list):
183202
resimulator.resimulate_all_events = False
184-
resimulator.events_to_resimulate = which_events
185203
if len(which_events) == 0:
186204
raise ValueError('which_events must contain at least one element if provided')
187205
if which_runs is None:
188-
resimulator.runs_to_resimulate = [ ]
206+
resimulator.care_about_run = False
207+
resimulator.events_to_resimulate = [ _EventToReSim(event) for event in which_events ]
189208
elif isinstance(which_runs, int):
190-
resimulator.runs_to_resimulate = [ which_runs ]
209+
resimulator.care_about_run = True
210+
resimulator.events_to_resimulate = [ _EventToReSim(event, which_runs) for event in which_events ]
191211
elif isinstance(which_runs, list):
192212
if len(which_runs) == 0:
193213
raise ValueError('which_runs must have at least one value if provided as a list')
194214
if len(which_runs) != len(which_events):
195215
raise ValueError('which_runs must have the same number of entries as which_events if more than one run is provided')
196-
resimulator.runs_to_resimulate = which_runs
216+
resimulator.care_about_run = True
217+
resimulator.runs_to_resimulate = [ _EventToReSim(event, run) for event, run in zip(which_events, which_runs) ]
197218
else:
198219
raise ValueError('which_runs must be an int or a list of ints if provided')
199220
else:

src/SimCore/ReSimulator.cxx

Lines changed: 22 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,55 +4,41 @@ namespace simcore {
44

55
void ReSimulator::configure(framework::config::Parameters& parameters) {
66
SimulatorBase::configure(parameters);
7-
resimulate_all_events =
7+
resimulate_all_events_ =
88
parameters.getParameter<bool>("resimulate_all_events");
9-
if (!resimulate_all_events) {
10-
events_to_resimulate_ =
11-
parameters.getParameter<std::vector<int>>("events_to_resimulate", {});
12-
runs_to_resimulate_ =
13-
parameters.getParameter<std::vector<int>>("runs_to_resimulate", {});
14-
if (events_to_resimulate_.size() == 0) {
9+
if (!resimulate_all_events_) {
10+
care_about_run_ = parameters.getParameter<bool>("care_about_run");
11+
auto configured_events{parameters.getParameter<std::vector<framework::config::Parameters>>("events_to_resimulate", {})};
12+
if (configured_events.size() == 0) {
1513
EXCEPTION_RAISE(
1614
"ReSimNoEvents",
1715
"ReSim was configured with resimulate_all_events marked false but "
1816
"no event numbers were requested.\n\nDid you forget to configure "
1917
"the events_to_resimulate parameter?\n");
2018
}
21-
if (runs_to_resimulate_.size() > 1 and runs_to_resimulate_.size() != events_to_resimulate_.size()) {
22-
EXCEPTION_RAISE(
23-
"ReSimBadConf",
24-
"ReSim needs the runs list to match the length of the events list if there is more than "
25-
"one run that needs to be resimulated.");
19+
for (const auto& run_event : configured_events) {
20+
events_to_resimulate_.emplace_back(
21+
run_event.getParameter<int>("run"),
22+
run_event.getParameter<int>("event")
23+
);
2624
}
2725
}
2826
}
27+
2928
void ReSimulator::produce(framework::Event& event) {
3029
/* numEventsBegan_++; */
3130
auto& eventHeader{event.getEventHeader()};
3231
const auto eventNumber{eventHeader.getEventNumber()};
33-
if (!resimulate_all_events) {
34-
auto found_event = std::find(std::begin(events_to_resimulate_),
35-
std::end(events_to_resimulate_), eventNumber);
36-
bool should_resim{found_event != std::end(events_to_resimulate_)};
37-
if (should_resim and runs_to_resimulate_.size() > 0) {
38-
// non-empty list of runs and event number in event list,
39-
// we need to check run number is also requested
40-
int run{event.getEventHeader().getRun()};
41-
if (runs_to_resimulate_.size() == 1) {
42-
// single run applied to all events requested
43-
should_resim = runs_to_resimulate_.at(0) == run;
44-
} else {
45-
// more than one run, look for run corresponding to index
46-
// of event number found
47-
// developer note: this code has a high-liklihood of throwing
48-
// the std::out_of_range error if the runs_to_resimulate_ vector
49-
// isn't pre-checked to be the same size as events_to_resimulate_
50-
should_resim = runs_to_resimulate_.at(
51-
std::distance(events_to_resimulate_.begin(), found_event)
52-
) == run;
53-
}
54-
}
55-
if (not should_resim) {
32+
if (!resimulate_all_events_) {
33+
auto found_event_to_resim = std::find_if(
34+
std::begin(events_to_resimulate_), std::end(events_to_resimulate_),
35+
[&](const std::pair<int,int>& run_event) -> bool {
36+
bool runs_match = true;
37+
if (care_about_run_) runs_match = (event.getEventHeader().getRun() == run_event.first);
38+
return eventNumber == run_event.second and runs_match;
39+
}
40+
);
41+
if (found_event_to_resim == std::end(events_to_resimulate_)) {
5642
if (verbosity_ > 1) {
5743
std::cout << "Skipping event: " << eventNumber
5844
<< " since it wasn't part of the requested events..."
@@ -83,7 +69,7 @@ void ReSimulator::produce(framework::Event& event) {
8369
std::to_string(eventNumber));
8470
}
8571

86-
eventHeader.setEventNumber(++events_resimulated);
72+
eventHeader.setEventNumber(++events_resimulated_);
8773
updateEventHeader(eventHeader);
8874
saveTracks(event);
8975

0 commit comments

Comments
 (0)