How does sensetive detector works when i produce 3 particles?

Hello Dear Experts,
I want to know how sensetive detector class works when particle gun producing more than 1 particle(i mean in /run/beamOn 1 i have 3 different particles). In sensetive detector class i go throw steps of one event and store data. Do i firstly go throw steps of particle one, then two, then three(in order of creating them with particle gun). Or this order can change? Am i right that EndOfEvent method is called after each particle event, not after all 3? Also i find that if will also store number of the event, i will get numbers that dont go in order e.g. 9967 1048 1049 9990 histogram with 10000 bins for 10000 should have 1 everywhere(exept if particle was killed before detector then it will be 0).

I invite you to read more on the events and tracking in Geant4 in the user guide on fundamentals and tracking. Event can contain more than one primary particle (as I understand is your case), and EndOfEvent means in this case processing of all those primaries (and secondaries). You cannot assume you know the order of particles. Typically it’s the reverse order from the stack, and once the first primary is taken, it’s last created secondary is processed, then the second to last (or a secondary of a secondary), etc. Only then the other primary will be taken. This assumes you do not include any stacking action that could reclassify the tracks. If you need to know which particle the step belongs to, check the ID of G4Track.

Regarding your histogram, how do you create it? What do you store (how and where retrieved)? Are you running in multithreaded mode?

Hello Anna,
Thank you for your answer. In my histogram i store number of event. Inside ProccessHits i go throw steps and when i get particle i need for the first time e.g. muon i increase some number by 1. After that i drop “flag” and this number cant be incremented again before and of the event.
While i was writing this i understood what is my problem. My sensetive detector constists of 2 parts. So each part is the sensetive detector class. And they cant communicate with each other. Thats why they have different numbers of events and so on. I tried to add sensetive detector to the logic volume where those 2 parts located but it doesnt work. Can i somehow merge them?

Sensitive detector is used to record what is happening at the step level, locally.
If you need to perform some operations on the event level, maybe Event Action would be more suitable? Or a Stepping Action if you need to access step-level information (in any volume, not only the sensitive volumes).
And ID of event is something you can obtain from G4, from G4Event::GetEventID().

Yeah, this is exatcly what i need. But i cant understand how to implement stepping action in my programm. To be precise. Im my programm SD class gets all the data. This class atached to DetectorConstruciton class. And i cant understand how to change SD to stepping action. To what class it should be atached?(by atached i mean from “main” i call DetectorConstruction and from DC i call SD). My programm have also RunAction class where i create output file and save it. Sorry for too many questions

Have a look at the examples provided by Geant4. Almost any example contains Stepping Action, but in particular basic/B4 shows how stepping action can be used instead of a sensitive detector.
And user guide contains several examples of various user actions.

Thank you very much for your help. B4 example really helped. Maybe because structure of B4example is similliar to my programm and B1-3 are not. Everything works now. Again thanks for your help))