Error Trying To Read Energy From File

Hello there,

I am using Geant4-v10.1 and I am trying to create a new generator in this big simulation for a big collaboration.

At this moment, I was able to create a new generator called “CustomEvt” which I needed for simulating two primaries, a neutron and a gamma.

Now, I want to read the neutron energy from a file called “neutron_spectrum.txt” that looks like this:

0.0 0.004183350400822896
0.01 0.0061559115469339894
0.02 0.007854697778009402
0.03 0.009467939925478
0.04 0.011116145382421989
0.05 0.012739389686657263
0.06 0.014277726005035818
0.07 0.01573603243530642
0.08 0.017280354048559564
0.09 0.01869268961789877
0.1 0.020057977701304144
0.11 0.021349977251555744
0.12 0.02267589182256974
0.13 0.023924753927035195
0.14 0.025105372035072307
0.15 0.026255454673943062
0.16 0.027368129542818653
0.17 0.028384116633618263
etc

Where the weights are normalized between 0 and 1 and energy goes up to almost 10 MeV.

For this, I am trying to do the following:

// Create the energy distribution object
G4SPSEneDistribution* energyDist = new G4SPSEneDistribution();

// Set the energy distribution from a file
energyDist->SetEnergyDisType("Arb");
energyDist->ArbEnergyHistoFile("/Users/diiego/software/wcsim_replacement/build/neutron_spectrum.txt");

// Obtain the definition of the neutron and sample an energy from the distribution
G4ParticleTable* particleTable = G4ParticleTable::GetParticleTable();
G4ParticleDefinition* neutronDefinition = particleTable->FindParticle("neutron");

// Sample an energy for the neutron
G4double nEnergy = energyDist->GenerateOne(neutronDefinition);

But when I add the G4double nEnergy = energyDist->GenerateOne(neutronDefinition); line, application crashes when running it, even though it compiles correctly. Here the output of the crash:

*************************************************************
 Geant4 version Name: geant4-10-01-patch-03    (5-February-2016)
                      Copyright : Geant4 Collaboration
                      Reference : NIM A 506 (2003), 250-303
                            WWW : http://cern.ch/geant4
*************************************************************

Setting water attenuation parameter 1.300000
Setting cathode reflectivity parameter 0.320000
Setting Rayleigh scattering parameter 0.750000
Setting blacksheet reflection parameter 2.500000
Setting Mie scattering parameter 0.000000
Setting Top Veto Off
Setting Top Veto PMT Spacing 100.000000
There are 20 available physics lists, and they are:
  FTFP_BERT
  FTFP_BERT_TRV
  FTFP_BERT_HP
  FTFP_INCLXX
  FTFP_INCLXX_HP
  FTF_BIC
  LBE
  QBBC
  QGSP_BERT
  QGSP_BERT_HP
  QGSP_BIC
  QGSP_BIC_HP
  QGSP_FTFP_BERT
  QGSP_INCLXX
  QGSP_INCLXX_HP
  QGS_BIC
  Shielding
  ShieldingLEND
  ShieldingM
  NuBeam
Setting Physics list to FTFP_BERT and delaying initialization
Setting Physics list to FTFP_BERT and delaying initialization
Setting neutron capture model to Default
Initializing physics list FTFP_BERT
G4PhysListFactory::GetReferencePhysList <FTFP_BERT>  EMoption= 0
<<< Geant4 Physics List simulation engine: FTFP_BERT 2.0

<<< Reference Physics List FTFP_BERT is built

RegisterPhysics: G4EmStandard
RegisterPhysics: G4GammaLeptoNuclearPhys
RegisterPhysics: Decay
RegisterPhysics: hElasticWEL_CHIPS
RegisterPhysics: hInelastic FTFP_BERT
RegisterPhysics: stopping
RegisterPhysics: ionInelasticFTFP_BIC
RegisterPhysics: neutronTrackingCut
RegisterPhysics: OpticalPhysics
Visualization Manager instantiating with verbosity "warnings (3)"...
Visualization Manager initialising...
Registering graphics systems...

