{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "# CMS Open Data Example #6: Fitting the J/$\\psi$ fit\n", "\n", "## Import required modules" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from ROOT import TFile, TCanvas, TH1F, TLorentzVector\n", "%jsroot on" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Read in Data from Input File" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [], "source": [ "file = TFile(\"../data/Dimuons.root\",\"READ\");" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Declare Histograms" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": true }, "outputs": [], "source": [ "Jpsi = TH1F(\"Jpsi\",\"#mu#mu mass;#mu#mu mass [GeV];Events\", 1000, 2, 3.5)" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "# Compute Invariant Mass of J/$\\psi$" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [], "source": [ "for dimu in file.Dimuons:\n", "\n", " if dimu.Muon1_Global and dimu.Muon2_Global: \n", " \n", " muon1 = TLorentzVector(dimu.Muon1_Px, dimu.Muon1_Py, dimu.Muon1_Pz, dimu.Muon1_Energy)\n", " \n", " muon2 = TLorentzVector(dimu.Muon2_Px, dimu.Muon2_Py, dimu.Muon2_Pz, dimu.Muon2_Energy)\n", " \n", " InvariantMass = (muon1 + muon2).M() \n", " \n", " if InvariantMass > 2.0 and InvariantMass < 4.0:\n", "\n", " Jpsi.Fill(InvariantMass)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Fitting the J/$\\psi$ Peak" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from ROOT import TF1, kGreen, kRed\n", "\n", "Canvas = TCanvas()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Possible fitting options:
\n", "\"gaus\" gaussian
\n", "\"pol1\" 1st order polynomial
" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " FCN=238.61 FROM MIGRAD STATUS=CONVERGED 71 CALLS 72 TOTAL\n", " EDM=7.76303e-09 STRATEGY= 1 ERROR MATRIX ACCURATE \n", " EXT PARAMETER STEP FIRST \n", " NO. NAME VALUE ERROR SIZE DERIVATIVE \n", " 1 Constant 5.82050e+02 4.65629e+00 2.59691e-02 -1.64617e-05\n", " 2 Mean 3.09292e+00 2.01159e-04 1.51576e-06 9.89004e-02\n", " 3 Sigma 3.05871e-02 2.03936e-04 1.30856e-05 2.50483e-02\n", "\n", "****************************************\n", "Minimizer is Linear\n", "Chi2 = 28549.5\n", "NDf = 998\n", "p0 = 65.9587 +/- 0.923571 \n", "p1 = -15.3932 +/- 0.313802 \n" ] }, { "data": { "text/html": [ "\n", "
\n", "
\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "Gaussian = TF1(\"Gaussian\",\"gaus\", 3.03, 3.16)\n", "Background = TF1(\"Background\",\"pol1\", 2, 3.5)\n", "\n", "Gaussian.SetLineColor(kRed)\n", "Background.SetLineColor(kGreen)\n", "\n", "Jpsi.Fit(Gaussian,\"R\")\n", "Jpsi.Fit(Background,\"R+\")\n", "\n", "Canvas.Draw()" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " FCN=268.513 FROM MIGRAD STATUS=CONVERGED 72 CALLS 73 TOTAL\n", " EDM=8.59885e-08 STRATEGY= 1 ERROR MATRIX ACCURATE \n", " EXT PARAMETER STEP FIRST \n", " NO. NAME VALUE ERROR SIZE DERIVATIVE \n", " 1 Constant 2.36718e+02 2.11587e+00 1.23773e-02 1.59380e-04\n", " 2 Mean 9.08217e+01 1.61174e-02 1.28180e-04 1.90289e-02\n", " 3 Sigma 2.10257e+00 1.75197e-02 1.75692e-05 1.38617e-01\n", "\n", "****************************************\n", "Minimizer is Linear\n", "Chi2 = 24551.1\n", "NDf = 943\n", "p0 = 41.589 +/- 0.584721 \n", "p1 = -0.347113 +/- 0.00546932 \n" ] }, { "data": { "text/html": [ "\n", "
\n", "
\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from ROOT import TFile, TCanvas, TH1F, TLorentzVector\n", "%jsroot on\n", "\n", "file = TFile(\"../data/Dimuons.root\",\"READ\");\n", "\n", "z_boson = TH1F(\"zboson\",\"#mu#mu mass;#mu#mu mass [GeV];Events\", 1000, 70, 120)\n", "\n", "for dimu in file.Dimuons:\n", "\n", " if dimu.Muon1_Global and dimu.Muon2_Global: \n", " \n", " muon1 = TLorentzVector(dimu.Muon1_Px, dimu.Muon1_Py, dimu.Muon1_Pz, dimu.Muon1_Energy)\n", " \n", " muon2 = TLorentzVector(dimu.Muon2_Px, dimu.Muon2_Py, dimu.Muon2_Pz, dimu.Muon2_Energy)\n", " \n", " InvariantMass = (muon1 + muon2).M() \n", " \n", " if InvariantMass > 70.0 and InvariantMass < 120.0:\n", "\n", " z_boson.Fill(InvariantMass)\n", " \n", "from ROOT import TF1, kGreen, kRed\n", "\n", "Canvas = TCanvas()\n", "\n", "Gaussian = TF1(\"Gaussian\",\"gaus\", 87, 95)\n", "Background = TF1(\"Background\",\"pol1\", 70, 120)\n", "\n", "Gaussian.SetLineColor(kRed)\n", "Background.SetLineColor(kGreen)\n", "\n", "z_boson.Fit(Gaussian,\"R\")\n", "z_boson.Fit(Background,\"R+\")\n", "\n", "Canvas.Draw()" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "data": { "application/javascript": [ "\n", "require(['notebook'],\n", " function() {\n", " IPython.CodeCell.config_defaults.highlight_modes['magic_text/x-c++src'] = {'reg':[/^%%cpp/]};\n", " console.log(\"JupyROOT - %%cpp magic configured\");\n", " }\n", ");\n" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "Welcome to JupyROOT 6.07/07\n" ] } ], "source": [ "from ROOT import TFile, TCanvas, TH1F, TLorentzVector\n", "%jsroot on\n", "\n", "file = TFile(\"../data/Dimuons.root\",\"READ\");\n", "\n", "upsilon = TH1F(\"upsilon\",\"#mu#mu mass;#mu#mu mass [GeV];Events\", 1000, 8.0, 12.0)\n", "\n", "for dimu in file.Dimuons:\n", "\n", " if dimu.Muon1_Global and dimu.Muon2_Global: \n", " \n", " muon1 = TLorentzVector(dimu.Muon1_Px, dimu.Muon1_Py, dimu.Muon1_Pz, dimu.Muon1_Energy)\n", " \n", " muon2 = TLorentzVector(dimu.Muon2_Px, dimu.Muon2_Py, dimu.Muon2_Pz, dimu.Muon2_Energy)\n", " \n", " InvariantMass = (muon1 + muon2).M() \n", " \n", " if InvariantMass > 8.0 and InvariantMass < 12.0:\n", "\n", " upsilon.Fill(InvariantMass)\n", " " ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " FCN=79.829 FROM MIGRAD STATUS=CONVERGED 89 CALLS 90 TOTAL\n", " EDM=4.19646e-10 STRATEGY= 1 ERROR MATRIX ACCURATE \n", " EXT PARAMETER STEP FIRST \n", " NO. NAME VALUE ERROR SIZE DERIVATIVE \n", " 1 Constant 2.12407e+02 2.73248e+00 8.48712e-03 1.08776e-05\n", " 2 Mean 9.45177e+00 1.42689e-03 6.25756e-06 2.67027e-03\n", " 3 Sigma 1.09200e-01 2.04239e-03 2.37270e-05 5.27763e-03\n", " FCN=75.2579 FROM MIGRAD STATUS=CONVERGED 84 CALLS 85 TOTAL\n", " EDM=7.31957e-10 STRATEGY= 1 ERROR MATRIX UNCERTAINTY 2.6 per cent\n", " EXT PARAMETER STEP FIRST \n", " NO. NAME VALUE ERROR SIZE DERIVATIVE \n", " 1 Constant 1.21647e+02 1.93729e+00 1.66805e-03 -3.12560e-05\n", " 2 Mean 1.00152e+01 3.65123e-03 -2.47985e-06 5.59594e-03\n", " 3 Sigma 1.65543e-01 7.09809e-03 -1.04327e-05 -1.09809e-03\n", " FCN=67.0953 FROM MIGRAD STATUS=CONVERGED 112 CALLS 113 TOTAL\n", " EDM=1.09651e-08 STRATEGY= 1 ERROR MATRIX ACCURATE \n", " EXT PARAMETER STEP FIRST \n", " NO. NAME VALUE ERROR SIZE DERIVATIVE \n", " 1 Constant 1.03373e+02 1.76854e+00 4.89702e-03 -8.22534e-05\n", " 2 Mean 1.03350e+01 8.08859e-03 3.00292e-05 -4.48081e-03\n", " 3 Sigma 2.32207e-01 2.24293e-02 1.56126e-04 -3.78166e-03\n", "\n", "****************************************\n", "Minimizer is Linear\n", "Chi2 = 9741.05\n", "NDf = 998\n", "p0 = -140.444 +/- 1.79423 \n", "p1 = 20.6121 +/- 0.188834 \n" ] }, { "data": { "text/html": [ "\n", "
\n", "
\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from ROOT import TF1, kGreen, kRed\n", "\n", "Canvas = TCanvas()\n", "\n", "Gaussian = TF1(\"Gaussian\",\"gaus\", 9.3, 9.6)\n", "Gaussian2 = TF1(\"Gaussian2\",\"gaus\", 9.84, 10.16)\n", "Gaussian3 = TF1(\"Gaussian3\",\"gaus\", 10.2, 10.5)\n", "Background = TF1(\"Background\",\"pol1\", 8, 12)\n", "\n", "Gaussian.SetLineColor(kRed)\n", "Gaussian2.SetLineColor(kRed)\n", "Gaussian3.SetLineColor(kRed)\n", "Background.SetLineColor(kGreen)\n", "\n", "upsilon.Fit(Gaussian,\"R\")\n", "upsilon.Fit(Gaussian2,\"R2+\")\n", "upsilon.Fit(Gaussian3,\"R3+\")\n", "upsilon.Fit(Background,\"R+\")\n", "\n", "Canvas.Draw()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "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.9" } }, "nbformat": 4, "nbformat_minor": 0 }