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;
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);
395 <<
"Special case of long decay length \n"
396 <<
"Assign daughters with to mother with decaylength="
397 << decaylength/cm <<
" cm";
400 vp->momentum().pz(), vp->momentum().e());
403 double proper_time = decaylength/(
p.Beta()*
p.Gamma()*c_light);
404 g4p->SetProperTime(proper_time);
407 LogDebug(
"SimG4CoreGenerator") <<
" px= "<<
p.px()
411 <<
" beta= "<<
p.Beta()
412 <<
" gamma= " <<
p.Gamma()
413 <<
" Proper time= " <<proper_time/ns <<
" ns";
418 double x1 = vp->end_vertex()->position().x();
419 double y1 = vp->end_vertex()->position().y();
420 double z1 = vp->end_vertex()->position().z();
422 for (HepMC::GenVertex::particle_iterator
423 vpdec= vp->end_vertex()->particles_begin(HepMC::children);
424 vpdec != vp->end_vertex()->particles_end(HepMC::children); ++vpdec) {
428 (*vpdec)->momentum().py(),
429 (*vpdec)->momentum().pz(),
430 (*vpdec)->momentum().e());
433 G4PrimaryParticle * g4daught=
434 new G4PrimaryParticle((*vpdec)->pdg_id(), pdec.x()*
GeV,
435 pdec.y()*
GeV, pdec.z()*
GeV);
437 if ( g4daught->GetG4code() != 0 )
439 g4daught->SetMass( g4daught->GetG4code()->GetPDGMass() ) ;
440 g4daught->SetCharge( g4daught->GetG4code()->GetPDGCharge() ) ;
446 setGenId( g4daught, (*vpdec)->barcode() );
449 <<
"Assigning a "<< (*vpdec)->pdg_id()
450 <<
" as daughter of a " << vp->pdg_id();
451 if ( (*vpdec)->status() == 2 && (*vpdec)->end_vertex() != 0 )
453 double x2 = (*vpdec)->end_vertex()->position().x();
454 double y2 = (*vpdec)->end_vertex()->position().y();
455 double z2 = (*vpdec)->end_vertex()->position().z();
456 double dd =
std::sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)+(z1-z2)*(z1-z2));
459 (*vpdec)->set_status(1000+(*vpdec)->status());
460 g4p->SetDaughter(g4daught);
462 if (
verbose > 1 ) g4daught->Print();
470 double ptot = p.mag();
476 if(ptot < pz + 1.
e-10) {
480 double eta = 0.5*G4Log((ptot + pz)/(ptot - pz));
487 double phi = p.phi();
494 <<
"Generator ptot(GeV)= " << ptot/GeV <<
" eta= " << p.eta()
495 <<
" phi= " << p.phi() <<
" Flag= " << flag;
503 for(HepMC::GenEvent::particle_const_iterator it = evt->particles_begin();
504 it != evt->particles_end(); ++it ) {
507 int g_status = gp->status();
510 int g_id = gp->pdg_id();
511 G4PrimaryParticle * g4p =
512 new G4PrimaryParticle(g_id,gp->momentum().px()*
GeV,
513 gp->momentum().py()*
GeV,
514 gp->momentum().pz()*
GeV);
515 if (g4p->GetG4code() != 0) {
516 g4p->SetMass(g4p->GetG4code()->GetPDGMass());
517 g4p->SetCharge(g4p->GetG4code()->GetPDGCharge());
524 G4PrimaryVertex *
v =
525 new G4PrimaryVertex(gp->production_vertex()->position().x()*mm,
526 gp->production_vertex()->position().y()*mm,
527 gp->production_vertex()->position().z()*mm,
528 gp->production_vertex()->position().t()*mm/c_light);
530 g4evt->AddPrimaryVertex(v);
531 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