You have successfully chosen to use the following graphics systems.
Current available graphics systems are:
  ASCIITree (ATree)
  DAWNFILE (DAWNFILE)
  G4HepRep (HepRepXML)
  G4HepRepFile (HepRepFile)
  OpenGLImmediateQt (OGLIQt)
  OpenGLStoredQt (OGLSQt)
  RayTracer (RayTracer)
  VRML1FILE (VRML1FILE)
  VRML2FILE (VRML2FILE)

You have successfully registered the following graphics systems.
Current available graphics systems are:
  ASCIITree (ATree)
  DAWNFILE (DAWNFILE)
  G4HepRep (HepRepXML)
  G4HepRepFile (HepRepFile)
  OpenGLImmediateQt (OGLIQt)
  OpenGLStoredQt (OGLSQt)
  RayTracer (RayTracer)
  VRML1FILE (VRML1FILE)
  VRML2FILE (VRML2FILE)

Registering model factories...
G4VisManager: No model factories registered with G4VisManager.
G4VisManager::RegisterModelFactories() should be overridden in derived
class. See G4VisExecutive for an example.

You have successfully registered the following model factories.
Registered model factories:
  None

Registered models:
  Unspecified (Current)

Registered filter factories:
  None

You have successfully registered the following user vis actions.
Run Duration User Vis Actions: none
End of Event User Vis Actions: none
End of Run User Vis Actions: none

Some /vis commands (optionally) take a string to specify colour.
Available colours:
  black, blue, brown, cyan, gray, green, grey, magenta, red, white, yellow

Digitizer choice set to SKI (this is a default set; it may be overwritten by user commands)
Trigger choice set to NDigits (this is a default set; it may be overwritten by user commands)
Saving only triggered events (this is a default set; it may be overwritten by user commands)
Trigger time for events which fail all triggers will be set to 100 ns (this is a default set; it may be overwritten by user commands)
SaveFailures pretrigger window set to -400 ns (this is a default set; it may be overwritten by user commands)
SaveFailures posttrigger window set to 950 ns (this is a default set; it may be overwritten by user commands)
Will adjust NDigits trigger threshold using average dark noise rate (this is a default set; it may be overwritten by user commands)
Setting DarkMode value 0 (this is a default set; it may be overwritten by user commands)
Setting DarkLow value 0 (this is a default set; it may be overwritten by user commands)
Setting DarkHigh value 100000 (this is a default set; it may be overwritten by user commands)
Setting DarkWindow value 5000 (this is a default set; it may be overwritten by user commands)
**** Building Cylindrical Detector ****
Computed tolerance = 4.08e-07 mm
Checking overlaps for volume WCBarrel ... OK!
Checking overlaps for volume WCBarrelAnnulus ... OK!
Checking overlaps for volume WCBarrelCellBlackSheet ... OK!
Checking overlaps for volume WCExtraTower ... OK!
Checking overlaps for volume WCExtraTowerBlackSheet ... OK!
========================================================
Expose height for PMT SuperK-glassFaceWCPMT : 180 mm
Radius at expose height : 254 mm
Radius of curvature : 269.211 mm
Reflector radius : 254 mm
Reflector thickness : 0 mm
========================================================
Checking overlaps for volume InteriorWCPMT ... OK!
Checking overlaps for volume SuperK-glassFaceWCPMT ... OK!
Checking overlaps for volume WCBarrelBorderRing ... OK!
Checking overlaps for volume WCBarrelCellBlackSheet ... OK!
Checking overlaps for volume WCExtraTowerBorderCell ... OK!
Checking overlaps for volume WCExtraTowerBlackSheet ... OK!
Checking overlaps for volume WCCap ... OK!
Checking overlaps for volume WCCapBlackSheet ... OK!
total on cap: 1672
Coverage was calculated to be: 0.380348
Checking overlaps for volume WCBarrelBorderRing ... OK!
Checking overlaps for volume WCBarrelCellBlackSheet ... OK!
Checking overlaps for volume WCExtraTowerBorderCell ... OK!
Checking overlaps for volume WCExtraTowerBlackSheet ... OK!
Checking overlaps for volume WCCap ... OK!
Checking overlaps for volume WCCapBlackSheet ... OK!
total on cap: 1672
Coverage was calculated to be: 0.380348
Checking overlaps for volume TopCapAssembly ... OK!
Checking overlaps for volume BottomCapAssembly ... OK!
 WCLength       = 40.8 m
position Y = 0
 expHallLength = 122.4
