# Non-rectangular region integration

**URL:** <https://root-forum.cern.ch/t/non-rectangular-region-integration/23765>\
**Category:** Roofit and RooStats\
**Created:** [February 15, 2017, 4:31pm UTC](https://root-forum.cern.ch/t/non-rectangular-region-integration/23765 "2017-02-15T16:31:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mgv4ce](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/8edcca/32.png) [@mgv4ce](https://root-forum.cern.ch/u/mgv4ce)\
**Post date:** [February 15, 2017, 4:31pm UTC](https://root-forum.cern.ch/t/non-rectangular-region-integration/23765/1 "2017-02-15T16:31:59Z")

</div>

Hi experts,  
I would like to do a 2D functional integration with 2D bounds. That is, I would like to integrate over a non-rectangular region, with things like AdaptiveIntegratorMultiDim or GSLMCIntegrator. Is this possible? It seems that only rectangular regions are supported, but I want to make sure.

Many thanks,  
Mike

---

<div class="post-metadata">

**Author:** ![Pepe\_Le\_Pew](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pepe_le_pew/32/12365_2.png) [@Pepe\_Le\_Pew](https://root-forum.cern.ch/u/Pepe_Le_Pew)\
**Post date:** [February 15, 2017, 6:52pm UTC](https://root-forum.cern.ch/t/non-rectangular-region-integration/23765/2 "2017-02-15T18:52:02Z")

</div>

some ideas: [Boundary integral](https://root-forum.cern.ch/t/boundary-integral/17872/1)  
some warnings: [Creating TF1 from TGraph](https://root-forum.cern.ch/t/creating-tf1-from-tgraph/10522/10) and [Convolution of two TF1](https://root-forum.cern.ch/t/convolution-of-two-tf1/23625/8)

---

<div class="post-metadata">

**Author:** ![mgv4ce](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/8edcca/32.png) [@mgv4ce](https://root-forum.cern.ch/u/mgv4ce)\
**Post date:** [February 16, 2017, 7:42pm UTC](https://root-forum.cern.ch/t/non-rectangular-region-integration/23765/3 "2017-02-16T19:42:35Z")

</div>

That is very helpful. Since I have a slightly different problem, I run it past you:

The boundaries are different: I need x\>1-0.5\*y-0.5\*sqrt(y^2-a^2). This is easily done by changing the example a bit. See below:

```cpp
double f2(const double *X)
{
//BRUTAL METHOD:
//if(x<=1.0 - 0.5*y - 0.5*sqrt(y*y-a*a)) return 0;

//GOOD METHOD
  double f = 0.0;
  double t = X[0]; // "t" (i.e. the original "x")
  double y = X[1]; // "y"
  double x = (0.5*y + 0.5*sqrt(y*y-a*a)) * t + (1.0 - 0.5*y - 0.5*sqrt(y*y-a*a)); // calculate new "x"
  //when t=0, x = 1.0 - 0.5*y - 0.5*sqrt(y*y-a*a), i.e. the lower bound.
  //when t=1, x=1
  
  // calculate the original function "f(x,y)"
  f = 1.0/(x+y-1.0-a*a/4);
  
  f *= (0.5*y + 0.5*sqrt(y*y-a*a)); // multiply the result by "dx/dt"
  return f;
}

```

The real follow-up question is whether this is also applicable to TFoam densities. I see that it is applicable to all cases of simple integration, but TFoam is sampling from the distribution also. I see no reason why the method should not work (it is just calculus I guess). But do the experts see some problem with shifting basis in this way for the density function in TFoam? Explicitly, could I set the function above via TFoam-\>SetRho(f2), and expect better sampling than the brutal method?

Of course I will experiment with it, but any expert comments are welcome.

Many Thanks!

EDIT. I added part of the function I am sampling/integrating to show that there is at least a potential problem with divergence.

---

<div class="post-metadata">

**Author:** ![mgv4ce](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/8edcca/32.png) [@mgv4ce](https://root-forum.cern.ch/u/mgv4ce)\
**Post date:** [February 16, 2017, 11:29pm UTC](https://root-forum.cern.ch/t/non-rectangular-region-integration/23765/4 "2017-02-16T23:29:46Z")

</div>

It seems that since my function is singular if you get too far out of the region, that the change of variables does not work.

The constraint x\>1 - 0.5\*y - 0.5\*sqrt(y\*y-a\*a) results in x+y\>1+a\*a/4+O(a^4). So the constraint keeps the denominator from becoming singular. When I change variables, the formula is evaluated at a singular value.

I don’t immediately see how to avoid this. I keep looking. Any thoughts?

---

<div class="post-metadata">

**Author:** ![mgv4ce](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/8edcca/32.png) [@mgv4ce](https://root-forum.cern.ch/u/mgv4ce)\
**Post date:** [February 17, 2017, 4:15pm UTC](https://root-forum.cern.ch/t/non-rectangular-region-integration/23765/5 "2017-02-17T16:15:15Z")

</div>

I was wrong about the singular values and solved it. (Was due to y values leaving valid regions).

I checked TFoam, and it seems I must apply the original transformation in the sampling phase if I expect to get sampled data in the original x,y.
