Hello,
I am writing a small program to read data from a text file, make a tree out of it, and make a straight line fit for the resulting plot. I have no problem making the fit for a 2-D histogram. But when making a profile plot out of it and try and make fit I get the following error:
$ Warning in : Fit data is empty
#include <stdlib.h>
#include "Riostream.h"
#include "TProfile.h"
p116q1()
{
TString dir = gSystem->UnixPathName(gInterpreter->GetCurrentMacroName());
dir.ReplaceAll("p116q1.C","");
dir.ReplaceAll("/./","/");
ifstream in; // variable to create space for the enty
in.open(Form("%sp116q1.dat",dir.Data()));
Float_t x, y; // variables to store variables
Int_t nlines =0;
TFile *f = new TFile("p116q1.root","RECREATE"); // create a new file.
TH1F *h1 = new TH1F("h1","x distribution",10,0,10); // create a histogram for x values
TH1F *h2 = new TH1F("h2","y distribution",100,0,100); // create a histogram for y values
TH2F *h3 = new TH2F("h3","xy distribution",10,0,10, 100, 0, 100); // create a histogram for xy values
TNtuple *ntuple = new TNtuple("ntuple","data from ascii file","x:y"); //Create an Ntuple for 2 variables
while (1) { // to read everything, see if you can make an eof statement at it
in >> x >> y;
if (!in.good()) break;
// if (nlines < 100) printf("x=%8f",x);
if (nlines < 100) printf("x=%8f y=%8f",x , y);
cout << endl;
h1->Fill(x);
h2->Fill(y);
h3->Fill(x,y);
ntuple->Fill(x,y);
nlines++;
}
//h3->SetError(0.4);
cout << endl;
cout <<"Mean Value (from ROOT) = " << endl;
cout << h1->GetMean(1) << endl; // getting the average from ROOT for the x value
cout <<"Mean Value (from ROOT) for y values = " << endl;
cout << h2->GetMean(1) << endl; // getting the average from ROOT for the y value
cout << "Standard Deviation (from ROOT) = " << endl;
cout << h1->GetRMS() << endl;
cout << "Standard Deviation (from ROOT) for y values = " << endl;
cout << h2->GetRMS() << endl;
cout << "The covariance (from ROOT) = " << endl;
cout << h3->GetCovariance() << endl;
cout << "The correlation (from ROOT) = " << endl;
cout << h3->GetCorrelationFactor() << endl;
printf(" found %d points\n",nlines);
// printf(" found %d points\n",nlines);
// printf(" found %d points\n",nlines);
in.close();
f->Write(); // write the info to the file.
//
cout << "now let's loop on the data point in the TTuple to find the average" << endl;
ntuple->SetBranchAddress("x", &x);
ntuple->SetBranchAddress("y", &y);
double Sum_x = 0, Average_x, Sigma_x, SumSquare_x = 0;
double Sum_y = 0, Average_y, Sigma_y, SumSquare_y = 0;
double Sum_xy = 0;
for(int i; i < nlines ;i++)
{
// cout << "Hello I am inside the loop" << endl;
cout << ntuple->GetEntry(i) << endl;
cout << "x = " << x << " y = " << y << endl;
Sum_x = Sum_x + x ;
Sum_y = Sum_y + y ;
}
cout << " Sum_x = " << Sum_x << endl;
cout << " Sum_y = " << Sum_y << endl;
Average_x = Sum_x / nlines;
Average_y = Sum_y / nlines;
cout << " Average_x = " << Average_x << " Average_y = " << Average_y << endl;
cout << "One more loop to calculate Sigma" << endl;
for(int ii ; ii < nlines ;ii++)
{
// cout << "Hello I am inside the second loop" << endl;
cout << ntuple->GetEntry(ii) << endl;
cout << " x = " << x << " y = " << y << endl;
SumSquare_x = SumSquare_x + ((x - Average_x)*(x - Average_x));
SumSquare_y = SumSquare_y + ((y - Average_y)*(y - Average_y));
Sum_xy = Sum_xy + ((x - Average_x)*(y - Average_y));
}
//
Sigma_x = sqrt(SumSquare_x/nlines);
Sigma_y = sqrt(SumSquare_y/nlines);
cout << "Sigma_x = " << sqrt(SumSquare_x/nlines) << endl;
cout << "Sigma_y = " << sqrt(SumSquare_y/nlines) << endl;
cout << "Covariance = " << (Sum_xy/nlines) << endl;
cout << "Correlation = " << (Sum_xy/nlines)/(Sigma_x * Sigma_y) << endl;
//Draw h3 to visualize the fitting
h3->SetMarkerStyle(21);
h3->SetMarkerSize(0.8);
ntuple->SetMarkerStyle(21);
ntuple->SetMarkerSize(0.8);
gStyle->SetOptFit(1111);
TF1 *line0 = new TF1("line0","[0]*x ");
//line0 -> SetParameters();
profx = new TProfile("profx","profx",10,0,10);
//profx = (TProfile*) -> h3->ProfileX();
//h3->Fit("line0");
ntuple->Draw("y:x");
TH2D *histo = new TH2D("histo","my histo",10,0.,10.,100,0.,100.);
ntuple->Draw("y:x>>histo");
TProfile *histo_pfx = histo->ProfileX();
histo_pfx->Draw();
// histo_pfx->Fit("line0","IM","",1,10);
histo_pfx->Fit("pol1");
and here is a sample .dat file
#########################p116q1.dat##############
1 11
2 19
3 33
4 40
5 49
6 61
#########################################