Use roofit to do constrain fit

Dear experts,

I was trying to do a constraint fit following example from … nts.C.html

1, I tried to modify this example, attached as: rf604_constraints_test.C
I consider only adding constraint to the model, called “modelc” as in original example.
Then I try to calculate NLL using the following ways:

nll = modelc.createNLL(*d);
nllc = modelc.createNLL(*d, Constrain(f));

it turns out nll and nllc have the same value: 139.616
then when I try to do modelc.fitTo(*d) and modelc.fitTo(*d, Constrain(f)), fitted parameters have the same final value and error

I am wondering if these are expected features, if they are, then why it would be good to add Constrain(f) explicitly?

2, next I move on applying this technique on my analysis

Attached testMacro.C reproduces my problem:

I want to get parameters(pTMean1_lep, pTMean1_lep) which maximize likelihood using one dataset(pTRECO1_lep, pTRECO1_lep)

likelihood is created from the following model: gauss1gauss2adhoc_Function
pTMean1 and pTMean2 are parameters from gauss1 and gauss2(mean),.
ad_hocFunction is a function of mZ, mZ is a rooFormulaVar, calculated from pTMean1,pTMean2 and other constants. This ad_hocFunction is designed to be a constraint for the model.

Then I tried to calculate NLL using these two ways:

nll = model->createNLL(*pTs);
nllc = model->createNLL(*pTs, Constrain(*mZ));

They return different values, first is 4.32775, second is 4.23396
This seems not consistent with example from rf604*.C
Roofit complains when using first way:

“gaussShape2” is a component of the ad_hocFunction, it indeed has a small pdf (close to 0) by itself when mZ = 53.2. I am wondering why roofit does not complain using the second way.

Also when I move on with fitting(commented in the end of testMarco.C):

r = model->fitTo(*pTs,  RooFit::Save(), RooFit::Minos(true), RooFit::PrintLevel(-1));
r = model->fitTo(*pTs,  RooFit::Save(), RooFit::Constrain(*mZ), RooFit::Minos(true), RooFit::PrintLevel(-1));

The second way does a proper fit from printOut of roofit, while the first one doesn’t, it keeps complaining:

This is related to the error message when calculating NLL in previous step

I understand to add "Constrain(mZ)" is the right way to deal with constrain fit in my case. But I don’t understand what is happening behind the scene. When I checked this post: [url]Fitting using constraints it was commented that specify a constraint meaning add an extra term in NLL. Then I am not sure in attached modified rf604.C, why nll and nllc have the same initial value, and give same fitted parameters in the end.
I would really appreciate if one can point me to some hints why in testMarco.C, fitTo without specifying “Constrain(*mZ)” failed so bad.

Many thanks,
rf604_constraints_test.C (3.35 KB)
testMacro.C (6.49 KB)


The second fit is the correct way to handle the constraints as shown in the tutorial. I think in case of simple constraints, such as those in the example you can avoid to specify them in the fitTo (or createNLL) command.
However in case of complicated model or non trivial constraints then you need to tell explicitly to RooFIt that these functions are constraints.

you can see that if you try a fit it will converge only in the second case

Best Regards