# Add a String Filter in Datafram

**URL:** <https://root-forum.cern.ch/t/add-a-string-filter-in-datafram/46200>\
**Category:** ROOT\
**Tags:** rdataframe\
**Created:** [August 6, 2021, 6:58am UTC](https://root-forum.cern.ch/t/add-a-string-filter-in-datafram/46200 "2021-08-06T06:58:19Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![CY\_Han](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/cy_han/32/27076_2.png) [@CY\_Han](https://root-forum.cern.ch/u/CY_Han)\
**Post date:** [August 6, 2021, 6:58am UTC](https://root-forum.cern.ch/t/add-a-string-filter-in-datafram/46200/1 "2021-08-06T06:58:19Z")

</div>

_ROOT Version:_ 6.24/02  
_Platform:_ Scientific Linux 7.3 (Nitrogen)  
_Compiler:_ gcc 4.8.5(Red Hat 4.8.5-28)

I am using RDataFrame to analyze some data in a root file, which involves the filtering of string data.  
i.e.

```auto
auto Si = d.Filter("ParticleName == \"proton\"").Histo1D({"Si","Si",4000,0,400},"particleEnergy");

```

That is to filter out the energy of protons.  
However, there is a problem with the above filter statement, and the error is as follows:

```auto
/opt/root62402/include/ROOT/RVec.hxx:609:23: error: comparison between pointer and integer ('cha
RVEC_LOGICAL_OPERATOR(==)
~~~~~~~~~~~~~~~~~~~~~~^~~
/opt/root62402/include/ROOT/RVec.hxx:579:49: note: expanded from macro 'RVEC_LOGICAL_OPERATOR'
   auto op = [y](const T0 &x) -> int { return x OP y; }; \
                                              ~ ^ ~
input_line_32:2:65: note: in instantiation of function template specialization 'ROOT::VecOps::op
auto lambda0 = [](ROOT::VecOps::RVec<Char_t>& var0){return var0 == "proton"
                                                                ^
terminate called after throwing an instance of 'std::runtime_error'
  what():  
RDataFrame: An error occurred during just-in-time compilation. The lines above might indicate th
 All RDF objects that have not run an event loop yet should be considered in an invalid state.

```

And here are some parts of my root file:

```auto
************************************************************************
* Row * eventID.e * trackID.t * particleI * ParticleName * particleEnergy *
************************************************************************
* 0 * 37724355 * 2 * 32.914987 * proton * 16.426902 *
* 1 * 52427664 * 1 * 3.8289047 * neutron * 1.8401179 *
* 2 * 90015881 * 2 * 2.1190153 * proton * 1.0689786 *
* 3 * 113350944 * 2 * 1.8670489 * proton * 0.7781645 *
* 4 * 113388706 * 1 * 0.1019912 * neutron * 0.0501000 *
* 5 * 201286952 * 1 * 19.599530 * neutron * 18.641971 *
* 6 * 297711814 * 2 * 2.8497625 * C12 * 0.2642544 *

```

How can I make it working?  
Thanks a lot  
han

---

<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:** [August 9, 2021, 10:15am UTC](https://root-forum.cern.ch/t/add-a-string-filter-in-datafram/46200/2 "2021-08-09T10:15:26Z")

</div>

Hi @CY_Han ,  
and welcome to the ROOT forum!  
What type is `ParticleName` stored as? (e.g. what does `TTree::Print` say the type of the branch is?)  
The problem is that RDF reads the branch as an `RVec<char>` instead of e.g. a `std::string`.

This should be a workaround:

```auto
d.Filter("std::string s(ParticleName.begin(), ParticleName.end()); return s == \"proton\";")

```

Cheers,  
Enrico

---

<div class="post-metadata">

**Author:** ![CY\_Han](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/cy_han/32/27076_2.png) [@CY\_Han](https://root-forum.cern.ch/u/CY_Han)\
**Post date:** [August 10, 2021, 2:46am UTC](https://root-forum.cern.ch/t/add-a-string-filter-in-datafram/46200/3 "2021-08-10T02:46:20Z")

</div>

Hi Enrico,  
thanks for your reply!  
The ParticleName stored as char, as you can see below.

```auto
root [2] tree->Print()
******************************************************************************
*Tree :tree : Stepping Data *
*Entries : 18 : Total = 4168 bytes File Size = 1721 *
* : : Tree compression factor = 1.23 *
******************************************************************************
*Br 0 :eventID : eventID/L *
*Entries : 18 : Total Size= 723 bytes File Size = 194 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 1.12 *
*............................................................................*
*Br 1 :trackID : trackID/L *
*Entries : 18 : Total Size= 723 bytes File Size = 120 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 1.82 *
*............................................................................*
*Br 2 :particleINEnergy : particleINEnergy/D *
*Entries : 18 : Total Size= 768 bytes File Size = 227 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 1.00 *
*............................................................................*
*Br 3 :ParticleName : ParticleName/C *
*Entries : 18 : Total Size= 807 bytes File Size = 191 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 1.52 *
*............................................................................*
*Br 4 :particleEnergy : particleEnergy/D *
*Entries : 18 : Total Size= 758 bytes File Size = 225 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 1.00 *
*............................................................................*

```

The code you mentioned did not report an error, but I did not get the data I wanted. It seems that there is no data meets the filter criteria, which is incomprehensible. Since we can clearly see the data meets the condition.  
In order to better illustrate the problem, I uploaded a data file and the processing code I used. Could you please help me find out where the problem is?

Thanks a lot  
han  
[plot\_multi.cpp](https://root-forum.cern.ch/uploads/short-url/3gZgpIUiZocQSnDJKzxfBGhdfT0.cpp) (1.1 KB)  
[data.root](https://root-forum.cern.ch/uploads/short-url/ra5h4GBkyLmvUdcyMxF8l8sl3ZJ.root) (7.1 KB)

---

<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:** [August 10, 2021, 8:53am UTC](https://root-forum.cern.ch/t/add-a-string-filter-in-datafram/46200/4 "2021-08-10T08:53:50Z")

</div>

Hi Han,  
thanks a lot for the complete reproducer, that always simplifies debugging 😄

It’s C-string shenanigans. It seems that `ParticleName` is always an array of 8 characters, padded with `\0` if the actual `ParticleName` is shorter. So we were comparing `s` which could be e.g. `proton\0\0` with `"proton"`. I found out by adding `std::cout << s << ' ' << s.size() << '\\n'; ` in the middle of the `Filter` expression.

This is a workaround:

```auto
auto Si = d.Filter(" std::string s(ParticleName.begin(), ParticleName.end()); return s.substr(0, 6) == \"proton\";")

```

An alternative is to store `std::string`s instead of arrays of characters.

Cheers,  
Enrico

---

<div class="post-metadata">

**Author:** ![CY\_Han](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/cy_han/32/27076_2.png) [@CY\_Han](https://root-forum.cern.ch/u/CY_Han)\
**Post date:** [August 10, 2021, 10:51am UTC](https://root-forum.cern.ch/t/add-a-string-filter-in-datafram/46200/5 "2021-08-10T10:51:21Z")

</div>

Cool!  
Thank you very much for solving my problem. 😃

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [September 6, 2021, 6:20am UTC](https://root-forum.cern.ch/t/add-a-string-filter-in-datafram/46200/7 "2021-09-06T06:20:21Z")

</div>

Closed on September 6, 2021, 6:20am UTC

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
