22 #include "EvtGen_i/EvtGenExternal/EvtPythiaEngine.hh"
24 #include "EvtGenBase/EvtDecayTable.hh"
25 #include "EvtGenBase/EvtExtGeneratorCommandsTable.hh"
26 #include "EvtGenBase/EvtPDL.hh"
27 #include "EvtGenBase/EvtParticleFactory.hh"
28 #include "EvtGenBase/EvtReport.hh"
29 #include "EvtGenBase/EvtSpinType.hh"
31 #include "EvtGen_i/EvtGenExternal/EvtPythia6CommandConverter.hh"
33 #include "Pythia8/Event.h"
34 #include "Pythia8/ParticleData.h"
40 #if PYTHIA_VERSION_INTEGER < 8304
48 EvtPythiaEngine::EvtPythiaEngine( std::string xmlDir,
bool convertPhysCodes,
49 bool useEvtGenRandom )
59 EvtGenReport( EVTGEN_INFO,
"EvtGen" )
60 <<
"Creating generic Pythia generator" << endl;
61 m_genericPythiaGen = std::make_unique<Pythia8::Pythia>( xmlDir );
63 EvtGenReport( EVTGEN_INFO,
"EvtGen" )
64 <<
"Creating alias Pythia generator" << endl;
65 m_aliasPythiaGen = std::make_unique<Pythia8::Pythia>( xmlDir,
false );
67 m_thePythiaGenerator = 0;
68 m_daugPDGVector.clear();
69 m_daugP4Vector.clear();
71 m_convertPhysCodes = convertPhysCodes;
75 m_useEvtGenRandom = useEvtGenRandom;
77 m_evtgenRandom = std::make_shared<EvtPythiaRandom>();
79 m_initialised =
false;
82 EvtPythiaEngine::~EvtPythiaEngine()
84 m_thePythiaGenerator =
nullptr;
85 this->clearDaughterVectors();
86 this->clearPythiaModeMap();
89 void EvtPythiaEngine::clearDaughterVectors()
91 m_daugPDGVector.clear();
92 m_daugP4Vector.clear();
95 void EvtPythiaEngine::clearPythiaModeMap()
98 for ( iter = m_pythiaModeMap.begin(); iter != m_pythiaModeMap.end(); ++iter ) {
99 std::vector<int> modeVector = iter->second;
103 m_pythiaModeMap.clear();
106 void EvtPythiaEngine::initialise()
108 if ( m_initialised ) {
112 this->clearPythiaModeMap();
114 this->updateParticleLists();
118 m_genericPythiaGen->readString(
"ProcessLevel:all = off" );
119 m_aliasPythiaGen->readString(
"ProcessLevel:all = off" );
122 m_genericPythiaGen->readString(
"Print:quiet = on" );
123 m_aliasPythiaGen->readString(
"Print:quiet = on" );
126 this->updatePhysicsParameters();
129 if ( m_useEvtGenRandom ==
true ) {
130 #if PYTHIA_VERSION_INTEGER >= 8310
131 m_genericPythiaGen->setRndmEnginePtr( m_evtgenRandom );
132 m_aliasPythiaGen->setRndmEnginePtr( m_evtgenRandom );
134 m_genericPythiaGen->setRndmEnginePtr( m_evtgenRandom.get() );
135 m_aliasPythiaGen->setRndmEnginePtr( m_evtgenRandom.get() );
139 m_genericPythiaGen->init();
140 m_aliasPythiaGen->init();
142 m_initialised =
true;
145 bool EvtPythiaEngine::doDecay( EvtParticle* theParticle )
162 if ( m_initialised ==
false ) {
166 if ( theParticle == 0 ) {
167 EvtGenReport( EVTGEN_INFO,
"EvtGen" )
168 <<
"Error in EvtPythiaEngine::doDecay. The mother particle is null. Not doing any Pythia decay."
174 if ( theParticle->getNDaug() != 0 ) {
175 bool keepChannel(
false );
176 theParticle->deleteDaughters( keepChannel );
179 EvtId particleId = theParticle->getId();
180 int isAlias = particleId.isAlias();
183 m_thePythiaGenerator = ( isAlias == 1 ? m_aliasPythiaGen.get()
184 : m_genericPythiaGen.get() );
192 int PDGCode = EvtPDL::getStdHep( particleId );
195 int colour( 0 ), anticolour( 0 );
196 double px( 0.0 ),
py( 0.0 ),
pz( 0.0 );
197 double m0 = theParticle->mass();
204 bool generatedEvent(
false );
205 for ( iTrial = 0; iTrial < 10; iTrial++ ) {
206 generatedEvent = m_thePythiaGenerator->next();
207 if ( generatedEvent ) {
212 bool success(
false );
214 if ( generatedEvent ) {
222 this->clearDaughterVectors();
227 this->storeDaughterInfo( theParticle, 1 );
232 this->createDaughterEvtParticles( theParticle );
243 void EvtPythiaEngine::storeDaughterInfo( EvtParticle* theParticle,
int startInt )
247 std::vector<int> daugList = theEvent.daughterList( startInt );
250 for ( daugIter = daugList.begin(); daugIter != daugList.end(); ++daugIter ) {
251 int daugInt = *daugIter;
256 if ( daugParticle.isQuark() || daugParticle.isGluon() ) {
258 this->storeDaughterInfo( theParticle, daugInt );
270 int daugPDGInt = daugParticle.id();
272 double px = daugParticle.px();
273 double py = daugParticle.py();
274 double pz = daugParticle.pz();
275 double E = daugParticle.e();
276 EvtVector4R daughterP4(
E,
px,
py,
pz );
279 m_daugPDGVector.push_back( daugPDGInt );
280 m_daugP4Vector.push_back( daughterP4 );
284 daugParticle.status( 1000 );
291 void EvtPythiaEngine::createDaughterEvtParticles( EvtParticle* theParent )
293 if ( theParent == 0 ) {
294 EvtGenReport( EVTGEN_INFO,
"EvtGen" )
295 <<
"Error in EvtPythiaEngine::createDaughterEvtParticles. The parent is null"
304 int nDaughters = m_daugPDGVector.size();
305 std::vector<EvtId> daugAliasIdVect( 0 );
307 EvtId particleId = theParent->getId();
310 int PDGId = EvtPDL::getStdHep( particleId );
311 int aliasInt = particleId.getAlias();
312 int pythiaAliasInt( aliasInt );
316 EvtId conjPartId = EvtPDL::chargeConj( particleId );
317 pythiaAliasInt = conjPartId.getAlias();
320 std::vector<int> pythiaModes = m_pythiaModeMap[pythiaAliasInt];
327 bool gotMode(
false );
329 for ( modeIter = pythiaModes.begin(); modeIter != pythiaModes.end();
336 int pythiaModeInt = *modeIter;
338 EvtDecayBase* decayModel = EvtDecayTable::getInstance()->findDecayModel(
339 aliasInt, pythiaModeInt );
341 if ( decayModel != 0 ) {
342 int nModeDaug = decayModel->getNDaug();
345 if ( nDaughters == nModeDaug ) {
347 for ( iModeDaug = 0; iModeDaug < nModeDaug; iModeDaug++ ) {
348 EvtId daugId = decayModel->getDaug( iModeDaug );
349 int daugPDGId = EvtPDL::getStdHep( daugId );
351 int pythiaPDGId = m_daugPDGVector[iModeDaug];
353 if ( daugPDGId == pythiaPDGId ) {
354 daugAliasIdVect.push_back( daugId );
359 int daugAliasSize = daugAliasIdVect.size();
360 if ( daugAliasSize == nDaughters ) {
366 daugAliasIdVect.clear();
375 if ( gotMode ==
false ) {
379 for ( iPyDaug = 0; iPyDaug < nDaughters; iPyDaug++ ) {
380 int daugPDGCode = m_daugPDGVector[iPyDaug];
381 EvtId daugPyId = EvtPDL::evtIdFromStdHep( daugPDGCode );
382 daugAliasIdVect.push_back( daugPyId );
387 theParent->makeDaughters( nDaughters, daugAliasIdVect );
392 for ( iDaug = 0; iDaug < nDaughters; iDaug++ ) {
393 EvtParticle* theDaughter = theParent->getDaug( iDaug );
396 if ( theDaughter != 0 ) {
397 EvtId theDaugId = daugAliasIdVect[iDaug];
398 const EvtVector4R theDaugP4 = m_daugP4Vector[iDaug];
399 theDaughter->init( theDaugId, theDaugP4 );
404 void EvtPythiaEngine::updateParticleLists()
418 m_addedPDGCodes.clear();
420 for ( iPDL = 0; iPDL < nPDL; iPDL++ ) {
421 EvtId particleId = EvtPDL::getEntry( iPDL );
422 int aliasInt = particleId.getAlias();
425 int PDGCode = EvtPDL::getStdHep( particleId );
428 double mass = EvtPDL::getMeanMass( particleId );
429 double width = EvtPDL::getWidth( particleId );
430 double lifetime = EvtPDL::getctau( particleId );
431 double mmin = EvtPDL::getMinMass( particleId );
432 double mmax = EvtPDL::getMaxMass( particleId );
444 m_genericPythiaGen->particleData.particleDataEntryPtr( PDGCode );
446 m_aliasPythiaGen->particleData.particleDataEntryPtr( PDGCode );
450 if ( entry_generic != 0 && this->validPDGCode( PDGCode ) ) {
451 entry_generic->setM0(
mass );
452 entry_generic->setMWidth(
width );
453 entry_generic->setTau0( lifetime );
455 if ( std::fabs(
width ) > 0.0 ) {
456 entry_generic->setMMin(
mmin );
457 entry_generic->setMMax(
mmax );
463 if ( entry_alias != 0 && this->validPDGCode( PDGCode ) ) {
464 entry_alias->setM0(
mass );
465 entry_alias->setMWidth(
width );
466 entry_alias->setTau0( lifetime );
468 if ( std::fabs(
width ) > 0.0 ) {
469 entry_alias->setMMin(
mmin );
470 entry_alias->setMMax(
mmax );
478 bool hasPythiaDecays = EvtDecayTable::getInstance()->hasPythia( aliasInt );
480 if ( hasPythiaDecays ) {
481 int isAlias = particleId.isAlias();
485 m_thePythiaGenerator = ( isAlias == 1 ? m_aliasPythiaGen.get()
486 : m_genericPythiaGen.get() );
489 std::string dataName = m_thePythiaGenerator->particleData.name(
491 bool alreadyStored = ( m_addedPDGCodes.find( abs( PDGCode ) ) !=
492 m_addedPDGCodes.end() );
494 if ( dataName ==
" " && !alreadyStored ) {
497 this->createPythiaParticle( particleId, PDGCode );
502 this->updatePythiaDecayTable( particleId, aliasInt, PDGCode );
515 bool EvtPythiaEngine::validPDGCode(
int PDGCode )
523 int absPDGCode = abs( PDGCode );
525 if ( absPDGCode == 0 || absPDGCode == 18 ) {
528 }
else if ( absPDGCode >= 81 && absPDGCode <= 100 ) {
536 void EvtPythiaEngine::updatePythiaDecayTable( EvtId& particleId,
int aliasInt,
546 int nModes = EvtDecayTable::getInstance()->getNModes( aliasInt );
549 bool firstMode(
true );
557 std::vector<int> pythiaModes( 0 );
560 for ( iMode = 0; iMode < nModes; iMode++ ) {
561 EvtDecayBase* decayModel =
562 EvtDecayTable::getInstance()->findDecayModel( aliasInt, iMode );
564 if ( decayModel != 0 ) {
565 int nDaug = decayModel->getNDaug();
573 std::string modelName = decayModel->getModelName();
575 if ( modelName ==
"PYTHIA" ) {
578 pythiaModes.push_back( iMode );
580 std::ostringstream oss;
581 oss.setf( std::ios::scientific );
588 oss <<
":oneChannel = ";
592 oss <<
":addChannel = ";
599 oss << onMode <<
" ";
601 double BF = decayModel->getBranchingFraction();
606 int modeInt = this->getModeInt( decayModel );
610 for ( iDaug = 0; iDaug < nDaug; iDaug++ ) {
611 EvtId daugId = decayModel->getDaug( iDaug );
612 int daugPDG = EvtPDL::getStdHep( daugId );
613 oss <<
" " << daugPDG;
617 m_thePythiaGenerator->readString( oss.str() );
622 EvtGenReport( EVTGEN_INFO,
"EvtGen" )
623 <<
"Warning in EvtPythiaEngine. Trying to redefine Pythia table for "
625 <<
" for a decay channel that has no daughters."
626 <<
" Keeping Pythia default (if available)." << endl;
630 EvtGenReport( EVTGEN_INFO,
"EvtGen" )
631 <<
"Error in EvtPythiaEngine. decayModel is null for particle "
632 <<
EvtPDL::name( particleId ) <<
" mode number " << iMode
638 m_pythiaModeMap[aliasInt] = pythiaModes;
641 std::ostringstream rescaleStr;
642 rescaleStr.setf( std::ios::scientific );
643 rescaleStr << PDGCode <<
":rescaleBR = 1.0";
645 m_thePythiaGenerator->readString( rescaleStr.str() );
648 int EvtPythiaEngine::getModeInt( EvtDecayBase* decayModel )
650 int tmpModeInt( 0 ), modeInt( 0 );
652 if ( decayModel != 0 ) {
653 int nVars = decayModel->getNArg();
657 tmpModeInt =
static_cast<int>( decayModel->getArg( 0 ) );
661 if ( m_convertPhysCodes ) {
666 if ( tmpModeInt == 0 ) {
668 }
else if ( tmpModeInt == 1 ) {
670 }
else if ( tmpModeInt == 2 ) {
672 }
else if ( tmpModeInt == 3 ) {
674 }
else if ( tmpModeInt == 4 ) {
676 }
else if ( tmpModeInt == 11 ) {
678 }
else if ( tmpModeInt == 12 ) {
680 }
else if ( tmpModeInt == 13 ) {
682 }
else if ( tmpModeInt == 14 ) {
684 }
else if ( tmpModeInt == 15 ) {
686 }
else if ( tmpModeInt >= 22 && tmpModeInt <= 30 ) {
687 modeInt = tmpModeInt +
689 }
else if ( tmpModeInt == 31 ) {
691 }
else if ( tmpModeInt == 32 ) {
693 }
else if ( tmpModeInt == 33 ) {
695 }
else if ( tmpModeInt == 41 ) {
697 }
else if ( tmpModeInt == 42 ) {
699 }
else if ( tmpModeInt == 43 ) {
701 }
else if ( tmpModeInt == 44 ) {
703 }
else if ( tmpModeInt == 48 ) {
705 }
else if ( tmpModeInt == 50 ) {
707 }
else if ( tmpModeInt == 51 ) {
709 }
else if ( tmpModeInt == 52 || tmpModeInt == 53 ) {
711 }
else if ( tmpModeInt == 84 ) {
713 }
else if ( tmpModeInt == 101 ) {
715 }
else if ( tmpModeInt == 102 ) {
718 EvtGenReport( EVTGEN_INFO,
"EvtGen" )
719 <<
"Pythia mode integer " << tmpModeInt
720 <<
" is not recognised. Using phase-space model" << endl;
726 modeInt = tmpModeInt;
732 void EvtPythiaEngine::createPythiaParticle( EvtId& particleId,
int PDGCode )
735 EvtId antiPartId = EvtPDL::chargeConj( particleId );
741 EvtSpinType::spintype spinType = EvtPDL::getSpinType( particleId );
742 int spin = EvtSpinType::getSpin2( spinType );
744 int charge = EvtPDL::chg3( particleId );
749 int PDGId = EvtPDL::getStdHep( particleId );
753 }
else if ( PDGId <= 8 && PDGId > 0 ) {
757 double m0 = EvtPDL::getMeanMass( particleId );
758 double mWidth = EvtPDL::getWidth( particleId );
759 double mMin = EvtPDL::getMinMass( particleId );
760 double mMax = EvtPDL::getMaxMass( particleId );
762 double tau0 = EvtPDL::getctau( particleId );
764 std::ostringstream oss;
765 oss.setf( std::ios::scientific );
766 int absPDGCode = abs( PDGCode );
767 oss << absPDGCode <<
":new = " << aliasName <<
" " << antiName <<
" "
769 <<
" " << mMin <<
" " << mMax <<
" " << tau0;
772 m_thePythiaGenerator->readString( oss.str() );
779 m_addedPDGCodes[absPDGCode] = 1;
782 void EvtPythiaEngine::updatePhysicsParameters()
792 std::string multiWeakCut(
"ParticleDecays:multIncreaseWeak = 2.0" );
793 m_genericPythiaGen->readString( multiWeakCut );
794 m_aliasPythiaGen->readString( multiWeakCut );
797 std::string multiCut(
"ParticleDecays:multIncrease = 4.5" );
798 m_genericPythiaGen->readString( multiCut );
799 m_aliasPythiaGen->readString( multiCut );
803 EvtExtGeneratorCommandsTable::getInstance()->getCommands(
"PYTHIA" );
808 std::vector<std::string> commandStrings;
810 if (
command[
"VERSION"] ==
"PYTHIA6" ) {
811 EvtGenReport( EVTGEN_INFO,
"EvtGen" )
812 <<
"Converting Pythia 6 command: " <<
command[
"MODULE"] <<
"("
815 }
else if (
command[
"VERSION"] ==
"PYTHIA8" ) {
816 commandStrings.push_back(
command[
"MODULE"] +
":" +
command[
"PARAM"] +
819 EvtGenReport( EVTGEN_ERROR,
"EvtGen" )
820 <<
"Pythia command received by EvtPythiaEngine has bad version:"
822 EvtGenReport( EVTGEN_ERROR,
"EvtGen" )
823 <<
"Received " <<
command[
"VERSION"]
824 <<
" but expected PYTHIA6 or PYTHIA8." << endl;
825 EvtGenReport( EVTGEN_ERROR,
"EvtGen" )
826 <<
"The error is likely to be in EvtDecayTable.cpp" << endl;
827 EvtGenReport( EVTGEN_ERROR,
"EvtGen" )
828 <<
"EvtGen will now abort." << endl;
836 for ( ; it2 != commandStrings.end(); ++it2 ) {
837 EvtGenReport( EVTGEN_INFO,
"EvtGen" )
838 <<
"Configuring generic Pythia generator: " << ( *it2 )
840 m_genericPythiaGen->readString( *it2 );
847 for ( ; it2 != commandStrings.end(); ++it2 ) {
848 EvtGenReport( EVTGEN_INFO,
"EvtGen" )
849 <<
"Configuring alias Pythia generator: " << ( *it2 )
851 m_aliasPythiaGen->readString( *it2 );