Skip to content

ZDC: replace legacy fast simulation with an ONNX channel-response model - #16147

Open
Fuligno wants to merge 3 commits into
AliceO2Group:devfrom
Fuligno:zdc-fastsim-pr
Open

Fuligno wants to merge 3 commits into
AliceO2Group:devfrom
Fuligno:zdc-fastsim-pr

Conversation

@Fuligno

@Fuligno Fuligno commented Nov 17, 2026

Copy link
Copy Markdown

Generate ZN and ZP photon counts from primary nucleons using an ONNX model packaged with its configuration in a ROOT/CCDB object. Add an end-of-event detector hook and integrate the generated hits with the existing ZDC simulation interface.

The code was written with assistance from Claude Fable 5.2.

@Fuligno
Fuligno marked this pull request as ready for review November 18, 2026 08:39
@Fuligno
Fuligno requested review from a team, coppedis, sawenzel and shahor02 as code owners November 18, 2026 08:39

@fcatalan92 fcatalan92 left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hi @Fuligno, the PR looks fine to me! I have a couple of suggestions:

  • maybe it is worth adding a README in ZDC/simulation explaining how the fast simulation works and how to turn it on
  • did you test this code in a simulation with also the other detectors (and not only the ZDC)? This would be nice to check

Generate ZN and ZP photon counts from primary nucleons using an ONNX model packaged with its configuration in a ROOT/CCDB object. Add an end-of-event detector hook and integrate the generated hits with the existing ZDC simulation interface.

The code was written with assistance from Claude Fable 5.2.
@sawenzel

Copy link
Copy Markdown
Collaborator

Thanks, this is a clear improvement over the old fast simulation. I have one point about reproducibility.

First, some background. O2 has a mode in which the random numbers of each track are independent of everything else in the event. It is switched on with SimCutParams.trackSeed=true. In this mode, the random generator is reset at the start of every track, with a seed computed from the track itself: a hash of its start vertex, momentum and PDG code. The hash function is getHash() in Detectors/Base/src/Stack.cxx, and the reseeding happens in Stack::PopNextTrack() for Geant3 and in O2MCApplicationBase::PreTrack() for Geant4:

// calculates a hash based on particle properties
// hash may serve as seed for this track
ULong_t getHash(TParticle const& p)
{
auto asLong = [](double x) {
return (ULong_t) * (reinterpret_cast<ULong_t*>(&x));
};
ULong_t hash;
o2::MCTrackT<double> track(p);
hash = asLong(track.GetStartVertexCoordinatesX());
hash ^= asLong(track.GetStartVertexCoordinatesY());
hash ^= asLong(track.GetStartVertexCoordinatesZ());
hash ^= asLong(track.GetStartVertexCoordinatesT());
hash ^= asLong(track.GetStartVertexMomentumX());
hash ^= asLong(track.GetStartVertexMomentumY());
hash ^= asLong(track.GetStartVertexMomentumZ());
hash += (ULong_t)track.GetPdgCode();
return hash;
}

void O2MCApplicationBase::PreTrack()
{
if (auto& beginTrack = o2::base::MTWorkerHooks::instance().beginTrack) {
beginTrack();
}
if (mCutParams.trackSeed && seedsInPreTrack()) {
// Per-track seeding for engines that do not go through
// o2::data::Stack::PopNextTrack(). Geant4 is one: it takes primaries via
// PopPrimaryForTracking and keeps secondaries internally, so the stack hook
// never fires and this is the only per-track hook available. It is called
// for primaries and secondaries alike
// (TG4TrackingAction::PreUserTrackingAction), and only on a track's first
// step, so a suspended track is not reseeded mid-flight.
auto hash = hashCurrentTrack(fMC);
// TRandom::SetSeed(0) means "seed from the clock" -- never let that happen.
o2::base::simRandom()->SetSeed(hash == 0 ? 1 : hash);
o2::base::VMCSeederService::instance().setSeed();
}
// dispatch now to function in FairRoot
FairMCApplication::PreTrack();

As a result, a given particle gives the same hits no matter which other particles are in the event, and no matter how the event is split into sub-events for the workers.

The fast simulation does not follow this rule yet. The noise for the model is drawn from simRandom() in the end-of-event hook, that is, after the whole sub-event has been transported. At that moment, the state of the generator is whatever the last transported track left behind. Moreover, the primaries take their noise one after the other from the same sequence, so the noise of one primary depends on which primaries come before it in the batch. Therefore, the ZDC signal of a given spectator changes when the event is split differently, for example with another chunk size, even with the same seed and with trackSeed enabled. This also means that the README sentence "reproducible with the seed" holds only for a fixed chunking.

The fix is small and only needed when trackSeed is enabled. In that case, before drawing the noise of a primary, you could reseed the generator from that primary, for example with the same getHash(). That function is currently local to Stack.cxx, so it would need to be exposed, for instance as a static method of Stack. This keeps the batched inference unchanged: you only draw the noise row by row, reseeding before each row. Then, in this mode, the output of each spectator depends only on the spectator itself, as for the transported tracks. Without trackSeed the current behaviour is fine, and the README could simply say that the result is reproducible for the same seed and the same chunking.

This branch has not been deployed

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

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants