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()) == 0 ) {
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] ;
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 ) {
141 HepMC::GenVertex::particle_iterator pitr;
142 for (pitr= (*vitr)->particles_begin(HepMC::children);
143 pitr != (*vitr)->particles_end(HepMC::children); ++pitr) {
146 if (1 == (*pitr)->status()) {
149 <<
"GenVertex barcode = " << (*vitr)->barcode()
150 <<
" selected for GenParticle barcode = " << (*pitr)->barcode();
156 else if (2 == (*pitr)->status()) {
158 if ( (*pitr)->end_vertex() != 0 ) {
159 double xx = (*pitr)->end_vertex()->position().x();
160 double yy = (*pitr)->end_vertex()->position().y();
161 double r_dd = xx*xx+yy*yy;
165 <<
"GenVertex barcode = " << (*vitr)->barcode()
166 <<
" selected for GenParticle barcode = "
167 << (*pitr)->barcode() <<
" radius = " <<
std::sqrt(r_dd);
184 double x1 = (*vitr)->position().x()*mm;
185 double y1 = (*vitr)->position().y()*mm;
186 double z1 = (*vitr)->position().z()*mm;
187 double t1 = (*vitr)->position().t()*mm/c_light;
189 G4PrimaryVertex* g4vtx =
new G4PrimaryVertex(x1, y1, z1, t1);
191 for (pitr= (*vitr)->particles_begin(HepMC::children);
192 pitr != (*vitr)->particles_end(HepMC::children); ++pitr){
201 <<
"Skip GenParticle barcode = " << (*pitr)->barcode()
202 <<
" PDG Id = " << (*pitr)->pdg_id();
210 double decay_length = 0.0;
211 int status = (*pitr)->status();
214 if (1 == status || 2 == status) {
218 if ( !(*pitr)->end_vertex() ) {
221 x2 = (*pitr)->end_vertex()->position().x();
222 y2 = (*pitr)->end_vertex()->position().y();
223 z2 = (*pitr)->end_vertex()->position().z();
225 std::sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1)+(z2-z1)*(z2-z1));
229 bool toBeAdded =
false;
230 double px = (*pitr)->momentum().px();
231 double py = (*pitr)->momentum().py();
232 double pz = (*pitr)->momentum().pz();
233 double ptot =
std::sqrt(px*px + py*py + pz*pz);
242 const double minTan = 1.e-20;
243 if(fabs(z1) <
Z_hector && fabs(pz) >= minTan*ptot) {
244 if(pz > 0.0) { zimpact =
Z_hector; }
246 double del = (zimpact - z1)/pz;
250 double rimpact2 = ximpact*ximpact + yimpact*yimpact;
253 <<
"Processing GenParticle barcode= " << (*pitr)->barcode()
254 <<
" pdg= " << (*pitr)-> pdg_id()
255 <<
" status= " << (*pitr)->status()
257 <<
" rimpact(cm)= " <<
std::sqrt(rimpact2)/cm
258 <<
" zimpact(cm)= " << zimpact/cm
259 <<
" ptot(GeV)= " << ptot
260 <<
" pz(GeV)= " << pz;
268 <<
"GenParticle barcode = " << (*pitr)->barcode()
282 double phi =
p.phi();
297 if(fabs(pz) >= minTan*ptot) {
298 if((zi >=
Z_lmax) & (pz < 0.0)) {
300 }
else if((zi <=
Z_lmin) & (pz > 0.0)) {
303 if(pz > 0) { zi =
Z_lmax; }
306 double del = (zi - z1)/pz;
320 <<
"GenParticle barcode = " << (*pitr)->barcode()
326 }
else if(2 == status &&
331 <<
"GenParticle barcode = " << (*pitr)->barcode()
333 <<
" decay_length(cm)= " << decay_length/cm;
337 G4int pdgcode= (*pitr)-> pdg_id();
338 G4PrimaryParticle* g4prim=
339 new G4PrimaryParticle(pdgcode, px*
GeV, py*GeV, pz*GeV);
341 if ( g4prim->GetG4code() != 0 ){
342 g4prim->SetMass( g4prim->GetG4code()->GetPDGMass() );
343 double charge = g4prim->GetG4code()->GetPDGCharge();
351 g4prim->SetCharge(charge);
357 setGenId( g4prim, (*pitr)->barcode() );
363 if (
verbose > 1 ) g4prim->Print();
364 g4vtx->SetPrimary(g4prim);
368 if (
verbose > 1 ) g4vtx->Print();
369 g4evt->AddPrimaryVertex(g4vtx);
376 G4PrimaryVertex* g4vtx =
new G4PrimaryVertex(0.0, 0.0, 0.0, 0.0);
377 if (
verbose > 1 ) g4vtx->Print();
378 g4evt->AddPrimaryVertex(g4vtx);
390 <<
"Special case of long decay length \n"
391 <<
"Assign daughters with to mother with decaylength="
392 << decaylength/cm <<
" cm";
395 vp->momentum().pz(), vp->momentum().e());
398 double proper_time = decaylength/(
p.Beta()*
p.Gamma()*c_light);
399 g4p->SetProperTime(proper_time);
402 LogDebug(
"SimG4CoreGenerator") <<
" px= "<<
p.px()
406 <<
" beta= "<<
p.Beta()
407 <<
" gamma= " <<
p.Gamma()
408 <<
" Proper time= " <<proper_time/ns <<
" ns";
413 double x1 = vp->end_vertex()->position().x();
414 double y1 = vp->end_vertex()->position().y();
415 double z1 = vp->end_vertex()->position().z();
417 for (HepMC::GenVertex::particle_iterator
418 vpdec= vp->end_vertex()->particles_begin(HepMC::children);
419 vpdec != vp->end_vertex()->particles_end(HepMC::children); ++vpdec) {
423 (*vpdec)->momentum().py(),
424 (*vpdec)->momentum().pz(),
425 (*vpdec)->momentum().e());
428 G4PrimaryParticle * g4daught=
429 new G4PrimaryParticle((*vpdec)->pdg_id(), pdec.x()*
GeV,
430 pdec.y()*
GeV, pdec.z()*
GeV);
432 if ( g4daught->GetG4code() != 0 )
434 g4daught->SetMass( g4daught->GetG4code()->GetPDGMass() ) ;
435 g4daught->SetCharge( g4daught->GetG4code()->GetPDGCharge() ) ;
441 setGenId( g4daught, (*vpdec)->barcode() );
444 <<
"Assigning a "<< (*vpdec)->pdg_id()
445 <<
" as daughter of a " << vp->pdg_id();
446 if ( (*vpdec)->status() == 2 && (*vpdec)->end_vertex() != 0 )
448 double x2 = (*vpdec)->end_vertex()->position().x();
449 double y2 = (*vpdec)->end_vertex()->position().y();
450 double z2 = (*vpdec)->end_vertex()->position().z();
451 double dd =
std::sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)+(z1-z2)*(z1-z2));
454 (*vpdec)->set_status(1000+(*vpdec)->status());
455 g4p->SetDaughter(g4daught);
457 if (
verbose > 1 ) g4daught->Print();
465 double ptot = p.mag();
471 if(ptot < pz + 1.
e-10) {
475 double eta = 0.5*G4Log((ptot + pz)/(ptot - pz));
482 double phi = p.phi();
489 <<
"Generator ptot(GeV)= " << ptot/GeV <<
" eta= " << p.eta()
490 <<
" phi= " << p.phi() <<
" Flag= " <<
flag;
498 for(HepMC::GenEvent::particle_const_iterator it = evt->particles_begin();
499 it != evt->particles_end(); ++it ) {
502 int g_status = gp->status();
505 int g_id = gp->pdg_id();
506 G4PrimaryParticle * g4p =
507 new G4PrimaryParticle(g_id,gp->momentum().px()*
GeV,
508 gp->momentum().py()*
GeV,
509 gp->momentum().pz()*
GeV);
510 if (g4p->GetG4code() != 0) {
511 g4p->SetMass(g4p->GetG4code()->GetPDGMass());
512 g4p->SetCharge(g4p->GetG4code()->GetPDGCharge());
519 G4PrimaryVertex *
v =
520 new G4PrimaryVertex(gp->production_vertex()->position().x()*mm,
521 gp->production_vertex()->position().y()*mm,
522 gp->production_vertex()->position().z()*mm,
523 gp->production_vertex()->position().t()*mm/c_light);
525 g4evt->AddPrimaryVertex(v);
526 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
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