ATLAS Offline Software
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 
16 MarsHaloGenerator::MarsHaloGenerator(const HepPDT::ParticleDataTable* particleTable,
17  const std::string& inputFile,
18  const std::vector<std::string>& 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 
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 
63 
65 
66  return 0;
67 }
68 
69 //------------------------------------------------------------------
70 
71 int 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 
99  m_eventNumber++;
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.
145  m_counters[TOT_READ]++;
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 
163 int 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.
193  m_counters[TOT_GEN]++;
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 //------------------------------------------------------------------
query_example.row
row
Definition: query_example.py:24
AsciiInput.h
BeamHaloParticle
A class to describe a generic beam halo particle.
Definition: BeamHaloParticle.h:22
MarsHaloGenerator::readEvent
virtual int readEvent(std::vector< BeamHaloParticle > *beamHaloEvent, CLHEP::HepRandomEngine *engine)
A function to read one event in a simplified format.
Definition: MarsHaloGenerator.cxx:163
BeamHaloGenerator::m_wsums
double m_wsums[NUM_COUNTERS]
Sum of weights of particles or events of dimension enumCounters.
Definition: BeamHaloGenerator.h:151
MarsParticle
A class to describe a MARS particle read from an input ASCII file.
Definition: MarsParticle.h:55
BeamHaloGenerator::m_enableSampling
bool m_enableSampling
Flag to enable or disable sampling.
Definition: BeamHaloGenerator.h:120
BeamHaloGenerator::m_bufferFileName
std::string m_bufferFileName
The name of the binary buffer file, needed for sampling from a converted file.
Definition: BeamHaloGenerator.h:124
BeamHaloParticleBuffer.h
BeamHaloGenerator::m_counters
long m_counters[NUM_COUNTERS]
Particle or event counters of dimension enumCounters.
Definition: BeamHaloGenerator.h:148
MarsHaloGenerator::genFinalize
virtual int genFinalize()
A function to finalise the generator.
Definition: MarsHaloGenerator.cxx:106
BeamHaloGenerator::TOT_AFTER
@ TOT_AFTER
Definition: BeamHaloGenerator.h:143
BeamHaloParticleBuffer::openForReading
int openForReading()
Definition: BeamHaloParticleBuffer.cxx:65
LArG4FSStartPointFilter.evt
evt
Definition: LArG4FSStartPointFilter.py:42
BeamHaloParticleBuffer::writeParticle
int writeParticle(BeamHaloParticle *particle)
A member function to append a particle to the binary file.
Definition: BeamHaloParticleBuffer.cxx:109
BeamHaloGenerator::TOT_GEN
@ TOT_GEN
Definition: BeamHaloGenerator.h:144
MarsHaloGenerator.h
BeamHaloGenerator::MARS_READ
@ MARS_READ
Definition: BeamHaloGenerator.h:87
AsciiInput::readRow
std::vector< std::string > readRow()
Read one line of the input text file into a vector of strings.
Definition: AsciiInput.cxx:42
BeamHaloGenerator::m_asciiInput
AsciiInput * m_asciiInput
A pointer to an AsciiInput object, used to read data from the Ascii input file.
Definition: BeamHaloGenerator.h:131
MarsHaloGenerator::fillEvt
virtual int fillEvt(HepMC::GenEvent *evt, CLHEP::HepRandomEngine *engine)
A function to create one event in HepMC format.
Definition: MarsHaloGenerator.cxx:71
jobOptions_CavernBackground.generatorSettings
generatorSettings
Definition: jobOptions_CavernBackground.py:35
dqt_zlumi_pandas.weight
int weight
Definition: dqt_zlumi_pandas.py:189
BeamHaloGeneratorAlg.h
BeamHaloGenerator::m_particleTable
const HepPDT::ParticleDataTable * m_particleTable
A pointer to the particle data table.
Definition: BeamHaloGenerator.h:105
BeamHaloParticle.h
BeamHaloGeneratorSettings.h
BeamHaloGenerator
An abstract base class to provide generic beam halo generator functionality.
Definition: BeamHaloGenerator.h:31
AsciiInput::close
int close()
Close the input file.
Definition: AsciiInput.cxx:32
BeamHaloGenerator::genFinalize
virtual int genFinalize()
A function to finalise the generator.
Definition: BeamHaloGenerator.cxx:57
BeamHaloParticle::fill
int fill(const HepPDT::ParticleDataTable *particleDataTable, MarsParticle *marsParticle)
A function to fill the data members from an input MarsParticle object.
Definition: BeamHaloParticle.cxx:52
CaloCondBlobAlgs_fillNoiseFromASCII.inputFile
string inputFile
Definition: CaloCondBlobAlgs_fillNoiseFromASCII.py:17
BeamHaloParticleBuffer::close
int close()
Definition: BeamHaloParticleBuffer.cxx:84
BeamHaloGenerator::MARS_SINGLE
@ MARS_SINGLE
Definition: BeamHaloGenerator.h:88
MarsParticle::read
int read(std::vector< std::string > *eventAsStringVector)
A function to read the values from a single row definition and fill the data members.
Definition: MarsParticle.cxx:37
BeamHaloGenerator::m_debug
bool m_debug
A flat to turn on or off debug print out.
Definition: BeamHaloGenerator.h:154
BeamHaloGenerator::m_beamHaloGeneratorSettings
BeamHaloGeneratorSettings * m_beamHaloGeneratorSettings
A pointer to a BeamHaloGeneratorSettings object used to filter particles.
Definition: BeamHaloGenerator.h:135
BeamHaloParticleBuffer::readRandomParticle
BeamHaloParticle * readRandomParticle(CLHEP::HepRandomEngine *engine)
A member function to read a random particle from the binary file.
Definition: BeamHaloParticleBuffer.cxx:171
MarsParticle.h
MarsHaloGenerator::readParticle
virtual int readParticle(BeamHaloParticle *beamHaloParticle)
A function to read one particle from the input ASCII file.
Definition: MarsHaloGenerator.cxx:125
BeamHaloGenerator::m_eventNumber
long m_eventNumber
A data member to count the event number.
Definition: BeamHaloGenerator.h:138
BeamHaloParticleBuffer
Definition: BeamHaloParticleBuffer.h:45
MarsHaloGenerator::MarsHaloGenerator
MarsHaloGenerator(const HepPDT::ParticleDataTable *particleTable, const std::string &inputFile, const std::vector< std::string > &generatorSettings)
Definition: MarsHaloGenerator.cxx:16
BeamHaloGeneratorSettings::checkParticle
bool checkParticle(BeamHaloParticle *beamHaloParticle)
Check if the supplied beam halo particle passes the generator settings.
Definition: BeamHaloGeneratorSettings.cxx:139
AsciiInput::open
int open()
Open the input file.
Definition: AsciiInput.cxx:22
BeamHaloParticle::weight
double weight() const
A function to return the weight of this particle within the input beam background simulation file.
Definition: BeamHaloParticle.h:70
BeamHaloGenerator::genInitialize
virtual int genInitialize()
A function to initialise the generator.
Definition: BeamHaloGenerator.cxx:48
BeamHaloGenerator::m_inputFile
std::string m_inputFile
Input file name.
Definition: BeamHaloGenerator.h:108
BeamHaloParticleBuffer::openForWriting
int openForWriting()
Definition: BeamHaloParticleBuffer.cxx:48
BeamHaloGenerator::convertEvent
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.
Definition: BeamHaloGenerator.cxx:94
BeamHaloGenerator::m_beamHaloParticleBuffer
BeamHaloParticleBuffer * m_beamHaloParticleBuffer
Binary particle buffer for caching converted events.
Definition: BeamHaloGenerator.h:127
HepMC::set_signal_process_id
void set_signal_process_id(GenEvent *e, const int i)
Definition: GenEvent.h:641
BeamHaloGenerator::TOT_READ
@ TOT_READ
Definition: BeamHaloGenerator.h:142
MarsHaloGenerator::genInitialize
virtual int genInitialize()
A function to initialise the generator.
Definition: MarsHaloGenerator.cxx:24