Hello!
I want to save vector in TTree.
I want to use VS2015.
I used this code root.cern.ch/root/html/tutorial … tor.C.html and simplified as soon as possible.
I added gROOT->ProcessLine("#include "); as told here Trees with vectors
#include "TFile.h"
#include "TTree.h"
#include "TRandom.h"
#include "TMath.h"
#include "TApplication.h"
#include "TROOT.h"
#include "TH1F.h"
#include "TCanvas.h"
#include "TFrame.h"
#include "TSystem.h"
#include <iostream>
using namespace std;
void write()
{
TFile *f = TFile::Open("D:\\Data_work\\test_folder\\hvector.root", "RECREATE");
if (!f) { return; }
std::vector<float> vpx;
// Create a TTree
TTree *t = new TTree("tvec", "Tree with vectors");
t->Branch("vpx", &vpx);
gRandom->SetSeed();
for (Int_t i = 0; i < 2; i++)
{
Int_t npx = (Int_t)(gRandom->Rndm(1) * 15);
vpx.clear();
cout << "i = " << i << " ; npx = " << npx << endl;
for (Int_t j = 0; j < npx; ++j) {
Float_t px, py, pz;
gRandom->Rannor(px, py);
pz = px*px + py*py;
Float_t random = gRandom->Rndm(1);
cout << "\t" << j << " " << px << endl;
vpx.push_back(px);
}
t->Fill();
}
f->Write();
delete f;
}
void read()
{
TFile *f = TFile::Open("D:\\Data_work\\test_folder\\hvector.root", "READ");
if (!f) { return; }
TTree *t; f->GetObject("tvec", t);
std::vector<float> *vpx = 0;
TBranch *bvpx = 0;
t->SetBranchAddress("vpx", &vpx, &bvpx);
for (Int_t i = 0; i < t->GetEntries(); i++)
{
Long64_t tentry = t->LoadTree(i);
bvpx->GetEntry(tentry);
cout << "i = " << i << " ; vpx->size() = " << vpx->size() << endl;
for (UInt_t j = 0; j < vpx->size(); ++j)
{
cout << "vpx->at(" << j << ") = " << vpx->at(j) << endl;
}
}
// Since we passed the address of a local variable we need
// to remove it.
t->ResetBranchAddresses();
}
int main(int argc, char *argv[])
{
TApplication theApp("theApp", &argc, argv);
gROOT->ProcessLine("#include <vector>");
write();
read();
cout << "all is ok" << endl;
system("pause");
theApp.Terminate();
theApp.Run();
return 0;
}
I have this run-time error
Unhandled exception at 0x76BEC54F in test_tree_io.exe: Microsoft C++ exception: std::length_error at memory location 0x0019F2CC.
or I do not have errors, but vpx->size() is 0 in read() function.
As the same time this code works in root.
root -l
.L Source.cpp
Error in <TApplication::TApplication>: only one instance of TApplication allowed
i = 0 ; npx = 2
0 -0.342657
1 -1.11391
i = 1 ; npx = 5
0 -0.732982
1 2.24394
2 -2.3976
3 0.359842
4 -0.239092
i = 0 ; vpx->size() = 2
vpx->at(0) = -0.342657
vpx->at(1) = -1.11391
i = 1 ; vpx->size() = 5
vpx->at(0) = -0.732982
vpx->at(1) = 2.24394
vpx->at(2) = -2.3976
vpx->at(3) = 0.359842
vpx->at(4) = -0.239092
all is ok
My root:
*******************************************
* *
* W E L C O M E to R O O T *
* *
* Version 5.34/36 5 April 2016 *
* *
* You are welcome to visit our Web site *
* http://root.cern.ch *
* *
*******************************************
ROOT 5.34/36 (v5-34-36@v5-34-36, Apr 05 2016, 10:25:45 on win32)
CINT/ROOT C/C++ Interpreter version 5.18.00, July 2, 2010
Type ? for help. Commands must be C++ statements.
Enclose multiple statements between { }.
Could you help?
Best regards, Vladislav.