#include "Riostream.h"
#include "TROOT.h"
#include "TApplication.h"
#include "TCanvas.h"
#include "TH1.h"
#include "TSystem.h"
#include "TBrowser.h"
#include "TFile.h"
#include "TRandom.h"
#include "TMultiDimFit.h"
#include "TVectorD.h"
#include "TMath.h"
void makeData(Double_t* x, Double_t& d, Double_t& e)
{
Double_t upp[5] = { 10, 10, 10, 10, 1 };
Double_t low[5] = { 0, 0, 0, 0, .1 };
for (int i = 0; i < 4; i++)
x[i] = (upp[i] - low[i]) * gRandom->Rndm() + low[i];
d = x[0] * TMath::Sqrt(x[1] * x[1] + x[2] * x[2] + x[3] * x[3]);
e = gRandom->Gaus(upp[4],low[4]);
}
int CompareResults(TMultiDimFit *fit)
{
double GoodCoeffs[] = {
-4.37056,
43.1468,
13.432,
13.4632,
13.3964,
13.328,
13.3016,
13.3519,
4.49724,
4.63876,
4.89036,
-3.69982,
-3.98618,
-3.86195,
4.36054,
-4.02597,
4.57037,
4.69845,
2.83819,
-3.48855,
-3.97612
};
int GoodPower[] = {
1, 1, 1, 1,
2, 1, 1, 1,
1, 1, 1, 2,
1, 1, 2, 1,
1, 2, 1, 1,
2, 2, 1, 1,
2, 1, 1, 2,
2, 1, 2, 1,
1, 1, 1, 3,
1, 3, 1, 1,
1, 1, 5, 1,
1, 1, 2, 2,
1, 2, 1, 2,
1, 2, 2, 1,
2, 1, 1, 3,
2, 2, 1, 2,
2, 1, 3, 1,
2, 3, 1, 1,
1, 2, 2, 2,
2, 1, 2, 2,
2, 2, 2, 1
};
Int_t nc = fit->GetNCoefficients();
Int_t nv = fit->GetNVariables();
const Int_t *powers = fit->GetPowers();
const Int_t *pindex = fit->GetPowerIndex();
if (nc != 21) return 1;
const TVectorD *coeffs = fit->GetCoefficients();
int k = 0;
for (Int_t i=0;i<nc;i++) {
if (TMath::Abs((*coeffs)[i] - GoodCoeffs[i]) > 5e-5) return 2;
for (Int_t j=0;j<nv;j++) {
if (powers[pindex[i]*nv+j] != GoodPower[k]) return 3;
k++;
}
}
gROOT->ProcessLine(".L MDF.C");
Double_t x[] = {5,5,5,5};
Double_t refMDF = 43.98;
Double_t rMDF = MDF(x);
if (TMath::Abs(rMDF -refMDF) > 1e-2) return 4;
return 0;
}
Int_t multidimfit()
{
cout << "*************************************************" << endl;
cout << "* Multidimensional Fit *" << endl;
cout << "* *" << endl;
cout << "* By Christian Holm <cholm@nbi.dk> 14/10/00 *" << endl;
cout << "*************************************************" << endl;
cout << endl;
gRandom = new TRandom();
TFile* output = new TFile("mdf.root", "RECREATE");
Int_t nVars = 4;
Int_t nData = 500;
Double_t x[4];
TMultiDimFit* fit = new TMultiDimFit(nVars, TMultiDimFit::kMonomials,"v");
Int_t mPowers[] = { 6 , 6, 6, 6 };
fit->SetMaxPowers(mPowers);
fit->SetMaxFunctions(1000);
fit->SetMaxStudy(1000);
fit->SetMaxTerms(30);
fit->SetPowerLimit(1);
fit->SetMinAngle(10);
fit->SetMaxAngle(10);
fit->SetMinRelativeError(.01);
Double_t d;
Double_t e;
fit->Print("p");
Int_t i;
for (i = 0; i < nData ; i++) {
makeData(x,d,e);
fit->AddRow(x,d,e);
}
fit->Print("s");
fit->MakeHistograms();
fit->FindParameterization();
fit->Print("rc");
Double_t *xMax = new Double_t[nVars];
Double_t *xMin = new Double_t[nVars];
for (i = 0; i < nVars; i++) {
xMax[i] = (*fit->GetMaxVariables())(i);
xMin[i] = (*fit->GetMinVariables())(i);
}
nData = fit->GetNCoefficients() * 100;
Int_t j;
for (i = 0; i < nData ; i++) {
makeData(x,d,e);
for (j = 0; j < nVars; j++)
if (x[j] < xMin[j] || x[j] > xMax[j])
break;
if (j == nVars)
fit->AddTestRow(x,d,e);
else
i--;
}
fit->Fit();
fit->Print("fc");
fit->MakeCode();
output->Write();
output->Close();
delete output;
Int_t compare = CompareResults(fit);
if (!compare) {
printf("\nmultidimfit .............................................. OK\n");
} else {
printf("\nmultidimfit .............................................. fails case %d\n",compare);
}
delete fit;
return compare;
}
|
|