void fitterLike2() { //Setting style labstyle(); // Create the canvas TCanvas *c_histo = new TCanvas("c_histo","canvas histo",600,600); c_histo->cd(); //Create the histogram TH1F *histo = new TH1F("histo","",100,-4.,4.); // Event generation (Double Gaussian) Int_t i=0; for(i=0;i<7000;++i){ xvar[i]= randGen.Gaus(mA,sA); histo->Fill(xvar[i]); }; for(i=7000;i<10000;++i){ xvar[i]= randGen.Gaus(mB,sB); histo->Fill(xvar[i]); }; // Draw the histogram histo->GetXaxis()->SetTitle("R [cm] "); // Let us assume a lenght on x axis histo->GetYaxis()->SetTitle("Events per 0.08 cm "); histo->Draw(); TFitter *fitter = new TFitter(PARAMETERS); //initialize TFitter with a maximum of 5 params fitter->SetDefaultFitter("Minuit"); // set minimizer. Default is Minuit. fitter->SetFCN(fcn); // set FCN // Set starting values and step sizes for parameters Double_t vstart[PARAMETERS] = {0.3, -1. , 1. , 0.5 , 0.5 }; Double_t step[PARAMETERS] = {0.0001, 0.0001, 0.0001, 0.0001, 0.0001}; //step 0 li rende costanti //Int_t SetParameter(Int_t ipar, const char* parname, Double_t value, Double_t verr, Double_t vlow, Double_t vhigh) fitter->SetParameter(0, "fA", vstart[0] , step[0], 0.,1.); // Bounded fitter->SetParameter(1, "mA", vstart[1] , step[1], 0.,0.); fitter->SetParameter(2, "mB", vstart[2] , step[2], 0.,0.); fitter->SetParameter(3, "sA", vstart[3] , step[3], 0.,0.); fitter->SetParameter(4, "sB", vstart[4] , step[4], 0.,0.); // Now ready for minimization step // Start minimization with arglist[0] max iterations and arglist[1] tolerance: // the default tolerance is 0.1, and the minimization will stop when // the estimated vertical distance to the minimum (EDM) is less than // 0.001*[tolerance]*ERRDEF //Int_t ExecuteCommand(const char* command, Double_t* args, Int_t nargs) Double_t arglist[2] = {0.,0.1}; // fitter->ExecuteCommand("MIGRAD", arglist, 2); //arglist[0] = 0.; //fitter->ExecuteCommand("HESSE", arglist, 1); //fitter->ExecuteCommand("MINOS", arglist, 1); //qui il metodo: https://root.cern.ch/root/html/TFitter.html#TFitter:GetStats Double_t fcnmin, edm, errdef; Int_t nvpar, nparx; fitter->GetStats(fcnmin, edm, errdef, nvpar, nparx); std::cout << "######### FIT INFO ################ " << std::endl; std::cout << "FCN(min) = " << fcnmin << endl; std::cout << "EDM = " << edm << endl; std::cout << "ERRDEF = " << errdef << endl; std::cout << "nvpar = " << nvpar << endl; std::cout << "nparx = " << nparx << endl; std::cout << "######### PARAMETERS ################ " << std::endl; std::cout << "fA = " << fitter->GetParameter(0) << "+/-" << fitter->GetParError(0) << std::endl; std::cout << "mA = " << fitter->GetParameter(1) << "+/-" << fitter->GetParError(1) << std::endl; std::cout << "sA = " << fitter->GetParameter(2) << "+/-" << fitter->GetParError(2) << std::endl; std::cout << "mB = " << fitter->GetParameter(3) << "+/-" << fitter->GetParError(3) << std::endl; std::cout << "sB = " << fitter->GetParameter(4) << "+/-" << fitter->GetParError(4) << std::endl; //Double_t GetCovarianceMatrixElement(Int_t i, Int_t j) const //std::cout << " prova del primo elemento della matrice = "<<fitter->GetCovarianceMatrixElement(0,0) << std::endl; std::cout << "######### COVARIANCE MATRIX ################ " << std::endl; // Write on .txt file covariance Matrix FILE *file_cov = fopen ("_txt/covariance_matrix2.txt", "w"); Int_t icol=0; Int_t irow=0; if (nvpar==1) { fprintf (file_cov, "%e \n", fitter->GetCovarianceMatrixElement(0,0)); printf ("%e \n", fitter->GetCovarianceMatrixElement(0,0)); } else { for (irow=0; irow<nvpar; irow++) { for (icol=0; icol<nvpar; icol++) { fprintf (file_cov, "%e ", fitter->GetCovarianceMatrixElement(irow,icol)); printf ("%e ", fitter->GetCovarianceMatrixElement(irow,icol)); }; fprintf (file_cov, "\n"); printf ("\n"); }; //fprintf (file_cov, "\n"); //printf ("\n"); }; fclose (file_cov); std::cout << "############################################ " << std::endl; //Projection TF1 *f_proj = new TF1("f_proj",pdf_proj,-4.,4.,5); // Sets values for(Int_t k=0; k<PARAMETERS; k++){ f_proj->SetParameter(k,fitter->GetParameter(k)); }; f_proj->Draw("SAME"); c_histo->Print("./_fig/histo2.pdf"); c_histo->Print("./_fig/histo2.eps"); }
int main (int argc, char**argv) { // acquisizione dati e riempimento istogramma; // i bin sono intervalli aperti a dx [low, up) double buf1, buf2; int ThBin, PhiBin; std::ifstream dati ("MesonDecayData.txt", std::ios::in); while(1) { dati >> buf1; if (dati.eof()) {break;} dati >> buf2; // find bin ThBin = floor((buf1+1.)/ThBinWidth); PhiBin = floor(buf2/PhiBinWidth); if(ThBin > nThetaBin || PhiBin > nPhiBin) {std::cout << "Warning: overflow in filling histogram\n";} else {IstoData[ThBin][PhiBin] += 1.;} } dati.close(); // gestisce la chiamata a Minuit TFitter* minimizer = new TFitter(4); // non stampa a schermo alcuna informazione double p1 = -1; minimizer -> ExecuteCommand("SET PRINTOUT", &p1, 1); // imposta la funzione per Minuit minimizer->SetFCN(minuitChiSquare); //SetParameter(ParNum, "PARNAME", InitalValue, InitialError, LowerLimit, UpperLimit) //Se LowerLimit = UpperLimit = 0, non c'è nessun limite minimizer -> SetParameter(0, "alpha", 0.7,1,0,0); minimizer -> SetParameter(1, "beta", 0.05,1,0,0); minimizer -> SetParameter(2, "gamma", -0.2,1,0,0); minimizer -> SetParameter(3, "norm", 100,1,0,0); // prima ricerca del minimo con il metodo del simplesso // è robusta e da una prima indicazione minimizer -> ExecuteCommand ("SIMPLEX", 0,0); // minimizzo usando la routine migrad minimizer -> ExecuteCommand ("MIGRAD", 0,0); double result [4]; double error[4]; const char* parnames [4] = {"Alpha", "Beta", "Gamma", "Norm"}; for (int i = 0; i < 4; i++) { result[i] = minimizer->GetParameter(i); error[i] = minimizer->GetParError(i); } for (int i = 0; i < 4; i++) { std::cout << parnames[i] << " = " << result[i] << " +/- " << error[i] << std::endl; } return 1; }
void fitterLike3() { // Setting per lo stile del canvas e del plot //gROOT->SetStyle("Plain"); //gStyle->SetOptStat(0000000); //gStyle->SetOptFit(1111); //opt stat //Setting style labstyle(); TCanvas *c_fun_tot = new TCanvas("c_fun_tot","canvas function",600,600); c_fun_tot->cd(); //Create function TF2 *f_fun_tot = new TF2("f_fun_tot",pdf2D,x_min,x_max,y_min,y_max,9); f_fun_tot->SetParameter(0, fA) ; f_fun_tot->SetParameter(1,mAx) ; f_fun_tot->SetParameter(2,mAy) ; f_fun_tot->SetParameter(3,mBx) ; f_fun_tot->SetParameter(4,mBy) ; f_fun_tot->SetParameter(5,sAx) ; f_fun_tot->SetParameter(6,sAy) ; f_fun_tot->SetParameter(7,sBx) ; f_fun_tot->SetParameter(8,sBy) ; f_fun_tot->Draw("surf"); std::cout << "Integral(fun_tot) = " << f_fun_tot->Integral(x_min,x_max,y_min,y_max) << std::endl; TCanvas *c_fun_A = new TCanvas("c_fun_A","canvas function",600,600); c_fun_A->cd(); TF2 *f_fun_A = new TF2("f_fun_A",pdf2D,x_min,x_max,y_min,y_max,9); f_fun_A->SetParameter(0, 1.) ; f_fun_A->SetParameter(1,mAx) ; f_fun_A->SetParameter(2,mAy) ; f_fun_A->SetParameter(3,mBx) ; f_fun_A->SetParameter(4,mBy) ; f_fun_A->SetParameter(5,sAx) ; f_fun_A->SetParameter(6,sAy) ; f_fun_A->SetParameter(7,sBx) ; f_fun_A->SetParameter(8,sBy) ; f_fun_A->Draw("surf"); std::cout << "Integral(fun_A) = " << f_fun_A->Integral(x_min,x_max,y_min,y_max) << std::endl; TCanvas *c_fun_B = new TCanvas("c_fun_B","canvas function",600,600); c_fun_B->cd(); TF2 *f_fun_B = new TF2("f_fun_B",pdf2D,x_min,x_max,y_min,y_max,9); f_fun_B->SetParameter(0, 0.) ; f_fun_B->SetParameter(1,mAx) ; f_fun_B->SetParameter(2,mAy) ; f_fun_B->SetParameter(3,mBx) ; f_fun_B->SetParameter(4,mBy) ; f_fun_B->SetParameter(5,sAx) ; f_fun_B->SetParameter(6,sAy) ; f_fun_B->SetParameter(7,sBx) ; f_fun_B->SetParameter(8,sBy) ; f_fun_B->Draw("surf"); std::cout << "Integral(fun_B) = " << f_fun_B->Integral(x_min,x_max,y_min,y_max) << std::endl; //Double_t bin_width_x = (x_max-x_min)/bin_number_x; //Double_t bin_width_y = (y_max-y_min)/bin_number_y; //Create the histogramz TH2F *histo_tot = new TH2F("histo_tot","",bin_number_x,x_min,x_max,bin_number_y,y_min,y_max); TH2F *histo_A = new TH2F("histo_A" ,"",bin_number_x,x_min,x_max,bin_number_y,y_min,y_max); TH2F *histo_B = new TH2F("histo_B" ,"",bin_number_x,x_min,x_max,bin_number_y,y_min,y_max); TH1F *histo_tot_x = new TH1F("histo_tot_x","",bin_number_x,x_min,x_max); TH1F *histo_tot_y = new TH1F("histo_tot_y","",bin_number_y,y_min,y_max); TH1F *histo_A_x = new TH1F("histo_A_x" ,"",bin_number_x,x_min,x_max); TH1F *histo_A_y = new TH1F("histo_A_y" ,"",bin_number_y,y_min,y_max); TH1F *histo_B_x = new TH1F("histo_B_x" ,"",bin_number_x,x_min,x_max); TH1F *histo_B_y = new TH1F("histo_B_y" ,"",bin_number_y,y_min,y_max); //proj TH2F *histo_proj_tot = new TH2F("histo_proj_tot","",bin_number_x,x_min,x_max,bin_number_y,y_min,y_max); TH2F *histo_proj_A = new TH2F("histo_proj_A" ,"",bin_number_x,x_min,x_max,bin_number_y,y_min,y_max); TH2F *histo_proj_B = new TH2F("histo_proj_B" ,"",bin_number_x,x_min,x_max,bin_number_y,y_min,y_max); TH1F *histo_proj_tot_x = new TH1F("histo_proj_tot_x","",s_proj*bin_number_x,x_min,x_max); TH1F *histo_proj_tot_y = new TH1F("histo_proj_tot_y","",s_proj*bin_number_y,y_min,y_max); TH1F *histo_proj_A_x = new TH1F("histo_proj_A_x" ,"",s_proj*bin_number_x,x_min,x_max); TH1F *histo_proj_A_y = new TH1F("histo_proj_A_y" ,"",s_proj*bin_number_y,y_min,y_max); TH1F *histo_proj_B_x = new TH1F("histo_proj_B_x" ,"",s_proj*bin_number_x,x_min,x_max); TH1F *histo_proj_B_y = new TH1F("histo_proj_B_y" ,"",s_proj*bin_number_y,y_min,y_max); // ################################################ // ###### GENERATION ############################## // ################################################ // Event generation (Double Gaussian) Int_t i=0; for(i=0;i<(Int_t) (MAX*fA);i++){ xvar[i] = randGen.Gaus(mAx,sAx); yvar[i] = randGen.Gaus(mAy,sAy); histo_tot->Fill(xvar[i],yvar[i]); histo_tot_x->Fill(xvar[i]); histo_tot_y->Fill(yvar[i]); histo_A->Fill(xvar[i],yvar[i]); histo_A_x->Fill(xvar[i]); histo_A_y->Fill(yvar[i]); //std::cout << "i = " << i << " "<< xvar[i] << " "<< yvar[i] << std::endl; }; for(i=(Int_t) (MAX*fA);i<MAX;i++){ xvar[i]= randGen.Gaus(mBx,sBx); yvar[i]= randGen.Gaus(mBy,sBy); histo_tot->Fill(xvar[i],yvar[i]); histo_tot_x->Fill(xvar[i]); histo_tot_y->Fill(yvar[i]); histo_B->Fill(xvar[i],yvar[i]); histo_B_x->Fill(xvar[i]); histo_B_y->Fill(yvar[i]); //std::cout << "i = " << i << " "<< xvar[i] << " "<< yvar[i] << std::endl; }; // Draw the histogram //histo->GetXaxis()->SetTitle("R [cm] "); // Let us assume a lenght on x axis //histo->GetYaxis()->SetTitle("Events per 0.08 cm "); histo_tot->Draw("contZ"); std::cout<< "Entries histo_tot " <<histo_tot->GetEntries() << std::endl; std::cout<< "Entries histo_A " <<histo_A->GetEntries() << std::endl; std::cout<< "Entries histo_B " <<histo_B->GetEntries() << std::endl; // ################################################ // ###### FIT 2D ################################### // ################################################ TFitter *fitter = new TFitter(PARAMETERS); //initialize TFitter with a maximum of 5 params fitter->SetDefaultFitter("Minuit"); // set minimizer. Default is Minuit. fitter->SetFCN(fcn); // set FCN // Set starting values and step sizes for parameters //Double_t vstart[PARAMETERS] = {0.3, -1. , 1. , 0.5 , 0.5 }; //Double_t step[PARAMETERS] = {0.0001, 0.0001, 0.0001, 0.0001, 0.0001}; //step 0 li rende costanti //Int_t SetParameter(Int_t ipar, const char* parname, Double_t value, Double_t verr, Double_t vlow, Double_t vhigh) fitter->SetParameter(0, "fA ", fA , 0.0001, 0.,1.); // Bounded fitter->SetParameter(1, "mAx", mAx , 0.0001, 0.,0.); fitter->SetParameter(2, "mAy", mAy , 0.0001, 0.,0.); fitter->SetParameter(3, "mBx", mBx , 0.0001, 0.,0.); fitter->SetParameter(4, "mBy", mBy , 0.0001, 0.,0.); fitter->SetParameter(5, "sAx", sAx , 0.0001, 0.,0.); fitter->SetParameter(6, "sAy", sAy , 0.0001, 0.,0.); fitter->SetParameter(7, "sBx", sBx , 0.0001, 0.,0.); fitter->SetParameter(8, "sBy", sBy , 0.0001, 0.,0.); // Now ready for minimization step // Start minimization with arglist[0] max iterations and arglist[1] tolerance: // the default tolerance is 0.1, and the minimization will stop when // the estimated vertical distance to the minimum (EDM) is less than // 0.001*[tolerance]*ERRDEF //Int_t ExecuteCommand(const char* command, Double_t* args, Int_t nargs) Double_t arglist[2] = {0.,0.1}; // fitter->ExecuteCommand("MIGRAD", arglist, 2); Double_t fcnmin, edm, errdef; Int_t nvpar, nparx; fitter->GetStats(fcnmin, edm, errdef, nvpar, nparx); Double_t vpar[PARAMETERS]; for(Int_t k=0; k<PARAMETERS; ++k){ vpar[k] = fitter->GetParameter(k); //std::cout << vpar[k] << std::endl; }; // ################################################ // ###### PROJECTIONS ############################# // ################################################ //TH2F *histo_proj_tot = new TH2F("histo_proj_tot","",bin_number_x,x_min,x_max,bin_number_y,y_min,y_max); //TH2F *histo_proj_A = new TH2F("histo_proj_A" ,"",bin_number_x,x_min,x_max,bin_number_y,y_min,y_max); //TH2F *histo_proj_B = new TH2F("histo_proj_B" ,"",bin_number_x,x_min,x_max,bin_number_y,y_min,y_max); //TH1F *histo_proj_tot_x = new TH1F("histo_proj_tot_x","",s_proj*bin_number_x,x_min,x_max); //TH1F *histo_proj_tot_y = new TH1F("histo_proj_tot_y","",s_proj*bin_number_y,y_min,y_max); //TH1F *histo_proj_A_x = new TH1F("histo_proj_A_x" ,"",s_proj*bin_number_x,x_min,x_max); //TH1F *histo_proj_A_y = new TH1F("histo_proj_A_y" ,"",s_proj*bin_number_y,y_min,y_max); //TH1F *histo_proj_B_x = new TH1F("histo_proj_B_x" ,"",s_proj*bin_number_x,x_min,x_max); //TH1F *histo_proj_B_y = new TH1F("histo_proj_B_y" ,"",s_proj*bin_number_y,y_min,y_max); Double_t x,y,weight,weight_A,weight_B; for(Int_t j=0;j<MAX_PROJ;j++){ x = randGen.Uniform(x_max-x_min) + x_min; y = randGen.Uniform(y_max-y_min) + y_min; weight = f_fun_tot->Eval(x,y); weight_A = f_fun_A->Eval(x,y); weight_B = f_fun_B->Eval(x,y); histo_proj_tot ->Fill(x,y,weight); histo_proj_A ->Fill(x,y,weight_A); histo_proj_B ->Fill(x,y,weight_B); histo_proj_tot_x ->Fill(x,weight); histo_proj_tot_y ->Fill(y,weight); histo_proj_A_x ->Fill(x,weight_A); histo_proj_A_y ->Fill(y,weight_A); histo_proj_B_x ->Fill(x,weight_B); histo_proj_B_y ->Fill(y,weight_B); //histo_A->Fill(xvar[i],yvar[i]); //std::cout << "i = " << i << " "<< xvar[i] << " "<< yvar[i] << std::endl; }; TCanvas *c_proj_tot_x = new TCanvas("c_proj_tot_x","Projection x",600,600); c_proj_tot_x->cd(); histo_tot_x->GetXaxis()->SetTitle("x [cm] "); // Let us assume a lenght on x axis histo_tot_x->GetYaxis()->SetTitle("Events per 0.08 cm "); histo_tot_x->Draw("e"); //data x histo_proj_tot_x->SetLineColor(2); histo_proj_tot_x->Scale(MAX/histo_proj_tot_x->Integral(1,bin_number_x)); histo_proj_A_x->Scale((MAX*fitter->GetParameter(0))/histo_proj_A_x->Integral(1,bin_number_x)); histo_proj_B_x->Scale((MAX*(1.-fitter->GetParameter(0)))/histo_proj_B_x->Integral(1,bin_number_x)); //std::cout << "Integrale proj = " << histo_proj_tot_x->Integral(1,bin_number_x) << std::endl; //std::cout << "Integrale data = " << histo_tot_x->Integral(1,bin_number_x) << std::endl; histo_proj_A_x->SetLineColor(4); histo_proj_B_x->SetLineColor(4); histo_proj_A_x->Draw("C same");//projection A x histo_proj_B_x->Draw("C same");//projection B x histo_proj_tot_x->Draw("C same");//projection x TCanvas *c_proj_tot_y = new TCanvas("c_proj_tot_y","Projection y",600,600); c_proj_tot_y->cd(); histo_tot_y->GetXaxis()->SetTitle("y [cm] "); // Let us assume a lenght on y axis histo_tot_y->GetYaxis()->SetTitle("Events per 0.08 cm "); histo_tot_y->Draw("e"); //data y histo_proj_tot_y->SetLineColor(2); histo_proj_tot_y->Scale(MAX/histo_proj_tot_y->Integral(1,bin_number_y)); histo_proj_A_y->Scale((MAX*fitter->GetParameter(0))/histo_proj_A_y->Integral(1,bin_number_y)); histo_proj_B_y->Scale((MAX*(1.-fitter->GetParameter(0)))/histo_proj_B_y->Integral(1,bin_number_y)); //std::cout << "Integrale proj = " << histo_proj_tot_y->Integral(1,bin_number_y) << std::endl; //std::cout << "Integrale data = " << histo_tot_y->Integral(1,bin_number_y) << std::endl; histo_proj_A_y->SetLineColor(4); histo_proj_B_y->SetLineColor(4); histo_proj_A_y->Draw("same");//projection A y histo_proj_B_y->Draw("same");//projection B y histo_proj_tot_y->Draw("same");//projection y // ################################################ // ###### FIT 1D ################################### // ################################################ TFitter *fitter1D = new TFitter(PARAMETERS); //initialize TFitter with a maximum of 5 params fitter1D->SetDefaultFitter("Minuit"); // set minimizer. Default is Minuit. fitter1D->SetFCN(fcn1D); // set FCN fitter1D->SetParameter(0, "fA ", fA , 0.0001, 0.,1.); // Bounded fitter1D->SetParameter(1, "mAx", mAx , 0.0001, 0.,0.); fitter1D->SetParameter(2, "mBx", mBx , 0.0001, 0.,0.); fitter1D->SetParameter(3, "sAx", sAx , 0.0001, 0.,0.); fitter1D->SetParameter(4, "sBx", sBx , 0.0001, 0.,0.); //Int_t ExecuteCommand(const char* command, Double_t* args, Int_t nargs) //Double_t arglist[2] = {0.,0.1}; // fitter1D->ExecuteCommand("MIGRAD", arglist, 2); //Double_t fcnmin, edm, errdef; //Int_t nvpar, nparx; fitter1D->GetStats(fcnmin, edm, errdef, nvpar, nparx); // ################################################ // ###### SEPARATION POWER ######################## // ################################################ // best separation power Double_t sigma_best; sigma_best = TMath::Sqrt(fA*(1-fA)/MAX); cout << " ####### SEPARATION POWER ########" << std::endl; cout << "sigma_best = " << sigma_best << std::endl; // calcolo 2D dal fit Double_t sigma_fit2D; sigma_fit2D = fitter->GetParError(0); cout << "sigma_fit2D = " << sigma_fit2D << std::endl; cout << "sigma_best/sigma_fit2D = " << sigma_best/sigma_fit2D << std::endl; // calcolo 1D dal fit Double_t sigma_fit1D; sigma_fit1D = fitter1D->GetParError(0); cout << "sigma_fit1D = " << sigma_fit1D << std::endl; cout << "sigma_best/sigma_fit1D = " << sigma_best/sigma_fit1D << std::endl; #if 0 //calcolo dal Minumu Variance Bound 2D Double_t sigma_mvb2D; histo_proj_A -> Scale(1./histo_proj_A->Integral(1,bin_number_x,1,bin_number_y)); histo_proj_B -> Scale(1./histo_proj_B->Integral(1,bin_number_x,1,bin_number_y)); TH2F *histo_num = new TH2F("histo_num" ,"",bin_number_x,x_min,x_max,bin_number_y,y_min,y_max); TH2F *histo_den = new TH2F("histo_den" ,"",bin_number_x,x_min,x_max,bin_number_y,y_min,y_max); Double_t binX,binY; sigma_mvb2D = (1./MAX); #endif }