Skip to content

Instantly share code, notes, and snippets.

@wiso
Created February 27, 2016 13:48
Show Gist options
  • Save wiso/d17062e45a759436767f to your computer and use it in GitHub Desktop.
Save wiso/d17062e45a759436767f to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import ROOT"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"TROOT::Append:0: RuntimeWarning: Replacing existing TH1: h (Potential memory leak).\n"
]
}
],
"source": [
"h = ROOT.TH1F(\"h\", \"h\", 10, 0, 10)\n",
"#h.Sumw2()\n",
"h.Fill(2)\n",
"h.Fill(2)\n",
"h.Fill(3)\n",
"h.Fill(-1)\n",
"h.SetBinErrorOption(ROOT.TH1F.kPoisson)\n",
"h.Draw('e')"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from array import array\n",
"def get_graph_normalized(h):\n",
" \"\"\"\n",
" Takes as input a TH1F and return a TGraphAsymmErrors\n",
" \"\"\"\n",
" rh = ROOT.RooHist(h)\n",
" norm = h.GetSumOfWeights()\n",
" x = array('d', [0])\n",
" y = array('d', [0])\n",
" for i in xrange(rh.GetN()):\n",
" rh.GetPoint(i, x, y)\n",
" if y[0] == 0:\n",
" rh.SetPointEYhigh(i, 0)\n",
" rh.SetPointEYlow(i, 0)\n",
" else:\n",
" rh.SetPoint(i, x[0], y[0] / norm)\n",
" rh.SetPointEYhigh(i, rh.GetErrorYhigh(i) / norm)\n",
" rh.SetPointEYlow(i, rh.GetErrorYlow(i) / norm)\n",
" \n",
" return rh"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"hnorm = get_graph_normalized(h)\n",
"hnorm.SetFillColor(ROOT.kBlue)\n",
"hnorm.Draw(\"APE2\")\n",
"hnorm.Draw(\"P\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"hide_input": false,
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.5"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment