# TCutG Histogram Integration

**URL:** <https://root-forum.cern.ch/t/tcutg-histogram-integration/51417>\
**Category:** Newbie\
**Created:** [August 31, 2022, 6:12pm UTC](https://root-forum.cern.ch/t/tcutg-histogram-integration/51417 "2022-08-31T18:12:17Z")\
**Posts on this page:** 2\
**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 31, 2022, 6:12pm UTC](https://root-forum.cern.ch/t/tcutg-histogram-integration/51417/1 "2022-08-31T18:12:17Z")

</div>

Hi there,  
Can you please provide help me to find the integration of the cut inside the histogram? I am attaching the cut file and histogram root file  
[output3.root](https://root-forum.cern.ch/uploads/short-url/9pPI6oNiTyoMCMEzP1bUNHr75te.root) (123.7 KB)

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

* * *

[mycut.c](https://root-forum.cern.ch/uploads/short-url/pnE1bqSVA7Mat1BQ82D9NdoHscc.c) (1.2 KB)

---

<div class="post-metadata">

**Author:** ![jonas](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jonas/32/24756_2.png) [@jonas](https://root-forum.cern.ch/u/jonas)\
**Post date:** [September 1, 2022, 1:09pm UTC](https://root-forum.cern.ch/t/tcutg-histogram-integration/51417/3 "2022-09-01T13:09:40Z")

</div>

Hi @gurmukh_dhindsa, thanks for asking!

Based on [this older forum post](https://root-forum.cern.ch/t/creating-new-th2-from-contents-of-tcutg/17359), I think the best you can do is to iterate over the bins, and add the bin content if the bin is inside the cut:

```c++
void testmycut() {

   auto file = TFile::Open("output3.root", "READ");

   auto h = file->Get<TH2F>("h");

   std::cout << "Integral of full histogram : " << h->Integral() << std::endl;

   TCutG *cutg = new TCutG("CUTG",23);
   cutg->SetVarX("plot");
   cutg->SetVarY("");
   cutg->SetTitle("Graph");
   cutg->SetFillStyle(1000);
   cutg->SetLineColor(2);
   cutg->SetLineWidth(3);
   cutg->SetPoint(0,2582.61,787.149);
   cutg->SetPoint(1,2615.72,772.773);
   cutg->SetPoint(2,2615.72,772.773);
   cutg->SetPoint(3,2628.35,723.483);
   cutg->SetPoint(4,2628.35,723.483);
   cutg->SetPoint(5,2645.34,702.261);
   cutg->SetPoint(6,2651.01,675.562);
   cutg->SetPoint(7,2659.72,654.34);
   cutg->SetPoint(8,2639.25,629.01);
   cutg->SetPoint(9,2590.89,617.372);
   cutg->SetPoint(10,2549.5,626.272);
   cutg->SetPoint(11,2549.5,626.272);
   cutg->SetPoint(12,2510.29,659.816);
   cutg->SetPoint(13,2510.29,659.816);
   cutg->SetPoint(14,2503.32,713.899);
   cutg->SetPoint(15,2512.9,735.805);
   cutg->SetPoint(16,2512.9,735.805);
   cutg->SetPoint(17,2527.28,745.39);
   cutg->SetPoint(18,2543.83,772.773);
   cutg->SetPoint(19,2562.13,790.572);
   cutg->SetPoint(20,2584.35,787.149);
   cutg->SetPoint(21,2583.04,787.149);
   cutg->SetPoint(22,2582.61,787.149);

   double integral = 0;

   for (int i=1; i<=h->GetNbinsX(); i++) {
      for (int j=1; j<=h->GetNbinsY(); j++) {
         double xc = h->GetXaxis()->GetBinCenter(i);
         double yc = h->GetYaxis()->GetBinCenter(j);
         if (cutg->IsInside(xc, yc)) {
            integral += h->GetBinContent(i, j);
         }
      }
   }

   std::cout << "Integral inside cut region : " << integral << std::endl;

   TCanvas c1{};
   h->Draw();
   cutg->Draw("SAME");
   c1.SaveAs("plot.png");

   delete file;
}

```

The output is:

```auto
Integral of full histogram : 662288
Integral inside cut region : 175007

```

 ![plot](https://root-forum.cern.ch/uploads/default/original/3X/9/7/970f41567f2b195e9e0b785d52b73486aa1665a1.png)

Hope this helps!

Jonas
