Ability to delete histograms after creation

Hi,

I’m writing a simulation with different geometries, using 2D and 3D histos to store the dose output. I am creating them at the time that the geometry is created. If I change the geometry using a macro, I clean the volume stores and rebuild the world with the new geometry and create new histograms. However, my old histograms persist and take up space in memory.

Is there an equivalent method or way to clear the histograms in the analysis manager?

I’ve been implementing something doing this:

G4AnalysisManager* analysisManager = G4AnalysisManager::Instance();

G4int n_H1s = analysisManager->GetNofH1s();
G4int n_H2s = analysisManager->GetNofH2s();
G4int n_H3s = analysisManager->GetNofH3s();

if (n_H1s > 0)
{
	for (G4int i=0; i<n_H1s; i++)
	{
		auto hist = analysisManager->GetH1(i);
		delete hist;
	}	
}
if (n_H2s > 0)
{
	for (G4int i=0; i<n_H2s; i++)
	{
		auto hist = analysisManager->GetH2(i);
		delete hist;
	}	
}
if (n_H3s > 0)
{
	for (G4int i=0; i<n_H3s; i++)
	{
		auto hist = analysisManager->GetH3(i);
		delete hist;
	}	
}

but analyisManager, or G4ToolsAnalysisManager is trying to save a “deleted” histogram. This makes sense seeing as I’m simply de-allocating the memory without telling the analysisManager to forget about the histogram too.

Hello,

At present, the analysis manager does not provide functions to delete individual histograms. This is considered for the future release.
You can however change the existing histograms properties via SetHn[Pn] functions, or equivalent commands.
Or, it is also possible to delete the analysis manager instance, and then create a new one.

Best regards,

Hi,

this topic is a bit old, but I am having the same problem as OP. I would like to change the name of my histogram for different runs, but that is not possible. I have tried to delete G4AnalysisManager but the program is crashing.

Would you be so kind and provide an example ho to delete and recreate G4AnalysisManager? And where in code to do it?

Thank you,
Martin

Hi
,
You can change most of histogram properties, including a title, but we do not provide a function to set a histogram name.

Adding a possibility of deleting selected histograms is in our work plan; we can also take a look if adding setName function for changing the objects names would not collide with the recently added possibility of writing the same object multiple times.

With the current version, I can only suggest to create all histograms that you need and use the activation option to activate only the useful ones for each run. In case of large number of histograms you may set the number of bins for the inactivated ones to a low number (1), to avoid huge memory consumption.

Best regards,

Thank you for your reply @ivana. I have solved my problem by calling G4AnalysisManager::Clear() at the G4UserRunAction::EndOfRunAction() method.

Kind regards,
Martin