Geofile written
### Adding tracking cuts for neutron  TimeCut(ns)= 10000  KinEnergyCut(MeV)= 0
WCSimPhysicsListFactory::SetCuts:CutLength : 1 mm
Available UI session types: [ Qt, GAG, tcsh, csh ]
position Y = 0
Geofile written
Warning in <TBranch::TBranch>: Extra characters after type tag 'Char_t' for branch 'GeometryType'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Double_t' for branch 'CylinderRadius'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Double_t' for branch 'CylinderLength'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Char_t' for branch 'PMTtype_ID'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Double_t' for branch 'PMTradius_ID'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Char_t' for branch 'PMTtype_OD'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Double_t' for branch 'PMTradius_OD'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Int_t' for branch 'numPMT_ID'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Int_t' for branch 'numPMT_OD'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Int_t' for branch 'Orientation'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Double_t' for branch 'Offset_x'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Double_t' for branch 'Offset_y'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Double_t' for branch 'Offset_z'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Int_t' for branch 'num_mPMT'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Int_t' for branch 'Tube'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Int_t' for branch 'mPMT'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Int_t' for branch 'mPMT_pmt'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Double_t' for branch 'x'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Double_t' for branch 'y'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Double_t' for branch 'z'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Int_t' for branch 'cylLocation'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Double_t' for branch 'direction_x'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Double_t' for branch 'direction_y'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Double_t' for branch 'direction_z'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Double_t' for branch 'phi'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Double_t' for branch 'theta'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Int_t' for branch 'Run'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Int_t' for branch 'Event'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Int_t' for branch 'SubEvent'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Int_t' for branch 'Type'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Int_t' for branch 'TriggeredDigits'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Double_t' for branch 'Length'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Double_t' for branch 'StartTime'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Int_t' for branch 'Run'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Int_t' for branch 'Event'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Int_t' for branch 'SubEvent'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Int_t' for branch 'InteractionMode'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Char_t' for branch 'VertexVolume'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Double_t' for branch 'Vertex_x'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Double_t' for branch 'Vertex_y'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Double_t' for branch 'Vertex_z'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Int_t' for branch 'Run'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Int_t' for branch 'Event'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Int_t' for branch 'SubEvent'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Int_t' for branch 'Ntracks'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Int_t' for branch 'Pid'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Int_t' for branch 'Flag'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Float_t' for branch 'Mass'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Float_t' for branch 'P'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Float_t' for branch 'Energy'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Int_t' for branch 'ParentID'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Int_t' for branch 'TrackID'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Float_t' for branch 'Time'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Float_t' for branch 'Dirx'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Float_t' for branch 'Diry'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Float_t' for branch 'Dirz'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Float_t' for branch 'Px'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Float_t' for branch 'Py'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Float_t' for branch 'Pz'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Float_t' for branch 'Start_x'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Float_t' for branch 'Start_y'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Float_t' for branch 'Start_z'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Float_t' for branch 'Stop_x'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Float_t' for branch 'Stop_y'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Float_t' for branch 'Stop_z'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Float_t' for branch 'Length'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Int_t' for branch 'Run'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Int_t' for branch 'Event'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Int_t' for branch 'SubEvent'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Int_t' for branch 'NHits'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Int_t' for branch 'NHits_noDN'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Int_t' for branch 'NPMTs'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Int_t' for branch 'NPMTs_noDN'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Float_t' for branch 'Time'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Int_t' for branch 'PMT_QTot'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Int_t' for branch 'PMT_QTot_noDN'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Int_t' for branch 'ParentID'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Int_t' for branch 'Vector_index'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Int_t' for branch 'Tube'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Int_t' for branch 'mPMT'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Int_t' for branch 'mPMT_pmt'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Int_t' for branch 'TrackID'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Float_t' for branch 'PMT_x'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Float_t' for branch 'PMT_y'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Float_t' for branch 'PMT_z'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Float_t' for branch 'PMT_dirx'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Float_t' for branch 'PMT_diry'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Float_t' for branch 'PMT_dirz'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Int_t' for branch 'Run'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Int_t' for branch 'Event'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Int_t' for branch 'SubEvent'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Int_t' for branch 'NDigiHits'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Int_t' for branch 'NDigiPMTs'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Float_t' for branch 'QTotDigi'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Float_t' for branch 'Q'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Float_t' for branch 'T'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Int_t' for branch 'Vector_index'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Int_t' for branch 'Tube'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Int_t' for branch 'mPMT'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Int_t' for branch 'mPMT_pmt'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Float_t' for branch 'PMT_x'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Float_t' for branch 'PMT_y'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Float_t' for branch 'PMT_z'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Float_t' for branch 'PMT_dirx'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Float_t' for branch 'PMT_diry'; must be one character.
Warning in <TBranch::TBranch>: Extra characters after type tag 'Float_t' for branch 'PMT_dirz'; must be one character.

 *** Break *** segmentation violation
