{ "cells": [ { "cell_type": "markdown", "id": "5b3373d9", "metadata": {}, "source": [ "# pdf008_BreitWigner\n", "Tutorial illustrating how to create a plot comparing a Breit Wigner to a Relativistic Breit Wigner\n", "\n", "can be run with:\n", "\n", "```cpp\n", " root[0] .x BreitWigner.C\n", "```\n", "\n", "\n", "\n", "\n", "**Author:** Jack Lindon \n", "This notebook tutorial was automatically generated with ROOTBOOK-izer from the macro found in the ROOT repository on Tuesday, May 19, 2026 at 08:26 PM." ] }, { "cell_type": "code", "execution_count": 1, "id": "d292fb5c", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:26:44.257473Z", "iopub.status.busy": "2026-05-19T20:26:44.257334Z", "iopub.status.idle": "2026-05-19T20:26:44.265290Z", "shell.execute_reply": "2026-05-19T20:26:44.264790Z" } }, "outputs": [], "source": [ "%%cpp -d\n", "#include \"TMath.h\"\n", "\n", "#include \n", "#include \n", "#include \"TAxis.h\"\n", "#include \"TGraph.h\"\n", "#include \"TCanvas.h\"\n", "#include \"TLatex.h\"\n", "#include \"TLegend.h\"\n", "#include \"TStyle.h\" //For gStyle to remove stat box." ] }, { "cell_type": "markdown", "id": "f2da14ae", "metadata": {}, "source": [ " Definition of a helper function: " ] }, { "cell_type": "code", "execution_count": 2, "id": "82bc174c", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:26:44.266698Z", "iopub.status.busy": "2026-05-19T20:26:44.266569Z", "iopub.status.idle": "2026-05-19T20:26:44.285414Z", "shell.execute_reply": "2026-05-19T20:26:44.284893Z" } }, "outputs": [], "source": [ "%%cpp -d\n", "void plotTwoTGraphs(Double_t x[], Double_t y1[], Double_t y2[], const Int_t nPoints\n", "\t\t , Double_t lowerXLimit, Double_t upperXLimit\n", "\t\t , Double_t lowerYLimit, Double_t upperYLimit\n", "\t\t , std::string legend1, std::string legend2\n", "\t\t , std::string plotTitle1, std::string plotTitle2, std::string plotTitle3\n", "\t\t , std::string pdfTitle){\n", "\n", " ///////////////////////////////////////////////////////\n", " //Define variables for plot aesthetics and positioning\n", " Double_t legendXPos = 0.63; Double_t legendYPos = 0.85;\n", " Double_t legendXWidth = 0.29; Double_t legendYHeight = 0.1;\n", " Double_t plotTitleXPos = 0.23; Double_t plotTitleYPos = 0.25;\n", " Double_t fontSize = 0.04;\n", " Double_t lineWidth = 2;\n", " bool setLimitPlotLogScale = true;\n", " std::string xAxisTitle = \"E [GeV]\"; std::string yAxisTitle = \"Events\";\n", " Double_t xAxisTitleOffset = 1; Double_t yAxisTitleOffset = 1.3;\n", " gStyle->SetOptStat(0);\n", "\n", " ///////////////////////////////////////////////////////\n", " // Initialize TGraphs\n", " TGraph* gr1 = new TGraph(nPoints,x,y1);\n", " TGraph* gr2 = new TGraph(nPoints,x,y2);\n", " gr1->SetLineWidth(lineWidth);\n", " gr2->SetLineWidth(lineWidth);\n", " gr1->SetLineColor(kBlack);\n", " gr2->SetLineColor(kBlue);\n", "\n", " /////////////////////////////////////////////////////////\n", " // Initialize canvas\n", " TCanvas* c1 = new TCanvas(\"c1\",\"transparent pad\",200,10,600,600);\n", " c1->SetLogy(setLimitPlotLogScale);\n", " c1->SetTicks(1,1);\n", " c1->SetRightMargin(0.02);\n", " c1->SetTopMargin(0.02);\n", "\n", "\n", " ///////////////////////////////////////////////////////\n", " //Make just a basic invisible TGraph just for the axes\n", " const Double_t axis_x[2] = {lowerXLimit,upperXLimit};\n", " const Double_t axis_y[2] = {lowerYLimit,upperYLimit};\n", " TGraph* grAxis = new TGraph(2, axis_x, axis_y);\n", " grAxis->SetTitle(\"\");\n", " grAxis->GetYaxis()->SetTitle(yAxisTitle.c_str());\n", " grAxis->GetXaxis()->SetTitle(xAxisTitle.c_str());\n", " grAxis->GetXaxis()->SetRangeUser(lowerXLimit,upperXLimit);\n", " grAxis->GetYaxis()->SetRangeUser(lowerYLimit,upperYLimit);\n", " grAxis->GetXaxis()->SetLabelSize(fontSize);\n", " grAxis->GetYaxis()->SetLabelSize(fontSize);\n", " grAxis->GetXaxis()->SetTitleSize(fontSize);\n", " grAxis->GetYaxis()->SetTitleSize(fontSize);\n", " grAxis->GetXaxis()->SetTitleOffset(xAxisTitleOffset);\n", " grAxis->GetYaxis()->SetTitleOffset(yAxisTitleOffset);\n", " grAxis->SetLineWidth(0);//So invisible\n", "\n", " ///////////////////////////////////////////////////////////\n", " // Make legend and set aesthetics\n", " auto legend = new TLegend(legendXPos,legendYPos,legendXPos+legendXWidth,legendYPos+legendYHeight);\n", " legend->SetFillStyle(0);\n", " legend->SetBorderSize(0);\n", " legend->SetTextSize(fontSize);\n", " legend->AddEntry(gr1,legend1.c_str(),\"L\");\n", " legend->AddEntry(gr2,legend2.c_str(),\"L\");\n", "\n", "\n", " /////////////////////////////////////////////////////////////\n", " // Add plot title to plot. Make in three lines so not crowded.\n", " // Shift each line down by shiftY\n", " float shiftY{0.037};\n", " TLatex* tex_Title = new TLatex(plotTitleXPos,plotTitleYPos-0*shiftY,plotTitle1.c_str());\n", " tex_Title->SetNDC();\n", " tex_Title->SetTextFont(42);\n", " tex_Title->SetTextSize(fontSize);\n", " tex_Title->SetLineWidth(lineWidth);\n", " TLatex* tex_Title2 = new TLatex(plotTitleXPos,plotTitleYPos-1*shiftY,plotTitle2.c_str());\n", " tex_Title2->SetNDC();\n", " tex_Title2->SetTextFont(42);\n", " tex_Title2->SetTextSize(fontSize);\n", " tex_Title2->SetLineWidth(lineWidth);\n", " TLatex* tex_Title3 = new TLatex(plotTitleXPos,plotTitleYPos-2*shiftY,plotTitle3.c_str());\n", " tex_Title3->SetNDC();\n", " tex_Title3->SetTextFont(42);\n", " tex_Title3->SetTextSize(fontSize);\n", " tex_Title3->SetLineWidth(lineWidth);\n", "\n", "\n", " /////////////////////////////////////\n", " // Draw everything\n", " grAxis->Draw(\"AL\");\n", " gr1->Draw(\"L same\");\n", " gr2->Draw(\"L same\");\n", " legend->Draw();\n", " tex_Title->Draw();\n", " tex_Title2->Draw();\n", " tex_Title3->Draw();\n", " c1->RedrawAxis(); //Be sure to redraw axis AFTER plotting TGraphs otherwise TGraphs will be on top of tick marks and axis borders.\n", "\n", " gPad->Print(pdfTitle.c_str());\n", "\n", "}" ] }, { "cell_type": "markdown", "id": "97088e61", "metadata": {}, "source": [ "Define x axis limits and steps for each plotted point" ] }, { "cell_type": "code", "execution_count": 3, "id": "b36c9b83", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:26:44.286770Z", "iopub.status.busy": "2026-05-19T20:26:44.286627Z", "iopub.status.idle": "2026-05-19T20:26:44.602032Z", "shell.execute_reply": "2026-05-19T20:26:44.601309Z" } }, "outputs": [], "source": [ "const Int_t nPoints = 1000;\n", "Double_t xMinimum = 0; Double_t xMaximum = 13000;\n", "Double_t xStepSize = (xMaximum-xMinimum)/nPoints;" ] }, { "cell_type": "markdown", "id": "a379d77f", "metadata": {}, "source": [ "Define arrays of (x,y) points." ] }, { "cell_type": "code", "execution_count": 4, "id": "16bbfbea", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:26:44.603783Z", "iopub.status.busy": "2026-05-19T20:26:44.603666Z", "iopub.status.idle": "2026-05-19T20:26:44.810451Z", "shell.execute_reply": "2026-05-19T20:26:44.809856Z" } }, "outputs": [], "source": [ "Double_t x[nPoints];\n", "Double_t y_nonRelBW[nPoints], y_relBW[nPoints];" ] }, { "cell_type": "markdown", "id": "1f7f3fec", "metadata": {}, "source": [ "Define Breit-Wigner parameters" ] }, { "cell_type": "code", "execution_count": 5, "id": "2109cae7", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:26:44.812643Z", "iopub.status.busy": "2026-05-19T20:26:44.812503Z", "iopub.status.idle": "2026-05-19T20:26:45.019216Z", "shell.execute_reply": "2026-05-19T20:26:45.018658Z" } }, "outputs": [], "source": [ "Double_t width = 1350;\n", "Double_t sigma = 269.7899;\n", "Double_t median = 9000;" ] }, { "cell_type": "markdown", "id": "557343a7", "metadata": {}, "source": [ "Loop over x axis range, filling in (x,y) points,\n", "and finding y minimums and maximums for axis limit." ] }, { "cell_type": "code", "execution_count": 6, "id": "9b5c9fdc", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:26:45.021341Z", "iopub.status.busy": "2026-05-19T20:26:45.021219Z", "iopub.status.idle": "2026-05-19T20:26:45.223389Z", "shell.execute_reply": "2026-05-19T20:26:45.222848Z" } }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "
\n", "
\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stderr", "output_type": "stream", "text": [ "Info in : pdf file BW_M9000_Gamma1350.pdf has been created\n" ] } ], "source": [ "Double_t yMinimum = std::numeric_limits::max();\n", "Double_t yMaximum = TMath::BreitWignerRelativistic(median,median,width); //y maximum is at x=median (and non relativistic = relativistic at median so choice of function does not matter).\n", "for (Int_t i=0;i\n", "\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "gROOT->GetListOfCanvases()->Draw()" ] } ], "metadata": { "kernelspec": { "display_name": "ROOT C++", "language": "c++", "name": "root" }, "language_info": { "codemirror_mode": "text/x-c++src", "file_extension": ".C", "mimetype": " text/x-c++src", "name": "c++" } }, "nbformat": 4, "nbformat_minor": 5 }