Getting 'malloc' error while making a TH3 from TTrees?

Hi all,

I was trying to make a TH3 histogram by filling data from a set of TTrees. The TTrees are being read from 295 different root files.

At the end of reading all 295 files, I am getting this error; for 10 files, it seems to run fine.

I suggest it’s a memory issue, but not sure how to manage it. Can someone please offer their insight/suggestions as to how to deal with this?

Thanks a lot!

root.exe: malloc.c:2617: sysmalloc: Assertion `(old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)' failed.


#include "TFile.h"
#include "TH1F.h"
#include "TH2F.h"
#include "TTreeReader.h"
#include "TTreeReaderValue.h"
//File I/O
#include <iostream>
#include <fstream>


const int nDet = 8;
const double dt = 2; 


void make_E_correl(){

	//time offsets 
	std::ifstream inputFile("Time_Shift.txt");			// Open the text file for reading	
	if (!inputFile.is_open()) std::cerr << "Error opening file!" << std::endl; 
	    	
	double offsets[nDet+1][nDet+1]; //array to store the offsets
	double shift; int m, n;
	while (inputFile >> m >>  n >>  shift) { 
		offsets[m][n] = shift;
	}	
	//----------------------------------------------------------------------------   

	//Making Histos
	TH1* h_Fat_E = new TH1F("h_Fat_E", "Fatima Energy All Crystals", 1200, 0, 1200);  
	h_Fat_E->SetXTitle("Ch No.");
	h_Fat_E->SetYTitle("Counts/keV");  
	 
	
	TH1* E_Shift_add = new TH1F("h_Fat_Eshift", "Fatima Energy Shifted All Crystals", 1200, 0, 1200);  
	E_Shift_add->SetXTitle("Ch No.");
	E_Shift_add->SetYTitle("Counts/keV");  
	
	TH1* E_Raw[9],  *E_Shift[9];
	for (int i = 1; i < 9; i ++){
		TString name = Form("E_Raw_Det_%d", i);
		TString name2 = Form("E_Shift_Det_%d", i);
		E_Raw[i] = new TH1F(name, name, 1200, 0, 1200);
		E_Shift[i] = new TH1F(name2, name2, 1200, 0, 1200);
	}
	
	TH2* gg_mat = new TH2F("gg_mat", "#gamma-#gamma LaBr3" , 1200, 0, 1200, 1200, 0, 1200);
	gg_mat->SetOption("colz");
	
	TH2* gg_mat_Ge = new TH2F("labr_labr_Ge", "LaBr_{3}-LaBr_{3}-Ge_Gate" , 1200, 0, 1200, 1200, 0, 1200);

	TH3 *gg_dt_cube = new TH3F("labr_labr_dt", "LaBr_{3}-LaBr_{3}-dt", 1200, 0, 1200, 1200, 0, 1200, 400, -5, 5); //gamma - gamma -dt cube



	//File Loop
	for (int file_num = 1; file_num <= 295; file_num++) {

		TFile* dataFile;
		dataFile = TFile::Open(Form("Trees/f6_%04d.root", file_num) , "READ");
		
		
		if (!dataFile || dataFile->IsZombie()) {
		      cout << "File is Zombie" << endl;
		      return;
		}		
		
		//Storing branch data in variables
		TTreeReader Reader("AnalysisxTree", dataFile);
		TTreeReaderArray<Double_t> Fat_E(Reader,"AnlEvent.QDC_E[9]");
		TTreeReaderValue<Int_t> Fat_mult(Reader,"AnlEvent.mult");
		TTreeReaderArray<Long64_t> Fat_T(Reader,"AnlEvent.TDC_T[9]");
		TTreeReaderArray<Int_t> Fat_E_ID(Reader,"AnlEvent.QDC_ID[9]");
		TTreeReaderArray<Int_t> Fat_T_ID(Reader,"AnlEvent.TDC_ID[9]");
		TTreeReaderArray<Double_t> Ge_E(Reader,"AnlEvent.E[2][7]");
		
		
		int evt_num = 0; 
		while (Reader.Next()) {

			for (int crys = 0; crys < 14; crys++) {		
			
				if ( Ge_E[crys] > 125.9 && Ge_E[crys] < 131.9)  { //EB range crys =  7 - 13
					
					for(int i=0; i<*Fat_mult; i++) {
					
						for (int j = i+1; j<*Fat_mult; j++){
						
						        double t_ij = (Fat_T[i] - Fat_T[j])*0.025; 
							
							double c_ij = offsets[ Fat_E_ID[i] ][ Fat_E_ID[j] ]; 
							double t_ij_align = t_ij - c_ij; 
								
							if (TMath::Abs(t_ij_align) < dt)
								gg_mat_Ge->Fill(Fat_E[i], Fat_E[j]);
								gg_mat_Ge->Fill(Fat_E[j], Fat_E[i]);
							
							
							gg_dt_cube->Fill(Fat_E[i], Fat_E[j], t_ij_align);
							
						}
						
					}
				}
			} 
			evt_num++;
		}
		cout << "Processed: " << evt_num << " events." << endl;		
	}	
	
	//Output  File
	TFile *f = new TFile ("ggdt_cube.root", "RECREATE");
	f->mkdir("Energy");
	f->cd("Energy"); 
	h_Fat_E->Write();
	gg_mat->Write();
	gg_mat_Ge->Write();
	gg_dt_cube->Write();
	f->Close();
	
}

In your "for loop"you open the 295 root file one by one and read data from each on them to fill histograms. But you never close the files. May be closing each file after the “while loop” will help ?

1 Like

Dear @couet,

Thank you for the input, I have now done

dataFile->Close();

after the 'while()" loop. I’ll get back with the results.

In the meantime, could you please share if it is possible to do this directly using a ‘TTree’? For context, I am trying to look at a coincidence matrix at a given time window, dt.
So basically if(once) the TH3 histogram is done. I need to take a projection in yx (Energy vs. Energy) for a given range in z(dt).

Not sure if this can be also done using TTree ?

Thank You

I am not sure I fully understand your problem, but, seems to me, it might be easier to make a chain with all your TTree and access them such as they are a single TTree. Then, once your TH3 is filled, you can do the projections you need.

Thank You. I’ll try using a TChain.

Dear @couet ,
So I am getting a *** Break *** segmentation violation after the script reads all the files.

Can you please help in this ?

he lines below might hint at the cause of the crash. If you see question
marks as part of the stack trace, try to recompile with debugging information
enabled and export CLING_DEBUG=1 environment variable before running.
You may get help by asking at the ROOT forum https://root.cern/forum
Only if you are really convinced it is a bug in ROOT then please submit a
report at https://root.cern/bugs Please post the ENTIRE stack trace
from above as an attachment in addition to anything else
that might help us fixing this issue.
===========================================================
#6  0x0000776c920d89e4 in TBufferFile::WriteFastArray(float const*, int) () from /home/frena_storage/akashrup/root_v6.26/install/lib/libRIO.so
#7  0x0000776c930f45ef in TStreamerBase::WriteBuffer(TBuffer&, char*) () from /home/frena_storage/akashrup/root_v6.26/install/lib/libCore.so
#8  0x0000776c9236e87c in int TStreamerInfo::WriteBufferAux<char**>(TBuffer&, char** const&, TStreamerInfo::TCompInfo* const*, int, int, int, int, int) () from /home/frena_storage/akashrup/root_v6.26/install/lib/libRIO.so
#9  0x0000776c921b9524 in TStreamerInfoActions::GenericWriteAction(TBuffer&, void*, TStreamerInfoActions::TConfiguration const*) () from /home/frena_storage/akashrup/root_v6.26/install/lib/libRIO.so
#10 0x0000776c920e0235 in TBufferFile::WriteClassBuffer(TClass const*, void*) () from /home/frena_storage/akashrup/root_v6.26/install/lib/libRIO.so
#11 0x0000776c9219140d in TKey::TKey(TObject const*, char const*, int, TDirectory*) () from /home/frena_storage/akashrup/root_v6.26/install/lib/libRIO.so
#12 0x0000776c9214e0c9 in TFile::CreateKey(TDirectory*, TObject const*, char const*, int) () from /home/frena_storage/akashrup/root_v6.26/install/lib/libRIO.so
#13 0x0000776c9213eb61 in TDirectoryFile::WriteTObject(TObject const*, char const*, char const*, int) () from /home/frena_storage/akashrup/root_v6.26/install/lib/libRIO.so
#14 0x0000776c92ff3a4d in TObject::Write(char const*, int, int) const () from /home/frena_storage/akashrup/root_v6.26/install/lib/libCore.so
#15 0x0000776c846d7843 in ?? ()
#16 0x00007ffe9f89c260 in ?? ()
#17 0x0000776c8e7bde25 in llvm::RuntimeDyldELF::loadObject(llvm::object::ObjectFile const&) () from /home/frena_storage/akashrup/root_v6.26/install/lib/libCling.so
#18 0xbff9e8c692f6e829 in ?? ()
#19 0x4005d2ad81adea89 in ?? ()
#20 0x4012ddbf487fcb92 in ?? ()
#21 0x0000000000000000 in ?? ()
===========================================================

We need run-able script to understand what’s wrong.

Thank You for the reply.

Though not sure how to share the 295 root Trees for running the script. Any suggestions ?

You can try to produce a minimal test reproducing the issue. Does it crash with 1 file ? 2 files ?..