12#include "GaudiKernel/MsgStream.h"
16#include "CLHEP/Units/PhysicalConstants.h"
18#include "CLHEP/Random/RandFlat.h"
19#include "CLHEP/Random/RandGauss.h"
22#include "HepPDT/ParticleDataTable.hh"
23#include "HepPDT/ParticleData.hh"
24#ifndef ENABLE_BACKWARDS_COMPATIBILITY
27using HepMC::GenVertex;
28using HepMC::GenParticle;
42 declareProperty(
"Pt",m_requestedPt = 5.0);
43 declareProperty(
"E",m_requestedE = 5.0);
44 declareProperty(
"Phi",m_requestedPhi = 0.0);
45 declareProperty(
"Theta",m_requestedTheta = 3.14/4.0);
47 declareProperty(
"VertexX",m_requestedX = 0.0);
48 declareProperty(
"VertexY",m_requestedY = 0.0);
49 declareProperty(
"VertexZ",m_requestedZ = 0.0);
50 declareProperty(
"Time",m_requestedT = 0.0);
52 declareProperty(
"MinPt",m_minPt = 1.);
53 declareProperty(
"MinE",m_minE = 1.);
54 declareProperty(
"MinTheta",m_minTheta = 0.);
55 declareProperty(
"MinPhi",m_minPhi = 0.);
57 declareProperty(
"MaxPt",m_maxPt = 100.);
58 declareProperty(
"MaxE",m_maxE = 100.);
59 declareProperty(
"MaxTheta",m_maxTheta=CLHEP::pi);
60 declareProperty(
"MaxPhi",m_maxPhi = CLHEP::twopi);
62 declareProperty(
"SigmaPt",m_sigmaPt = 0.1);
63 declareProperty(
"SigmaE",m_sigmaE = 0.1);
65 declareProperty(
"SigmaTheta",m_sigmaTheta= 0.1);
66 declareProperty(
"SigmaPhi",m_sigmaPhi = 0.1);
75 declareProperty(
"PdgCode",m_pdgCode=211);
92 MsgStream log(messageService(), name());
94 static const bool CREATEIFNOTTHERE(
true);
95 StatusCode RndmStatus = service(
"BesRndmGenSvc", p_BesRndmGenSvc, CREATEIFNOTTHERE);
96 if (!RndmStatus.isSuccess() || 0 == p_BesRndmGenSvc)
98 log << MSG::ERROR <<
" Could not initialize Random Number Service" << endreq;
104 const HepPDT::ParticleData* particle =
m_particleTable->particle(HepPDT::ParticleID(
abs(m_pdgCode)));
105 m_mass = particle->mass().value();
112 if(!m_PtGenMode && (m_minPt > m_requestedPt || m_maxPt < m_requestedPt)
113 || m_maxPt < m_minPt ) {
114 log << MSG:: ERROR <<
" Pt min and max out of range. \n" <<
115 " Will set Pt mode to Fixed!!!" << endreq;
118 if(!m_ThetaGenMode && (m_minTheta > m_requestedTheta || m_maxTheta < m_requestedTheta)
119 || m_maxTheta < m_minTheta ) {
120 log << MSG:: ERROR <<
" Theta min and max out of range. \n" <<
121 " Will set Theta mode to Fixed!!!" << endreq;
124 if(!m_PhiGenMode && (m_minPhi > m_requestedPhi || m_maxPhi < m_requestedPhi)
125 || m_maxPhi < m_minPhi ) {
126 log << MSG:: ERROR <<
" Phi min and max out of range. \n" <<
127 " Will set Phi mode to Fixed!!!" << endreq;
132 if(!m_EGenMode && (m_minE > m_requestedE || m_maxE < m_requestedE)
133 || m_maxE < m_minE ) {
134 log << MSG:: ERROR <<
" E min and max out of range. \n" <<
135 " Will set E mode to Fixed!!!" << endreq;
138 if(!m_ThetaGenMode && (m_minTheta > m_requestedTheta || m_maxTheta < m_requestedTheta)
139 || m_maxTheta < m_minTheta ) {
140 log << MSG:: ERROR <<
" Theta min and max out of range. \n" <<
141 " Will set Theta mode to Fixed!!!" << endreq;
144 if(!m_PhiGenMode && (m_minPhi > m_requestedPhi || m_maxPhi < m_requestedPhi)
145 || m_maxPhi < m_minPhi ) {
146 log << MSG:: ERROR <<
" Phi min and max out of range. \n" <<
147 " Will set Phi mode to Fixed!!!" << endreq;
152 return StatusCode::SUCCESS;
161 CLHEP::HepRandomEngine* engine = p_BesRndmGenSvc->
GetEngine(
"SINGLE");
162 const long*
s = engine->getSeeds();
164 m_seeds.push_back(
s[0]);
165 m_seeds.push_back(
s[1]);
169 double pt,phi,theta,E,px,py,pz,p;
173 pt = generateValue(m_PtGenMode,m_requestedPt, m_sigmaPt,
175 theta = generateValue(m_ThetaGenMode,m_requestedTheta, m_sigmaTheta,
176 m_minTheta, m_maxTheta);
177 phi = generateValue(m_PhiGenMode,m_requestedPhi, m_sigmaPhi,
185 m_fourMom.setVectM(CLHEP::Hep3Vector(px,py,pz),m_mass);
186 m_fourPos.set(m_requestedX,m_requestedY,m_requestedZ,m_requestedT);
187 return StatusCode::SUCCESS;
190 E = generateValue(m_EGenMode,m_requestedE, m_sigmaE,
192 theta = generateValue(m_ThetaGenMode,m_requestedTheta, m_sigmaTheta,
193 m_minTheta, m_maxTheta);
194 phi = generateValue(m_PhiGenMode,m_requestedPhi, m_sigmaPhi,
199 if(E*E-m_mass*m_mass < 0.){
200 MsgStream log(messageService(), name());
201 log << MSG::ERROR <<
"You have Generated a Tachyon!! Increase energy or change particle ID" << endreq;
202return StatusCode::FAILURE;
204 p=sqrt(E*E-m_mass*m_mass);
205 px = p*
sin(theta)*
cos(phi);
206 py = p*
sin(theta)*
sin(phi);
209 m_fourMom.setVectM(CLHEP::Hep3Vector(px,py,pz),m_mass);
210 m_fourPos.set(m_requestedX,m_requestedY,m_requestedZ,m_requestedT);
211 return StatusCode::SUCCESS;
213 return StatusCode::SUCCESS;
219 return StatusCode::SUCCESS;
227 evt->set_event_number(m_events);
228 GenVertex* v1 =
new GenVertex(m_fourPos);
230 std::cout <<
" SingleParticleGun::fillEvt --> " <<std::endl;
231 std::cout <<
" Event number:: " << m_events << std::endl;
232 std::cout <<
" vertex.x = " << m_fourPos.x()
233 <<
" vertex.y = " << m_fourPos.y()
234 <<
" vertex.z = " << m_fourPos.z()
235 <<
" vertex.t = " << m_fourPos.t() << std::endl;
237 std::cout <<
" momentum.x = " << m_fourMom.x()
238 <<
" momentum.y = " << m_fourMom.y()
239 <<
" momentum.z = " << m_fourMom.z()
240 <<
" momentum.t = " << m_fourMom.t() << std::endl;
242 evt->add_vertex( v1 );
245 v1->add_particle_out(
new GenParticle( m_fourMom, m_pdgCode,1) );
247 std::cout <<
" particles_size = " << evt->particles_size()
248 <<
" vertices_size = " << evt->vertices_size()
251 evt->set_signal_process_id(
SINGLE);
252 evt->set_random_states(m_seeds);
254 return StatusCode::SUCCESS;
258double SingleParticleGun::generateValue(
int mode,
double val,
double sigma,
263 CLHEP::HepRandomEngine* engine = p_BesRndmGenSvc->
GetEngine(
"SINGLE");
264 const int maxtries = 100;
275 }
while ( (tmp<
min) || (tmp >
max) && (i < maxtries));
277 MsgStream log(messageService(), name());
278 log << MSG::ERROR <<
"Cant generate value in range (min, max) "
279 << val <<
"\t" <<
min <<
"\t" <<
max << endreq;
287 MsgStream log(messageService(), name());
288 log << MSG::ERROR <<
"Unknown Generation Mode" << endreq;
double tan(const BesAngle a)
double sin(const BesAngle a)
double cos(const BesAngle a)
HepPDT::ParticleDataTable * m_particleTable
virtual CLHEP::HepRandomEngine * GetEngine(const std::string &StreamName)=0
Interface to the CLHEP engine.
virtual StatusCode fillEvt(GenEvent *evt)
virtual StatusCode genFinalize()
virtual StatusCode genInitialize()
virtual StatusCode callGenerator()
SingleParticleGun(const std::string &name, ISvcLocator *pSvcLocator)
virtual ~SingleParticleGun()