Created
July 16, 2015 14:51
-
-
Save wiso/02e3d99eb066000a78d5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"0L" | |
] | |
}, | |
"execution_count": 4, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"import ROOT\n", | |
"from math import *\n", | |
"\n", | |
"def safe_factory(func):\n", | |
" def wrapper(self, *args):\n", | |
" result = func(self, *args)\n", | |
" if not result:\n", | |
" raise ValueError('invalid factory input \"%s\"' % args)\n", | |
" return result\n", | |
" return wrapper\n", | |
"\n", | |
"ROOT.RooWorkspace.factory = safe_factory(ROOT.RooWorkspace.factory)\n", | |
"\n", | |
"def safe_decorator(func):\n", | |
" def wrapper(self, *args):\n", | |
" result = func(self, *args)\n", | |
" if not result:\n", | |
" raise ValueError('cannot find %s' % args[0])\n", | |
" return result\n", | |
" return wrapper\n", | |
"\n", | |
"ROOT.RooWorkspace.data = safe_decorator(ROOT.RooWorkspace.data)\n", | |
"ROOT.RooWorkspace.obj = safe_decorator(ROOT.RooWorkspace.obj)\n", | |
"ROOT.RooWorkspace.var = safe_decorator(ROOT.RooWorkspace.var)\n", | |
"ROOT.RooWorkspace.pdf = safe_decorator(ROOT.RooWorkspace.pdf)\n", | |
"\n", | |
"\n", | |
"def iter_collection(rooAbsCollection):\n", | |
" iterator = rooAbsCollection.createIterator()\n", | |
" object = iterator.Next()\n", | |
" while object:\n", | |
" yield object\n", | |
" object = iterator.Next()\n", | |
"\n", | |
"\n", | |
"def list2argset(l):\n", | |
" s = ROOT.RooArgSet()\n", | |
" for ll in l:\n", | |
" s.add(ll)\n", | |
" return s\n", | |
"\n", | |
"ROOT.gROOT.ProcessLine(\".L $ROOTSYS/tutorials/roostats/StandardHypoTestDemo.C\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"False" | |
] | |
}, | |
"execution_count": 5, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"ws = ROOT.RooWorkspace('ws')\n", | |
"mass = ws.factory('mass[1000,5000]')\n", | |
"ws.factory('M[1000, 5000]')\n", | |
"nsig = ws.factory('nsig[100, 0, 1000]') # POI has range\n", | |
"ws.factory('nbkg[10000]')\n", | |
"ws.factory('RooGaussian:signal(mass, M, width[50])')\n", | |
"ws.factory('RooPolynomial:bkg(mass, {par0[0]})')\n", | |
"pdf = ws.factory(\"SUM:pdf(nsig * signal, nbkg * bkg)\")\n", | |
"\n", | |
"frame = ws.var('mass').frame()\n", | |
"\n", | |
"sbModel = ROOT.RooStats.ModelConfig(\"sbModel\")\n", | |
"sbModel.SetWorkspace(ws)\n", | |
"sbModel.SetParametersOfInterest('nsig')\n", | |
"sbModel.SetPdf('pdf')\n", | |
"sbModel.SetObservables('mass')\n", | |
"sbModel.SetNuisanceParameters('nbkg')\n", | |
"sbModel.SetSnapshot(ROOT.RooArgSet(nsig))\n", | |
"getattr(ws, 'import')(sbModel)\n", | |
"\n", | |
"\n", | |
"data_sb = ws.pdf('pdf').generateBinned(ROOT.RooArgSet(mass), ROOT.RooFit.ExpectedData())\n", | |
"data_sb.SetName('data_asimov_sb')\n", | |
"getattr(ws, 'import')(data_sb)\n", | |
"\n", | |
"data_sb.plotOn(frame)\n", | |
"pdf.plotOn(frame)\n", | |
"\n", | |
"\n", | |
"bModel = ROOT.RooStats.ModelConfig(\"bModel\")\n", | |
"bModel.SetWorkspace(ws)\n", | |
"bModel.SetParametersOfInterest('nsig')\n", | |
"bModel.SetPdf('pdf')\n", | |
"bModel.SetObservables('mass')\n", | |
"bModel.SetNuisanceParameters('nbkg')\n", | |
"nsig.setVal(0)\n", | |
"bModel.SetSnapshot(ROOT.RooArgSet(nsig))\n", | |
"getattr(ws, 'import')(bModel)\n", | |
"\n", | |
"data_b = ws.pdf('pdf').generateBinned(ROOT.RooArgSet(mass), ROOT.RooFit.ExpectedData())\n", | |
"data_b.SetName('data_asimov_b')\n", | |
"\n", | |
"\n", | |
"data_b.plotOn(frame)\n", | |
"pdf.plotOn(frame)\n", | |
"\n", | |
"getattr(ws, 'import')(data_b)\n", | |
"\n", | |
"frame.Draw()\n", | |
"\n", | |
"ws.Print()\n", | |
"ws.writeToFile('ws_simple.root')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"ROOT.StandardHypoTestDemo('ws_simple.root', \"ws\", \"sbModel\", \"bModel\",\n", | |
" \"data_asimov_b\", 2, 3)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"4.08248290463863" | |
] | |
}, | |
"execution_count": 4, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"100 / sqrt(600)" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 2", | |
"language": "python", | |
"name": "python2" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment