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