# Merging two datasets with different selections

**URL:** <https://root-forum.cern.ch/t/merging-two-datasets-with-different-selections/53431>\
**Category:** Newbie\
**Tags:** rdataframe\
**Created:** [February 2, 2023, 5:57pm UTC](https://root-forum.cern.ch/t/merging-two-datasets-with-different-selections/53431 "2023-02-02T17:57:43Z")\
**Posts on this page:** 1\
**Showing post:** 2

<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 2, 2023, 6:10pm UTC](https://root-forum.cern.ch/t/merging-two-datasets-with-different-selections/53431/2 "2023-02-02T18:10:52Z")

</div>

Hi @Clara_Landesa_Gomez ,

and welcome to the ROOT forum!

In the following I will assume that the two trees have the same schema (same branches with same names and types) and you want to concatenate the datasets vertically (i.e. make a dataset that has the same columns as the two original datasets and the union of their rows). If that’s not the case, please clarify what you mean with merging here.

> [@Clara\_Landesa\_Gomez](#):
>
> I understand that one option would be to SnapShot both of them into TTrees and then merge the trees, but this seems memory-demanding.

That will require storage for the skimmed versions of the two datasets, but if that’s not a problem, depending on your analysis workflow, this might be a one-time operation (or a rare operation anyway) and it might prove to be the simplest solution.

Otherwise you can read both datasets into the same RDataFrame object (e.g. with `RDataFrame("Events", {"f1.root", "f2.root"})`, which will concatenate them vertically, or by first building a `TChain` and them passing that to the `RDataFrame` constructor) and then change the behavior of your `Filter`s based on which file is being processed by using [DefinePerSample](https://root.cern/doc/master/classROOT_1_1RDF_1_1RInterface.html#a29d77593e95c0f84e359a802e6836a0e), e.g.:

```python
df.DefinePerSample("isMC", "rdfsampleinfo_.Contains("f1") ? true : false")
  .Filter("isMC ? x > 1.5 : x > 2.")

```

I hope this helps!  
Cheers,  
Enrico

---

_[View the full topic](https://root-forum.cern.ch/t/merging-two-datasets-with-different-selections/53431)._
