ROOT Version: 5.3
Platform: Not Provided
Compiler: Not Provided
Hi.
I am trying to draw two fitted data from an two separate ascii files on the same canvas. I tried the following but no success:
#include "TGraphErrors.h"
#include "TCanvas.h"
#include "TH1D.h"
#include <iostream>
#include <fstream>
void fit_v1()
{
const Int_t n = 66; // number of data points
Double_t x[n] ={0};
Double_t y[n]={0.0};
Double_t ex[n]={0.0};
Double_t ey[n]={0.0};
ifstream in("Timewalk_Run11_gate1172.dat"); // name of the input file Timewalk_60Co_Run11_gate344.dat
if(!in) {
cout << "Cannot open input file!!!" << endl;
}
if(in){
for(int i = 0; i<n ; i++){
in >> x[i] >> y[i] >> ey[i];
}
in.close();
}
for(int i = 0; i<n ; i++){
cout << x[i] << "," << " " << y[i] << "," << " " << ey[i] << endl;
}
gStyle->SetOptFit(1);
TCanvas *c1 = new TCanvas("c1","Canvas",500,500);
TGraphErrors *p = new TGraphErrors(n,x,y,NULL,ey);
p->Draw("AP");
/////////////////////////***********************THis is what I used to fit the first function*********************************/////////////////////////////////////
TF1 *fit = new TF1("fit"," (([0]/(sqrt([1]+x)))+[2]*x + [3])",0,1600);
fit->SetParameters(0.5,1.0,7.0,7.0); // initial values for (a0,a2,a4)
p->Fit(fit, "");
p->GetXaxis()->SetRangeUser(200, 1600);
p->GetYaxis()->SetRangeUser(4500, 5000);
/////////////////////////***********************Now I want to fit the second function*********************************/////////////////////////////////////
/////////////////////////***********************First I want the second function *********************************/////////////////////////////////////
ifstream in("Timewalk_reversegate.txt");
if(!in) {
cout << "Cannot open input file!!!" << endl;
}
if(in){
for(int i = 0; i<n ; i++){
in >> x[i] >> y[i] >> ey[i];
}
in.close();
}
for(int i = 0; i<n ; i++){
cout << x[i] << "," << " " << y[i] << "," << " " << ey[i] << endl;
}
TH1D *p1 = (TH1D*)(p->Clone());
TF1 *fit = new TF1("fit"," (([0]/(sqrt([1]+x)))+[2]*x + [3])",0,1600);
fit->SetParameters(0.5,1.0,7.0,7.0); // initial values for (a0,a2,a4)
//p->Fit(fit, "");
//p->GetXaxis()->SetRangeUser(200, 1600);
//p->GetYaxis()->SetRangeUser(4500, 5000);
p1->fit("sames");
p1->Draw("sames");
c1->Update();
TPaveStats *stat = (TPaveStats*)(p->FindObject("stats"));
TPaveStats *stat1 = (TPaveStats*)(p1->FindObject("stats"));
if(stat && stat1) {
stat->SetTextColor(kBlue);
stat1->SetTextColor(kGreen);
float height = stat1->GetY2NDC() - stat1->GetY1NDC();
stat1->SetY1NDC(stat->GetY1NDC() - height);
stat1->SetY2NDC(stat->GetY1NDC() );
stat1->Draw();
}
return;
}