{ "cells": [ { "cell_type": "markdown", "id": "b7f237ef", "metadata": {}, "source": [ "# hist056_TPolyMarker_contour\n", "Make a contour plot and get the first contour in a TPolyMarker.\n", "This macro generates a color contour plot by selecting entries\n", "from an ntuple file.\n", "The TGraph object corresponding to the first contour line is\n", "accessed and displayed into a separate canvas.\n", "\n", "\n", "\n", "\n", "**Author:** Rene Brun \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:13 PM." ] }, { "cell_type": "code", "execution_count": null, "id": "952299e0", "metadata": { "collapsed": false }, "outputs": [], "source": [ "TString dir = gROOT->GetTutorialDir();\n", "dir.Append(\"/hsimple.C\");\n", "dir.ReplaceAll(\"/./\", \"/\");\n", "if (!gInterpreter->IsLoaded(dir.Data()))\n", " gInterpreter->LoadMacro(dir.Data());\n", "TFile *file = (TFile *)gROOT->ProcessLineFast(\"hsimple(1)\");\n", "if (!file)\n", " return;\n", "TTree *ntuple = (TTree *)file->Get(\"ntuple\");\n", "\n", "TCanvas *c1 = new TCanvas(\"c1\", \"Contours\", 10, 10, 800, 600);\n", "ntuple->Draw(\"py:px\", \"px*px+py*py < 20\", \"contz,list\");" ] }, { "cell_type": "markdown", "id": "d3ce84fd", "metadata": {}, "source": [ "we must call Update to force the canvas to be painted. When\n", "painting the contour plot, the list of contours is generated\n", "and a reference to it added to the Root list of special objects" ] }, { "cell_type": "code", "execution_count": null, "id": "bb4c968e", "metadata": { "collapsed": false }, "outputs": [], "source": [ "c1->Update();\n", "\n", "TCanvas *c2 = new TCanvas(\"c2\", \"First contour\", 100, 100, 800, 600);\n", "\n", "TObjArray *contours = (TObjArray *)gROOT->GetListOfSpecials()->FindObject(\"contours\");\n", "if (!contours)\n", " return;\n", "TList *lcontour1 = (TList *)contours->At(0);\n", "if (!lcontour1)\n", " return;\n", "TGraph *gc1 = (TGraph *)lcontour1->First();\n", "if (!gc1)\n", " return;\n", "if (gc1->GetN() < 10)\n", " return;\n", "gc1->SetMarkerStyle(21);\n", "gc1->Draw(\"alp\");" ] }, { "cell_type": "markdown", "id": "42cb7ac6", "metadata": {}, "source": [ "We make a TCutG object with the array obtained from this graph" ] }, { "cell_type": "code", "execution_count": null, "id": "fabc0eaf", "metadata": { "collapsed": false }, "outputs": [], "source": [ "TCutG *cutg = new TCutG(\"cutg\", gc1->GetN(), gc1->GetX(), gc1->GetY());" ] }, { "cell_type": "markdown", "id": "26cc23b7", "metadata": {}, "source": [ "We create a polymarker object with npmax points." ] }, { "cell_type": "code", "execution_count": null, "id": "66bf7060", "metadata": { "collapsed": false }, "outputs": [], "source": [ "const Int_t npmax = 50000;\n", "TPolyMarker *pm = new TPolyMarker(npmax);\n", "Int_t np = 0;\n", "while (1) {\n", " Double_t x = -4 + 8 * gRandom->Rndm();\n", " Double_t y = -4 + 8 * gRandom->Rndm();\n", " if (cutg->IsInside(x, y)) {\n", " pm->SetPoint(np, x, y);\n", " np++;\n", " if (np == npmax)\n", " break;\n", " }\n", "}\n", "pm->Draw();" ] }, { "cell_type": "markdown", "id": "0fd1e5d2", "metadata": {}, "source": [ "Draw all canvases " ] }, { "cell_type": "code", "execution_count": null, "id": "4269f4ee", "metadata": { "collapsed": false }, "outputs": [], "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 }