//GridTest(0.0001,-1.00501,0.0431441) // In new version of ROOT you need to comment out // the gSystem->Load("libMatrix"); // and symply enter it interactively before you do // .L JerryKahona.C #ifndef __CINT__ #include "TMatrixD.h" #include "TVectorD.h" gSystem->Load("libMatrix"); #endif // data arrays are global variables Int_t NumPoints=0; Float_t x[100],y[100],errory[100]; Double_t chiSq1,chiSq2,chiSq3; //______________________________________________________________________________ Double_t func(Double_t x,Double_t y,Double_t *par) { // Bevington Chapt 5 Fit function // Double_t value=( par[0]+par[1]*exp((-1.0)*x/par[3])+par[2]*exp((-1.0)*x/par[4])); //Temp-vs-Voltage Fit function Double_t value=( par[0]+par[1]*x); return value; } //______________________________________________________________________________ Double_t CalcChiSq(Int_t npar, Double_t *par, Int_t iflag) { Int_t i; //calculate chisquare Double_t chisq = 0; Double_t delta; for (i=0;i> x[NumPoints] >> y[NumPoints] ; errory[NumPoints]=0.05; } else { in >> x[NumPoints] >> y[NumPoints] ; if(y[NumPoints]) errory[NumPoints]=sqrt(y[NumPoints]); else errory[NumPoints]=y[NumPoints]; if(errory[NumPoints]==0) errory[NumPoints]=1.0; } // printf("%d\t%g\t%g\n",NumPoints,x[NumPoints],y[NumPoints]); NumPoints++; } in.close(); } void DataArrayPrint(void) { if(NumPoints) { printf("__X__\t__Y__\tEror Y\n"); for(Int_t i=0;iFill(Alpha[0],Alpha[1],ChiSqrd); } } // ChiMin->Draw(); ChiMin->Draw("Surf1"); } Double_t Chi2ParabMin(Double_t *x, Double_t *y, Int_t NumPnts) { // Find the minimum of a prabola using 3 points on the parabola //To find the parabola you choose 3 points and form a system // of 3-equations and 3 unkowns // using a*par[i]^2 + b*par[i] + c = \chi^2 //then solve the system of equations for a,b,c // by inverting the matrix // this will give you a,b,c but not the minima //you need to set derivative to zero for critical point // giving // X=-b/2/a = MIN gSystem->Load("libMatrix"); const Int_t nRowsCols = 3; TMatrixD AlphaMat(nRowsCols,nRowsCols); TMatrixD CorMat(nRowsCols,nRowsCols); Double_t Beta[5],Alpha[5]; for(Int_t i=0;i<5;i++) { // printf("x[%d]=%g\ty[%d]=%g\terrory[%d]=%g\n",i,x[i],i,y[i],i,errory[i]); } // Fill the Alpha Matrix // x[i] , y[i] , DeltaY[i] are already set from above Int_t j,k,m; for(m=0;m chiSq2) //started in wrong direction { delta = -delta; par[j] = par[j] + delta; save = chiSq2; //interchange 2 and 3 so 3 is lower chiSq2 = chiSq3; chiSq3 = save; } // -Increment or decrement a[j] until chi squared increases- do { chiSq1 = chiSq2; //move back to prepare for quad fit chiSq2 = chiSq3; par[j] = par[j] + delta; chiSq3 = CalcChiSq(npar, par, 0); // printf("Parameter Steps: par[%d]=%g chiSq3=%g\n",j,par[j],chiSq3); } while (chiSq3 < chiSq2); ParabolaPntsX[2]=par[j]; // value of parameter at chiSq3 for min ParabolaPntsY[2]=chiSq3; // par[j] and chiSq2 are estimates of the min chi^2 // lets find 2 points on each side of the parabola // which are more than 2*chiSq2 large than chiSq2 // so we can get a good parabola fit do { par[j]= par[j] + delta; chiSq3 = CalcChiSq(npar, par, 0); // printf("par[%d]=%g chiSq3=%g\n",j,par[j],chiSq3); } while (chiSq3 < 2*chiSq2); // printf("Right Side Parabola search: par[%d]=%g chiSq3=%g\n",j,par[j],chiSq3); ParabolaPntsX[3]=par[j]; // value of parameter at chiSq3 for Right Max ParabolaPntsY[3]=chiSq3; do { par[j]= par[j] + delta; chiSq3 = CalcChiSq(npar, par, 0); // printf("par[%d]=%g chiSq3=%g\n",j,par[j],chiSq3); } while (chiSq3 < 3.1*chiSq2); // printf("Right Side Parabola search: par[%d]=%g chiSq3=%g\n",j,par[j],chiSq3); ParabolaPntsX[4]=par[j]; // value of parameter at chiSq3 for Right Max ParabolaPntsY[4]=chiSq3; par[j]= ParabolaPntsX[2]; do { par[j]= par[j] - delta; chiSq1 = CalcChiSq(npar, par, 0); // printf("par[%d]=%g chiSq1=%g\n",j,par[j],chiSq1); } while (chiSq1 < 1.9*chiSq2); // printf("Left Side Parabola search: par[%d]=%g chiSq3=%g\n",j,par[j],chiSq3); ParabolaPntsX[1]=par[j]; // value of parameter at chiSq1 for Left Max ParabolaPntsY[1]=chiSq1; do { par[j]= par[j] - delta; chiSq1 = CalcChiSq(npar, par, 0); // printf("par[%d]=%g chiSq1=%g\n",j,par[j],chiSq1); } while (chiSq1 < 3.2*chiSq2); // printf("Left Side Parabola search: par[%d]=%g chiSq3=%g\n",j,par[j],chiSq3); ParabolaPntsX[0]=par[j]; // value of parameter at chiSq1 for Left Max ParabolaPntsY[0]=chiSq1; par[j]= ParabolaPntsX[2]; printf("The five Chi^2 points for fitting min are:\n"); for(i=0;iSetTitle("Counts -vs- Time"); gr->GetXaxis()->SetTitle("Time (s)"); gr->GetYaxis()->SetTitle("Counts"); */ gr->SetTitle("Temp -vs- Voltage"); gr->GetXaxis()->SetTitle("Temp (C)"); gr->GetYaxis()->SetTitle("Voltage(mV)"); gr->SetMarkerColor(4); gr->SetMarkerSize(0.3); gr->SetMarkerStyle(21); gr->Draw("AP"); // gr->Draw(); //Fit results for(Int_t i=0;iDraw("C"); } void GridTest(Double_t StepSize, Double_t Param1, Double_t Param2 ) { const Int_t NPar=2; Double_t Parameter[NPar],ParameterStepSize[NPar],InitChiSqr; // Parameter[0]=-1.00503; // Parameter[1]=0.043144; Parameter[0]=Param1; Parameter[1]=Param2; // Parameter[2]=140.0; // Parameter[3]=32.5; // Parameter[4]=193.; ParameterStepSize[0]=StepSize; ParameterStepSize[1]=StepSize; InitChiSqr=CalcChiSq(NPar, Parameter, 0); GridLS(NPar, Parameter, ParameterStepSize); printf("GridSearch Results:\n"); for(Int_t i=0;i