Printing fit parameter with certain order

Hello,

I would like to ask about the order of printing the parameters from RooFitResult.
In my case, I declare a custom PDF class, called RooMyPdf, with the following constructor

RooMyPdf::RooMyPdf(
		const char* name, const char* title,
		RooAbsReal& x, RooAbsReal& sigma, RooAbsReal& mean );

I then create the PDF as following:

RooRealVar* x = new RooRealVar( "x", "", -10, 10 )
RooRealVar* sigma = new RooRealVar( "sigma", "", 0, 10 );
RooRealVar* mean  = new RooRealVar( "mean",  "", -1, 1 );
RooMyPdf* mypdf = new RooMyPdf( "mypdf", "", *x, *sigma, *mean );

Then do some fitting with the function.

When I look at the log file of the fitting, and also when I loop over the fit parameters, I found that the “mean” parameter is printed before the “sigma” parameter, even when “sigma” was placed in front of “mean” in the constructor.
In general, the parameters seem to be sorted in alphabetical order.

Is there any way to print the parameters in the same order as they are defined in the constructor?
Thank you very much.

Hoa.

I am sur @jonas can help you

1 Like

Hi @LongHoa,

I’m sorry, the alphabetical sorting is hardcoded in RooAbsReal::getParameters(), which is also used by the RooMinimizer to get the fit parameters.

Here is an example for how you can get the unsorted parameters with a little workaround:

   RooRealVar x("x", "x", -10, 10);
   // the parameter names are silly on purpose, such that the
   // alphabetical order is different from the argument order
   RooRealVar mean("sean", "mean of gaussian", 1, -10, 10);
   RooRealVar sigma("migma", "width of gaussian", 1, 0.1, 10);
   RooGaussian gauss("gauss", "gaussian PDF", x, mean, sigma);

   // The problem is that getParameters() always does alphabetical sort
   RooArgSet observables{x};
   RooArgSet parameters;
   gauss.getParameters(&observables, parameters);
   parameters.Print();

   // To get a list of parameters that is sorted in the same order as the
   // computation graph was created (this usually coincides with the
   // constructor argument order), we can use treeNodeServerList() to
   /// first get such  a list for all RooAbsArgs in the model.
   RooArgList nodes;
   gauss.treeNodeServerList(&nodes);

   // Now we filter the parameters
   RooArgSet parametersUnsorted;
   for(RooAbsArg *arg : nodes) {
      if(parameters.find(arg->GetName())) {
         parametersUnsorted.add(*arg);
      }
   }
   parametersUnsorted.Print();

Is this workaround pattern good enough for you?

If this is not enough, then you should make an improvement request on GitHub, because RooFit itself needs to be changed. Maybe it would be good to be able to optionally disable the alphabetical sorting, as it even has annoyed myself many times. It’s just inconvenient if the show log reshuffles just because you renamed one parameter! The problem is just that this needs to be configurable. If the parameter order just changes from one ROOT release to the next, it will make it much harder for the experiments to validate new ROOT versions.

I hope this helps,
Jonas

1 Like

Hi Jonas,

Thank you very much for your answer.
I think it’s good enough for me for the time being.

Best,
Hoa.

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