How to save vector<float> in TTree correctly?

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.

Hi,

Your code works for me. I compiled it with:

cl -nologo -Z7 -MD -GR -EHsc -I%ROOTSYS%\include -FIw32pragma.h test_tree_io.cxx /link -debug -LIBPATH:%ROOTSYS%\lib libCore.lib libCint.lib libGui.lib libRint.lib libGui.lib libThread.lib libRIO.lib libTree.lib libMathcore.lib /out:test_tree_io.exe
And then, when I run it:

[code]C:\Users\bellenot\rootdev\Forum\Vladislav>test_tree_io.ex
i = 0 ; npx = 11
0 0.353271
1 0.905267
2 -0.348922
3 -1.97133
4 -0.0702454
5 0.398407
6 -0.63246
7 -0.106947
8 -0.0296084
9 -0.269979
10 -0.660652
i = 1 ; npx = 14
0 -2.38874
1 -1.06839
2 -1.79744
3 0.463017
4 -0.49519
5 0.856804
6 0.466098
7 0.257076
8 -0.521555
9 2.19667
10 -0.541682
11 -1.37693
12 -0.790656
13 0.600883
i = 0 ; vpx->size() = 11
vpx->at(0) = 0.353271
vpx->at(1) = 0.905267
vpx->at(2) = -0.348922
vpx->at(3) = -1.97133
vpx->at(4) = -0.0702454
vpx->at(5) = 0.398407
vpx->at(6) = -0.63246
vpx->at(7) = -0.106947
vpx->at(8) = -0.0296084
vpx->at(9) = -0.269979
vpx->at(10) = -0.660652
i = 1 ; vpx->size() = 14
vpx->at(0) = -2.38874
vpx->at(1) = -1.06839
vpx->at(2) = -1.79744
vpx->at(3) = 0.463017
vpx->at(4) = -0.49519
vpx->at(5) = 0.856804
vpx->at(6) = 0.466098
vpx->at(7) = 0.257076
vpx->at(8) = -0.521555
vpx->at(9) = 2.19667
vpx->at(10) = -0.541682
vpx->at(11) = -1.37693
vpx->at(12) = -0.790656
vpx->at(13) = 0.600883
all is ok
Press any key to continue . . .

C:\Users\bellenot\rootdev\Forum\Vladislav>[/code]
Cheers, Bertrand.

Hello!

Hmm, your method works.
But what is the reason of error when I complile directly from GUI?

I thought that happaned bacause I did not include w32pragma.h as forced include file.
But even if include this file I have the same error.

I have this error in VS2015 and VS2013.

In VS2015 Configuration properties / C/C++ comand line :

/GS /analyze- /W3 /Zc:wchar_t /ZI /Gm /Od /sdl /Fd"Debug\vc140.pdb" /FI"w32pragma.h" /Zc:inline /fp:precise /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_CRT_SECURE_NO_WARNINGS" /D "_UNICODE" /D "UNICODE" /errorReport:prompt /WX- /Zc:forScope /RTC1 /Gd /Oy- /MDd /Fa"Debug\" /EHsc /nologo /Fo"Debug\" /Fp"Debug\test_tree_io.pch" 

VS2015 project drive.google.com/open?id=0B_9W0 … HhPM1huaUk

Best regards, Vladislav.

Hi Vladislav,

First, VS 2015 is not supported, and you have to use the exact same version of Visual Studio than the one ROOT has been built with. Then, you have to build your application the same way ROOT has been built. (i.e. you cannot mix debug/release builds on Windows)

Cheers, Bertrand.

Hello!

Yes, I understand this.
But this error was in VS2013 too.

This code works in release, but do not work in debug.
Why?
I installed root from this file root_v5.34.36.win32.vc12.debug.exe ( Windows Visual Studio 2013 (dbg) ).
Has ROOT from this file been built in release mode?

Best regards, Vladislav.

