# Retrieve TVectorD from a root file

**URL:** <https://root-forum.cern.ch/t/retrieve-tvectord-from-a-root-file/51744>\
**Category:** Newbie\
**Created:** [September 28, 2022, 2:10pm UTC](https://root-forum.cern.ch/t/retrieve-tvectord-from-a-root-file/51744 "2022-09-28T14:10:49Z")\
**Posts on this page:** 9\
**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 28, 2022, 2:10pm UTC](https://root-forum.cern.ch/t/retrieve-tvectord-from-a-root-file/51744/1 "2022-09-28T14:10:49Z")

</div>

Hello experts,  
I stored TVectorD in a root file.But I cannot receive it from the root file.  
Could someone help me who’s the error?

```auto

void mxtrix_multiplication(){
    
    const int nx=3,nQ2=4,ntmin_t=10,n_weight=6;
    
    
    TFile *hFile2=TFile::Open("testing_1.root");
    hFile2->ls();
    
    char MatrixName_1[64];
    
    TVectorD Data_array_vec(6);
    
    for(int ixB=0; ixB<nx; ixB++){
            for(int iQ2=0; iQ2<nQ2; iQ2++){
            for(int itmin_t=0 ; itmin_t < ntmin_t; itmin_t++){
            
                for(int i_DATA=0; i_DATA < n_weight; i_DATA++){
                    
                    sprintf(MatrixName_1,"Data_array_xB%03d_iQ2%03d_tmin%03d",ixB,iQ2,itmin_t);
                    
                    
                    
            Data_array_vec[i_DATA]= hFile2->Get(MatrixName_1);
                    
                    
                    
                }
                
                Data_array_vec.Print();
                
            }
            }
    }
    
    
}

```

```auto
error: assigning to 'double' from incompatible type 'TObject *'
            Data_array_vec[i_DATA]= hFile2->Get(MatrixName_1);
                                    ~~~~~~~~^~~~~~~~~~~~~~~~~

```

Unfortunately I cannot upload my root file since it’s 5MB.

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [September 28, 2022, 2:18pm UTC](https://root-forum.cern.ch/t/retrieve-tvectord-from-a-root-file/51744/2 "2022-09-28T14:18:14Z")

</div>

May be:

```auto
            Data_array_vec[i_DATA] = (TVectorD*) hFile2->Get(MatrixName_1);

```

assuming `Data_array_vec ` is an array of `TVectorD `.

---

<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 28, 2022, 2:20pm UTC](https://root-forum.cern.ch/t/retrieve-tvectord-from-a-root-file/51744/3 "2022-09-28T14:20:39Z")

</div>

Thanks for the suggestion.But answer is No.

```auto
 error: assigning to 'double' from incompatible type 'TVectorD *' (aka 'TVectorT<double> *')
            Data_array_vec[i_DATA]= (TVectorD*)hFile2->Get(MatrixName_1);

```

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [September 28, 2022, 2:21pm UTC](https://root-forum.cern.ch/t/retrieve-tvectord-from-a-root-file/51744/4 "2022-09-28T14:21:50Z")

</div>

`TVectorD Data_array_vec[n_weight];`

`(Data_array_vec[i_DATA]).ResizeTo(6) = *((TVectorD*)(hFile2->Get(MatrixName_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 28, 2022, 2:28pm UTC](https://root-forum.cern.ch/t/retrieve-tvectord-from-a-root-file/51744/5 "2022-09-28T14:28:47Z")

</div>

Thank You [Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote).  
But I cannot print this.I got following error.

```auto
error: member reference base type 'TVectorD [6]' is not a structure or union
                Data_array_vec.Print();

```

Then I print inside the “i\_Data” loop as `Data_array_vec[i_Data].Print()`;  
But I got wrong vectors.

Error in \<TVectorT::operator=(const TVectorT &)\>: vectors not compatible

Vector (0) is as follows

```
 | 1 |

```

* * *

Error in \<TVectorT::operator=(const TVectorT &)\>: vectors not compatible

Vector (0) is as follows

```
 | 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 28, 2022, 2:40pm UTC](https://root-forum.cern.ch/t/retrieve-tvectord-from-a-root-file/51744/7 "2022-09-28T14:40:51Z")

</div>

Thank You.It’s working now.

---

<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 28, 2022, 2:51pm UTC](https://root-forum.cern.ch/t/retrieve-tvectord-from-a-root-file/51744/9 "2022-09-28T14:51:22Z")

</div>

Thank You @Wile\_E\_Coyote.This is helpful.  
Do I need to follow the same method when retrieve `TMatrixD matrixGEN(6,6);`?  
(for future reference)  
I need to multiply Tmatrix(6 x6) by TVectorD.

matrixGEN(6,6) \* Data\_array\_vex(6)

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [September 28, 2022, 2:55pm UTC](https://root-forum.cern.ch/t/retrieve-tvectord-from-a-root-file/51744/10 "2022-09-28T14:55:47Z")

</div>

`TMatrixD matrixGEN[n_weight];`

`(matrixGEN[i_DATA]).ResizeTo(6, 6) = *((TMatrixD*)(hFile2->Get(MatrixName_1)));`

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [September 28, 2022, 3:28pm UTC](https://root-forum.cern.ch/t/retrieve-tvectord-from-a-root-file/51744/11 "2022-09-28T15:28:17Z")

</div>

Actually, I think you’d better use pointers.

```cpp
TVectorD *Data_array_vec[n_weight];
TMatrixD *matrixGEN[n_weight];

```

```cpp
hFile2->GetObject("saved_vector_name", Data_array_vec[i_DATA]);
hFile2->GetObject("saved_matrix_name", matrixGEN[i_DATA]);

```

` (*matrixGEN[i_DATA]) * (*Data_array_vex[i_DATA]) /*multiplication */`

`delete Data_array_vec[i_DATA]; delete matrixGEN[i_DATA]; /* when no longer needed */`
