-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHistoWeight.py
executable file
·66 lines (51 loc) · 1.5 KB
/
HistoWeight.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
import os
import array
import glob
import math
import ROOT
import sys
from ROOT import *
from array import *
from optparse import OptionParser
parser = OptionParser()
parser.add_option('-i', '--inputf', metavar='F', type='string', action='store',
default = 'NULL_INPUT',
dest = 'inputf',
help = 'input root file')
parser.add_option('-w', '--weight', metavar='F', type='float', action='store',
default=1.0,
dest='weight',
help='weight')
parser.add_option('-o', '--outputf', metavar='F', type='string', action='store',
default = 'NULL_OUTPUT',
dest = 'outputf',
help = 'output root file')
(options, args) = parser.parse_args()
infile = ROOT.TFile(options.inputf)
output = ROOT.TFile( options.outputf, "recreate" )
D = infile.GetListOfKeys()
weight = options.weight
try :
reweight = array('d',[0.])
weighttree = infile.Get('Weight')
weighttree.SetBranchAddress("weightv",reweight)
weighttree.GetEntry(0)
print "reweighting. Previous weight " + str(reweight[0])
except :
reweight =array('d',[1.])
for i in range(0,len(D)):
a = D[i].ReadObj()
try:
a.Scale(weight/reweight[0])
a.Write()
except:
if a.GetName() != 'Weight':
print "not scaling " + str(a.GetName())
a.CloneTree().Write()
weightv = array('d',[weight])
t = TTree("Weight", "Weight");
t.Branch('weightv',weightv, "weightv/D")
#weightv = weight
t.Fill()
t.Write()
output.Close()