Some questions about sensitive detector of exampleB2a

Geant4 Version:4.11.1

In the

TrackerSD::TrackerSD(const G4String& name, Preformatted textconst G4String& hitsCollectionName)
 : G4VSensitiveDetector(name)

void TrackerSD::Initialize(G4HCofThisEvent* hce)
  // Create hits collection
  fHitsCollection = new TrackerHitsCollection(SensitiveDetectorName, collectionName[0]);
  // Add this collection in hce
  G4int hcID = G4SDManager::GetSDMpointer()->GetCollectionID(collectionName[0]);
  hce->AddHitsCollection( hcID, fHitsCollection );

1 I can not find the definition of “collectionName” and “SensitiveDetectorName”. It seems like user define not the G4class has. But this program can run well.(I do not find these in Geant4 Book For Application Developers Release 11.1)

2 In the function"Initialize", the mean of this new TrackerHistCollection is what. It means append a collection to sensitivedetector?

3 whether the

 G4int hcID = G4SDManager::GetSDMpointer()->GetCollectionID(collectionName[0]); hce->AddHitsCollection( hcID, fHitsCollection ); 

means the G4eventManager knows which sensitive logical volumes, so the ProcessHits only handle these steps

Thank you very much!

1 Like

This is a member of the G4VSensitiveDetector base class. That’s not complete documentation, but the LXR code browser is a good accompaniment to the main user guide and the examples.

It’s a container (think like std::vector or std::map) that TrackerSD will use to store any hits created in its ProcessHits member function. So you can think of it as “appending” a collection, but it’s really just holding a pointer to the collection for the current event that this SD will use.

No, the association of sensitive detectors to logical volumes is handled in the ConstructSDandField() member function of the detector construction. These lines are registering the newly constructed TrackerHitsCollection object with the set of all hits collections for the event, G4HCofThisEvent. Example Basic/B5 demonstrates how this works with multiple sensitive detectors and hit collections.

Thanks, I think I have deeper understanding about the meaning of these functions, and next time I try to use LXR code browser to search the information.

And I want to ask another question about the data handling. In the exampleB2 and B3, the author use sensitive detector to handle the specific logical volumes, and use the hit G4VHit G4THitsCollection to handle the data. In B3, the author uses the G4VPrimitiveScorer to handle datas. In B3b, the author uses G4run class to handle data.

I find the G4 gives us varity of ways to handle data. But if there are some ways better than others? If there some reference material or answers or some valuable experience in your practice?

My teacher tells us we can use G4RootAnalysisManager in runAction. In BeginOfRunAction() to create root file and TTree. In UserSteppingAction() of stepingAction, we can get the information we want, and Fill them to the root. And we can use StackingAction to change the state of our simulation, use TrackingAction, EventAcion an so on to meet demand.

After simulation, we use ROOT to analysis our data. I want to know weather this way can achieve the same goal in extend or basic examples. I have learned Root, and I can use root to do some analysis. If
this way can achieve same goal, I will try to use it when I plan to use G4 to do my project.
:pray: :pray:

Dear @Ziyang_He ,

Indeed there are different way to get information of your simulation. I would say the SensitiveDetector way is probably THE way to go if you intend to analyze your simulation event-by-event.

If on the other hand you mainly care for the accumulated response, I would probably have a look at the different Scorers.

Now… independent on which way you go, you can always dump the acquired information at different stages (Step, Event, Run) to file using the G4Analysis class which includes options like CSV, ROOT, …

P.S: You can use both ways obviously :wink:

1 Like

:grinning:thank you very much. These days I sees lots of examples, and sensitive detector or scorers are convenient to get the information. while to only record original information of G4step and anlysis these data may be some too trouble.

and I have a new quesion about scorers and sensitive detector.
If I let a SD to a Logical Volume, such as
example B2a

  G4String trackerChamberSDname = "B2/TrackerChamberSD";
  auto aTrackerSD = new TrackerSD(trackerChamberSDname, "TrackerHitsCollection");
  SetSensitiveDetector( fLogicChamber,  aTrackerSD );

If the fLogicChamber has lots of copies, whether only the “B2/TrackerChamberSD” only create one “TrackerHitsCollection” in one event. If the particle is in the fLogicChamber (lots of fLogicChamber), the ProcessHit() will be called .

Another question is about scores. such as G4PSEnergyDeposit (use G4HitMap). If
example B4d

  // declare Absorber as a MultiFunctionalDetector scorer
  auto absDetector = new G4MultiFunctionalDetector("Absorber");

  G4VPrimitiveScorer* primitive;
  primitive = new G4PSEnergyDeposit("Edep");
  primitive = new G4PSTrackLength("TrackLength");
  auto charged = new G4SDChargedFilter("chargedFilter");
  primitive ->SetFilter(charged);


if the AbsoLV has lots of copies in his mother LV, and his mother LV has lots of copies, is the G4PSEnergyDeposit will not distinguish AbsoLVs with the same copy number.

If I want to distinguish, is I need to use G4PSEnergyDeposit3D(Example RE02).
I want to know the idea behind these scorers.(I see the code of G4PSEnergyDeposit3D, but not understand)


Hello @Ziyang_He ,

I think in general for both question the answer is that even though they may be only one logical volume once you place them (via Placemente, Replica, Imprint, etc…) they become “real”… that is… they stop being “the idea” of a shape with a material and they are brought into “existence”.

Those volumes that exist it mean you can “touch” them. Therefore touchables objects in Geant4. For element in the geometry is unique via its own name, own name + copy number, own name + copy number + copy number of parent volume…

Using that uniqueness you can identify which actual fLogicChamber was hit, for instance.

Please have a look at this code snippet that may help you understand this idea:

SensitiveDetector::ProcessHits(G4Step* step, G4TouchableHistory*)
  G4TouchableHandle touchable = step->GetPreStepPoint()->GetTouchableHandle();

  /** Identify the detector **/
  G4VPhysicalVolume* detector_phys = touchable->GetVolume();
  G4String           detector_name = detector_phys->GetName();

   G4cout << detector_name << " : \n";
    for (G4int i = touchable->GetHistoryDepth(); i >= 0; i--)
      G4cout << i << " -> " << touchable->GetReplicaNumber(i) << "\n";



Thank you very much
It is vivd to help me understand this question.
with this answer, I think I can handle the origion data from G4step to construct my data.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.