Trouble with definite integral calculating probabilities I cannot solve this:
$$\int_{-\frac{\pi }{2}}^{\frac{\pi }{2}} \tan ^{-1}(a+\tan (x)) \, dx$$
it apeared when trying to find out the probability:
$$P\{\tan a - \tan b \leq 2x\},\ \   0 < x < 1\sqrt{3}$$
Knowing that the joint distribution $f(a,b)$ is
$f(a,b) = \frac{2}{\pi^2}$ in the region $-\pi/2 < b < a < \pi/2$ (triangle)
"$a$" has marginal density:
$\frac{2x+\pi}{\pi^2}$ where $-\pi/2 < a < \pi/2$.
and "$b$" has density: $\frac{\pi-2x}{\pi^2}$ being $ -\pi/2 < b < a$.
 A: $$\tan{u} = a + \tan{x} \implies x=\arctan{(\tan{u}-a)} \implies dx = \frac{\sec^2{u}}{1+(\tan{u}-a)^2} du $$
Then the integral is equal to 
$$\begin{align}\int_{-\pi/2}^{\pi/2} du \frac{u \sec^2{u}}{1+(\tan{u}-a)^2} &= \int_{-\pi/2}^{\pi/2} du \frac{u}{\cos^2{u} + (\sin{u}-a \cos{u})^2} \\ &= \frac12 \int_{-\pi}^{\pi} dv \frac{v}{2+a^2+a^2 \cos{v}-2 a \sin{v}} \end{align}$$
Let
$$J(\beta) = -\frac{i}{2} \int_{-\pi}^{\pi} dv \frac{e^{i \beta v}}{2+a^2+a^2 \cos{v}-2 a \sin{v}} $$
Then the integral we seek is $J'(0)$.
Consider the integral
$$-\oint_C dz \frac{z^{\beta}}{(a^2+i 2 a) z^2+(a^2+2) z+(a^2-i 2 a)} $$
where $C$ is the contour pictured below:

