# Unable to Print Detector index

**URL:** <https://geant4-forum.web.cern.ch/t/unable-to-print-detector-index/10669>\
**Category:** Particles, Track, Event, Run and Biasing\
**Created:** [June 8, 2023, 1:02pm UTC](https://geant4-forum.web.cern.ch/t/unable-to-print-detector-index/10669 "2023-06-08T13:02:36Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![shapeandsymmetry](https://geant4-forum.web.cern.ch/user_avatar/geant4-forum.web.cern.ch/shapeandsymmetry/32/5883_2.png) [@shapeandsymmetry](https://geant4-forum.web.cern.ch/u/shapeandsymmetry)\
**Post date:** [June 8, 2023, 1:02pm UTC](https://geant4-forum.web.cern.ch/t/unable-to-print-detector-index/10669/1 "2023-06-08T13:02:37Z")

</div>

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

 ![Screenshot from 2023-06-05 08-16-02](https://geant4-forum.web.cern.ch/uploads/default/original/2X/a/aa2fe8708dc44cd014b27246ffedc58477cef666.png)

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.

---

<div class="post-metadata">

**Author:** ![mkelsey](https://geant4-forum.web.cern.ch/user_avatar/geant4-forum.web.cern.ch/mkelsey/32/49_2.png) [@mkelsey](https://geant4-forum.web.cern.ch/u/mkelsey)\
**Post date:** [June 8, 2023, 1:53pm UTC](https://geant4-forum.web.cern.ch/t/unable-to-print-detector-index/10669/2 "2023-06-08T13:53:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![shapeandsymmetry](https://geant4-forum.web.cern.ch/user_avatar/geant4-forum.web.cern.ch/shapeandsymmetry/32/5883_2.png) [@shapeandsymmetry](https://geant4-forum.web.cern.ch/u/shapeandsymmetry)\
**Post date:** [June 8, 2023, 2:18pm UTC](https://geant4-forum.web.cern.ch/t/unable-to-print-detector-index/10669/3 "2023-06-08T14:18:37Z")

</div>

@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

---

<div class="post-metadata">

**Author:** ![mkelsey](https://geant4-forum.web.cern.ch/user_avatar/geant4-forum.web.cern.ch/mkelsey/32/49_2.png) [@mkelsey](https://geant4-forum.web.cern.ch/u/mkelsey)\
**Post date:** [June 8, 2023, 3:13pm UTC](https://geant4-forum.web.cern.ch/t/unable-to-print-detector-index/10669/4 "2023-06-08T15:13:47Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![shapeandsymmetry](https://geant4-forum.web.cern.ch/user_avatar/geant4-forum.web.cern.ch/shapeandsymmetry/32/5883_2.png) [@shapeandsymmetry](https://geant4-forum.web.cern.ch/u/shapeandsymmetry)\
**Post date:** [June 9, 2023, 5:16am UTC](https://geant4-forum.web.cern.ch/t/unable-to-print-detector-index/10669/5 "2023-06-09T05:16:59Z")

</div>

@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 ?

---

<div class="post-metadata">

**Author:** ![mkelsey](https://geant4-forum.web.cern.ch/user_avatar/geant4-forum.web.cern.ch/mkelsey/32/49_2.png) [@mkelsey](https://geant4-forum.web.cern.ch/u/mkelsey)\
**Post date:** [June 10, 2023, 6:37am UTC](https://geant4-forum.web.cern.ch/t/unable-to-print-detector-index/10669/6 "2023-06-10T06:37:11Z")

</div>

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”?

---

<div class="post-metadata">

**Author:** ![shapeandsymmetry](https://geant4-forum.web.cern.ch/user_avatar/geant4-forum.web.cern.ch/shapeandsymmetry/32/5883_2.png) [@shapeandsymmetry](https://geant4-forum.web.cern.ch/u/shapeandsymmetry)\
**Post date:** [June 10, 2023, 11:50am UTC](https://geant4-forum.web.cern.ch/t/unable-to-print-detector-index/10669/7 "2023-06-10T11:50:33Z")

</div>

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

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

fScoringVolume = logicBox;  
return physWorld;  
}

fScoringVolume = logicBox ; defined in construction.cc

---

<div class="post-metadata">

**Author:** ![mkelsey](https://geant4-forum.web.cern.ch/user_avatar/geant4-forum.web.cern.ch/mkelsey/32/49_2.png) [@mkelsey](https://geant4-forum.web.cern.ch/u/mkelsey)\
**Post date:** [June 10, 2023, 5:19pm UTC](https://geant4-forum.web.cern.ch/t/unable-to-print-detector-index/10669/8 "2023-06-10T17:19:30Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![shapeandsymmetry](https://geant4-forum.web.cern.ch/user_avatar/geant4-forum.web.cern.ch/shapeandsymmetry/32/5883_2.png) [@shapeandsymmetry](https://geant4-forum.web.cern.ch/u/shapeandsymmetry)\
**Post date:** [June 10, 2023, 6:03pm UTC](https://geant4-forum.web.cern.ch/t/unable-to-print-detector-index/10669/9 "2023-06-10T18:03:48Z")

</div>

@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.

---

<div class="post-metadata">

**Author:** ![weller](https://geant4-forum.web.cern.ch/user_avatar/geant4-forum.web.cern.ch/weller/32/3357_2.png) [@weller](https://geant4-forum.web.cern.ch/u/weller)\
**Post date:** [June 10, 2023, 7:37pm UTC](https://geant4-forum.web.cern.ch/t/unable-to-print-detector-index/10669/10 "2023-06-10T19:37:37Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![shapeandsymmetry](https://geant4-forum.web.cern.ch/user_avatar/geant4-forum.web.cern.ch/shapeandsymmetry/32/5883_2.png) [@shapeandsymmetry](https://geant4-forum.web.cern.ch/u/shapeandsymmetry)\
**Post date:** [June 10, 2023, 7:47pm UTC](https://geant4-forum.web.cern.ch/t/unable-to-print-detector-index/10669/11 "2023-06-10T19:47:33Z")

</div>

@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?

---

<div class="post-metadata">

**Author:** ![weller](https://geant4-forum.web.cern.ch/user_avatar/geant4-forum.web.cern.ch/weller/32/3357_2.png) [@weller](https://geant4-forum.web.cern.ch/u/weller)\
**Post date:** [June 10, 2023, 9:34pm UTC](https://geant4-forum.web.cern.ch/t/unable-to-print-detector-index/10669/12 "2023-06-10T21:34:45Z")

</div>

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](https://geant4-forum.web.cern.ch/t/multiple-steps-on-geometricboundary/10540)

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](https://github.com/Geant4/geant4/blob/v11.1.1/examples/basic/B4/B4d/src/EventAction.cc)

---

<div class="post-metadata">

**Author:** ![mkelsey](https://geant4-forum.web.cern.ch/user_avatar/geant4-forum.web.cern.ch/mkelsey/32/49_2.png) [@mkelsey](https://geant4-forum.web.cern.ch/u/mkelsey)\
**Post date:** [June 10, 2023, 9:37pm UTC](https://geant4-forum.web.cern.ch/t/unable-to-print-detector-index/10669/13 "2023-06-10T21:37:10Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![shapeandsymmetry](https://geant4-forum.web.cern.ch/user_avatar/geant4-forum.web.cern.ch/shapeandsymmetry/32/5883_2.png) [@shapeandsymmetry](https://geant4-forum.web.cern.ch/u/shapeandsymmetry)\
**Post date:** [June 10, 2023, 10:29pm UTC](https://geant4-forum.web.cern.ch/t/unable-to-print-detector-index/10669/14 "2023-06-10T22:29:16Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![shapeandsymmetry](https://geant4-forum.web.cern.ch/user_avatar/geant4-forum.web.cern.ch/shapeandsymmetry/32/5883_2.png) [@shapeandsymmetry](https://geant4-forum.web.cern.ch/u/shapeandsymmetry)\
**Post date:** [June 10, 2023, 10:31pm UTC](https://geant4-forum.web.cern.ch/t/unable-to-print-detector-index/10669/15 "2023-06-10T22:31:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mkelsey](https://geant4-forum.web.cern.ch/user_avatar/geant4-forum.web.cern.ch/mkelsey/32/49_2.png) [@mkelsey](https://geant4-forum.web.cern.ch/u/mkelsey)\
**Post date:** [June 11, 2023, 3:01am UTC](https://geant4-forum.web.cern.ch/t/unable-to-print-detector-index/10669/16 "2023-06-11T03:01:41Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![shapeandsymmetry](https://geant4-forum.web.cern.ch/user_avatar/geant4-forum.web.cern.ch/shapeandsymmetry/32/5883_2.png) [@shapeandsymmetry](https://geant4-forum.web.cern.ch/u/shapeandsymmetry)\
**Post date:** [June 11, 2023, 4:30am UTC](https://geant4-forum.web.cern.ch/t/unable-to-print-detector-index/10669/17 "2023-06-11T04:30:38Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![weller](https://geant4-forum.web.cern.ch/user_avatar/geant4-forum.web.cern.ch/weller/32/3357_2.png) [@weller](https://geant4-forum.web.cern.ch/u/weller)\
**Post date:** [June 11, 2023, 6:45am UTC](https://geant4-forum.web.cern.ch/t/unable-to-print-detector-index/10669/18 "2023-06-11T06:45:35Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![shapeandsymmetry](https://geant4-forum.web.cern.ch/user_avatar/geant4-forum.web.cern.ch/shapeandsymmetry/32/5883_2.png) [@shapeandsymmetry](https://geant4-forum.web.cern.ch/u/shapeandsymmetry)\
**Post date:** [June 11, 2023, 8:01am UTC](https://geant4-forum.web.cern.ch/t/unable-to-print-detector-index/10669/19 "2023-06-11T08:01:57Z")

</div>

@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();

---

<div class="post-metadata">

**Author:** ![weller](https://geant4-forum.web.cern.ch/user_avatar/geant4-forum.web.cern.ch/weller/32/3357_2.png) [@weller](https://geant4-forum.web.cern.ch/u/weller)\
**Post date:** [June 11, 2023, 9:31am UTC](https://geant4-forum.web.cern.ch/t/unable-to-print-detector-index/10669/20 "2023-06-11T09:31:57Z")

</div>

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](https://geant4-forum.web.cern.ch/t/multiple-steps-on-geometricboundary/10540)

[Next page](https://geant4-forum.web.cern.ch/t/unable-to-print-detector-index/10669.md?page=2)
