In my application I would like to change world/detector geometry between runs and save histograms and ntuples as CSV files with unique names based on RunID for each run. And I use MT to reduce computation time.
In reality I see that G4 creates ntuples for Master and Workers threads as expected and it just removes empty ntuple for Master after end of the run. But for histograms the situation quite different!
Geant4 create histograms only once with RunID = 0 and after that it ignores OpenFile() with a new name and overwrites existing histograms for each new run.
Is it possible to save histograms and ntuples with unique names for multiple consecutive runs???
Yes, you are right - AnaEx03 works fine, but it uses hard-coded output file names “e-”, “proton” only.
In my case, I make many consecutive Runs in cycle and a file name generation using “GetRunID()” is the simplest solution. Unfortunately, it not works correctly for histograms. It is definitely some bug in code “GetRunID”, because when I use stupid solution in my code with private static variable, all works fine!
I was just hoping to hear that maybe there is another explanation for this effect.
My stupid solution:
class RunAction : public G4UserRunAction
{
...
static G4int nGloabalID;
...
}
and in *.cc file:
G4int RunAction:: nGloabalID = 0;
void RunAction::BeginOfRunAction(const G4Run* aRun)
{
...
//set file format
std::stringstream stm("");
stm << "RUN" << std::setw(5) << std::setfill('0') << nGloabalID;
man->OpenFile(stm.str());
...
}
void RunAction::EndOfRunAction(const G4Run* aRun)
{
G4cout << "RunAction::EndOfRunAction. number of event = " << aRun->GetNumberOfEvent() << G4endl;
auto man = G4AnalysisManager::Instance();
// Save histograms
man->Write();
man->CloseFile();
if (!IsMaster())
{
G4cout << "### Run " << aRun->GetRunID() << " (slave) ended." << G4endl;
}
else
{
G4cout << "### Run " << aRun->GetRunID() << " (global) ended." << G4endl;
//Increment RUNID for master only
nGloabalID++;
}
}
By default, histograms are created only once, at the first run, and they are reset with each CloseFile call. It is possible to disable this resetting histograms with calling CloseFile with false argument. Then the content of the histograms will be preserved.
In my case, I make many consecutive Runs in cycle and a file name generation using “GetRunID()” is the simplest solution. Unfortunately, it not works correctly for histograms.
GetRunID() has nothing to do with histograms and if used in RunAction::BeginOfRunAction, it should work. Could you give more details why you find it failing on your side ? (Eg. post your output)
And also, which version of Geant4 do you use ? Thank you.