# Converting a TH2D to a Matrix for Inversion

**URL:** <https://root-forum.cern.ch/t/converting-a-th2d-to-a-matrix-for-inversion/44689>\
**Category:** Newbie\
**Created:** [May 3, 2021, 12:46pm UTC](https://root-forum.cern.ch/t/converting-a-th2d-to-a-matrix-for-inversion/44689 "2021-05-03T12:46:18Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![jalopezg](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jalopezg/32/25844_2.png) [@jalopezg](https://root-forum.cern.ch/u/jalopezg)\
**Post date:** [May 3, 2021, 1:27pm UTC](https://root-forum.cern.ch/t/converting-a-th2d-to-a-matrix-for-inversion/44689/2 "2021-05-03T13:27:06Z")

</div>

Hi @fstocker,

This forum post looks similar to what you are trying to attain.

> [@TMatrixD Inversion Issue](https://root-forum.cern.ch/t/tmatrixd-inversion-issue/26255):
>
> Hi all- I have a covariance matrix that I need to invert to use for a Chi-2 calculation (X^2 = M^(T) \* C^(-1) \* M). I first convert the covariance matrix from a TH2 to a TMatrixD. Then I invert it and assigned the inverted matrix back to another TH2. I then attempt to multiply the two TH2’s (doing standard matrix multiplication) to get the identity. What I get is nonsense. The TH2’s I’m multiplying are hCovMatrixTotal\_alt and hCovMatrixTotalInv. I keep in mind that TH2’s are enumerated from th…

However, the following was suggested in RootTalk [here](https://root.cern.ch/root/roottalk/roottalk02/3283.html):

```C++
TH2D histo(...);
// ...
TMatrix m(nbinsx+2, nbinsy+2, histo.GetArray(), "F")

```

Maybe @couet and/or @moneta can confirm the validity of the last method?

Cheers,  
J.

---

_[View the full topic](https://root-forum.cern.ch/t/converting-a-th2d-to-a-matrix-for-inversion/44689)._
