Create weighted RooDataset with RDataframe

Hi ROOTers,

I have encountered a problem while creating a RooDataSet using the RDataFrame interface. I followed the tutorial and wrote the following code:

RooRealVar Ks_M("Ks_M", "Ks Mass", fit_min, fit_max);
RooRealVar sb_weight("sb_weight", "Sideband weight", 0.0);

auto filtered_df = df
    .Filter(All_Cut.GetTitle())
    .Define("sb_weight", []() { return -0.6; });

auto data_weighted = filtered_df.Book<double>(
    RooDataSetHelper{
        "weighted_dataset",
        "weighted_Dataset",
        RooArgSet(Ks_M),
    },
    {"Ks_M", "sb_weight"}
);

However, I receive the following error:

terminate called after throwing an instance of 'std::runtime_error'
  what():  1 column name is required but 2 were provided: "Ks_M", "sb_weight".

Please let me know if you have any idea.

Best regards,
bshi

ROOT version: 6.32.02

Hi @bshi,

Thanks for your question. I’m adding @vpadulan in the loop.

Cheers,
Dev

Did you try removing “sb_weight” from the last line?

or alternatively maybe adding sb_weight to rooargset (and adding second double) in Book<>

Hi @ferhue,

Thank you for your help. I implemented the changes as you recommended, and here is the updated code:

auto data_weighted = filtered_df.Book<double, double>(
    RooDataSetHelper{
                    "weighted dataset",  
                    "weighted Dataset",  
                    RooArgSet(Ks_M, sb_weight)},  
    {"Ks_M"}  
);

However, I encountered the following error:

terminate called after throwing an instance of 'std::runtime_error'  
  what():  2 column names are required but 1 was provided: "Ks_M".  

To resolve this, I modified the last line to include both columns:

{"Ks_M", "sb_weight"}  

The code now runs without errors, but it doesn’t produce the intended result. Specifically, when I calculate the sum of weights using the following code:

double sum_weight = 0.0;  
for (int i = 0; i < data_weighted->numEntries(); ++i) {  
    sum_weight += data_weighted->weight();  
}  
std::cout << "Sum of weights = " << sum_weight << std::endl;  

The output is:

Sum of weights = 55  

This indicates that the weighting is not being applied correctly, as the original number of events is 55, and I included negative weights in the dataset.

Additionally, I performed a test using the following code:

auto data_weighted = filtered_df.Book<double, double>(  
    RooDataSetHelper{  
        "weighted dataset",  
        "weighted Dataset",  
        RooArgSet(Ks_M),  
        RooFit::WeightVar("sb_weight")  
    },  
    {"Ks_M", "sb_weight"}  
);  

However, the error shows:

terminate called after throwing an instance of 'std::invalid_argument'  
  what():  RooDataSet can hold 1 variables per event, but RDataFrame passed 2 columns.  

I look forward to your more idea on this issue.

Thank you!

Best regards,
bshi

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