How to use complex function to define a new column using ROOT.RDataFrame

Dear experts,

I try to use a little complex function to define a new column using ROOT.RDataFrame,
Now I can achieve it, but it’s a little complex, could you please help me to simplify it ?
The related codes are shown below

FF_file_name = args.FF_name
FF_file_name_2 = args.FF_name2 

ROOT.gInterpreter.Declare(f"""        
using namespace ROOT;      
TFile*f1=new TFile("{FF_file_name}","read");
TH1F*fake_hist=(TH1F*)f1->Get("fake_factor_hist;1");
cout<<"{FF_file_name}"<<" is read"<<endl;
TFile*f2=new TFile("{FF_file_name_2}","read");
TH1F*fake_hist_2=(TH1F*)f2->Get("fake_factor_hist;1");
cout<<"{FF_file_name_2}"<<" is read"<<endl;
     
"""  
)    
ROOT.gInterpreter.ProcessLine("""     
TH1F*fake_hist=(TH1F*)f1->Get("fake_factor_hist;1");
TH1F*fake_hist2=(TH1F*)f2->Get("fake_factor_hist;1");
""")

rdf_filtered = rdf_filtered.Define('fakefactor',f"fake_hist->Interpolate(ph_pt)")

By the way , it x >10 ,I want to use fake_hist to interpolate, but if x<10, I want to use fake_hist_2 to interpolate, Is there any simple way to achieve it?
Many thanks in advance!

Thanks for this interesting post!

Let me start by saying that we are working hard to pythonise better the interface of RDataFrame: the analysis interface is very powerful, and we are aware that the experience of our users could greatly benefits if some obstacles are removed when it comes to C++ and Python interplay.

This example should show how you can simplify your code: the workaround consists in leveraging a feature of ROOT which allows to access names of objects as if they were pointers.

I hope it helps, perhaps as a start!

FF_file_name = args.FF_name

f1 = ROOT.TFile.Open(FF_file_name)
fake_hist = f1.fake_factor_hist

# Workaround: make the histos known to the interpreter
ROOT.gInterpreter.ProcessLine("auto fh1 = fake_hist")

rdf_filtered = rdf_filtered.Define('fakefactor',f"fh1->Interpolate(ph_pt)")

Hi Danilo,

Thank you very much, but I got two issues.

  1. input_line_81:2:24: error: variable ‘fake_hist_temp’ declared with deduced type ‘auto’ cannot appear in its own initializer
    auto fake_hist_temp = fake_hist_temp

I used TH1F* to substitute auto, it seems OK.

  1. /eos/home-z/zgao/H_to_Zy/samples/root_to_parquet_2_test.py:95: DeprecationWarning: The attribute syntax for TNetXNGFile is deprecated and will be removed in ROOT 6.34. Please use TNetXNGFile[“fake_factor_hist”] instead of TNetXNGFile.fake_factor_hist
    fake_hist_temp = fake_file_1.fake_factor_hist

It seems no problem, is it better to use

fake_file_1["fake_factor_hist"]
1 Like

Hi,

Thanks. You do have a point.
I am curious: what ROOT version are you using? The complaint about type deduction is somewhat not expected…

Cheers,
D

Hi,
I’m using ROOT 6.32.06

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