Verbosity and data formatting issues

Hi all,

I am attempting to print the info(KineE, DEStep, stepLeng, etc) to the terminal for each particle for each event as they traverse my geometry (as shown below).

However when I run the program, the information is printed in a very unreadable way (see below).

I have tried to change the verboity settings in my macros and made alterations to the steppingVerbose.cc and nothing has worked.

SteppingVerbose::SteppingVerbose()
: G4SteppingVerbose()
{ }

//…oooOO0OOooo…oooOO0OOooo…oooOO0OOooo…oooOO0OOooo…

SteppingVerbose::~SteppingVerbose()
{}

//…oooOO0OOooo…oooOO0OOooo…oooOO0OOooo…oooOO0OOooo…

void SteppingVerbose::TrackingStarted()
{
CopyState();

G4int prec = G4cout.precision(3);

//Step zero
//
if( verboseLevel > 0 ){
G4cout << std::setw( 5) << “Step#” << " "
<< std::setw( 6) << “X” << " "
<< std::setw( 6) << “Y” << " "
<< std::setw( 6) << “Z” << " "
<< std::setw( 9) << “KineE” << " "
<< std::setw( 9) << “dEStep” << " "
<< std::setw(10) << “StepLeng”
<< std::setw(10) << “TrakLeng”
<< std::setw(10) << “Volume” << " "
<< std::setw(10) << “Process” << G4endl;

G4cout << std::setw(5) << fTrack->GetCurrentStepNumber() << " "
    << std::setw(6) << G4BestUnit(fTrack->GetPosition().x(),"Length")
    << std::setw(6) << G4BestUnit(fTrack->GetPosition().y(),"Length")
    << std::setw(6) << G4BestUnit(fTrack->GetPosition().z(),"Length")
    << std::setw(6) << G4BestUnit(fTrack->GetKineticEnergy(),"Energy")
    << std::setw(6) << G4BestUnit(fStep->GetTotalEnergyDeposit(),"Energy")
    << std::setw(6) << G4BestUnit(fStep->GetStepLength(),"Length")
    << std::setw(6) << G4BestUnit(fTrack->GetTrackLength(),"Length")
    << std::setw(10) << fTrack->GetVolume()->GetName()
    << "   initStep" << G4endl;        

}
G4cout.precision(prec);
}

//…oooOO0OOooo…oooOO0OOooo…oooOO0OOooo…oooOO0OOooo…

void SteppingVerbose::StepInfo()
{
CopyState();

G4int prec = G4cout.precision(3);

if( verboseLevel >= 1 ){
if( verboseLevel >= 4 ) VerboseTrack();
if( verboseLevel >= 3 ){
G4cout << G4endl;
G4cout << std::setw( 5) << “#Step#” << " "
<< std::setw( 6) << “X” << " "
<< std::setw( 6) << “Y” << " "
<< std::setw( 6) << “Z” << " "
<< std::setw( 9) << “KineE” << " "
<< std::setw( 9) << “dEStep” << " "
<< std::setw(10) << “StepLeng”
<< std::setw(10) << “TrakLeng”
<< std::setw(10) << “Volume” << " "
<< std::setw(10) << “Process” << G4endl;
}

G4cout << std::setw( 5) << fTrack->GetCurrentStepNumber() << " "
    << std::setw(6) << G4BestUnit(fTrack->GetPosition().x(),"Length")
    << std::setw(6) << G4BestUnit(fTrack->GetPosition().y(),"Length")
    << std::setw(6) << G4BestUnit(fTrack->GetPosition().z(),"Length")
    << std::setw(6) << G4BestUnit(fTrack->GetKineticEnergy(),"Energy")
    << std::setw(6) << G4BestUnit(fStep->GetTotalEnergyDeposit(),"Energy")
    << std::setw(6) << G4BestUnit(fStep->GetStepLength(),"Length")
    << std::setw(6) << G4BestUnit(fTrack->GetTrackLength(),"Length")
    << std::setw(10) << fTrack->GetVolume()->GetName();

const G4VProcess* process 
                  = fStep->GetPostStepPoint()->GetProcessDefinedStep();
G4String procName = " UserLimit";
if (process) procName = process->GetProcessName();
if (fStepStatus == fWorldBoundary) procName = "OutOfWorld";
G4cout << "   " << std::setw(10) << procName;
G4cout << G4endl;

if (verboseLevel == 2) {
  const std::vector<const G4Track*>* secondary 
                                = fStep->GetSecondaryInCurrentStep();
  size_t nbtrk = (*secondary).size();
  if (nbtrk) {
    G4cout << "\n    :----- List of secondaries ----------------" << G4endl;
    G4cout.precision(4);
    for (size_t lp=0; lp<(*secondary).size(); lp++) {
      G4cout << "   "
             << std::setw(13)                 
             << (*secondary)[lp]->GetDefinition()->GetParticleName()
             << ":  energy ="
             << std::setw(6)
             << G4BestUnit((*secondary)[lp]->GetKineticEnergy(),"Energy")
             << "  time ="
             << std::setw(6)
             << G4BestUnit((*secondary)[lp]->GetGlobalTime(),"Time");
      G4cout << G4endl;
    }
          
    G4cout << "    :------------------------------------------\n" << G4endl;
  }
}

}
G4cout.precision(prec);
}

//…oooOO0OOooo…oooOO0OOooo…oooOO0OOooo…oooOO0OOooo…

Im sure there has to be a simple way of formatting this information to make it more readable.
Any help would be very much appreciated!

It’s due to multiple threads printing the output simultaneously.
You can add this UI command:

/control/cout/useBuffer 1

It will group the buffers and print them at the end of the job.
Or you may send the output stream to the files (one file per thread) by setting the output name:

/control/cout/setCoutFile <NAME>

Hi Anna,

That’s great, thanks a lot!

I have one more issue that I have been struggling with.
Is there a way that I can filter the particles printed to the terminal (eg just gamma’s and their secondaries and omitting the rest)?
I will need to reduce the amount information printed and output to file as I increase the number of generated particles.

Thanks again,
Jason.

you can implement your own SteppingVerbose, as it is done in many electromagnetic or hadronic examples. Then, you can control the printing here.

Michel’s solution is the recommended way to refine stepping in most scenarios. For example you can use it also to increase the precision of position.

But printing is not mean for bulk output of information from Geant4. It is primarily meant for diagnosis of a typical event or track, for debugging difficult events or demonstrating what is involved in tracking.

So it would seem better to use a different way provided in Geant4 to collect and output information - e.g. creating a sensitive detector to measure energy deposition, or by using the ‘command-line scoring’ to record a large variety of quantities or tallies without coding.

Hi,

Thank you all for your responses.

I have done some command-line scoring in order to find the energy deposited in a lead shield and the track length/number of steps of gamma’s released due to Tb160 decay using the following macro.

#Define scoring Region with gamma filter
/score/create/boxMesh target1
/score/mesh/boxSize 51. 51. 51. cm
/score/mesh/translate/xyz 0. 0. 0. mm
/score/mesh/nBin 60 60 60
/score/quantity/nOfStep nOfStepGamma
/score/quantity/energyDeposit eDep
/score/filter/particle gammaFilter gamma
/score/close
#Run decay
/run/beamOn 1000000
/score/dumpQuantityToFile target1 eDep gammafill.csv

However, when I run this I get the following output.
image

Would you know the reason for this?

Thank you for the help.