Creating new TH2 from contents of TCutG

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

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

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

{
   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);
}