11 #include "G4HEPEvtParticle.hh"
12 #include "G4ParticleDefinition.hh"
13 #include "G4UnitsTable.hh"
14 #include "G4SystemOfUnits.hh"
15 #include "G4PhysicalConstants.hh"
23 fPCuts(p.getParameter<bool>(
"ApplyPCuts")),
24 fPtransCut(p.getParameter<bool>(
"ApplyPtransCut")),
25 fEtaCuts(p.getParameter<bool>(
"ApplyEtaCuts")),
26 fPhiCuts(p.getParameter<bool>(
"ApplyPhiCuts")),
27 theMinPhiCut(p.getParameter<double>(
"MinPhiCut")),
28 theMaxPhiCut(p.getParameter<double>(
"MaxPhiCut")),
29 theMinEtaCut(p.getParameter<double>(
"MinEtaCut")),
30 theMaxEtaCut(p.getParameter<double>(
"MaxEtaCut")),
31 theMinPCut(p.getParameter<double>(
"MinPCut")),
32 theMaxPCut(p.getParameter<double>(
"MaxPCut")),
33 theEtaCutForHector(p.getParameter<double>(
"EtaCutForHector")),
34 verbose(p.getUntrackedParameter<int>(
"Verbosity",0)),
48 double theRDecLenCut = p.
getParameter<
double>(
"RDecLenCut")*cm;
56 if ( p.
exists(
"PDGselection") ) {
58 getParameter<bool>(
"PDGfilterSel");
60 getParameter<std::vector< int > >(
"PDGfilter");
61 if(0 < pdgFilter.size()) {
63 for (
unsigned int ii = 0;
ii < pdgFilter.size(); ++
ii) {
66 <<
" *** Selecting only PDG ID = " << pdgFilter[
ii];
69 <<
" *** Filtering out PDG ID = " << pdgFilter[
ii];
84 <<
" Rdecaycut= " << theRDecLenCut/cm
85 <<
" cm; Zdecaycut= " << theDecLenCut/cm
87 <<
" cm; Z_hector = " <<
Z_hector <<
" cm\n"
91 <<
" ApplyLumiMonitorCuts: " <<
lumi;
103 HepMC::GenEvent *evt=
new HepMC::GenEvent(*evt_orig);
105 if ( *(evt->vertices_begin()) ==
nullptr ) {
106 throw SimG4Exception(
"SimG4CoreGenerator: Corrupted Event - GenEvent with no vertex");
109 if (evt->weights().size() > 0) {
112 for (
unsigned int iw=1; iw<evt->weights().size(); ++iw) {
115 if ( evt->weights()[iw] <= 0 )
break;
116 weight_ *= evt->weights()[iw] ;
120 if (
vtx_ !=
nullptr) {
delete vtx_; }
122 (*(evt->vertices_begin()))->position().y(),
123 (*(evt->vertices_begin()))->position().z(),
124 (*(evt->vertices_begin()))->position().t());
128 LogDebug(
"SimG4CoreGenerator") <<
"Primary Vertex = ("
134 unsigned int ng4vtx = 0;
136 for(HepMC::GenEvent::vertex_const_iterator vitr= evt->vertices_begin();
137 vitr != evt->vertices_end(); ++vitr ) {
144 HepMC::GenVertex::particle_iterator pitr;
145 for (pitr= (*vitr)->particles_begin(HepMC::children);
146 pitr != (*vitr)->particles_end(HepMC::children); ++pitr) {
152 int status = (*pitr)->status();
153 if (status > 3 &&
isExotic(*pitr)) {
166 <<
"GenVertex barcode = " << (*vitr)->barcode()
167 <<
" selected for GenParticle barcode = " << (*pitr)->barcode();
173 else if (status == 2) {
174 if ( (*pitr)->end_vertex() != 0 ) {
175 double xx = (*pitr)->end_vertex()->position().x();
176 double yy = (*pitr)->end_vertex()->position().y();
177 double r_dd = xx*xx+yy*yy;
181 <<
"GenVertex barcode = " << (*vitr)->barcode()
182 <<
" selected for GenParticle barcode = "
183 << (*pitr)->barcode() <<
" radius = " <<
std::sqrt(r_dd);
200 double x1 = (*vitr)->position().x()*mm;
201 double y1 = (*vitr)->position().y()*mm;
202 double z1 = (*vitr)->position().z()*mm;
203 double t1 = (*vitr)->position().t()*mm/c_light;
205 G4PrimaryVertex* g4vtx =
new G4PrimaryVertex(x1, y1, z1, t1);
207 for (pitr= (*vitr)->particles_begin(HepMC::children);
208 pitr != (*vitr)->particles_end(HepMC::children); ++pitr){
217 <<
"Skip GenParticle barcode = " << (*pitr)->barcode()
218 <<
" PDG Id = " << (*pitr)->pdg_id();
226 double decay_length = 0.0;
227 int status = (*pitr)->status();
229 if (status > 3 &&
isExotic(*pitr)) {
234 if (1 == status || 2 == status) {
238 if ( !(*pitr)->end_vertex() ) {
241 x2 = (*pitr)->end_vertex()->position().x();
242 y2 = (*pitr)->end_vertex()->position().y();
243 z2 = (*pitr)->end_vertex()->position().z();
245 std::sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1)+(z2-z1)*(z2-z1));
249 bool toBeAdded =
false;
250 double px = (*pitr)->momentum().px();
251 double py = (*pitr)->momentum().py();
252 double pz = (*pitr)->momentum().pz();
253 double ptot =
std::sqrt(px*px + py*py + pz*pz);
262 const double minTan = 1.e-20;
263 if(fabs(z1) <
Z_hector && fabs(pz) >= minTan*ptot) {
264 if(pz > 0.0) { zimpact =
Z_hector; }
266 double del = (zimpact - z1)/pz;
270 double rimpact2 = ximpact*ximpact + yimpact*yimpact;
273 <<
"Processing GenParticle barcode= " << (*pitr)->barcode()
274 <<
" pdg= " << (*pitr)-> pdg_id()
275 <<
" status= " << (*pitr)->status()
277 <<
" rimpact(cm)= " <<
std::sqrt(rimpact2)/cm
278 <<
" zimpact(cm)= " << zimpact/cm
279 <<
" ptot(GeV)= " << ptot
280 <<
" pz(GeV)= " << pz;
288 <<
"GenParticle barcode = " << (*pitr)->barcode()
302 double phi =
p.phi();
317 if(fabs(pz) >= minTan*ptot) {
318 if((zi >=
Z_lmax) & (pz < 0.0)) {
320 }
else if((zi <=
Z_lmin) & (pz > 0.0)) {
323 if(pz > 0) { zi =
Z_lmax; }
326 double del = (zi - z1)/pz;
340 <<
"GenParticle barcode = " << (*pitr)->barcode()
346 }
else if(2 == status &&
351 <<
"GenParticle barcode = " << (*pitr)->barcode()
353 <<
" decay_length(cm)= " << decay_length/cm;
357 G4int pdgcode= (*pitr)-> pdg_id();
358 G4PrimaryParticle* g4prim=
359 new G4PrimaryParticle(pdgcode, px*
GeV, py*GeV, pz*GeV);
361 if ( g4prim->GetG4code() !=
nullptr ){
362 g4prim->SetMass( g4prim->GetG4code()->GetPDGMass() );
363 double charge = g4prim->GetG4code()->GetPDGCharge();
371 g4prim->SetCharge(charge);
377 setGenId( g4prim, (*pitr)->barcode() );
383 if (
verbose > 1 ) g4prim->Print();
384 g4vtx->SetPrimary(g4prim);
388 if (
verbose > 1 ) g4vtx->Print();
389 g4evt->AddPrimaryVertex(g4vtx);
396 G4PrimaryVertex* g4vtx =
new G4PrimaryVertex(0.0, 0.0, 0.0, 0.0);
397 if (
verbose > 1 ) g4vtx->Print();
398 g4evt->AddPrimaryVertex(g4vtx);
410 <<
"Special case of long decay length \n"
411 <<
"Assign daughters with to mother with decaylength="
412 << decaylength/cm <<
" cm";
415 vp->momentum().pz(), vp->momentum().e());
418 double proper_time = decaylength/(
p.Beta()*
p.Gamma()*c_light);
419 g4p->SetProperTime(proper_time);
422 LogDebug(
"SimG4CoreGenerator") <<
" px= "<<
p.px()
426 <<
" beta= "<<
p.Beta()
427 <<
" gamma= " <<
p.Gamma()
428 <<
" Proper time= " <<proper_time/ns <<
" ns";
433 double x1 = vp->end_vertex()->position().x();
434 double y1 = vp->end_vertex()->position().y();
435 double z1 = vp->end_vertex()->position().z();
437 for (HepMC::GenVertex::particle_iterator
438 vpdec= vp->end_vertex()->particles_begin(HepMC::children);
439 vpdec != vp->end_vertex()->particles_end(HepMC::children); ++vpdec) {
443 (*vpdec)->momentum().py(),
444 (*vpdec)->momentum().pz(),
445 (*vpdec)->momentum().e());
448 G4PrimaryParticle * g4daught=
449 new G4PrimaryParticle((*vpdec)->pdg_id(), pdec.x()*
GeV,
450 pdec.y()*
GeV, pdec.z()*
GeV);
452 if ( g4daught->GetG4code() != 0 )
454 g4daught->SetMass( g4daught->GetG4code()->GetPDGMass() ) ;
455 g4daught->SetCharge( g4daught->GetG4code()->GetPDGCharge() ) ;
460 setGenId( g4daught, (*vpdec)->barcode() );
463 <<
"Assigning a "<< (*vpdec)->pdg_id()
464 <<
" as daughter of a " << vp->pdg_id();
465 if ( (*vpdec)->status() == 2 && (*vpdec)->end_vertex() !=
nullptr)
467 double x2 = (*vpdec)->end_vertex()->position().x();
468 double y2 = (*vpdec)->end_vertex()->position().y();
469 double z2 = (*vpdec)->end_vertex()->position().z();
470 double dd =
std::sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)+(z1-z2)*(z1-z2));
473 (*vpdec)->set_status(1000+(*vpdec)->status());
474 g4p->SetDaughter(g4daught);
476 if (
verbose > 1 ) g4daught->Print();
484 double ptot = p.mag();
490 if(ptot < pz + 1.
e-10) {
494 double eta = 0.5*G4Log((ptot + pz)/(ptot - pz));
501 double phi = p.phi();
508 <<
"Generator ptot(GeV)= " << ptot/GeV <<
" eta= " << p.eta()
509 <<
" phi= " << p.phi() <<
" Flag= " << flag;
516 int pdgid =
abs(p->pdg_id());
517 if ((pdgid >= 1000000 && pdgid < 4000000) ||
532 for(HepMC::GenEvent::particle_const_iterator it = evt->particles_begin();
533 it != evt->particles_end(); ++it ) {
536 int g_status = gp->status();
539 int g_id = gp->pdg_id();
540 G4PrimaryParticle * g4p =
541 new G4PrimaryParticle(g_id,gp->momentum().px()*
GeV,
542 gp->momentum().py()*
GeV,
543 gp->momentum().pz()*
GeV);
544 if (g4p->GetG4code() != 0) {
545 g4p->SetMass(g4p->GetG4code()->GetPDGMass());
546 g4p->SetCharge(g4p->GetG4code()->GetPDGCharge());
550 G4PrimaryVertex *
v =
551 new G4PrimaryVertex(gp->production_vertex()->position().x()*mm,
552 gp->production_vertex()->position().y()*mm,
553 gp->production_vertex()->position().z()*mm,
554 gp->production_vertex()->position().t()*mm/c_light);
556 g4evt->AddPrimaryVertex(v);
557 if(
verbose > 0) { v->Print(); }
T getParameter(std::string const &) const
void setGenId(G4PrimaryParticle *p, int id) const
double theEtaCutForHector
void HepMC2G4(const HepMC::GenEvent *g, G4Event *e)
Generator(const edm::ParameterSet &p)
bool exists(std::string const ¶meterName) const
checks if a parameter exists
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
void particleAssignDaughters(G4PrimaryParticle *p, HepMC::GenParticle *hp, double length)
XYZTLorentzVectorD XYZTLorentzVector
Lorentz vector with cylindrical internal representation using pseudorapidity.
LumiMonitorFilter * fLumiFilter
Abs< T >::type abs(const T &t)
bool isExotic(HepMC::GenParticle *p) const
bool isGoodForLumiMonitor(const HepMC::GenParticle *) const
math::XYZTLorentzVector * vtx_
void nonBeamEvent2G4(const HepMC::GenEvent *g, G4Event *e)
bool particlePassesPrimaryCuts(const G4ThreeVector &p) const
std::vector< int > pdgFilter
volatile std::atomic< bool > shutdown_flag false