-
Notifications
You must be signed in to change notification settings - Fork 2
/
createInputs.py
266 lines (204 loc) · 9.72 KB
/
createInputs.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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
import ROOT,sys
ROOT.gROOT.SetBatch(True)
ROOT.gErrorIgnoreLevel = 1
from ROOT import *
sys.path.append('cfgs/')
sys.path.append('input/')
def setIntegrator(ws,name):
config = RooNumIntConfig(ws.pdf(name).getIntegratorConfig())
config.method1D().setLabel("RooAdaptiveGaussKronrodIntegrator1D")
config.getConfigSection("RooAdaptiveGaussKronrodIntegrator1D").setCatLabel("method","61Points")
config.getConfigSection("RooAdaptiveGaussKronrodIntegrator1D").setRealValue("maxSeg",1000)
config.method1D().setLabel("RooAdaptiveGaussKronrodIntegrator1D")
config.getConfigSection("RooAdaptiveGaussKronrodIntegrator1D").setCatLabel("method","61Points")
config.getConfigSection("RooAdaptiveGaussKronrodIntegrator1D").setRealValue("maxSeg",1000)
ws.pdf(name).setIntegratorConfig(config)
def createSignalDataset(massVal,name,channel,width,nEvents,CB,tag=""):
#ROOT.gSystem.Load("shapes/ZPrimeted onine check-inMuonBkgPdf_cxx.so")
# ROOT.gSystem.AddIncludePath("-Ishapes"
ROOT.RooMsgService.instance().setGlobalKillBelow(RooFit.FATAL)
ROOT.RooRandom.randomGenerator().SetSeed(0)
import glob
for f in glob.glob("userfuncs/*.cxx"):
ROOT.gSystem.Load(f)
configName ="channelConfig_%s"%channel
config = __import__(configName)
dataFile = config.dataFile
ws = RooWorkspace("tempWS")
mass = RooRealVar('massFullRange','massFullRange',massVal, 200, 5000 )
getattr(ws,'import')(mass,ROOT.RooCmdArg())
peak = RooRealVar("peak","peak",massVal, 200,5000)
peak.setConstant()
getattr(ws,'import')(peak,ROOT.RooCmdArg())
### define signal shape
if CB:
ws.factory("BreitWigner::bw(massFullRange, peak, %.3f)"%(massVal*width))
ws.factory("RooCBShape::cb(massFullRange, mean[0.0], %.3f, alpha[1.43], n[3])"%(massVal*config.getResolution(massVal)))
bw = ws.pdf("bw")
cb = ws.pdf("cb")
mass.setBins(10000,"cache")
mass.setMin("cache",0)
mass.setMax("cache",6000); ## need to be adjusted to be higher than limit setting
sigpdf = RooFFTConvPdf("sig_pdf","sig_pdf",mass,bw,cb)
getattr(ws,'import')(sigpdf,ROOT.RooCmdArg())
else:
ws.factory("Voigtian::sig_pdf(massFullRange,peak,%.3f, %.3f)"%(massVal*width,massVal*config.getResolution(massVal)))
ws = config.loadBackgroundShape(ws)
with open(dataFile) as f:
masses = f.readlines()
nBkg = len(masses)
dataSet = ws.pdf("bkgpdf_fullRange").generate(ROOT.RooArgSet(ws.var("massFullRange")),nBkg)
if nEvents > 0:
nSignal = int(round(nEvents*config.signalEff(massVal)))
dataSet.append(ws.pdf("sig_pdf").generate(ROOT.RooArgSet(ws.var("massFullRange")),nSignal))
masses = []
for i in range(0,dataSet.numEntries()):
masses.append(dataSet.get(i).getRealValue("massFullRange"))
if "toy" in tag:
f = open("%s%s.txt"%(name,tag), 'w')
elif CB:
f = open("%s_%d_%.3f_%d_CB%s.txt"%(name,massVal,width,nEvents,tag), 'w')
else:
f = open("%s_%d_%.3f_%d%s.txt"%(name,massVal,width,nEvents,tag), 'w')
for mass in masses:
f.write("%.4f\n" % mass)
f.close()
def createWS(massVal,minNrEv,name,channel,width,correlateMass,dataFile="",CB=True,write=True):
ROOT.RooMsgService.instance().setGlobalKillBelow(RooFit.FATAL)
import glob
for f in glob.glob("userfuncs/*.cxx"):
ROOT.gSystem.Load(f)
configName ="channelConfig_%s"%channel
config = __import__(configName)
if dataFile == "":
dataFile = config.dataFile
if not correlateMass:
peakName = "_%s"%channel
else:
peakName = ""
effWidth = width + config.getResolution(massVal)
from tools import getMassRange
massLow, massHigh = getMassRange(massVal,minNrEv,effWidth,dataFile,200)
ws = RooWorkspace(channel)
massFullRange = RooRealVar('massFullRange','massFullRange',massVal, 200, 5000 )
getattr(ws,'import')(massFullRange,ROOT.RooCmdArg())
mass = RooRealVar('mass_%s'%channel,'mass_%s'%channel,massVal, massLow, massHigh )
getattr(ws,'import')(mass,ROOT.RooCmdArg())
peak = RooRealVar("peak%s"%peakName,"peak%s"%peakName,massVal, massLow, massHigh)
peak.setConstant()
getattr(ws,'import')(peak,ROOT.RooCmdArg())
### mass scale uncertainty defined on peak position
beta_peak = RooRealVar('beta_peak%s'%peakName,'beta_peak%s'%peakName,0,-5,5)
getattr(ws,'import')(beta_peak,ROOT.RooCmdArg())
scaleUncert = 1. + config.provideUncertainties(massVal)["massScale"]
peak_kappa = RooRealVar('peak%s_kappa'%peakName,'peak%s_kappa'%peakName,scaleUncert)
peak_kappa.setConstant()
getattr(ws,'import')(peak_kappa,ROOT.RooCmdArg())
ws.factory("PowFunc::peak_nuis%s(peak%s_kappa, beta_peak%s)"%(peakName,peakName,peakName))
ws.factory("prod::peak_scaled%s(peak%s, peak_nuis%s)"%(peakName,peakName,peakName))
if CB:
ws.factory("BreitWigner::bw(mass_%s, peak_scaled%s, %.3f)"%(channel,peakName,massVal*width))
ws.factory("RooCBShape::cb(mass_%s, mean[0.0], %.3f, alpha[1.43], n[3])"%(channel,massVal*config.getResolution(massVal)))
bw = ws.pdf("bw")
cb = ws.pdf("cb")
mass.setBins(10000,"cache")
mass.setMin("cache",0)
mass.setMax("cache",6000); ## need to be adjusted to be higher than limit setting
sigpdf = RooFFTConvPdf("sig_pdf_%s"%channel,"sig_pdf_%s"%channel,mass,bw,cb)
getattr(ws,'import')(sigpdf,ROOT.RooCmdArg())
else:
ws.factory("Voigtian::sig_pdf_%s(mass_%s, peak_scaled%s, %.3f, %.3f)"%(channel,channel,peakName,massVal*width,massVal*config.getResolution(massVal)))
setIntegrator(ws,'sig_pdf_%s'%channel)
ws = config.loadBackgroundShape(ws)
setIntegrator(ws,'bkgpdf_fullRange')
setIntegrator(ws,'bkgpdf_%s'%channel)
ds = RooDataSet.read(dataFile,RooArgList(mass))
ds.SetName('data_%s'%channel)
ds.SetTitle('data_%s'%channel)
getattr(ws,'import')(ds,ROOT.RooCmdArg())
ws.addClassDeclImportDir("shapes/")
ws.importClassCode()
if config.nBkg == -1:
with open(dataFile) as f:
masses = f.readlines()
nBkgTotal = len(masses)
else:
nBkgTotal = config.nBkg
if write:
ws.writeToFile("%s.root"%name,True)
from tools import getBkgEstInWindow
return getBkgEstInWindow(ws,massLow,massHigh,nBkgTotal)
else:
return ws
def getBinning(mass):
if mass < 700:
return [1,1000000]
if mass < 1000:
return [1,1000000]
elif mass < 2000:
return [2,1000000]
else:
return [5,500000]
def createHistograms(massVal,minNrEv,name,channel,width,correlateMass,binWidth,dataFile="",CB=True):
ROOT.RooMsgService.instance().setGlobalKillBelow(RooFit.FATAL)
configName ="channelConfig_%s"%channel
config = __import__(configName)
if dataFile == "":
dataFile = config.dataFile
effWidth = width + config.getResolution(massVal)
if config.nBkg == -1:
with open(dataFile) as f:
masses = f.readlines()
nBkgTotal = len(masses)
else:
nBkgTotal = config.nBkg
from tools import getMassRange
massLow, massHigh = getMassRange(massVal,minNrEv,effWidth,dataFile,200)
if not correlateMass:
peakName = "_%s"%channel
else:
peakName = ""
ws = createWS(massVal,minNrEv,name,channel,width,correlateMass,dataFile=dataFile,CB=CB,write=False)
from tools import getBkgEstInWindow
nBackground = getBkgEstInWindow(ws,massLow,massHigh,nBkgTotal)
binWidth = getBinning(massVal)[0]
numEvents = getBinning(massVal)[1]
nBins = int((massHigh - massLow)/binWidth)
ws.var("mass_%s"%channel).setBins(nBins)
histFile = ROOT.TFile("%s.root"%name, "RECREATE")
scaleName = "scale"
if not correlateMass:
scaleName +="_%s"%channel
scaleUncert = config.provideUncertainties(massVal)["massScale"]
sigShape = ws.pdf("sig_pdf_%s"%channel).generate(ROOT.RooArgSet(ws.var("mass_%s"%channel)),numEvents)
ws.var("peak%s"%peakName).setVal(massVal*(1+scaleUncert))
sigShapeUp = ws.pdf("sig_pdf_%s"%channel).generate(ROOT.RooArgSet(ws.var("mass_%s"%channel)),numEvents)
ws.var("peak%s"%peakName).setVal(massVal*(1-scaleUncert))
sigShapeDown = ws.pdf("sig_pdf_%s"%channel).generate(ROOT.RooArgSet(ws.var("mass_%s"%channel)),numEvents)
sigHistRooFit = ROOT.RooDataHist("sigHist_%s"%channel, "sigHist_%s"%channel, ROOT.RooArgSet(ws.var('mass_%s'%channel)), sigShape)
sigHist = sigHistRooFit.createHistogram("sigHist_%s"%channel,ws.var("mass_%s"%channel))
sigHist.SetName("sigHist_%s"%channel)
sigHistRooFitUp = ROOT.RooDataHist("sigHist_%s_%sUp"%(channel,scaleName), "sigHist_%s_%sUp"%(channel,scaleName), ROOT.RooArgSet(ws.var('mass_%s'%channel)), sigShapeUp)
sigHistUp = sigHistRooFitUp.createHistogram("sigHist_%s_%sUp"%(channel,scaleName),ws.var("mass_%s"%channel))
sigHistUp.SetName("sigHist_%s_%sUp"%(channel,scaleName))
sigHistRooFitDown = ROOT.RooDataHist("sigHist_%s_%sDown"%(channel,scaleName), "sigHist_%s_%sDown"%(channel,scaleName), ROOT.RooArgSet(ws.var('mass_%s'%channel)), sigShapeDown)
sigHistDown = sigHistRooFitDown.createHistogram("sigHist_%s_%sDown"%(channel,scaleName),ws.var("mass_%s"%channel))
sigHistDown.SetName("sigHist_%s_%sDown"%(channel,scaleName))
sigHist.Scale(1./(sigHist.Integral())*config.provideSignalScaling(massVal)*1e-7)
sigHistUp.Scale(1./(sigHistUp.Integral())*config.provideSignalScaling(massVal)*1e-7)
sigHistDown.Scale(1./(sigHistDown.Integral())*config.provideSignalScaling(massVal)*1e-7)
bkgShape = ws.pdf("bkgpdf_%s"%channel).generate(ROOT.RooArgSet(ws.var("mass_%s"%channel)),numEvents)
bkgHistRooFit = ROOT.RooDataHist("bkgHist_%s"%channel, "bkgHist_%s"%channel, ROOT.RooArgSet(ws.var('mass_%s'%channel)), bkgShape)
bkgHist = bkgHistRooFit.createHistogram("bkgHist_%s"%channel,ws.var("mass_%s"%channel))
bkgHist.SetName("bkgHist_%s"%channel)
bkgHist.Scale(1./(bkgHist.Integral())*nBackground)
dataHist = ROOT.TH1F("data_%s"%channel,"data_%s"%channel,nBins,massLow,massHigh)
with open(dataFile) as f:
masses = f.readlines()
for mass in masses:
mass = float(mass)
if (mass >= massLow and mass <= massHigh):
dataHist.Fill(mass)
histFile.Write()
histFile.Close()
return nBackground