//------------------------------------------ void setsigmay(TGraphErrors *g, TF1 *f) { Int_t N = g->GetN(); double sr=0; for (int i=0; iGetX()[i]; double y = g->GetY()[i]; double res = y - f->Eval(x); sr += res*res; } double sigmay = TMath::Sqrt(sr/(N-2)); printf("---> sigmay = %f\n",sigmay); for (int i=0; iSetPointError(i,0,sigmay); } //------------------------------------------ TGraphErrors *residui(TGraphErrors *g, TF1 *f) { TGraphErrors *r = new TGraphErrors(*g); r->SetName("res"); Int_t N = g->GetN(); for (int i=0; iGetX()[i]; double y = g->GetY()[i]; double res = y - f->Eval(x); r->SetPoint(i,x,res); } return r; } //----------------------------------------- void macro4() { // carica un grafico da file di testo TGraphErrors *g1 = new TGraphErrors("R1err.dat"); g1->SetName("g1"); g1->SetMarkerStyle(20); g1->SetMarkerColor(4); g1->SetLineColor(2); g1->GetXaxis()->SetTitle("Intensita' [mA]"); g1->GetYaxis()->SetTitle("Tensione [mV]"); g1->Draw("apl"); // crea la funzione (retta) per il fit TF1 *f1 = new TF1("f1","[0]+x*[1]",0,50); // esegue il fit con f1 nel range specificato in f1 (da 0 a 50) g1->Fit(f1,"RNQ"); f1->Draw("same"); // recupera i parametri double a0 = f1->GetParameter(0); double a1 = f1->GetParameter(1); // recupera le deviazioni std. double ea0 = f1->GetParError(0); double ea1 = f1->GetParError(1); printf("a0 = %f +- %f \n",a0,ea0); printf("a1 = %f +- %f \n",a1,ea1); // recupera gli elementi della matrice di covarianza TVirtualFitter *vf = TVirtualFitter::GetFitter(); double cm01 = vf->GetCovarianceMatrixElement(0,1); double cm00 = vf->GetCovarianceMatrixElement(0,0); double cm11 = vf->GetCovarianceMatrixElement(1,1); // calcola il coeff. di correlazione tra i parametri double rho = cm01/TMath::Sqrt(cm00)/TMath::Sqrt(cm11); printf("correl. factor = %f \n",rho); TCanvas *c2 = new TCanvas("c2"); setsigmay(g1,f1); TGraphErrors *res = residui(g1,f1)->Draw("ap"); }