Fitting issue

Hello.
I am using ROOT version 6.22/02 on Ubuntu 18.04.
My code is in attachment.
If, when i fit the graph, I write the fitting region as

  TF1 *func = new TF1("fit",fitf,16, 21,2);   

everything works fine, but if I replace it with

  TF1 *func = new TF1("fit",fitf,EF[0],EF[cont1-1],2); 

I get that the "Warning in <Fit>: Fit data is empty".
Could anyone explain me why, please? I think in the 2 cases I should obtain the same thing (everything should work fine), so now I am not sure I can trust the result when I use

TF1 *func = new TF1("fit",fitf,16, 21,2);  

Could anyone give a look please? The code and data file are pasted below.
Can I trust the result when i use TF1 *func = new TF1(“fit”,fitf,16, 21,2), please?

Thanks,
cheers,

// CPP includes
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <cmath>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <fstream>

// C includes
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

// ROOT includes
#include <TSystem.h>
#include <TROOT.h>
#include <TApplication.h>
#include <TRint.h>
#include <TDirectory.h>
#include <TFile.h>
#include <TChain.h>
#include <TTree.h>
#include <TNtuple.h>
#include <TBranch.h>
#include <TH1.h>
#include <TAxis.h>
#include <TCanvas.h>
#include <TColor.h>
#include <TPad.h>
#include <TStyle.h>
#include <TKey.h>
#include <TLegend.h>
#include <TText.h>
#include <TPaveText.h>
#include <TLine.h>
#include <TMarker.h>
#include <TLatex.h>
#include <TMath.h>
#include <TH1.h>
#include <TH2.h>
#include <TF1.h>
#include <TF2.h>
#include <TString.h>
#include "TObjString.h"
#include "TRandom3.h"
#include "TGraph.h"
#include "TGraphErrors.h"

#include "TGraphSmooth.h"
#include "TSpline.h"

using namespace std;

Double_t fitf(Double_t *x,Double_t *par) {
      Double_t arg = 0;
      arg = -par[1]/x[0];
      Double_t fitval = par[0]*TMath::Exp(arg);
      return fitval;
   }
   
void Plot(){

  TH1::AddDirectory(kFALSE);
  TH1::AddDirectory(kFALSE);
  gStyle->SetOptStat(0);
  int cont1=0;
  double x,y,z;
  TCanvas*c = new TCanvas("c","c",600,600);
  FILE *myfile1;
  myfile1 = fopen("data.dat","r");
  if (myfile1 == NULL) {
                       printf("Unable to open file 1\n");
                       }

   while(1)
    {
     fscanf(myfile1, " %lf  %lf  %lf", &x, &y, &z );
     if(feof(myfile1)!=NULL) break;
     cont1++;
     }

  
  double pressure    = 750.062*3.5; // Torr   
  double *EF         = new double[cont1]; //V/cm
  double *ALPHA      = new double[cont1];
  double *EF_red     = new double[cont1];
  double *ALPHA_red  = new double[cont1];
  double *dALPHA     = new double[cont1];  
  double *dALPHA_red = new double[cont1];
  
  
  
  rewind(myfile1);
  for(int i =0;i<cont1;i++) {fscanf(myfile1, " %lf  %lf %lf", &EF[i], &ALPHA[i], &dALPHA[i] );
                             EF_red[i]     = EF[i]/pressure; 
                             ALPHA_red[i]  = ALPHA[i]/pressure;
                             dALPHA[i]     = ALPHA[i]*dALPHA[i]/100;
                             dALPHA_red[i] = dALPHA[i]/pressure;} 
      
  TGraphErrors *gJ = new TGraphErrors(cont1, EF_red, ALPHA_red, 0 ,dALPHA_red);
  gJ->SetMarkerColor(1);
  gJ->SetLineColor(1);
  gJ->SetLineWidth(2);
  gJ->SetLineStyle(7);
  gJ->SetMarkerStyle(20);  
  //
  gJ->GetXaxis()->SetRangeUser(0,100);  
  gJ->GetXaxis()->SetRange(0,100);  
  gJ->GetXaxis()->SetLimits(0,100);  
  //
  gJ->GetYaxis()->SetLimits(0,4);  
  gJ->GetYaxis()->SetRange(0,4);  
  gJ->GetYaxis()->SetRangeUser(0,4);  
  
  
  gJ->GetXaxis()->SetTitle("E_{red} (V cm^{-1} Torr^{-1})");  
  gJ->GetYaxis()->SetTitle("#alpha_{red} (cm^{-1} Torr^{-1})");  
  gJ->SetTitle();  
  gJ->Draw("ALP");
  
  // Fitting function
  TF1 *func = new TF1("fit",fitf,16, 21,2);   
  //TF1 *func = new TF1("fit",fitf,EF[0],EF[cont1-1],2); 
  func->SetParameters(1,70);  
  func->SetLineWidth(5);
  func->SetLineColor(2);
  gJ->Fit("fit","R0");
  func->Draw("same");
  
  
  
  /*
  TF1 *func2 = new TF1("fit2",fitf,0,1000,2);
  func2->FixParameter(0,func->GetParameter(0));  
  func2->FixParameter(1,func->GetParameter(1));  
  func2->SetLineColor(kCyan);
  func2->SetLineStyle(10);
  func2->Draw("same");
  cout << "A " << func->GetParameter(0)<< endl;
  cout << "B " << func->GetParameter(1)<< endl;
  */
  
  
}

