But I am unable to print my detector index .
I did the following :
const G4VTouchable *touchable = step->GetPreStepPoint()->GetTouchable();
G4int copyNo = touchable->GetCopyNumber();
G4cout << "Copy number: " << copyNo << G4endl;
In both cases, have you printed out the volume name (both the PV and LV)? With an SD, the post-step should always and only be the LV where you’ve attached the SD. If you are seeing other volume names, that may be an indication that you haven’t attached your SD properly.
@mkelsey after removing the line " track->SetTrackStatus(fStopAndKill); " I am getting detector id for each event. But still I am getting 0 and -1 for many events. I also printed the energy deposition in each event.
Here the codes I have in stepping action :
void B1SteppingAction::UserSteppingAction(const G4Step* step)
{
G4AnalysisManager *man = G4AnalysisManager::Instance();
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
Geant4 is flexible. You could do this in several different ways. You get to choose which method you like best, and then implement it.
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.
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.
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.
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?
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?
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.
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