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

**URL:** <https://root-forum.cern.ch/t/getting-malloc-error-while-making-a-th3-from-ttrees/59326>\
**Category:** Newbie\
**Created:** [May 14, 2024, 5:13am UTC](https://root-forum.cern.ch/t/getting-malloc-error-while-making-a-th3-from-ttrees/59326 "2024-05-14T05:13:43Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![utkarsh-2000](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/u/e47774/32.png) [@utkarsh-2000](https://root-forum.cern.ch/u/utkarsh-2000)\
**Post date:** [May 14, 2024, 5:13am UTC](https://root-forum.cern.ch/t/getting-malloc-error-while-making-a-th3-from-ttrees/59326/1 "2024-05-14T05:13:43Z")

</div>

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!

```auto
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.

```

```auto

#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();
	
}

```

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [May 14, 2024, 5:47am UTC](https://root-forum.cern.ch/t/getting-malloc-error-while-making-a-th3-from-ttrees/59326/2 "2024-05-14T05:47:43Z")

</div>

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 ?

---

<div class="post-metadata">

**Author:** ![utkarsh-2000](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/u/e47774/32.png) [@utkarsh-2000](https://root-forum.cern.ch/u/utkarsh-2000)\
**Post date:** [May 14, 2024, 6:13am UTC](https://root-forum.cern.ch/t/getting-malloc-error-while-making-a-th3-from-ttrees/59326/3 "2024-05-14T06:13:32Z")

</div>

Dear @couet,

Thank you for the input, I have now done

```auto
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

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [May 14, 2024, 6:29am UTC](https://root-forum.cern.ch/t/getting-malloc-error-while-making-a-th3-from-ttrees/59326/4 "2024-05-14T06:29:23Z")

</div>

I am not sure I fully understand your problem, but, seems to me, it might be easier to make [a chain](https://root.cern/doc/master/classTChain.html) 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.

---

<div class="post-metadata">

**Author:** ![utkarsh-2000](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/u/e47774/32.png) [@utkarsh-2000](https://root-forum.cern.ch/u/utkarsh-2000)\
**Post date:** [May 14, 2024, 6:36am UTC](https://root-forum.cern.ch/t/getting-malloc-error-while-making-a-th3-from-ttrees/59326/5 "2024-05-14T06:36:12Z")

</div>

Thank You. I’ll try using a TChain.

---

<div class="post-metadata">

**Author:** ![utkarsh-2000](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/u/e47774/32.png) [@utkarsh-2000](https://root-forum.cern.ch/u/utkarsh-2000)\
**Post date:** [May 14, 2024, 8:37am UTC](https://root-forum.cern.ch/t/getting-malloc-error-while-making-a-th3-from-ttrees/59326/6 "2024-05-14T08:37:19Z")

</div>

> [@utkarsh-2000](#):
>
> I’ll get back with the results.

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

Can you please help in this ?

```auto
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 ?? ()
===========================================================

```

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [May 14, 2024, 8:43am UTC](https://root-forum.cern.ch/t/getting-malloc-error-while-making-a-th3-from-ttrees/59326/7 "2024-05-14T08:43:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![utkarsh-2000](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/u/e47774/32.png) [@utkarsh-2000](https://root-forum.cern.ch/u/utkarsh-2000)\
**Post date:** [May 14, 2024, 9:01am UTC](https://root-forum.cern.ch/t/getting-malloc-error-while-making-a-th3-from-ttrees/59326/8 "2024-05-14T09:01:26Z")

</div>

Thank You for the reply.

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

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [May 14, 2024, 9:04am UTC](https://root-forum.cern.ch/t/getting-malloc-error-while-making-a-th3-from-ttrees/59326/9 "2024-05-14T09:04:48Z")

</div>

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