Home | News | Documentation | Download

Converting a TH2D to a Matrix for Inversion

Hi all,

From corresponding reconstructed and true values I’ve built a TH2D Histogram that illustrates an energy smearing. I’d like to convert the TH2D to a Matrix in order to be able to build the inverted matrix and put it back into a TH2D for further calculations within my analysis.

I have not been able to find examples doing this.
Can someone point me to relevant examples?


ROOT Version: 6.22
Platform: macosx64

Hi @fstocker,

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

However, the following was suggested in RootTalk here:

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

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


Sounds good to me. TMatrix has an “Invert” method.

Since you are using a TH2D and not TH2F, teh correct code is this one:

H2D histo(...);
// ...
TMatrixD m(nbinsx+2, nbinsy+2, histo.GetArray(), "D")

Also note that the resulting matrix will contain also the underflow and overflow.
If you want to exclude, just do from the previous obtained matrix:

TMatrixD m2 = m.GetSub(1,nbinsx,1,nbinsy)

and now you will obtaioned a matrix containining only the bin contents in the histogram axis range.


Thank you for the replies @moneta, @couet & @jalopezg!
I tried and they work for implementation, unfortunately my matrix is singular :grimacing: :thinking: and results with a pseudo inverse also do not look satisfying. I guess I’ll have to do some chi2 method