Integration of Ricci curvature on a compact manifold. $\mathbf {The \ Problem \ is}:$ Let, $(M,g)$ be a compact Riemannian manifold and $X\in \chi(M).$ Show that $\int Ric_g(X,X)\mu_g=\int ((tr(\nabla_.X))^2-tr(\nabla_.X\circ \nabla_.X))\mu_g.$
$\mathbf {My \  approach}:$ There's a hint: to use $\int_M \Delta f  \mu_g=0$ where $\Delta f$ is Laplacian of $f.$
I started with $\nabla$ is torsion-free .
Define $\operatorname{T}(v)=\nabla_vX$ then $\operatorname{T}:T_pM\to T_pM$ is linear for each $p\in M.$
Let, $p\in M$ and $(U,\{e_i\}_{i=1}^n)$ be a normal neighbourhood around $p.$
Now,$\nabla_X(\nabla_{e_i}X)=\nabla_{\nabla_{e_i}X}X+[X,\nabla_{e_i}X]
\implies \nabla_{e_i}(\nabla_XX)-R(e_i,X)X-\nabla_{[e_i,X]}X=\nabla_{\nabla_{e_i}X}X+[X,\nabla_{e_i}X]
\implies R(e_i,X)X=\nabla_{e_i}(\nabla_XX)-\nabla_{[e_i,X]}X-\nabla_{\nabla_{e_i}X}X-[X,\nabla_{e_i}X]
\implies R(e_i,X)X=\nabla_{e_i}(\nabla_XX)-\nabla_X(\nabla_{e_i}X)-\operatorname{T}\circ \operatorname{T}(e_i)$ where we used $\nabla_{e_i}X=[e_i,X]$ as $\nabla$ is torsion-free and again we use torsion-freeness on two vector fields $X$ and  $[e_i,X].$ Then taking trace of the left side and of $\operatorname{T}\circ \operatorname{T},$ we obtain 2 terms in the expression but I can't proceed after this .
I don't know how to bring $(tr(\nabla_.X))^2.$
Do we need to start with $f(p)=\frac{1}{2}\langle X,X\rangle(p) ?$
Thanks in advance for any help .
 A: Using the abstract index notation, we can rewrite the identity in question as
$$
\int_M Ric_{a b}X^a X^b \mu_g = \int_M \big( (\nabla_a X^a)^2 - (\nabla_a X^b) (\nabla_b X^a) \big) \mu_g
$$
To show that this holds on a closed (compact, without boundary) manifold $M$, equipped with a Riemannian metric $g$, and $\nabla$ being the Levi-Civita connection for $g$, we can start from the definition of the Riemann curvature operator:
$$
Riem_{a b}{}^{c}{}_{d}X^d = \nabla_a \nabla_b X^c - \nabla_b \nabla_a X^c
$$
Taking into account that $Ric_{b d} := Riem_{a b}{}^{a}{}_{d}$ by definition, we get:
$$
Ric_{b d} X^b X^d = X^b \nabla_a \nabla_b X^a - X^b \nabla_b \nabla_a X^a
$$
Renaming the dummy indices of the first term on the right-hand side, we see that
$$
Ric_{b d} X^b X^d = X^a \nabla_b \nabla_a X^b - X^b \nabla_b \nabla_a X^a \tag{1}
$$
By the Leibniz rule, we have the identities
$$
\nabla_b (X^a \nabla_a X^b) = (\nabla_b X^a)(\nabla_a X^b) + X^a \nabla_b \nabla_a X^b \tag{2}
$$
and
$$
\nabla_b (X^b \nabla_a X^a) = (\nabla_b X^b) (\nabla_a X^a) + X^b \nabla_b \nabla_a X^a \tag{3}
$$
which we can use to express the terms on the right-hand side of $(1)$ as follows:
$$
Ric_{b d} X^b X^d = \nabla_b(X^a \nabla_a X^b) - (\nabla_b X^a)(\nabla_a X^b) - \nabla_b(X^b \nabla_a X^a) + (\nabla_b X^b) (\nabla_a X^a)
$$
The left-hand sides of both $(2)$ and $(3)$ are in the divergence form, so the corresponding terms (1-st and 3-rd) integrate to zero over a closed manifold.
