Projectionx binning issue

Hi Rooters,

I have a question about the number of bins in TH2::ProjectionX()
I was under the impression that this should be preserved, but I am seeing that it’s changing.
For example I am getting the following output:
original bins x: 6
projection bins x: 12
In this code, I am plotting the TH2 in the top pad, the projection in the bottom pad:

import sys
import ROOT
import numpy as np
pidCatLabels = ["e", "mu", "pi", "K", "p", "d"]

def plotConfusionMatrix():
    hMax = ROOT.TH2D("hmax", "hmax", 100, 0, 1, 100, 0, 1)
    hMax.GetXaxis().SetTitle("maximum value of likelihood ratio")
    hMax.GetYaxis().SetTitle("difference to 2nd-highest value")

    pidCategories = ROOT.TH2D("pidcat", "pidcat", 6, 0, 6, 100, 0, 1)
    for ibin in range(6):
        pidCategories.GetXaxis().SetBinLabel(ibin + 1, pidCatLabels[ibin])
    pidCategories.GetYaxis().SetTitle("maximum value of likelihood ratio")

    nEntries = 10000
    pidVals = np.random.rand(10000, 6)
    for i in range(nEntries):
        event = []
        for idx, pid in enumerate(pidVals[i]):
            event.append((pid, idx))
        hMax.Fill(event[-1][0], event[-1][0] - event[-2][0])
        pidCategories.Fill(0.5 + event[-1][1], event[-1][0])
    c1 = ROOT.TCanvas()
    c2 = ROOT.TCanvas()
    # Upper plot will be in pad1
    pad1 = ROOT.TPad("pad1", "pad1", 0, 0.3, 1, 1.0)
    pad1.SetBottomMargin(0)  # // Upper and lower plot are joined
    pad1.Draw()  #             // Draw the upper pad: pad1  #               // pad1 becomes the current pad

    # lower plot will be in pad  #          // Go back to the main canvas before defining pad2
    pad2 = ROOT.TPad("pad2", "pad2", 0, 0.05, 1, 0.3)
    x = pidCategories.ProjectionX("pidnames", 0, -1)
    print("original bins x:", pidCategories.GetNbinsX())
    print("projection bins x:", x.GetNbinsX())
    c3 = ROOT.TCanvas()

if __name__ == "__main__":

I am confused by this. The binning in x should be the same for the two histograms. How can I achieve that?

ROOT 6.13/02
Built for macosx64
From tag v6-13-02, 20 March 2018

    x = pidCategories.ProjectionX("pidnames", 0, -1)

Thank you. Is there a reason why this is needed, or a rule when I have to call this and when not?

This method remove extra alphanumeric bins created at the end of the histogram when a label is added.
When a label is added there is more bins added to the histogram (to improve performances compare to add only the single bin needed). So when you think all the alphanumeric labels are in place, you should call this method.

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