[quote=“Vladislav”]This code works in release, but do not work in debug.
Why?
I installed root from this file root_v5.34.36.win32.vc12.debug.exe ( Windows Visual Studio 2013 (dbg) ).
Has ROOT from this file been built in release mode?[/quote]
root_v5.34.36.win32.vc12.debug.exe has been build in debug mode, but without the debug run-time libraries (hence the /MD compiler option instead of /MDd)
You can see all the flags in $(ROOTSYS)/include/compiledata.h

Cheers, Bertrand.

Hello!

I changed /MDd to /MD and added /GR.
I use VS2013 with such keys:

/GS /analyze- /W3 /Zc:wchar_t /ZI /Gm /Od /sdl /Fd"Debug\vc120.pdb" /FI"w32pragma.h" /fp:precise /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_LIB" /D "_CRT_SECURE_NO_WARNINGS" /D "_UNICODE" /D "UNICODE" /errorReport:prompt /WX- /Zc:forScope /RTC1 /GR /Gd /Oy- /MD /Fa"Debug\" /EHsc /nologo /Fo"Debug\" /Fp"Debug\test_tree_io.pch" 

But I have error again.
What also should I change to work properly in Debug mode?

My compile.h file:

/* This is file is automatically generated */
#define BUILD_ARCH "win32"
#define BUILD_NODE "EC-WIN7-X64-01"
#define COMPILER "C:/Program Files (x86)/Microsoft Visual Studio 12.0/VC/bin/cl.exe"
#define COMPILERVERS ""
#define  MAKESHAREDLIB "cl $Opt -nologo -TP -c -nologo -IC:/build/workspace/root-release-5.34/BUILDTYPE/Debug/COMPILER/vc12/LABEL/win7/sources/root_v5.34.36/root/build/win -FIw32pragma.h -FIsehmap.h -MD -GR -EHsc- -W3 -wd4244 -D_WIN32 /MP  $IncludePath $SourceFiles -Fo$ObjectFiles && bindexplib $LibName $ObjectFiles > $BuildDir\\$LibName.def && lib -nologo -MACHINE:IX86 -out:$BuildDir\\$LibName.lib $ObjectFiles -def:$BuildDir\\$LibName.def && link -nologo $ObjectFiles -DLL -out:$BuildDir\\$LibName.dll $BuildDir\\$LibName.exp -LIBPATH:%ROOTSYS%\\lib  $LinkedLibs libCore.lib libCint.lib kernel32.lib advapi32.lib user32.lib gdi32.lib comdlg32.lib winspool.lib && if EXIST \"$BuildDir\\$LibName.dll.manifest\" ( mt -nologo -manifest \"$BuildDir\\$LibName.dll.manifest\" \"-outputresource:$BuildDir\\$LibName.dll;2\" && del \"$BuildDir\\$LibName.dll.manifest\" )"
#define MAKEEXE "cl -nologo -TP -Iinclude -I../include -c $Opt -nologo -IC:/build/workspace/root-release-5.34/BUILDTYPE/Debug/COMPILER/vc12/LABEL/win7/sources/root_v5.34.36/root/build/win -FIw32pragma.h -FIsehmap.h -MD -GR -EHsc- -W3 -wd4244 -D_WIN32 /MP  $IncludePath $SourceFiles && link -opt:ref  $ObjectFiles $LinkedLibs advapi32.lib -out:$ExeName  && if EXIST \"$ExeName.exe.manifest\" ( mt -nologo -manifest \"$ExeName.exe.manifest\" \"-outputresource:$ExeName.exe;1\" && del \"$ExeName.exe.manifest\" )"
#define CXXOPT "-O2"
#define CXXDEBUG "-Z7"
#define ROOTBUILD "debug"
#define LINKEDLIBS "-LIBPATH:%ROOTSYS% lib/libCore.lib lib/libCint.lib lib/libRint.lib "
#define INCLUDEPATH "-I%ROOTSYS%/include"
#define OBJEXT "obj"
#define SOEXT "dll" 

Best regards, Vladislav.

[quote=“Vladislav”]What also should I change to work properly in Debug mode?
[/quote]Remove the /D “_DEBUG”.

It works!
Thanks!