-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathCloneSample.py
More file actions
169 lines (136 loc) · 5.45 KB
/
CloneSample.py
File metadata and controls
169 lines (136 loc) · 5.45 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#
# Code to remove one sample from the datacard
#
import re
from sys import argv
import os.path
from optparse import OptionParser
from math import sqrt,fabs
parser = OptionParser()
parser.add_option("-i", "--input", dest="nameFileChange", help="file with samples to remove (e.g. ttH)", default='blabla.py')
parser.add_option("-o", "--output", dest="nameOutFileDC", help="file where to dump the new DC", default='test.txt')
(options, args) = parser.parse_args()
options.bin = True # fake that is a binary output, so that we parse shape lines
options.noJMax = False
options.nuisancesToExclude = ''
options.stat = False
nameFactor = {}
if os.path.exists(options.nameFileChange):
handle = open(options.nameFileChange,'r')
exec(handle)
handle.close()
print "nameFactor = ", nameFactor
import sys
sys.path.append('tools')
from DatacardParser import *
DC = parseCard(file(args[0]), options)
nuisToConsider = [ y for y in DC.systs ]
# copy header
# everything up to "observation", included
lines = open (args[0], 'r').read().split ('\n')
header = []
foundObservation = 0
for line in lines:
if (foundObservation == 0) : header.append (line)
tempLine = line.split (' ')
tempLine = filter(lambda a: a != '', tempLine)
if len(tempLine)>0 and tempLine[0] == 'observation' :
foundObservation = 1
print header
# write new datacard
filename = options.nameOutFileDC
f = open(filename, 'w')
# header
for line in header: f.write (line + '\n')
f.write ("---------------------------------------------------------------------------------------------------- \n")
# bin name
f.write ("bin ")
for channel in DC.exp:
for process in DC.exp[channel]:
f.write ("%13s " % channel)
if (channel in nameFactor.keys()) : # in this channel ther are some processes to clone
for processTocClone in nameFactor[channel]:
f.write ("%13s " % channel)
f.write("\n")
# process names (a.k.a. process)
f.write ("process ")
for channel in DC.exp:
for process in DC.exp[channel]:
f.write ("%13s " % process)
if (channel in nameFactor.keys()) : # in this channel ther are some processes to clone
for processTocClone in nameFactor[channel]:
f.write ("%13s " % processTocClone[1])
f.write("\n")
# indices numbers: -1 0 1 2 3 ...
f.write ("process ")
for channel in DC.exp:
numSig = 0
numBkg = 1
for process in DC.exp[channel]:
if DC.isSignal[process] :
f.write ("%13d " % numSig)
numSig = numSig - 1
else :
f.write ("%13d " % numBkg)
numBkg = numBkg + 1
if (channel in nameFactor.keys()) : # in this channel ther are some processes to clone
for processTocClone in nameFactor[channel]:
if DC.isSignal[processTocClone[0]] :
f.write ("%13d " % numSig)
numSig = numSig - 1
else :
f.write ("%13d " % numBkg)
numBkg = numBkg + 1
f.write("\n")
# rate
f.write ("rate ")
for channel in DC.exp:
for process in DC.exp[channel]:
f.write ("%9.4f " % DC.exp[channel][process] )
if (channel in nameFactor.keys()) : # in this channel ther are some processes to clone
for processTocClone in nameFactor[channel]:
f.write ("%9.4f " % DC.exp[channel][processTocClone[0]] )
f.write("\n")
# systematics
## list of [(name of uncert, boolean to indicate whether to float this nuisance or not, type, list of what additional arguments (e.g. for gmN), keyline element)]
for nuis in nuisToConsider:
f.write ("%25s" % nuis[0]) # name
if nuis[2] != 'gmN':
f.write ("%10s" % nuis[2]) #lnN
else :
f.write ("%5s" % nuis[2]) # gmN
f.write ("%5s" % nuis[3][0]) # Ncontrol
f.write (" ")
for channel in DC.exp:
for process in DC.exp[channel]:
if channel in nuis[4]:
if process in nuis[4][channel] :
if not isinstance ( nuis[4][channel][process], float ) :
# [0.95, 1.23] ---> from 0.95/1.23
f.write (" {0:4.3f}/{1:4.3f}".format(nuis[4][channel][process][0], nuis[4][channel][process][1]))
else :
if (nuis[4][channel][process] != 0) :
f.write ("%9.4f" % nuis[4][channel][process])
else :
f.write ("%13s" % "-")
else :
f.write ("%13s" % "-")
else :
f.write ("%13s" % "-")
if (channel in nameFactor.keys()) : # in this channel ther are some processes to clone
for processTocClone in nameFactor[channel]:
if processTocClone[0] in nuis[4][channel] :
if not isinstance ( nuis[4][channel][processTocClone[0]], float ) :
# [0.95, 1.23] ---> from 0.95/1.23
f.write (" {0:4.3f}/{1:4.3f}".format(nuis[4][channel][processTocClone[0]][0], nuis[4][channel][processTocClone[0]][1]))
else :
if (nuis[4][channel][processTocClone[0]] != 0) :
f.write ("%9.4f" % nuis[4][channel][processTocClone[0]])
else :
f.write ("%13s" % "-")
else :
f.write ("%13s" % "-")
else :
f.write ("%13s" % "-")
f.write("\n")
f.close ()