Apply ROOT::Math::Minimizer using RDataFrame


I’m trying to use ROOT::Math::Minimizer to minimize a log-likelihood that I define by summing the information of all the events included in a RDataFrame and one parameter.
Starting from the tutorial/fit/NumericalMinimization.C, I expect to define a Minimizer (done) and a Functor, but I’m not able to implement the Functor giving it access to RDataFrame columns.

Do you have any suggestion to point me in the right direction?
Or eventually a smarter method than using the Minimizer?


Please read tips for efficient and successful posting and posting code

Please fill also the fields below. Note that root -b -q will tell you this info, and starting from 6.28/06 upwards, you can call .forum bug from the ROOT prompt to pre-populate a topic.

ROOT Version: Not Provided
Platform: Not Provided
Compiler: Not Provided

Dear @Simone_Vallarino ,

I will try to give you an idea based on the context you describe, we could start from here and then try to work together towards your final solution. Given the minimizer created as in the tutorial, you can use it inside any type of C++ function and pass it to methods of the RDataFrame API, for example Define

auto external_par{1.};
df.Define("newcol", [&external_par](float val_1, float val_2){
    std::unique_ptr<ROOT::Math::Minimizer> mymin{ROOT::Math::Factory::CreateMinimizer(minName, algoName);}
    // do what's needed here and return values of the "newcol"
    {"col_1", "col_2"});

If instead you would like to use the functor to fill some object in a custom way at every event of your dataset, I suggest you take a look at this tutorial for the usage of the Book method paired with the Helper class interface ROOT: tutorials/dataframe/df018_customActions.C File Reference


Dear @vpadulan,

I implemented your example and my software is running now, thank you.
I’m going to check if I obtain the expected results and in that case my issue is perfectly solved! If not probably I will need to look into it further.


Hi @Simone_Vallarino ,

note that for production code you might want to lift the call to Factory::Create outside of the lambda that the Define executes (e.g. at the level of external_par in Vincenzo’s example), so you don’t re-create a minimizer at every event.


1 Like

Thanks Enrico.

My question now became: How can effectively minimize and access to the minimized parameter?
Because if I try to printout simply the value of “external_par” (always following the Vincenzo example) I obtain the initial value I set. But if I try to minimize mymin, it return “Error in : Minuit2Minimizer::Minimize FCN function has not been set”.


Did you also call minimum->SetFunction(f) like in the tutorial you were mentioning originally? I am not aware of all the extra machinery you might need to use in your function, I was just hinting at a possible starting point with Define. I also very much agree with Enrico’s suggestion to create the minimizer object outside of the RDataFrame calls, just taking it by reference when needed.


Ok, I agree that I should set the function, but how can I do it using RDataFrame? Probably it’s a simple question, but it’s the first time i’m using it.
The function that I need to minimize is the sum over all the events of the content of the “newcol”.


Do you have any suggestion about it?
It is not clear how to implement the “SetFunction” because the RDataFrame return a ROOT::RDF::RResultPtr object while SetFunction(const ROOT::Math::IMultiGenFunction & func) expect a different kind of argument.


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