Getting Repeated Or Not Correct Step Numbers In Volume When Using SteppingAction

Hi There,

I am trying to count the number of steps taken inside a volume and I have come across a very strange error where after a certain number of steps taken for a given track, the step number suddenly goes to 1 and begins to count up again.
The conditions I have set in my SteppingAction are as follows:

   //check if particle is entering the cavity
  if (namePreStepPoint=="cavity" && aStep->IsFirstStepInVolume()){ 

  firstStepNumber = aStep->GetTrack()->GetCurrentStepNumber();

  G4cout << "Enters Cavity with initial step number = " << firstStepNumber << G4endl;

  //Set in cavity condition to true
  InCavity = true;

  //Get Track IDs
  trackID += 1;

}

//If the particle is in the cavity
if (InCavity){
  //Check current step number and only enter if the current step is greater than or equal to the first step
  currentStep = aStep->GetTrack()->GetCurrentStepNumber();
  G4cout << "For TrackID = " << trackID << ", the current step number in the Cavity = " << currentStep << G4endl;

  // Check if the step number is less than the first one
  // The code is picking this up sometimes and sometimes it is not because the initial step number is 1 so the step can never be less than one.
  if (currentStep < firstStepNumber){

    G4cout << "The current step  = " << currentStep << ", but the first step in the volume was = " << firstStepNumber << G4endl;
    G4cout << "This step number is lower than the first!!" << G4endl;

  } 
}

//If the step is last in volume, return InCavity parameter to false
if (InCavity && aStep->IsLastStepInVolume()){
    
  G4cout << "Passed Into Exit Condition with step number = " << currentStep << G4endl;

  //Set InCavity Condition to false and also reset trackLengthInCavity and StepNumber to zero for next track
  InCavity = false;

}

The output for this seems normal for a few tracks and then becomes:

Enters Cavity with initial step number = 26
G4WT0 > For TrackID = 7, the current step number in the Cavity = 26
G4WT0 > For TrackID = 7, the current step number in the Cavity = 27
G4WT0 > For TrackID = 7, the current step number in the Cavity = 28
G4WT0 > For TrackID = 7, the current step number in the Cavity = 29
G4WT0 > For TrackID = 7, the current step number in the Cavity = 30
G4WT0 > For TrackID = 7, the current step number in the Cavity = 31
G4WT0 > For TrackID = 7, the current step number in the Cavity = 32
G4WT0 > For TrackID = 7, the current step number in the Cavity = 33
G4WT0 > For TrackID = 7, the current step number in the Cavity = 34
G4WT0 > For TrackID = 7, the current step number in the Cavity = 35
G4WT0 > For TrackID = 7, the current step number in the Cavity = 36
G4WT0 > For TrackID = 7, the current step number in the Cavity = 37
G4WT0 > For TrackID = 7, the current step number in the Cavity = 38
G4WT0 > For TrackID = 7, the current step number in the Cavity = 39
G4WT0 > For TrackID = 7, the current step number in the Cavity = 40
G4WT0 > For TrackID = 7, the current step number in the Cavity = 41
G4WT0 > For TrackID = 7, the current step number in the Cavity = 42
G4WT0 > For TrackID = 7, the current step number in the Cavity = 43
G4WT0 > For TrackID = 7, the current step number in the Cavity = 44
G4WT0 > For TrackID = 7, the current step number in the Cavity = 45
G4WT0 > For TrackID = 7, the current step number in the Cavity = 46
G4WT0 > For TrackID = 7, the current step number in the Cavity = 47
G4WT0 > For TrackID = 7, the current step number in the Cavity = 48
G4WT0 > For TrackID = 7, the current step number in the Cavity = 49
G4WT0 > For TrackID = 7, the current step number in the Cavity = 50
G4WT0 > For TrackID = 7, the current step number in the Cavity = 51
G4WT0 > For TrackID = 7, the current step number in the Cavity = 52
G4WT0 > For TrackID = 7, the current step number in the Cavity = 53
G4WT0 > For TrackID = 7, the current step number in the Cavity = 54
G4WT0 > For TrackID = 7, the current step number in the Cavity = 55
G4WT0 > For TrackID = 7, the current step number in the Cavity = 56
G4WT0 > For TrackID = 7, the current step number in the Cavity = 57
G4WT0 > For TrackID = 7, the current step number in the Cavity = 58
G4WT0 > For TrackID = 7, the current step number in the Cavity = 59
G4WT0 > For TrackID = 7, the current step number in the Cavity = 60
G4WT0 > For TrackID = 7, the current step number in the Cavity = 61
G4WT0 > For TrackID = 7, the current step number in the Cavity = 62
G4WT0 > For TrackID = 7, the current step number in the Cavity = 63
G4WT0 > For TrackID = 7, the current step number in the Cavity = 64
G4WT0 > For TrackID = 7, the current step number in the Cavity = 65
G4WT0 > For TrackID = 7, the current step number in the Cavity = 66
G4WT0 > For TrackID = 7, the current step number in the Cavity = 67
G4WT0 > For TrackID = 7, the current step number in the Cavity = 68
G4WT0 > For TrackID = 7, the current step number in the Cavity = 69
G4WT0 > For TrackID = 7, the current step number in the Cavity = 70
G4WT0 > For TrackID = 7, the current step number in the Cavity = 71
G4WT0 > For TrackID = 7, the current step number in the Cavity = 1
G4WT0 > The current step = 1, but the first step in the volume was = 26
G4WT0 > This step number is lower than the first!!
G4WT0 > For TrackID = 7, the current step number in the Cavity = 2
G4WT0 > The current step = 2, but the first step in the volume was = 26
G4WT0 > This step number is lower than the first!!
G4WT0 > For TrackID = 7, the current step number in the Cavity = 3
G4WT0 > The current step = 3, but the first step in the volume was = 26
G4WT0 > This step number is lower than the first!!
G4WT0 > For TrackID = 7, the current step number in the Cavity = 4
G4WT0 > The current step = 4, but the first step in the volume was = 26
G4WT0 > This step number is lower than the first!!
G4WT0 > For TrackID = 7, the current step number in the Cavity = 5
G4WT0 > The current step = 5, but the first step in the volume was = 26
G4WT0 > This step number is lower than the first!!
G4WT0 > For TrackID = 7, the current step number in the Cavity = 6
G4WT0 > The current step = 6, but the first step in the volume was = 26
G4WT0 > This step number is lower than the first!!
G4WT0 > For TrackID = 7, the current step number in the Cavity = 7
G4WT0 > The current step = 7, but the first step in the volume was = 26
G4WT0 > This step number is lower than the first!!
G4WT0 > For TrackID = 7, the current step number in the Cavity = 8
G4WT0 > The current step = 8, but the first step in the volume was = 26
G4WT0 > This step number is lower than the first!!

