# Writting and Getting a std::vector from a TFile

**URL:** <https://root-forum.cern.ch/t/writting-and-getting-a-std-vector-from-a-tfile/26207>\
**Category:** ROOT\
**Created:** [September 13, 2017, 5:36pm UTC](https://root-forum.cern.ch/t/writting-and-getting-a-std-vector-from-a-tfile/26207 "2017-09-13T17:36:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jasot](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/e495f1/32.png) [@jasot](https://root-forum.cern.ch/u/jasot)\
**Post date:** [September 13, 2017, 5:36pm UTC](https://root-forum.cern.ch/t/writting-and-getting-a-std-vector-from-a-tfile/26207/1 "2017-09-13T17:36:35Z")

</div>

Hi,

I’m trying to save a std::vector in a root file, and it looks like it is well saved (I don’t get any error), but I don’t know how to read it again.

I copy some code reproducing the error:

```
TFile fout;
fout.Open("TEST.root","RECREATE");

std::vector<Double_t> xx; // I create and fill the vector to be saved.
for( int k = 0; k <3; k++ ) xx.push_back(k);
fout.WriteObject(&xx,"xx"); // I store the vector in the TFile
fout.Close();

TFile fin;
fin.Open("TEST.root","READ");
std::vector<Double_t> *yy;
fin.GetObject("xx",yy); // I try to retrieve the vector

```

And this is the error I get:

> Error in TFile::ReadBuffer: error reading all requested bytes from file TEST.root, got 214 of 300  
> Error in TFile::Init: TEST.root failed to read the file type data.

Thank you very much in advance for you help!

J

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [September 14, 2017, 6:51am UTC](https://root-forum.cern.ch/t/writting-and-getting-a-std-vector-from-a-tfile/26207/2 "2017-09-14T06:51:32Z")

</div>

Try this way (`TFile::Open()` is a static method):

```auto
   TFile *fout = TFile::Open("TEST.root", "RECREATE");

   std::vector<Double_t> xx; // I create and fill the vector to be saved.
   for (int k = 0; k<3; k++) xx.push_back(k);
   fout->WriteObject(&xx, "xx"); // I store the vector in the TFile
   fout->Close();

   TFile *fin = TFile::Open("TEST.root", "READ");
   std::vector<Double_t> *yy;
   fin->GetObject("xx", yy); // I try to retrieve the vector
   for(std::vector<Double_t>::iterator it = yy->begin(); it != yy->end(); ++it) {
      std::cout << *it << '\n';
   }

```

Cheers, Bertrand.

---

<div class="post-metadata">

**Author:** ![jasot](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/e495f1/32.png) [@jasot](https://root-forum.cern.ch/u/jasot)\
**Post date:** [September 14, 2017, 11:53am UTC](https://root-forum.cern.ch/t/writting-and-getting-a-std-vector-from-a-tfile/26207/3 "2017-09-14T11:53:02Z")

</div>

It works! Thank you!

I would prefer a way to write and retrieve to the same kind of class, instead to get it into a pointer. I mean to do something like:

std::vector\<Double\_t\> yy;  
fin-\>GetObject(“xx”, yy);

Instead of:

std::vector\<Double\_t\> \*yy;  
fin-\>GetObject(“xx”, yy);

But the first doesn’t seems to be available.  
Anyway like this is fine. Thanks again!

J.

---

<div class="post-metadata">

**Author:** ![sbinet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/sbinet/32/13166_2.png) [@sbinet](https://root-forum.cern.ch/u/sbinet)\
**Post date:** [September 14, 2017, 12:11pm UTC](https://root-forum.cern.ch/t/writting-and-getting-a-std-vector-from-a-tfile/26207/4 "2017-09-14T12:11:29Z")

</div>

```auto
std::vector<Double_t> *tmp;
fin->GetObject("xx", tmp);
std::vector<Double_t> yy = *tmp;

```

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [September 28, 2017, 12:22pm UTC](https://root-forum.cern.ch/t/writting-and-getting-a-std-vector-from-a-tfile/26207/5 "2017-09-28T12:22:58Z")

</div>

Closed on September 28, 2017, 12:22pm UTC

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
