void graph2derrorsfit() { TCanvas *c1 = new TCanvas("c1"); Double_t rnd, x, y, z, ex, ey, ez; Double_t e = 0.3; Int_t nd = 500; TRandom r; TF2 *f2 = new TF2("f2","1000*(([0]*sin(x)/x)*([1]*sin(y)/y))+200",-6,6,-6,6); f2->SetParameters(1,1); TGraph2DErrors *dte = new TGraph2DErrors(nd); // Fill the 2D graph for (Int_t i=0; i<nd; i++) { f2->GetRandom2(x,y); rnd = r.Uniform(-e,e); // Generate a random number in [-e,e] z = f2->Eval(x,y)*(1+rnd); dte->SetPoint(i,x,y,z); ex = 0.05*r.Rndm(); ey = 0.05*r.Rndm(); ez = TMath::Abs(z*rnd); dte->SetPointError(i,ex,ey,ez); } f2->SetParameters(0.5,1.5); dte->Fit(f2); TF2 *fit2 = (TF2*)dte->FindObject("f2"); fit2->SetTitle("Minuit fit result on the Graph2DErrors points"); fit2->Draw("surf1"); dte->Draw("axis p0"); }
// Create, Draw and fit a TGraph2DErrors void macro4(){ gStyle->SetPalette(1); const double e = 0.3; const int nd = 500; TRandom3 my_random_generator; TF2 *f2 = new TF2("f2", "1000*(([0]*sin(x)/x)*([1]*sin(y)/y))+200", -6,6,-6,6); f2->SetParameters(1,1); TGraph2DErrors *dte = new TGraph2DErrors(nd); // Fill the 2D graph double rnd, x, y, z, ex, ey, ez; for (Int_t i=0; i<nd; i++) { f2->GetRandom2(x,y); // A random number in [-e,e] rnd = my_random_generator.Uniform(-e,e); z = f2->Eval(x,y)*(1+rnd); dte->SetPoint(i,x,y,z); ex = 0.05*my_random_generator.Uniform(); ey = 0.05*my_random_generator.Uniform(); ez = TMath::Abs(z*rnd); dte->SetPointError(i,ex,ey,ez); } // Fit function to generated data f2->SetParameters(0.7,1.5); // set initial values for fit f2->SetTitle("Fitted 2D function"); dte->Fit(f2); // Plot the result TCanvas *c1 = new TCanvas(); f2->Draw("Surf1"); dte->Draw("P0 Same"); // Make the x and y projections TCanvas* c_p= new TCanvas("ProjCan", "The Projections",1000,400); c_p->Divide(2,1); c_p->cd(1); dte->Project("x")->Draw(); c_p->cd(2); dte->Project("y")->Draw(); }
TGraph2DErrors* GenerateRegionIntegratedSurface(TGraph2DErrors *gr, const ROOT::Math::IMultiGenFunction& func, std::vector<Regions>& regions) { RegionIntegratorMultiDim Integrator; Integrator.SetFunction(func); TGraph2DErrors* mygr = new TGraph2DErrors(gr->GetN()); mygr->SetDirectory(0); Double_t *fX = gr->GetX(); Double_t *fY = gr->GetY(); Double_t *fZ = gr->GetZ(); Double_t v=0, err=0; Int_t n = std::count_if(fZ, fZ + gr->GetN(), std::bind2nd(std::greater<Double_t>(),0)); std::cout << n << std::endl; UInt_t count = 0; std::cout << "\nGenerating the integrated surface"; std::cout << "\n"; boost::progress_display prg(n); for (Int_t i=0; i<gr->GetN() && i<regions.size(); i++) { if (fZ[i]==0) continue; regions[i] *= CLHEP::deg; v = Integrator.RegionIntegral(regions[i]); mygr->SetPoint(count,fX[i],fY[i],v); count++; ++prg; } std::cout << std::endl; return mygr; }
void ExpManager::GetExp2DGraph(TString NameTitle, double xmin, double xmax, double ymin, double ymax, double zmin, double zmax) { TGraph2DErrors *fGraph = new TGraph2DErrors(); //= new TGraph2DErrors(np, x_array, y_array, bz_array, ex, ey, ez); fGraph->SetTitle("Exp Data;Y (mm);X (mm);Magnetic Field (mT)"); fGraph->SetMarkerSize(1.2); fGraph->SetMarkerStyle(20); fGraph->SetMarkerColor(kBlue); fGraph->SetLineColor(kBlue); fGraph->SetLineWidth(2); int graph_counter = 0 ; for (unsigned i=0; i< fExpY.size(); i++) { if( (fExpX.at(i) >= xmin && fExpX.at(i) <= xmax) && (fExpY.at(i) >= ymin && fExpY.at(i) <= ymax) && (fExpZ.at(i) >= zmin && fExpZ.at(i) <= zmax) ) { fGraph->SetPoint(graph_counter,fExpY.at(i),fExpX.at(i),fExpB.at(i)); fGraph->SetPointError(graph_counter,fExpYErr.at(i),fExpXErr.at(i),fExpBErr.at(i)); graph_counter++; } } fGraph->SetTitle(NameTitle+Form(" Experimental Data : %.2f < X < %.2f mm __ %.2f < Y < %.2f mm;Y (mm);X (mm);Magnetic Field (mT)",xmin,xmax,ymin,ymax)); fGraph->SetName(NameTitle+Form("_Exp_X_%.2f_%.2fmm_Y_%.2f_%.2fmm",xmin,xmax,ymin,ymax)); fGraph->Write(); }
void plot2d(){ ifstream infile; infile.open("/Users/keithlandry/Desktop/Research/2012IFF/rootFiles/asymmetriesOutfile.txt"); if (!infile) { cout << "f**k" << endl; } TGraph2DErrors* gPtEta = new TGraph2DErrors(); TGraph2DErrors* gMassEta = new TGraph2DErrors(); TGraph2DErrors* gPtMass = new TGraph2DErrors(); gPtEta->Set(nPtBins*nEtaBins); gMassEta->Set(nMassBins*nEtaBins); gPtMass->Set(nPtBins*nMassBins); double x; double y; double x; double z; double ex; double ey; double ez; double ptbin; double etabin; double massbin; double point = 0; string junk; infile >> junk; cout << junk << endl; for (int i=0; i<nPtBins; i++) { for (int j = 0; j<nEtaBins; j++) { infile >> ptbin; infile >> etabin; infile >> x; infile >> y; infile >> z; infile >> ex; infile >> ey; infile >> ez; cout << point << " " << x << " " << y << " " << z << " " << ex << " " << ey << " " << ez << endl; gPtEta->SetPoint(point,x,y,z); gPtEta->SetPointError(point,ex,ey,ez); point++; } } TCanvas* c1 = new TCanvas(); gPtEta->Draw("p0 err "); infile >> junk; cout << junk << endl; point = 0; for (int i=0; i<nMassBins; i++) { for (int j = 0; j<nEtaBins; j++) { infile >> ptbin; infile >> massbin; infile >> x; infile >> y; infile >> z; infile >> ex; infile >> ey; infile >> ez; cout << point << " " << x << " " << y << " " << z << " " << ex << " " << ey << " " << ez << endl; gMassEta->SetPoint(point,x,y,z); gMassEta->SetPointError(point,ex,ey,ez); point++; } } TCanvas* c2 = new TCanvas(); gMassEta->GetYaxis()->SetRangeUser(-0.9,0.9); gMassEta->Draw("p0 err "); gMassEta->GetYaxis()->SetRangeUser(-0.9,0.9); infile >> junk; cout << junk << endl; point = 0; for (int i=0; i<nPtBins; i++) { for (int j = 0; j<nMassBins; j++) { infile >> ptbin; infile >> massbin; infile >> x; infile >> y; infile >> z; infile >> ex; infile >> ey; infile >> ez; cout << point << " " << x << " " << y << " " << z << " " << ex << " " << ey << " " << ez << endl; gPtMass->SetPoint(point,x,y,z); gPtMass->SetPointError(point,ex,ey,ez); point++; } } TCanvas* c3 = new TCanvas(); gPtMass->GetZaxis()->SetRangeUser(0,0.07); gPtMass->Draw("p0 err "); }