BOSS 7.0.7
BESIII Offline Software System
Loading...
Searching...
No Matches
SingleParticleGun.cxx
Go to the documentation of this file.
1// ------------------------------------------
2// File: SingleParticleGun/SingleParticleGun.cpp
3// Description:
4// Allows the user to "shoot" Monte Carlo particles and store the result
5// in the Transient Store.
6// -------------------------------------------------------------
7
10
11// Framework Related Headers:-
12#include "GaudiKernel/MsgStream.h"
13
14// Other classes used by this class:-
15#include <math.h>
16#include "CLHEP/Units/PhysicalConstants.h"
17
18#include "CLHEP/Random/RandFlat.h"
19#include "CLHEP/Random/RandGauss.h"
21
22#include "HepPDT/ParticleDataTable.hh"
23#include "HepPDT/ParticleData.hh"
24#ifndef ENABLE_BACKWARDS_COMPATIBILITY
25typedef double HepDouble;
26#endif
27using HepMC::GenVertex;
28using HepMC::GenParticle;
29
30// File scope declarations:-
31
32//--------------------------------------------------------------------------
33SingleParticleGun::SingleParticleGun(const std::string& name, ISvcLocator* pSvcLocator)
34 : GenModule(name,pSvcLocator)
35{
36//--------------------------------------------------------------------------
37 declareProperty("Mode",m_Emode = SingleEnergyMode::PtMode);
38 // in PtMode user specifies Pt theta and phi
39 // in EMode user specifies E, theta and phi
40 // defaults are set so that something is always generated
41
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);
46
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);
51
52 declareProperty("MinPt",m_minPt = 1.);
53 declareProperty("MinE",m_minE = 1.);
54 declareProperty("MinTheta",m_minTheta = 0.);
55 declareProperty("MinPhi",m_minPhi = 0.);
56
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);
61
62 declareProperty("SigmaPt",m_sigmaPt = 0.1);
63 declareProperty("SigmaE",m_sigmaE = 0.1);
64
65 declareProperty("SigmaTheta",m_sigmaTheta= 0.1);
66 declareProperty("SigmaPhi",m_sigmaPhi = 0.1);
67
68 declareProperty("ModePt",m_PtGenMode = SingleParticleGunGenMode::FixedMode);
69 declareProperty("ModeE",m_EGenMode = SingleParticleGunGenMode::FixedMode);
70 declareProperty("ModeTheta",m_ThetaGenMode=SingleParticleGunGenMode::FixedMode);
71
72 declareProperty("ModePhi",m_PhiGenMode=SingleParticleGunGenMode::FixedMode);
73 // specifies type of distribution
74
75 declareProperty("PdgCode",m_pdgCode=211);
76}
77
78//--------------------------------------------------------------------------
80//--------------------------------------------------------------------------
81// if(m_pFlatGenerator) delete m_pFlatGenerator;
82// if(m_pGaussGenerator) delete m_pGaussGenerator;
83}
84
85//---------------------------------------------------------------------------
87//---------------------------------------------------------------------------
88
89 // Create the flat and gaussian generators
90 //
91
92 MsgStream log(messageService(), name());
93
94 static const bool CREATEIFNOTTHERE(true);
95 StatusCode RndmStatus = service("BesRndmGenSvc", p_BesRndmGenSvc, CREATEIFNOTTHERE);
96 if (!RndmStatus.isSuccess() || 0 == p_BesRndmGenSvc)
97 {
98 log << MSG::ERROR << " Could not initialize Random Number Service" << endreq;
99 return RndmStatus;
100 }
101
102 // Get the mass of the particle to be generated
103 //
104 const HepPDT::ParticleData* particle = m_particleTable->particle(HepPDT::ParticleID(abs(m_pdgCode)));
105 m_mass = particle->mass().value();
106
107 //
108 // Make sure the parameters are in a sensible range...
109 //
110 switch (m_Emode){
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;
117 }
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;
123 }
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;
129 }
130 break;
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;
137 }
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;
143 }
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;
149 }break;
150 }
151 m_events = 0;
152 return StatusCode::SUCCESS;
153}
154
155//---------------------------------------------------------------------------
157//---------------------------------------------------------------------------
158
159 ++m_events;
160 // Save the random number seeds in the event
161 CLHEP::HepRandomEngine* engine = p_BesRndmGenSvc->GetEngine("SINGLE");
162 const long* s = engine->getSeeds();
163 m_seeds.clear();
164 m_seeds.push_back(s[0]);
165 m_seeds.push_back(s[1]);
166
167 // Generate values for pt, theta and phi
168 //
169 double pt,phi,theta,E,px,py,pz,p;
170 switch(m_Emode){
171
173 pt = generateValue(m_PtGenMode,m_requestedPt, m_sigmaPt,
174 m_minPt, m_maxPt);
175 theta = generateValue(m_ThetaGenMode,m_requestedTheta, m_sigmaTheta,
176 m_minTheta, m_maxTheta);
177 phi = generateValue(m_PhiGenMode,m_requestedPhi, m_sigmaPhi,
178 m_minPhi, m_maxPhi);
179
180 // Transform to x,y,z coordinates
181 //
182 px = pt*cos(phi);
183 py = pt*sin(phi);
184 pz = pt/tan(theta);
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;
188
190 E = generateValue(m_EGenMode,m_requestedE, m_sigmaE,
191 m_minE, m_maxE);
192 theta = generateValue(m_ThetaGenMode,m_requestedTheta, m_sigmaTheta,
193 m_minTheta, m_maxTheta);
194 phi = generateValue(m_PhiGenMode,m_requestedPhi, m_sigmaPhi,
195 m_minPhi, m_maxPhi);
196
197 // Transform to x,y,z coordinates
198 //
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;
203 }
204 p=sqrt(E*E-m_mass*m_mass);
205 px = p*sin(theta)*cos(phi);
206 py = p*sin(theta)*sin(phi);
207 pz = p*cos(theta);
208
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;
212}
213 return StatusCode::SUCCESS;
214}
215
216//---------------------------------------------------------------------------
218//---------------------------------------------------------------------------
219 return StatusCode::SUCCESS;
220}
221
222//---------------------------------------------------------------------------
223StatusCode SingleParticleGun::fillEvt(GenEvent* evt) {
224//---------------------------------------------------------------------------
225 // Note: The vertex is owned by the event so the event is responsible
226 // for deleting its memory
227 evt->set_event_number(m_events); // Set the event number
228 GenVertex* v1 = new GenVertex(m_fourPos);
229
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;
236
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;
241
242 evt->add_vertex( v1 );
243
244
245 v1->add_particle_out( new GenParticle( m_fourMom, m_pdgCode,1) );
246
247 std::cout << " particles_size = " << evt->particles_size()
248 << " vertices_size = " << evt->vertices_size()
249 << std::endl;
250
251 evt->set_signal_process_id(SINGLE);
252 evt->set_random_states(m_seeds);
253
254 return StatusCode::SUCCESS;
255}
256
257//---------------------------------------------------------------------------
258double SingleParticleGun::generateValue(int mode, double val, double sigma,
259 double min, double max) {
260//---------------------------------------------------------------------------
261 double tmp;
262 int i = 0;
263 CLHEP::HepRandomEngine* engine = p_BesRndmGenSvc->GetEngine("SINGLE");
264 const int maxtries = 100;
265 switch (mode) {
267 return val;
269 tmp = max+1.0;
270 i = 0;
271 do{
272// tmp = m_pGaussGenerator->fire((HepDouble) val,(HepDouble) sigma);
273 tmp = CLHEP::RandGauss::shoot(engine, (HepDouble) val,(HepDouble) sigma);
274 i++;
275 } while ( (tmp<min) || (tmp > max) && (i < maxtries));
276 if(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;
280 }
281 return tmp;
283// tmp = m_pFlatGenerator->fire((HepDouble) min,(HepDouble) max);
284 tmp = CLHEP::RandFlat::shoot(engine, (HepDouble) min,(HepDouble) max);
285 return tmp;
286 default:
287 MsgStream log(messageService(), name());
288 log << MSG::ERROR << "Unknown Generation Mode" << endreq;
289 return 0.;
290 }
291}
double tan(const BesAngle a)
Definition: BesAngle.h:216
double sin(const BesAngle a)
Definition: BesAngle.h:210
double cos(const BesAngle a)
Definition: BesAngle.h:213
TTree * sigma
double HepDouble
Definition: EvtVub.cc:37
@ SINGLE
Definition: GeneratorName.h:12
XmlRpcServer s
Definition: HelloServer.cpp:11
double HepDouble
HepPDT::ParticleDataTable * m_particleTable
Definition: GenModule.h:73
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)