# How to save selected events from a ROOT file to another file?

**URL:** <https://root-forum.cern.ch/t/how-to-save-selected-events-from-a-root-file-to-another-file/57110>\
**Category:** Newbie\
**Created:** [November 14, 2023, 10:50am UTC](https://root-forum.cern.ch/t/how-to-save-selected-events-from-a-root-file-to-another-file/57110 "2023-11-14T10:50:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![praha](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/praha/32/35022_2.png) [@praha](https://root-forum.cern.ch/u/praha)\
**Post date:** [November 14, 2023, 10:50am UTC](https://root-forum.cern.ch/t/how-to-save-selected-events-from-a-root-file-to-another-file/57110/1 "2023-11-14T10:50:47Z")

</div>

Hi!  
I want to save a small subset of events with a certain branch from a series of rootfiles onto a single new rootfile.

```auto
file=0
chain = ROOT.TChain("cbmsim")
path="path/to/files/"
for inputFile in os.listdir(path):
	        file+=1        
	        if file>3: break
	        chain.Add(inputFile)

nrOfEvents = chain.GetEntries()      
newfile = ROOT.TFile("test.root", "recreate")
newtree = chain.CloneTree(0)
for i in range(nrOfEvents):
	    chain.GetEntry(i)
	    if hasattr(chain,"Digitised_Hits"): 
	       newtree.Fill()        
newfile.Write()
print('Done')

```

However, the above code gives me a segmentation fault when cloning the tree. How do I solve this?

---

<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:** [November 14, 2023, 2:26pm UTC](https://root-forum.cern.ch/t/how-to-save-selected-events-from-a-root-file-to-another-file/57110/2 "2023-11-14T14:26:54Z")

</div>

Dear @praha ,

I believe the best tool for you would be [RDataFrame](https://root.cern/doc/master/classROOT_1_1RDataFrame.html)

```python

dataset_name = "cbmsim"
file_names = ["f1.root", "f2.root", "f3.root"]

df = ROOT.RDataFrame(dataset_name, file_names)
# You can further filter events here with the `Filter` operation.
# See the RDataFrame docs linked above

# This saves to an output TTree called `out_data` in file `out_file.root`
# storing only the `Digitised_Hits` column
df.Snapshot("out_data", "out_file.root", ["Digitised_Hits"])

```

Cheers,  
Vincenzo

---

<div class="post-metadata">

**Author:** ![LongHoa](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/longhoa/32/25905_2.png) [@LongHoa](https://root-forum.cern.ch/u/LongHoa)\
**Post date:** [December 14, 2023, 8:41pm UTC](https://root-forum.cern.ch/t/how-to-save-selected-events-from-a-root-file-to-another-file/57110/3 "2023-12-14T20:41:30Z")

</div>

Hello,

I assume that `Digitised_Hits` is the branch name. Then what you can do is:

```auto
# deactivate all branches first
chain.SetBranchStatus( "*", 0 )
# then activate the branch you want
chain.SetBranchStatus( "Digitised_Hits", 1 );
# then clone the tree
newtree = chain.CloneTree( )

```
