Skip to content

Instantly share code, notes, and snippets.

@mcitron
Last active August 1, 2016 09:00
Show Gist options
  • Save mcitron/a6524f90262c66c8873ee2ebc904843c to your computer and use it in GitHub Desktop.
Save mcitron/a6524f90262c66c8873ee2ebc904843c to your computer and use it in GitHub Desktop.
import ROOT as r
class Transpose():
def transposeHist(self,inHist):
outName = inHist.GetName()
binWidthX = 2*(inHist.GetXaxis().GetBinCenter(1)-inHist.GetXaxis().GetBinLowEdge(1))
binWidthY = 2*(inHist.GetYaxis().GetBinCenter(1)-inHist.GetYaxis().GetBinLowEdge(1))
xMass = [inHist.GetXaxis().GetBinCenter(x) for x in range(1,inHist.GetXaxis().GetNbins()+1)]
yMass = [inHist.GetYaxis().GetBinCenter(y) for y in range(1,inHist.GetYaxis().GetNbins()+1)]
xLow = [inHist.GetXaxis().GetBinLowEdge(x) for x in range(1,inHist.GetXaxis().GetNbins()+1)]
yAxisTranspose = []
for x in xMass:
for y in yMass:
if x-y >= binWidthY/2.:
yAxisTranspose.append(x-y-binWidthY/2.)
minY = max(0,min(yAxisTranspose))
maxY = max(yAxisTranspose)
xAxis = array('d',xLow)
yValue = minY
yAxisTranspose = []
while yValue <= maxY+binWidthY:
yAxisTranspose.append(yValue)
yValue += binWidthY
yAxisTranspose = array('d',yAxisTranspose)
outHist = r.TH2D(outName+"_transpose","",len(xAxis)-1,xAxis,len(yAxisTranspose)-1,yAxisTranspose)
outHist.SetDirectory(0)
for x in range(1,outHist.GetXaxis().GetNbins()+1):
for y in range(1,outHist.GetYaxis().GetNbins()+1):
value = inHist.GetBinContent(x,y)
xValue,yValue = self.getBinCenter2D(x,y,inHist)
newMass = xValue - yValue
outHist.Fill(xValue,newMass,value)
outHist.SetName(outName)
return outHist
def transposeGraph(self,graph):
outputGraph = graph.Clone()
outputSize = graph.GetN()
outputX,outputY = array('d',[0.]*outputSize),array('d',[0.]*outputSize)
tempX,tempY = r.Double(0.),r.Double(0.)
for i in range(outputSize):
graph.GetPoint(i,tempX,tempY)
outputX[i],outputY[i] = tempX,tempX-tempY
outputGraph = r.TGraph(outputSize,outputX,outputY)
outputGraph.SetName(graph.GetName())
return outputGraph
def getBinCenter2D(self,x,y,hist):
xValue = hist.GetXaxis().GetBinCenter(x)
yValue = hist.GetYaxis().GetBinCenter(y)
return xValue,yValue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment