Skip to content

Commit c911204

Browse files
committed
[math][fitter] Fix crash when doing a weighted likelihood fit
A bug was introduced in #10439 affecting the weighted likelihood fits Add missing test for Weighted likelihood fits in stressHistoFit
1 parent 938626c commit c911204

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

math/mathcore/src/Fitter.cxx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -847,14 +847,16 @@ bool Fitter::DoMinimization(std::unique_ptr<ObjFunc_t> objFunc, const ROOT::Mat
847847
template<class ObjFunc_t>
848848
bool Fitter::DoWeightMinimization(std::unique_ptr<ObjFunc_t> objFunc, const ROOT::Math::IMultiGenFunction * chi2func) {
849849
// perform the minimization initializing the minimizer starting from a given obj function
850-
// and apply afterwards the correction for weights. This applyies only for logL fitting
850+
// and apply afterwards the correction for weights. This applies only for logL fitting
851851
this->fFitType = objFunc->Type();
852852
fExtObjFunction = nullptr;
853-
fObjFunction = std::move(objFunc);
853+
// need to use a temporary shared pointer to the objective function since we cannot use the unique pointer when it has been moved
854+
std::shared_ptr<ObjFunc_t> sObjFunc{ std::move(objFunc)};
855+
fObjFunction = sObjFunc;
854856
if (!DoInitMinimizer()) return false;
855857
if (!DoMinimization(chi2func)) return false;
856-
objFunc->UseSumOfWeightSquare();
857-
return ApplyWeightCorrection(*objFunc);
858+
sObjFunc->UseSumOfWeightSquare();
859+
return ApplyWeightCorrection(*sObjFunc);
858860
}
859861

860862

test/stressHistoFit.cxx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -771,6 +771,7 @@ int test1DObjects(vector< vector<algoType> >& listH,
771771
for (int i = 1; i <= h1->GetNbinsX() + 1; ++i)
772772
h1->SetBinContent(i, rndm.Poisson(func->Eval(h1->GetBinCenter(i))));
773773

774+
h1->Sumw2(); // make as a weighted histo to test weighted fits
774775
gTestIndex++;
775776
if (gSelectedTest == 0 || gSelectedTest == gTestIndex) {
776777
// fill equal bin 1D histogram
@@ -803,7 +804,7 @@ int test1DObjects(vector< vector<algoType> >& listH,
803804
h2->GetXaxis()->GetBinUpEdge(i));
804805
h2->SetBinContent(i,rndm.Poisson(expValue));
805806
}
806-
807+
h2->Sumw2(); // make as a weighted histo to test weighted fits
807808
if (c0 && !__DRAW__) delete c0;
808809
c0 = new TCanvas(TString::Format("c%d_H1D", gTestIndex), "Histogram1D Variable");
809810
ObjectWrapper<TH1D *> owh2(h2);
@@ -1219,6 +1220,7 @@ void init_structures()
12191220
noGraphAlgos.push_back( algoType( "Minuit2", "Migrad", "EX", CompareResult()) );
12201221
noGraphAlgos.push_back( algoType( "Minuit", "Migrad", "L", CompareResult(defCmpOpt,3,0.1)) ); // normal binned likelihood fit
12211222
noGraphAlgos.push_back( algoType( "Minuit2", "Migrad", "L", CompareResult(defCmpOpt,3,0.1)) );
1223+
noGraphAlgos.push_back( algoType( "Minuit2", "Migrad", "WL", CompareResult(defCmpOpt,3,0.1)) );
12221224
noGraphAlgos.push_back( algoType( "Minuit2", "Migrad", "G", CompareResult()) ); // gradient chi2 fit
12231225
//noGraphAlgos.push_back( algoType( "Minuit", "Migrad", "G", CompareResult()) ); // skip TMinuit with G
12241226
noGraphAlgos.push_back( algoType( "Minuit2", "Minimize", "G", CompareResult()) );

0 commit comments

Comments
 (0)