Fill histogram with values and their uncertainities

Is there any function of Histogram in root to fill it with values and their uncertainties?
Like I want to plot these values on y axis : 0.44519 ± 0.00054353 , 0.77636 ± 0.0014048 etc.

Hi,
To plot data points with their errors, you can use the TH1 or TGraphErrors class.
Here is an example using two data points, that you can easily extend to more.
Note that I have increase your error for plotting purpose. In your case they are too small and they will not be visible.

{
   
    std::vector<double>y = {0.44519, 0.77636};
    std::vector<double> ey = { 0.05 , 0.01};  
    int n = y.size();
    auto h = new TH1D("h","Data Points with errors",n, 0, double(n));
    for (int i = 0; i < n; i++) {
         h->SetBinContent(i+1, y[i]);  
         h->SetBinError(i+1, ey[i]);   
    }
    h->SetMarkerStyle(20);
    h->Draw("EX0");
}

Ok, thank you.
Now if i want to input data from a file which contains :
x y ey
0.5 0.44519 0.00054353
0.6 0.56459 0.0054027 … (16 lines total)

I dont get a desired histogram where I want ‘x’ values on the x axis and error bars with ‘y’ on the yaxis.
How can I modify this code :

 double x, y,ey;   
    fstream file;
    string line;
    file.open("pmean",ios::in);
    
    auto h = new TH2D("h","Data Points with errors",16, 0., 16.0,15,0.,15.);
    
    for(int i=0;i<16;i++) {
    	 file >> x >> y >> ey;
    	 h->Fill(x,y);
         h->SetBinContent(i+2, y);  
         h->SetBinError(i+1, ey);  
    }
    h->SetMarkerStyle(20);
    h->Draw("EXO");
    file.close();

You better use TGraphErrors with this constructor I guess.

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