13 #ifndef Pythia8_HepMC2_H 14 #define Pythia8_HepMC2_H 16 #ifdef Pythia8_HepMC3_H 17 #error Cannot include HepMC2.h if HepMC3.h has already been included. 23 #include "HepMC/IO_BaseClass.h" 24 #include "HepMC/IO_GenEvent.h" 25 #include "HepMC/GenEvent.h" 26 #include "HepMC/Units.h" 27 #include "Pythia8/Pythia.h" 28 #include "Pythia8/HIInfo.h" 39 virtual const char* what()
const throw() {
return "Pythia8ToHepMCException";}
56 ss <<
"Bad end vertex at " << i <<
" for flavour " << pdg_idIn;
62 virtual const char*
what()
const throw() {
return msg.c_str();}
65 int index()
const {
return iSave;}
66 int pdg_id()
const {
return idSave;}
84 m_free_parton_exception(true), m_convert_gluon_to_0(false),
85 m_store_pdf(true), m_store_proc(true), m_store_xsec(true),
86 m_store_weights(true) {;}
91 int ievnum = -1,
bool append =
false, GenParticle* rootParticle = 0,
92 int iBarcode = -1 ) {
return fill_next_event( pythia.
event, &evt, ievnum,
93 &pythia.
info, &pythia.
settings, append, rootParticle, iBarcode);}
95 int ievnum = -1,
bool append =
false, GenParticle* rootParticle = 0,
96 int iBarcode = -1 ) {
return fill_next_event( pythia.
event, evt, ievnum,
97 &pythia.
info, &pythia.
settings, append, rootParticle, iBarcode);}
103 GenParticle* rootParticle = 0,
int iBarcode = -1) {
104 return fill_next_event(pyev, &evt, ievnum, pyinfo, pyset,
105 append, rootParticle, iBarcode); }
110 GenParticle* rootParticle = 0,
int iBarcode = -1);
114 bool free_parton_exception()
const {
return m_free_parton_exception;}
115 bool free_parton_warnings()
const {
return m_free_parton_exception;}
116 bool convert_gluon_to_0()
const {
return m_convert_gluon_to_0;}
117 bool store_pdf()
const {
return m_store_pdf;}
118 bool store_proc()
const {
return m_store_proc;}
119 bool store_xsec()
const {
return m_store_xsec;}
120 bool store_weights()
const {
return m_store_weights;}
124 void set_free_parton_exception(
bool b =
true) {m_free_parton_exception = b;}
125 void set_free_parton_warnings(
bool b =
true) {m_free_parton_exception = b;}
126 void set_convert_gluon_to_0(
bool b =
false) {m_convert_gluon_to_0 = b;}
127 void set_store_pdf(
bool b =
true) {m_store_pdf = b;}
128 void set_store_proc(
bool b =
true) {m_store_proc = b;}
129 void set_store_xsec(
bool b =
true) {m_store_xsec = b;}
130 void set_store_weights(
bool b =
true) {m_store_weights = b;}
137 std::string message, std::string extraInfo =
"") {
138 if (pyinfo !=
nullptr)
140 else if ( print_inconsistency() )
141 std::cout <<
"Warning in " << loc <<
": " << message << extraInfo
147 virtual bool fill_next_event( GenEvent* ) {
return 0; }
148 virtual void write_event(
const GenEvent* ) {;}
154 int m_internal_event_number;
155 bool m_print_inconsistency, m_free_parton_exception, m_convert_gluon_to_0,
156 m_store_pdf, m_store_proc, m_store_xsec, m_store_weights;
172 if (evt ==
nullptr)
return warning(pyinfo,
173 "Pythia8ToHepMC::fill_next_event",
"passed null event");
178 evt->set_event_number(ievnum);
179 m_internal_event_number = ievnum;
181 evt->set_event_number(m_internal_event_number);
182 ++m_internal_event_number;
187 double momFac = HepMC::Units::conversion_factor(HepMC::Units::GEV,
188 evt->momentum_unit());
189 double lenFac = HepMC::Units::conversion_factor(HepMC::Units::MM,
196 if (rootParticle ==
nullptr)
return warning(pyinfo,
197 "Pythia8ToHepMC::fill_next_event",
198 "passed null root particle in append mode");
200 newBarcode = (iBarcode > -1) ? iBarcode : evt->particles_size();
202 GenVertex* prod_vtx0 =
new GenVertex();
203 prod_vtx0->add_particle_in( rootParticle );
204 evt->add_vertex( prod_vtx0 );
208 if ( pyinfo && pyinfo->
hiInfo ) {
216 ion.set_impact_parameter(pyinfo->
hiInfo->
b());
217 evt->set_heavy_ion(ion);
222 std::vector<GenParticle*> hepevt_particles( pyev.
size() );
223 for (
int i = iStart; i < pyev.
size(); ++i) {
226 hepevt_particles[i] =
new GenParticle(
227 FourVector( momFac * pyev[i].px(), momFac * pyev[i].py(),
228 momFac * pyev[i].pz(), momFac * pyev[i].e() ),
229 pyev[i].
id(), pyev[i].statusHepMC() );
230 if (iBarcode != 0) ++newBarcode;
231 hepevt_particles[i]->suggest_barcode( (append) ? newBarcode : i );
232 hepevt_particles[i]->set_generated_mass( momFac * pyev[i].m() );
235 int colType = pyev[i].colType();
236 if (colType == 1 || colType == 2)
237 hepevt_particles[i]->set_flow(1, pyev[i].col());
238 if (colType == -1 || colType == 2)
239 hepevt_particles[i]->set_flow(2, pyev[i].acol());
245 evt->set_beam_particles( hepevt_particles[1], hepevt_particles[2] );
250 for (
int i = iStart; i < pyev.
size(); ++i) {
251 GenParticle* p = hepevt_particles[i];
254 std::vector<int> mothers = pyev[i].motherList();
255 unsigned int imother = 0;
257 if ( !mothers.empty() ) mother = mothers[imother];
258 GenVertex* prod_vtx = p->production_vertex();
259 while ( !prod_vtx && mother > 0 ) {
260 prod_vtx = (append && mother == 1) ? rootParticle->end_vertex()
261 : hepevt_particles[mother]->end_vertex();
262 if (prod_vtx) prod_vtx->add_particle_out( p );
263 mother = ( ++imother < mothers.size() ) ? mothers[imother] : -1;
268 FourVector prod_pos( lenFac * pyev[i].xProd(), lenFac * pyev[i].yProd(),
269 lenFac * pyev[i].zProd(), lenFac * pyev[i].tProd() );
270 if ( !prod_vtx && ( mothers.size() > 0 || prod_pos != FourVector() ) ) {
271 prod_vtx =
new GenVertex();
272 prod_vtx->add_particle_out( p );
273 evt->add_vertex( prod_vtx );
277 if ( prod_vtx && prod_vtx->position() == FourVector() )
278 prod_vtx->set_position( prod_pos );
283 if ( !mothers.empty() ) mother = mothers[imother];
284 while ( prod_vtx && mother > 0 ) {
287 GenParticle* ppp = (append && mother == 1) ? rootParticle
288 : hepevt_particles[mother];
289 if ( !ppp->end_vertex() ) {
290 prod_vtx->add_particle_in( ppp );
291 }
else if (ppp->end_vertex() != prod_vtx ) {
297 if ( (pyev[i].statusAbs() == 43 || pyev[i].statusAbs() == 51
298 || pyev[i].statusAbs() == 53) && mother >= 1)
break;
303 warning(pyinfo,
"Pythia8ToHepMC::fill_next_event",
304 "inconsistent mother/daugher information in Pythia8 event",
310 mother = ( ++imother < mothers.size() ) ? mothers[imother] : -1;
315 bool doHadr = (pyset == 0) ? m_free_parton_exception
316 : pyset->
flag(
"HadronLevel:all") && pyset->
flag(
"HadronLevel:Hadronize");
321 for (
int i = iStart; i < pyev.
size(); ++i) {
322 if ( hepevt_particles[i]->end_vertex() ==
nullptr &&
323 hepevt_particles[i]->production_vertex() == nullptr ) {
324 warning(pyinfo,
"Pythia8ToHepMC::fill_next_event" 326 GenVertex* prod_vtx =
new GenVertex();
327 prod_vtx->add_particle_out( hepevt_particles[i] );
328 evt->add_vertex( prod_vtx );
332 if ( doHadr && m_free_parton_exception ) {
333 int pdg_tmp = hepevt_particles[i]->pdg_id();
334 if ( (abs(pdg_tmp) <= 6 || pdg_tmp == 21)
335 && !hepevt_particles[i]->end_vertex() )
341 if (append)
return true;
345 if (m_store_pdf && pyinfo != 0) {
346 int id1pdf = pyinfo->
id1pdf();
347 int id2pdf = pyinfo->id2pdf();
348 if ( m_convert_gluon_to_0 ) {
349 if (id1pdf == 21) id1pdf = 0;
350 if (id2pdf == 21) id2pdf = 0;
354 evt->set_pdf_info( PdfInfo( id1pdf, id2pdf, pyinfo->x1pdf(),
355 pyinfo->x2pdf(), pyinfo->QFac(), pyinfo->pdf1(), pyinfo->pdf2() ) );
359 if (m_store_proc && pyinfo != 0) {
360 evt->set_signal_process_id( pyinfo->code() );
361 evt->set_mpi( pyinfo->
nMPI() );
362 evt->set_event_scale( pyinfo->QRen() );
363 if (evt->alphaQED() <= 0) evt->set_alphaQED( pyinfo->alphaEM() );
364 if (evt->alphaQCD() <= 0) evt->set_alphaQCD( pyinfo->alphaS() );
369 if (m_store_xsec && pyinfo != 0) {
370 HepMC::GenCrossSection xsec;
371 xsec.set_cross_section( pyinfo->
sigmaGen() * 1e9,
373 evt->set_cross_section(xsec);
375 std::vector<double> xsecVec = pyinfo->weightContainerPtr->
getTotalXsec();
376 if (xsecVec.size() > 0) {
377 xsec.set_cross_section(xsecVec[0]*1e9);
378 evt->set_cross_section(xsec);
382 if (m_store_weights && pyinfo != 0) {
383 for (
int iweight = 0; iweight < pyinfo->numberOfWeights();
385 std::string name = pyinfo->weightNameByIndex(iweight);
386 double value = pyinfo->weightValueByIndex(iweight);
387 evt->weights()[name] = value;
418 typedef shared_ptr<GenEvent> EventPtr;
419 typedef HepMC::IO_GenEvent Writer;
420 typedef shared_ptr<Writer> WriterPtr;
427 setNewFile(filename, ft);
434 writerPtr = make_shared<HepMC::IO_GenEvent>(filename);
440 return writerPtr !=
nullptr;
446 geneve = make_shared<GenEvent>();
447 return fill_next_event(pythia, *geneve);
452 writerPtr->write_event(&*geneve);
459 if ( !fillNextEvent(pythia) )
return false;
486 HepMC::GenCrossSection xs;
487 xs.set_cross_section(xsec, xsecerr);
488 geneve->set_cross_section(xs);
493 if ( wv.size() != weightNames.size() )
494 geneve->weights() = wv;
496 for (
int i= 0, N = wv.size(); i < N; ++i )
497 geneve->weights()[weightNames[i]] = wv[i];
507 double scale,
double xf1,
double xf2,
508 int pdf1 = 0,
int pdf2 = 0) {
509 HepMC::PdfInfo pdf(id1, id2, x1, x2, scale, xf1, xf2, pdf1, pdf2);
510 geneve->set_pdf_info(pdf);
516 EventPtr geneve =
nullptr;
519 WriterPtr writerPtr =
nullptr;
522 vector<string> weightNames;
Pythia8ToHepMC()
Constructor and destructor.
Definition: HepMC2.h:83
void setWeightNames(const vector< string > &wnv)
Set all weight names in the current run.
Definition: HepMC2.h:501
bool flag(string keyIn)
Give back current value, with check that key exists.
Definition: Settings.cc:1599
GenEvent & event()
Get a reference to the current GenEvent.
Definition: HepMC2.h:465
int nCollND() const
The number of non-diffractive sub-collisions in the event.
Definition: HIInfo.h:60
The Event class holds all info on the generated event.
Definition: Event.h:408
Exception thrown when an undecayed parton is written into the record.
Definition: HepMC2.h:47
void setPdfInfo(int id1, int id2, double x1, double x2, double scale, double xf1, double xf2, int pdf1=0, int pdf2=0)
Update the PDF information in the current GenEvent.
Definition: HepMC2.h:506
Logger * loggerPtr
Pointer to the logger.
Definition: Info.h:86
Pythia8ToHepMC(string filename, OutputType ft=ascii2)
Construct an object with an internal output stream.
Definition: HepMC2.h:426
vector< double > getTotalXsec()
Return cross section estimate for total run.
Definition: Weights.cc:1137
int nMPI() const
Number of multiparton interactions, with code and pT for them.
Definition: Info.h:270
Settings settings
Settings: databases of flags/modes/parms/words to control run.
Definition: Pythia.h:374
double b() const
The impact-parameter distance in the current event.
Definition: HIInfo.h:38
bool writeNextEvent(Pythia &pythia)
Definition: HepMC2.h:458
The Pythia8ToHepMC class.
Definition: HepMC2.h:78
double sigmaErr(int i=0) const
The uncertainty on the estimated cross-section in units of mb.
Definition: Info.h:320
Event event
The event record for the complete event history.
Definition: Pythia.h:365
Base exception for all exceptions that Pythia8ToHepMC might throw.
Definition: HepMC2.h:36
PartonEndVertexException(int i, int pdg_idIn)
Constructor and destructor.
Definition: HepMC2.h:52
int id1pdf(int i=0) const
Hard process flavours, x values, parton densities, couplings, Q2 scales.
Definition: Info.h:169
void setXSec(double xsec, double xsecerr)
Set cross section information in the current GenEvent.
Definition: HepMC2.h:485
void warningMsg(string loc, string message, string extraInfo="", bool showAlways=false)
Warnings indicate that there might be an issue, but the run will continue.
Definition: Logger.cc:32
void writeEvent()
Write out the current GenEvent to the internal stream.
Definition: HepMC2.h:451
virtual const char * what() const
Throw exception.
Definition: HepMC2.h:62
EventPtr getEventPtr()
Get a pointer to the current GenEvent.
Definition: HepMC2.h:470
Pythia8ToHepMC()
The empty constructor does not creat an aoutput stream.
Definition: HepMC2.h:423
HIInfo * hiInfo
Definition: Info.h:113
string toString(bool val)
Convert a boolean to a string.
Definition: PythiaStdlib.h:217
bool fill_next_event(Pythia8::Event &pyev, GenEvent &evt, int ievnum=-1, const Pythia8::Info *pyinfo=0, Pythia8::Settings *pyset=0, bool append=false, GenParticle *rootParticle=0, int iBarcode=-1)
Alternative method to convert Pythia events into HepMC ones.
Definition: HepMC2.h:100
double sigmaGen(int i=0) const
The estimated cross-section in units of mb.
Definition: Info.h:312
bool setNewFile(string filename, OutputType ft=ascii2)
Open a new external output stream.
Definition: HepMC2.h:431
int size() const
Event record size.
Definition: Event.h:459
int nAbsTarg() const
The number of absorptively wounded target nucleons in the event.
Definition: HIInfo.h:95
WriterPtr outputPtr()
Get a pointer to the internal stream.
Definition: HepMC2.h:480
bool print_inconsistency() const
Read out values for some switches.
Definition: HepMC2.h:113
int nAbsProj() const
The number of absorptively wounded projectile nucleons in the event.
Definition: HIInfo.h:83
void setWeights(const vector< double > &wv)
Update all weights in the current GenEvent.
Definition: HepMC2.h:492
HepMC::GenEvent GenEvent
Typedef for the version 2 specific classes used.
Definition: HepMC2.h:417
void set_print_inconsistency(bool b=true)
Set values for some switches.
Definition: HepMC2.h:123
OutputType
We can either have standard ascii output or none at all.
Definition: HepMC2.h:414
Header for classes to set beam momentum and interaction vertex spread.
Definition: Analysis.h:20
The Pythia class contains the top-level routines to generate an event.
Definition: Pythia.h:71
bool fillNextEvent(Pythia &pythia)
Definition: HepMC2.h:445
const Info & info
Public information and statistic on the generation.
Definition: Pythia.h:368
Writer & output()
Get a reference to the internal stream.
Definition: HepMC2.h:475
int nDiffTarg() const
The number of diffractively wounded target nucleons in the event.
Definition: HIInfo.h:98
int index() const
Return info on location and flavour of bad parton.
Definition: HepMC2.h:65
int nDiffProj() const
The number of diffractively wounded projectile nucleons in the event.
Definition: HIInfo.h:86
bool fill_next_event(Pythia8::Pythia &pythia, GenEvent &evt, int ievnum=-1, bool append=false, GenParticle *rootParticle=0, int iBarcode=-1)
The recommended method to convert Pythia events into HepMC ones.
Definition: HepMC2.h:90
Definition: Settings.h:196
int nCollTot() const
The total number of sub-collisions in the event.
Definition: HIInfo.h:57