ATLAS Offline Software
Loading...
Searching...
No Matches
MarsHaloGenerator.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
13
14#include "CLHEP/Units/PhysicalConstants.h"
15
16MarsHaloGenerator::MarsHaloGenerator(const HepPDT::ParticleDataTable* particleTable,
17 const std::string& inputFile,
18 const std::vector<std::string>& generatorSettings):
19 BeamHaloGenerator(particleTable, inputFile, generatorSettings) {
20}
21
22//------------------------------------------------------------------
23
25 int ret_val;
26
27 // Initialise base class
28 if((ret_val = BeamHaloGenerator::genInitialize()) != 0) return ret_val;
29
30 if(m_asciiInput->open()) {
31 std::cout << "Error: Could not open ascii input file " << m_inputFile << std::endl;
32 return 1;
33 }
34
35 // Check if sampling is required if sampling is required leave
36 // the ASCII file open for the event loop.
37 if(!m_enableSampling) return 0;
38
40 if(m_beamHaloParticleBuffer->openForWriting()) {
41 std::cout << "Error: Could not open binary buffer file " << m_bufferFileName << " for writing." << std::endl;
42 return 1;
43 }
44
45 std::cout << "Info: Reading ascii input file " << m_inputFile << " into buffer" << std::endl;
46
47 // Loop over all lines in the text file.
48 BeamHaloParticle beamHaloParticle;
49 bool eof = false;
50 while(!eof) {
51 ret_val = readParticle(&beamHaloParticle);
52 if(ret_val < 0) { eof = true; continue; }
53 else if(ret_val > 0) { continue; }
54
55 // Write the BeamHaloParticle into the binary buffer.
56 if(m_beamHaloParticleBuffer->writeParticle(&beamHaloParticle)) {
57 std::cout << "Warning: writeParticle failed." << std::endl;
58 }
59 }
60
61 m_asciiInput->close();
63
64 m_beamHaloParticleBuffer->openForReading();
65
66 return 0;
67}
68
69//------------------------------------------------------------------
70
71int MarsHaloGenerator::fillEvt(HepMC::GenEvent* evt,
72 CLHEP::HepRandomEngine* engine) {
73 std::vector<BeamHaloParticle> beamHaloEvent;
74 int ret_val;
75
76 // Read one MARS event which passes the generator settings.
77 if((ret_val = readEvent(&beamHaloEvent, engine)) != 0) return ret_val;
78
79 // Convert the particles to GenParticles and attach them to the
80 // event. Flip the event if needed.
81 if((ret_val = BeamHaloGenerator::convertEvent(&beamHaloEvent, evt, engine)) != 0) return ret_val;
82
83 // Set the event number
84 evt->set_event_number(m_eventNumber);
85
86 // Clear the values set by the base class convertEvent method
87 evt->weights().clear();
88
89 // Check if sampling has been enabled or not.
90 if(!m_enableSampling) {
92 evt->weights().push_back(beamHaloEvent[0].weight());
93 }
94 else {
96 evt->weights().push_back(1.0);
97 }
98
100
101 return 0;
102}
103
104//------------------------------------------------------------------
105
107 int ret_val;
108
109 // Check if sampling has been enabled or not.
110 if(!m_enableSampling) {
111 m_asciiInput->close();
112 }
113 else {
115 }
116
117 // Finalise base class
118 if((ret_val = BeamHaloGenerator::genFinalize()) != 0) return ret_val;
119
120 return 0;
121}
122
123//------------------------------------------------------------------
124
126 std::vector<std::string> row;
127 MarsParticle marsParticle;
128
129 // Read one row of the ASCII file.
130 row = m_asciiInput->readRow();
131 if(row.size() == 0) return -1; // EOF
132
133 // Fill a MarsParticle with values from the string vector.
134 if(marsParticle.read(&row)) {
135 return 1;
136 }
137
138 // Fill the BeamHaloParticle with the data in the MarsParticle
139 if(beamHaloParticle->fill(m_particleTable, &marsParticle)) {
140 std::cout << "Warning: Conversion from MarsParticle to BeamHaloParticle failed." << std::endl;
141 return 1;
142 }
143
144 // Increment the particles read from file information.
146 m_wsums[TOT_READ]+= beamHaloParticle->weight();
147
148 // Check the generator settings. If this particle fails skip it.
149 if(!m_beamHaloGeneratorSettings->checkParticle(beamHaloParticle)) {
150 if(m_debug) std::cout << "Debug: Particle fails generator settings cuts." << std::endl;
151 return 1;
152 }
153
154 // Increment the particles after cuts information.
156 m_wsums[TOT_AFTER]+= beamHaloParticle->weight();
157
158 return 0;
159}
160
161//------------------------------------------------------------------
162
163int MarsHaloGenerator::readEvent(std::vector<BeamHaloParticle> *beamHaloEvent,
164 CLHEP::HepRandomEngine* engine) {
165 BeamHaloParticle *beamHaloParticle;
166 int ret_val;
167
168 // Clear the event
169 beamHaloEvent->clear();
170
171 // Check if sampling is required
172 if(!m_enableSampling) {
173 beamHaloParticle = new BeamHaloParticle();
174 bool eof = false, gotParticle = false;
175 while(!eof && !gotParticle) {
176 ret_val = readParticle(beamHaloParticle);
177 if(ret_val < 0) { eof = true; }
178 else if(ret_val == 0) { gotParticle = true; }
179 }
180 }
181 else {
182 // Read one particle at random from the binary buffer. This uses
183 // the particle weights to produce a flat distribution rather than a
184 // weighted one, but may generate the same particle twice.
185 beamHaloParticle = m_beamHaloParticleBuffer->readRandomParticle(engine);
186 if(!beamHaloParticle) {
187 std::cout << "Error: readRandomParticle failed." << std::endl;
188 return 1;
189 }
190 }
191
192 // Increment generated particles information.
194 m_wsums[TOT_GEN]+= beamHaloParticle->weight();
195
196
197 // Copy the BeamHaloParticle into this event
198 beamHaloEvent->push_back(*beamHaloParticle);
199
200 // Delete the pointer to the generated particle.
201 delete beamHaloParticle;
202
203 return 0;
204}
205
206//------------------------------------------------------------------
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)
BeamHaloParticleBuffer * m_beamHaloParticleBuffer
Binary particle buffer for caching converted events.
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.
bool m_enableSampling
Flag to enable or disable sampling.
const HepPDT::ParticleDataTable * m_particleTable
A pointer to the particle data table.
std::string m_bufferFileName
The name of the binary buffer file, needed for sampling from a converted file.
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.
double weight() const
A function to return the weight of this particle within the input beam background simulation file.
virtual int readEvent(std::vector< BeamHaloParticle > *beamHaloEvent, CLHEP::HepRandomEngine *engine)
A function to read one event in a simplified format.
MarsHaloGenerator(const HepPDT::ParticleDataTable *particleTable, const std::string &inputFile, const std::vector< std::string > &generatorSettings)
virtual int genFinalize()
A function to finalise the generator.
virtual int readParticle(BeamHaloParticle *beamHaloParticle)
A function to read one particle from the input ASCII file.
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.
A class to describe a MARS particle read from an input ASCII file.
int read(std::vector< std::string > *eventAsStringVector)
A function to read the values from a single row definition and fill the data members.
void set_signal_process_id(GenEvent *e, const int i)
Definition GenEvent.h:643