25 #include "TObjArray.h" 26 #include "TObjString.h" 40 :
TGo4FitterAbstract(), fxDatas(), fxModels(), fiFitFunctionType(0), fiMemoryUsage(0), fxUserFitFunction(nullptr),
60 if (IsAddStandardActions)
74 else if (iMemoryUsage > 100)
84 for (Int_t ndata = 0; ndata <
GetNumData(); ndata++)
87 for (Int_t nmodel = 0; nmodel <
GetNumModel(); nmodel++) {
106 return (n >= 0) && (n < GetNumData()) ? dynamic_cast<TGo4FitData *>(
fxDatas[n]) :
nullptr;
116 return !DataName ? nullptr :
dynamic_cast<TGo4FitData *
>(
fxDatas.FindObject(DataName));
130 if ((lrange < rrange) || (rrange != 0.))
147 if ((lrange < rrange) || (rrange != 0.))
164 for (Int_t n = 0; n < comp->
NumSlots(); n++) {
167 for (Int_t n2 = 0; n2 <
NumSlots(); n2++) {
202 return (n >= 0) && (n < GetNumModel()) ? dynamic_cast<TGo4FitModel *>(
fxModels[n]) :
nullptr;
236 Double_t lrange, Double_t rrange)
241 Bool_t flag = kFALSE;
243 Bool_t createmodel = kFALSE;
246 TString Prefix(NamePrefix);
250 Bool_t findsame = kFALSE;
252 for (Int_t Order = 0; Order <= MaxOrder; Order++) {
253 TString Name(Prefix);
257 if (findsame && !createmodel)
263 if ((lrange < rrange) || (rrange != 0.))
272 createmodel = kFALSE;
273 }
else if (createmodel)
286 Bool_t flag = kFALSE;
288 Bool_t createmodel = kFALSE;
291 Bool_t findsame = kFALSE;
293 for (Int_t n = 0; n < Coef.GetSize(); n++) {
294 TString Name(NamePrefix);
300 if (findsame && !createmodel)
314 createmodel = kFALSE;
315 }
else if (createmodel)
328 TArrayD Orders(NumAxis);
331 Bool_t flag = kFALSE;
333 Bool_t createmodel = kFALSE;
337 TString Prefix(NamePrefix);
341 Bool_t findsame = kFALSE;
344 TString Name(Prefix);
345 for (Int_t n = 0; n < NumAxis; n++) {
347 Name += Int_t(Orders[n]);
350 if (findsame && !createmodel)
362 if (Orders[nn] <= MaxOrder)
366 }
while (nn < NumAxis);
367 flag = (nn < NumAxis);
373 createmodel = kFALSE;
374 }
else if (createmodel)
383 Double_t iWidth, Double_t iAmpl, Int_t Axis)
401 newname = NewName ? NewName : mod->GetName();
502 Int_t dbuf = -1, mbuf = -1;
516 default: dbuf = -1; mbuf = -1;
534 std::cout <<
" User fit function not set. Switch to least squares " << std::endl;
552 switch (FitFunctionType) {
554 Double_t zn1 = (value - modelvalue);
560 Double_t zn2 = (value - modelvalue);
561 return zn2 * zn2 / standdev;
564 if (modelvalue <= 0.)
566 Double_t zn3 = (value - modelvalue);
567 return zn3 * zn3 / modelvalue;
570 Double_t zn4 = (value - modelvalue);
571 return zn4 * zn4 / ((value < 1.) ? 1. : value);
576 Double_t zn5 = (value + ((value < 1.) ? 0. : 1.) - modelvalue);
577 return zn5 * zn5 / (value + 1.);
580 if (modelvalue <= 0.)
582 return modelvalue - value * TMath::Log(modelvalue);
587 default:
return (value - modelvalue) * (value - modelvalue);
600 if (selectdata && (dat != selectdata))
610 for (Int_t nbin = 0; nbin < size; nbin++)
612 PointFitFunction(FitFunctionType, DataAmpl * values[nbin], res[nbin], DataAmpl * DataAmpl * devs[nbin]);
631 Double_t value = DataAmpl * iter->Value();
632 Double_t deviat = DataAmpl * DataAmpl * iter->StandardDeviation();
634 Double_t modelvalue = 0.;
635 for (Int_t nm = 0; nm <= Models.GetLast(); nm++) {
642 }
while (iter->Next());
644 for (Int_t nm = 0; nm <= Models.GetLast(); nm++)
654 if (FitFunctionType < 0)
709 for (Int_t nbin = 0; nbin < size; nbin++)
776 return !model ? nullptr : model->
GetModelBins(DataName);
782 if (Opt ==
"Ampls") {
785 }
else if (Opt ==
"Pars") {
788 }
else if (Opt ==
"Results") {
791 }
else if (Opt ==
"Lines") {
797 std::cout <<
"Fitiing function type: ";
799 case ff_chi_square: std::cout <<
"ff_chi_square" << std::endl;
break;
800 case ff_chi_Pearson: std::cout <<
"ff_chi_Pearson" << std::endl;
break;
801 case ff_chi_Neyman: std::cout <<
"ff_chi_Neyman" << std::endl;
break;
802 case ff_chi_gamma: std::cout <<
"ff_chi_gamma" << std::endl;
break;
803 case ff_ML_Poisson: std::cout <<
"ff_ML_Poisson" << std::endl;
break;
804 case ff_user: std::cout <<
"user defined" << std::endl;
break;
805 default: std::cout <<
"ff_least_squares" << std::endl;
807 std::cout << std::endl <<
" LIST OF DATA OBJECTS" << std::endl;
809 std::cout << std::endl <<
" LIST OF MODEL OBJECTS" << std::endl;
824 Int_t size = iter->CountPoints(UseRanges);
834 TArrayD Ampls(Models.GetLast() + 1);
835 for (Int_t n = 0; n <= Models.GetLast(); n++) {
841 TArrayD bins(size), scales(size);
845 if (iter->Reset(UseRanges))
847 Double_t value = iter->Value();
848 for (Int_t n = 0; n <= Models.GetLast(); n++) {
852 value = TMath::Abs(value);
854 scales[ppnt] = iter->x();
856 }
while (iter->Next(UseRanges));
858 for (Int_t n = 0; n <= Models.GetLast(); n++) {
866 Double_t sum00 = 0., sum11 = 0., sum22 = 0.;
867 for (Int_t pnt = 0; pnt < size; pnt++)
868 if ((bins[pnt] > 0.) && ((niter == 0) || (TMath::Abs(scales[pnt] - first) < second * 2.))) {
870 sum11 += bins[pnt] * scales[pnt];
871 sum22 += bins[pnt] * scales[pnt] * scales[pnt];
875 Double_t mid = sum11 / sum00;
876 Double_t dev = TMath::Sqrt(sum22 / sum00 - mid * mid);
879 if ((dev / second > 0.8) && (dev / second < 1.2))
889 }
while (niter++ < 8);
901 if (ModelName && !model)
905 if (!iter || !iter->Reset(kTRUE))
908 double sum = 0., ampl = 1.;
916 double dx = onlycounts ? 1. : iter->xWidths();
918 sum += ampl * value * dx;
919 }
while (iter->Next(kTRUE));
947 Int_t groupindex = -1;
950 TString modelname(ModelName);
951 if (modelname ==
"Background")
953 else if (modelname.Index(
"Group", 0, TString::kExact) == 0) {
954 modelname.Remove(0, 5);
956 groupindex = strtol(modelname.Data(), &err, 10);
957 if (err && (*err != 0))
965 if ((groupindex < 0) || (model->
GetGroupIndex() == groupindex))
969 if (Models.GetLast() < 0)
974 if (!iter || !iter->Reset(kFALSE))
977 TH1 *histo =
nullptr;
979 TGraph *gr =
nullptr;
980 Bool_t UseRanges = kTRUE;
982 if (iter->HasIndexes() && (iter->IndexesSize() == iter->ScalesSize()) && iter->HasWidths()) {
983 histo = iter->CreateHistogram(ResName, kFALSE, !IsModel);
986 ndim = histo->GetDimension();
987 UseRanges = Models.GetLast() >= 0;
990 gr = iter->CreateGraph(ResName, kTRUE, !IsModel);
994 if ((histo || gr) && IsModel) {
995 TArrayD Ampls(Models.GetLast() + 1);
997 for (Int_t n = 0; n <= Models.GetLast(); n++) {
1003 if (iter->Reset(UseRanges))
1006 for (Int_t n = 0; n <= Models.GetLast(); n++) {
1012 case 1: histo->SetBinContent(iter->Indexes()[0] + 1, zn);
break;
1013 case 2: histo->SetBinContent(iter->Indexes()[0] + 1, iter->Indexes()[1] + 1, zn);
break;
1015 histo->SetBinContent(iter->Indexes()[0] + 1, iter->Indexes()[1] + 1, iter->Indexes()[2] + 1, zn);
1019 (gr->GetX())[iter->Point()] = iter->x();
1020 (gr->GetY())[iter->Point()] = zn;
1023 }
while (iter->Next(UseRanges));
1025 for (Int_t n = 0; n <= Models.GetLast(); n++) {
1032 for (Int_t n1 = 0; n1 < gr->GetN() - 1; n1++)
1033 for (Int_t n2 = n1 + 1; n2 < gr->GetN(); n2++)
1034 if ((gr->GetX())[n1] > (gr->GetX())[n2]) {
1035 Double_t xx = (gr->GetX())[n1];
1036 (gr->GetX())[n1] = (gr->GetX())[n2];
1037 (gr->GetX())[n2] = xx;
1038 Double_t yy = (gr->GetY())[n1];
1039 (gr->GetY())[n1] = (gr->GetY())[n2];
1040 (gr->GetY())[n2] = yy;
1043 TNamed *res =
nullptr;
1052 title.Form(
"Draw of model %s", ModelName);
1054 title.Form(
"Draw of full model of %s", DataName);
1056 title.Form(
"Draw of data %s", DataName);
1057 res->SetTitle(title.Data());
1078 TString opt(option);
1080 TCanvas *fCanvas =
nullptr;
1082 if ((opt.Length() > 0) && (opt[0] ==
'#')) {
1087 CanvasName =
"Canvas";
1089 }
while (gROOT->FindObject(CanvasName.Data()));
1090 fCanvas =
new TCanvas(CanvasName, TString(
"Draw of fitter ") + GetName() +
" " + opt, 3);
1094 Bool_t drawdata = kFALSE, drawcomp = kFALSE, drawmodel = kFALSE;
1096 TObjArray selectmodels;
1104 while (opt.Length() > 0) {
1105 Int_t len = opt.Index(
",", 0, TString::kExact);
1112 TString optpart(opt.Data(), len);
1113 while ((optpart.Length() > 0) && (optpart[0] ==
' '))
1114 optpart.Remove(0, 1);
1116 Bool_t find = kFALSE;
1119 if (optpart.Index(
GetDataName(n), 0, TString::kExact) == 0) {
1127 else if (optpart ==
"-")
1133 selectmodels.Add(
new TObjString(optpart));
1135 if (model && !selectdata)
1142 if (opt.Length() > 0)
1146 if (!selectdata && !drawdata)
1153 for (Int_t nn = 0; nn <
GetNumData(); nn++) {
1155 if (selectdata && (data != selectdata))
1159 TObject *obj =
CreateDrawObject(TString(data->GetName()) +
"_bins", data->GetName(), kFALSE);
1161 TAttLine *line =
dynamic_cast<TAttLine *
>(obj);
1163 line->SetLineColor(1);
1164 line->SetLineWidth(1);
1171 TObject *mobj =
CreateDrawObject(TString(data->GetName()) +
"_fullmodel", data->GetName(), kTRUE);
1173 TAttLine *line =
dynamic_cast<TAttLine *
>(mobj);
1175 line->SetLineColor(4);
1176 line->SetLineWidth(2);
1183 for (Int_t nmodel = 0; nmodel <
GetNumModel(); nmodel++) {
1188 TObject *cobj =
CreateDrawObject(TString(data->GetName()) +
"_" + model->GetName(), data->GetName(), kTRUE,
1191 TAttLine *line =
dynamic_cast<TAttLine *
>(cobj);
1193 line->SetLineColor(6);
1194 line->SetLineWidth(1);
1200 for (Int_t n = 0; n <= selectmodels.GetLast(); n++) {
1201 TString name = ((TObjString *)(selectmodels[n]))->String();
1203 TObject *cobj =
CreateDrawObject(TString(data->GetName()) +
"_" + name, data->GetName(), kTRUE, name.Data());
1205 TAttLine *line =
dynamic_cast<TAttLine *
>(cobj);
1207 line->SetLineColor(6);
1208 line->SetLineWidth(1);
1218 Bool_t allhisto = kTRUE;
1219 for (Int_t n = 0; n <=
fxDrawObjs->GetLast(); n++)
1220 if (!(
fxDrawObjs->At(n)->InheritsFrom(TH1::Class())))
1224 new THStack(TString(
"Stack") +
"_" +
fxDrawObjs->At(0)->GetName(),
fxDrawObjs->At(0)->GetName());
1225 for (Int_t n = 0; n <=
fxDrawObjs->GetLast(); n++)
1229 stack->Draw(
"nostack");
1231 for (Int_t n = 0; n <=
fxDrawObjs->GetLast(); n++)
1244 std::cout << std::endl <<
"*** LIST OF AMPLITUDES VALUE ***" << std::endl;
1255 std::cout << std::endl <<
" *** LIST OF LINES PARAMETERS ***" << std::endl;
1263 for (
int naxis = 0; naxis < 3; naxis++)
1268 std::cout << std::setw(10) <<
"Name" << std::setw(12) <<
"Ampl";
1269 for (Int_t naxis = 0; naxis <= MaxAxis; naxis++)
1270 std::cout << std::setw(11) <<
"Pos" << naxis << std::setw(11) <<
"Width" << naxis;
1271 std::cout << std::endl;
1277 std::cout << std::setw(10) << m->GetName() << std::setw(12) << m->
GetAmplValue();
1279 for (
int naxis = 0; naxis <= MaxAxis; naxis++) {
1280 Double_t pos, width;
1281 std::cout << std::setw(12);
1286 std::cout << std::setw(12);
1292 std::cout << std::endl;
1308 for (Int_t n = 0; n <=
fxDrawObjs->GetLast(); n++)
1320 name.Form(
"%s%d", Head, n++);
1321 }
while (isModel ?
FindModel(name.Data()) !=
nullptr :
FindData(name.Data()) !=
nullptr);
1325 void TGo4Fitter::Streamer(TBuffer &b)
1327 if (b.IsReading()) {
1328 TGo4Fitter::Class()->ReadBuffer(b,
this);
1334 TGo4Fitter::Class()->WriteBuffer(b,
this);
void CollectParsTo(TGo4FitParsList &list) override
friend class TGo4FitAmplEstimation
Double_t CalculatesIntegral(const char *DataName, const char *ModelName=nullptr, Bool_t OnlyCounts=kFALSE)
TGo4FitDataGraph * AddGraph(const char *DataName, TGraph *gr, Bool_t Owned=kFALSE, Double_t lrange=0., Double_t rrange=0.)
void RebuildAll(Bool_t ForceBuild=kFALSE)
void SetMemoryUsage(Int_t iMemoryUsage)
void Print(Option_t *option="") const override
void SetHistogram(TH1 *iHistogram, Bool_t iHistogramOwned=kFALSE)
TGo4FitSlot * GetSlot(Int_t nslot)
void PrintResults() const
TGo4FitComponent * GetComp(Int_t n)
void MoveDrawObjectsToROOT()
void SetFitFunctionType(Int_t iFitFunctionType)
void AddAmplEstimation(Int_t NumIters=1)
const char * GetAmplFullName()
Double_t * GetBinsDevs() const
void EstimateAmplitudes(Int_t NumIters=1)
TGo4FitModel * FindModel(const char *ModelName)
void ClearModelAssignmentTo(const char *ModelName, const char *DataName=nullptr)
Double_t CalculateFitFunction(Double_t *pars=nullptr, Int_t FitFunctionType=-1, const char *DataName=nullptr)
void AddPolynoms(const char *DataName, const char *NamePrefix, Int_t MaxOrder=1, Int_t NumAxis=1, Int_t GroupIndex=0)
void ClearConnectionToSlot()
void AssignToData(const char *DataName, Double_t RatioValue=1., Bool_t FixRatio=kFALSE)
Bool_t InitFitterData() override
Int_t NumModelsAssosiatedTo(const char *DataName)
void AddAction(TGo4FitterAction *Action)
Double_t * GetDataBinsDevs(TGo4FitData *data) const
Double_t * GetBinsResult() const
void CheckSlotsBeforeDelete(TGo4FitComponent *comp)
Double_t DoCalculation() override
void AddPolynomX(const char *DataName, const char *NamePrefix, Int_t MaxOrder=1, Int_t GroupIndex=0, Double_t lrange=0., Double_t rrange=0.)
void DoAction(TGo4FitterAbstract *Fitter) override
virtual Bool_t Initialize(Int_t UseBuffers=-1)
void ClearAssignmentTo(const char *DataName)
Double_t PointFitFunction(Int_t FitFunctionType, Double_t value, Double_t modelvalue, Double_t standdev)
void AssignModelTo(const char *ModelName, const char *DataName, Double_t RatioValue=1., Bool_t FixRatio=kFALSE)
void ClearSlot(TGo4FitSlot *slot, Bool_t NonOwned)
void SetAmplValue(Double_t iAmpl)
const char * AssignmentName(Int_t n)
void SetOwner(TNamed *iOwner)
TGo4FitData * RemoveData(const char *DataName, Bool_t IsDel=kFALSE)
Double_t * GetDataBinsResult(TGo4FitData *data) const
Int_t GetDataBinsSize(TGo4FitData *data) const
TGo4FitModel * RemoveModel(const char *ModelName, Bool_t IsDel=kFALSE)
TUserFitFunction fxUserFitFunction
void CollectAllPars() override
Bool_t IsAssignTo(const char *DataName) const
virtual Double_t EvaluateAtPoint(TGo4FitData *data, Int_t nbin, Bool_t UseRanges=kTRUE)
void DeleteModelsAssosiatedTo(const char *DataName)
void PrepareSlotsForWriting()
TGo4FitData * AddData(TGo4FitData *d)
virtual Bool_t GetPosition(Int_t naxis, Double_t &pos)
void ChangeDataNameInAssignments(const char *oldname, const char *newname)
void SetGraph(TGraph *iGraph, Bool_t iGraphOwned=kFALSE)
Double_t * GetDataBinsValues(TGo4FitData *data) const
void FillSlotList(TSeqCollection *) override
TGo4FitSlot * GetConnectedSlot() const
void CheckDuplicatesOnSlot()
TGo4FitModelGauss1 * AddGauss1(const char *DataName, const char *ModelName, Double_t iPosition, Double_t iWidth, Double_t iAmpl=1., Int_t Axis=0)
void ChangeDataNameInAssignments(const char *oldname, const char *newname)
void SetRange(Int_t naxis, Double_t min, Double_t max)
virtual void CollectAllPars()
Double_t CalculateFCN(Int_t FitFunctionType, TGo4FitData *selectdata=nullptr)
TGo4FitDataGraph * SetGraph(const char *DataName, TGraph *gr, Bool_t Owned=kFALSE)
void FinalizeFitterData() override
TGo4FitModel * GetModel(Int_t n)
Double_t CalculatesModelIntegral(const char *ModelName, Bool_t OnlyCounts=kFALSE)
Int_t GetGroupIndex() const
Int_t DoNDFCalculation() override
virtual Bool_t Initialize(Int_t UseBuffers=-1)
virtual Bool_t BeforeEval(Int_t ndim)
void ConnectToDataIfAssigned(TGo4FitData *data)
TString FindNextName(const char *Head, Int_t start, Bool_t isModel=kTRUE)
Double_t * GetBinsValues() const
virtual void FillSlotList(TSeqCollection *lst)
Int_t GetNumModel() const
TGo4FitData * GetData(Int_t n)
void SetGroupIndex(Int_t index=-1)
virtual Bool_t GetWidth(Int_t naxis, Double_t &width)
TObject * CreateDrawObject(const char *ObjName, const char *DataName, Bool_t IsModel=kFALSE, const char *ModelName=nullptr)
virtual std::unique_ptr< TGo4FitDataIter > MakeIter()
Int_t NumAssigments() const
Int_t GetFitFunctionType() const
Bool_t DataBuffersAllocated(TGo4FitData *data)
Bool_t BuffersAllocated() const
const char * GetDataName(Int_t n)
Bool_t BuffersAllocated() const
TGo4FitModel * AddModel(TGo4FitModel *m)
TGo4FitModel * CloneModel(const char *ModelName, const char *NewName=nullptr)
Double_t * GetModelBinsValues(TGo4FitModel *model, const char *DataName) const
void RebuildShape(Bool_t ForceBuild=kFALSE)
void Clear(Option_t *option="") override
TGo4FitDataHistogram * SetH1(const char *DataName, TH1 *histo, Bool_t Owned=kFALSE)
Bool_t CalculatesMomentums(const char *DataName, Bool_t UseRanges, Bool_t SubstractModels, Double_t &first, Double_t &second)
Double_t * GetModelBins(const char *DataName) const
TGo4FitData * FindData(const char *DataName)
Bool_t AddModelToDataResult(TGo4FitData *data)
Int_t CalculateNDF(const char *DataName=nullptr)
Double_t GetRatioValueFor(const char *DataName)
void ProvideLastDrawObjects(TObjArray &lst)
void Draw(Option_t *option) override
void Clear(Option_t *option="") override
Bool_t ModelBuffersAllocated(TGo4FitModel *model)
Int_t GetMemoryUsage() const
Int_t GetBinsSize() const
void AddStandardActions()
TGo4FitParameter * GetAmplPar()
void ExecuteDependencies(Double_t *pars)
void SetParsValues(Double_t *pars)
TGo4FitDataHistogram * AddH1(const char *DataName, TH1 *histo, Bool_t Owned=kFALSE, Double_t lrange=0., Double_t rrange=0.)
void Print(Option_t *option="") const override