10#include "CorrFitFunc.h"
12#include "ChiSqMap2D.h"
13#include "CorrFitGUI.h"
14#include "CorrFitHDFunc.h"
15#include "CorrFitPainter.h"
26#include <Math/Factory.h>
27#include <Math/Functor.h>
28#include <Math/Minimizer.h>
35#include <TLegendEntry.h>
39#include <TVirtualPad.h>
50 fMaxIterations(10000),
56 fDenominatorHistogram(nullptr),
57 fNumeratorHistogram(nullptr),
58 fCorrelationFunctionHistogram(nullptr),
64 for (
int i = 0; i <
fDim * 2; i++) {
74 if (option.EqualTo(
"fitted")) {
76 }
else if (option.EqualTo(
"fitting")) {
114 for (
int i = 0; i <
fDim; i++) {
135 const Double_t NumErr,
137 const Double_t DenErr,
139 Double_t& cfe)
const {
141 Double_t D2 = Den * Den;
142 cfe = TMath::Sqrt((NumErr * NumErr + Num * Num / D2 * DenErr) / D2);
146 ROOT::Math::Minimizer *min, *min2 =
nullptr;
149 min = GetMinimizer1(fMinAlgo);
150 min2 = GetMinimizer2(fMinAlgo);
151 ROOT::Math::Functor f;
159 if (min2) min2->SetFunction(f);
164 const double* parameters_guess = min->X();
165 const double* errors_guess = min->Errors();
166 PrepareSecondMiminizer(min, parameters_guess, errors_guess);
167 SetParsOfMinimizer(min2);
171 if (min2 ==
nullptr) min2 = min;
173 const double* parameters = min2->X();
174 const double* errors = min2->Errors();
188 int to = fFitOrder[i];
195 fChi[0] = GetChiTF(parameters);
223 auto testParam = [&](Int_t par, Int_t& step, Double_t& min, Double_t& max) {
225 min = Std::Discretize(parval.GetNPoints() - 1, parval.GetMapMin(), parval.GetMapMax(), min,
'-');
226 max = Std::Discretize(parval.GetNPoints() - 1, parval.GetMapMin(), parval.GetMapMax(), max,
'+');
227 if (min < parval.GetMapMin()) min = parval.GetMapMin();
228 if (max > parval.GetMapMax()) max = parval.GetMapMax();
229 double step_size = parval.GetDParam();
230 min -= step_size * 0.5;
231 max += step_size * 0.5;
232 step = std::round((max - min) / parval.GetDParam());
236 if (
IsParDiscrete(par1)) { testParam(par1, par1_steps, par1_min, par1_max); }
237 if (
IsParDiscrete(par2)) { testParam(par2, par2_steps, par2_min, par2_max); }
242 case kChi: var =
"#chi^{2}_{den}";
break;
243 case kLog: var =
"loglike";
break;
244 case kChi2: var =
"#chi^{2}";
break;
246 TString title = var +
"map";
247 if (scale) { title = var +
" map (scaled)"; }
248 ChiSqMap2D* map =
new ChiSqMap2D(
"chi_map", par1_steps, par1_min, par1_max, par2_steps, par2_min, par2_max);
254 for (
double i = 1; i <= par1_steps; i++) {
255 Double_t par1_val = map->
GetHist()->GetXaxis()->GetBinCenter(i);
256 for (
double j = 1; j <= par2_steps; j++) {
257 Double_t par2_val = map->
GetHist()->GetYaxis()->GetBinCenter(j);
263 chi = GetChiTFD(params);
266 chi = GetChiTF(params);
269 chi = GetLogTFD(params);
271 default: chi = 0;
break;
276 map->
GetHist()->SetBinContent(i, j, chi / bins);
279 map->
GetHist()->SetBinContent(i, j, chi);
293 CorrFitFunc::~CorrFitFunc() {
314 Double_t f = GetChiTFD(params);
316 std::cout <<
"*** <ChiTFD> ***" << f <<
" / " <<
fActiveBins << std::endl;
318 std::cout <<
"\t" <<
GetParameterName(i) << Form(
"%4.5f", params[i]) << std::endl;
328 Double_t f = GetChiTF(params);
330 std::cout <<
"*** <ChiTF> ***" << f <<
" / " <<
fActiveBins << std::endl;
332 std::cout <<
"\t" <<
GetParameterName(i) << Form(
"%4.5f", params[i]) << std::endl;
342 Double_t f = GetLogTFD(params);
344 std::cout <<
"*** <LogTFD> ***" << f <<
" / " <<
fActiveBins << std::endl;
346 std::cout <<
"\t" <<
GetParameterName(i) << Form(
"%4.5f", params[i]) << std::endl;
379 if (fMinAlgo == EMinAlgo::kHalAnt) {
380 min->SetMinimizerType(
"ant");
382 min->SetMinimizerType(
"scan");
386 std::string name = Param.GetParName().Data();
387 if (Param.IsFixed()) {
390 Double_t lower = Param.GetMin();
391 Double_t upper = Param.GetMax();
392 min->
SetLimitedVariable(i, name, 0.5 * (lower + upper), (upper - lower), lower, upper);
396 min->SetParamConf(conf,
false);
410 for (
unsigned int i = 0; i <
fParameters.size(); i++) {
414 if (fMinAlgo == kDefaultAlgo) fMinAlgo = kMinuitScan;
429 for (
unsigned int i = 0; i <
fParameters.size(); i++) {
433 if (fMinAlgo == kDefaultAlgo) fMinAlgo = kMinuitScan;
440 if (Param.IsFixed()) {
441 if (TMath::IsNaN(Param.GetStartVal())) {
442 Cout::Text(Form(
" Par No. %i Is Nan fixed parameter", fFitOrder[i]),
"M", kRed);
444 min->SetFixedVariable(i, Param.GetParName().Data(), Param.GetStartVal());
446 if (TMath::IsNaN(Param.GetStartVal())) {
Cout::Text(Form(
" Par No. %i Is Nan parameter", fFitOrder[i]),
"M", kRed); }
447 Double_t step = TMath::Max(Param.GetDParam(), (Param.GetMax() - Param.GetMin()) / 100.0);
448 min->SetLimitedVariable(i, Param.GetParName().Data(), Param.GetMin(), step, Param.GetMin(), Param.GetMax());
464 for (
unsigned int i = 0; i <
fParameters.size(); i++) {
468 if (fMinAlgo == kDefaultAlgo) fMinAlgo = kMinuitScan;
473 ROOT::Math::Minimizer* min =
nullptr;
476 static_cast<Minimizer*
>(min)->SetMinimizerType(
"scan");
479 SetParsOfMinimizer(min);
480 auto minx =
static_cast<Minimizer*
>(min);
483 ROOT::Math::Functor f;
493 const double* parameters = min->X();
498 std::cout <<
"Estimated minimum of parameter " <<
fParameters[i].GetName() <<
" is " << Form(
"%4.4f", parameters[i]);
501 }
else if (bins > 0) {
507 double dx = p.GetDParam();
508 SetParLimits(i, parameters[i] - scale * dx, parameters[i] + scale * dx);
509 std::cout <<
"PRESCALED " << p.GetParName() <<
" " << (parameters[i] - scale * dx) <<
" "
510 << (parameters[i] + scale * dx) << std::endl;
513 }
else if (bins < 0) {
518 double dx = p.GetDParam();
519 SetParLimits(i, parameters[i] - dx, parameters[i] + dx);
521 double npoints = dx / bins + 1;
522 conf.
ConfigureParameter(p.GetParName(), npoints, parameters[i] - dx, parameters[i] + dx);
523 minx->SetParamConf(conf,
false);
529 int to = fFitOrder[i];
536 fChi[0] = GetChiTF(parameters);
542 std::ofstream file(xmlFile);
543 file <<
"<minimizer>" << std::endl;
546 <<
"\" step=\"0.01\"></param>" << std::endl;
548 file <<
"</minimizer>" << std::endl;
552 void CorrFitFunc::Draw(Option_t* option) {
553 bool painter_set =
true;
574 void CorrFitFunc::Repaint() {
582 for (
int ispe = 0; ispe < 2; ispe++) {
583 if (gSystem->ProcessEvents())
break;
588 void CorrFitFunc::SetParsOfMinimizer(ROOT::Math::Minimizer* min)
const {
594 Int_t CorrFitFunc::CountNDF()
const {
595 Int_t freeParameters = 0;
602 ROOT::Math::Minimizer* CorrFitFunc::GetMinimizer1(EMinAlgo algo)
const {
603 auto algos = AlgoToOptions(fMinAlgo);
604 if (algos.size() < 2) algos.push_back(
"");
605 TString pat1 = algos[0];
606 TString pat2 = algos[1];
607 ROOT::Math::Minimizer* min =
nullptr;
608 if (pat1.EqualTo(
"HalMinimizer")) {
610 static_cast<Minimizer*
>(min)->SetMinimizerType(pat2);
611 static_cast<Minimizer*
>(min)->SetNDF(
fNDF);
614 min = ROOT::Math::Factory::CreateMinimizer(pat1.Data(), pat2.Data());
617 SetParsOfMinimizer(min);
621 ROOT::Math::Minimizer* CorrFitFunc::GetMinimizer2(EMinAlgo algo)
const {
622 auto algos = AlgoToOptions(fMinAlgo);
623 if (algos.size() <= 3)
return nullptr;
624 TString pat1 = algos[2];
625 TString pat2 = algos[3];
626 ROOT::Math::Minimizer* min =
nullptr;
627 if (pat1.EqualTo(
"HalMinimizer")) {
630 static_cast<Minimizer*
>(min)->SetMinimizerType(pat2);
631 static_cast<Minimizer*
>(min)->SetNDF(
fNDF);
634 min = ROOT::Math::Factory::CreateMinimizer(pat1.Data(), pat2.Data());
637 SetParsOfMinimizer(min);
641 void CorrFitFunc::PrepareSecondMiminizer(ROOT::Math::Minimizer* min,
642 const double* parameters_guess,
643 const double* errors_guess)
const {
646 if (Param.IsFixed()) {
647 min->SetFixedVariable(i, Param.GetParName().Data(), Param.GetStartVal());
649 Double_t param = parameters_guess[i];
650 Double_t minpar = param - 3.0 * errors_guess[i];
651 Double_t maxpar = param + 3.0 * errors_guess[i];
652 minpar = TMath::Max(Param.GetMin(), minpar);
653 maxpar = TMath::Min(Param.GetMax(), maxpar);
654 Double_t step = (maxpar - minpar) * 0.1;
655 min->SetLimitedVariable(i, Param.GetParName().Data(), parameters_guess[i], step, minpar, maxpar);
657 std::cout <<
"Set limits " << Param.GetParName() <<
"\t"
658 << Form(
" %4.4f+/-%4.4f", parameters_guess[i], 3.0 * errors_guess[i]) << std::endl;
void MakeBigger(Int_t new_dim)
void SetParNames(TString xpar, TString ypar)
void SetXFit(Double_t Value, Double_t errLow=0, Double_t errHigh=-1)
void SetYFit(Double_t Value, Double_t errLow=0, Double_t errHigh=-1)
TH1 * fDenominatorHistogram
virtual void PrepareRootMinimizer(ROOT::Math::Minimizer *minizer) const
double FunctorLogTFD(const double *params)
CorrFitPainter * fPainter
void ParametersChanged() const
virtual void PreFit(TObject *histo, Double_t bins=1)
void MakeDummyXMLConfig(TString xmlFile)
TH1 * fCorrelationFunctionHistogram
double FunctorChiTF(const double *params)
ChiSqMap2D * GetChiSquareMap(Int_t par1, Int_t par1_steps, Int_t par2, Int_t par2_steps, Bool_t scale=kTRUE, EMinFunc=kChi2) const
Double_t GetRangeMin(Int_t flag=0) const
void NumericalPreMinimization(Double_t bins)
void DummyNumericalFunction()
Femto::EKinematics fKinematics
virtual void FitDummy(TObject *histo)
virtual void Fit(TObject *histo)
void NumericalMinimization()
Hal::CorrFitGUI * StartGui(Int_t prec=-1)
virtual void EstimateActiveBins()=0
void SetupFunction(TF1 *f) const
Double_t GetChiNDF(Option_t *opt="fitted") const
virtual void PrepareHalMinimizer() const
virtual void MakePainter(TString options)=0
void CalcError(const Double_t Num, const Double_t NumErr, const Double_t Den, const Double_t DenErr, Double_t &cf, Double_t &cfe) const
void SetMinimizerConf(const MinimizerStepConf &conf)
void SetRange(Double_t min, Double_t max)
Int_t GetFreeParamsNo() const
double FunctorChiTFD(const double *params)
virtual void PrepareRaw()=0
Double_t GetRangeMax(Int_t flag=0) const
CorrFitFunc(Int_t nparams=1, Int_t dim=1)
Array_1< Double_t > fRange
Double_t GetChiSquare(Option_t *opt="fitted") const
Double_t GetParError(Int_t par) const
Int_t GetParametersNo() const
TString GetParameterName(Int_t no) const
Double_t GetParameter(Int_t par) const
Double_t GetParMax(Int_t par) const
Double_t GetParMin(Int_t par) const
Bool_t IsParDiscrete(Int_t par) const
Double_t * fTempParamsEval
void SetParLimits(Int_t par, Double_t min, Double_t max)
std::vector< FitParam > fParameters
Bool_t IsParFixed(Int_t par) const
static void Text(TString text, TString option="L", Color_t color=-1)
static void PrintInfo(TString text, Hal::EInfo status)
static Hal::EInfo GetVerboseMode()
void ConfigureParameter(TString name, Double_t step, Double_t min, Double_t max, TString option="")
void LoadFromXML(TString xmlFile)
virtual bool SetFixedVariable(unsigned int ivar, const std::string &name, double val)
static Minimizer * Instance()
virtual bool SetLimitedVariable(unsigned int ivar, const std::string &name, double val, double points, double lower, double upper)
virtual void SetOption(TString option)