/
importBsStuff.py
executable file
·431 lines (369 loc) · 15.5 KB
/
importBsStuff.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
#!/bin/sh
# -*- mode: python; coding: utf-8 -*-
# vim: ft=python:sw=4:tw=78:expandtab
# ---------------------------------------------------------------------------
# @file importBsTau.py
#
# @import BsTau data, plot and determine lifetime
#
# per-event mistag, average sigma_t, spline acceptance
#
# @author Manuel Schiller
# @date 2012-07-08
# ---------------------------------------------------------------------------
# This file is used as both a shell script and as a Python script.
""":"
# This part is run by the shell. It does some setup which is convenient to save
# work in common use cases.
# make sure the environment is set up properly
if test -n "$CMTCONFIG" \
-a -f $B2DXFITTERSROOT/$CMTCONFIG/libB2DXFittersDict.so \
-a -f $B2DXFITTERSROOT/$CMTCONFIG/libB2DXFittersLib.so; then
# all ok, software environment set up correctly, so don't need to do
# anything
true
else
if test -n "$CMTCONFIG"; then
# clean up incomplete LHCb software environment so we can run
# standalone
echo Cleaning up incomplete LHCb software environment.
PYTHONPATH=`echo $PYTHONPATH | tr ':' '\n' | \
egrep -v "^($User_release_area|$MYSITEROOT/lhcb)" | \
tr '\n' ':' | sed -e 's/:$//'`
export PYTHONPATH
LD_LIBRARY_PATH=`echo $LD_LIBRARY_PATH | tr ':' '\n' | \
egrep -v "^($User_release_area|$MYSITEROOT/lhcb)" | \
tr '\n' ':' | sed -e 's/:$//'`
export LD_LIBRARY_PATH
exec env -u CMTCONFIG -u B2DXFITTERSROOT "$0" "$@"
fi
# automatic set up in standalone build mode
if test -z "$B2DXFITTERSROOT"; then
cwd="$(pwd)"
# try to find from where script is executed, use current directory as
# fallback
tmp="$(dirname $0)"
tmp=${tmp:-"$cwd"}
# convert to absolute path
tmp=`readlink -f "$tmp"`
# move up until standalone/setup.sh found, or root reached
while test \( \! -d "$tmp"/standalone \) -a -n "$tmp" -a "$tmp"\!="/"; do
tmp=`dirname "$tmp"`
done
if test -d "$tmp"/standalone; then
cd "$tmp"/standalone
. ./setup.sh
else
echo `basename $0`: Unable to locate standalone/setup.sh
exit 1
fi
cd "$cwd"
unset tmp
unset cwd
fi
fi
# set batch scheduling (if schedtool is available)
schedtool="`which schedtool 2>/dev/zero`"
if test -n "$schedtool" -a -x "$schedtool"; then
echo "enabling batch scheduling for this job (schedtool -B)"
schedtool="$schedtool -B -e"
else
schedtool=""
fi
# set ulimit to protect against bugs which crash the machine: 3G vmem max,
# no more then 8M stack
ulimit -v $((4096 * 1024))
ulimit -s $(( 8 * 1024))
# trampoline into python
exec $schedtool /usr/bin/time -v env python -O "$0" - "$@"
"""
__doc__ = """ real docstring """
import ROOT
from ROOT import TFile, RooFit, TTree, RooDataSet, RooArgSet, RooRealVar, TCanvas, RooWorkspace
from B2DXFitters.WS import WS
import sys,os,time
def buildTimePdf(config, tupleDataSet, tupleDict):
from B2DXFitters.WS import WS
print 'CONFIGURATION'
for k in sorted(config.keys()):
print ' %32s: %32s' % (k, config[k])
ws = RooWorkspace('ws_%s' % config['Context'])
one = WS(ws, RooConstVar('one', '1', 1.0))
zero = WS(ws, RooConstVar('zero', '0', 0.0))
###USE FIT CONTEXT
"""
build time pdf, return pdf and associated data in dictionary
"""
# start by defining observables
time = WS(ws, tupleDataSet.get().find('ct'));
#qt = WS(ws, tupleDataSet.get().find('ssDecision'));
'''
time = WS(ws, RooRealVar('time', 'time [ps]', 0.2, 15.0))
'''
qf = WS(ws, RooCategory('qf', 'final state charge'))
qf.defineType('h+', +1)
qf.defineType('h-', -1)
qt = WS(ws, RooCategory('qt', 'tagging decision'))
qt.defineType( 'B+', +1)
qt.defineType('Untagged', 0)
qt.defineType( 'B-', -1)
# now other settings
Gamma = WS(ws, RooRealVar( 'Gamma', 'Gamma', 0.661)) # ps^-1
DGamma = WS(ws, RooRealVar('DGamma', 'DGamma', 0.106)) # ps^-1
Dm = WS(ws, RooRealVar( 'Dm', 'Dm', 17.719)) # ps^-1
# HACK (1/2): be careful about lower bound on eta, since mistagpdf below
# is zero below a certain value - generation in accept/reject would get
# stuck
if 'GEN' in config['Context'] or 'FIT' in config['Context']:
eta = WS(ws, RooRealVar('eta', 'eta', 0.35,
0.0 if 'FIT' in config['Context'] else (1. + 1e-5) * max(0.0,
config['TrivialMistagParams']['omega0']), 0.5))
mistag = WS(ws, RooRealVar('mistag', 'mistag', 0.35, 0.0, 0.5))
tageff = WS(ws, RooRealVar('tageff', 'tageff', 0.60, 0.0, 1.0))
terrpdf = WS(ws, tupleDataSet.get().find('cterr'));
timeerr = WS(ws, RooRealVar('timeerr', 'timeerr', 0.040, 0.001, 0.100))
#MISTAGPDF
# fit average mistag
# add mistagged
#ge rid of untagged events by putting restriction on qf or something when reduceing ds
# now build the PDF
from B2DXFitters.timepdfutils import buildBDecayTimePdf
from B2DXFitters.resmodelutils import getResolutionModel
from B2DXFitters.acceptanceutils import buildSplineAcceptance
obs = [ qf, qt, time]
acc, accnorm = buildSplineAcceptance(ws, time, 'Bs2DsPi_accpetance',
config['SplineAcceptance']['KnotPositions'],
config['SplineAcceptance']['KnotCoefficients'][config['Context'][0:3]],
'FIT' in config['Context']) # float for fitting
# get resolution model
resmodel, acc = getResolutionModel(ws, config, time, timeerr, acc)
mistagpdf = WS(ws,RooArgList(tupleDataSet.get().find('ssMistag'),tupleDataSet.get().find('osMistag')));#???
'''
if 'GEN' in config['Context']:
# build a (mock) mistag distribution
mistagpdfparams = {} # start with parameters of mock distribution
for sfx in ('omega0', 'omegaavg', 'f'):
mistagpdfparams[sfx] = WS(ws, RooRealVar(
'Bs2DsPi_mistagpdf_%s' % sfx, 'Bs2DsPi_mistagpdf_%s' % sfx,
config['TrivialMistagParams'][sfx]))
# build mistag pdf itself
mistagpdf = WS(ws, [tupleDataSet.reduce('ssMistag'), tupleDataSet.reduce('osMistag')]);
mistagcalibparams = {} # start with parameters of calibration
for sfx in ('p0', 'p1', 'etaavg'):
mistagcalibparams[sfx] = WS(ws, RooRealVar('Bs2DsPi_mistagcalib_%s' % sfx, 'Bs2DsPi_mistagpdf_%s' % sfx,config['MistagCalibParams'][sfx]));
for sfx in ('p0', 'p1'): # float calibration paramters
mistagcalibparams[sfx].setConstant(False)
mistagcalibparams[sfx].setError(0.1)
# build mistag pdf itself
omega = WS(ws, MistagCalibration(
'Bs2DsPi_mistagcalib', 'Bs2DsPi_mistagcalib',
eta, mistagcalibparams['p0'], mistagcalibparams['p1'],
mistagcalibparams['etaavg']))
# build the time pdf
if 'GEN' in config['Context']:
pdf = buildBDecayTimePdf(
config, 'Bs2DsPi', ws,
time, timeerr, qt, qf, [ [ omega ] ], [ tageff ],
Gamma, DGamma, Dm,
C = one, D = zero, Dbar = zero, S = zero, Sbar = zero,
timeresmodel = resmodel, acceptance = acc, timeerrpdf,
mistagpdf = [mistagpdf], mistagobs = eta)
else:
pdf = buildBDecayTimePdf(
config, 'Bs2DsPi', ws,
time, timeerr, qt, qf, [ [ eta ] ], [ tageff ],
Gamma, DGamma, Dm,
C = one, D = zero, Dbar = zero, S = zero, Sbar = zero,
timeresmodel = resmodel, acceptance = acc, timeerrpdf = None)
'''
pdf = buildBDecayTimePdf(
config, 'Bs2DsPi', ws,
time, timeerr, qt, qf, [ [ eta ] ], [ tageff ],
Gamma, DGamma, Dm,
C = one, D = zero, Dbar = zero, S = zero, Sbar = zero,
timeresmodel = resmodel, acceptance = acc, timeerrpdf = terrpdf,
mistagpdf = [mistagpdf], mistagobs = eta)
return { # return things
'ws': ws,
'pdf': pdf,
'obs': obs
}
def importTupleDict(name):
try:
lines = file(name, 'r').readlines()
except:
print "Reading tuple dictionary from file failed."
sys.exit(1)
if name != None:
where = name
else:
where = 'unknown location'
s = ''.join(lines)
d = eval(compile(s, where, 'eval'))
if dict != type(d):
raise TypeError('configuration from %s does not evaluate to dictionary' % where)
return d
#tupleDictFilename = "default filename"
tupleDictFilename = str(sys.argv[-1]);
if 'r' in sys.argv:
#print "WRONG"
#sys.exit(1);
if not os.path.isfile(tupleDictFilename) or tupleDictFilename == "-":
print "Filename argument invalid; running for default tuple dictionary file"
tupleDictFilename = os.environ["B2DXFITTERSROOT"] + "/tutorial/tupleDict2.py"
in_file = TFile("/mnt/cdrom/Bs2Dspipipi_MC_fullSel_reweighted_combined.root");
ROOT.SetOwnership(in_file, False)
keyList = in_file.GetListOfKeys()
tree1 = keyList.At(0).ReadObj();
print tree1.GetName();
tupleDict = importTupleDict(tupleDictFilename);
print tupleDict.keys()
#sys.exit(0);
varList = []
varRenamedList = []
for i in range(len(tupleDict)):
varName = tupleDict.keys()[i]
varList += [RooRealVar(varName,tree1.GetBranch(varName).GetTitle(),tree1.GetMinimum(varName),tree1.GetMaximum(varName))];
varRenamedList += [RooFit.RenameVariable(varName, tupleDict[varName])]
#RooFit.RenameVariable(varName, tupleDict[varName]);
#varList[i].SetName(tupleDict.keys()[i]);
#tupleDataSet = RooDataSet("treeData","treeData",tree1,RooArgSet(*varList));
weightvar = RooRealVar("weight", "weight", -1e7, 1e7)
tupleDataSet = RooDataSet("tupleDataSet", "tupleDataSet",
RooArgSet(*varList),
RooFit.Import(tree1), RooFit.WeightVar(weightvar))
ws = RooWorkspace('ws_FIT')
tupleDataSet = WS(ws, tupleDataSet, varRenamedList)
tupleDataSet.Print();
sys.exit(0);
#qt needs to be a category?
# Manuel's shit...
#weightvar = RooRealVar("weight", "weight", -1e7, 1e7)
#tupleDataSet = RooDataSet("tupleDataSet", "tupleDataSet",
# RooArgSet(treetime, treeqt, treeqf, treeeta),
# RooFit.Import(tree1), RooFit.WeightVar(weightvar))
#tupleDS = WS(ws, tupleDS, [
# RooFit.RenameVariable("Bs_ctau", "time"), ... ])
# # note: do not forget to add to fitTo options: RooFit.SumW2Error(True)
#ROOT.SetOwnership(tupleDataSet,False);
#tupleDataSet.get().find(varName).Print();
'''
for i in range(len(tupleDict)):
varName = tupleDict.keys()[i];
a = tupleDataSet.get().find(varName);
a.SetName(tupleDict[varName]);
a.SetTitle(tupleDict[varName]);
from ROOT import TFile
import time
g = TFile('../BsStuff.root', 'recreate')
g.WriteTObject(tupleDataSet)
g.Close()
del g'''
else:
print "Running without 'r' argument disabled"
sys.exit(0);
rootInFile = TFile("../BsStuff.root");
keyList = rootInFile.GetListOfKeys();
keyList.At(0).ReadObj().Print();
keyList.At(1).ReadObj().Print();
tupleDataSet = rootInFile.Get('treeData');
if not os.path.isfile(tupleDictFilename) or tupleDictFilename == "-":
print "Filename argument invalid; running for default tuple dictionary file"
tupleDictFilename = os.environ["B2DXFITTERSROOT"] + "/tutorial/tupleDict2.py"
tupleDict = importTupleDict(tupleDictFilename);
tupleDataSet = tupleDataSet.reduce('osDecision==ssDecision');
from B2DXFitters.utils import configDictFromFile
config = configDictFromFile('bsConfig.py');
# start with RooFit stuff
from ROOT import ( RooRealVar, RooConstVar, RooCategory, RooWorkspace,
RooArgSet, RooArgList, RooLinkedList, RooAbsReal, RooRandom, TRandom3,
MistagDistribution, MistagCalibration, RooFormulaVar
)
# safe settings for numerical integration (if needed)
RooAbsReal.defaultIntegratorConfig().setEpsAbs(1e-9)
RooAbsReal.defaultIntegratorConfig().setEpsRel(1e-9)
RooAbsReal.defaultIntegratorConfig().getConfigSection(
'RooAdaptiveGaussKronrodIntegrator1D').setCatLabel('method','15Points')
RooAbsReal.defaultIntegratorConfig().getConfigSection(
'RooAdaptiveGaussKronrodIntegrator1D').setRealValue('maxSeg', 1000)
RooAbsReal.defaultIntegratorConfig().method1D().setLabel(
'RooAdaptiveGaussKronrodIntegrator1D')
RooAbsReal.defaultIntegratorConfig().method1DOpen().setLabel(
'RooAdaptiveGaussKronrodIntegrator1D')
from B2DXFitters.WS import WS
fitpdf = buildTimePdf(config, tupleDataSet, tupleDict);
ws = fitpdf['ws'];
tupleDataSet = WS(ws, tupleDataSet);
# set constant what is supposed to be constant
from B2DXFitters.utils import setConstantIfSoConfigured
setConstantIfSoConfigured(config, fitpdf['pdf'])
# set up fitting options
fitopts = [ RooFit.Timer(), RooFit.Save(),
RooFit.Strategy(config['FitConfig']['Strategy']),
RooFit.Optimize(config['FitConfig']['Optimize']),
RooFit.Offset(config['FitConfig']['Offset']),
RooFit.NumCPU(config['FitConfig']['NumCPU']) ]
# set up blinding for data
fitopts.append(RooFit.Verbose(not (config['IsData'] and config['Blinding'])))
if config['IsData'] and config['Blinding']:
from ROOT import RooMsgService
RooMsgService.instance().setGlobalKillBelow(RooFit.WARNING)
fitopts.append(RooFit.PrintLevel(-1))
fitOpts = RooLinkedList()
for o in fitopts: fitOpts.Add(o)
# fit
rawfitresult = fitpdf['pdf'].fitTo(tupleDataSet, fitOpts)
# pretty-print the result
from B2DXFitters.FitResult import getDsHBlindFitResult
result = getDsHBlindFitResult(config['IsData'], config['Blinding'],
rawfitresult)
print result
'''
doubleCanvas = TCanvas();
frameList = []
print '\n\n',tupleDict.values(),'\n\n'
doubleCanvas.Divide(3,3,0,0)
for i in range(len(tupleDict)):
#varName = tupleDict.keys()[i];
frameList += [tupleDataSet.get().find(tupleDict.values()[i]).frame()];
tupleDataSet.plotOn(frameList[i], RooFit.DrawOption('b'));
doubleCanvas.cd(i+1);
frameList[i].Draw();
'''
for i in range(len(tupleDict)):
tupleDataSet.get().find(tupleDict.values()[i]).Print();
sys.exit(0);
'''
doubleCanvas.Update();
raw_input('Press Enter to continue');
#doubleCanvas.Clear();
#print tupleDict.keys()[i];
#a.Print();
#print
doubleCanvas.Close();'''
tupleDataSet.Print();
sys.exit(0);
dirName = "tupleTestPlots";
currentTime = time.time();
os.chdir(os.environ['B2DXFITTERSROOT']+'/tutorial');
if(not os.path.exists(dirName)):
os.mkdir(dirName)
os.chdir(dirName);
doubleCanvas.SaveAs("plot_%f.pdf" % currentTime);
raw_input("Press Enter to continue...")
'''
from B2DXFitters.WS import WS
# for now, we're in the generation stage
#-config['Context'] = 'GEN'
#-genpdf = buildTimePdf(config)
import copy
# FIXME: need to deep-copy the config dictionary, and need to disable a few
# tweaks that speed up fitting during generation (because they waste time there)
genconfig = copy.deepcopy(config)
genconfig['Context'] = 'GEN'
genconfig['NBinsAcceptance'] = 0
genconfig['NBinsProperTimeErr'] = 0
genconfig['ParameteriseIntegral'] = False
genpdf = buildTimePdf(genconfig)
'''
#vim: sw=4:et