[/usr/lib/system/libsystem_platform.dylib] _sigtramp (no debug info)
[/Users/diiego/software/GEANT4/GEANT4_10.1/install/lib/libG4event.dylib] G4SPSRandomGenerator::GenRandEnergy() (no debug info)
[/Users/diiego/software/GEANT4/GEANT4_10.1/install/lib/libG4event.dylib] G4SPSEneDistribution::GenArbPointEnergies() (no debug info)
[/Users/diiego/software/GEANT4/GEANT4_10.1/install/lib/libG4event.dylib] G4SPSEneDistribution::GenerateOne(G4ParticleDefinition*) (no debug info)
[/Users/diiego/software/wcsim_replacement/build/WCSim] WCSimPrimaryGeneratorAction::GeneratePrimaries(G4Event*) (no debug info)
[/Users/diiego/software/GEANT4/GEANT4_10.1/install/lib/libG4run.dylib] G4RunManager::GenerateEvent(int) (no debug info)
[/Users/diiego/software/GEANT4/GEANT4_10.1/install/lib/libG4run.dylib] G4RunManager::ProcessOneEvent(int) (no debug info)
[/Users/diiego/software/GEANT4/GEANT4_10.1/install/lib/libG4run.dylib] G4RunManager::DoEventLoop(int, char const*, int) (no debug info)
[/Users/diiego/software/GEANT4/GEANT4_10.1/install/lib/libG4run.dylib] G4RunManager::BeamOn(int, char const*, int) (no debug info)
[/Users/diiego/software/GEANT4/GEANT4_10.1/install/lib/libG4run.dylib] G4RunMessenger::SetNewValue(G4UIcommand*, G4String) (no debug info)
[/Users/diiego/software/GEANT4/GEANT4_10.1/install/lib/libG4intercoms.dylib] G4UIcommand::DoIt(G4String) (no debug info)
[/Users/diiego/software/GEANT4/GEANT4_10.1/install/lib/libG4intercoms.dylib] G4UImanager::ApplyCommand(char const*) (no debug info)
[/Users/diiego/software/GEANT4/GEANT4_10.1/install/lib/libG4intercoms.dylib] G4UIbatch::ExecCommand(G4String const&) (no debug info)
[/Users/diiego/software/GEANT4/GEANT4_10.1/install/lib/libG4intercoms.dylib] G4UIbatch::SessionStart() (no debug info)
[/Users/diiego/software/GEANT4/GEANT4_10.1/install/lib/libG4intercoms.dylib] G4UImanager::ExecuteMacroFile(char const*) (no debug info)
[/Users/diiego/software/GEANT4/GEANT4_10.1/install/lib/libG4intercoms.dylib] G4UIcontrolMessenger::SetNewValue(G4UIcommand*, G4String) (no debug info)
[/Users/diiego/software/GEANT4/GEANT4_10.1/install/lib/libG4intercoms.dylib] G4UIcommand::DoIt(G4String) (no debug info)
[/Users/diiego/software/GEANT4/GEANT4_10.1/install/lib/libG4intercoms.dylib] G4UImanager::ApplyCommand(char const*) (no debug info)
[/Users/diiego/software/wcsim_replacement/build/WCSim] main (no debug info)
[/usr/lib/dyld] start (no debug info)

I suspect the error comes from not having set a G4SPSRandomGenerator. I tried and also crashes.

Maybe I should use the User function from the SetEnergyDisType? When I could use GPS from the macro file that controls the simulation I used the User Defined Histograms and everything worked fine and easily, but now I find this extremely confusing.

Hope you can help me, thanks in advance, Diego