Why is my detector not storing hits?

I have written the following for the sensitive volume:

G4Tubs *solidDetector = new G4Tubs("solidDetector", 0, raiofioouro, comprimento, 0, twopi);
logicDetector = new G4LogicalVolume(solidDetector, Ouro, "logicDetector");
auto fiointFisico = new G4PVPlacement(0, {0,0,0}, logicDetector, "Ânodo de Ouro", gasativoLogico, false, 0);

for (G4int i = 0; i < n; i++)
{
for (G4int j = 0; j < n; j++) 
    {
        G4VPhysicalVolume *physDetector = new G4PVPlacement(0, {0,0,0}, logicDetector, "Ânodo de Ouro", gasativoLogico, false, 0);
    }
}

And, at the detector.cc

#include "detector.hh"

MySensitiveDetector::MySensitiveDetector(G4String name) : G4VSensitiveDetector(name)
{}

MySensitiveDetector::~MySensitiveDetector()
{}

G4bool MySensitiveDetector::ProcessHits(G4Step *aStep, G4TouchableHistory *ROhist)
{

   G4Track *track = aStep->GetTrack();
   
   track->SetTrackStatus(fStopAndKill);
   
   G4StepPoint *preStepPoint = aStep->GetPreStepPoint();
   G4StepPoint *postStepPoint = aStep->GetPostStepPoint();
   
   G4ThreeVector posPhoton = preStepPoint->GetPosition();
   
   G4cout << "Photon position: " << posPhoton << G4endl;

}

When i make it, everything runs well and i can run the executable. The problem is that, after i use /run/beamOn, instead of the simulator show where the particle hits it at the terminal, it just print blank:

And this runs forever, only printting blank space. Where is my error?

Just to summarize, i want to create a cylindrical detector

i would speculate that there are several issues.

  1. all copies of your G4LogicalVolume feature the same copy number. (Don’t know if that is actually an issue.)
  2. all copies of your G4LogicalVolume are at the same position, so they completely overlap. Could be that the tracks only “see” the topmost placement!?
  3. there is no sensitive detector attached to the logical volume - or at least you did not post this part…
  4. if you do attach the sensitive detector: does it make a difference if you fStopAndKill the track after reading PreStepPoints etc.? edit: tried it, it does not

I have edited now the end code. It is now as follows: I have retired the “for conditions”.

//Detector/anodo de ouro
G4Tubs *solidDetector = new G4Tubs("solidDetector", 0, raiofioouro, comprimento, 0, twopi);

logicDetector = new G4LogicalVolume(solidDetector, Ouro, "logicDetector");

auto fiointFisico = new G4PVPlacement(0, {0,0,0}, logicDetector, "Ânodo de Ouro", gasativoLogico, false, 0);

G4VPhysicalVolume *physDetector = new G4PVPlacement(0, {0,0,0}, logicDetector, "Ânodo de Ouro", gasativoLogico, false, 0, true);


return physWorld;

}

void MyDetectorConstruction::ConstructSDandField()
{
MySensitiveDetector *sensDet = new MySensitiveDetector("Ânodo de Ouro");
logicDetector->SetSensitiveDetector(sensDet);}

The .cc folder explaining the sensitive detector is like this:

G4bool MySensitiveDetector::ProcessHits(G4Step *aStep, G4TouchableHistory *ROhist)
{

   G4Track *track = aStep->GetTrack();
   
   track->SetTrackStatus(fStopAndKill);
   
   G4StepPoint *preStepPoint = aStep->GetPreStepPoint();
   G4StepPoint *postStepPoint = aStep->GetPostStepPoint();
   
   G4ThreeVector posPhoton = preStepPoint->GetPosition();
   
   G4cout << "Photon position: " << posPhoton << G4endl;
}

I have realized that, if i retired the part "G4cout << “Photon position: " << posPhoton << G4endl;”, when i try to run the programm /run/beamOn, the geant4 gives segmentation fault. I think the problem is, in fact, this part:

   G4Track *track = aStep->GetTrack();
   
   track->SetTrackStatus(fStopAndKill);
   
   G4StepPoint *preStepPoint = aStep->GetPreStepPoint();
   G4StepPoint *postStepPoint = aStep->GetPostStepPoint();
   
   G4ThreeVector posPhoton = preStepPoint->GetPosition();

But i didn’t know exactly what can i do, since i am new at the geant4.

missing a
return true;
to satisfy the G4Bool type (no that is not it :frowning: )

i copy pasted your snippet into a sensitive detector, but it all works fine.

1 Like

I think the OP and I are following the same Geant4 example. I also am not getting the photon positions printed on the command line, nor the root file with posPhoton ntuples. Note that I am trying to record the positions of optical photons, emitted by a particle gun, on a sensitive detector.

Following is my ProcessHits() method.

G4bool MySensitiveDetector::ProcessHits(G4Step *aStep, G4TouchableHistory *ROhist)
{
G4Track *track = aStep->GetTrack();

    track->SetTrackStatus(fStopAndKill);

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

    G4ThreeVector posPhoton = preStepPoint->GetPosition();

    G4cout << "Photon position:" << posPhoton << G4endl;   //Either this...
    const G4VTouchable *touchable = aStep->GetPreStepPoint()->GetTouchable();

    G4int copyNo = touchable->GetCopyNumber();

   // G4cout << "Copy number:" << copyNo << G4endl;      OR this...

    G4VPhysicalVolume *physVol = touchable->GetVolume();
    G4ThreeVector posDetector = physVol->GetTranslation();

    G4cout << "Detector position:" << posDetector << G4endl;  //OR this...

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

    G4AnalysisManager *man = G4AnalysisManager::Instance();
    man->FillNtupleIColumn(0, evt);
    man->FillNtupleDColumn(1, posDetector[0]);
    man->FillNtupleDColumn(2, posDetector[1]);
    man->FillNtupleDColumn(3, posDetector[2]);
    man->AddNtupleRow(0);

    return true;
}

And following is the code where I describe the run action.

MyRunAction::MyRunAction()
{
    G4AnalysisManager *man = G4AnalysisManager::Instance();

    man->CreateNtuple("Hits", "Hits");
    man->CreateNtupleIColumn("fEvent");
    man->CreateNtupleDColumn("fX");
    man->CreateNtupleDColumn("fY");
    man->CreateNtupleDColumn("fZ");
    man->FinishNtuple(0);

}

MyRunAction::~MyRunAction()
{}

void MyRunAction::BeginOfRunAction(const G4Run* run)
{
    G4AnalysisManager *man = G4AnalysisManager::Instance();

    G4int runID = run->GetRunID();

    std::stringstream strRunID;
    strRunID << runID;

    G4String FileName = "output"+strRunID.str()+".root";
    man->OpenFile(FileName);
}

void MyRunAction::EndOfRunAction(const G4Run*)
{
    G4AnalysisManager *man = G4AnalysisManager::Instance();

    man->Write();
    man->CloseFile();
}

I have no idea why I am not getting the expected results.

Hi,

I am also getting the same error, in addition to “segmentation violation”. If you already solved the problem, kindly do let me know.