# Bad Allocation Error looping though TTree

**URL:** <https://root-forum.cern.ch/t/bad-allocation-error-looping-though-ttree/53911>\
**Category:** ROOT\
**Tags:** tree\
**Created:** [March 7, 2023, 10:49am UTC](https://root-forum.cern.ch/t/bad-allocation-error-looping-though-ttree/53911 "2023-03-07T10:49:25Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![DistantOstrich](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/d/e47774/32.png) [@DistantOstrich](https://root-forum.cern.ch/u/DistantOstrich)\
**Post date:** [March 7, 2023, 10:49am UTC](https://root-forum.cern.ch/t/bad-allocation-error-looping-though-ttree/53911/1 "2023-03-07T10:49:26Z")

</div>

* * *

\_ROOT Version: 6.26  
\_Platform: Windows 10  
\_Compiler: Visual Studio

* * *

I’m trying to loop through the entries in a TTree from a .root file, but I’m getting bad\_alloc errors. The files are 80 - 300 MB in size. I believe I’m running out of memory, as I don’t get the error on smaller files (~ 1 MB).

I have tried both python and C++, and get the same behaviour. Is there a way of looping through the entries in a TTree that doesn’t load the whole thing into memory?

Alternatively, what I’m trying to do is take a root file containing data from several hours of an experiment and split it into several root files, based on the timestamp of each event, to check for changes in the detector response over time. So for example, get a separate root file for each hour of data. Is there a way to do this that doesn’t involve looping through the whole TTree?

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [March 7, 2023, 10:53am UTC](https://root-forum.cern.ch/t/bad-allocation-error-looping-though-ttree/53911/2 "2023-03-07T10:53:38Z")

</div>

> [@DistantOstrich](#):
>
> a way of looping through the entries in a TTree that doesn’t load the whole thing into memory

That’s precisely what ROOT does.  
It looks like you have some bug(s) in your code (you “allocate” space and do not “release” it).

---

<div class="post-metadata">

**Author:** ![DistantOstrich](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/d/e47774/32.png) [@DistantOstrich](https://root-forum.cern.ch/u/DistantOstrich)\
**Post date:** [March 7, 2023, 11:14am UTC](https://root-forum.cern.ch/t/bad-allocation-error-looping-though-ttree/53911/3 "2023-03-07T11:14:59Z")

</div>

This C++ snippet produces the bad\_alloc error:

```cpp
std::string filename = "C:\\runxxx.root";
TFile* tf = TFile::Open(filename.c_str(), "READ");

	TTree* tree = dynamic_cast<TTree*>(tf->Get("Board 0"));

ULong64_t timeStamp;
	UShort_t energy;
	UShort_t channel;
	tree->SetBranchAddress("timeStamp", &timeStamp);
	tree->SetBranchAddress("energy", &energy);
	tree->SetBranchAddress("channel", &channel);

	float ts = 0;
	int nEntries = tree->GetEntriesFast();
	for (int i = 0; i < nEntries; i++) {

		tree->GetEntry(i);

		if (timeStamp > ts) {
			ts = timeStamp;
		}
	}

```

(Sorry about the code formatting)

Could you please explain where I allocate the space and how I would release it after each iteration?

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [March 7, 2023, 11:22am UTC](https://root-forum.cern.ch/t/bad-allocation-error-looping-though-ttree/53911/4 "2023-03-07T11:22:10Z")

</div>

You allocate nothing in this piece, but this is not your complete routine.

Inside the “for” loop, use:  
`if (tree->GetEntry(i) <= 0) break; // we are done`

At the end of your routine (after the “for” loop), you should:  
`delete tf; // automatically deletes the tree, too`

BTW. Try (check branch types): `tree->Print();`

---

<div class="post-metadata">

**Author:** ![DistantOstrich](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/d/e47774/32.png) [@DistantOstrich](https://root-forum.cern.ch/u/DistantOstrich)\
**Post date:** [March 7, 2023, 12:12pm UTC](https://root-forum.cern.ch/t/bad-allocation-error-looping-though-ttree/53911/5 "2023-03-07T12:12:53Z")

</div>

I tried changing my GetEntry(i) call to

```cpp
 if (tree->GetEntry(i) <= 0) break; 

```

and adding delete tf at the end of the loop, but I’m still getting the same bad\_alloc error during the loop. The error happens after around 200,000 iterations, but not always at exactly the same one.

My code that produces the error now reads

```cpp
void testSnippet() {

	std::string filename = "C:\\run277_lf.root";
	TFile* tf = TFile::Open(filename.c_str(), "READ");

	TTree* tree = dynamic_cast<TTree*>(tf->Get("Board 0"));

	tree->Print();

	ULong64_t timeStamp;
	tree->SetBranchAddress("timeStamp", &timeStamp);

	float ts = 0;
	int nEntries = tree->GetEntriesFast();
	for (int i = 0; i < nEntries; i++) {

		if (tree->GetEntry(i) <= 0) {
			break;
		}

		if (timeStamp > ts) {
			ts = timeStamp;
		}
	}
	delete tf;
	std::cout << ts << std::endl;
}

```

The output of tree-\>Print() is the following:

```auto
******************************************************************************
*Tree :Board 0 : Board 0 *
*Entries : 19457543 : Total = 331056491 bytes File Size = 111694493 *
* : : Tree compression factor = 2.96 *
******************************************************************************
*Br 0 :pu : pu/O *
*Entries : 19457543 : Total Size= 19473930 bytes File Size = 131197 *
*Baskets : 175 : Basket Size= 1317888 bytes Compression= 148.40 *
*............................................................................*
*Br 1 :satu : satu/O *
*Entries : 19457543 : Total Size= 19474288 bytes File Size = 103519 *
*Baskets : 175 : Basket Size= 1317888 bytes Compression= 188.09 *
*............................................................................*
*Br 2 :lost : lost/O *
*Entries : 19457543 : Total Size= 19474288 bytes File Size = 103519 *
*Baskets : 175 : Basket Size= 1317888 bytes Compression= 188.09 *
*............................................................................*
*Br 3 :channel : channel/s *
*Entries : 19457543 : Total Size= 38948181 bytes File Size = 449540 *
*Baskets : 338 : Basket Size= 2635776 bytes Compression= 86.62 *
*............................................................................*
*Br 4 :cfd : cfd/s *
*Entries : 19457543 : Total Size= 38946813 bytes File Size = 205429 *
*Baskets : 338 : Basket Size= 2635264 bytes Compression= 189.55 *
*............................................................................*
*Br 5 :timeStamp : timeStamp/l *
*Entries : 19457543 : Total Size= 155790759 bytes File Size = 86452034 *
*Baskets : 1314 : Basket Size= 25600000 bytes Compression= 1.80 *
*............................................................................*
*Br 6 :energy : energy/s *
*Entries : 19457543 : Total Size= 38947839 bytes File Size = 24222836 *
*Baskets : 338 : Basket Size= 2635264 bytes Compression= 1.61 *
*............................................................................*

```

I assume the /s /l /O after the branch names are the data type (short, long, boolean)?

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [March 7, 2023, 12:22pm UTC](https://root-forum.cern.ch/t/bad-allocation-error-looping-though-ttree/53911/6 "2023-03-07T12:22:07Z")

</div>

So, the “`testSnippet`” looks fine (I would use “`Long64_t`” for “`nEntries`” and “`i`”).  
Also, the types of branches are fine, see: [TTree → Add a column (“branch”) holding fundamental types and arrays thereof](https://root.cern/doc/master/classTTree.html#addcolumnoffundamentaltypes)

Maybe @pcanal will have some ideas.

BTW. Could you try to change the last line: `std::cout << ts << "\n";`

---

<div class="post-metadata">

**Author:** ![DistantOstrich](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/d/e47774/32.png) [@DistantOstrich](https://root-forum.cern.ch/u/DistantOstrich)\
**Post date:** [March 7, 2023, 1:07pm UTC](https://root-forum.cern.ch/t/bad-allocation-error-looping-though-ttree/53911/7 "2023-03-07T13:07:14Z")

</div>

That’s a good point about the type for nEntries and i, I’m just so used to typing int for these things.

Is there a particular reason to use “\n” over std::endl? The code hits the bad\_alloc error before completing the for loop, so it makes no difference on this occasion, but I generally prefer to use the standard library if I can.

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [March 7, 2023, 1:11pm UTC](https://root-forum.cern.ch/t/bad-allocation-error-looping-though-ttree/53911/8 "2023-03-07T13:11:21Z")

</div>

I remember problems with “`std::endl`” on Windows.

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [March 7, 2023, 2:45pm UTC](https://root-forum.cern.ch/t/bad-allocation-error-looping-though-ttree/53911/9 "2023-03-07T14:45:14Z")

</div>

Attach the full “log” from the crash.

---

<div class="post-metadata">

**Author:** ![DistantOstrich](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/d/e47774/32.png) [@DistantOstrich](https://root-forum.cern.ch/u/DistantOstrich)\
**Post date:** [March 7, 2023, 2:56pm UTC](https://root-forum.cern.ch/t/bad-allocation-error-looping-though-ttree/53911/10 "2023-03-07T14:56:41Z")

</div>

Does Root have in-built logging? All I can see at the moment are the attached images.

If there is no in-built logging then I can log manually. What information would you like to see?  
 ![Error1](https://root-forum.cern.ch/uploads/default/original/3X/c/f/cfc2f27b5e0b3afe56e44643c8c8c949c0dba9af.png)

 ![Error2](https://root-forum.cern.ch/uploads/default/original/3X/2/c/2c16403e2c2686fe2720c5ccb9eff5d547b5aef2.png)

If I run with the debugger I get this:

 ![Error3](https://root-forum.cern.ch/uploads/default/original/3X/9/b/9bb490f82946b50a4a4ef5e8ad3ad946c025503b.png)

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [March 7, 2023, 3:00pm UTC](https://root-forum.cern.ch/t/bad-allocation-error-looping-though-ttree/53911/11 "2023-03-07T15:00:47Z")

</div>

Well, we do not have your “`Analyse16O`” source code for inspection.

Can you open the shell and simply run: `root testSnippet.cxx`

BTW. Maybe @bellenot can help with crashing executables on Windows.

---

<div class="post-metadata">

**Author:** ![DistantOstrich](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/d/e47774/32.png) [@DistantOstrich](https://root-forum.cern.ch/u/DistantOstrich)\
**Post date:** [March 7, 2023, 3:19pm UTC](https://root-forum.cern.ch/t/bad-allocation-error-looping-though-ttree/53911/12 "2023-03-07T15:19:19Z")

</div>

If I run that from cmd I don’t get any message:

 ![Cmd](https://root-forum.cern.ch/uploads/default/original/3X/5/1/519cab4243a8346302ba14cef588a5be87a591c7.png)

If I open a root window and run it then root just crashes to desktop without any error message.

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [March 7, 2023, 3:20pm UTC](https://root-forum.cern.ch/t/bad-allocation-error-looping-though-ttree/53911/13 "2023-03-07T15:20:48Z")

</div>

Have you tried to replace “`std::endl`” with `"\n"`?

BTW. I can now see “6.26/04”. Can you try the latest “6.28/00”? There should appear “6.28/02” shortly.

---

<div class="post-metadata">

**Author:** ![DistantOstrich](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/d/e47774/32.png) [@DistantOstrich](https://root-forum.cern.ch/u/DistantOstrich)\
**Post date:** [March 7, 2023, 3:27pm UTC](https://root-forum.cern.ch/t/bad-allocation-error-looping-though-ttree/53911/14 "2023-03-07T15:27:02Z")

</div>

Yes, I got the same (lack of) result with “\n”. I’m pretty sure it crashes during the for loop, before it gets to that line.

I’ll try with the latest version. It will probably be tomorrow before I can get to that, but thanks for your help so far.

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [March 7, 2023, 3:30pm UTC](https://root-forum.cern.ch/t/bad-allocation-error-looping-though-ttree/53911/15 "2023-03-07T15:30:37Z")

</div>

Another thing to try … work in a directory whose name does not contain any “special” characters (e.g., “`O16_p_y`” instead of “`16O(p,y)`”, and maybe “`O16Analysis`” instead of “`16OAnalysis`”).

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [March 7, 2023, 3:35pm UTC](https://root-forum.cern.ch/t/bad-allocation-error-looping-though-ttree/53911/16 "2023-03-07T15:35:07Z")

</div>

> I have tried both python and C++, and get the same behaviour. Is there a way of looping through the entries in a TTree that doesn’t load the whole thing into memory?

As Wile mentioned, ROOT already does not load the data chunk-a-time. However even if it was loading all the data at once (300 MB), you should not be running out of memory (assuming your machine has several GB of RAM).

So something else is going. A straight-forward solution would be (if you can) to run your failing example on Linux and use the tool `valgrind` to pin point the problem; Alternatively, you can try to cut portion of your code until it stops failing and that might give you an indication of the issues. Another alternative is to build your code in debug mode and use the debugger to find out where it fails.

---

<div class="post-metadata">

**Author:** ![vpadulan](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/vpadulan/32/25315_2.png) [@vpadulan](https://root-forum.cern.ch/u/vpadulan)\
**Post date:** [March 7, 2023, 3:59pm UTC](https://root-forum.cern.ch/t/bad-allocation-error-looping-though-ttree/53911/17 "2023-03-07T15:59:56Z")

</div>

> [@DistantOstrich](#):
>
> So for example, get a separate root file for each hour of data. Is there a way to do this that doesn’t involve looping through the whole TTree?

Something along these lines with [RDataFrame](https://root.cern/doc/master/classROOT_1_1RDataFrame.html) should help

```python
import ROOT

df = ROOT.RDataFrame("TTreeNameHere", "FileNameHere")

hours = [(datetime.datetime.now() + datetime.timedelta(hours=x)).timestamp() for x in range(5)]

opts = ROOT.RDF.RSnapshotOptions()
opts.fLazy = True # This avoids that Snapshot calls trigger the execution right away

# Book all different Snapshot calls in advance
snapshots = [
    df.Filter(f"timestamp >= {hour_begin} && timestamp < {hour_end}")\
      .Snapshot(treeName, FileName, listOfCols, opts)
    for hour_begin, hour_end in zip(hours[:-1],hours[1:])
]

# Trigger execution of one of the Snapshots, all others will be executed at the same time
snap_df = snapshots[0].GetValue()

```

---

<div class="post-metadata">

**Author:** ![DistantOstrich](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/d/e47774/32.png) [@DistantOstrich](https://root-forum.cern.ch/u/DistantOstrich)\
**Post date:** [March 7, 2023, 4:11pm UTC](https://root-forum.cern.ch/t/bad-allocation-error-looping-though-ttree/53911/18 "2023-03-07T16:11:45Z")

</div>

Yes, I have 8GB of RAM, so I wouldn’t expect it to actually have run out of memory.

I don’t have easy access to a linux version of root, so I would like to leave that as a last resort. However I based the code on a colleague’s python script using root that runs fine on linux using

tree = f1.Get( “Board 0” )  
for evt in tree:

I’m confident that it’s the

tree-\>GetEntry(i)

call that is the problem. If I remove that then it runs without error, and running in the debugger shows it failing at that line.

---

<div class="post-metadata">

**Author:** ![DistantOstrich](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/d/e47774/32.png) [@DistantOstrich](https://root-forum.cern.ch/u/DistantOstrich)\
**Post date:** [March 7, 2023, 4:12pm UTC](https://root-forum.cern.ch/t/bad-allocation-error-looping-though-ttree/53911/19 "2023-03-07T16:12:44Z")

</div>

Thanks @vpadulan I’ll give that a try tomorrow. I hadn’t heard of RDataFrame, but it does look useful

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [March 7, 2023, 5:02pm UTC](https://root-forum.cern.ch/t/bad-allocation-error-looping-though-ttree/53911/20 "2023-03-07T17:02:56Z")

</div>

> [@DistantOstrich](#):
>
> I based the code on a colleague’s python script using root that runs fine on linux using
> 
> tree = f1.Get( “Board 0” )  
> for evt in tree:
> 
> I’m confident that it’s the
> 
> tree-\>GetEntry(i)
> 
> call that is the problem

Note that the 'for evt in tree`should be calling`GetEntry` under the hood :).

> If I remove that then it runs without error, and running in the debugger shows it failing at that line.

If you upload your code and your ROOT file we can try running on Linux and see what the problem might be.

[Next page](https://root-forum.cern.ch/t/bad-allocation-error-looping-though-ttree/53911.md?page=2)
