示例#1
0
//____________________________________________________________________
void fitCircle(Int_t n=10000) {
   //generates n points around a circle and fit them
   TCanvas *c1 = new TCanvas("c1","c1",600,600);
   c1->SetGrid();
   gr = new TGraph(n);
   if (n> 999) gr->SetMarkerStyle(1);
   else        gr->SetMarkerStyle(3);
   TRandom3 r;
   Double_t x,y;
   for (Int_t i=0;i<n;i++) {
      r.Circle(x,y,r.Gaus(4,0.3));
      gr->SetPoint(i,x,y);
   }
   c1->DrawFrame(-5,-5,5,5);
   gr->Draw("p");

   //Fit a circle to the graph points
   TVirtualFitter::SetDefaultFitter("Minuit");  //default is Minuit
   TVirtualFitter *fitter = TVirtualFitter::Fitter(0, 3);
   fitter->SetFCN(myfcn);

   fitter->SetParameter(0, "x0",   0, 0.1, 0,0);
   fitter->SetParameter(1, "y0",   0, 0.1, 0,0);
   fitter->SetParameter(2, "R",    1, 0.1, 0,0);

   Double_t arglist[1] = {0};
   fitter->ExecuteCommand("MIGRAD", arglist, 0);

   //Draw the circle on top of the points
   TArc *arc = new TArc(fitter->GetParameter(0),
      fitter->GetParameter(1),fitter->GetParameter(2));
   arc->SetLineColor(kRed);
   arc->SetLineWidth(4);
   arc->Draw();
}
示例#2
0
文件: TwoHistoFit2D.C 项目: Y--/root
int TwoHistoFit2D(bool global = true) {

    // create two histograms

    int nbx1 = 50;
    int nby1 = 50;
    int nbx2 = 50;
    int nby2 = 50;
    double xlow1 = 0.;
    double ylow1 = 0.;
    double xup1 = 10.;
    double yup1 = 10.;
    double xlow2 = 5.;
    double ylow2 = 5.;
    double xup2 = 20.;
    double yup2 = 20.;

    TH2D * h1 = new TH2D("h1","core",nbx1,xlow1,xup1,nby1,ylow1,yup1);
    TH2D * h2 = new TH2D("h2","tails",nbx2,xlow2,xup2,nby2,ylow2,yup2);

    double iniParams[10] = { 100, 6., 2., 7., 3, 100, 12., 3., 11., 2. };
    // create fit function
    TF2 * func = new TF2("func",my2Dfunc,xlow2,xup2,ylow2,yup2, 10);
    func->SetParameters(iniParams);

    // fill Histos
    int n1 = 50000;
    int n2 = 50000;
    //  h1->FillRandom("func", n1);
    //h2->FillRandom("func",n2);
    FillHisto(h1,n1,iniParams);
    FillHisto(h2,n2,iniParams);

    // scale histograms to same heights (for fitting)
    double dx1 = (xup1-xlow1)/double(nbx1);
    double dy1 = (yup1-ylow1)/double(nby1);
    double dx2 = (xup2-xlow2)/double(nbx2);
    double dy2 = (yup2-ylow2)/double(nby2);
//   h1->Sumw2();
//   h1->Scale( 1.0 / ( n1 * dx1 * dy1 ) );
    // scale histo 2 to scale of 1
    h2->Sumw2();
    h2->Scale(  ( double(n1) * dx1 * dy1 )  / ( double(n2) * dx2 * dy2 ) );


    if (global) {
        // fill data structure for fit (coordinates + values + errors)
        std::cout << "Do global fit" << std::endl;
        // fit now all the function together

        // fill data structure for fit (coordinates + values + errors)
        TAxis *xaxis1  = h1->GetXaxis();
        TAxis *yaxis1  = h1->GetYaxis();
        TAxis *xaxis2  = h2->GetXaxis();
        TAxis *yaxis2  = h2->GetYaxis();

        int nbinX1 = h1->GetNbinsX();
        int nbinY1 = h1->GetNbinsY();
        int nbinX2 = h2->GetNbinsX();
        int nbinY2 = h2->GetNbinsY();

        /// reset data structure
        coords = std::vector<std::pair<double,double> >();
        values = std::vector<double>();
        errors = std::vector<double>();


        for (int ix = 1; ix <= nbinX1; ++ix) {
            for (int iy = 1; iy <= nbinY1; ++iy) {
                if ( h1->GetBinContent(ix,iy) > 0 ) {
                    coords.push_back( std::make_pair(xaxis1->GetBinCenter(ix), yaxis1->GetBinCenter(iy) ) );
                    values.push_back( h1->GetBinContent(ix,iy) );
                    errors.push_back( h1->GetBinError(ix,iy) );
                }
            }
        }
        for (int ix = 1; ix <= nbinX2; ++ix) {
            for (int iy = 1; iy <= nbinY2; ++iy) {
                if ( h2->GetBinContent(ix,iy) > 0 ) {
                    coords.push_back( std::make_pair(xaxis2->GetBinCenter(ix), yaxis2->GetBinCenter(iy) ) );
                    values.push_back( h2->GetBinContent(ix,iy) );
                    errors.push_back( h2->GetBinError(ix,iy) );
                }
            }
        }

        TVirtualFitter::SetDefaultFitter("Minuit");
        TVirtualFitter * minuit = TVirtualFitter::Fitter(0,10);
        for (int i = 0; i < 10; ++i) {
            minuit->SetParameter(i, func->GetParName(i), func->GetParameter(i), 0.01, 0,0);
        }
        minuit->SetFCN(myFcn);

        double arglist[100];
        arglist[0] = 0;
        // set print level
        minuit->ExecuteCommand("SET PRINT",arglist,2);

// minimize
        arglist[0] = 5000; // number of function calls
        arglist[1] = 0.01; // tolerance
        minuit->ExecuteCommand("MIGRAD",arglist,2);

        //get result
        double minParams[10];
        double parErrors[10];
        for (int i = 0; i < 10; ++i) {
            minParams[i] = minuit->GetParameter(i);
            parErrors[i] = minuit->GetParError(i);
        }
        double chi2, edm, errdef;
        int nvpar, nparx;
        minuit->GetStats(chi2,edm,errdef,nvpar,nparx);

        func->SetParameters(minParams);
        func->SetParErrors(parErrors);
        func->SetChisquare(chi2);
        int ndf = coords.size()-nvpar;
        func->SetNDF(ndf);

        std::cout << "Chi2 Fit = " << chi2 << " ndf = " << ndf << "  " << func->GetNDF() << std::endl;

        // add to list of functions
        h1->GetListOfFunctions()->Add(func);
        h2->GetListOfFunctions()->Add(func);
    }
    else {
        // fit independently
        h1->Fit(func);
        h2->Fit(func);
    }



    // Create a new canvas.
    TCanvas * c1 = new TCanvas("c1","Two HIstogram Fit example",100,10,900,800);
    c1->Divide(2,2);
    gStyle->SetOptFit();
    gStyle->SetStatY(0.6);

    c1->cd(1);
    h1->Draw();
    func->SetRange(xlow1,ylow1,xup1,yup1);
    func->DrawCopy("cont1 same");
    c1->cd(2);
    h1->Draw("lego");
    func->DrawCopy("surf1 same");
    c1->cd(3);
    func->SetRange(xlow2,ylow2,xup2,yup2);
    h2->Draw();
    func->DrawCopy("cont1 same");
    c1->cd(4);
    h2->Draw("lego");
    gPad->SetLogz();
    func->Draw("surf1 same");

    return 0;
}
void makegraph_xyhits_mchamp1000(){

  //bool save_plots = false;
  bool save_plots = true;

  TCanvas* canvas = new TCanvas("c1","c1",10,10,700,550);
  //canvas_style(canvas);

  //TH2* h1=new TH2F("h1","",2000,0,2000,2000,-1000,1000);
  TH2* h1=new TH2F("h1","",1000,-1000,0,1000,0,1000);
  h1->SetStats(kFALSE);
  h1->SetTitle(";x [cm];y [cm]");
  //h2_style(h1,3,1,1,1001,50,-1111.,-1111.,510,510,20,1,1.4,0);

  const Int_t n_hits = 20;
  Float_t x[n_hits] = {-522.992,
		       -524.292,
		       -525.592,
		       -526.892,
		       -606.525,
		       -607.825,
		       -609.124,
		       -610.424,
		       -630.144,
		       -631.444,
		       -632.744,
		       -634.044,
		       -741.681,
		       -743.086,
		       -744.378,
		       -745.665,
		       -765.758,
		       -767.126,
		       -768.413,
		       -769.755};
  
  Float_t y[n_hits] = {82.024,
		       82.388,
		       82.661,
		       82.971,
		       101.400,
		       101.678,
		       101.929,
		       102.318,
		       106.826,
		       107.214,
		       107.421,
		       107.769,
		       132.755,
		       132.924,
		       133.286,
		       133.657,
		       138.406,
		       138.637,
		       139.007,
		       139.285};

  graph = new TGraph(n_hits, x, y);
  //gr_style(graph,1,1,1,1001,50,-1111,-1111,510,510,20,1,1.3,1);
  graph->SetMarkerStyle(20);
  graph->SetMarkerSize(1);
  graph->SetMarkerColor(1);
  graph->SetTitle(";x [cm];y [cm]");
  //graph->GetYaxis()->SetRangeUser(0,20);


  //Fit a circle to the graph points
  TVirtualFitter::SetDefaultFitter("Minuit");  //default is Minuit
  TVirtualFitter *fitter = TVirtualFitter::Fitter(0, 3);
  Int_t a,c;
  Double_t b,f,par;
  fitter->SetFCN(myfcn);

  fitter->SetParameter(0, "x0",   0,    0.1, -1000, 1000);
  fitter->SetParameter(1, "y0",   1000, 0.1, 0,     2000);
  fitter->SetParameter(2, "R",    1000, 0.1, 0,     1000);

  Double_t arglist[1] = {0};
  fitter->ExecuteCommand("MIGRAD", arglist, 0);

  //Draw the circle on top of the points
  TArc *arc = new TArc(fitter->GetParameter(0),
		       fitter->GetParameter(1),fitter->GetParameter(2));
  arc->SetLineColor(kRed);
  arc->SetLineWidth(4);



  Leg1 = new TLegend(0.45,0.75,0.75,0.85);
  //Leg1->AddEntry(graph_noChaCut,"Average Before TS","p");
  Leg1->AddEntry(graph,"After TS","p");
  //Leg1->AddEntry(graph_predicted,"Predicted After TS","p");
  Leg1->SetBorderSize(0);
  Leg1->SetTextSize(0.04);
  Leg1->SetFillColor(0);


  //TPaveLabel *text1 = new TPaveLabel(.17,.85,.37,.89,"CMS Preliminary    #sqrt{s}=8 TeV","NDC");
  //TPaveLabel *text1 = new TPaveLabel(.17,.85,.37,.89,"CMS Preliminary    #sqrt{s}=8 TeV    3.4 fb^{-1}","NDC");
  //TPaveLabel *text1 = new TPaveLabel(.17,.85,.37,.89,"CMS Preliminary    #sqrt{s}=8 TeV    4.4 fb^{-1}","NDC");
  TPaveLabel *text1 = new TPaveLabel(.17,.85,.37,.89,"CMS Preliminary    #sqrt{s}=8 TeV","NDC");
  text1->SetBorderSize(0);
  text1->SetTextSize(0.7);
  text1->SetFillColor(0);

  TPaveLabel *text2 = new TPaveLabel(.17,.85,.37,.89,"1000 GeV H++, Event 58654","NDC");
  text2->SetBorderSize(0);
  text2->SetTextSize(0.7);
  text2->SetFillColor(0);


  TPaveText* text1a = new TPaveText(-500,500,-300,700);
  text1a->SetBorderSize(0);
  text1a->SetTextSize(0.05);
  text1a->SetFillColor(0);
  double p0  = fitter->GetParameter(0);
  double p1  = fitter->GetParameter(1);
  double p2  = fitter->GetParameter(2);
  double e0  = fitter->GetParError(0);
  double e1  = fitter->GetParError(1);
  double e2  = fitter->GetParError(2);
  char p0name[50], p1name[50], p2name[50];
  sprintf(p0name,"x0 = %.2f #pm %.2f",p0,e0);
  sprintf(p1name,"y0 = %.2f #pm %.2f",p1,e1);
  sprintf(p2name,"R = %.2f #pm %.2f",p2,e2);
  text1a->AddText(p0name);
  text1a->AddText(p1name);
  text1a->AddText(p2name);


  canvas->cd();
  h1->Draw();
  arc->Draw("same");
  graph->Draw("Psame");
  //text1->Draw();
  text1a->Draw();
  text2->Draw();
  //Leg1->Draw();
  if(save_plots) canvas->SaveAs("../plots/hits_xy_mchamp1000.eps");

  //return 0;

}