# Creating new TH2 from contents of TCutG

**URL:** <https://root-forum.cern.ch/t/creating-new-th2-from-contents-of-tcutg/17359>\
**Category:** ROOT\
**Created:** [March 16, 2014, 9:40am UTC](https://root-forum.cern.ch/t/creating-new-th2-from-contents-of-tcutg/17359 "2014-03-16T09:40:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![domrafferty](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@domrafferty](https://root-forum.cern.ch/u/domrafferty)\
**Post date:** [March 16, 2014, 9:40am UTC](https://root-forum.cern.ch/t/creating-new-th2-from-contents-of-tcutg/17359/1 "2014-03-16T09:40:32Z")

</div>

Hi there,

I am wondering if there is an easy way to create a new TH2 containing only the events contained within a TCutG as applied to another TH2. I am aware that the draw function does this, but I want to be able to do it without having to draw the new histogram. For example, the TH2-\>ProjectionX/Y() methods allow you to simply specify the name of the cut, then returns a new TH1 containing only the cut contents, without having to draw it.

Thanks

D

---

<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:** [March 17, 2014, 10:29am UTC](https://root-forum.cern.ch/t/creating-new-th2-from-contents-of-tcutg/17359/2 "2014-03-17T10:29:06Z")

</div>

Hi,

We don’t have a method for doing this in ROOT, but it is not complicated to make a new histogram with the bins filled only inside the graphical cut.  
You should just loop on all bins and copy the content only for the bins which have the function  
TCutg::IsInside(xbincenter, ybincenter) returning true.

Best Regards

Lorenzo

---

<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:** [March 17, 2014, 1:29pm UTC](https://root-forum.cern.ch/t/creating-new-th2-from-contents-of-tcutg/17359/3 "2014-03-17T13:29:41Z")

</div>

I found an example you can take inspiration from. hsimple.root is generated by $ROOTSYS/tutorials/hsimple.C

```auto
{
   TCutG *gcut = new TCutG("gcut",8);
   gcut->SetPoint(0,-0.646552,0.932203);
   gcut->SetPoint(1,-1.26437,0.105932);
   gcut->SetPoint(2,-0.574713,-1.10169);
   gcut->SetPoint(3,0.948276,-0.338983);
   gcut->SetPoint(4,1.07759,0.720339);
   gcut->SetPoint(5,-0.316092,-0.0847458);
   gcut->SetPoint(6,-0.45977,0.402542);
   gcut->SetPoint(7,-0.646552,0.932203);

   TFile f("hsimple.root");
   TH2F *hpxpy = (TH2F*)f.Get("hpxpy");

   Int_t i,j;
   Int_t nx = hpxpy->GetNbinsX();
   Int_t ny = hpxpy->GetNbinsY();
   Double_t area = 0;
   Double_t xc,yc;

   for (i=1; i<=nx; i++) {
      for (j=1; j<=nx; j++) {
         xc = hpxpy->GetXaxis()->GetBinCenter(i);
         yc = hpxpy->GetYaxis()->GetBinCenter(i);
         if (gcut->IsInside(xc, yc)) {
            area = area + hpxpy->GetXaxis()->GetBinWidth(i)*
                          hpxpy->GetYaxis()->GetBinWidth(j);
         }
      }
   }
   printf("Area defined by gcut = %d\n",area);
}

```
