Problem using other functions in Define()

Hello, everyone!
I have a problem while using the Define() function in rdataframe.
Here is the thing. I want to use other conditional statements inside define().
I had problems with complex data:
data file:
test2.root (215.8 KB)
cpp file:
dataframe_define_test_2.cpp (378 Bytes)

auto test=ds2.Define(“theta”,(double z){
if(“MDz2<0”){
return 0;
}
if(“MDz2>0”){
return 1;
}
},{“MDz2”}).Mean(“theta”);

The output is “0”, while “MDz2” did have positive values.
It seems that the second ‘if’ didn’t work…

But when I replaced it with simpler data, the problem disappeared.
data file:
test.root (5.3 KB)
cpp file:
dataframe_define_test.cpp (418 Bytes)

ROOT Version: 6.24/02

The lambda inside of the define should look like:

	auto test=ds2.Define("theta",[](double z){
			if(z < 0)
                return 1;
			else
                return 0;
           // or better: return (z < 0) ? 1 : 0;
    },{"MDz2"});

Then, the easiest method to quickly check that you are doing things correctly is to display your results like:

    auto d1 = test.Display({"theta", "MDz2"});
    d1->Print();

(To use the Display, however, you need to disable implicit MT (so just comment the line ROOT::EnableImplicitMT(30);).)

P.S. I am surprised that checking if(“MDz2<0”) worked in the minimal example

2 Likes

Hi @CY_Han ,

as @ikabadzhov says if ("MDz2<0") should be if (z < 0).

The reason why if ("MDz2<0") compiles (and always evaluates to true, so your function always returns 0) is that the string "MDz2<0" is of type const char * (pointer to array of characters) and if ("some_string") then is equivalent to if (bool(addressof("some_string)) == true) (i.e. you are actually testing that the address of the string is positive). Classic C++ shenanigans.

Cheers,
Enrico

2 Likes

Thanks!@ikabadzhov and @eguiraud :handshake:
What an obvious mistake, I really shouldn’t code at night. :joy:

Hi @ikabadzhov ,
Actually in the minimal example,

if(“d1<0”)

didn’t work.
It’s always judged to be true as @eguiraud said, and the value of d1 is read out in sequence. :neutral_face:

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