# Getting photon with maximum momentum?

**URL:** <https://root-forum.cern.ch/t/getting-photon-with-maximum-momentum/57942>\
**Category:** Newbie\
**Tags:** rdataframe, root\
**Created:** [January 23, 2024, 11:02pm UTC](https://root-forum.cern.ch/t/getting-photon-with-maximum-momentum/57942 "2024-01-23T23:02:34Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![somebody.nobody](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/e19b73/32.png) [@somebody.nobody](https://root-forum.cern.ch/u/somebody.nobody)\
**Post date:** [January 23, 2024, 11:02pm UTC](https://root-forum.cern.ch/t/getting-photon-with-maximum-momentum/57942/1 "2024-01-23T23:02:35Z")

</div>

I am trying to access the photon with maximum momentum from a root data frame. Can someone explain why this command does not accomplish this? I am receiving errors that the filter can not be interpreted as a boolean.

```auto
    df = df.Alias("Photon0", "Photon#0.index")
    df = df.Define("photons_all", "FCCAnalyses::ReconstructedParticle::get(Photon0, ReconstructedParticles)")
    df = df.Define("photons_p", "FCCAnalyses::ReconstructedParticle::get_p(photons_all)")
    max_p = (df.Max("photons_p").GetValue())

    # Filter the DataFrame to include only rows with the maximum photon momentum
    df_max_photons = df.Filter("photons_p == max_p")

```

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [January 24, 2024, 7:54am UTC](https://root-forum.cern.ch/t/getting-photon-with-maximum-momentum/57942/2 "2024-01-24T07:54:20Z")

</div>

Welcome to the ROOT Forum!  
Maybe I’m wrong, @vpadulan can correct me, but maybe you have to add:

```auto
df.Define("max_p ", max_p)

```

---

<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:** [January 24, 2024, 8:45am UTC](https://root-forum.cern.ch/t/getting-photon-with-maximum-momentum/57942/3 "2024-01-24T08:45:35Z")

</div>

Dear @somebody.nobody ,

First, a caveat. By calling `GetValue` and then perform new operations on the same histogram, you are triggering a computation graph run (i.e. an event loop) and then at some point you will call another, so you have more than one event loop over the same dataset. I believe in your case this is unavoidable, since you need to compute a quantity (the maximum pt) over all the events in the dataset first.

Back to your question, @bellenot is right in the sense that your Filter expression `"photons_p == max_p"` supposes that the quantity `max_p` is reachable by RDataFrame somehow, but you have given no way for the RDataFrame to know about it. The variable `max_p` in your snippet is just a normal Python variable holding a value (in this case a float representing the maximum pt of your events). So this quantity needs to be declared to RDataFrame somehow. @bellenot is showing you one way, which is defining a new column in the dataset with this quantity. Another way which avoids repeating the same float for each event is as follows:

```python
max_p = (df.Max("photons_p").GetValue())

df_max_photons = df.Filter(f"photons_p == {max_p}")

```

Where practically you are inserting the constant float value held by `max_p` in the string expression, practically embedding it inside the expression that will be JIT-compiled by cling.

Cheers,  
Vincenzo

---

<div class="post-metadata">

**Author:** ![somebody.nobody](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/e19b73/32.png) [@somebody.nobody](https://root-forum.cern.ch/u/somebody.nobody)\
**Post date:** [January 24, 2024, 2:32pm UTC](https://root-forum.cern.ch/t/getting-photon-with-maximum-momentum/57942/4 "2024-01-24T14:32:10Z")

</div>

I think the program may be running into an issue on the line max\_p = (df.Max(“photons\_p”).GetValue()).  
I get an extended error message, but the key problem seems to be here: /tmp/root/spack-stage/spack-stage-root-6.28.06-dgx5r6vwya5ynyeoef36d2aw6vk6v2jc/spack-build-dgx5r6v/include/ROOT/RDF/InterfaceUtils.hxx:313:4: error: static\_assert failed due to requirement ‘std::is\_convertible\<ROOT::VecOps::RVec, bool\>::value’ “filter expression returns a type that is not  
convertible to bool”

---

<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:** [January 24, 2024, 3:08pm UTC](https://root-forum.cern.ch/t/getting-photon-with-maximum-momentum/57942/5 "2024-01-24T15:08:47Z")

</div>

The error talks about a `filter expression`, so it’s hard that the problem is at the `Max` call. Do you have any other `Filter` calls in your applications? Double check that the expressions you write in a `Filter` return a boolean value, that is a requirement.

Cheers,  
Vincenzo

---

<div class="post-metadata">

**Author:** ![somebody.nobody](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/e19b73/32.png) [@somebody.nobody](https://root-forum.cern.ch/u/somebody.nobody)\
**Post date:** [January 24, 2024, 6:20pm UTC](https://root-forum.cern.ch/t/getting-photon-with-maximum-momentum/57942/6 "2024-01-24T18:20:51Z")

</div>

I fixed the filter problem, but I’m running into another difficulty with the selection. Is there a way to get the particle with max. momentum for each event, rather than out of every particle in the data frame?

---

<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:** [January 24, 2024, 8:04pm UTC](https://root-forum.cern.ch/t/getting-photon-with-maximum-momentum/57942/7 "2024-01-24T20:04:36Z")

</div>

Dear @somebody.nobody ,

I see from your previous snippet that you already have a column named `photons_p` which holds the values of the pt for all the particles in the event. Supposing that the elements of this column are of a type akin to `std::vector`, you could do something like

```python
df.Define(
    "max_p_of_event",
    "*(std::max_element(std::begin(photons_p), std::end(photons_p)))"
)

```

Note the extra de-reference `*` which is needed since [`max_element`](https://en.cppreference.com/w/cpp/algorithm/max_element) returns an iterator to the maximum element of the vector.

Cheers,  
Vincenzo
