Unable to Print Detector index

@mkelsey I am not using ConstructSDandField() function. I modified B1 example ,

auto solidBox = new G4Box(“Sphere”, x = 0.5m, y = 1.0m, z = 1.5*m);
auto logicBox = new G4LogicalVolume(solidBox, mat, “Box”);

fScoringVolume = logicBox;
return physWorld;
}

fScoringVolume = logicBox ; defined in construction.cc

If you have defined a sensitive detector, then you must be attaching your sensitive detector to your scoring volume. If you aren’t, then what volume are you attaching the SD to?

@mkelsey In my detector construction I only made boxes. And, I attached logicBox (which is my LV) as my ScoringVolume. There is no volume other than boxes.

I just want to count the scattered events (which scattered from one LV to another LV). So for a particular event id I must have different detector id.

since you are using the user stepping action, this method is called for every single step.
so also for the step where particles are leaving the world volume, and also for the initial step placing the particle into the world. this could be the -1 and 0 prints!?

i did not see any filtering for the fScoringVolume in your usersteppingaction. maybe this would help to make further progress?

@weller I just want to count the scattered events (which scattered from one LV to another LV). So for a particular event id I must have different detector id.
How should I approach then?

I personally prefer to use sensitive detector instead of usersteppingaction, because then you don’t need to discard all the steps
that are not within the volumes that you attached the SD to.

since you want to only record each volume
once, get rid of the duplicates from multiple steps in the same volume, e.g. like so: Multiple Steps on geometricBoundary

you would need to keep track of the copyID of the visited volumes somehow. you could use the „hitsmap“ for this, and then process the map at the end of each event to check for multiple entries.
inspiration can be found here: geant4/EventAction.cc at v11.1.1 · Geant4/geant4 · GitHub

1 Like

Geant4 is flexible. You could do this in several different ways. You get to choose which method you like best, and then implement it.

  1. Create a proper SensitiveDetector class, attach it to your “scoring volume” in the ConstructSDandField() function of your detector construction class. That will guarantee that only steps within your desired volume(s) get counted.

  2. Use the UserSteppingAction, but include a test yourself on whether the volume associated with the step (with the PostStepPoint) is the volume you care about. That’s what you’re missing in your current code.

  3. Use Geant4’s built in scorers, which you can configure with macro commands. I believe that you can attach scorers to specific volumes, and request that specific quantities be accumulated. You won’t get an N-tuple, and you won’t be able to analyze cross correlations, but you will get the spectra in each volume.

1 Like

After get rid of the duplicates from multiple steps in the same volume I am getting :
Event id: 9994 detector id: 306
Event id: 9995 detector id: 407
Event id: 9995 detector id: 402
Event id: 9995 detector id: 402
Here for Event No 9995 I have two different detector id. But value of energy deposition in each volume gets changed for a given event which is obvious. But @weller I noticed there is a repetition of Event id and detector id for many cases . Why?

Thanks a lot @mkelsey . Now I understand why I must use proper SensitiveDetector class.

How close together are your individual detector volumes, and what material is between them? How likely is it to get scattering from A to B and back to A?

Yes there is a possibility to get scattering from A to B and back to A. Material used here is Germanium. But I can see the repetition happens other than scattered events also. This repetition coming not more than twice, like
Event id: 99390 detector id: 302
Event id: 99390 detector id: 302
But I stopped the multiple steps in the same volume.
@mkelsey If there is a scattering from A to B and back to A then I shouldn’t get
Event id: 99953 detector id: 106
Event id: 99953 detector id: 107
Event id: 99953 detector id: 107
I should get
Event id: 99953 detector id: 106
Event id: 99953 detector id: 107
Event id: 99953 detector id: 106
Am I correct?

that would be correct.

duplicate hits could be that secondaries are produced and also hit a/the SD.
I think secondaries should have a distinct trackID, print that as well and see if it clarifies.

@weller I printed trackID and got 1 for all events
Event id: 99830 detector id: 106 trackid: 1
Event id: 99830 detector id: 105 trackid: 1
Event id: 99830 detector id: 105 trackid: 1

I added the line to get the trackID :
G4Track *track = step->GetTrack();
G4int trackid = track->GetTrackID();

in this case that could be the same bug as in the linked post. try filtering also for IsFirstStepInVolume, as suggested in Multiple Steps on geometricBoundary

After attaching the lines as you @weller suggested I got
Debug: StepStatus: 1 IsFirstStepInVolume: 1
Debug: StepStatus: 1 IsFirstStepInVolume: 1
Debug: StepStatus: 1 IsFirstStepInVolume: 1
Debug: StepStatus: 1 IsFirstStepInVolume: 1
Debug: StepStatus: 1 IsFirstStepInVolume: 1
Debug: StepStatus: 1 IsFirstStepInVolume: 1
Debug: StepStatus: 1 IsFirstStepInVolume: 1

so, still duplicate entries of detector id?

yes! @weller still I am getting duplicate entries of detector id.
Debug: StepStatus: 1 IsFirstStepInVolume: 1
Event id: 9998 detector id: 504 trackid: 1
Debug: StepStatus: 1 IsFirstStepInVolume: 1
Event id: 9998 detector id: 404 trackid: 1
Debug: StepStatus: 1 IsFirstStepInVolume: 1
Event id: 9998 detector id: 404 trackid: 1

ok, so it appears that the same track is actually leaving and re-entering the same volume again, and that is happening frequently.

what kind of particles are we talking about? can you identify that in the stepping action? I could imagine optical photons being reflected on the surface being counted twice.

@weller I am using gamma for the simulation.
I actually don’t know how to identify particle in the stepping action. If you kindly suggest how to obtain that would be great. I added the following lines
if (track->GetDefinition() == G4Electron::Definition()) { G4cout << " got an electron!" << G4endl;}
if (track->GetDefinition() == G4Gamma::Definition()) { G4cout << " got a photon!" << G4endl;}

Now I got

Event id: 99945 detector id: 401 trackid: 1
got a photon!
Event id: 99946 detector id: 104 trackid: 1
got a photon!
Event id: 99946 detector id: 103 trackid: 1
got a photon!
Event id: 99946 detector id: 103 trackid: 1
got a photon!
Event id: 99948 detector id: 603 trackid: 1
got a photon!

I could see Event id 99947 also missing ?

I think you are making good progress in understanding what is going on. instead of comparing the definition, you could also simply print the definition directly, but what you did is perfectly fine here.
since it’s gamma also for the duplicates, I think the most reasonable would be to assume it is indeed re-entry. you could verify with verbose output for tracking, but depending on how often that phenomenon occurs, it could be a lot of output

1 Like