-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathseparateTestingTrainingTrees.py
executable file
·74 lines (51 loc) · 2.65 KB
/
separateTestingTrainingTrees.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import ROOT, math, numpy
import sys
def separate(initial,target,filename,inputTree, outfilename):
print "opening file pfClusters_%s.root" %(filename)
#outputFile = ROOT.TFile.Open('pfClusters_%s_%s.root' %(filename,outfilename), 'RECREATE')
# outputFile = ROOT.TFile.Open('eos/cms/store/group/phys_egamma/PFClusteRegressionTrees/pfClusters_%s_%s.root' %(filename,outfilename), 'RECREATE')
# outputFile = ROOT.TFile.Open('eos/cms/store/group/phys_egamma/PFClusteRegressionTrees/afterDebug_16june/pfClusters_%s_%s.root' %(filename,outfilename), 'RECREATE')
outputFile = ROOT.TFile.Open('/eos/cms/store/group/phys_egamma/PFClusterCalibration/MC18_V2/FlatTrees/pfClusters_%s_%s.root' %(filename,outfilename), 'RECREATE')
outputFile.mkdir('een_analyzer')
outputFile.cd('een_analyzer')
nentries = int(inputTree.GetEntries())
print "Total entries in original tree ",nentries
print "For training tree, copying ",target-initial, " entries"
#outputTree = inputTree.CopyTree('','',target,0)
#outputTree.Write('PfTree')
#outputFile.Close()
outputTree = inputTree.CloneTree(0)
# print "Now writing the branch weight in the output file"
weight = numpy.zeros(1, dtype=float)
outputTree.Branch('weight',weight,'weight/D')
# print "Entries in output tree ", outputTree.GetEntries()
###0 till target-1
for ev in range(initial,target):
if (inputTree.GetEntry(ev) <= 0):
raise Exception('TTree::GetEntry() failed')
weight[0] = (1.0/(target-initial))
# print "weight is ",weight[0]
outputTree.Fill()
print "Looped over the output treee"
outputFile.cd('een_analyzer')
outputTree.Write('PfTree')
###################################################################3
####training tree
#inputFile = ROOT.TFile.Open('root://eoscms.cern.ch//eos/cms/store/group/phys_egamma/PFClusteRegressionTrees/pfClusters_%s.root' % sys.argv[1])
#inputFile = ROOT.TFile.Open('root://eoscms.cern.ch//eos/cms/store/group/phys_egamma/PFClusteRegressionTrees/afterDebug_16june/pfClusters_%s.root' % sys.argv[1])
print "input file is /eos/cms/store/group/phys_egamma/PFClusterCalibration/MC18_V2/FlatTrees/pfClusters_%s.root", sys.argv[1]
inputFile = ROOT.TFile.Open('/eos/cms/store/group/phys_egamma/PFClusterCalibration/MC18_V2/FlatTrees/pfClusters_%s.root' % sys.argv[1])
inputTree = inputFile.Get('een_analyzer/PfTree')
nentries = int(inputTree.GetEntries())
####training tree
initial = 0
target = int(3.*nentries/4.)
filename = sys.argv[1]
outfilename = "training"
separate(initial,target,filename,inputTree,outfilename)
####training tree
initial = int(3.*nentries/4.)
target = nentries
filename = sys.argv[1]
outfilename = "testing"
separate(initial,target,filename,inputTree,outfilename)