BUG with G4FORCE_GRAINSIZE and BUG with integer overflow in G4RNGHelper::GetSeed()

this is linked to the error reported here: Seed number error

I got the G4Exception : Run0115 in my simulation and was able to reproduce it with a standard example.

System: “Debian GNU/Linux 12 (bookworm)”
example basic B1

run 1

modify run1.mac to r1.mac (no eventModulo) r1_mac.txt (540 Bytes)

env G4FORCE_GRAINSIZE=64 ./exampleB1 r1.mac >file1.log 2>file1.err

→ G4FORCE_GRAINSIZE=64 does not work

run 2

modify run1.mac to r2.mac (too large eventModulo second parameter=0) r2_mac.txt (539 Bytes)

env G4FORCE_GRAINSIZE=64 ./exampleB1 r2.mac >file2.log 2>file2.err


In G4TaskRunManager::ComputeNumberOfTasks() in G4TaskRunManager.cc at line 334 only if eventModulo > nEvtsPerTask the effect of G4FORCE_GRAINSIZE is realized!

Same if second parameter=1 in /run/eventModulo

run 3

modify run1.mac to r3.mac (too large eventModulo second parameter=0 and large number of events) r3_mac.txt (542 Bytes)

env G4FORCE_GRAINSIZE=64 ./exampleB1 r3.mac >file3.log 2>file3.err

→ but ERROR:

G4WT4 > 
-------- EEEE ------- G4Exception-START -------- EEEE -------
*** G4Exception : Run0115
      issued by : G4RNGHelper::GetSeed
No seed number 15464(15464 available)
 Original seed number -311832 filled so far 2147320000
*** Fatal Exception *** core dump ***
G4WT4 >  **** Track information is not available at this moment
G4WT4 >  **** Step information is not available at this moment
G4WT4 > 
-------- EEEE -------- G4Exception-END --------- EEEE -------

G4WT4 > 
G4WT4 > *** G4Exception: Aborting execution ***

Similar behavior was posted here:
Seed number error

I think it is cause due to an integer overflow in G4TaskRunManager::SetUpNEvents in line 702

seedsQueue->push(helper->GetSeed(nSeedsPerEvent * nSeedsUsed));

In G4TaskRunManager.cc in line 702:
nSeedsPerEvent * nSeedsUsed = 2 * 2147327732 = 4294655464

In G4RNGHelper.hh in line 66:
G4int seedId = sdId - 2 * offset;

=> seedId=15464
=> offset=2147320000
=> sdId=-311832 here is the integer overflow !

Due to 4294655464 which is
bin(4294655464)=0b11111111111110110011110111101000 which becomes int32(0b11111111111110110011110111101000)=-311832

This integer overflow happens despite the number of events is /run/beamOn 2147327732 is less than 2**31 !

run 4

modify run1.mac to r4.mac (too large eventModulo second parameter=1 and large number of events) r4_mac.txt (542 Bytes)

env G4FORCE_GRAINSIZE=64 ./exampleB1 r4.mac >file4.log 2>file4.err

→ but simulation get stucked with message from each thread like this:

G4WT38 > Applying command " " @ ProcessOneEvent:251

Best regards