OR even stranger is the following:

G4WT0 > Enters Cavity with initial step number = 1
G4WT0 > For TrackID = 14, the current step number in the Cavity = 1
G4WT0 > For TrackID = 14, the current step number in the Cavity = 2
G4WT0 > For TrackID = 14, the current step number in the Cavity = 1
G4WT0 > For TrackID = 14, the current step number in the Cavity = 2
G4WT0 > For TrackID = 14, the current step number in the Cavity = 3
G4WT0 > For TrackID = 14, the current step number in the Cavity = 4
G4WT0 > For TrackID = 14, the current step number in the Cavity = 5
G4WT0 > For TrackID = 14, the current step number in the Cavity = 6
G4WT0 > For TrackID = 14, the current step number in the Cavity = 7
G4WT0 > For TrackID = 14, the current step number in the Cavity = 8
G4WT0 > For TrackID = 14, the current step number in the Cavity = 1
G4WT0 > For TrackID = 14, the current step number in the Cavity = 2
G4WT0 > For TrackID = 14, the current step number in the Cavity = 3
G4WT0 > For TrackID = 14, the current step number in the Cavity = 4
G4WT0 > For TrackID = 14, the current step number in the Cavity = 5
G4WT0 > For TrackID = 14, the current step number in the Cavity = 6
G4WT0 > For TrackID = 14, the current step number in the Cavity = 7
G4WT0 > For TrackID = 14, the current step number in the Cavity = 8
G4WT0 > For TrackID = 14, the current step number in the Cavity = 1
G4WT0 > For TrackID = 14, the current step number in the Cavity = 2
G4WT0 > For TrackID = 14, the current step number in the Cavity = 3
G4WT0 > For TrackID = 14, the current step number in the Cavity = 4
G4WT0 > For TrackID = 14, the current step number in the Cavity = 5
G4WT0 > For TrackID = 14, the current step number in the Cavity = 6
G4WT0 > For TrackID = 14, the current step number in the Cavity = 7
G4WT0 > For TrackID = 14, the current step number in the Cavity = 1

Where it just loops over the same few steps.

I am not an expert in C++ and I was wondering if anyone can see anything wrong with the code I have used and I am missing something simple. I am not running the simulation in MT mode so I shouldn’t have a problem with the code counting incrementally through the steps.

Thanks in advance,

Michael

