Adding data with SetBinContent

Hello everyone,

I have made a 2D histogram like so

TH2D* h = new TH2D("h","h",20,-10,10,20,-10,10);

and I have added some point

    h->SetBinContent(10,10,1);
    h->SetBinContent(10,10,1);
    h->SetBinContent(10,10,2);

I am expecting to see that the histogram to have 4 hits, however it is only taking the last entry of “2”, why is it not adding the hits? is there a way to do that?

SetBinContent, as its name says, replaces the content of the bin by the given value. What you are looking for is Fill

How do I use Fill in this situation, when I have the x bin and y bin and the content?

This is quite clearly explained in the reference guide in the ROOT Manual in the ROOT Primer and in the many histograms’ tutorials. Basically you should do:

    h->Fill(10,10,1);
    h->Fill(10,10,1);
    h->Fill(10,10,2);

Thanks that worked

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