# \[new script\] Splitting a ROOT file tree into N files

**URL:** <https://root-forum.cern.ch/t/new-script-splitting-a-root-file-tree-into-n-files/33734>\
**Category:** Evolution Proposals, Apps & Ideas\
**Created:** [April 23, 2019, 5:18pm UTC](https://root-forum.cern.ch/t/new-script-splitting-a-root-file-tree-into-n-files/33734 "2019-04-23T17:18:11Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![vfioretti](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/v/ad7895/32.png) [@vfioretti](https://root-forum.cern.ch/u/vfioretti)\
**Post date:** [April 23, 2019, 5:18pm UTC](https://root-forum.cern.ch/t/new-script-splitting-a-root-file-tree-into-n-files/33734/1 "2019-04-23T17:18:11Z")

</div>

Dear ROOT forum,

the possibility of splitting a ROOT file into many smaller files has been introduced in ROOT by means of the “rooteventselector” python script, However this script presents a bug: it does not work when multiple trees are present with the same name.  
See:

> [@Rooteventselector and multiple TTree with the same name](https://root-forum.cern.ch/t/rooteventselector-and-multiple-ttree-with-the-same-name/28096):
>
> I want to use the nice commandline tool rooteventselector to apply a simple selection on a tree. The file looks like this: DYTuple/ DecayTree;73 DecayTree;72 DecayTree;71 The different trees with the same name are of course the different revisions from writing the file. However, if I try to use this with rooteventselector source.root:DYTuple/DecayTree dest.root, it complains about those duplicate names and does not process them, instead of taking the most recent revision like all …

I don’t know if the issue has been solved (with ROOT 6.16 it still does not work), but I created a small python script to get the work done. You can get it here:

> <https://github.com/vfioretti/ROOT_utilities/blob/master/splitROOT.py>

The script works with python 2.7 + ROOT 6.x (it should also work with ROOT 5.x but it has not been tested) and it requires pyROOT + Numpy.  
It is based on available tutorials and it could be much more pythonic… but I don’t have much time to improve it (i.e. suit yourself!)

Comments, questions, corrections?  
Cheers,  
Valentina

---

<div class="post-metadata">

**Author:** ![Danilo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/danilo/32/34528_2.png) [@Danilo](https://root-forum.cern.ch/u/Danilo)\
**Post date:** [April 24, 2019, 9:59am UTC](https://root-forum.cern.ch/t/new-script-splitting-a-root-file-tree-into-n-files/33734/2 "2019-04-24T09:59:27Z")

</div>

Hi Valentina,

nice. We take your post as a reminder to fix for 6.18 `rooteventselector`.  
Two curiosities:

1. Why do you need to split your ROOT files? What are you trying to achieve?
2. Why does your script depend on Numpy?

Cheers,  
D

---

<div class="post-metadata">

**Author:** ![vfioretti](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/v/ad7895/32.png) [@vfioretti](https://root-forum.cern.ch/u/vfioretti)\
**Post date:** [April 24, 2019, 10:19am UTC](https://root-forum.cern.ch/t/new-script-splitting-a-root-file-tree-into-n-files/33734/3 "2019-04-24T10:19:46Z")

</div>

Hi!

- 
  1. Why do you need to split your ROOT files? What are you trying to achieve?  
I use ROOT files as input for Geant4 simulations, and I needed to break the input file in smaller ones to reduce the CPU time by running many smaller jobs in a cluster.  
I am in early development phase, and everything is manual right now but I will likely integrate the script in a pipeline.

- 
  1. Why does your script depend on Numpy?  
Oops, you are right! I use it for the ints and floats, but I could use python math in this code, since I don’t require any fancy numpy feature. I will remove it.

Thanks!  
Valentina

---

<div class="post-metadata">

**Author:** ![hmwakeling](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/hmwakeling/32/24505_2.png) [@hmwakeling](https://root-forum.cern.ch/u/hmwakeling)\
**Post date:** [February 25, 2021, 5:57pm UTC](https://root-forum.cern.ch/t/new-script-splitting-a-root-file-tree-into-n-files/33734/4 "2021-02-25T17:57:38Z")

</div>

Hi there,

I’m looking to split my root files in half, such that I have a training and test file of roughly equal size for a Multi Variate Analysis. I have tried using your script (thank you!) however the output files are at least 10 times the size of the originals.

Has functionality to split a file been added to ROOT since this discussion that I have not been able to find in my searches? If not, would you know what could be causing this increase in size of the files?

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [February 26, 2021, 8:33am UTC](https://root-forum.cern.ch/t/new-script-splitting-a-root-file-tree-into-n-files/33734/5 "2021-02-26T08:33:48Z")

</div>

Hi @hmwakeling ,  
`rooteventselector`, a helper tool that comes with ROOT, should do what you want. If not, [RDataFrame](https://root.cern/doc/master/classROOT_1_1RDataFrame.html) can also be used to split files, e.g. in Python

```cpp
df = ROOT.RDataFrame("treename", "filename.root")
df.Range(nEntries / 2).Snapshot("newtree", "newfile1.root")
df.Range(nEntries / 2, nEntries).Snapshot("newtree", "newfile2.root")

```

(In general please consider creating a new topic rather than replying to one that’s two years old, more chances to get an up-to-date reply).

Cheers,  
Enrico

---

<div class="post-metadata">

**Author:** ![sbinet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/sbinet/32/13166_2.png) [@sbinet](https://root-forum.cern.ch/u/sbinet)\
**Post date:** [February 26, 2021, 9:45am UTC](https://root-forum.cern.ch/t/new-script-splitting-a-root-file-tree-into-n-files/33734/6 "2021-02-26T09:45:46Z")

</div>

there’s also `root-split` from [groot](https://go-hep.org/x/hep/groot):

- [GoHEP/groot: v0.27.0 (root-split, groot-faster-than-root)](https://root-forum.cern.ch/t/gohep-groot-v0-27-0-root-split-groot-faster-than-root/39609)
- [https://go-hep.org/dist/latest/root-split-linux\_amd64.exe](https://go-hep.org/dist/latest/root-split-linux_amd64.exe) (for Linux/64b)
- [https://go-hep.org/dist/latest/root-split-darwin\_amd64.exe](https://go-hep.org/dist/latest/root-split-darwin_amd64.exe) (for macOS)
- [https://go-hep.org/dist/latest/root-split-windows\_amd64.exe](https://go-hep.org/dist/latest/root-split-windows_amd64.exe) (for Win/64b)

full list [here](https://go-hep.org/dist/latest).

ex:

```auto
$> root-split -h
Usage: root-split [options] file.root

ex:
 $> root-split -o out.root -n 10 ./testdata/chain.flat.1.root

options:
  -n int
    	number of events to split into (default 100)
  -o string
    	path to output ROOT files (default "out.root")
  -t string
    	input tree name to split (default "tree")
  -v	enable verbose mode

```

hth,  
-s

---

<div class="post-metadata">

**Author:** ![hmwakeling](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/hmwakeling/32/24505_2.png) [@hmwakeling](https://root-forum.cern.ch/u/hmwakeling)\
**Post date:** [February 26, 2021, 2:53pm UTC](https://root-forum.cern.ch/t/new-script-splitting-a-root-file-tree-into-n-files/33734/7 "2021-02-26T14:53:46Z")

</div>

Thank you very much for your help!  
In future I shall open a new thread 🙂
