-
Notifications
You must be signed in to change notification settings - Fork 0
/
rootConversionUtils.py
37 lines (25 loc) · 1.57 KB
/
rootConversionUtils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import ROOT,json
from collections import OrderedDict
import os
def writeJSON(sfDict,f):
json.dump(sfDict,f,sort_keys=False,indent=4)
def readFile(filename, th2name, idname, datasetname):
idDict = OrderedDict() #OrderedDict keeps the order in which elements are inserted into the dictionary
sfDict = OrderedDict()
print('opening ', filename)
print('get th2: ',th2name)
rootfile = ROOT.TFile.Open(filename)
th2 = rootfile.Get(th2name)
nEtaBins = th2.GetNbinsX()
nPtBins = th2.GetNbinsY()
etaAxis = th2.GetXaxis()
ptAxis = th2.GetYaxis()
sfDict[idname]=OrderedDict()
for iPt in range(1,nPtBins+1):# do i need +1?
sfDict[idname]["pt:["+str(ptAxis.GetBinLowEdge(iPt))+","+str(ptAxis.GetBinUpEdge(iPt))+"]"] = OrderedDict()
for iEta in range(1,nEtaBins+1):
sfDict[idname]["pt:["+str(ptAxis.GetBinLowEdge(iPt))+","+str(ptAxis.GetBinUpEdge(iPt))+"]"]["eta:["+str(etaAxis.GetBinLowEdge(iEta))+","+str(etaAxis.GetBinUpEdge(iEta))+"]"] = OrderedDict()
sfDict[idname]["pt:["+str(ptAxis.GetBinLowEdge(iPt))+","+str(ptAxis.GetBinUpEdge(iPt))+"]"]["eta:["+str(etaAxis.GetBinLowEdge(iEta))+","+str(etaAxis.GetBinUpEdge(iEta))+"]"]["value"] = th2.GetBinContent(iEta,iPt)
sfDict[idname]["pt:["+str(ptAxis.GetBinLowEdge(iPt))+","+str(ptAxis.GetBinUpEdge(iPt))+"]"]["eta:["+str(etaAxis.GetBinLowEdge(iEta))+","+str(etaAxis.GetBinUpEdge(iEta))+"]"]["error"] = th2.GetBinError(iEta,iPt)
f = open(datasetname+str("_")+idname+str(".json"),'w')
writeJSON(sfDict,f)