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))
event.sort()
hMax.Fill(event[-1][0], event[-1][0] - event[-2][0])
pidCategories.Fill(0.5 + event[-1][1], event[-1][0])
c1 = ROOT.TCanvas()
hMax.Draw()
c1.SaveAs("maximumPIDValues.pdf")
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.cd() # // pad1 becomes the current pad
pidCategories.SetMinimum(0.16)
pidCategories.Draw("colz")
# lower plot will be in pad
c2.cd() # // Go back to the main canvas before defining pad2
pad2 = ROOT.TPad("pad2", "pad2", 0, 0.05, 1, 0.3)
pad2.SetTopMargin(0)
pad2.SetBottomMargin(0.2)
pad2.Draw()
pad2.cd()
x = pidCategories.ProjectionX("pidnames", 0, -1)
print("original bins x:", pidCategories.GetNbinsX())
print("projection bins x:", x.GetNbinsX())
x.Draw()
c2.SaveAs("highestPIDCategory.pdf")
c3 = ROOT.TCanvas()
x.Draw()
c3.SaveAs("c3.pdf")
if __name__ == "__main__":
plotConfusionMatrix()
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