10 #include "G4HEPEvtParticle.hh"
11 #include "G4ParticleDefinition.hh"
12 #include "G4UnitsTable.hh"
13 #include "G4SystemOfUnits.hh"
14 #include "G4PhysicalConstants.hh"
22 fPCuts(p.getParameter<bool>(
"ApplyPCuts")),
23 fPtransCut(p.getParameter<bool>(
"ApplyPtransCut")),
24 fEtaCuts(p.getParameter<bool>(
"ApplyEtaCuts")),
25 fPhiCuts(p.getParameter<bool>(
"ApplyPhiCuts")),
26 theMinPhiCut(p.getParameter<double>(
"MinPhiCut")),
27 theMaxPhiCut(p.getParameter<double>(
"MaxPhiCut")),
28 theMinEtaCut(p.getParameter<double>(
"MinEtaCut")),
29 theMaxEtaCut(p.getParameter<double>(
"MaxEtaCut")),
30 theMinPCut(p.getParameter<double>(
"MinPCut")),
31 theMaxPCut(p.getParameter<double>(
"MaxPCut")),
32 theEtaCutForHector(p.getParameter<double>(
"EtaCutForHector")),
33 verbose(p.getUntrackedParameter<int>(
"Verbosity",0)),
43 double theRDecLenCut = p.
getParameter<
double>(
"RDecLenCut")*cm;
51 if ( p.
exists(
"PDGselection") ) {
53 getParameter<bool>(
"PDGfilterSel");
55 getParameter<std::vector< int > >(
"PDGfilter");
56 if(0 < pdgFilter.size()) {
58 for (
unsigned int ii = 0;
ii < pdgFilter.size(); ++
ii) {
61 <<
" *** Selecting only PDG ID = " << pdgFilter[
ii];
79 <<
" Rdecaycut= " << theRDecLenCut/cm
80 <<
" cm; Zdecaycut= " << theDecLenCut/cm
82 <<
" cm; Z_hector = " <<
Z_hector <<
" cm\n"
94 if ( *(evt->vertices_begin()) == 0 ) {
95 throw SimG4Exception(
"SimG4CoreGenerator: Corrupted Event - GenEvent with no vertex");
98 if (evt->weights().size() > 0) {
101 for (
unsigned int iw=1; iw<evt->weights().size(); ++iw) {
104 if ( evt->weights()[iw] <= 0 )
break;
105 weight_ *= evt->weights()[iw] ;
111 (*(evt->vertices_begin()))->position().y(),
112 (*(evt->vertices_begin()))->position().z(),
113 (*(evt->vertices_begin()))->position().t());
117 LogDebug(
"SimG4CoreGenerator") <<
"Primary Vertex = ("
123 unsigned int ng4vtx = 0;
125 for(HepMC::GenEvent::vertex_const_iterator vitr= evt->vertices_begin();
126 vitr != evt->vertices_end(); ++vitr ) {
130 HepMC::GenVertex::particle_iterator pitr;
131 for (pitr= (*vitr)->particles_begin(HepMC::children);
132 pitr != (*vitr)->particles_end(HepMC::children); ++pitr) {
135 if (1 == (*pitr)->status()) {
138 <<
"GenVertex barcode = " << (*vitr)->barcode()
139 <<
" selected for GenParticle barcode = " << (*pitr)->barcode();
145 else if (2 == (*pitr)->status()) {
147 if ( (*pitr)->end_vertex() != 0 ) {
148 double xx = (*pitr)->end_vertex()->position().x();
149 double yy = (*pitr)->end_vertex()->position().y();
150 double r_dd = xx*xx+yy*yy;
154 <<
"GenVertex barcode = " << (*vitr)->barcode()
155 <<
" selected for GenParticle barcode = "
156 << (*pitr)->barcode() <<
" radius = " <<
std::sqrt(r_dd);
173 double x1 = (*vitr)->position().x()*mm;
174 double y1 = (*vitr)->position().y()*mm;
175 double z1 = (*vitr)->position().z()*mm;
176 double t1 = (*vitr)->position().t()*mm/c_light;
178 G4PrimaryVertex* g4vtx =
new G4PrimaryVertex(x1, y1, z1, t1);
180 for (pitr= (*vitr)->particles_begin(HepMC::children);
181 pitr != (*vitr)->particles_end(HepMC::children); ++pitr){
190 <<
"Skip GenParticle barcode = " << (*pitr)->barcode()
191 <<
" PDG Id = " << (*pitr)->pdg_id();
199 double decay_length = 0.0;
200 int status = (*pitr)->status();
203 if (1 == status || 2 == status) {
207 if ( !(*pitr)->end_vertex() ) {
210 x2 = (*pitr)->end_vertex()->position().x();
211 y2 = (*pitr)->end_vertex()->position().y();
212 z2 = (*pitr)->end_vertex()->position().z();
214 std::sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1)+(z2-z1)*(z2-z1));
218 bool toBeAdded =
false;
219 double px = (*pitr)->momentum().px();
220 double py = (*pitr)->momentum().py();
221 double pz = (*pitr)->momentum().pz();
222 double ptot =
std::sqrt(px*px + py*py + pz*pz);
231 const double minTan = 1.e-20;
232 if(fabs(z1) <
Z_hector && fabs(pz) >= minTan*ptot) {
233 if(pz > 0.0) { zimpact =
Z_hector; }
235 double del = (zimpact - z1)/pz;
239 double rimpact2 = ximpact*ximpact + yimpact*yimpact;
242 <<
"Processing GenParticle barcode= " << (*pitr)->barcode()
243 <<
" pdg= " << (*pitr)-> pdg_id()
244 <<
" status= " << (*pitr)->status()
246 <<
" rimpact(cm)= " <<
std::sqrt(rimpact2)/cm
247 <<
" zimpact(cm)= " << zimpact/cm
248 <<
" ptot(GeV)= " << ptot
249 <<
" pz(GeV)= " << pz;
257 <<
"GenParticle barcode = " << (*pitr)->barcode()
271 double phi =
p.phi();
286 if(fabs(pz) >= minTan*ptot) {
287 if((zi >=
Z_lmax) & (pz < 0.0)) {
289 }
else if((zi <=
Z_lmin) & (pz > 0.0)) {
292 if(pz > 0) { zi =
Z_lmax; }
295 double del = (zi - z1)/pz;
307 <<
"GenParticle barcode = " << (*pitr)->barcode()
313 }
else if(2 == status &&
318 <<
"GenParticle barcode = " << (*pitr)->barcode()
320 <<
" decay_length(cm)= " << decay_length/cm;
324 G4int pdgcode= (*pitr)-> pdg_id();
325 G4PrimaryParticle* g4prim=
326 new G4PrimaryParticle(pdgcode, px*GeV, py*GeV, pz*GeV);
328 if ( g4prim->GetG4code() != 0 ){
329 g4prim->SetMass( g4prim->GetG4code()->GetPDGMass() );
330 double charge = g4prim->GetG4code()->GetPDGCharge();
338 g4prim->SetCharge(charge);
344 setGenId( g4prim, (*pitr)->barcode() );
350 if (
verbose > 1 ) g4prim->Print();
351 g4vtx->SetPrimary(g4prim);
355 if (
verbose > 1 ) g4vtx->Print();
356 g4evt->AddPrimaryVertex(g4vtx);
363 G4PrimaryVertex* g4vtx =
new G4PrimaryVertex(0.0, 0.0, 0.0, 0.0);
364 if (
verbose > 1 ) g4vtx->Print();
365 g4evt->AddPrimaryVertex(g4vtx);
375 <<
"Special case of long decay length \n"
376 <<
"Assign daughters with to mother with decaylength="
377 << decaylength/cm <<
" cm";
380 vp->momentum().pz(), vp->momentum().e());
383 double proper_time = decaylength/(
p.Beta()*
p.Gamma()*c_light);
384 g4p->SetProperTime(proper_time);
387 LogDebug(
"SimG4CoreGenerator") <<
" px= "<<
p.px()
391 <<
" beta= "<<
p.Beta()
392 <<
" gamma= " <<
p.Gamma()
393 <<
" Proper time= " <<proper_time/ns <<
" ns";
398 double x1 = vp->end_vertex()->position().x();
399 double y1 = vp->end_vertex()->position().y();
400 double z1 = vp->end_vertex()->position().z();
402 for (HepMC::GenVertex::particle_iterator
403 vpdec= vp->end_vertex()->particles_begin(HepMC::children);
404 vpdec != vp->end_vertex()->particles_end(HepMC::children); ++vpdec) {
408 (*vpdec)->momentum().py(),
409 (*vpdec)->momentum().pz(),
410 (*vpdec)->momentum().e());
413 G4PrimaryParticle * g4daught=
414 new G4PrimaryParticle((*vpdec)->pdg_id(), pdec.x()*GeV,
415 pdec.y()*GeV, pdec.z()*GeV);
417 if ( g4daught->GetG4code() != 0 )
419 g4daught->SetMass( g4daught->GetG4code()->GetPDGMass() ) ;
420 g4daught->SetCharge( g4daught->GetG4code()->GetPDGCharge() ) ;
426 setGenId( g4daught, (*vpdec)->barcode() );
429 <<
"Assigning a "<< (*vpdec)->pdg_id()
430 <<
" as daughter of a " << vp->pdg_id();
431 if ( (*vpdec)->status() == 2 && (*vpdec)->end_vertex() != 0 )
433 double x2 = (*vpdec)->end_vertex()->position().x();
434 double y2 = (*vpdec)->end_vertex()->position().y();
435 double z2 = (*vpdec)->end_vertex()->position().z();
436 double dd =
std::sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)+(z1-z2)*(z1-z2));
440 (*vpdec)->set_status(1000+(*vpdec)->status());
441 g4p->SetDaughter(g4daught);
443 if (
verbose > 1 ) g4daught->Print();
451 double ptot = p.mag();
457 if(ptot < pz + 1.
e-10) {
461 double eta = 0.5*G4Log((ptot + pz)/(ptot - pz));
468 double phi = p.phi();
475 <<
"Generator ptot(GeV)= " << ptot/GeV <<
" eta= " << p.eta()
476 <<
" phi= " << p.phi() <<
" Flag= " <<
flag;
484 for(HepMC::GenEvent::particle_const_iterator it = evt->particles_begin();
485 it != evt->particles_end(); ++it ) {
488 int g_status = gp->status();
491 int g_id = gp->pdg_id();
492 G4PrimaryParticle * g4p =
493 new G4PrimaryParticle(g_id,gp->momentum().px()*GeV,
494 gp->momentum().py()*GeV,
495 gp->momentum().pz()*GeV);
496 if (g4p->GetG4code() != 0) {
497 g4p->SetMass(g4p->GetG4code()->GetPDGMass());
498 g4p->SetCharge(g4p->GetG4code()->GetPDGCharge());
505 G4PrimaryVertex *
v =
506 new G4PrimaryVertex(gp->production_vertex()->position().x()*mm,
507 gp->production_vertex()->position().y()*mm,
508 gp->production_vertex()->position().z()*mm,
509 gp->production_vertex()->position().t()*mm/c_light);
511 g4evt->AddPrimaryVertex(v);
512 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.
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