This repository has been archived by the owner on Apr 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgetEventYields.py
57 lines (49 loc) · 1.81 KB
/
getEventYields.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import subprocess
import re
import time
import os
import ROOT
import json
txtFiles = sorted(os.listdir("/vols/build/cms/vc1117/CMSSW_10_2_0_pre6/src/samples/testing"))
processDict = {}
pileupHists = {}
for txtFile in txtFiles:
process = txtFile.split(".")[0]
if process.find("SingleEle")>=0 or process.find("SingleMu")>=0:
print "skip ",process
continue
processDict[process] = 0.
pileupHists[process] = ROOT.TH1F(process,"",101,0,100)
pileupHists[process].SetDirectory(0)
f = open ("/vols/build/cms/vc1117/CMSSW_10_2_0_pre6/src/samples/testing/"+txtFile)
print "reading ",txtFile, "...",
for l in f:
if len(l)>0:
fileName = l.replace("\n","").replace("\r","")
print "opening",fileName
rootFile=None
while (rootFile==None):
rootFile = ROOT.TFile.Open(fileName)
if not rootFile:
print "Cannot found file: ",rootFile, "-> retry"
continue
break
tree = rootFile.Get("Events")
if not rootFile or not tree:
print "Cannot found tree in file: ",rootFile
continue
h = ROOT.TH1F("pu","",101,0,100)
tree.Project(h.GetName(),"Pileup_nTrueInt","genWeight")
pileupHists[process].Add(h)
processDict[process] += h.Integral()
rootFile.Close()
#break
print processDict[process]
with open('eventyields.json', 'w') as outfile:
json.dump(processDict, outfile,ensure_ascii=True,indent=2,sort_keys=True)
rootFile = ROOT.TFile("pileup.root","RECREATE")
for process in pileupHists.keys():
pileupHists[process].SetDirectory(rootFile)
pileupHists[process].SetName(process)
pileupHists[process].Write()
rootFile.Close()