Need to overload ROOT::Math::GSLMinimizer::Minimize()

Within a large software framework F used by dozens of people, I utilize ROOT::Math::GSLMinimizer::Minimize(). ROOT::Math::GSLMinimizer derives from abstract class ROOT::Math::Minimizer. I wrote F::ClassE that lives within our framework, successfully creates an object pointer of type ROOT::Math::Minimizer*, and successfully uses the function ROOT::Math::GSLMinimizer::Minimize(). What I need is a very slightly modified overloaded version of that function, Minimize( int &iteration ), that gives the end-user access to the current iteration within my F::ClassE. If I lived in a vacuum and no one else needed to use ROOT, I would just modify the source code for ROOT::Math::GSLMinimizer to include my overloaded function, and la la la la life goes on. However, many people rely on ROOT, and it is not the best idea for me to patch ROOT and try to convince the powers-that-be to use my patched version of ROOT for everything else, even though the change would not affect others.

I propose to create either a friend class (which I have never used) or an inherited class (which I have used plenty, but not from an already derived class) F::ClassG, which should live inside my package, include this overloaded function that I want, allow me to use it within F::ClassE, and not disrupt ROOT as it is compiled for everyone’s use. Should ClassG inherit from ROOT::Math::Minimizer, or from R::Math::GSLMinimizer? Or some other approach? I’ve tried a few approaches that do not compile, either because they are incorrect or because I haven’t carried them out properly.

I currently create the object pointer this way:
ROOT::Math::Minimzer* min = ROOT::Math::factory::CreateMinimizer( m_MinCategory, m_MinType );
The method Minimize() exists in both the parent class ROOT::Math::Minimizer (abstract) and the derived class ROOT::Math::GSLMinimizer, where I need the overloaded function.
The m_MinCategory selects GSLMinimizer, which selects that class’s implementation of the abstract method ::Minimize().

Do I need a modified version of ROOT::Math::Factory that would CreateMinimizer of my new category/type? Is there a simple solution that I am overlooking completely, besides using a private version of ROOT?


The simplest solution is to create a new plug-in which instantiates your derived class. You make your class deriving either from GSLMinimizer or from Minimizer, depends on you and how many things are in common.
If it is just to add a method, I would inherit from GSLMinimizer.

Then to define a plugin you can :

  • either add a new entry in etc/plugins/ROOT@@Math@@Minimizer

with a new macro as:

void P099_MyMinimizer()

   gPluginMgr->AddHandler("ROOT::Math::Minimizer", "my_minimizer", "MyMinimizer",
      "MyLibrary", "MyMinimizer(const char *)");

where the second parameter is the plug-in name that you use to create the class in ROOT::Math::Factory, the third is the class name, the 4th the library name and the 5th the constructor signature.

If you don’t want to add the new entry in /etc, you can just add the line of code contained in the macro in your initialisation code or in your .root_login

Best Regards


Lorenzo, I think I implemented this properly in my initialization code, but at compile time my object of type ROOT::Math::Minimizer still can’t see my modified version of the method Minimize( unisigned int&). Is the following no longer valid?

ROOT::Math::Minimizer* min = ROOT::Math::factory::CreateMinimizer( m_MinCategory, m_MinType );

I try min->Minimize( m_currentIteration );

but Minimizer of course has no method of this signature, only our inherited class has it.