TVector - retrieving an element

Hi,

I have stored a TVector in a ROOT file, but am having problems retrieving the values back from the file.

I can load my ROOT file and then read the TVector from file and create a pointer to it:-

TVector bb = (TVector)_file0->Get(“SimData/PatientOrigin”);

Then issuing the command:-

bb->Print() gives the correct values:-

 |        1  |

0 |-250
1 |-376
2 |8

However, when trying to use the [] operator to retrieve values, such as :-

( (*bb) )[0]
( (*bb) )[1]
( (*bb) )[2]

I get 3 very different values:-
0.0
-3.738
0.0

What is going on here? I have seen this question come up a few more times on the forum but people have not posted back to say if they worked out how to correctly retrieve the values. It seems like this should be very straightfoward, so I am clearly mis-reading the TVector documentation.

Grateful for any help,
Nick.

Hi,

I could not recreate the problem (root6.03/02).

Look at this dump from my root session:

root [0] TVector foo(3);
root [1] foo[0] = -250
(float) -2.500000e+02
root [2] foo[1] = -376
(float) -3.760000e+02
root [3] foo[2] = 8
(float) 8.000000e+00
root [4] foo.Print()

Vector (3)  is as follows

     |        1  |
------------------
   0 |-250 
   1 |-376 
   2 |8 

root [5] TFile *f = TFile::Open("test.root","recreate")
(class TFile *) 0x28ec0e0
root [6] foo.Write()
(Int_t) 96
root [7] f->Close()
root [8] .q

root -l

root [0] TFile *f = TFile::Open("test.root")
(class TFile *) 0x2d42fe0
root [1] f->ls()
TFile**         test.root
 TFile*         test.root
  KEY: TVectorT<float>  TVectorT<float>;1
root [2] TVector *bb = (TVector*)f->Get("TVectorT<float>")
(TVector *) 0x34b4fd0
root [3] bb->Print()

Vector (3)  is as follows

     |        1  |
------------------
   0 |-250 
   1 |-376 
   2 |8 

root [4] ( (*bb) )[0]
(float) -2.500000e+02
root [5] ( (*bb) )[1]
(float) -3.760000e+02
root [6] ( (*bb) )[2]
(float) 8.000000e+00

Maybe you could try such a sequence too in order to make sure it is not
something with the stored data.

Cheers,
Jochen

Hi again,

I think I know what’s wrong. The Vector in your File is a

TVectorD and not a TVector.

Cheers,
Jochen

Jochen,

Brilliant. This was exactly the problem. Thanks for pointing this out.

Nick.