# Plotting the integral of a histogram

Hi all,

Given a collection of root files myrootfile_1.root, myrootfile_2.root,…, I plot muon_pt, stored in the tree mytree, as follows:

TChain ch(“mytree”);
TCanvas *C = new TCanvas();
TH1F *h = new TH1F(“h”, “”, nbins, x1, x2);
ch.Draw(“muon_pt>>h”);
C->SaveAs(“muon_pt.pdf”);

Starting from this plot, I would like to plot now the percentage of muons (on the y axis) as a function of muon_pt. In other words, I would like to plot I(muon_pt>=x), where I is the integral of the previous plot between x and +∞.

How can I do?
Thank you for your time.

Hi Mark,

what do you mean by

What previous plot?
What do you want to do exactly? Plot the number of muons with pT higher than in the current bin?

Note that

– that’s your original histogram.

Hi yus,

The y axis of my original histogram is the number of entries for bin. For example, for the 0 GeV < muon_pt < 0.1 GeV bin, there are 170 entries, for the 1.0 GeV < muon_pt < 1.1 GeV bin there are 48 entries, and so on.

Exactly, I would like to plot the number of muons with muon_pt larger then in the current in bin divided by the total number of muons. For example, the number of muons with muon_pt higher than 0 GeV, the number of muons with muon_pt higher than 0.1 GeV and so on.

Ah, okay, then try something like that in ROOT6 (won’t work in ROOT5):

``````TH1* h_cumulative = h->GetCumulative(false);//false because this is cumulative in a backward direction
h_cumulative->GetYaxis()->SetTitle("Percentage of muons with higher p_{T} [%]");
h_cumulative->Scale(100./h_cumulative->GetBinContent(1));//scale so that the first bin is at 100% no matter what
``````

Actually, you need ROOT 5.34/23 or newer.

Hello yus,

Thank you again for your help.

Starting from

how can I plot directly the cumulative histogram without plotting the original one?

I think you can tell ROOT not to plot your original histogram with

``````ch.Draw("muon_pt>>h", "", "goff");
``````

How I can do If I want to get a continuous line for the cumulative distribution instead of a stair histogram?

Try

``````h_cumulative->Draw("histC");
``````

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