TBranch and vector<class> inside class

Hello, I have been really looking for an answer for weeks on this issue.

Originally I had created a TTree with 2 branches containing two custom classes. This works fine. I use CINT to generate dictionaries for the custom classes in the custom built tool of each .h file.

Then I tried to create a 3rd branch just like the other two, with the only difference that inside the class attached to TBranch (class A below) there is an element vector b;

The code compiles fine but then I get a seg fault or exception when executing the TBranch statement.

I dumbed down the problem to a trivial class B containint only an integer.

When I replace vector b; in class A with just B b; TBranch does not crash. No matter what random combinations I tried in linkdef or use pointer definitions such as vector *b;

the program crashes at InspectElement, or BranchImp. After reading various not-so-well matching posts, I removed ClassImp from the cpp files. Nothing helped. I paste below the offending parts of the code.

ROOT Version: 5.34.09
Platform: Windows 10
Compiler: VS2019 C++

#pragma once
#include <TObject.h>
#include <vector>
class B : public TObject
	int i;

	ClassDef(B, 1);
class A :
    public TObject
    TString moduleName;
    int version;
    TString dateCreated;
    TString creator;
    TString notes;
    std::vector<B> b; //this doesnt work
    //B b;//this works
    ClassDef(A, 1);


.cpp files only have some trivial initialisations of the integers and stirngs.


#ifdef __CINT__

#pragma link off all globals;
#pragma link off all classes;
#pragma link off all functions;
#pragma link C++ nestedclass;
#pragma link C++ nestedtypedef;

//#pragma link C++ class B+; //this causes an 'already defined error during compilation
#pragma link C++ class A+;
#pragma link C++ class vector<B>+;


B linkdef

#ifdef __CINT__

#pragma link off all globals;
#pragma link off all classes;
#pragma link off all functions;
#pragma link C++ nestedclass;
#pragma link C++ nestedtypedef;

#pragma link C++ class vector<B>+; //this was added in the end but made no difference
#pragma link C++ class B+;


In main:
variables declared in the global scope

        recordArchive = new TTree("RecordArchive", "Record Archive");

	record = new Record();
	baseData = new BaseData();
	a = new A();

	brBase = recordArchive->Branch("Base Data", "BaseData", &baseData);
	brRecords = recordArchive->Branch("Records", "Record", &record);
	brA = recordArchive->Branch("A A", "A", &a); //this crashes when A contains vector<B> b; but not when it just contains B b;

Advice appreciated. Thanks

I tried with ROOT 6 and the problem does not seem to be there. Can you update to ROOT6? ROOT 5 is very old, perhaps @pcanal knows if this is a known issue.

Firstly apologies, for tha autoformating of the code, in many places the editor changed “” to bold letters!

Secondly, I tried to use root 6 and I made a number of attempts over the period of perhaps a year, but then it turned out that I need to use a very specific version of visual studio and I need to compile root myself. I just don’t have time for that level of involvement. One would have hoped that there would be a simple set of instructions saying “use X version of visual studio, with Y version of compile tools and use this Z version of binaries”. Unfortunately this simple thing does not exist so until then, I will stick to these old vesions.

Many thanks, looking forward to further advice.

As an alternative to native ROOT on Windows, with recent Windows 10 you can also ROOT for Linux in the Windows subsystem for Linux.

So your response helped me solve the problem because it narrowed it down to the correct version of root. I upgraded to the most recent version of root5 and the problem went away. Thank you!

However since we are at it, I do have a grievance in relation to root6. This is not directed to you but whoever is related to this situation. From what I understand, there is a way to compile root6 to work with Visual Studio 2019. What I don’t understand is why doesnt someone write 6 bullet points on exactly how they’ve done it, which versions they used, what steps they took, which pitfalls to avoid.

Even if we park the issue on why a supposedly platform independent application like ROOT has terrible problems of compatibility with the most popular platform in the world, even in the poor situation where one needs to ‘fiddle’ to install root in windows, why isn’t this ‘fiddle’ documented? Even informally, like a runaway readme doc. Would have saved many people hours of frustration.

BTW using linux is not an option for me because then I have issues with all my own legacy code.

Kind regards

Many thanks for the feedback! I’m not an expert myself on ROOT on Windows, but I’ll make sure to forward the suggestions to improve the installation procedure.


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