Problem with TTree filling on a GEANT4 Simulation

Good Evening,
i’ve got a little problem while simulating a number of pions (100 GeV) colliding to a simulated calorimeter through GEANT4. Basically I have a lot of entries and the Output fills two trees in my code. Unfortunately I noticed that the memory usage increase a lot while the data fills the tree itself, making my computer completely crash. This is the code where I fill and write my trees and write them into a file.
My pc has 12 GB of ram.

void
CALAnalysisManager::BeginOfRun()
{
	G4String m_pDataFile = "events.root"; 
	m_pOutputFile = new TFile(m_pDataFile, "RECREATE");
	//namefile.open("names.txt");
	
	m_pTree = new TTree("events","Tree that contains info");
	m_pTree1 = new TTree("Total Energy deposited","Tree that contains info about energy"); 
	
	m_pTree1->Branch("E_{TOT}", &m_pEventData->eTot); //energia depositata totale nei gap  
	m_pTree->Branch("E_{DEP}", &m_pEventData->eDep); //Energia depositata per step per singolo hit
	m_pTree->Branch("LayerID", &m_pEventData->layerId);
	m_pTree->Branch("Position", &m_pEventData->position);
	m_pTree->Branch("EventID", &m_pEventData->eventId);
	m_pTree->Branch("ParticleID", &idparticle); //nome particella}

void
Preformatted textCALAnalysisManager::EndOfRun()
{
  m_pOutputFile->cd();
  m_pOutputFile->Write();
  m_pOutputFile->Close();
  //namefile.close();
}


void
CALAnalysisManager::BeginOfEvent(const G4Event* pEvent)
{ 
	G4cout << "\n============================================================\n"
		   << "Begin of event: "<< pEvent->GetEventID() << G4endl
		   << "-------------------------------------------------------------\n";
	
	if (absCollID == -1)
	{
		G4SDManager * pSDManager = G4SDManager::GetSDMpointer();
		absCollID = pSDManager->GetCollectionID("AbsorberHitsCollectionName");
	}
}

void
CALAnalysisManager::EndOfEvent(const G4Event *pEvent)
{
	G4cout<<"ENDOFEVENT\n";
	G4HCofThisEvent * pHCofThisEvent = pEvent->GetHCofThisEvent();
	G4cout<<"GOT THE HC\n";
	CALCalorHitsCollection * absHitsCollection = (CALCalorHitsCollection*) (pHCofThisEvent->GetHC(absCollID));
	G4cout<<"GOT THE abs HC\n";
	
	G4int totEntries = absHitsCollection->entries();
	G4cout<<"GOT totEntries:"<<totEntries<<G4endl;
	G4double eTmp = 0;
	CALCalorHit * absoHit;
	G4cout<<"INIZIO LA LETTURA DELLE ENERGIE\n";
	for (int i=0; i<totEntries; i++)
	{
		absoHit = (*absHitsCollection)[i];
		eTmp += absoHit->getTotalEnergyDeposited();
		
		m_pEventData->eDep = absoHit->getTotalEnergyDeposited();
		m_pEventData->layerId = absoHit->getLayerId();
		m_pEventData->particleId = absoHit->getParticleId();
		names = m_pEventData->particleId;		
		if(names == "pi-")
			idparticle = 0;
		else if(names == "pi+")
			idparticle = 1;
		else if(names == "neutron")
			idparticle = 2;
		else if(names == "proton")
			idparticle = 3;
		else if(names == "gamma")
			idparticle = 4;
		else if(names == "e-")
			idparticle = 5;
		else if(names == "e+")
			idparticle = 6;
		else if(names == "mu+")
			idparticle = 7;
		else if(names == "mu-")
			idparticle = 8;
		else 
			idparticle = -1;
		m_pEventData->position = {absoHit->getHitPos()[0],absoHit->getHitPos()[1],absoHit->getHitPos()[2]};
		m_pEventData->eventId = pEvent->GetEventID();
		//namefile << names << '\n';
		m_pTree->Fill();
	}
	G4cout<<"LETTE\n";
	m_pEventData->eTot = eTmp;
	m_pTree1->Fill();
	m_pEventData->Clear();
}

Could you help me solving this problem please?


_ROOT Version:6.06/8
_Platform:Ubuntu 16.04
_Compiler5.5.0 20171010171010

Hi,

are you sure it is ROOT which is hogging memory? What is “m_pEventData”? Can you confirm that all memory is freed event by event by your data structures?

Cheers,
D

You are right. I just checked and it seems that the problem is not given by the tree, but, probably, by GEANT4 itself. m_pEventData is generated using

m_pEventData = new CALEventData;

which is a user defined class, very simple one.
I can’t confirm that all memory is freed by event.

Hi,

that could hurt.

given that you are persistifying pieces of this structure in the branches, perhaps you can get away with just making m_pEventData a unique_ptr? The syntax will be perhaps everywhere almost the same…

Cheers,
D

Could you give me an example? I’m not sure I got it.

Hi,

maybe you can start from this: https://en.cppreference.com/w/cpp/memory/unique_ptr

Cheers,
D

I just noticed that I called Clear() on that pointer, but the function contained nothing.
I’ll try to see if changing the function works first of all. After that i’ll tru Unique pointer. Thanks for the help.

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