# Confidence Levels in Root

**URL:** <https://root-forum.cern.ch/t/confidence-levels-in-root/31602>\
**Category:** Newbie\
**Created:** [November 15, 2018, 4:55am UTC](https://root-forum.cern.ch/t/confidence-levels-in-root/31602 "2018-11-15T04:55:44Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![srishino](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/5f9b8f/32.png) [@srishino](https://root-forum.cern.ch/u/srishino)\
**Post date:** [November 15, 2018, 4:55am UTC](https://root-forum.cern.ch/t/confidence-levels-in-root/31602/1 "2018-11-15T04:55:45Z")

</div>

Hello Everyone!  
I am trying to plot 2-D histogram for contour making at 1,2 and 3 sigma confidence level.  
I am reading data from a .dat file with three columns and i want plot it as a contour at above mentioned confidence levels . Please suggest me how to set 1,2 and 3 sigma confidence levels in my code…  
‘’’  
#include  
#include\<math.h\>  
#include \<TVirtualFitter.h\>  
#include \<TF2.h\>

void rootContourPlot()

{

ifstream f1;  
TCanvas \*c1 = new TCanvas(“c1”,“c1”,600,400);  
TH2F \*hcont2 = new TH2F(“hcont2”," ",40,-4,-2,40,0,200);

Double\_t x[10001]={0.}, y[10001]={0.},z[10001]={0.}, i,j,k;  
int ss=0;

f1.open(“test1.dat”);  
while(!f1.eof())  
{  
f1\>\>i\>\>j\>\>k;

```
   hcont2->Fill(i,j,k);
   (TVirtualFitter::GetFitter())->GetConfidenceIntervals(hcont2, 0.95);

 }

```

hcont2-\>Draw("CONT2 ");  
c1-\>Update();  
f1.close();

}  
‘’’

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [November 15, 2018, 7:41am UTC](https://root-forum.cern.ch/t/confidence-levels-in-root/31602/2 "2018-11-15T07:41:54Z")

</div>

Hi,

The method TVirtualFitter::GetFitter())-\>GetConfidenceIntervals(hcont2, 0.95) computes the confidence level of your model (fitting) function after having it fitted to an histogram.  
I don’t see in your code where you have performed the fit.  
If you have an histogram and you want to plot the contour lines, you just need to do hcont2-\>Draw(“CONT2”)

Lorenzo

---

<div class="post-metadata">

**Author:** ![srishino](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/5f9b8f/32.png) [@srishino](https://root-forum.cern.ch/u/srishino)\
**Post date:** [November 15, 2018, 8:05am UTC](https://root-forum.cern.ch/t/confidence-levels-in-root/31602/3 "2018-11-15T08:05:43Z")

</div>

Hello Thank you for your reply… I have a file with three columns and want to see how z varies with x and y at 1, 2 and 3 sigma levels, i am not getting what function should i use to plot this… A 3D graph or 2DProfile histogram, I want to see only x and y axes… I have made it using Gnuplot but not able to make it from ROOT  
.[example1.pdf](https://root-forum.cern.ch/uploads/default/original/3X/5/1/51edbc88bd060eefa4422e82b0303251e979adef.pdf) (9.3 KB)

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [November 15, 2018, 8:14am UTC](https://root-forum.cern.ch/t/confidence-levels-in-root/31602/4 "2018-11-15T08:14:48Z")

</div>

[${ROOTSYS}/tutorials/hist/ContourList.C](https://root.cern.ch/doc/master/ContourList_8C.html)

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [November 15, 2018, 8:15am UTC](https://root-forum.cern.ch/t/confidence-levels-in-root/31602/5 "2018-11-15T08:15:47Z")

</div>

Hi,

Then what you need to use is hcont2-\>Draw(“CONT1”). You can set the contour and their level with hcont2-\>SetContours(n, levels), where n is the number of contours you want and levels are the z values for which you want to draw the contour.

Lorenzo

---

<div class="post-metadata">

**Author:** ![srishino](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/5f9b8f/32.png) [@srishino](https://root-forum.cern.ch/u/srishino)\
**Post date:** [November 15, 2018, 8:20am UTC](https://root-forum.cern.ch/t/confidence-levels-in-root/31602/6 "2018-11-15T08:20:11Z")

</div>

I understood the meaning of your first two lines now… yes I havnt implemented any fitting function…  
I performed simply - hcont2-\>Draw(“CONT2”) but this is not my desired result… i have attached :  
[rootcontour.pdf](https://root-forum.cern.ch/uploads/default/original/3X/d/0/d0d453a7a2aa1b70f5f14708c45eab1a0bd55d02.pdf) (36.1 KB)

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [November 15, 2018, 9:00am UTC](https://root-forum.cern.ch/t/confidence-levels-in-root/31602/7 "2018-11-15T09:00:49Z")

</div>

As I said you need to set the number of contours if you don’t like the default.  
You should look at the documentation of the Contour option of TH2  
[https://root.cern/doc/v610/classTHistPainter.html#HP16](https://root.cern/doc/v610/classTHistPainter.html#HP16)

Lorenzo

---

<div class="post-metadata">

**Author:** ![srishino](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/5f9b8f/32.png) [@srishino](https://root-forum.cern.ch/u/srishino)\
**Post date:** [November 15, 2018, 9:01am UTC](https://root-forum.cern.ch/t/confidence-levels-in-root/31602/8 "2018-11-15T09:01:24Z")

</div>

> [@moneta](#):
>
> SetContours(n, levels)

Thanks a lot… it worked with a little a more help from another example 🙂

---

<div class="post-metadata">

**Author:** ![srishino](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/5f9b8f/32.png) [@srishino](https://root-forum.cern.ch/u/srishino)\
**Post date:** [November 15, 2018, 9:05am UTC](https://root-forum.cern.ch/t/confidence-levels-in-root/31602/9 "2018-11-15T09:05:48Z")

</div>

I was using help from this very link only since 2 days but i wasnt able to set the contour levels…that I wanted

---

<div class="post-metadata">

**Author:** ![srishino](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/5f9b8f/32.png) [@srishino](https://root-forum.cern.ch/u/srishino)\
**Post date:** [November 15, 2018, 10:25am UTC](https://root-forum.cern.ch/t/confidence-levels-in-root/31602/10 "2018-11-15T10:25:25Z")

</div>

How can I make my best fit point in this ? I am attaching the plot [example1root.pdf](https://root-forum.cern.ch/uploads/default/original/3X/e/c/ec1f716ebf01c333bd92283f85aa54bf22c98f28.pdf) (15.1 KB)  
…

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [November 15, 2018, 10:50am UTC](https://root-forum.cern.ch/t/confidence-levels-in-root/31602/11 "2018-11-15T10:50:06Z")

</div>

I am not sure I have understood exactly what you want. Add a point in the plot ?  
Maybe @couet can help you with this

Lorenzo

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [November 15, 2018, 11:17am UTC](https://root-forum.cern.ch/t/confidence-levels-in-root/31602/12 "2018-11-15T11:17:03Z")

</div>

[`(new TMarker(-3., 90., 30))->Draw();`](https://root.cern.ch/doc/master/classTMarker.html)

---

<div class="post-metadata">

**Author:** ![srishino](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/5f9b8f/32.png) [@srishino](https://root-forum.cern.ch/u/srishino)\
**Post date:** [November 15, 2018, 2:54pm UTC](https://root-forum.cern.ch/t/confidence-levels-in-root/31602/13 "2018-11-15T14:54:51Z")

</div>

Hii… Thanks a lot , it made the best fit point but please tell me how did you write the entries in your statement given above…

---

<div class="post-metadata">

**Author:** ![srishino](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/5f9b8f/32.png) [@srishino](https://root-forum.cern.ch/u/srishino)\
**Post date:** [November 17, 2018, 3:49pm UTC](https://root-forum.cern.ch/t/confidence-levels-in-root/31602/14 "2018-11-17T15:49:47Z")

</div>

Hi,

Do you know how to make best fit point in gnuplot ??

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [November 19, 2018, 2:03pm UTC](https://root-forum.cern.ch/t/confidence-levels-in-root/31602/15 "2018-11-19T14:03:37Z")

</div>

> [@srishino](#):
>
> how did you write the entries in your statement given above…

What do you mean by “write the entry” ? the code Wile proposed will draw a marker at the position (-3, 90)

---

<div class="post-metadata">

**Author:** ![srishino](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/5f9b8f/32.png) [@srishino](https://root-forum.cern.ch/u/srishino)\
**Post date:** [November 19, 2018, 2:20pm UTC](https://root-forum.cern.ch/t/confidence-levels-in-root/31602/16 "2018-11-19T14:20:12Z")

</div>

yes I realized it later… Thank you

---

<div class="post-metadata">

**Author:** ![srishino](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/5f9b8f/32.png) [@srishino](https://root-forum.cern.ch/u/srishino)\
**Post date:** [November 20, 2018, 11:14am UTC](https://root-forum.cern.ch/t/confidence-levels-in-root/31602/17 "2018-11-20T11:14:46Z")

</div>

Hi,

I want to know how does root calculates confidence levels at 1,2 and 3 sigma values…  
For one data file I am getting a plot using the code below but for another data file I am not getting a proper contour plot while both these files are giving contour plots with gnuplot… I am not able to get this…  
I am using the code below…

Also I have one more doubt about contour plots… What i know is- If we have three columns x,y,z then we fix a particular value of z=confidence level and plot corresponding x and y… am I correct ?  
Please help me out  
Thank you…

```auto
#include<iostream>
#include<math.h>
#include <TVirtualFitter.h>
#include <TF2.h>

void rootContourPlot()

  {

   ifstream f1;
   TCanvas *c1 = new TCanvas("c1","c1",600,400);
   TH2F *hcont2 = new TH2F("hcont2"," ",40,0,360,40,0,0.9);

   Double_t x[10001]={0.}, y[10001]={0.},z[10001]={0.},i,j,k;
   int ss=0;

   double contours[3];
   contours[0] = 2.3;
   contours[1] = 6.2;
   contours[2] = 11.8;
  
   f1.open("test12.dat");
   while(!f1.eof())
      {
      f1>>i>>j>>k;

      hcont2->Fill(i,j,k);
     

       }
   hcont2->SetContour(3,contours);
   hcont2->Draw("CONT1");
// (new TMarker(-3., 90., 30))->Draw(); // Makes the best fit point
   c1->Update();
   f1.close();

   }

```

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [November 20, 2018, 12:55pm UTC](https://root-forum.cern.ch/t/confidence-levels-in-root/31602/18 "2018-11-20T12:55:20Z")

</div>

> [@srishino](#):
>
> For one data file I am getting a plot using the code below but for another data file I am not getting a proper contour plot while both these files are giving contour plots with gnuplot…

Can you provide the data files ?

---

<div class="post-metadata">

**Author:** ![srishino](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/5f9b8f/32.png) [@srishino](https://root-forum.cern.ch/u/srishino)\
**Post date:** [November 20, 2018, 1:45pm UTC](https://root-forum.cern.ch/t/confidence-levels-in-root/31602/19 "2018-11-20T13:45:38Z")

</div>

[datafiles.tar.gz](https://root-forum.cern.ch/uploads/default/original/3X/0/b/0b0e912f674e7b144ab4d2d7f73477f91235df8b.gz) (23.1 KB)

Hi,  
Please find the tar file which contains the datafiles.  
Thank you.

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [November 20, 2018, 2:50pm UTC](https://root-forum.cern.ch/t/confidence-levels-in-root/31602/20 "2018-11-20T14:50:24Z")

</div>

In case of test1.dat you are filling the histogram outside its limits. SO all the Fill() you do generate underflow or overflow and the histogram is empty.

[Next page](https://root-forum.cern.ch/t/confidence-levels-in-root/31602.md?page=2)