Here is the data file data.dat

42400.0000 84.3 0.43
42800.0000 87.0 0.45
43000.0000 88.6 0.68
43200.0000 91.5 1.24
43400.0000 93.3 1.30
43600.0000 92.5 0.70
44000.0000 95.7 1.43
44200.0000 99.8 1.93
44400.0000 99.2 0.49
44600.0000 98.8 3.13
44800.0000 103.3 1.75
45000.0000 104.0 0.73
45200.0000 105.7 0.55
45400.0000 106.0 0.80
45600.0000 108.3 0.44
46000.0000 110.9 0.80
46200.0000 113.3 1.37
46400.0000 114.5 0.63
46800.0000 118.9 1.44
47000.0000 118.0 2.14
47200.0000 122.4 0.83
47400.0000 124.3 0.98
47600.0000 125.3 0.31
48000.0000 130.4 0.62
48200.0000 131.4 1.03
48400.0000 134.2 1.10
48600.0000 132.7 0.62
48800.0000 134.6 0.77
49000.0000 137.5 1.15
49200.0000 139.7 1.34
49400.0000 140.4 1.02
49600.0000 143.6 0.46
49800.0000 143.8 1.59
50000.0000 144.3 1.91
50400.0000 152.1 1.42
50600.0000 153.5 0.92
50800.0000 155.4 1.50
51000.0000 158.2 0.69
51200.0000 160.1 0.67
51400.0000 162.1 0.72
51600.0000 163.1 0.51
51800.0000 165.4 0.45
52000.0000 168.1 0.70
52200.0000 169.1 0.68
52400.0000 173.0 0.89
52600.0000 175.0 0.72
52800.0000 174.9 0.63
53000.0000 178.5 0.75
53200.0000 179.9 0.33
53400.0000 183.0 0.58
53600.0000 183.5 0.43
53800.0000 187.1 1.07
54000.0000 189.5 0.49
54200.0000 189.7 0.99
54400.0000 192.1 0.70
54600.0000 195.9 0.68
54800.0000 196.2 0.58
55000.0000 200.9 0.78
55200.0000 201.6 0.84
55400.0000 204.8 0.37
55600.0000 206.8 0.99
55800.0000 208.8 0.61
56000.0000 210.5 0.68

Have you check what are the value of EF[0] and EF[cont1-1] ?
are they equal to 16 and 21 respectively ?

They are 16.151 and 21.3316 respectively

Make sure. Don’t assume they are. Print them out from the macro just before the Fit line.

1 Like

Yes, sorry. I had to use EF_red, not EF.

Thanks. It works now!

Cheers,
Franciuska