Skip to content

Rework RNG initialisation to only fire once so Pythia8 does not silently fail, PR to master - #515

Open
kjplows wants to merge 4 commits into
GENIE-MC:masterfrom
kjplows:patch/kplows-pythia8-seed-master
Open

kjplows wants to merge 4 commits into
GENIE-MC:masterfrom
kjplows:patch/kplows-pythia8-seed-master

Conversation

@kjplows

@kjplows kjplows commented Sep 5, 2026

Copy link
Copy Markdown
Contributor

Copy of #514 , targeting master.

Summoning @nusense and @sjgardiner for review, again!

@sjgardiner

Copy link
Copy Markdown
Member

@nusense Please have a look when you get back to work. I've already merged this into a release-candidate branch that John is actively exercising. We made a separate PR to master to keep around for review.

@kjplows kjplows mentioned this pull request Sep 8, 2026

@nusense nusense left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm still working through all the typical scenarios, of which I see five:

(1)   utils::app_init::RandGen() called  non-zero
      [tune is configured]
      rndm = RandomGen::Instance()->RndFlux().Rndm()
(2)   utils:app_init::RandGen(0)
      [tune is configured]
      rndm = RandomGen::Instance()->RndFlux().Rndm()
(3)   [tune is configured]
      utils::app_init::RandGen() called  non-zero
      rndm = RandomGen::Instance()->RndFlux().Rndm()
(4)   [tune is configured]
      utils:app_init::RandGen(0)
      rndm = RandomGen::Instance()->RndFlux().Rndm()
(5)   utils::app_init::RandGen() never called; [tune is configured]
      rndm = RandomGen::Instance()->RndFlux().Rndm()

I'm still working them through in my head. I'm also working out the use case for static RandomGen * Instance(long int seed). I'm not sure where Instance(seed) gets constructed rather than Instance().

Would it be possible to incorporate the changes in #516 into this PR, so it would be easier to see the totality of the changes.

I think I copied:

  // sync GENIE and PYTHIA8 seeds
  RandomGen * rnd = RandomGen::Instance();
  long int seed = rnd->GetSeed();
  fPythia->readString("Random:setSeed = on");
  fPythia->settings.mode("Random:seed", seed);

from Costas' first attempt at supporting Pythia8, in src/Physics/Hadronization/Pythia8Hadro2019.cxx circa R-3_04_00.
Which is why the random seed is initialized in the algorithm. I'm not sure why it was done this way, but I blindly followed it.

But it would make sense to also have it in RandomGen::SetSeed() (around lines 22, 115) similarly to how the pythia6 seed is set .

#ifdef __GENIE_PYTHIA8_ENABLED__
#include "Framework/Utils/Pythia8Singleton.h"
#endif
...
#ifdef __GENIE_PYTHIA8_ENABLED__
    Pythia8::Pythia* gPythia = Pythia8Singleton::Instance()->Pythia8();
    gPythia->readString("Random:setSeed = on");
    gPythia->settings.mode("Random:seed", seed);
    gPythia->init();
#endif

One probably needs init() there to ensure that it was picked up in case where the tune was constructed prior to the SetSeed() scenarios 3-5. Even if we fix the order in GENIE supplied apps, that doesn't guarantee that user generated alternative applications won't have it the same as they used to be. With this addition, it might make sense to remove the Random:[set]Seed lines from the 3 algorithms. Those algorithms also set other things, so I think those must stay with their accompanying init()

@nusense

nusense commented Sep 9, 2026

Copy link
Copy Markdown
Member

Hmm. I may have remembered why the direct Pythia8 call wasn't in RandomGen. I think this introduces a circular dependency, where Framework/Numerical/RandomGen now depends on Framework/Utils/Pythia8Singleton, but various classes in Framework/Utils rely on a number of classes in Framework/Numerical. [sigh].

@nusense

nusense commented Sep 9, 2026

Copy link
Copy Markdown
Member

Is RandomGen a "Numerical" thing, or a "Util" thing :-) We could consider moving it -- with lots of changes to header include lines everywhere. That's probably too big a "lift" as it might also affect user code.

@kjplows

kjplows commented Sep 9, 2026

Copy link
Copy Markdown
Contributor Author

Yep moving RandomGen seems quite a heavy lift indeed!

So, I think that regardless of coverage (seed instantiation vs tune configuration), having some synchronisation of the seed is better than having none, and that seems to be resolved without adding the calls to the RandomGen code. So, perhaps a middle ground is to keep the c'tor with seed, and not explicitly call init() to Pythia8 in RandomGen? 🤔

@nusense

nusense commented Sep 10, 2026

Copy link
Copy Markdown
Member

I think without the call to init() in RandomGen there's not guarantee that init() will be called appropriately if the tune is created before the utils::app_init::RandGen(seed) is called in the app. And then it sits in the settings database but isn't actually seeded into the Pythia RNG.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants