# Applying cuts to histogram without having a TTree branch

**URL:** <https://root-forum.cern.ch/t/applying-cuts-to-histogram-without-having-a-ttree-branch/57441>\
**Category:** Newbie\
**Created:** [December 7, 2023, 5:57pm UTC](https://root-forum.cern.ch/t/applying-cuts-to-histogram-without-having-a-ttree-branch/57441 "2023-12-07T17:57:13Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![amishgog](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/amishgog/32/34940_2.png) [@amishgog](https://root-forum.cern.ch/u/amishgog)\
**Post date:** [December 7, 2023, 5:57pm UTC](https://root-forum.cern.ch/t/applying-cuts-to-histogram-without-having-a-ttree-branch/57441/1 "2023-12-07T17:57:13Z")

</div>

Hi

So I created 2 list of masses and i want to apply a cut to one variable to draw the histogram of the other but I am having trouble in adding branches to my existing root file. Is there a way to apply a cut to the histogram without using the tree Draw method. Thanks for the help

_ROOT Version:_ 6.3.0 (PyROOT to be specific)

* * *

---

<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:** [December 8, 2023, 11:02am UTC](https://root-forum.cern.ch/t/applying-cuts-to-histogram-without-having-a-ttree-branch/57441/2 "2023-12-08T11:02:13Z")

</div>

Hello,

Once the histogram is created, the data is reduced, and no cut can be applied on the entries any more.  
If you want to easily create new columns of a dataset and represent their entries as histograms, perhaps applying selection cuts, I would recommend to look into [RDataFrame](https://root.cern/doc/master/classROOT_1_1RDataFrame.html#crash-course).

Best,  
Danilo

---

<div class="post-metadata">

**Author:** ![amishgog](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/amishgog/32/34940_2.png) [@amishgog](https://root-forum.cern.ch/u/amishgog)\
**Post date:** [December 8, 2023, 12:57pm UTC](https://root-forum.cern.ch/t/applying-cuts-to-histogram-without-having-a-ttree-branch/57441/3 "2023-12-08T12:57:56Z")

</div>

Hi Danilo,

Thanks for the reply, I guess I can use the Filter method of RDataFrame but that would still have me add a new branch to my dataset. When I use the TTree Branch method to add a list of masses to my root file it only adds one entry, so I was trying to look for a different solution. Would it be possible for you to show me the way I can I add a list as a branch to my dataset?

---

<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:** [December 8, 2023, 1:54pm UTC](https://root-forum.cern.ch/t/applying-cuts-to-histogram-without-having-a-ttree-branch/57441/4 "2023-12-08T13:54:40Z")

</div>

Hi,

Sure: you can do it through a `Define`: [ROOT: ROOT::RDF::RInterface\< Proxied, DataSource \> Class Template Reference](https://root.cern/doc/master/classROOT_1_1RDF_1_1RInterface.html#a4698601205a55ac49279150d56fc904f)

Cheers,  
Danilo

---

<div class="post-metadata">

**Author:** ![amishgog](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/amishgog/32/34940_2.png) [@amishgog](https://root-forum.cern.ch/u/amishgog)\
**Post date:** [December 8, 2023, 2:03pm UTC](https://root-forum.cern.ch/t/applying-cuts-to-histogram-without-having-a-ttree-branch/57441/5 "2023-12-08T14:03:58Z")

</div>

So the second parameter to the Define method is a producer as written in the docs. Would that be referring to a function that outputs the list of masses?

---

<div class="post-metadata">

**Author:** ![amishgog](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/amishgog/32/34940_2.png) [@amishgog](https://root-forum.cern.ch/u/amishgog)\
**Post date:** [December 8, 2023, 2:15pm UTC](https://root-forum.cern.ch/t/applying-cuts-to-histogram-without-having-a-ttree-branch/57441/6 "2023-12-08T14:15:29Z")

</div>

I tried the Define method and here is my code:

 ![Screenshot 2023-12-08 at 7.44.38 PM](https://root-forum.cern.ch/uploads/default/original/3X/2/6/26dd71b5eabcdd6c33d4029fc743707389e63a2a.png)

This return the following error:

 ![Screenshot 2023-12-08 at 7.45.23 PM](https://root-forum.cern.ch/uploads/default/original/3X/4/8/487d9ba6e4246cc8eb8276c78f7b045f7ad754f7.png)

---

<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:** [December 8, 2023, 2:46pm UTC](https://root-forum.cern.ch/t/applying-cuts-to-histogram-without-having-a-ttree-branch/57441/7 "2023-12-08T14:46:58Z")

</div>

Hi Amish,

I think I start to understand what you are trying to accomplish: if I am not too wrong, we can identify an easy solution.  
Are the names `J_psi_15_PX` `J_psi_15_PY` `J_psi_15_PZ` `J_psi_15_PE` and `phi_1020_PX`… your branches?

Cheers,  
D

---

<div class="post-metadata">

**Author:** ![amishgog](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/amishgog/32/34940_2.png) [@amishgog](https://root-forum.cern.ch/u/amishgog)\
**Post date:** [December 8, 2023, 2:59pm UTC](https://root-forum.cern.ch/t/applying-cuts-to-histogram-without-having-a-ttree-branch/57441/8 "2023-12-08T14:59:41Z")

</div>

Hi,

Yes and just a correction the branch names for Jpsi are J\_psi\_1S…

---

<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:** [December 8, 2023, 3:30pm UTC](https://root-forum.cern.ch/t/applying-cuts-to-histogram-without-having-a-ttree-branch/57441/9 "2023-12-08T15:30:14Z")

</div>

Hello,

I think this is what is needed for the calculation of the mass in your case

```auto
ROOT.gInterpreter.Declare("""
float InvMass(float px1, float py1, float pz1, float e1, float px2, float py2, float pz2, float e2)
{
    ROOT::Math::PxPyPzEVector v1(px1, py1, pz1, e1);
    ROOT::Math::PxPyPzEVector v2(px2, py2, pz2, e2);
    return (v1 + v2).M();
}
""")

myDF.Define("JpsiPhiMass", "InvMass(J_psi_15_PX, J_psi_15_PY, J_psi_15_PZ, J_psi_15_PE, phi_1020_PX, phi_1020_PY, phi_1020_PZ, phi_1020_PE)")

```

Cheers,  
Danilo

---

<div class="post-metadata">

**Author:** ![amishgog](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/amishgog/32/34940_2.png) [@amishgog](https://root-forum.cern.ch/u/amishgog)\
**Post date:** [December 8, 2023, 3:48pm UTC](https://root-forum.cern.ch/t/applying-cuts-to-histogram-without-having-a-ttree-branch/57441/10 "2023-12-08T15:48:52Z")

</div>

I will try this solution and let you know. Thank you for all your help Danilo

Cheers,  
AG

---

<div class="post-metadata">

**Author:** ![amishgog](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/amishgog/32/34940_2.png) [@amishgog](https://root-forum.cern.ch/u/amishgog)\
**Post date:** [December 8, 2023, 5:50pm UTC](https://root-forum.cern.ch/t/applying-cuts-to-histogram-without-having-a-ttree-branch/57441/11 "2023-12-08T17:50:15Z")

</div>

Hi,

So this code worked but I wanted to ask, does this code add a column to my dataframe or a branch to the tree in my root file? In order to use the tree Draw method I would need a need a new branch in my tree.

And also when I do mydf.Display(“JpsiPhiMass”) it gives me the error that regex “JpsiPhiMass” did not match any column.

---

<div class="post-metadata">

**Author:** ![amishgog](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/amishgog/32/34940_2.png) [@amishgog](https://root-forum.cern.ch/u/amishgog)\
**Post date:** [December 9, 2023, 3:48am UTC](https://root-forum.cern.ch/t/applying-cuts-to-histogram-without-having-a-ttree-branch/57441/12 "2023-12-09T03:48:05Z")

</div>

Alright so the final solution is as follows:  
I used the above code given by Danilo and then I execute the following command:  
myDF.Snapshot(“nameofTree”, “path\_to\_root\_file”)

This will create the new column and add it to the tree in the ROOT file. Thank you for all the help on this.

---

<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:** [December 9, 2023, 10:01am UTC](https://root-forum.cern.ch/t/applying-cuts-to-histogram-without-having-a-ttree-branch/57441/13 "2023-12-09T10:01:54Z")

</div>

That’s right, Snapshot allows you to dump on disk the enriched ROOT dataset. Thanks for the interesting use case!

D
