# Matrix multiplication in two files

**URL:** <https://root-forum.cern.ch/t/matrix-multiplication-in-two-files/51730>\
**Category:** Newbie\
**Created:** [September 27, 2022, 4:24pm UTC](https://root-forum.cern.ch/t/matrix-multiplication-in-two-files/51730 "2022-09-27T16:24:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dil](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dil/32/26887_2.png) [@Dil](https://root-forum.cern.ch/u/Dil)\
**Post date:** [September 27, 2022, 4:24pm UTC](https://root-forum.cern.ch/t/matrix-multiplication-in-two-files/51730/1 "2022-09-27T16:24:53Z")

</div>

Dear experts,  
This is kind of simple question for you.  
I have 120(6X6 matrix) TMatrixD(from generated) and 120 TvectorD(from data) made from two different runs.  
I can print them in the terminal /copy and paste into a txt file.  
I need to multiply these two(Aj= Mij\* Ai), what is the easy way to do that?  
No idea how to multiply matrix and vector in two different files.  
is there a way to store Matrices/vectors in .root file ?

```auto
TMatrixD matrixGEN(6,6);
TVectorD Data_array_vec(6);

```

Any idea/code would be greatly appreciated.

Thank You  
Dil

---

<div class="post-metadata">

**Author:** ![RENATO\_QUAGLIANI](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/renato_quagliani/32/19496_2.png) [@RENATO\_QUAGLIANI](https://root-forum.cern.ch/u/RENATO_QUAGLIANI)\
**Post date:** [September 27, 2022, 6:04pm UTC](https://root-forum.cern.ch/t/matrix-multiplication-in-two-files/51730/2 "2022-09-27T18:04:34Z")

</div>

I might be wrong , but since TMatrix inherits from TObject, you can write it to a TFile and retrieve it back.  
Have you tried to write the object to a TFile and check you can get it back when reading the file?

```auto
import ROOT as r
fOut = r.TFile("test_save.root","RECREATE")
rows = 5
cols = 6
matrix = r.TMatrixD(5, 6)
matrix[0][1] = 1
matrix.Print()
fOut.WriteObject( matrix,"mymatrix")
fOut.Close()
fIn = r.TFile("test_save.root")
matrixRead = fIn.Get("mymatrix")
matrixRead.Print()
fIn.Close()

```

Tried on a notebook and there is no problem to write a matrix to a file and read it back

---

<div class="post-metadata">

**Author:** ![Dil](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dil/32/26887_2.png) [@Dil](https://root-forum.cern.ch/u/Dil)\
**Post date:** [September 27, 2022, 6:34pm UTC](https://root-forum.cern.ch/t/matrix-multiplication-in-two-files/51730/3 "2022-09-27T18:34:11Z")

</div>

Thank You [RENATO\_QUAGLIANI](https://root-forum.cern.ch/t/matrix-multiplication-in-two-files/51730/2).  
I will try this way.
