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();
157 else if (2 == (*pitr)->status() || 104 == (*pitr)->status()) {
159 if ( (*pitr)->end_vertex() != 0 ) {
160 double xx = (*pitr)->end_vertex()->position().x();
161 double yy = (*pitr)->end_vertex()->position().y();
162 double r_dd = xx*xx+yy*yy;
166 <<
"GenVertex barcode = " << (*vitr)->barcode()
167 <<
" selected for GenParticle barcode = "
168 << (*pitr)->barcode() <<
" radius = " <<
std::sqrt(r_dd);
185 double x1 = (*vitr)->position().x()*mm;
186 double y1 = (*vitr)->position().y()*mm;
187 double z1 = (*vitr)->position().z()*mm;
188 double t1 = (*vitr)->position().t()*mm/c_light;
190 G4PrimaryVertex* g4vtx =
new G4PrimaryVertex(x1, y1, z1, t1);
192 for (pitr= (*vitr)->particles_begin(HepMC::children);
193 pitr != (*vitr)->particles_end(HepMC::children); ++pitr){
202 <<
"Skip GenParticle barcode = " << (*pitr)->barcode()
203 <<
" PDG Id = " << (*pitr)->pdg_id();
211 double decay_length = 0.0;
212 int status = (*pitr)->status();
219 if (1 == status || 2 == status) {
223 if ( !(*pitr)->end_vertex() ) {
226 x2 = (*pitr)->end_vertex()->position().x();
227 y2 = (*pitr)->end_vertex()->position().y();
228 z2 = (*pitr)->end_vertex()->position().z();
230 std::sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1)+(z2-z1)*(z2-z1));
234 bool toBeAdded =
false;
235 double px = (*pitr)->momentum().px();
236 double py = (*pitr)->momentum().py();
237 double pz = (*pitr)->momentum().pz();
238 double ptot =
std::sqrt(px*px + py*py + pz*pz);
247 const double minTan = 1.e-20;
248 if(fabs(z1) <
Z_hector && fabs(pz) >= minTan*ptot) {
249 if(pz > 0.0) { zimpact =
Z_hector; }
251 double del = (zimpact - z1)/pz;
255 double rimpact2 = ximpact*ximpact + yimpact*yimpact;
258 <<
"Processing GenParticle barcode= " << (*pitr)->barcode()
259 <<
" pdg= " << (*pitr)-> pdg_id()
260 <<
" status= " << (*pitr)->status()
262 <<
" rimpact(cm)= " <<
std::sqrt(rimpact2)/cm
263 <<
" zimpact(cm)= " << zimpact/cm
264 <<
" ptot(GeV)= " << ptot
265 <<
" pz(GeV)= " << pz;
273 <<
"GenParticle barcode = " << (*pitr)->barcode()
287 double phi =
p.phi();
302 if(fabs(pz) >= minTan*ptot) {
303 if((zi >=
Z_lmax) & (pz < 0.0)) {
305 }
else if((zi <=
Z_lmin) & (pz > 0.0)) {
308 if(pz > 0) { zi =
Z_lmax; }
311 double del = (zi - z1)/pz;
325 <<
"GenParticle barcode = " << (*pitr)->barcode()
331 }
else if(2 == status &&
336 <<
"GenParticle barcode = " << (*pitr)->barcode()
338 <<
" decay_length(cm)= " << decay_length/cm;
342 G4int pdgcode= (*pitr)-> pdg_id();
343 G4PrimaryParticle* g4prim=
344 new G4PrimaryParticle(pdgcode, px*
GeV, py*GeV, pz*GeV);
346 if ( g4prim->GetG4code() != 0 ){
347 g4prim->SetMass( g4prim->GetG4code()->GetPDGMass() );
348 double charge = g4prim->GetG4code()->GetPDGCharge();
356 g4prim->SetCharge(charge);
362 setGenId( g4prim, (*pitr)->barcode() );
368 if (
verbose > 1 ) g4prim->Print();
369 g4vtx->SetPrimary(g4prim);
373 if (
verbose > 1 ) g4vtx->Print();
374 g4evt->AddPrimaryVertex(g4vtx);
381 G4PrimaryVertex* g4vtx =
new G4PrimaryVertex(0.0, 0.0, 0.0, 0.0);
382 if (
verbose > 1 ) g4vtx->Print();
383 g4evt->AddPrimaryVertex(g4vtx);
387 for(HepMC::GenEvent::vertex_const_iterator vitr= evt->vertices_begin(); vitr != evt->vertices_end(); ++vitr ) {
388 HepMC::GenVertex::particle_iterator pitr;
389 for (pitr= (*vitr)->particles_begin(HepMC::children);
390 pitr != (*vitr)->particles_end(HepMC::children); ++pitr){
391 int status = (*pitr)->status();
393 (*pitr)->set_status(status - 1000);
408 <<
"Special case of long decay length \n"
409 <<
"Assign daughters with to mother with decaylength="
410 << decaylength/cm <<
" cm";
413 vp->momentum().pz(), vp->momentum().e());
416 double proper_time = decaylength/(
p.Beta()*
p.Gamma()*c_light);
417 g4p->SetProperTime(proper_time);
420 LogDebug(
"SimG4CoreGenerator") <<
" px= "<<
p.px()
424 <<
" beta= "<<
p.Beta()
425 <<
" gamma= " <<
p.Gamma()
426 <<
" Proper time= " <<proper_time/ns <<
" ns";
431 double x1 = vp->end_vertex()->position().x();
432 double y1 = vp->end_vertex()->position().y();
433 double z1 = vp->end_vertex()->position().z();
435 for (HepMC::GenVertex::particle_iterator
436 vpdec= vp->end_vertex()->particles_begin(HepMC::children);
437 vpdec != vp->end_vertex()->particles_end(HepMC::children); ++vpdec) {
441 (*vpdec)->momentum().py(),
442 (*vpdec)->momentum().pz(),
443 (*vpdec)->momentum().e());
446 G4PrimaryParticle * g4daught=
447 new G4PrimaryParticle((*vpdec)->pdg_id(), pdec.x()*
GeV,
448 pdec.y()*
GeV, pdec.z()*
GeV);
450 if ( g4daught->GetG4code() != 0 )
452 g4daught->SetMass( g4daught->GetG4code()->GetPDGMass() ) ;
453 g4daught->SetCharge( g4daught->GetG4code()->GetPDGCharge() ) ;
459 setGenId( g4daught, (*vpdec)->barcode() );
462 <<
"Assigning a "<< (*vpdec)->pdg_id()
463 <<
" as daughter of a " << vp->pdg_id();
464 if ( (*vpdec)->status() == 2 && (*vpdec)->end_vertex() != 0 )
466 double x2 = (*vpdec)->end_vertex()->position().x();
467 double y2 = (*vpdec)->end_vertex()->position().y();
468 double z2 = (*vpdec)->end_vertex()->position().z();
469 double dd =
std::sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)+(z1-z2)*(z1-z2));
472 (*vpdec)->set_status(1000+(*vpdec)->status());
473 g4p->SetDaughter(g4daught);
475 if (
verbose > 1 ) g4daught->Print();
483 double ptot = p.mag();
489 if(ptot < pz + 1.
e-10) {
493 double eta = 0.5*G4Log((ptot + pz)/(ptot - pz));
500 double phi = p.phi();
507 <<
"Generator ptot(GeV)= " << ptot/GeV <<
" eta= " << p.eta()
508 <<
" phi= " << p.phi() <<
" Flag= " <<
flag;
516 for(HepMC::GenEvent::particle_const_iterator it = evt->particles_begin();
517 it != evt->particles_end(); ++it ) {
520 int g_status = gp->status();
523 int g_id = gp->pdg_id();
524 G4PrimaryParticle * g4p =
525 new G4PrimaryParticle(g_id,gp->momentum().px()*
GeV,
526 gp->momentum().py()*
GeV,
527 gp->momentum().pz()*
GeV);
528 if (g4p->GetG4code() != 0) {
529 g4p->SetMass(g4p->GetG4code()->GetPDGMass());
530 g4p->SetCharge(g4p->GetG4code()->GetPDGCharge());
537 G4PrimaryVertex *
v =
538 new G4PrimaryVertex(gp->production_vertex()->position().x()*mm,
539 gp->production_vertex()->position().y()*mm,
540 gp->production_vertex()->position().z()*mm,
541 gp->production_vertex()->position().t()*mm/c_light);
543 g4evt->AddPrimaryVertex(v);
544 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