# To find the integartion of area drawn using graphical cut

**URL:** <https://root-forum.cern.ch/t/to-find-the-integartion-of-area-drawn-using-graphical-cut/51386>\
**Category:** Newbie\
**Tags:** hist\
**Created:** [August 28, 2022, 6:42pm UTC](https://root-forum.cern.ch/t/to-find-the-integartion-of-area-drawn-using-graphical-cut/51386 "2022-08-28T18:42:37Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![gurmukh\_dhindsa](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/gurmukh_dhindsa/32/30326_2.png) [@gurmukh\_dhindsa](https://root-forum.cern.ch/u/gurmukh_dhindsa)\
**Post date:** [August 28, 2022, 6:42pm UTC](https://root-forum.cern.ch/t/to-find-the-integartion-of-area-drawn-using-graphical-cut/51386/1 "2022-08-28T18:42:38Z")

</div>

Hi there  
I am trying to find the number of counts inside the Graphical cut on my 2D histo which I created using graphical cut in gPad and saved that cut as a cut.c file.  
How can I do that, I already tried to use IntegralHist () but failed.

\_ROOT Version:6.04/06  
_Platform:_ Not Provided  
_Compiler:_ Not Provided

* * *

---

<div class="post-metadata">

**Author:** ![gurmukh\_dhindsa](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/gurmukh_dhindsa/32/30326_2.png) [@gurmukh\_dhindsa](https://root-forum.cern.ch/u/gurmukh_dhindsa)\
**Post date:** [August 29, 2022, 12:54am UTC](https://root-forum.cern.ch/t/to-find-the-integartion-of-area-drawn-using-graphical-cut/51386/2 "2022-08-29T00:54:29Z")

</div>

![Screenshot from 2022-08-28 15-37-24](https://root-forum.cern.ch/uploads/default/original/3X/c/2/c2bcb483b138071f3d2cd2252c4333f42b52fe5f.png)

here is what I want to say, I want to know, how many events do exist inside this graphical cut .

---

<div class="post-metadata">

**Author:** ![Dilicus](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dilicus/32/14338_2.png) [@Dilicus](https://root-forum.cern.ch/u/Dilicus)\
**Post date:** [August 29, 2022, 5:55am UTC](https://root-forum.cern.ch/t/to-find-the-integartion-of-area-drawn-using-graphical-cut/51386/3 "2022-08-29T05:55:08Z")

</div>

Hi,  
you can just apply the graphical cut created to your data.  
Like

```auto
 tree->Draw("TSd2rEnergy: TSd1rEnergy", "all other cuts +CUTG","goff")

```

in this way you will get the number of counts inside the CUTG and all the other cuts you applied for drawing the TH2D

Best  
Stefano

PS  
If you want avoid to overwrite your graphical cut before creating a new one remember to  
`CUTG->SetName("myCutG1)` and use the new name inside the list of cut.

---

<div class="post-metadata">

**Author:** ![gurmukh\_dhindsa](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/gurmukh_dhindsa/32/30326_2.png) [@gurmukh\_dhindsa](https://root-forum.cern.ch/u/gurmukh_dhindsa)\
**Post date:** [August 29, 2022, 12:40pm UTC](https://root-forum.cern.ch/t/to-find-the-integartion-of-area-drawn-using-graphical-cut/51386/5 "2022-08-29T12:40:16Z")

</div>

sorry didn’t understand it.  
Here I am attaching the root file which contains TSd1rEnergy:TSd2rEnergy histogram and cut1.c is my graphical cut file.  
Help will be appreciated.  
[cut1.c](https://root-forum.cern.ch/uploads/short-url/htdtqmysvePcDHqXXo3KTRlFRu7.c) (638 Bytes)  
[output.root](https://root-forum.cern.ch/uploads/short-url/Dt3csP8lkJbZJ3tIV6fstL2Pre.root) (34.5 KB)

---

<div class="post-metadata">

**Author:** ![Dilicus](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dilicus/32/14338_2.png) [@Dilicus](https://root-forum.cern.ch/u/Dilicus)\
**Post date:** [August 29, 2022, 5:41pm UTC](https://root-forum.cern.ch/t/to-find-the-integartion-of-area-drawn-using-graphical-cut/51386/6 "2022-08-29T17:41:16Z")

</div>

Try this code

```auto
void count_hist()
{

   TCutG *cutg = new TCutG("CUTG",10);
   cutg->SetVarX("plot");
   cutg->SetVarY("");
   cutg->SetTitle("Graph");
   cutg->SetFillStyle(1000);
   cutg->SetPoint(0,2449.88,791.422);
   cutg->SetPoint(1,2637.08,840.748);
   cutg->SetPoint(2,2637.08,840.748);
   cutg->SetPoint(3,2721.62,694.914);
   cutg->SetPoint(4,2649.15,568.382);
   cutg->SetPoint(5,2471.01,602.696);
   cutg->SetPoint(6,2471.01,602.696);
   cutg->SetPoint(7,2446.86,787.132);
   cutg->SetPoint(8,2449.88,787.132);
   cutg->SetPoint(9,2449.88,791.422);
   cutg->Draw("");
    
    
    TFile *f=new TFile("output.root","open");
    
    TH2F *h = (TH2F*)g->Get("h");
    
    int i,j=0;
    double sum=0;
    
    for(i=1;i<=h->GetNbinsX();i++)
    {
        for(j=1;j<=h->GetNbinsY();j++)
        {
            
            if(cutg->IsInside())
                sum+=h->GetBinContent(i,j);
            
            
        }
    }
    
    cout<<"Event inside the CutG "<<sum;
    
}

```

---

<div class="post-metadata">

**Author:** ![Dilicus](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dilicus/32/14338_2.png) [@Dilicus](https://root-forum.cern.ch/u/Dilicus)\
**Post date:** [August 29, 2022, 5:44pm UTC](https://root-forum.cern.ch/t/to-find-the-integartion-of-area-drawn-using-graphical-cut/51386/7 "2022-08-29T17:44:21Z")

</div>

In my previous message I supposed you had the data used to fill the histogram.

So if you had the CutG and the data you can just fill an histogram with data inside the CutG

---

<div class="post-metadata">

**Author:** ![gurmukh\_dhindsa](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/gurmukh_dhindsa/32/30326_2.png) [@gurmukh\_dhindsa](https://root-forum.cern.ch/u/gurmukh_dhindsa)\
**Post date:** [August 29, 2022, 6:01pm UTC](https://root-forum.cern.ch/t/to-find-the-integartion-of-area-drawn-using-graphical-cut/51386/8 "2022-08-29T18:01:10Z")

</div>

![Screenshot from 2022-08-29 15-00-29](https://root-forum.cern.ch/uploads/default/original/3X/e/3/e310e5491d0a2ceb52ac675ed9e84b1a80e25c66.png)  
**I am getting this error now.**

---

<div class="post-metadata">

**Author:** ![Dilicus](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dilicus/32/14338_2.png) [@Dilicus](https://root-forum.cern.ch/u/Dilicus)\
**Post date:** [August 29, 2022, 6:03pm UTC](https://root-forum.cern.ch/t/to-find-the-integartion-of-area-drawn-using-graphical-cut/51386/9 "2022-08-29T18:03:01Z")

</div>

sorry few small errors

```auto
void count_hist()
{

   TCutG *cutg = new TCutG("CUTG",10);
   cutg->SetVarX("plot");
   cutg->SetVarY("");
   cutg->SetTitle("Graph");
   cutg->SetFillStyle(1000);
   cutg->SetPoint(0,2449.88,791.422);
   cutg->SetPoint(1,2637.08,840.748);
   cutg->SetPoint(2,2637.08,840.748);
   cutg->SetPoint(3,2721.62,694.914);
   cutg->SetPoint(4,2649.15,568.382);
   cutg->SetPoint(5,2471.01,602.696);
   cutg->SetPoint(6,2471.01,602.696);
   cutg->SetPoint(7,2446.86,787.132);
   cutg->SetPoint(8,2449.88,787.132);
   cutg->SetPoint(9,2449.88,791.422);
   cutg->Draw("");
    
    
    TFile *f=new TFile("output.root","open");
    
    TH2F *h = (TH2F*)f->Get("h");
    
    int i,j=0;
    double sum=0;
    
    for(i=1;i<=h->GetNbinsX();i++)
    {
        for(j=1;j<=h->GetNbinsY();j++)
        {
            
            if(cutg->IsInside( ((TAxis*)h->GetXaxis())->GetBinCenter(i) , ((TAxis*)h->GetYaxis())->GetBinCenter(j) ))
                sum+=h->GetBinContent(i,j);
            
            
        }
    }
    
    cout<<"Event inside the CutG "<<sum;
    
}

```

---

<div class="post-metadata">

**Author:** ![gurmukh\_dhindsa](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/gurmukh_dhindsa/32/30326_2.png) [@gurmukh\_dhindsa](https://root-forum.cern.ch/u/gurmukh_dhindsa)\
**Post date:** [August 29, 2022, 6:10pm UTC](https://root-forum.cern.ch/t/to-find-the-integartion-of-area-drawn-using-graphical-cut/51386/10 "2022-08-29T18:10:56Z")

</div>

Sorry, I am again getting an error

 ![Screenshot from 2022-08-29 15-10-11](https://root-forum.cern.ch/uploads/default/original/3X/4/b/4bc6406ecadab8ecca5f5e9f3de80c8889cc293f.png)

---

<div class="post-metadata">

**Author:** ![Dilicus](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dilicus/32/14338_2.png) [@Dilicus](https://root-forum.cern.ch/u/Dilicus)\
**Post date:** [August 29, 2022, 6:23pm UTC](https://root-forum.cern.ch/t/to-find-the-integartion-of-area-drawn-using-graphical-cut/51386/11 "2022-08-29T18:23:29Z")

</div>

Just change h2 with h inside the if

---

<div class="post-metadata">

**Author:** ![gurmukh\_dhindsa](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/gurmukh_dhindsa/32/30326_2.png) [@gurmukh\_dhindsa](https://root-forum.cern.ch/u/gurmukh_dhindsa)\
**Post date:** [August 29, 2022, 6:25pm UTC](https://root-forum.cern.ch/t/to-find-the-integartion-of-area-drawn-using-graphical-cut/51386/12 "2022-08-29T18:25:27Z")

</div>

![Screenshot from 2022-08-29 15-24-36](https://root-forum.cern.ch/uploads/default/original/3X/f/f/ff7bd13f46cbb4edaecfb22349a9c2f2ab633e2e.png)  
I did that, but still nothing. Appreciating your help.

---

<div class="post-metadata">

**Author:** ![Dilicus](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dilicus/32/14338_2.png) [@Dilicus](https://root-forum.cern.ch/u/Dilicus)\
**Post date:** [August 29, 2022, 6:27pm UTC](https://root-forum.cern.ch/t/to-find-the-integartion-of-area-drawn-using-graphical-cut/51386/13 "2022-08-29T18:27:48Z")

</div>

Very sorry in the if you need a , instead of &&.  
I will edit the code above

---

<div class="post-metadata">

**Author:** ![gurmukh\_dhindsa](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/gurmukh_dhindsa/32/30326_2.png) [@gurmukh\_dhindsa](https://root-forum.cern.ch/u/gurmukh_dhindsa)\
**Post date:** [August 29, 2022, 6:38pm UTC](https://root-forum.cern.ch/t/to-find-the-integartion-of-area-drawn-using-graphical-cut/51386/14 "2022-08-29T18:38:07Z")

</div>

Hi there, very big thanks. Code is working now. I have tested it
