Draw two fitted data from ascii file in root


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;
}

Try:

{
  TGraphErrors *p = new TGraphErrors("Timewalk_Run11_gate1172.dat", "%lg %lg %lg");
  TGraphErrors *p1 = new TGraphErrors("Timewalk_reversegate.txt", "%lg %lg %lg");
  TF1 *fit = new TF1("fit", "[0]/sqrt([1]+x) + [2]*x + [3]", 0., 1600.);
  fit->SetLineColor(kRed);
  fit->SetParameters(0.5, 1.0, 7.0, 7.0);
  p->Fit(fit, "");
  p->Draw();
  fit->SetLineColor(kGreen);
  fit->SetParameters(0.5, 1.0, 7.0, 7.0);
  p1->Fit(fit, "");
  p1->Draw("same");
}

BTW. When you post “source code” or “output” here, do remember to enclose them into two lines which contain just three characters ``` (see how your post has been edited above).

1 Like

Thank you very much it works very well. I find difficulty, however, when I have to use the fit panel in order to optimize both fits. i.e in selecting the fit range for both data sets. Would the best be to draw and fit the data sets on separate canvases and the optimize the fits and then draw them in one canvas?

I figured that if I name the two data sets I can use the fit panel for each. Thank you very much for toy help.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.