ATLAS Offline Software
Loading...
Searching...
No Matches
BeamHaloGenerator.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4
11#include "AtlasHepMC/GenEvent.h"
12#include "CLHEP/Random/RandFlat.h"
13#include "CLHEP/Units/PhysicalConstants.h"
14#include "TMath.h"
15#include <cmath>
16
18 const std::string& inputFile,
19 const std::vector<std::string>& generatorSettings):
20 m_gendata(std::make_shared<GenData>()),
21 m_inputFile(inputFile),
23 m_enableFlip(false),
25 m_enableSampling(false),
26 m_bufferFileName("buffer.bin"),
28 m_debug(false),
29 m_generatorSettings(generatorSettings) {
30 for(int i=0;i<NUM_COUNTERS;i++) {
31 m_counters[i] = 0;
32 m_wsums[i] = 0.;
33 }
34}
35
36//------------------------------------------------------------------
37
40
41//------------------------------------------------------------------
42
44 m_asciiInput = std::make_unique<AsciiInput>(m_inputFile);
45 m_beamHaloGeneratorSettings = std::make_unique< BeamHaloGeneratorSettings>(m_generatorSettings);
46
47 return 0;
48}
49
50//------------------------------------------------------------------
51
53
54 std::cout << "=================================================================" << std::endl;
55 std::cout << "| | Total Number | Total Weight |" << std::endl;
56 std::cout << "|---------------------------------------------------------------|" << std::endl;
57 std::cout << "| Particles read from input file | ";
58 std::cout << std::setw(12) << m_counters[TOT_READ] << " | ";
59 std::cout << std::setw(12) << std::setprecision(8) << m_wsums[TOT_READ] << " |" << std::endl;
60 std::cout << "| Particles after cuts | ";
61 std::cout << std::setw(12) << m_counters[TOT_AFTER] << " | ";
62 std::cout << std::setw(12) << std::setprecision(8) << m_wsums[TOT_AFTER] << " |" << std::endl;
63 std::cout << "| Particles generated | ";
64 std::cout << std::setw(12) << m_counters[TOT_GEN] << " | ";
65 std::cout << std::setw(12) << std::setprecision(8) << m_wsums[TOT_GEN] << " |" << std::endl;
66 std::cout << "=================================================================" << std::endl;
67
68 std::cout << std::setprecision(6);
69
70 return 0;
71}
72
73//------------------------------------------------------------------
74
75bool BeamHaloGenerator::flipEvent(CLHEP::HepRandomEngine* engine) {
76 if(!m_enableFlip) return false;
77
78 // Check to see if the event should be flipped or not
79 if(CLHEP::RandFlat::shoot(engine) <= m_flipProbability) {
80 if(m_debug) std::cout << "Debug: Flipping event" << std::endl;
81 return true;
82 }
83
84 return false;
85}
86
87//------------------------------------------------------------------
88
89int BeamHaloGenerator::convertEvent(std::vector<BeamHaloParticle>* beamHaloEvent,
90 HepMC::GenEvent* evt,
91 CLHEP::HepRandomEngine* engine) {
92 double pz;
93 bool flipFlag = flipEvent(engine);
94 HepMC::GenParticlePtr genParticle;
95 HepMC::GenVertexPtr genVertex;
96 const double c = 2.99792458E+11; // speed of light in mm/s
97
98 std::vector<BeamHaloParticle>::iterator itr = beamHaloEvent->begin();
99 std::vector<BeamHaloParticle>::iterator itr_end = beamHaloEvent->end();
100
101 if(itr != itr_end) {
102 // Take the primary information from the first particle in the event. If the event is more than one
103 // particle, the particles come from the same primary proton.
104 evt->weights().push_back((*itr).weight()); // event weight
105 HepMC::FourVector primaryPosition = (*itr).positionAtPrimary();
106 evt->weights().push_back(primaryPosition.x()); // Starting position of primary particle: x position (cm)
107 evt->weights().push_back(primaryPosition.y()); // Starting position of primary particle: y position (cm)
108 evt->weights().push_back(primaryPosition.z()); // Starting position of primary particle: z position (cm)
109 evt->weights().push_back(primaryPosition.t()); // Starting position of primary particle: t (s)
110 }
111
112 // Append each particle to the GenEvent
113 for(;itr!=itr_end;++itr) {
114 HepMC::FourVector position = (*itr).positionAtScoringPlane();
115 HepMC::FourVector fourVector = (*itr).fourVector();
116
117 if(!flipFlag) {
118 genParticle = HepMC::newGenParticlePtr(fourVector,
119 (*itr).pdgId(),
120 1);
121
122 genVertex = HepMC::newGenVertexPtr(HepMC::FourVector(position.x(),
123 position.y(),
124 (position.z() + m_interfacePlane),
125 c*position.t() -1.0*std::fabs(m_interfacePlane)),
126 1);
127 }
128 else {
129 pz = fourVector.pz();
130 fourVector.setPz(-pz);
131
132 genParticle = HepMC::newGenParticlePtr(fourVector,
133 (*itr).pdgId(),
134 1);
135
136 genVertex = HepMC::newGenVertexPtr(HepMC::FourVector(position.x(),
137 position.y(),
138 -1.0*(position.z() + m_interfacePlane),
139 c*position.t() -1.0*std::fabs(m_interfacePlane)),
140 1);
141 }
142
143 genVertex->add_particle_out(genParticle);
144 evt->add_vertex(genVertex);
145 }
146
147 return 0;
148}
149
150//------------------------------------------------------------------
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.
float m_flipProbability
Flip probability.
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.
bool m_enableFlip
Flag for flipping event.
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.
std::vector< std::string > m_generatorSettings
A vector of generator settings in string command form.
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.
bool m_enableSampling
Flag to enable or disable sampling.
double m_interfacePlane
The position of the interface plane in mm.
bool flipEvent(CLHEP::HepRandomEngine *engine)
A member function to check if the event should be flipped.
std::string m_bufferFileName
The name of the binary buffer file, needed for sampling from a converted file.
std::shared_ptr< GenData > m_gendata
A shared pointer to the GenData helper object.
GenData is a class for particle data access.
Definition GenData.h:30
HepMC3::FourVector FourVector
GenParticlePtr newGenParticlePtr(const HepMC3::FourVector &mom=HepMC3::FourVector::ZERO_VECTOR(), int pid=0, int status=0)
Definition GenParticle.h:21
HepMC3::GenParticlePtr GenParticlePtr
Definition GenParticle.h:19
GenVertexPtr newGenVertexPtr(const HepMC3::FourVector &pos=HepMC3::FourVector::ZERO_VECTOR(), const int i=0)
Definition GenVertex.h:25
HepMC3::GenVertexPtr GenVertexPtr
Definition GenVertex.h:23
HepMC3::GenEvent GenEvent
Definition GenEvent.h:39
STL namespace.