source: trunk/examples/novice/gemc/src/MPrimaryGeneratorAction.cc@ 1190

Last change on this file since 1190 was 807, checked in by garnier, 17 years ago

update

File size: 12.2 KB
RevLine 
[807]1// %%%%%%%%%%
2// G4 headers
3// %%%%%%%%%%
4#include "G4Event.hh"
5#include "G4ParticleGun.hh"
6#include "G4ParticleTable.hh"
7#include "G4ParticleDefinition.hh"
8#include "Randomize.hh"
9#include "G4UnitsTable.hh"
10
11// %%%%%%%%%%%%%
12// gemc headers
13// %%%%%%%%%%%%%
14#include "MPrimaryGeneratorAction.h"
15#include "detector.h"
16
17// %%%%%%%%%%%
18// C++ headers
19// %%%%%%%%%%%
20#include <iostream>
21using namespace std;
22
23MPrimaryGeneratorAction::MPrimaryGeneratorAction(gemc_opts opts)
24{
25 gemcOpt = opts;
26 hd_msg = gemcOpt.args["LOG_MSG"].args + " Beam Settings >> " ;
27 input_gen = gemcOpt.args["INPUT_GEN_FILE"].args;
28 GEN_VERBOSITY = gemcOpt.args["GEN_VERBOSITY"].arg;
29
30 particleTable = G4ParticleTable::GetParticleTable();
31
32 setBeam(gemcOpt);
33
34 particleGun = new G4ParticleGun(1);
35
36 if(input_gen == "gemc_internal")
37 {
38 cout << endl << hd_msg << " Beam Type: " << Particle->GetParticleName() << endl;
39 cout << hd_msg << " Beam Momentum: " << G4BestUnit(mom, "Energy") ;
40 if(dmom > 0) cout << " +- " << G4BestUnit(dmom, "Energy") ;
41 cout << endl;
42 cout << hd_msg << " Beam Direction: (theta, phi) = (" << theta/deg << ", " << phi/deg << ")" ;
43 if(dtheta > 0 || dphi > 0) cout << " +- (" << dtheta/deg << ", " << dphi/deg << ")" ;
44 cout << " deg " << endl;
45 cout << hd_msg << " Beam Vertex: (" << vx/cm << ", " << vy/cm << ", " << vz/cm << ")" ;
46 if(dvx + dvy + dvz > 0) cout << " +- (" << dvx/cm << ", " << dvy/cm << ", " << dvz/cm << ")" ;
47 cout << " cm " << endl;
48 }
49
50
51 if(NP>0)
52 {
53 cout << endl << hd_msg << " Luminosity Particle Type: " << L_Particle->GetParticleName() << endl;
54 cout << hd_msg << " Luminosity Particle Momentum: " << G4BestUnit(L_Mom, "Energy") ;
55 cout << endl;
56 cout << hd_msg << " Luminosity Particle Direction: (theta, phi) = (" << L_Theta/deg << ", " << L_Phi/deg << ")" ;
57 cout << " deg " << endl;
58 cout << hd_msg << " Luminosity Particle Vertex: (" << L_vx/cm << ", " << L_vy/cm << ", " << L_vz/cm << ") cm" << endl;
59 cout << hd_msg << " Number of Luminosity Particles: " << NP << endl;
60 cout << hd_msg << " Luminosity Time Window: " << TWINDOW/ns << " nanoseconds." << endl ;
61 cout << hd_msg << " Luminosity Time Between Bunches: " << TBUNCH/ns << " nanoseconds." << endl;
62 }
63
64}
65
66
67void MPrimaryGeneratorAction::GeneratePrimaries(G4Event* anEvent)
68{
69 // internal generator. Particle defined by command line
70 if(input_gen == "gemc_internal")
71 {
72 // Primary Particle
73 particleGun->SetParticleDefinition(Particle);
74
75 // 4-momenta
76 Mom = mom/MeV + (2.0*G4UniformRand()-1.0)*dmom/MeV;
77 Theta = theta/rad + (2.0*G4UniformRand()-1.0)*dtheta/rad;
78 Phi = phi/rad + (2.0*G4UniformRand()-1.0)*dphi/rad;
79 double mass = Particle->GetPDGMass();
80 double akine = sqrt(Mom*Mom + mass*mass) - mass ;
81 beam_dir = G4ThreeVector(cos(Phi/rad)*sin(Theta/rad), sin(Phi/rad)*sin(Theta/rad), cos(Theta/rad));
82 particleGun->SetParticleEnergy(akine);
83 particleGun->SetParticleMomentumDirection(beam_dir);
84
85 // vertex
86 Vx = vx/mm + (2.0*G4UniformRand()-1.0)*dvx/mm;
87 Vy = vy/mm + (2.0*G4UniformRand()-1.0)*dvy/mm;
88 Vz = vz/mm + (2.0*G4UniformRand()-1.0)*dvz/mm;
89 beam_vrt = G4ThreeVector(Vx, Vy, Vz);
90 particleGun->SetParticlePosition(beam_vrt);
91
92 // Primary particle generated int the middle of Time window
93 particleGun->SetParticleTime(TWINDOW/2);
94 particleGun->GeneratePrimaryVertex(anEvent);
95 }
96 else
97 // external generator: input file
98 {
99 // LUND format
100 if(gformat == "LUND" && !gif.eof())
101 {
102 double tmp, px, py, pz;
103 int NPART, pdef;
104 gif >> NPART >> tmp >> tmp >> tmp >> tmp >> tmp >> tmp >> tmp >> tmp >> tmp;
105 for(int p=0; p<NPART; p++)
106 {
107 gif >> tmp >> tmp >> tmp >> pdef >> tmp >> tmp >> px >> py >> pz >> tmp >> tmp >> Vx >> Vy >> Vz;
108
109 // Primary Particle
110 Particle = particleTable->FindParticle(pdef);
111 particleGun->SetParticleDefinition(Particle);
112
113 // 4-momenta
114 G4ThreeVector pmom(px*GeV, py*GeV, pz*GeV);
115 Mom = pmom.mag();
116 Phi = pmom.getPhi();
117 Theta = pmom.getTheta();
118 double mass = Particle->GetPDGMass();
119 double akine = sqrt(Mom*Mom + mass*mass) - mass ;
120
121 beam_dir = G4ThreeVector(cos(Phi/rad)*sin(Theta/rad), sin(Phi/rad)*sin(Theta/rad), cos(Theta/rad));
122 particleGun->SetParticleEnergy(akine);
123 particleGun->SetParticleMomentumDirection(beam_dir);
124
125 // vertex
126 beam_vrt = G4ThreeVector(Vx*cm, Vy*cm, Vz*cm);
127 particleGun->SetParticlePosition(beam_vrt);
128
129 // Primary particle generated int the middle of Time window
130 particleGun->SetParticleTime(TWINDOW/2);
131 particleGun->GeneratePrimaryVertex(anEvent);
132 if(GEN_VERBOSITY > 3)
133 cout << hd_msg << " Particle Number: " << p << ", id=" << pdef
134 << " Vertex=" << beam_vrt << "cm, momentum=" << pmom/GeV << " GeV" << endl;
135 }
136 }
137 }
138
139
140
141 // Luminosity Particles
142 int NBUNCHES = (int) floor(TWINDOW/TBUNCH);
143 int PBUNCH = (int) floor((double)NP/NBUNCHES);
144
145 particleGun->SetParticleDefinition(L_Particle);
146 particleGun->SetParticlePosition(L_beam_vrt);
147 double L_mass = L_Particle->GetPDGMass();
148 double L_akine = sqrt(L_Mom*L_Mom + L_mass*L_mass) - L_mass ;
149
150 particleGun->SetParticleEnergy(L_akine);
151 particleGun->SetParticleMomentumDirection(L_beam_dir);
152 for(int b=0; b<NBUNCHES; b++)
153 {
154 for(int p=0; p<PBUNCH; p++)
155 {
156 particleGun-> SetParticleTime(TBUNCH*b);
157 particleGun->GeneratePrimaryVertex(anEvent);
158 }
159 }
160
161}
162
163
164
165void MPrimaryGeneratorAction::setBeam(gemc_opts)
166{
167 string hd_msg = gemcOpt.args["LOG_MSG"].args + " Beam Settings >> " ;
168
169 if(input_gen == "gemc_internal")
170 {
171
172 // %%%%%%%%%%%%
173 // Primary Beam
174 // %%%%%%%%%%%%
175 string beam = gemcOpt.args["BEAM_P"].args;
176 string vert = gemcOpt.args["BEAM_V"].args;
177 string spread_b = gemcOpt.args["SPREAD_P"].args;
178 string spread_v = gemcOpt.args["SPREAD_V"].args;
179
180 string blockstr;
181 int ppos = beam.find(",");
182 blockstr.assign(beam, 0, ppos);
183
184 Particle = particleTable->FindParticle(blockstr);
185
186 if(!Particle)
187 {
188 if(blockstr == "show_all")
189 {
190 for(int i=0; i<particleTable->entries(); i++)
191 cout << hd_msg << " g4 particle: " << particleTable->GetParticleName(i) << endl;;
192 }
193 else
194 cout << hd_msg << " Particle " << blockstr << " not found in G4 table. Exiting" << endl << endl;
195
196 exit(0);
197 }
198
199 // %%%%%%%%
200 // Momentum
201 // %%%%%%%%
202 // energy
203 beam.assign(beam, ppos+1, beam.size());
204 ppos = beam.find(",");
205 blockstr.assign(beam, 0, ppos);
206 mom = get_number(blockstr);
207 // theta angle
208 beam.assign(beam, ppos+1, beam.size());
209 ppos = beam.find(",");
210 blockstr.assign(beam, 0, ppos);
211 theta = get_number(blockstr);
212 // phi angle
213 beam.assign(beam, ppos+1, beam.size());
214 ppos = beam.find("\"");
215 blockstr.assign(beam, 0, ppos);
216 phi = get_number(blockstr);
217
218 // %%%%%%
219 // Spread
220 // %%%%%%
221 // delta energy
222 ppos = spread_b.find(",");
223 blockstr.assign(spread_b, 0, ppos);
224 dmom = get_number(blockstr);
225 // delta theta angle
226 spread_b.assign(spread_b, ppos+1, spread_b.size());
227 ppos = spread_b.find(",");
228 blockstr.assign(spread_b, 0, ppos);
229 dtheta = get_number(blockstr);
230 // delta phi angle
231 spread_b.assign(spread_b, ppos+1, spread_b.size());
232 ppos = spread_b.find(",");
233 blockstr.assign(spread_b, 0, ppos);
234 dphi = get_number(blockstr);
235
236
237 // %%%%%%
238 // Vertex
239 // %%%%%%
240 // units
241 ppos = vert.find(")");
242 string units;
243 units.assign(vert, ppos+1, vert.size() - ppos);
244 // vx
245 ppos = vert.find("(");
246 vert.assign(vert, ppos+1, vert.size());
247 ppos = vert.find(",");
248 blockstr.assign(vert, 0, ppos);
249 vx = get_number(blockstr + "*" + units);
250 // vy
251 ppos = vert.find(",");
252 vert.assign(vert, ppos+1, vert.size());
253 ppos = vert.find(",");
254 blockstr.assign(vert, 0, ppos);
255 vy = get_number(blockstr + "*" + units);
256 // vz
257 ppos = vert.find(",");
258 vert.assign(vert, ppos+1, vert.size());
259 ppos = vert.find(",");
260 blockstr.assign(vert, 0, ppos);
261 vz = get_number(blockstr + "*" + units);
262
263
264 // %%%%%%%%%%%%%%%%%%%%%%%%%
265 // Vertex Spread (cartesian)
266 // %%%%%%%%%%%%%%%%%%%%%%%%%
267 // units
268 ppos = spread_v.find(")");
269 units.assign(spread_v, ppos+1, spread_v.size() - ppos);
270 // dvx
271 ppos = spread_v.find("(");
272 spread_v.assign(spread_v, ppos+1, spread_v.size());
273 ppos = spread_v.find(",");
274 blockstr.assign(spread_v, 0, ppos);
275 dvx = get_number(blockstr + "*" + units);
276 // dvy
277 ppos = spread_v.find(",");
278 spread_v.assign(spread_v, ppos+1, spread_v.size());
279 ppos = spread_v.find(",");
280 blockstr.assign(spread_v, 0, ppos);
281 dvy = get_number(blockstr + "*" + units);
282 // dvz
283 ppos = spread_v.find(",");
284 spread_v.assign(spread_v, ppos+1, spread_v.size());
285 ppos = spread_v.find(",");
286 blockstr.assign(spread_v, 0, ppos);
287 dvz = get_number(blockstr + "*" + units);
288 }
289 else
290 {
291 gformat.assign( input_gen, 0, input_gen.find(",")) ;
292 gfilename.assign(input_gen, input_gen.find(",") + 1, input_gen.size()) ;
293 cout << hd_msg << " Opening " << gformat << " file: " << TrimSpaces(gfilename).c_str() << endl;
294 gif.open(TrimSpaces(gfilename).c_str());
295
296
297 if(!gif)
298 {
299 cerr << hd_msg << " Can't open output file " << TrimSpaces(gfilename).c_str() << ". Exiting. " << endl;
300 exit(1);
301 }
302 }
303
304
305 // %%%%%%%%%%%%%%%
306 // Luminosity Beam
307 // %%%%%%%%%%%%%%%
308 string L_beam = gemcOpt.args["LUMI_P"].args;
309 string L_vert = gemcOpt.args["LUMI_V"].args;
310
311 string L_blockstr;
312 int L_ppos = L_beam.find(",");
313 L_blockstr.assign(L_beam, 0, L_ppos);
314
315 L_Particle = particleTable->FindParticle(L_blockstr);
316
317 if(!L_Particle)
318 {
319 if(L_blockstr == "show_all")
320 {
321 for(int i=0; i<particleTable->entries(); i++)
322 cout << hd_msg << " g4 particle: " << particleTable->GetParticleName(i) << endl;
323 }
324 else
325 cout << hd_msg << " Particle " << L_blockstr << " not found in G4 table. Exiting" << endl << endl;
326
327 exit(0);
328 }
329
330 // %%%%%%%%%%%%%%%%%%%
331 // Luminosity Momentum
332 // %%%%%%%%%%%%%%%%%%%
333 // energy
334 L_beam.assign(L_beam, L_ppos+1, L_beam.size());
335 L_ppos = L_beam.find(",");
336 L_blockstr.assign(L_beam, 0, L_ppos);
337 L_Mom = get_number(L_blockstr);
338 // theta angle
339 L_beam.assign(L_beam, L_ppos+1, L_beam.size());
340 L_ppos = L_beam.find(",");
341 L_blockstr.assign(L_beam, 0, L_ppos);
342 L_Theta = get_number(L_blockstr);
343 // phi angle
344 L_beam.assign(L_beam, L_ppos+1, L_beam.size());
345 L_ppos = L_beam.find("\"");
346 L_blockstr.assign(L_beam, 0, L_ppos);
347 L_Phi = get_number(L_blockstr);
348
349 L_beam_dir = G4ThreeVector(cos(L_Phi/rad)*sin(L_Theta/rad), sin(L_Phi/rad)*sin(L_Theta/rad), cos(L_Theta/rad));
350
351 // %%%%%%%%%%%%%%%%%
352 // Luminosity Vertex
353 // %%%%%%%%%%%%%%%%%
354 // units
355 L_ppos = L_vert.find(")");
356 string L_units;
357 L_units.assign(L_vert, L_ppos+1, L_vert.size() - L_ppos);
358 // vx
359 L_ppos = L_vert.find("(");
360 L_vert.assign(L_vert, L_ppos+1, L_vert.size());
361 L_ppos = L_vert.find(",");
362 L_blockstr.assign(L_vert, 0, L_ppos);
363 L_vx = get_number(L_blockstr + "*" + L_units);
364 // vy
365 L_ppos = L_vert.find(",");
366 L_vert.assign(L_vert, L_ppos+1, L_vert.size());
367 L_ppos = L_vert.find(",");
368 L_blockstr.assign(L_vert, 0, L_ppos);
369 L_vy = get_number(L_blockstr + "*" + L_units);
370 // vz
371 L_ppos = L_vert.find(",");
372 L_vert.assign(L_vert, L_ppos+1, L_vert.size());
373 L_ppos = L_vert.find(",");
374 L_blockstr.assign(L_vert, 0, L_ppos);
375 L_vz = get_number(L_blockstr + "*" + L_units);
376
377 L_beam_vrt = G4ThreeVector(L_vx, L_vy, L_vz);
378
379 // %%%%%%%%%%%%%%%%%%%%%
380 // Luminosity Parameters
381 // %%%%%%%%%%%%%%%%%%%%%
382 string L_pars = gemcOpt.args["LUMI_EVENT"].args;
383 // NP
384 L_ppos = L_pars.find(",");
385 L_blockstr.assign(L_pars, 0, L_ppos);
386 NP = atoi(L_blockstr.c_str());
387 // TWINDOW
388 L_ppos = L_pars.find(",");
389 L_pars.assign(L_pars, L_ppos+1, L_pars.size());
390 L_ppos = L_pars.find(",");
391 L_blockstr.assign(L_pars, 0, L_ppos);
392 TWINDOW = get_number(L_blockstr);
393 // TBUNCH
394 L_ppos = L_pars.find(",");
395 L_pars.assign(L_pars, L_ppos+1, L_pars.size());
396 L_ppos = L_pars.find(",");
397 L_blockstr.assign(L_pars, 0, L_ppos-1);
398 TBUNCH = get_number(L_blockstr);
399
400}
401
402
403MPrimaryGeneratorAction::~MPrimaryGeneratorAction()
404{
405 delete particleGun;
406 gif.close();
407}
408
409
410
411
412
413
414
415
416
417
Note: See TracBrowser for help on using the repository browser.