18 #include "Riostream.h"
24 #include "TObjArray.h"
25 #include "TObjString.h"
43 fxDatas(), fxModels(), fiFitFunctionType(0), fiMemoryUsage(0),
44 fxUserFitFunction(0), fxDrawObjs(0)
50 fxDatas(), fxModels(), fiFitFunctionType(0), fiMemoryUsage(0), fxUserFitFunction(0), fxDrawObjs(0)
58 fxDatas(), fxModels(), fiFitFunctionType(0), fiMemoryUsage(100), fxUserFitFunction(0), fxDrawObjs(0)
105 return (n>=0) && (n<GetNumData()) ? dynamic_cast<TGo4FitData*> (
fxDatas[n]) : 0;
129 if ((lrange<rrange) || (rrange!=0.)) data->
SetRange(0,lrange,rrange);
145 if ((lrange<rrange) || (rrange!=0.)) data->
SetRange(0,lrange,rrange);
153 if (data!=0) data->
SetGraph(gr, Owned);
160 for (Int_t n=0;n<comp->
NumSlots();n++) {
163 for(Int_t n2=0;n2<
NumSlots();n2++) {
195 return (n>=0) && (n<GetNumModel()) ? dynamic_cast<TGo4FitModel*> (
fxModels[n]) : 0;
225 void TGo4Fitter::AddPolynomX(
const char* DataName,
const char* NamePrefix, Int_t MaxOrder, Int_t GroupIndex, Double_t lrange, Double_t rrange)
227 if (DataName==0)
return;
229 Bool_t flag = kFALSE;
231 Bool_t createmodel = kFALSE;
234 TString Prefix(NamePrefix);
235 if (NumTry>0) Prefix+=NumTry;
237 Bool_t findsame = kFALSE;
239 for(Int_t Order=0; Order<=MaxOrder; Order++) {
240 TString Name(Prefix);
244 if (findsame && !createmodel)
break;
249 if ((lrange<rrange) || (rrange!=0.)) comp->
SetRange(0,lrange,rrange);
255 if (findsame) { NumTry++; createmodel = kFALSE; }
else
256 if (createmodel) flag = kFALSE;
257 else createmodel = kTRUE;
264 if (DataName==0)
return;
266 Bool_t flag = kFALSE;
268 Bool_t createmodel = kFALSE;
271 Bool_t findsame = kFALSE;
273 for (Int_t n=0;n<Coef.GetSize();n++) {
274 TString Name(NamePrefix);
275 if (NumTry>0) Name+=NumTry;
279 if (findsame && !createmodel)
break;
290 if (findsame) { NumTry++; createmodel = kFALSE; }
else
291 if (createmodel) flag = kFALSE;
292 else createmodel = kTRUE;
299 if (DataName==0)
return;
300 TArrayD Orders(NumAxis);
303 Bool_t flag = kFALSE;
305 Bool_t createmodel = kFALSE;
309 TString Prefix(NamePrefix);
310 if (NumTry>0) Prefix+=NumTry;
312 Bool_t findsame = kFALSE;
315 TString Name(Prefix);
316 for(Int_t n=0; n<NumAxis; n++) {
318 Name+=Int_t(Orders[n]);
321 if (findsame && !createmodel)
break;
332 if (Orders[nn]<=MaxOrder)
break;
335 }
while (nn<NumAxis);
340 if (findsame) { NumTry++; createmodel = kFALSE; }
else
341 if (createmodel) flag = kFALSE;
342 else createmodel = kTRUE;
358 if (mod==0)
return 0;
364 newname = NewName ? NewName : mod->GetName();
442 if (model==0)
return;
456 Int_t dbuf = -1, mbuf = -1;
458 case 0: dbuf = 0; mbuf = 0;
break;
459 case 1: dbuf = 1; mbuf = 0;
break;
460 case 2: dbuf = 1; mbuf = 1;
break;
461 default: dbuf = -1; mbuf = -1;
477 std::cout <<
" User fit function not set. Switch to least squares " << std::endl;
493 switch (FitFunctionType) {
495 Double_t zn1 = (value-modelvalue);
498 if (standdev<=0.)
return 0.;
499 Double_t zn2 = (value-modelvalue);
500 return zn2*zn2/standdev; }
502 if (modelvalue<=0.)
return 0.;
503 Double_t zn3 = (value-modelvalue);
504 return zn3*zn3/modelvalue; }
506 Double_t zn4 = (value-modelvalue);
507 return zn4*zn4/((value<1.) ? 1. : value); }
509 if (value<0.)
return 0.;
510 Double_t zn5 = (value+((value<1.) ? 0. : 1.)-modelvalue);
511 return zn5*zn5/(value+1.); }
513 if (modelvalue<=0.)
return 0.;
514 return modelvalue - value*TMath::Log(modelvalue);
519 return (value-modelvalue)*(value-modelvalue);
531 if (selectdata && (dat!=selectdata))
continue;
540 for(Int_t nbin=0;nbin<size;nbin++)
541 fSum +=
PointFitFunction(FitFunctionType, DataAmpl*values[nbin], res[nbin], DataAmpl*DataAmpl*devs[nbin] );
545 if (!iter->
Reset()) {
delete iter;
continue; }
559 Double_t value = DataAmpl * iter->
Value();
562 Double_t modelvalue = 0.;
563 for(Int_t nm=0;nm<=Models.GetLast();nm++) {
570 }
while (iter->
Next());
572 for(Int_t nm=0;nm<=Models.GetLast();nm++)
638 for (Int_t nbin=0;nbin<size;nbin++) result[nbin] = 0.;
710 if (Opt==
"Ampls") {
PrintAmpls();
return; }
else
711 if (Opt==
"Pars") {
PrintPars();
return; }
else
716 std::cout <<
"Fitiing function type: ";
718 case ff_chi_square : std::cout <<
"ff_chi_square" << std::endl;
break;
719 case ff_chi_Pearson : std::cout <<
"ff_chi_Pearson" << std::endl;
break;
720 case ff_chi_Neyman : std::cout <<
"ff_chi_Neyman" << std::endl;
break;
721 case ff_chi_gamma : std::cout <<
"ff_chi_gamma" << std::endl;
break;
722 case ff_ML_Poisson : std::cout <<
"ff_ML_Poisson" << std::endl;
break;
723 case ff_user : std::cout <<
"user defined" << std::endl;
break;
724 default: std::cout <<
"ff_least_squares" << std::endl;
726 std::cout << std::endl <<
" LIST OF DATA OBJECTS" << std::endl;
728 std::cout << std::endl <<
" LIST OF MODEL OBJECTS" << std::endl;
735 if (data==0)
return kFALSE;
738 if (iter==0)
return kFALSE;
741 if (size==0) {
delete iter;
return kFALSE; }
749 TArrayD Ampls(Models.GetLast()+1);
750 for(Int_t n=0;n<=Models.GetLast();n++) {
756 TArrayD bins(size), scales(size);
760 if (iter->
Reset(UseRanges))
do {
761 Double_t value = iter->
Value();
762 for(Int_t n=0;n<=Models.GetLast();n++) {
766 value = TMath::Abs(value);
768 scales[pnt] = iter->
x();
770 }
while (iter->
Next(UseRanges));
774 for(Int_t n=0;n<=Models.GetLast();n++) {
785 for (Int_t pnt=0;pnt<size;pnt++)
786 if ((bins[pnt]>0.) && ((niter==0) || (TMath::Abs(scales[pnt]-first)<second*2.))) {
788 sum11 += bins[pnt]*scales[pnt];
789 sum22 += bins[pnt]*scales[pnt]*scales[pnt];
793 Double_t mid = sum11/sum00;
794 Double_t dev = TMath::Sqrt(sum22/sum00-mid*mid);
797 if ((dev/second>0.8) && (dev/second<1.2)) niter=10;
799 first = mid; second = dev;
811 if (data==0)
return 0.;
814 if ((ModelName!=0) && (model==0))
return 0.;
817 if (iter==0)
return 0.;
822 if (!iter->
Reset(kTRUE)) {
833 double dx = onlycounts ? 1. : iter->
xWidths();
835 sum += ampl*value*dx;
836 }
while (iter->
Next(kTRUE));
849 if (model==0)
return 0.;
856 if (data==0)
return 0;
861 if (model) Models.Add(model);
else {
862 Int_t groupindex = -1;
865 TString modelname(ModelName);
866 if (modelname==
"Background") groupindex = 0;
else
867 if (modelname.Index(
"Group",0,TString::kExact)==0) {
868 modelname.Remove(0,5);
870 groupindex = strtol(modelname.Data(),&err,10);
871 if (err && (*err!=0)) groupindex=-1;
882 if (Models.GetLast()<0)
return 0;
886 if (iter==0)
return 0;
888 if (!iter->
Reset(kFALSE)) {
delete iter;
return 0; }
893 Bool_t UseRanges = kTRUE;
899 ndim = histo->GetDimension();
900 UseRanges = Models.GetLast() >= 0;
907 if (((histo!=0) || (gr!=0)) && IsModel) {
908 TArrayD Ampls(Models.GetLast()+1);
910 for(Int_t n=0;n<=Models.GetLast();n++) {
916 if (iter->
Reset(UseRanges))
do {
918 for(Int_t n=0;n<=Models.GetLast();n++) {
924 case 1: histo->SetBinContent(iter->
Indexes()[0]+1,zn);
break;
925 case 2: histo->SetBinContent(iter->
Indexes()[0]+1,iter->
Indexes()[1]+1,zn);
break;
926 case 3: histo->SetBinContent(iter->
Indexes()[0]+1,iter->
Indexes()[1]+1,iter->
Indexes()[2]+1,zn);
break;
929 (gr->GetX())[iter->
Point()] = iter->
x();
930 (gr->GetY())[iter->
Point()] = zn;
933 }
while (iter->
Next(UseRanges));
935 for(Int_t n=0;n<=Models.GetLast();n++) {
944 for(Int_t n1=0;n1<gr->GetN()-1;n1++)
945 for(Int_t n2=n1+1;n2<gr->GetN();n2++)
946 if ((gr->GetX())[n1]>(gr->GetX())[n2]) {
947 Double_t xx = (gr->GetX())[n1];
948 (gr->GetX())[n1] = (gr->GetX())[n2];
949 (gr->GetX())[n2] = xx;
950 Double_t yy = (gr->GetY())[n1];
951 (gr->GetY())[n1] = (gr->GetY())[n2];
952 (gr->GetY())[n2] = yy;
956 if (histo) res = histo;
else res = gr;
960 if (ModelName) { title =
"Draw of model "; title+=ModelName; }
961 else { title =
"Draw of full model of "; title+=DataName; }
962 else { title =
"Draw of data "; title+=DataName; }
963 res->SetTitle(title.Data());
987 TCanvas *fCanvas = 0;
989 if ((opt.Length()>0) && (opt[0]==
'#')) {
994 CanvasName =
"Canvas";
996 }
while (gROOT->FindObject(CanvasName.Data()));
997 fCanvas =
new TCanvas(CanvasName,TString(
"Draw of fitter ")+GetName()+
" "+opt,3);
1001 Bool_t drawdata = kFALSE;
1003 TObjArray selectmodels;
1005 Bool_t drawcomp = kFALSE;
1006 Bool_t drawmodel = kFALSE;
1008 if (opt==
"*") { opt =
""; drawdata = kTRUE; }
1010 while (opt.Length()>0) {
1011 Int_t len = opt.Index(
",",0,TString::kExact);
1013 if (len<0) len = opt.Length();
1016 TString optpart(opt.Data(), len);
1017 while ((optpart.Length()>0) && (optpart[0]==
' ')) optpart.Remove(0,1);
1019 Bool_t find = kFALSE;
1022 if (optpart.Index(
GetDataName(n),0,TString::kExact)==0) {
1028 if (optpart==
"*") drawcomp = kTRUE;
else
1029 if (optpart==
"-") drawmodel = kFALSE;
1034 selectmodels.Add(
new TObjString(optpart));
1036 if (model && (selectdata==0))
1043 if (opt.Length()>0) opt.Remove(0,1);
1046 if ((selectdata==0) && !drawdata)
1054 if (selectdata && (data!=selectdata))
continue;
1057 TObject* obj =
CreateDrawObject(TString(data->GetName())+
"_bins", data->GetName(), kFALSE);
1059 TAttLine* line =
dynamic_cast<TAttLine*
> (obj);
1061 line->SetLineColor(1);
1062 line->SetLineWidth(1);
1068 TObject* mobj =
CreateDrawObject(TString(data->GetName())+
"_fullmodel", data->GetName(), kTRUE);
1070 TAttLine* line =
dynamic_cast<TAttLine*
> (mobj);
1072 line->SetLineColor(4);
1073 line->SetLineWidth(2);
1079 for(Int_t nmodel=0;nmodel<
GetNumModel();nmodel++) {
1081 if ( !model->
IsAssignTo(data->GetName()))
continue;
1083 TObject* cobj =
CreateDrawObject(TString(data->GetName())+
"_"+model->GetName(), data->GetName(), kTRUE, model->GetName());
1085 TAttLine* line =
dynamic_cast<TAttLine*
> (cobj);
1087 line->SetLineColor(6);
1088 line->SetLineWidth(1);
1093 for (Int_t n=0;n<=selectmodels.GetLast();n++) {
1094 TString name = ((TObjString*) (selectmodels[n]))->String();
1096 TObject* cobj =
CreateDrawObject(TString(data->GetName())+
"_" +name, data->GetName(), kTRUE, name.Data());
1098 TAttLine* line =
dynamic_cast<TAttLine*
> (cobj);
1100 line->SetLineColor(6);
1101 line->SetLineWidth(1);
1109 Bool_t allhisto = kTRUE;
1111 if (!(
fxDrawObjs->At(n)->InheritsFrom(TH1::Class()))) allhisto = kFALSE;
1113 THStack* stack =
new THStack(TString(
"Stack")+
"_"+
fxDrawObjs->At(0)->GetName(),
fxDrawObjs->At(0)->GetName());
1117 stack->Draw(
"nostack");
1124 if (fCanvas!=0) fCanvas->Update();
1129 std::cout << std::endl <<
"*** LIST OF AMPLITUDES VALUE ***" << std::endl;
1139 std::cout << std::endl <<
" *** LIST OF LINES PARAMETERS ***" << std::endl;
1146 for (
int naxis=0;naxis<3;naxis++)
1150 std::cout << std::setw(10) <<
"Name" << std::setw(12) <<
"Ampl";
1151 for(Int_t naxis=0;naxis<=MaxAxis;naxis++)
1152 std::cout << std::setw(11) <<
"Pos" << naxis << std::setw(11) <<
"Width" << naxis;
1153 std::cout << std::endl;
1158 std::cout << std::setw(10) << m->GetName() << std::setw(12) << m->
GetAmplValue();
1160 for (
int naxis=0;naxis<=MaxAxis;naxis++) {
1161 Double_t pos, width;
1162 std::cout << std::setw(12);
1164 else std::cout <<
"---";
1165 std::cout << std::setw(12);
1166 if (m->
GetWidth(naxis,width)) std::cout << width;
1167 else std::cout <<
"---";
1169 std::cout << std::endl;
1198 name.Form(
"%s%d", Head, n++);
1203 void TGo4Fitter::Streamer(TBuffer& b)
1205 if (b.IsReading()) {
1206 TGo4Fitter::Class()->ReadBuffer(b,
this);
1212 TGo4Fitter::Class()->WriteBuffer(b,
this);
virtual void DoAction(TGo4FitterAbstract *Fitter)
TH1 * CreateHistogram(const char *HistoName, Bool_t UseRanges=kFALSE, Bool_t SetBins=kFALSE)
virtual void Print(Option_t *option) const
friend class TGo4FitAmplEstimation
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)
virtual Double_t DoCalculation()
virtual void Clear(Option_t *option="")
void SetMemoryUsage(Int_t iMemoryUsage)
void SetHistogram(TH1 *iHistogram, Bool_t iHistogramOwned=kFALSE)
virtual Int_t DoNDFCalculation()
TGo4FitSlot * GetSlot(Int_t nslot)
TGo4FitComponent * GetComp(Int_t n)
Double_t * GetBinsValues()
void MoveDrawObjectsToROOT()
void SetFitFunctionType(Int_t iFitFunctionType)
virtual void Draw(Option_t *option)
void AddAmplEstimation(Int_t NumIters=1)
void ClearModelAssignmentTo(const char *ModelName, const char *DataName=0)
virtual void CollectAllPars()
const char * GetAmplFullName()
virtual TGo4FitDataIter * MakeIter()
virtual void FinalizeFitterData()
Int_t GetDataBinsSize(TGo4FitData *data)
void EstimateAmplitudes(Int_t NumIters=1)
TGo4FitModel * FindModel(const char *ModelName)
Int_t GetGroupIndex() const
void AddPolynoms(const char *DataName, const char *NamePrefix, Int_t MaxOrder=1, Int_t NumAxis=1, Int_t GroupIndex=0)
void ClearConnectionToSlot()
Double_t * GetDataBinsResult(TGo4FitData *data)
void AssignToData(const char *DataName, Double_t RatioValue=1., Bool_t FixRatio=kFALSE)
Double_t * GetModelBins(const char *DataName) const
Int_t NumModelsAssosiatedTo(const char *DataName)
void AddAction(TGo4FitterAction *Action)
void CheckSlotsBeforeDelete(TGo4FitComponent *comp)
TGraph * CreateGraph(const char *GraphName, Bool_t UseRanges=kFALSE, Bool_t SetBins=kFALSE)
void AddPolynomX(const char *DataName, const char *NamePrefix, Int_t MaxOrder=1, Int_t GroupIndex=0, Double_t lrange=0., Double_t rrange=0.)
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)
TGo4FitModel * RemoveModel(const char *ModelName, Bool_t IsDel=kFALSE)
TUserFitFunction fxUserFitFunction
Double_t * GetDataBinsDevs(TGo4FitData *data)
virtual Bool_t Reset(Bool_t UseRanges=kTRUE)
virtual Double_t EvaluateAtPoint(TGo4FitData *data, Int_t nbin, Bool_t UseRanges=kTRUE)
void DeleteModelsAssosiatedTo(const char *DataName)
virtual void FillSlotList(TSeqCollection *list)
Double_t CalculatesIntegral(const char *DataName, const char *ModelName=0, Bool_t OnlyCounts=kFALSE)
void PrepareSlotsForWriting()
TGo4FitData * AddData(TGo4FitData *d)
virtual Bool_t GetPosition(Int_t naxis, Double_t &pos)
virtual void Print(Option_t *option) const
Double_t * GetBinsResult()
void ChangeDataNameInAssignments(const char *oldname, const char *newname)
void SetGraph(TGraph *iGraph, Bool_t iGraphOwned=kFALSE)
Bool_t BuffersAllocated() const
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()
Int_t CalculateNDF(const char *DataName=0)
TGo4FitDataGraph * SetGraph(const char *DataName, TGraph *gr, Bool_t Owned=kFALSE)
TGo4FitModel * GetModel(Int_t n)
Double_t CalculatesModelIntegral(const char *ModelName, Bool_t OnlyCounts=kFALSE)
void PrintResults() const
Int_t CountPoints(Bool_t UseRanges=kTRUE)
virtual Bool_t Initialize(Int_t UseBuffers=-1)
Int_t GetBinsSize() const
virtual Bool_t Next(Bool_t UseRanges=kTRUE)
virtual Bool_t BeforeEval(Int_t ndim)
void ConnectToDataIfAssigned(TGo4FitData *data)
TString FindNextName(const char *Head, Int_t start, Bool_t isModel=kTRUE)
virtual void FillSlotList(TSeqCollection *lst)
TGo4FitData * GetData(Int_t n)
void SetGroupIndex(Int_t index=-1)
virtual Bool_t GetWidth(Int_t naxis, Double_t &width)
Int_t NumAssigments() const
Int_t GetFitFunctionType()
Double_t * GetModelBinsValues(TGo4FitModel *model, const char *DataName)
Double_t CalculateFitFunction(Double_t *pars=0, Int_t FitFunctionType=-1, const char *DataName=0)
virtual void CollectParsTo(TGo4FitParsList &list)
Bool_t DataBuffersAllocated(TGo4FitData *data)
Bool_t HasIndexes() const
TGo4FitModel * CloneModel(const char *ModelName, const char *NewName=0)
const char * GetDataName(Int_t n)
Bool_t BuffersAllocated() const
Int_t GetNumModel() const
TGo4FitModel * AddModel(TGo4FitModel *m)
const Int_t * Indexes() const
TObject * CreateDrawObject(const char *ObjName, const char *DataName, Bool_t IsModel=kFALSE, const char *ModelName=0)
virtual Bool_t InitFitterData()
void RebuildShape(Bool_t ForceBuild=kFALSE)
Bool_t IsAssignTo(const char *DataName) const
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 * GetDataBinsValues(TGo4FitData *data)
virtual void Clear(Option_t *option=0)
TGo4FitData * FindData(const char *DataName)
Double_t CalculateFCN(Int_t FitFunctionType, TGo4FitData *selectdata=0)
Bool_t AddModelToDataResult(TGo4FitData *data)
Int_t IndexesSize() const
Double_t StandardDeviation() const
Double_t GetRatioValueFor(const char *DataName)
void ProvideLastDrawObjects(TObjArray &lst)
Bool_t ModelBuffersAllocated(TGo4FitModel *model)
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.)