Ntuple saving leads to EXC_BAD_ACCESS in Hadr04

Hi, Ive been trying to make a new simulation to extract some information from Hadr04 with a fresh Geant4.11 install (focusing on neutrons and opticalphotons). I removed the H1 histograms since I do not require them, but when I only attempt to save ntuples I get this error saying that I am saying to a null pointer (using lldb)

--> G4TaskRunManager::CreateAndStartWorkers() --> Creating 1 tasks with 1 events/task...

Adding task 0 to task-group...
G4WT2 > ### Run 0 starts on worker thread 2.
G4WT2 > Photon!
G4WT2 > EVENTID 0 TRACKID 999 MOM (1.945e-07,7.63403e-07,1.02818e-06) POS (224.647,884.317,1190.6) ENERGY - 1.29529e-06 TIME 6.58153 PARENT opticalphoton
G4WT2 > Copying Photons...
Process 93946 stopped
* thread #5, stop reason = EXC_BAD_ACCESS (code=1, address=0x0)
    frame #0: 0x000000010002003a Hadr04`SteppingAction::UserSteppingAction(G4Step const*) [inlined] G4VAnalysisManager::FillNtupleDColumn(this=0x0000000105fc8030, ntupleId=1, columnId=1, value=224.64703205946708) at G4VAnalysisManager.icc:143:27 [opt]
   140 	G4bool G4VAnalysisManager::FillNtupleDColumn(G4int ntupleId, G4int columnId,
   141 	                                            G4double value)
   142 	{
-> 143 	  return fVNtupleManager->FillNtupleDColumn(ntupleId, columnId, value);
   144 	}
   146 	//_____________________________________________________________________________
Target 0: (Hadr04) stopped.
warning: Hadr04 was compiled with optimization - stepping may behave oddly; variables may not be available.

If I only print the values out with multithreading turned on I manage to run the program with no problems - now I just got to get them saved to root files!

I notice also that the program mentions

--> G4TaskRunManager::CreateAndStartWorkers() --> Creating 1 tasks with 1 events/task...

Yet there appear to be multiple workers if I run with the default

./Hadr04 <macro> <nthreads>

I guess this could be a symptom of my problem :thinking:

RunAction.cc (4.2 KB)
SteppingAction.cc (7.9 KB)
HistoManager.cc (8.2 KB)

Thanks for any help!

_Geant4 Version:_4.11.0 (Multithreaded)
_Operating System:_Mac OS Monterey
_Compiler/Version:_Apple clang version 14.0.0
_CMake Version:_3.28.1

So since I am using multithreading, would it be better to change




? I could see another version of this class written by @ivana in the extended/parallel/MPI/ examples (so called such as G4RootMpiAnalysisManager), though I am not certain how or which to implement (maybe it is exactly what I would need but not sure if Worker Threads = MPI in this case).
Thanks again for any advice!


Please, keep using G4AnalysisManager.

I see in your code a mismatch in ntupleId. In HistoManager you set


But then, you use ‘0’ in

analysisManager->SetNtupleActivation(0,true);     //enable inactivation of histograms

and also in SteppingAction:


So I suggest first to check, that you use consistently either firstID = 1, and then fill ntuple with ID = 1, 2; or you remove the SetFirstNtupleId(1) call and fill ntuple with ID = 0, 1.

Best regards,

Hi @ivana, ahhh I’m so sorry for missing this! I amended this although unfortunately I wasn’t able to confirm a root file being generated and the same error persisted.

I modified the physics list based on the other hadronic examples and also the macro file (all of which are linked below): I am really thankful for your guidance!
Hadr04.cc (4.3 KB)

ActionInitialization.hh (2.2 KB)
DetectorConstruction.hh (3.2 KB)
DetectorMessenger.hh (2.8 KB)
ElectromagneticPhysics.hh (2.7 KB)
GammaNuclearPhysics.hh (550 Bytes)
HadronElasticPhysicsHP.hh (2.5 KB)
HistoManager.hh (2.3 KB)
NeutronHPphysics.hh (2.4 KB)
PhysicsList.hh (2.1 KB)
PrimaryGeneratorAction.hh (2.6 KB)
RadioactiveDecayPhysics.hh (2.3 KB)
Run.hh (3.3 KB)
RunAction.hh (3.0 KB)
StackingAction.hh (2.3 KB)
SteppingAction.hh (2.4 KB)
TrackingAction.hh (2.5 KB)
ActionInitialization.cc (3.1 KB)
DetectorConstruction.cc (9.5 KB)
DetectorMessenger.cc (5.4 KB)
ElectromagneticPhysics.cc (6.6 KB)
GammaNuclearPhysics.cc (1.6 KB)
HadronElasticPhysicsHP.cc (3.9 KB)
HistoManager.cc (4.1 KB)
NeutronHPphysics.cc (6.2 KB)
PhysicsList.cc (5.9 KB)
PrimaryGeneratorAction.cc (3.6 KB)
RadioactiveDecayPhysics.cc (4.1 KB)
Run.cc (9.6 KB)
RunAction.cc (4.2 KB)
StackingAction.cc (2.9 KB)
SteppingAction.cc (7.1 KB)
TrackingAction.cc (3.7 KB)
run01.cc (409 Bytes)

