Manually setting bin content of histogram

I am manually setting the bin content and the bin error of the cern root histogram. But now the histogram shows 0 mean value and 0 std. dev. value in the stat box.

ROOT 6.26/14

Can you post the code you are using? ideally, a minimum reproducer. And also show what you have before and after getting the zeroes (I’m guessing the histogram had data before?). Without details we can’t know what’s wrong.

I just checked that the problem is due to setting the bin labels.
here is the code for reference

#include<iostream>
using namespace std;

void testcode() {
    TH1F *hist = new TH1F("", "", 10, 0, 10);
    for (int i = 0; i < 10; i++)
    {
        hist->SetBinContent(i+1, 5);
        hist->SetBinError(i+1, 0.5);
    }
     for (int i = 0; i < 10; i++)
    {
        hist->GetXaxis()->SetBinLabel(i + 1, Form("%d", i));
    }
    hist->Draw();
    
}

I have solved the problem with this code, but do you have any better option?

#include<iostream>
using namespace std;

void testcode() {
    TH1F *hist = new TH1F("", "", 10, 0, 10);
    for (int i = 0; i < 10; i++)
    {
        hist->SetBinContent(i+1, 5);
        hist->SetBinError(i+1, 0.5);
    }
    TH1F *histclone = (TH1F *)hist->Clone();
     for (int i = 0; i < 10; i++)
    {
        histclone->GetXaxis()->SetBinLabel(i + 1, Form("%d", i));
    }
    histclone->SetStats(0);
    // histclone->Draw();
    TCanvas *c1 = new TCanvas();
    hist->Draw();
    gPad->Update();
    TPaveStats *st = (TPaveStats *)hist->FindObject("stats");
    st->SetX1NDC(0.6);
    st->SetX2NDC(0.8);
    st->SetY1NDC(0.6);
    st->SetY2NDC(0.8);
    st->Draw();
    c1->Clone();

    TCanvas *c = new TCanvas();
    histclone->Draw();
    hist->Draw("same");
    st->Draw();
    
}

If the bins are strings (as in your example), how would you define a mean and a standard deviation?
So, use numbers as labels, or hide the stats, or take the stats from a second histogram like in your solution.

1 Like

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