-
Notifications
You must be signed in to change notification settings - Fork 20
/
splitNTUP.py
51 lines (39 loc) · 1.54 KB
/
splitNTUP.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
import ROOT
import os
import sys
eventsPerFile = 250
def splitFile(inDir, oldFileName):
# Get old file, old tree and set top branch address
dirname = "ana"
prefix = "root://eoscms.cern.ch/"
newFileName = oldFileName.strip(".root")
oldFileFullName = "{}/{}/{}".format(prefix, inDir, oldFileName)
print "Splitting", oldFileFullName
oldfile = ROOT.TFile(oldFileFullName)
oldtree = oldfile.Get("{}/hgc".format(dirname))
nentries = oldtree.GetEntries()
if nentries > eventsPerFile:
oldtree.SetBranchStatus("*", 1)
# Create a new file + a clone of old tree in new file
index = 0
for i in range(0, nentries, eventsPerFile):
print "Event", i, "of", nentries
newfile = ROOT.TFile("{}/{}/{}_{}.root".format(prefix, inDir, newFileName, index), "recreate")
newfile.mkdir(dirname)
newfile.cd(dirname)
newtree = oldtree.CopyTree("", "", eventsPerFile, i)
newtree.AutoSave()
newfile.Write()
newfile.Close()
index += 1
print "Deleting {}/{}".format(inDir, oldFileName)
os.remove("{}/{}".format(inDir, oldFileName))
else:
print "No need to split", oldFileFullName
def main():
inDir = sys.argv[1]
# /eos/cms/store/cmst3/group/hgcal/CMG_studies/Production/_SinglePiPt10Eta1p6_2p8_PhaseIITDRFall17DR-noPUFEVT_93X_upgrade2023_realistic_v2-v1_GEN-SIM-RECO/NTUP
for rootFile in os.listdir(inDir):
splitFile(inDir, rootFile)
if __name__ == '__main__':
main()