There are still some problems left;

  1. First you are still using G4RootAnalysisManager::Instance() in your RunAction ;
    this should be replaced with G4AnalysisManager::Instance()

  2. analysisManager->SetNtupleMerging(true); should be performed before creating any analysis object, so you can move this call in HistoManager::Book() just after the line

   G4AnalysisManager* analysisManager = G4AnalysisManager::Instance();
   analysisManager->SetNtupleMerging(true);     // HERE
  1. And finally, as you do not use the “Activation” feature, you should remove the test before calling OpenFile in RunAction::BeginOfRunAction :
  if ( analysisManager->IsActive() ) {
  // ...

as it returns false when the “activation” option is not set on, and also the std::cin.get(); call is not useful here at all, so your code can be simplified to:

    auto analysisManager = G4AnalysisManager::Instance();
    if (!analysisManager->OpenFile()) {
       G4cout << "!*!*!*!*!*!* ERROR OPENING FILE !*!*!*!*!*!*" << G4endl;

I hope this helps.

Thanks so much @ivana! these changes caused the root files to be made :slight_smile:
I tried to run the program with run01.cc as the macro, and it generated three files, Ice.root, Ice_t2.root and Ice_t8.root. When I load the Ice.root using ROOT6 I see the following message:

Attaching file Ice.root as _file0... Warning in <TFile::Init>: no StreamerInfo found in Ice.root therefore preventing schema evolution when reading this file. The file was produced with version 4.00/00 of ROOT. (TFile *) 0x7fa9842cca20

It looks like the file is blank, even though it generated?

I could see in the std::out output though at least optical photons were generated (some of which should satisfy the condition in the stepping action I think!)

`The run is 1 e- of 50 GeV through 1.5 m of Ice (density: 920 mg/cm3)

Process calls frequency :
Cerenkov= 38009 NoProcess= 7449 OpAbsorption= 1286181 Rayl= 387 Scintillation= 44 Transportation= 310739 annihil= 19 compt= 5849 conv= 81 eBrem= 1228 eIoni= 17500 hIoni= 17 ionIoni= 1 msc= 8 phot= 845 photonNuclear= 1

Nb of incident particles surviving after 1.5 m of Ice : 310739

Parcours of incident neutron:
nb of collisions E>1eV= 0 E<1eV= 0 total= 0
track length E>1eV= 0 fm E<1eV= 0 fm total= 0 fm
time of flight E>1eV= 0 ps E<1eV= 0 ps total= 0 ps

List of generated particles:
N15: 1 Emean = 618.97 keV ( 618.97 keV → 618.97 keV)
e+: 6616 Emean = 230.86 MeV ( 246.02 keV → 2.3551 GeV)
e-: 22214 Emean = 1.1005 GeV ( 100.24 eV → 50 GeV)
gamma: 1343 Emean = 65.137 MeV ( 2.8361 keV → 14.663 GeV)
opticalphoton: 1596444 Emean = 2.6137 eV ( 1.2523 eV → 3.9515 eV )
proton: 1 Emean = 7.8081 MeV ( 7.8081 MeV → 7.8081 MeV)`

With the files ending in the t suffixes, the errors look like

Attaching file Ice_t2.root as _file0... Error in <TFile::ReadBuffer>: error reading all requested bytes from file Ice_t2.root, got 259 of 300 Error in <TFile::Init>: Ice_t2.root failed to read the file type data. (TFile *) nullptr

I guess the fact multiple root files still exists means the merging failed somehow? Once again thank you so much for your help!

Screenshot 2024-03-29 at 23.28.41


I noticed that you have an old Geant4 version, 11.0. Did you get all the patches to this version ? There were released 4 patches for this version, and some contained also fixes in analysis.

I checked your code on my side with the latest version (Geant4 11.2.p1) and your macro runs ok on my side and produces just one Root file.

Best regards,

Ah! I am sorry for this, I was not aware there were such patches - I will upgrade my geant version. Honestly thank you so much for your help! :pray: