How to save histograms between runs?

Hello all,

I am trying to simulate an experiment with a proton beam, a water phantom and I would like to change a detector position between runs to measure the dose profile.
To do that I realized the simple cycle in a Main.cpp to change detector position between runs:

  for(int i = 0; i<40; i++)  
  {
	  runManager->BeamOn(60000);
	  G4ThreeVector pos = det->GetSensorPosition();
	  pos.setZ(pos.z() +  5. * CLHEP::mm);
	  det->SetSensorPosition(pos);
	  det->UpdateGeometry();
  }

to save the useful data I used Csv:

using G4AnalysisManager = G4CsvAnalysisManager;

and the following code in RunAction:

RunAction::RunAction()
{

// Get/create analysis manager: need to do that in the master and in the workers
// The choice of analysis technology is done via selectin of a namespace
// in Analysis.hh
auto man = G4AnalysisManager::Instance();
man->SetVerboseLevel(1);
G4cout << "Using " << man->GetType() << G4endl;
//Enable activation managment
man->SetActivation(true);

//Set index of the first histogramm
man->SetFirstHistoId(1);

// Book histograms
man->CreateH1(“opt_Angle”,“Angle distribution of light”, 180, 0., 180.);

etc…

void RunAction::BeginOfRunAction(const G4Run* aRun)
{
G4cout << “RunAction::BeginOfRunAction. Run " << aRun->GetRunID() << " start.” << G4endl;

//define output filename
auto man = G4AnalysisManager::Instance();

std::stringstream stm(“”);
stm << “RUN” << std::setw(5) << std::setfill(‘0’) << aRun->GetRunID();
man->OpenFile(stm.str());

etc…

and

void RunAction::EndOfRunAction(const G4Run* aRun)
{
//stop timer
timer->Stop();
G4cout << *timer << G4endl;

G4cout << "RunAction::EndOfRunAction. number of event = " << aRun->GetNumberOfEvent() << G4endl;

// Write histograms to file.
//The call to Write() triggers writing remaining data to all open files and the call to CloseFile() closing all files
//and automatic resetting histograms and ntuples data.
auto man = G4AnalysisManager::Instance();
man->Write();
man->CloseFile();

etc…

if I run the cycle more than 1 times I see a strange thing: at each BeamOn all old histograms are disappear!!! It means when histograms with names RUN00001_.csv are accumulated, the histograms with RUN00000_.csv automatically removed.

Could anybody give me an advice how to save histograms between runs or how to prevent auto-delete
of the saved data?

Many thanks in advance,
Vyacheslav

Hello,
Can you let us know your version of Geant4? There were some fixes made in the patches to avoid deleting files by mistake.
I tested your workflow on B4a example, where I set the file outout type to CSV and changes filename as in your case and the files from Run 0 stays after running more runs.

Best regards,

Dear Ivana,

I am using v11.1.1

Looking around the issue I have found possible solution of my problem. Because I have used multithreading in my application I just add verification of “IsMaster” to OPEN and SAVE data.
At present it save all histograms… but I not checked correctness of data merging yet.

void RunAction::BeginOfRunAction(const G4Run* aRun)
{
if (!IsMaster()) //it is a slave, do nothing else
{
G4cout << “ooo Run " << aRun->GetRunID() << " starts on slave.” << G4endl;
return;
}

//define output filename
auto man = G4AnalysisManager::Instance();

std::stringstream stm(“”);
stm << “RUN” << std::setw(5) << std::setfill(‘0’) << aRun->GetRunID();
man->OpenFile(stm.str());

and

void RunAction::EndOfRunAction(const G4Run* aRun)
{

if (!IsMaster())
{
G4cout << “### Run " << aRun->GetRunID() << " (slave) ended.” << G4endl;
return;
}

// Write histograms to file.
//The call to Write() triggers writing remaining data to all open files and the call to CloseFile() closing all files
//and automatic resetting histogramsand ntuples data.
auto man = G4AnalysisManager::Instance();
man->Write();
man->CloseFile();

Unfortunately my solution doesn’t work correctly…something going wrong else.

I have found possible reason (BUG???) of my problems… after geometry reinitialization different threads
have different RunID!!!

Bag

I just printed it at BeginOfRunAction
void RunAction::BeginOfRunAction(const G4Run* aRun)
{
G4cout << “RunAction::BeginOfRunAction. Run " << aRun->GetRunID() << " start.” << G4endl;

//set file format
auto man = G4AnalysisManager::Instance();
man->SetDefaultFileType("csv");

std::stringstream stm("");
stm << "RUN" << std::setw(5) << std::setfill('0') << aRun->GetRunID();
man->OpenFile(stm.str());

if (!IsMaster()) //it is a slave, do nothing else
{
    G4cout << "ooo Run " << aRun->GetRunID() << " starts on slave." << G4endl;
    return;
}

//Master or sequential
G4cout << "ooo Run " << aRun->GetRunID() << " starts (global)." << G4endl;
if (seed < 0) //not initialized by anybody else
{
    seed = (G4long)time(0);
    G4Random::setTheSeed(seed, luxury);
    G4Random::showEngineStatus();
}

}

therfore the histograms with the name RUN00000 are created