G4cout does not not work

Dear Experts Please suggest I can print out in GEANT4
For Example in this code: G4Track *track = aStep->GetTrack();


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

G4ThreeVector posPhoton = preStepPoint->GetPosition();
G4cout<< “Photon Position” << posPhoton << G4endl;
const G4VTouchable *touchable = aStep->GetPreStepPoint()->GetTouchable();

G4int copyNo = touchable->GetCopyNumber();

G4cout << "Copy number: " << copyNo << G4endl;
G4VPhysicalVolume *physVol = touchable->GetVolume();
G4ThreeVector posDetector = physVol->GetTranslation();

I can’t print copyNo or Photon Position.

Please suggest a solution

Dear @yali,

Can you please provide more information. That being:
1.) What is the output of your code? Does it crash? or is it able to run succesfully but your print statements are unsuccesful?
2.) Do the print statements print only “Photon Position” and "Copy number: " or do they not print at all?
3.) The code sample which you have provided here, where in your code does it reside? Is it part of the G4UserSteppingAction?


Dear @JDecunha Thanks Problem was solved.

Want to explain how, so that the next person who does a search looking for help actually finds something?

Just recompiled it starts working

I am actually having the problem suggested in (2) here. If I do something like:

// steppingAction.cc

const G4VProcess *process = step->GetPostStepPoint()->GetProcessDefinedStep();
if (step == NULL) {G4cout << "step is NULL" << G4endl;}
G4cout << "step: " << step << G4endl;
G4cout << "presteppoint: " << step->GetPreStepPoint() << G4endl;
G4cout << process->GetProcessName(); << " detected! energy [eV]: "
             << step->GetPreStepPoint()->GetKineticEnergy() / eV
             << " in " << step->GetPreStepPoint()->GetPhysicalVolume()->GetName()
             << G4endl;

I get:

neutronInelastic detected! energy [eV]:  in crystal

even when the step is not NULL. Somehow the step-related values are just not printing to terminal.

Simply recompiling as suggested above does not work.

This same issue occurs even in the top-level main file of my program, where it will print a summary of command-line arguments parsed like:

  boolFlag:   // nothing gets printed here, but it should print `true` or `false`
  outputFile: data.dat

Even doing something like

G4cout << true << G4endl;
G4cout << false << G4endl;

just produces blank lines.

Maybe related or maybe not, my program segfaults soon after, which is (also) a new behavior not observed (as far as I remember) on an older MacOS version. I would be surprised if this was directly causing the segfault, though, due to the if (step == NULL) check never triggering. But the segfault happens soon enough that I never see a second one of these print tests occur.

Finally I have checked that in a simple “hello world”-type c++ program compiled with g++ that printing with std::cout works as expected.

Geant4 version 10.07.p01, Mac OSX 12.5

I think this was due to some conflicting conda environments, having built Geant4 in my standard env and my application in another. The issues (both printing and segfault) went away after completely building Geant4 in the proper env. I don’t know how that would have caused such a strange error, however.