Dear ROOT experts,
I’m trying to create histograms using RDataFrame
, but I’m unable to use the Aliases for the branches I’ve stored in a file beforehand. Here’s a reproducer
import ROOT
def create_file(file_name, tree_name):
df = ROOT.ROOT.RDataFrame(10).Define("x", "gRandom->Rndm()")
df.Snapshot(tree_name, file_name, "")
def create_alias(file_path, tree_name):
file = ROOT.TFile(file_path, 'update')
tree = file.Get(tree_name)
tree.SetAlias("x_alias", "x")
tree.Write('', ROOT.TObject.kOverwrite)
file.Close()
def create_hist_tree(file_path, tree_name):
file = ROOT.TFile(file_path)
tree = file.Get(tree_name)
tree.Draw("x_alias")
def create_hist_df(file_path, tree_name):
df = ROOT.RDataFrame(tree_name, file_path)
df.Histo1D(("h", "h", 10, 0, 100), "x_alias")
def main():
file_path = 'f.root'
tree_name = 'tree'
print("Creating file")
create_file(file_path, tree_name)
print("Creating alias")
create_alias(file_path, tree_name)
print("Making hist with TTree")
create_hist_tree(file_path, tree_name)
print("Making hist with RDataFrame")
create_hist_df(file_path, tree_name)
if __name__ == "__main__":
main()
The output of the program is
Creating file
Creating alias
Making hist with TTree
Info in <TCanvas::MakeDefCanvas>: created default TCanvas with name c1
Making hist with RDataFrame
Traceback (most recent call last):
File "rdf_alias.py", line 36, in <module>
main()
File "rdf_alias.py", line 33, in main
create_hist_df(file_path, tree_name)
File "rdf_alias.py", line 21, in create_hist_df
df.Histo1D(("h", "h", 10, 0, 100), "x_alias")
File "/home/alex/root_62806/root/lib/ROOT/_pythonization/_rdataframe.py", line 379, in __call__
res = self._original_method(model, *args[1:])
TypeError: Template method resolution failed:
none of the 4 overloaded methods succeeded. Full details:
ROOT::RDF::RResultPtr<TH1D> ROOT::RDF::RInterface<ROOT::Detail::RDF::RLoopManager,void>::Histo1D(experimental::basic_string_view<char,char_traits<char> > vName) =>
TypeError: takes at most 1 arguments (2 given)
ROOT::RDF::RResultPtr<TH1D> ROOT::RDF::RInterface<ROOT::Detail::RDF::RLoopManager,void>::Histo1D(const ROOT::RDF::TH1DModel& model, experimental::basic_string_view<char,char_traits<char> > vName, experimental::basic_string_view<char,char_traits<char> > wName) =>
TypeError: takes at least 3 arguments (2 given)
ROOT::RDF::RResultPtr<TH1D> ROOT::RDF::RInterface<ROOT::Detail::RDF::RLoopManager,void>::Histo1D(experimental::basic_string_view<char,char_traits<char> > vName, experimental::basic_string_view<char,char_traits<char> > wName) =>
TypeError: could not convert argument 1
ROOT::RDF::RResultPtr<TH1D> ROOT::RDF::RInterface<ROOT::Detail::RDF::RLoopManager,void>::Histo1D(const ROOT::RDF::TH1DModel& model = {"", "", 128U, 0., 0.}, experimental::basic_string_view<char,char_traits<char> > vName = "") =>
runtime_error: Unknown column: "x_alias"
Failed to instantiate "Histo1D(ROOT::RDF::TH1DModel*,std::string)"
So the TTree::Draw()
approach is able to use the x_alias
name for the x
branch, but the RDataFrame
cannot find such column
ROOT::RDF::RResultPtr<TH1D> ROOT::RDF::RInterface<ROOT::Detail::RDF::RLoopManager,void>::Histo1D(const ROOT::RDF::TH1DModel& model = {"", "", 128U, 0., 0.}, experimental::basic_string_view<char,char_traits<char> > vName = "") =>
runtime_error: Unknown column: "x_alias"
Why might that happen and is there a way around it? I would like to avoid using Alias()
function of RDataFrame
and make aliases before the processing to not complicate the code.
Thanks in advance,
Aleksandr
ROOT Version: 6.28/06
Platform: Ubuntu 20.04.6
Compiler: precompiled