Save Sweight Factor into RDataFrame Directly

Dear all,

as the title indicates, can we save Sweight factor into RDataFrame directly?

I know classic TTree interfaces can do it. But I am wondering it is faster when using RDataFrame?

Thank you for your attention to my simple question.

Boan

Dear @bshi ,

Could you be more specific about your use case? For example, a small snippet of code showing what you would try to do with TTree might help.

Cheers,
Vincenzo

Hi Vpadulan,

Thanks for your reply.

The simple code using TTree interfaces is as follows.

TFile *file = new TFile(out_file, “recreate”);
TTree *result = tree1->CloneTree(0);// tree1 is the mother tree
double sig_sw{0};
result->Branch(“sig_sw”, &sig_sw, “sig_sw/D”);
for(Int_t i=0; i< nevent; ++i){
tree1->GetEntry(i);
sig_sw = sData->GetSWeight(i,“sig”);
result->Fill();
}
file->cd();
result->Write();
file->Close();

I want to convert them into RDataFrame classes. For example,

ROOT::RDataFrame rdf(*tree1);
auto new_rdf = rdf.Define(“sig_sw”,“expression”).Snapshot(“new_tree”,output_file);

Do you have any good ideas?

With best respect,
Boan

Hi @bshi ,

One possibility could be

TFile input_f{"original.root"};
std::unique_ptr<TTree> input_t{f.Get<TTree>("original")};


ROOT::RDataFrame df{*input_t};

df.Define("sWeight", 
          [&](std::uint64_t entry){
              return sData->GetSWeight(entry, "sig");
          },
          {"rdfentry_"})
  .Snapshot("out_tree", "out_file.root");

Caveat: the rdfentry_ special column corresponds to the current entry in the original tree traversed sequentially only in single thread. In multithreaded execution it gets shuffled (see the docs).

Hi, @vpadulan.

I meet this problem when executing your code.

terminate called after throwing an instance of ‘std::runtime_error’
what(): RColumnValue: type specified for column “rdfentry_” is unsigned long but temporary column has type ULong64_t

I don’t understand it. Could you tell me what this is about?

With best respect,
Boan

Hi @bshi ,

I may have slightly mispecified the column type, instead of std::uint64_t just type unsigned long and that error should go away.

Cheers,
Vincenzo

Hi @vpadulan .

This problem still remains when type unsigned long.

df.Define("sWeight", 
          [&](unsigned long entry){
              return sData->GetSWeight(entry, "sig");
          },
          {"rdfentry_"})
  .Snapshot("out_tree", "out_file.root");
terminate called after throwing an instance of 'std::runtime_error'
  what():  RColumnValue: type specified for column "rdfentry_" is unsigned long but temporary column has type ULong64_t

Thanks for your help again.

Boan

Maybe with ULong64_t :)?

Yes, it is.

Thank you! :smile:

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