How to Count Events within an Odd-Shaped Region?

Hello everyone,

I am trying to count the number of events within an odd-shaped area of a 2D histogram. The area is indicated below shown in bold to distinguish from other boxes.

image

Is there a version of the Integral() function that can count the number of events of a skewed shaped like this?

Best Regards,

Juan


Please read tips for efficient and successful posting and posting code

ROOT Version: 5.34
Platform: Ubuntu 18.04 LTS
Compiler: g++ 4.4.7


I don’t know if there is a dedicated function, but what you can do is:
1 - define the closed polygon as a TCutg (https://root.cern/doc/master/classTCutG.html)
2 - loop over the histogram bins, get the bin centres and use IsInside() to check whether the point is inside the area; if it is, get its contents and add them.

  for (Int_t ix=1; ix<Nbx; ++ix) {
    for (Int_t iy=1; iy<Nby; ++iy) {
      x = h->GetXaxis()->GetBinCenter(ix);
      y = h->GetYaxis()->GetBinCenter(iy);
      if (cutg->IsInside(x,y)) {
        // add the bin contents
      }
    }
  }

Thank you, this did the job for me.

EDIT: I also found a method in TCutG that calculates the integral within a 2D histogram. Which work even better for my purpose.

https://root.cern.ch/doc/master/classTCutG.html#a06bcd20bcfdded1d14e791fe7847de03

1 Like