i.e., a unit circle with a keyhole about the negative real axis.  The contour integral is then
$$J(\beta) - i 2\sin{\pi \beta} \int_0^1 dx \frac{x^{\beta}}{(a^2+i 2 a) x^2-(a^2+2) x+(a^2-i 2 a)}$$
This is also equal to $-i 2 \pi$ times the residue at the pole $z_0=-a/(a+i 2)$ inside $C$, which is $z_0^{\beta}/2$.  Thus,
$$\begin{align}J'(0) &= i 2 \pi \int_0^1 \frac{dx}{(a^2+i 2 a) x^2-2 (a^2+2) x+(a^2-i 2 a)} - i \frac{\pi}{2} \log{z_0} \\ &= \frac{1}{2} \pi  \arctan\left(\frac{a}{2}\right)-\frac{1}{4} i \pi  \log \left(\frac{4}{a^2}+1\right) + \frac{1}{2} \pi  \arctan\left(\frac{a}{2}\right)+\frac{1}{4} i \pi  \log \left(\frac{4}{a^2}+1\right) \\ &= \pi \arctan{\left ( \frac{a}{2}\right )} \end{align}$$
A: An approach that is less elegant but more elementary than residues.
Let
$$
J(b) = \int_{-\pi/2}^{\pi/2} \tan^{-1} \left( 2b + \tan x \right) dx.
$$
Then $J(0) = 0$ and using the substitution $\tan x = t$,
$$
\begin{align}
J'(b) &= \int_{-\infty}^\infty \frac{2}{(1+t^2)(1+(t+2b)^2)} dt
= \int_{-\infty}^\infty \frac{dt}{b(1+b^2)}\left[ \frac{3 b+t}{4 b^2+4 b t+t^2+1}+\frac{b-t}{t^2+1}\right]
\\&= \left.\frac{dt}{2b(1+b^2)}\left[ \log \left(\frac{4 b^2+4 b t+t^2+1}{t^2+1}\right)+2 b \tan ^{-1}(t)+2 b \tan ^{-1}(2 b+t)\right] \right\lvert_{-\infty}^\infty
\\&= \frac{\pi}{1+b^2}.
\end{align}
$$
By integrating back,
$$
J(b) = \int_0^b \frac{\pi \,db'}{1+b'^2} = \pi \tan^{-1} b.
$$
A: $\newcommand{\angles}[1]{\left\langle\, #1 \,\right\rangle}
 \newcommand{\braces}[1]{\left\lbrace\, #1 \,\right\rbrace}
 \newcommand{\bracks}[1]{\left\lbrack\, #1 \,\right\rbrack}
 \newcommand{\ceil}[1]{\,\left\lceil\, #1 \,\right\rceil\,}
 \newcommand{\dd}{{\rm d}}
 \newcommand{\ds}[1]{\displaystyle{#1}}
 \newcommand{\dsc}[1]{\displaystyle{\color{red}{#1}}}
 \newcommand{\expo}[1]{\,{\rm e}^{#1}\,}
 \newcommand{\fermi}{\,{\rm f}}
 \newcommand{\floor}[1]{\,\left\lfloor #1 \right\rfloor\,}
 \newcommand{\half}{{1 \over 2}}
 \newcommand{\ic}{{\rm i}}
 \newcommand{\iff}{\Longleftrightarrow}
 \newcommand{\imp}{\Longrightarrow}
 \newcommand{\Li}[1]{\,{\rm Li}_{#1}}
 \newcommand{\norm}[1]{\left\vert\left\vert\, #1\,\right\vert\right\vert}
 \newcommand{\pars}[1]{\left(\, #1 \,\right)}
 \newcommand{\partiald}[3][]{\frac{\partial^{#1} #2}{\partial #3^{#1}}}
 \newcommand{\pp}{{\cal P}}
 \newcommand{\root}[2][]{\,\sqrt[#1]{\vphantom{\large A}\,#2\,}\,}
 \newcommand{\sech}{\,{\rm sech}}
 \newcommand{\sgn}{\,{\rm sgn}}
 \newcommand{\totald}[3][]{\frac{{\rm d}^{#1} #2}{{\rm d} #3^{#1}}}
 \newcommand{\ul}[1]{\underline{#1}}
 \newcommand{\verts}[1]{\left\vert\, #1 \,\right\vert}$
$\ds{\int_{-\pi/2}^{\pi/2}\arctan\pars{a + \tan\pars{x}}\,\dd x
     =\pi\arctan\pars{a \over 2}:\ {\large ?}\,,\qquad a \in {\mathbb R}}$.

Note that
\begin{align}&\color{#66f}{\large%
\int_{-\pi/2}^{\pi/2}\arctan\pars{a + \tan\pars{x}}\,\dd x}
=\Im\int_{-\pi/2}^{\pi/2}\ln\pars{1 + \bracks{a + \tan\pars{x}}\ic}\,\dd x
\end{align}
which let us to avoid the 'double' $\ds{\tt arctan}$-branch cut along $\ds{\braces{y\ic\ \mid\ \verts{y} \geq 1\,,\ y \in {\mathbb R}}}$. With the change of variables
$\ds{\dsc{t} \equiv \dsc{1 + \bracks{a + \tan\pars{x}}\ic}\ \imp\
     \dsc{x}=\dsc{\arctan\pars{-a + \bracks{1 - t}\ic}}}$
\begin{align}&\color{#66f}{\large%
\int_{-\pi/2}^{\pi/2}\arctan\pars{a + \tan\pars{x}}\,\dd x}
=\Im\int_{1 - \infty\ic}^{1 + \infty\ic}\ln\pars{t}\,
{-\ic\,\dd t \over \bracks{-a + \pars{1 - t}\ic}^{\,2} + 1}
\\[5mm]&=\Re\int_{1 - \infty\ic}^{1 + \infty\ic}
{\ln\pars{t}\, \over t^{2} - 2\pars{1 + a\ic}t - a^{2} + 2a\ic}\,\dd t
=\Re\int_{1 - \infty\ic}^{1 + \infty\ic}
{\ln\pars{t}\, \over \pars{t - t_{-}}\pars{t - t_{+}}}\,\dd t
\\[5mm]&\mbox{where}\quad t_{-} \equiv a\ic\,,\quad t_{+} \equiv 2 + a\ic. 
\end{align}

We can choose the ${\tt ln}$-branch cut in such a way that it doesn't 'cross' the line $\ds{x = 1}$. For instance
$$
\ln\pars{z}=\ln\pars{\verts{z}} + \,{\rm Arg}\pars{z}\ic\,,\qquad
\verts{\,{\rm Arg}\pars{z}} < \pi\,,\quad z \not= 0
$$
We close the contour with a semicircle
$\ds{\pars{~\mbox{of radius}\ R > \root{a^{2} + 1}~}}$ to the 'right' of the complex plane such that:
\begin{align}\color{#66f}{\large%
\int_{-\pi/2}^{\pi/2}\arctan\pars{a + \tan\pars{x}}\,\dd x}
&=\Re\bracks{-2\pi\ic\,{\ln\pars{t_{+}}\, \over t_{+} - t_{-}}}
=\pi\,\Im\ln\pars{2 + a\ic}
\\[5mm]&=\left\{\begin{array}{lcl}
\pi\bracks{-\arctan\pars{-\,{a \over 2}}} & \mbox{if} & a \leq 0
\\[2mm]
\pi\arctan\pars{a \over 2} & \mbox{if} & a > 0
\end{array}\right.
\end{align}
The contribution of the above mentioned semicircle vanishes out when
$\ds{R \to \infty}$: Its magnitude is $\ds{\sim\pi\,{\ln\pars{R} \over R}}$ when
$\ds{R \gg \root{a^{2} + 1}}$.
Then,
$$
\color{#66f}{\large%
\int_{-\pi/2}^{\pi/2}\arctan\pars{a + \tan\pars{x}}\,\dd x}
=\color{#66f}{\large\pi\arctan\pars{a \over 2}}
$$
