Prove that $\int_E |g|^2 \mathop{d \mu} = 2 \int_0^\infty x \, \mu(|g| > x) \mathop{dx}$ Let $(E,\mu)$ be a measure space.

Why for all $g \in L^2(\mu)$, we have
  $$\int_E |g|^2 \mathop{d \mu} = 2 \int_0^\infty x \, \mu(|g| > x) \mathop{dx} ?$$

 A: It is known as Robin's identity in probability theory. In fact, we
can generalize it to the following:
Let $p\in[1,\infty)$ and let $(E,\mathcal{M},\mu)$ be a measure
space. Let $f:E\rightarrow[0,\infty)$ be a measurable function such
that $\int f^{p}\,d\mu<\infty$. Then 
\begin{eqnarray*}
\int f^{p}\,d\mu & = & \int_{0}^{\infty}pt^{p-1}\mu\left(f^{-1}\left[[t,\infty)\right]\right)dt.\\
 & = & \int_{0}^{\infty}pt^{p-1}\mu\left(f^{-1}\left[(t,\infty)\right]\right)dt
\end{eqnarray*}
Proof: Since $\int f^{p}\,d\mu<\infty$, by restricting $\mu$ on
the support of $f$, without loss of generality, we may assume that
$\mu$ is $\sigma$-finite. Let $m$ be the Lebesgue measure on $\mathbb{R}$.
Define a set $A=\{(t,\omega)\mid0<t<f(\omega)\}\subseteq\mathbb{R}\times E$.
Firstly, we show that $A$ is measurable with respect to the product
$\sigma$-algebra. We assert that 
$$
A=\bigcup_{\substack{a,b\in\mathbb{Q}\\
0<a<b
}
}(a,b)\times f^{-1}\left[(b,\infty)\right].
$$
For, let $(t,\omega)\in A$, then $0<t<f(\omega)$. There exist
$a_{0},b_{0}\in\mathbb{Q}$ such that $0<a_{0}<t<b_{0}<f(\omega)$.
Therefore 
$$
(t,\omega)\in(a_{0},b_{0})\times f^{-1}\left[(b_{0},\infty)\right]\subseteq\bigcup_{\substack{a,b\in\mathbb{Q}\\
0<a<b
}
}(a,b)\times f^{-1}\left[(b,\infty)\right].
$$
Conversely, if $(t,\omega)\in\bigcup_{\substack{a,b\in\mathbb{Q}\\
0<a<b
}
}(a,b)\times f^{-1}\left[(b,\infty)\right]$, then there exist $a_{0},b_{0}\in\mathbb{Q}$ with $0<a_{0}<b_{0}$
such that $(t,\omega)\in(a_{0},b_{0})\times f^{-1}\left[(b_{0},\infty)\right]$.
Therefore $0<a_{0}<t<b_{0}<f(\omega)$ and hence $(t,\omega)\in A$.
It follows that 
$$
A=\bigcup_{\substack{a,b\in\mathbb{Q}\\
0<a<b
}
}(a,b)\times f^{-1}\left[(b,\infty)\right].
$$
Note that for each $a,b\in\mathbb{Q}$ with $0<a<b$, $(a,b)\times f^{-1}\left[(b,\infty)\right]$
is a measurable subset (with respect to the product $\sigma$-algebra)
of $\mathbb{R}\times E$. Since the union is countable, $A$ is measurable
with respect to the product $\sigma$-algebra.
Now, we are ready to apply Fubini-Tonelli Theorem. Consider the integral
with respect to the product measure $m\times\mu$
$$
\int ps^{p-1}1_{A}(s,\omega)\,d(m\times\mu)(s,\omega).
$$
On one hand, 
\begin{eqnarray*}
 &  & \int ps^{p-1}1_{A}(s,\omega)\,d(m\times\mu)(s,\omega)\\
 & = & \int\left(\int ps^{p-1}1_{A}(s,\omega)dm(s)\right)d\mu(\omega)\\
 & = & \int\left(\int_{0}^{f(\omega)}ps^{p-1}dm(s)\right)d\mu(\omega)\\
 & = & \int\left(f(\omega)\right)^{p}\,d\mu(\omega).
\end{eqnarray*}
On the other hand, 
\begin{eqnarray*}
 &  & \int ps^{p-1}1_{A}(s,\omega)\,d(m\times\mu)(s,\omega)\\
 & = & \int\left(\int ps^{p-1}1_{A}(s,\omega)d\mu(\omega)\right)dm(s)\\
 & = & \int_{0}^{\infty}\left(ps^{p-1}\mu\left(f^{-1}\left[(s,\infty)\right]\right)\right)dm(s).
\end{eqnarray*}
Therefore 
$$
\int f^{p}d\mu=p\int_{0}^{\infty}s^{p-1}\mu\left(f^{-1}\left[(s,\infty)\right]\right)ds.
$$
Lastly, by replacing $A$ with $A=\{(t,\omega)\mid0\leq t\leq f(\omega)\}$
and repeat the same argument, we can show that 
$$
\int f^{p}d\mu=p\int_{0}^{\infty}s^{p-1}\mu\left(f^{-1}\left[[s,\infty)\right]\right)ds.
$$
