11 #include "G4EventManager.hh"
12 #include "G4HEPEvtParticle.hh"
13 #include "G4ParticleDefinition.hh"
14 #include "G4UnitsTable.hh"
23 fPCuts(p.getParameter<bool>(
"ApplyPCuts")),
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 theRDecLenCut(p.getParameter<double>(
"RDecLenCut")*cm),
33 theEtaCutForHector(p.getParameter<double>(
"EtaCutForHector")),
34 verbose(p.getUntrackedParameter<int>(
"Verbosity",0)),
45 if ( p.
exists(
"PDGselection") ) {
49 for (
unsigned int ii = 0;
ii < pdgFilter.size() ;
ii++ ) {
51 edm::LogWarning(
"SimG4CoreGenerator") <<
" *** Selecting only PDG ID = " << pdgFilter[
ii];
53 edm::LogWarning(
"SimG4CoreGenerator") <<
" *** Filtering out PDG ID = " << pdgFilter[
ii];
77 if ( *(evt_orig->vertices_begin()) == 0 )
79 throw SimG4Exception(
"SimG4CoreGenerator: Corrupted Event - GenEvent with no vertex" ) ;
83 HepMC::GenEvent* evt =
new HepMC::GenEvent(*evt_orig) ;
85 if ( evt->weights().size() > 0 )
88 for (
unsigned int iw=1; iw<evt->weights().size(); iw++ )
91 if ( evt->weights()[iw] <= 0 )
break;
98 (*(evt->vertices_begin()))->position().y(),
99 (*(evt->vertices_begin()))->position().z(),
100 (*(evt->vertices_begin()))->position().t());
104 LogDebug(
"SimG4CoreGenerator") <<
"Primary Vertex = (" <<
vtx_->x() <<
","
112 unsigned int ng4vtx = 0;
114 for(HepMC::GenEvent::vertex_const_iterator vitr= evt->vertices_begin();
115 vitr != evt->vertices_end(); ++vitr ) {
120 for (HepMC::GenVertex::particle_iterator pitr= (*vitr)->particles_begin(HepMC::children);
121 pitr != (*vitr)->particles_end(HepMC::children); ++pitr) {
123 if ((*pitr)->status()==1) {
125 if (
verbose > 2 )
LogDebug(
"SimG4CoreGenerator") <<
"GenVertex barcode = " << (*vitr)->barcode()
126 <<
" selected for GenParticle barcode = " << (*pitr)->barcode() << std::endl;
132 else if ( (*pitr)->status()== 2 ) {
133 if ( (*pitr)->end_vertex() != 0 ) {
136 double xx = (*pitr)->end_vertex()->position().x();
137 double yy = (*pitr)->end_vertex()->position().y();
141 if (
verbose > 2 )
LogDebug(
"SimG4CoreGenerator") <<
"GenVertex barcode = " << (*vitr)->barcode()
142 <<
" selected for GenParticle barcode = " << (*pitr)->barcode() <<
" radius = " << r_dd << std::endl;
154 double x1 = (*vitr)->position().x();
155 double y1 = (*vitr)->position().y();
156 double z1 = (*vitr)->position().z();
157 double t1 = (*vitr)->position().t();
158 G4PrimaryVertex* g4vtx=
159 new G4PrimaryVertex(x1*mm, y1*mm, z1*mm, t1*mm/c_light);
161 for (HepMC::GenVertex::particle_iterator vpitr= (*vitr)->particles_begin(HepMC::children);
162 vpitr != (*vitr)->particles_end(HepMC::children); ++vpitr){
172 double r_decay_length=-1;
173 double decay_length=-1;
175 if ( (*vpitr)->status() == 1 || (*vpitr)->status() == 2 ) {
177 if ( (*vpitr)->end_vertex() != 0 ) {
182 double x2 = (*vpitr)->end_vertex()->position().x();
183 double y2 = (*vpitr)->end_vertex()->position().y();
184 double z2 = (*vpitr)->end_vertex()->position().z();
186 decay_length=
std::sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)+(z1-z2)*(z1-z2));
192 bool toBeAdded =
false;
194 (*vpitr)->momentum().py(),
195 (*vpitr)->momentum().pz(),
196 (*vpitr)->momentum().e());
199 const double minTan = 1.e-20;
201 if ( fabs(z1) <
Z_hector && fabs(
p.pt()/
p.pz()) >= minTan ) {
206 if (
verbose > 2 )
LogDebug(
"SimG4CoreGenerator") <<
"Processing GenParticle barcode = " << (*vpitr)->barcode()
207 <<
" status = " << (*vpitr)->status()
208 <<
" zimpact = " << zimpact;
216 if (
verbose > 2 )
LogDebug(
"SimG4CoreGenerator") <<
"Skip GenParticle barcode = " << (*vpitr)->barcode()
217 <<
" PDG Id = " << (*vpitr)->pdg_id() << std::endl;
224 if( (*vpitr)->status() == 1 && fabs(zimpact) <
Z_hector ) {
229 if (
verbose > 2 )
LogDebug(
"SimG4CoreGenerator") <<
"GenParticle barcode = " << (*vpitr)->barcode() <<
" passed case 1" << std::endl;
233 else if((*vpitr)->status() == 2 && r_decay_length >
theRDecLenCut &&
236 if (
verbose > 2 )
LogDebug(
"SimG4CoreGenerator") <<
"GenParticle barcode = " << (*vpitr)->barcode() <<
" passed case 2" << std::endl;
241 else if( (*vpitr)->status() == 1 && fabs(zimpact) >=
Z_hector && fabs(z1) >=
Z_hector) {
243 if (
verbose > 2 )
LogDebug(
"SimG4CoreGenerator") <<
"GenParticle barcode = " << (*vpitr)->barcode() <<
" passed case 3" << std::endl;
248 G4int pdgcode= (*vpitr)-> pdg_id();
249 G4PrimaryParticle* g4prim=
250 new G4PrimaryParticle(pdgcode,
p.Px()*GeV,
p.Py()*GeV,
p.Pz()*GeV);
252 if ( g4prim->GetG4code() != 0 ){
253 g4prim->SetMass( g4prim->GetG4code()->GetPDGMass() ) ;
254 g4prim->SetCharge( g4prim->GetG4code()->GetPDGCharge() ) ;
260 setGenId( g4prim, (*vpitr)->barcode() ) ;
262 if ( (*vpitr)->status() == 2 )
264 if (
verbose > 1 ) g4prim->Print();
265 g4vtx->SetPrimary(g4prim);
267 if ( (*vpitr)->status()==1 && (*vpitr)->end_vertex()!=0) {
268 double proper_time=decay_length/(
p.Beta()*
p.Gamma()*c_light);
269 if (
verbose > 1 )
LogDebug(
"SimG4CoreGenerator") <<
"Setting proper time for beta="<<
p.Beta()<<
" gamma="<<
p.Gamma()<<
" Proper time=" <<proper_time<<
" ns" ;
270 g4prim->SetProperTime(proper_time*ns);
275 if (
verbose > 1 ) g4vtx->Print();
276 g4evt->AddPrimaryVertex(g4vtx);
286 G4PrimaryVertex* g4vtx=
287 new G4PrimaryVertex(x1*mm, y1*mm, z1*mm, t1*mm/c_light);
288 if (
verbose > 1 ) g4vtx->Print();
289 g4evt->AddPrimaryVertex(g4vtx);
300 if ( !(vp->end_vertex()) )
return ;
303 LogDebug(
"SimG4CoreGenerator") <<
"Special case of long decay length \n"
304 <<
"Assign daughters with to mother with decaylength=" << decaylength <<
"mm";
306 double proper_time=decaylength/(
p.Beta()*
p.Gamma()*c_light);
308 LogDebug(
"SimG4CoreGenerator") <<
" px="<<vp->momentum().px()
309 <<
" py="<<vp->momentum().py()
310 <<
" pz="<<vp->momentum().pz()
311 <<
" e="<<vp->momentum().e()
313 <<
" gamma="<<
p.Gamma()
314 <<
" Proper time=" <<proper_time<<
" ns" ;
316 g4p->SetProperTime(proper_time*ns);
317 double x1 = vp->end_vertex()->position().x();
318 double y1 = vp->end_vertex()->position().y();
319 double z1 = vp->end_vertex()->position().z();
320 for (HepMC::GenVertex::particle_iterator
321 vpdec= vp->end_vertex()->particles_begin(HepMC::children);
322 vpdec != vp->end_vertex()->particles_end(HepMC::children); ++vpdec) {
326 (*vpdec)->momentum().py(),
327 (*vpdec)->momentum().pz(),
328 (*vpdec)->momentum().e());
330 G4PrimaryParticle * g4daught=
331 new G4PrimaryParticle((*vpdec)->pdg_id(), pdec.x()*GeV, pdec.y()*GeV, pdec.z()*GeV);
332 if ( g4daught->GetG4code() != 0 )
334 g4daught->SetMass( g4daught->GetG4code()->GetPDGMass() ) ;
335 g4daught->SetCharge( g4daught->GetG4code()->GetPDGCharge() ) ;
340 setGenId( g4daught, (*vpdec)->barcode() ) ;
341 if (
verbose > 1 )
LogDebug(
"SimG4CoreGenerator") <<
"Assigning a "<<(*vpdec)->pdg_id()
342 <<
" as daughter of a " <<vp->pdg_id() ;
343 if ( (*vpdec)->status() == 2 && (*vpdec)->end_vertex() != 0 )
345 double x2 = (*vpdec)->end_vertex()->position().x();
346 double y2 = (*vpdec)->end_vertex()->position().y();
347 double z2 = (*vpdec)->end_vertex()->position().z();
348 double dd =
std::sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)+(z1-z2)*(z1-z2));
351 (*vpdec)->set_status(1000+(*vpdec)->status());
352 g4p->SetDaughter(g4daught);
360 double phi = mom.Phi() ;
361 double nrg = mom.P() ;
372 if (
verbose > 2 )
LogDebug(
"SimG4CoreGenerator") <<
"Generator p = " << nrg <<
" z_imp = " << zimp <<
" phi = " << mom.Phi() <<
" Flag = " <<
flag;
380 G4ThreeVector mom = p->GetMomentum();
381 double phi = mom.phi() ;
382 double nrg =
sqrt(p->GetPx()*p->GetPx() + p->GetPy()*p->GetPy() + p->GetPz()*p->GetPz());
395 if (
verbose > 2 )
LogDebug(
"SimG4CoreGenerator") <<
"Generator p = " << nrg <<
" eta = " << eta <<
" theta = " << mom.theta() <<
" phi = " << phi <<
" Flag = " <<
flag;
404 for(HepMC::GenEvent::particle_const_iterator it = evt->particles_begin();
405 it != evt->particles_end(); ++it )
409 int g_status = g->status();
413 int g_id = g->pdg_id();
414 G4PrimaryParticle * g4p =
415 new G4PrimaryParticle(g_id,g->momentum().px()*GeV,g->momentum().py()*GeV,g->momentum().pz()*GeV);
416 if (g4p->GetG4code() != 0)
418 g4p->SetMass(g4p->GetG4code()->GetPDGMass());
419 g4p->SetCharge(g4p->GetG4code()->GetPDGCharge()) ;
427 G4PrimaryVertex *
v =
new
428 G4PrimaryVertex(g->production_vertex()->position().x()*mm,
429 g->production_vertex()->position().y()*mm,
430 g->production_vertex()->position().z()*mm,
431 g->production_vertex()->position().t()*mm/c_light);
433 g4evt->AddPrimaryVertex(v);
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)
The Signals That Services Can Subscribe To This is based on ActivityRegistry and is current per Services can connect to the signals distributed by the ActivityRegistry in order to monitor the activity of the application Each possible callback has some defined which we here list in angle e g
void particleAssignDaughters(G4PrimaryParticle *p, HepMC::GenParticle *hp, double length)
XYZTLorentzVectorD XYZTLorentzVector
Lorentz vector with cylindrical internal representation using pseudorapidity.
Tan< T >::type tan(const T &t)
math::XYZTLorentzVector * vtx_
void nonBeamEvent2G4(const HepMC::GenEvent *g, G4Event *e)
std::vector< int > pdgFilter
bool particlePassesPrimaryCuts(const G4PrimaryParticle *p) const