LDMX Software
LHEReader.cxx
2
3namespace simcore {
4namespace lhe {
5
6LHEReader::LHEReader(std::string& filename) {
7 ldmx_log(info) << "Opening LHE file " << filename;
8 ifs_.open(filename.c_str(), std::ifstream::in);
9
10 if (!ifs_.is_open()) {
11 EXCEPTION_RAISE("BadFile", "Failed to open LHE file: " + filename);
12 }
13}
14
15std::unique_ptr<LHEEvent> LHEReader::readNextEvent() {
16 std::string line;
17 bool found_event_element = false;
18 while (getline(ifs_, line)) {
19 auto back =
20 std::find_if_not(line.rbegin(), line.rend(), [](unsigned char c) {
21 return std::isspace(c);
22 }).base();
23 line.erase(back, line.end());
24 if (line == "<event>") {
25 found_event_element = true;
26 break;
27 }
28 }
29
30 if (!found_event_element) {
31 ldmx_log(warn) << "No next <event> element was found by the LHE reader.";
32 return nullptr;
33 }
34
35 getline(ifs_, line);
36
37 // Create the LHEEvent using std::make_unique
38 auto next_event = std::make_unique<LHEEvent>(line);
39
40 while (getline(ifs_, line)) {
41 auto back =
42 std::find_if_not(line.rbegin(), line.rend(), [](unsigned char c) {
43 return std::isspace(c);
44 }).base();
45 line.erase(back, line.end());
46 if (line == "</event>" || line == "<mgrwt>") {
47 // break if the event ended or in LHE 3.0 if we reach the mgrwt block
48 break;
49 }
50
51 if (line.find("#") == std::string::npos) { // not a comment line
52 // Create LHEParticle using std::make_unique and add it to the event
53 auto particle = std::make_unique<LHEParticle>(line);
54 next_event->addParticle(std::move(particle));
55 } else {
56 if (line.find("#vertex") != std::string::npos) {
57 next_event->setVertex(line);
58 }
59 }
60 }
61
62 const std::vector<std::unique_ptr<LHEParticle>>& particles =
63 next_event->getParticles();
64 for (const auto& particle : particles) {
65 if (particle->getMother(0) != 0) {
66 int mother1 = particle->getMother(0);
67 int mother2 = particle->getMother(1);
68 if (mother1 > 0) {
69 particle->setMother(0, particles[mother1 - 1].get());
70 }
71 if (mother2 > 0) {
72 particle->setMother(1, particles[mother2 - 1].get());
73 }
74 }
75 }
76
77 return next_event;
78}
79
80} // namespace lhe
81} // namespace simcore
Class for reading LHE event data.
std::ifstream ifs_
The input file stream.
Definition LHEReader.h:48
std::unique_ptr< LHEEvent > readNextEvent()
Read the next event.
Definition LHEReader.cxx:15
LHEReader(std::string &fileName)
Class constructor.
Definition LHEReader.cxx:6
Dynamically loadable photonuclear models either from SimCore or external libraries implementing this ...