Multiple runs give empty histograms

Geant4 Version: 11.2.1
Operating System: Fedora 40
Compiler/Version: gcc 14.1.1
CMake Version: 3.28.2


Hello people, first post here. I’m a geant newbie.
My issue is pretty simple.
I have a simulation that has to save data to histograms. The histogram is created inside BeginOfRunAction() with the following code

auto analysisManager = G4AnalysisManager::Instance();
G4String fileName = "B4.root";
analysisManager->OpenFile(fileName);
analysisManager->CreateH1("Counts", "Revealed neutrons", 100, 1e-12*GeV, 10*GeV, "GeV", "none", "log");

The histogram is then filled in EndOfEventAction() like this

if (fEdep >= 0.01*MeV) {
    analysisManager->FillH1(1, particleEnergy);
}

(the ID is indeed 1 because there is also the edep histogram)
then in EndOfRunAction() there is

auto analysisManager = G4AnalysisManager::Instance();
analysisManager->Write();
analysisManager->CloseFile();

I then have a run.mac macro. If in the macro I put exclusively

/gun/particle neutron
/gun/energy 1 MeV
/run/beamOn 10000

everything works fine for whatever energy: I get my B4.root file that has the values I want.
The problem though is that I have to run this using multiple energies, so what I would like to do is put inside my macro multiple runs like this

/gun/particle neutron
/gun/energy 1 MeV
/run/beamOn 10000
/gun/energy 10 MeV
/run/beamOn 10000
/gun/energy 100 MeV
/run/beamOn 10000

If I do this, inside B4.root multiple histograms are created (the correct number) called Counts;1 Counts;2 and Counts;3 BUT only ONE of them contains any entries (only for one energy and not even the same amount that running that energy alone gives me), the other two are empty. Mind you that running 10 MeV or 100MeV alone does instead give me entries.
The ideal thing would be having exclusively one histogram with all the entries, but as far as I can understand you create histograms at the begin of RunAction and one beamOn command constitutes a run. The middle ground of having different histograms and opening them up manually (or with a root macro) to get the data out is fine, but having every single time to change the macro > recompile everything and rerun is a bit tedious.
Do you have an idea as to why this might be? Thank you for the help.

I have tried going with

analysisManager->FillH1(analysisManager->GetH1Id("Counts;1"), particleEnergy);

and that gives three empty histograms, which to me makes absolutely no sense.

Hi @leonardo-lena

Can you try to add in function → beginofrunaction(){…

G4RootAnalysisManager* analysisManager = G4RootAnalysisManager::Instance();
G4String fileName="lenaRoot";
  
if(!analysisManager->GetFileName().empty())
    fileName = analysisManager->GetFileName();
  
  analysisManager->OpenFile(fileName);

In your macro
before → /run/beamOn call → /analysis/setFilename xyz.root and so on.

Reference:[1] #include “G4RootAnalysisManager.hh”
[2] Geant4: G4RootAnalysisManager Class Reference

VRS

This method gives me an error

***** COMMAND NOT FOUND </analysis/setFilename first.root> *****

***** Batch is interrupted!! *****

EDIT:
solving the typo of setFileName, I then ran a macro containing

/gun/particle neutron
/gun/energy 1e-7 GeV
/analysis/setFileName first.root
/run/beamOn 10000
/gun/energy 1e-6 GeV
/analysis/setFileName second.root
/run/beamOn 10000

This gives no second.root file and seven versions of first.root called first.root, first_t0.root — first_t7.root
Using TBrowser to open them up, it fails to do so giving error

Error in <TFile::ReadBuffer>: error reading all requested bytes from file first.root, got 178 of 300
Error in <TFile::Init>: first.root failed to read the file type data.
(TFile *) 0x5636e4ee27c0

and analogous ones for all the other files.

Hi @leonardo-lena

This will go in constructor.

And remake your project and run the macro please.

VRS

Did it, same result.
Noticed that the job ends with a “segmentation fault, core dumped”.
Replacing the G4RootAnalysisManager with a G4AnalysisManager did work out though, so now I currently do get different histograms that I can then merge up with a macro. Thank you!

For anybody coming here with the same problem: instantiate the G4AnalysisManager and the histogram in your constructor, then in BeginOfRunAction()

G4String fileName="lenaRoot";
G4AnalysisManager* analysisManager = G4AnalysisManager::Instance();
if(!analysisManager->GetFileName().empty()) {
  fileName = analysisManager->GetFileName();
}
analysisManager->OpenFile(fileName);

Then the macro built like this

/gun/energy 1e-7 GeV
/analysis/setFileName first.root
/run/beamOn 10000
/gun/energy 1e-6 GeV
/analysis/setFileName second.root
/run/beamOn 10000

this will create one file per run. Unfortunately I cannot get them all to go in the same file, but it’s fine enough, I can then just make a simple python script to open up all the root files and get my final result that way.

use hadd to merge all files.

Happy that problem solved.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.