9 #include "HepPDT/ParticleID.hh"
12 #include "G4HEPEvtParticle.hh"
14 #include "G4ParticleDefinition.hh"
15 #include "G4PhysicalConstants.hh"
16 #include "G4SystemOfUnits.hh"
17 #include "G4UnitsTable.hh"
24 : fPCuts(
p.getParameter<
bool>(
"ApplyPCuts")),
25 fPtransCut(
p.getParameter<
bool>(
"ApplyPtransCut")),
26 fEtaCuts(
p.getParameter<
bool>(
"ApplyEtaCuts")),
27 fPhiCuts(
p.getParameter<
bool>(
"ApplyPhiCuts")),
28 theMinPhiCut(
p.getParameter<double>(
"MinPhiCut")),
29 theMaxPhiCut(
p.getParameter<double>(
"MaxPhiCut")),
30 theMinEtaCut(
p.getParameter<double>(
"MinEtaCut")),
31 theMaxEtaCut(
p.getParameter<double>(
"MaxEtaCut")),
32 theMinPCut(
p.getParameter<double>(
"MinPCut")),
33 theMaxPCut(
p.getParameter<double>(
"MaxPCut")),
34 theEtaCutForHector(
p.getParameter<double>(
"EtaCutForHector")),
35 verbose(
p.getUntrackedParameter<
int>(
"Verbosity", 0)),
45 bool lumi =
p.getParameter<
bool>(
"ApplyLumiMonitorCuts");
50 double theRDecLenCut =
p.getParameter<
double>(
"RDecLenCut") * CLHEP::cm;
55 double theDecLenCut =
p.getParameter<
double>(
"LDecLenCut") * CLHEP::cm;
62 if (
p.exists(
"PDGselection")) {
68 ss <<
"SimG4Core/Generator: ";
70 ss <<
" Selecting only PDG ID = ";
72 ss <<
" Filtering out PDG ID = ";
88 edm::LogVerbatim(
"SimG4CoreGenerator") <<
"SimG4Core/Generator: Rdecaycut= " << theRDecLenCut / CLHEP::cm
90 <<
"Z_min= " <<
Z_lmin / CLHEP::cm <<
" cm; Z_max= " <<
Z_lmax / CLHEP::cm
94 <<
" Z_hector = " <<
Z_hector / CLHEP::cm <<
" cm\n"
98 <<
" LumiMonitorCut: " <<
lumi;
115 if (*(evt->vertices_begin()) ==
nullptr) {
116 std::stringstream
ss;
117 ss <<
"SimG4Core/Generator: in event " << g4evt->GetEventID() <<
" Corrupted Event - GenEvent with no vertex \n";
121 if (!evt->weights().empty()) {
123 for (
unsigned int iw = 1; iw < evt->weights().size(); ++iw) {
125 if (evt->weights()[iw] <= 0)
131 if (
vtx_ !=
nullptr) {
135 (*(evt->vertices_begin()))->position().y(),
136 (*(evt->vertices_begin()))->position().z(),
137 (*(evt->vertices_begin()))->position().t());
142 unsigned int ng4vtx = 0;
143 unsigned int ng4par = 0;
145 for (HepMC::GenEvent::vertex_const_iterator vitr = evt->vertices_begin(); vitr != evt->vertices_end(); ++vitr) {
151 HepMC::GenVertex::particle_iterator pitr;
156 int status = (*pitr)->status();
157 int pdg = (*pitr)->pdg_id();
176 LogDebug(
"SimG4CoreGenerator") <<
"GenVertex barcode = " << (*vitr)->barcode()
177 <<
" selected for GenParticle barcode = " << (*pitr)->barcode();
184 if ((*pitr)->end_vertex() !=
nullptr) {
185 double xx = (*pitr)->end_vertex()->position().x();
186 double yy = (*pitr)->end_vertex()->position().y();
192 <<
"GenVertex barcode = " << (*vitr)->barcode()
193 <<
" selected for GenParticle barcode = " << (*pitr)->barcode() <<
" radius = " <<
std::sqrt(r_dd);
211 double x1 = (*vitr)->position().x() * CLHEP::mm;
212 double y1 = (*vitr)->position().y() * CLHEP::mm;
213 double z1 = (*vitr)->position().z() * CLHEP::mm;
214 double t1 = (*vitr)->position().t() * CLHEP::mm / CLHEP::c_light;
216 G4PrimaryVertex *g4vtx =
new G4PrimaryVertex(
x1,
y1, z1,
t1);
219 int status = (*pitr)->status();
220 int pdg = (*pitr)->pdg_id();
221 bool hasDecayVertex = (
nullptr != (*pitr)->end_vertex());
229 <<
" Skiped GenParticle barcode= " << (*pitr)->barcode() <<
" PDGid= " <<
pdg <<
" status= " <<
status
231 <<
" isInTheList: " << isInTheList <<
" hasDecayVertex: " << hasDecayVertex;
238 <<
"Generator: pdg= " <<
pdg <<
" status= " <<
status <<
" hasPreDefinedDecay: " << hasDecayVertex
245 status = hasDecayVertex ? 2 : 1;
249 if (2 ==
status && !hasDecayVertex) {
251 << g4evt->GetEventID() <<
" a particle "
252 <<
" pdgid= " <<
pdg <<
" has status=2 but has no decay vertex, so will be fully tracked by Geant4";
259 double decay_length = 0.0;
261 x2 = (*pitr)->end_vertex()->position().x();
262 y2 = (*pitr)->end_vertex()->position().y();
263 z2 = (*pitr)->end_vertex()->position().z();
269 double px = (*pitr)->momentum().px();
270 double py = (*pitr)->momentum().py();
271 double pz = (*pitr)->momentum().pz();
281 const double minTan = 1.e-20;
284 double del = (zimpact - z1) / pz;
288 double rimpact2 = ximpact * ximpact + yimpact * yimpact;
291 LogDebug(
"SimG4CoreGenerator") <<
"Processing GenParticle barcode= " << (*pitr)->barcode() <<
" pdg= " <<
pdg
292 <<
" status= " << (*pitr)->status() <<
" st= " <<
status
293 <<
" rimpact(cm)= " <<
std::sqrt(rimpact2) / cm
294 <<
" zimpact(cm)= " << zimpact / cm <<
" ptot(GeV)= " << ptot
295 <<
" pz(GeV)= " << pz;
304 <<
"GenParticle barcode = " << (*pitr)->barcode() <<
" very forward; to be added: " << toBeAdded;
315 double phi =
p.phi();
329 if (
std::abs(pz) >= minTan * ptot) {
330 if ((zi >=
Z_lmax) & (pz < 0.0)) {
332 }
else if ((zi <=
Z_lmin) & (pz > 0.0)) {
341 double del = (zi - z1) / pz;
356 <<
"GenParticle barcode = " << (*pitr)->barcode() <<
" passed case 1";
364 edm::LogVerbatim(
"SimG4CoreGenerator") <<
"GenParticle barcode = " << (*pitr)->barcode() <<
" passed case 2"
365 <<
" decay_length(cm)= " << decay_length / CLHEP::cm;
369 G4PrimaryParticle *g4prim =
new G4PrimaryParticle(
pdg,
px *
GeV,
py *
GeV, pz *
GeV);
371 if (g4prim->GetG4code() !=
nullptr) {
372 g4prim->SetMass(g4prim->GetG4code()->GetPDGMass());
373 double charge = g4prim->GetG4code()->GetPDGCharge();
380 g4prim->SetCharge(
charge);
386 setGenId(g4prim, (*pitr)->barcode());
395 g4vtx->SetPrimary(g4prim);
396 edm::LogVerbatim(
"SimG4CoreGenerator") <<
" " << ng4par <<
". new Geant4 particle pdg= " <<
pdg
398 <<
" status= " <<
status <<
"; dir= " << g4prim->GetMomentumDirection();
404 g4evt->AddPrimaryVertex(g4vtx);
411 G4PrimaryVertex *g4vtx =
new G4PrimaryVertex(0.0, 0.0, 0.0, 0.0);
415 g4evt->AddPrimaryVertex(g4vtx);
418 edm::LogVerbatim(
"SimG4CoreGenerator") <<
"The list of Geant4 primaries includes " << ng4par <<
" particles in "
419 << ng4vtx <<
" vertex";
425 LogDebug(
"SimG4CoreGenerator") <<
"Special case of long decay length \n"
426 <<
"Assign daughters with to mother with decaylength=" << decaylength / cm <<
" cm";
431 double proper_time = decaylength / (
p.Beta() *
p.Gamma() * c_light);
432 g4p->SetProperTime(proper_time);
435 LogDebug(
"SimG4CoreGenerator") <<
" px= " <<
p.px() <<
" py= " <<
p.py() <<
" pz= " <<
p.pz() <<
" e= " <<
p.e()
436 <<
" beta= " <<
p.Beta() <<
" gamma= " <<
p.Gamma()
437 <<
" Proper time= " << proper_time / ns <<
" ns";
442 double x1 = vp->end_vertex()->position().x();
443 double y1 = vp->end_vertex()->position().y();
444 double z1 = vp->end_vertex()->position().z();
446 for (HepMC::GenVertex::particle_iterator vpdec = vp->end_vertex()->particles_begin(
HepMC::children);
451 (*vpdec)->momentum().px(), (*vpdec)->momentum().py(), (*vpdec)->momentum().pz(), (*vpdec)->momentum().e());
454 G4PrimaryParticle *g4daught =
457 if (g4daught->GetG4code() !=
nullptr) {
458 g4daught->SetMass(g4daught->GetG4code()->GetPDGMass());
459 g4daught->SetCharge(g4daught->GetG4code()->GetPDGCharge());
464 setGenId(g4daught, (*vpdec)->barcode());
467 LogDebug(
"SimG4CoreGenerator") <<
"Assigning a " << (*vpdec)->pdg_id() <<
" as daughter of a " << vp->pdg_id();
469 if ((*vpdec)->status() == 2 && (*vpdec)->end_vertex() !=
nullptr) {
470 double x2 = (*vpdec)->end_vertex()->position().x();
471 double y2 = (*vpdec)->end_vertex()->position().y();
472 double z2 = (*vpdec)->end_vertex()->position().z();
476 (*vpdec)->set_status(1000 + (*vpdec)->status());
477 g4p->SetDaughter(g4daught);
487 double ptot =
p.mag();
493 if (ptot < pz + 1.
e-10) {
497 double eta = 0.5 * G4Log((ptot + pz) / (ptot - pz));
504 double phi =
p.phi();
511 LogDebug(
"SimG4CoreGenerator") <<
"Generator ptot(GeV)= " << ptot /
GeV <<
" eta= " <<
p.eta()
512 <<
" phi= " <<
p.phi() <<
" Flag= " <<
flag;
528 int charge = pid.threeCharge();
543 int i = g4evt->GetNumberOfPrimaryVertex();
544 for (HepMC::GenEvent::particle_const_iterator it = evt->particles_begin(); it != evt->particles_end(); ++it) {
549 if (
gp->status() != 1)
552 int pdg =
gp->pdg_id();
553 G4PrimaryParticle *g4p =
new G4PrimaryParticle(
555 if (g4p->GetG4code() !=
nullptr) {
556 g4p->SetMass(g4p->GetG4code()->GetPDGMass());
557 g4p->SetCharge(g4p->GetG4code()->GetPDGCharge());
560 G4PrimaryVertex *
v =
new G4PrimaryVertex(
gp->production_vertex()->position().x() * CLHEP::mm,
561 gp->production_vertex()->position().y() * CLHEP::mm,
562 gp->production_vertex()->position().z() * CLHEP::mm,
563 gp->production_vertex()->position().t() * CLHEP::mm / CLHEP::c_light);
565 g4evt->AddPrimaryVertex(
v);