Instead of trying to compute a track ID yourself, what happens if you access the real track ID, using trackID = aStep->GetTrack()->GetTrackID()`?

If you’re getting step #1 after step #71, it’s certainly a new track.

In G4, there is no “looping over the same steps.” The G4Step object is a temporary, transient thing. Once the step is completed, the object is deleted and a new one created for the next step. The same is true of tracks: once a track is killed, the object is deleted and a new one created. The only record of past tracks are the use of their trackID values as parentID of secondaries.

Hi, thanks for the reply. Using the real track ID shows that it is indeed a new track, but if anything, it makes the results even harder to understand as there will be multiple TrackID = 1 events entering the cavity since it is the first track for a given parent particle.
For example:
Enters Cavity with initial step number = 215
G4WT0 > For TrackID = 1, the current step number in the Cavity = 215
G4WT0 > For TrackID = 1, the current step number in the Cavity = 216
G4WT0 > For TrackID = 1, the current step number in the Cavity = 217
G4WT0 > For TrackID = 1, the current step number in the Cavity = 218
G4WT0 > For TrackID = 1, the current step number in the Cavity = 219
G4WT0 > For TrackID = 1, the current step number in the Cavity = 220
G4WT0 > For TrackID = 1, the current step number in the Cavity = 221
G4WT0 > For TrackID = 1, the current step number in the Cavity = 222
G4WT0 > For TrackID = 1, the current step number in the Cavity = 223
G4WT0 > For TrackID = 1, the current step number in the Cavity = 224
G4WT0 > For TrackID = 1, the current step number in the Cavity = 225
G4WT0 > For TrackID = 1, the current step number in the Cavity = 226
G4WT0 > For TrackID = 1, the current step number in the Cavity = 227
G4WT0 > Passed Into Exit Condition with step number = 227
G4WT0 > Enters Cavity with initial step number = 180
G4WT0 > For TrackID = 1, the current step number in the Cavity = 180
G4WT0 > For TrackID = 1, the current step number in the Cavity = 181
G4WT0 > For TrackID = 1, the current step number in the Cavity = 182
G4WT0 > For TrackID = 1, the current step number in the Cavity = 183
G4WT0 > For TrackID = 1, the current step number in the Cavity = 184
G4WT0 > For TrackID = 1, the current step number in the Cavity = 185
G4WT0 > For TrackID = 1, the current step number in the Cavity = 186
G4WT0 > For TrackID = 1, the current step number in the Cavity = 187
G4WT0 > For TrackID = 1, the current step number in the Cavity = 188
G4WT0 > For TrackID = 1, the current step number in the Cavity = 189
G4WT0 > Passed Into Exit Condition with step number = 189
G4WT0 > Enters Cavity with initial step number = 211
G4WT0 > For TrackID = 1, the current step number in the Cavity = 211
G4WT0 > For TrackID = 1, the current step number in the Cavity = 212
G4WT0 > For TrackID = 1, the current step number in the Cavity = 213
G4WT0 > For TrackID = 1, the current step number in the Cavity = 214
G4WT0 > For TrackID = 1, the current step number in the Cavity = 215
G4WT0 > For TrackID = 1, the current step number in the Cavity = 216
G4WT0 > For TrackID = 1, the current step number in the Cavity = 217
G4WT0 > For TrackID = 1, the current step number in the Cavity = 218
G4WT0 > For TrackID = 1, the current step number in the Cavity = 219
G4WT0 > For TrackID = 1, the current step number in the Cavity = 220
G4WT0 > For TrackID = 1, the current step number in the Cavity = 221
G4WT0 > For TrackID = 1, the current step number in the Cavity = 222
G4WT0 > For TrackID = 1, the current step number in the Cavity = 223
G4WT0 > For TrackID = 1, the current step number in the Cavity = 224
G4WT0 > For TrackID = 1, the current step number in the Cavity = 225
G4WT0 > For TrackID = 1, the current step number in the Cavity = 226
G4WT0 > For TrackID = 1, the current step number in the Cavity = 227
G4WT0 > For TrackID = 1, the current step number in the Cavity = 228
G4WT0 > For TrackID = 1, the current step number in the Cavity = 229
G4WT0 > For TrackID = 1, the current step number in the Cavity = 230
G4WT0 > For TrackID = 1, the current step number in the Cavity = 231
G4WT0 > For TrackID = 1, the current step number in the Cavity = 232
G4WT0 > For TrackID = 1, the current step number in the Cavity = 233
G4WT0 > For TrackID = 1, the current step number in the Cavity = 234
G4WT0 > For TrackID = 1, the current step number in the Cavity = 235
G4WT0 > For TrackID = 1, the current step number in the Cavity = 236
G4WT0 > For TrackID = 1, the current step number in the Cavity = 237
G4WT0 > For TrackID = 1, the current step number in the Cavity = 238
G4WT0 > For TrackID = 1, the current step number in the Cavity = 239
G4WT0 > For TrackID = 1, the current step number in the Cavity = 240
G4WT0 > For TrackID = 1, the current step number in the Cavity = 241
G4WT0 > For TrackID = 1, the current step number in the Cavity = 242
G4WT0 > For TrackID = 1, the current step number in the Cavity = 243
G4WT0 > For TrackID = 1, the current step number in the Cavity = 244
G4WT0 > For TrackID = 1, the current step number in the Cavity = 245
G4WT0 > For TrackID = 1, the current step number in the Cavity = 246
G4WT0 > For TrackID = 1, the current step number in the Cavity = 247
G4WT0 > For TrackID = 1, the current step number in the Cavity = 248
G4WT0 > For TrackID = 1, the current step number in the Cavity = 249
G4WT0 > For TrackID = 1, the current step number in the Cavity = 250
G4WT0 > For TrackID = 1, the current step number in the Cavity = 251
G4WT0 > For TrackID = 1, the current step number in the Cavity = 252
G4WT0 > For TrackID = 1, the current step number in the Cavity = 253
G4WT0 > Passed Into Exit Condition with step number = 253

This is a part of the new output using actual trackID. I am still running into the problem where the last step in the cavity is not registering in thee exit condition so I get an output like:
G4WT0 > For TrackID = 145, the current step number in the Cavity = 129
G4WT0 > For TrackID = 145, the current step number in the Cavity = 130
G4WT0 > For TrackID = 145, the current step number in the Cavity = 131
G4WT0 > This particle has reached 10 keV in the cavity and is being killed
G4WT0 > For TrackID = 144, the current step number in the Cavity = 1
G4WT0 > The current step = 1, but the first step in the volume was = 29
G4WT0 > This step number is lower than the first!!
G4WT0 > For TrackID = 144, the current step number in the Cavity = 2
G4WT0 > The current step = 2, but the first step in the volume was = 29
G4WT0 > This step number is lower than the first!!
G4WT0 > For TrackID = 144, the current step number in the Cavity = 3
G4WT0 > The current step = 3, but the first step in the volume was = 29
G4WT0 > This step number is lower than the first!!
G4WT0 > This particle has reached 10 keV in the cavity and is being killed
G4WT0 > For TrackID = 143, the current step number in the Cavity = 1
G4WT0 > The current step = 1, but the first step in the volume was = 29
G4WT0 > This step number is lower than the first!!
G4WT0 > For TrackID = 143, the current step number in the Cavity = 2
G4WT0 > The current step = 2, but the first step in the volume was = 29
G4WT0 > This step number is lower than the first!!
G4WT0 > For TrackID = 143, the current step number in the Cavity = 3
G4WT0 > The current step = 3, but the first step in the volume was = 29
G4WT0 > This step number is lower than the first!!
G4WT0 > For TrackID = 143, the current step number in the Cavity = 4
G4WT0 > The current step = 4, but the first step in the volume was = 29
G4WT0 > This step number is lower than the first!!
G4WT0 > For TrackID = 143, the current step number in the Cavity = 5
G4WT0 > The current step = 5, but the first step in the volume was = 29
G4WT0 > This step number is lower than the first!!
G4WT0 > For TrackID = 143, the current step number in the Cavity = 6
G4WT0 > The current step = 6, but the first step in the volume was = 29
G4WT0 > This step number is lower than the first!!
G4WT0 > For TrackID = 143, the current step number in the Cavity = 7
G4WT0 > The current step = 7, but the first step in the volume was = 29
G4WT0 > This step number is lower than the first!!
G4WT0 > This particle has reached 10 keV in the cavity and is being killed
G4WT0 > For TrackID = 151, the current step number in the Cavity = 1
G4WT0 > The current step = 1, but the first step in the volume was = 29
G4WT0 > This step number is lower than the first!!
G4WT0 > For TrackID = 151, the current step number in the Cavity = 2
G4WT0 > The current step = 2, but the first step in the volume was = 29
G4WT0 > This step number is lower than the first!!

Where the new trackID is still someone referencing the first step number of the previous track.
G4WT0 > Enters Cavity with initial step number = 1
G4WT0 > For TrackID = 123, the current step number in the Cavity = 1
G4WT0 > For TrackID = 123, the current step number in the Cavity = 2
G4WT0 > For TrackID = 122, the current step number in the Cavity = 1
G4WT0 > For TrackID = 122, the current step number in the Cavity = 2
G4WT0 > For TrackID = 122, the current step number in the Cavity = 3
G4WT0 > For TrackID = 121, the current step number in the Cavity = 1
G4WT0 > For TrackID = 121, the current step number in the Cavity = 2
G4WT0 > For TrackID = 121, the current step number in the Cavity = 3
G4WT0 > For TrackID = 120, the current step number in the Cavity = 1
G4WT0 > For TrackID = 120, the current step number in the Cavity = 2
G4WT0 > For TrackID = 120, the current step number in the Cavity = 3
G4WT0 > For TrackID = 119, the current step number in the Cavity = 1
G4WT0 > For TrackID = 119, the current step number in the Cavity = 2
G4WT0 > For TrackID = 119, the current step number in the Cavity = 3
G4WT0 > For TrackID = 118, the current step number in the Cavity = 1
G4WT0 > For TrackID = 118, the current step number in the Cavity = 2
G4WT0 > For TrackID = 118, the current step number in the Cavity = 3
G4WT0 > For TrackID = 118, the current step number in the Cavity = 4
G4WT0 > For TrackID = 117, the current step number in the Cavity = 1
G4WT0 > For TrackID = 117, the current step number in the Cavity = 2
G4WT0 > For TrackID = 117, the current step number in the Cavity = 3
G4WT0 > For TrackID = 117, the current step number in the Cavity = 4
G4WT0 > For TrackID = 116, the current step number in the Cavity = 1
G4WT0 > For TrackID = 116, the current step number in the Cavity = 2
G4WT0 > For TrackID = 115, the current step number in the Cavity = 1
G4WT0 > For TrackID = 115, the current step number in the Cavity = 2
G4WT0 > For TrackID = 115, the current step number in the Cavity = 3
G4WT0 > For TrackID = 115, the current step number in the Cavity = 4
G4WT0 > For TrackID = 115, the current step number in the Cavity = 5
G4WT0 > For TrackID = 115, the current step number in the Cavity = 6
G4WT0 > For TrackID = 114, the current step number in the Cavity = 1
G4WT0 > For TrackID = 114, the current step number in the Cavity = 2
G4WT0 > For TrackID = 114, the current step number in the Cavity = 3
G4WT0 > For TrackID = 113, the current step number in the Cavity = 1
G4WT0 > For TrackID = 113, the current step number in the Cavity = 2
G4WT0 > For TrackID = 113, the current step number in the Cavity = 3
G4WT0 > For TrackID = 112, the current step number in the Cavity = 1
G4WT0 > For TrackID = 112, the current step number in the Cavity = 2
G4WT0 > For TrackID = 111, the current step number in the Cavity = 1
G4WT0 > For TrackID = 111, the current step number in the Cavity = 2
G4WT0 > For TrackID = 110, the current step number in the Cavity = 1
G4WT0 > For TrackID = 110, the current step number in the Cavity = 2
G4WT0 > For TrackID = 110, the current step number in the Cavity = 3
G4WT0 > For TrackID = 110, the current step number in the Cavity = 4

So I am seeing the same step numbers appearing for lots of trackIDs.
Do you see any problem with how I have set my entry and exit conditions in the code above?

Thanks again!

Every track starts with step #1 (actually, it starts with step #0, but you’ll never see that step in your own code). The step counting applies to the current track.

Don’t cache step numbers or track numbers, or try to “compute” them yourself. Use the accessor functions to get the indices assigned by the G4 tracking code.

The IsFirstStepInVolume() and IsLastStepInVolume() flags should be fine to use, but keep in mind that those are “local” flags, not global to the track’s whole trajectory. Particles can scatter around outside your volume of interest and re-enter the volume. That will still count, as far as tracking is concerned, as the “first step in the volume”.

Hi,

So I think what I was missing was I wasn’t accounting for particles which had the status fStopAndKill in the volume, and in this case IsLastStepInVolume() was not returning true and therefore the track would not enter the exit condition.

For the case you describe where a track leaves the volume and then returns, this should be taken into account by the code, as the step with which the track leaves will return true for IsLastStepInVolume(), and then any subsequent step which enters the cavity again will trigger the first Enter condition again.

I hope that makes sense,

Thanks again for your help!

Ah, ha! Yeah, that would confuse your code. If you wanted to, you could keep a cached (static G4ThreadLocal or class data member) value for the track ID, and if the step has a different track ID, reset all those flags of yours.

That would work for normal physics, but it won’t work if you’re using Cherenkov processes, where tracks get paused and resumed.