Unable to Print Detector index

I have placed my logical volume at different theta phi using loop and got like this:

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;

I am getting Copy number 0 for all event.

Why are you using the PreStepPoint()? What happens when you use the PostStepPoint() instead?

@mkelsey Here I used PreStepPoint()
const G4VTouchable *touchable = step->GetPreStepPoint()->GetTouchable();

After doing
const G4VTouchable *touchable = step->GetPostStepPoint()->GetTouchable();

I am getting 0 and -1 for different event ID.

Run 0 starts.

Event id: 0 detector id: -1
Event id: 1 detector id: -1
Event id: 2 detector id: -1
Event id: 3 detector id: -1
Event id: 4 detector id: -1
Event id: 5 detector id: -1
Event id: 6 detector id: -1
Event id: 7 detector id: -1
Event id: 8 detector id: 0
Event id: 8 detector id: 0
Event id: 8 detector id: 0
Event id: 9 detector id: -1
Event id: 10 detector id: 0

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.

Event id: 513 detector id: 102
Event id: 513 detector id: 102
Event id: 513 detector id: 102
Event id: 513 detector id: 102
Event id: 513 detector id: 102
Event id: 513 detector id: 102
Event id: 513 detector id: 102
Event id: 513 detector id: 102
Energy deposition: 0.790249
Event id: 514 detector id: -1
Energy deposition: 0
Event id: 515 detector id: 402
Event id: 515 detector id: 402
Event id: 515 detector id: 402
Event id: 515 detector id: 402
Event id: 515 detector id: 402
Event id: 515 detector id: 402
Event id: 515 detector id: 402
Energy deposition: 0.771568
Event id: 516 detector id: -1
Energy deposition: 0

Here the codes I have in stepping action :
void B1SteppingAction::UserSteppingAction(const G4Step* step)
{
G4AnalysisManager *man = G4AnalysisManager::Instance();

G4LogicalVolume *volume = step->GetPreStepPoint()->GetTouchableHandle()->GetVolume()->GetLogicalVolume();

const B1DetectorConstruction *detectorConstruction = static_cast<const B1DetectorConstruction*> (G4RunManager::GetRunManager()->GetUserDetectorConstruction());

G4LogicalVolume *fScoringVolume = detectorConstruction->GetScoringVolume();

G4Track *track = step->GetTrack();

// track->SetTrackStatus(fStopAndKill);

G4StepPoint *preStepPoint = step->GetPreStepPoint();
G4StepPoint *postStepPoint = step->GetPostStepPoint();

G4ThreeVector posPhoton = preStepPoint->GetPosition();

const G4VTouchable *touchable = step->GetPostStepPoint()->GetTouchable();

G4int copyNo = touchable->GetCopyNumber();

// G4cout << "Copy number: " << copyNo << G4endl;

G4int evt = G4RunManager::GetRunManager()->GetCurrentEvent()->GetEventID();

G4cout << "Event id: " << evt << "\t"<<"detector id: "<<copyNo << G4endl;

Here fScoringVolume is my logical volume.
And one thing I couldn’t understand why there are repetition of same event id and detector id ?

Are you fetching the “fScoringVolume” in order to compare that to the local “volume” of the step? I don’t see that code in there.

In your ConstructSDandField() function, are you attaching your SD to any volumes other than “fScoringVolume”?

@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