FoxWise
December 10, 2020, 10:58am
1
I have a RDataFrame consisted of 2 trees like this.
ch = ROOT.TChain("tree1")
ch.Add("/path/to/file1.root")
ch1 = ROOT.TChain("tree2")
ch1.Add("/path/to/file2.root")
ch.AddFriend(ch1, "tr2")
df = ROOT.RDataFrame(ch)
I can use columns of the friend tree fine with Filter
and Define
.
But to make them work in Histo1D i need create another temporary column - copy. Is it how its supposed to be? Or am I missing something?
# **doesn't work!**
h2 = df.Histo1D(("h2", "title", 10, 0, 10), "tr2.x")
# works fine
h3 = df.Define("tree2_x", "tr2.x")\
.Histo1D(("h3", "title", 10, 0, 10), "tree2_x")
ROOT Version: 6.22
Platform: Centos 7
Compiler: g++ 9.2
Hi @FoxWise ,
thanks for the report, it looks like a bug but I can’t reproduce it in v6.22/06. Does the following program fail for you?
import ROOT
if __name__ == "__main__":
ROOT.RDataFrame(1).Define("x", "42").Snapshot("t", "f1.root")
ROOT.RDataFrame(1).Define("x", "42").Snapshot("t", "f2.root")
ch1 = ROOT.TChain("t")
ch1.Add("f1.root")
ch2 = ROOT.TChain("t")
ch2.Add("f2.root")
ch1.AddFriend(ch2, "tr2")
df = ROOT.RDataFrame(ch1)
print(df.Histo1D("tr2.x").GetMean())
If yes, does this also fail (needs to be compiled e.g. with g++ main.cpp $(root-config --libs --cflags)
?
#include <ROOT/RDataFrame.hxx>
#include <TChain.h>
#include <iostream>
int main() {
{
TFile f("f.root", "recreate");
TTree t("t", "t");
int x = 42;
t.Branch("x", &x);
t.Fill();
t.Write();
}
TChain ch1("t");
ch1.Add("f.root");
TChain ch2("t");
ch2.Add("f.root");
ch1.AddFriend(&ch2, "tr2");
ROOT::RDataFrame df(ch1);
std::cout << df.Histo1D("tr2.x")->GetMean() << '\n';
return 0;
}
If any of these fail for you, can you please try with 6.22/06?
If not, what am I doing differently?
Cheers,
Enrico
FoxWise
December 10, 2020, 12:13pm
3
Hi @eguiraud
thanks for the fast feedback. Sorry, I oversimplified my case in the first place.
Here is the code I would like to work without re-defining thingie:
import ROOT
vectors = '''
#include "Math/Vector3D.h"
using namespace ROOT::Math;
'''
ROOT.gInterpreter.Declare(vectors)
if __name__ == "__main__":
ROOT.RDataFrame(1).Define("vec", "XYZVector(42, 42*42, 42*42*42)").Snapshot("t", "f1.root")
ROOT.RDataFrame(1).Define("vec", "XYZVector(42, 42*42, 42*42*42)").Snapshot("t", "f2.root")
ch1 = ROOT.TChain("t")
ch1.Add("f1.root")
ch2 = ROOT.TChain("t")
ch2.Add("f2.root")
ch1.AddFriend(ch2, "tr2")
df = ROOT.RDataFrame(ch1)
df = df.Define("tr2_vec_Z", "tr2.vec.Z()")
print(df.Histo1D("tr2_vec_Z").GetMean()) # works
print(df.Histo1D("tr2.vec.Z()").GetMean()) # doesn't work
But probably doing calculations/executing methods are not allowed inside Histo1D? Is there no more fancy way?
cheers,
Bohdan
Ah alright
Histo1D
does not accept expressions, just column names. Define
is the right thing to use in this case. It’s a bit more verbose but it keeps responsibilities clearly separated.
Cheers,
Enrico
system
Closed
December 24, 2020, 12:28pm
5
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.