ATLAS Offline Software
Loading...
Searching...
No Matches
FlukaHaloGenerator.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3*/
4
6
10
11#include "AtlasHepMC/GenEvent.h"
12#include "CLHEP/Random/RandFlat.h"
13#include <cmath>
14
16 const std::string& inputFile,
17 const std::vector<std::string>& generatorSettings):
18 BeamHaloGenerator(inputFile, generatorSettings),
19 m_sameEvent(true),
20 m_firstEvent(true),
23}
24
25//--------------------------------------------------------------------------
26
27
29 int ret_val;
30
31 // Initialise base class
32 if((ret_val = BeamHaloGenerator::genInitialize()) != 0) return ret_val;
33
34 if(m_asciiInput->open() != 0) {
35 std::cout << "Error: Could not open ascii input file " << m_inputFile << std::endl;
36 return 1;
37 }
38
39 std::cout << "Info: Reading ascii input file " << m_inputFile << std::endl;
40
41 return 0;
42}
43
44//--------------------------------------------------------------------------
45
46
48 CLHEP::HepRandomEngine* engine) {
49 std::vector<BeamHaloParticle> beamHaloEvent;
50 int ret_val;
51
52 // Read one FLUKA event which passes the generator settings.
53 if((ret_val = readEvent(&beamHaloEvent, engine)) != 0) return ret_val;
54
55 // Convert the particles to GenParticles and attach them to the
56 // event. Flip the event if needed.
57 if((ret_val = BeamHaloGenerator::convertEvent(&beamHaloEvent, evt, engine)) != 0) return ret_val;
58
59 // Set the event number
60 evt->set_event_number(m_eventNumber);
61
62 // Set the signal process
64
65 // Increment the event number
67
68 return 0;
69}
70
71//--------------------------------------------------------------------------
72
74 int ret_val;
75
76 m_asciiInput->close();
77
78 // Finalise base class
79 if((ret_val = BeamHaloGenerator::genFinalize()) != 0) return ret_val;
80
81 return 0;
82}
83
84//--------------------------------------------------------------------------
85
87
88 std::cout << "Warning: FlukaHaloGenerator::readParticle is not yet available: "<< beamHaloParticle << std::endl;
89 return 0;
90}
91
92//--------------------------------------------------------------------------
93
94int FlukaHaloGenerator::readEvent(std::vector<BeamHaloParticle> *beamHaloEvent,
95 CLHEP::HepRandomEngine* engine) {
96 BeamHaloParticle beamHaloParticle;
97
98 // Clear the event
99 beamHaloEvent->clear();
100
101 // If there was a last event.
102 if(!m_firstEvent) {
103
104 // If the last event caused the same event flag to be set to false
105 // copy the last particle into the vector of those in this event.
106 if(!m_sameEvent) {
107 // Fill the BeamHaloParticle with the data in the FlukaParticle
108 if(beamHaloParticle.fill(&m_lastFlukaParticle)) {
109 std::cout << "Error: Conversion from FlukaParticle to BeamHaloParticle failed." << std::endl;
110 return 1;
111 }
112 // Append the BeamHalo particle to the event.
113 beamHaloEvent->push_back(beamHaloParticle);
114
115 // Set the same event flag to enter the while loop to read the
116 // rest of this event.
117 m_sameEvent = true;
118 }
119 }
120
121 // Loop over the ascii input and read each particle until a new
122 // event is found or there are no more particles.
123 std::vector<std::string> row;
124 bool endOfFile = false;
125 while(m_sameEvent && !endOfFile) {
126 row = m_asciiInput->readRow(); // Read one line of the ascii file.11
127
128 if(row.size() == 0) {
129 endOfFile = true;
130 continue;
131 }
132
133 if(m_flukaParticle.read(&row)) { // Fill the particle from the string vector
134 continue;
135 }
136
137 // Check if there was a last particle.
138 if(!m_firstEvent) {
139
140 m_sameEvent = false;
141
142 // Check if the event id of the last particle is the same as this particle.
144 && (m_lastFlukaParticle.eventId() == m_flukaParticle.eventId())
145 && (m_lastFlukaParticle.partGenNum() == m_flukaParticle.partGenNum())) m_sameEvent = true;
146
148 && (m_lastFlukaParticle.eventId() == m_flukaParticle.eventId())) m_sameEvent = true;
149 }
150 else {
151 // For the first event.
152 m_firstEvent = false;
153 m_sameEvent = true;
154 }
155
156 // If this is the same event copy the particle into the vector for
157 // this event.
158 if(m_sameEvent) {
159
160 // Fill the BeamHaloParticle with the data in the FlukaParticle
161 if(beamHaloParticle.fill(&m_flukaParticle)) {
162 std::cout << "Error: Conversion from FlukaParticle to BeamHaloParticle failed." << std::endl;
163 return 1;
164 }
165
166 // Append the BeamHalo particle to the event.
167 beamHaloEvent->push_back(beamHaloParticle);
168 }
169
170 // Copy this particle into the last particle.
172
173 }
174
175 if(beamHaloEvent->size() == 0) {
176 std::cout << "Error: No particles read from " << m_inputFile << std::endl;
177 return 1;
178 }
179
181 m_wsums[TOT_READ]+= 1.0;
182
183 // Check if one of the particles in the event passes the generator settings.
184 std::vector<BeamHaloParticle>::iterator itr = beamHaloEvent->begin();
185 std::vector<BeamHaloParticle>::iterator itr_end = beamHaloEvent->end();
186 bool passed = false;
187 for(;itr!=itr_end;++itr) {
188 beamHaloParticle = *itr;
189 // Check the generator settings for this particle.
190 if(m_beamHaloGeneratorSettings->checkParticle(&beamHaloParticle)) {
191 passed = true;
192 }
193 else {
194 if(m_debug) std::cout << "Debug: Particle fails generator settings cuts." << std::endl;
195 }
196 }
197
199 m_wsums[TOT_AFTER]+= 1.0;
201 m_wsums[TOT_GEN]+= 1.0;
202
203 // If all of the particles from this event fail read another event.
204 // If there are no more events this function will exit with a
205 // WARNING.
206 if(!passed) {
207 int ret_val;
208 if((ret_val = readEvent(beamHaloEvent, engine)) != 0) return ret_val;
209 }
210
211 return 0;
212}
bool passed(DecisionID id, const DecisionIDContainer &)
checks if required decision ID is in the set of IDs in the container
long m_eventNumber
A data member to count the event number.
double m_wsums[NUM_COUNTERS]
Sum of weights of particles or events of dimension enumCounters.
int convertEvent(std::vector< BeamHaloParticle > *beamHaloEvent, HepMC::GenEvent *evt, CLHEP::HepRandomEngine *engine)
A member function to convert a vector of beam halo particles into a GenEvent.
std::unique_ptr< AsciiInput > m_asciiInput
A pointer to an AsciiInput object, used to read data from the Ascii input file.
std::string m_inputFile
Input file name.
virtual int genInitialize()
A function to initialise the generator.
BeamHaloGenerator(const std::string &inputFile, const std::vector< std::string > &generatorSettings)
long m_counters[NUM_COUNTERS]
Particle or event counters of dimension enumCounters.
virtual int genFinalize()
A function to finalise the generator.
bool m_debug
A flat to turn on or off debug print out.
std::unique_ptr< BeamHaloGeneratorSettings > m_beamHaloGeneratorSettings
A pointer to a BeamHaloGeneratorSettings object used to filter particles.
A class to describe a generic beam halo particle.
int fill(MarsParticle *marsParticle)
A function to fill the data members from an input MarsParticle object.
FlukaParticle m_lastFlukaParticle
virtual int readParticle(BeamHaloParticle *beamHaloParticle)
A function to read one particle from the input ASCII file.
FlukaHaloGenerator(int type, const std::string &inputFile, const std::vector< std::string > &generatorSettings)
virtual int genInitialize()
A function to initialise the generator.
virtual int fillEvt(HepMC::GenEvent *evt, CLHEP::HepRandomEngine *engine)
A function to create one event in HepMC format.
virtual int genFinalize()
A function to finalise the generator.
virtual int readEvent(std::vector< BeamHaloParticle > *beamHaloEvent, CLHEP::HepRandomEngine *engine)
A function to read one event in a simplified format.
FlukaParticle m_flukaParticle
void set_signal_process_id(GenEvent *e, const int i=0)
Definition GenEvent.h:580
HepMC3::GenEvent GenEvent
Definition GenEvent.h:39