Reading vector branches in GUI

Hi, I’m trying to write a display (GUI) that can read vectors from a tree and display them.
However, it seems that the branches are read properly but the vectors then do not exist.

In fact, running the following script I get:


#include “TApplication.h”
#include “TSystem.h”
#include <TMath.h>
#include <TROOT.h>
#include <TGClient.h>
#include <TCanvas.h>
#include <TF1.h>
#include <TFile.h>
#include <TTree.h>
#include <TLine.h>
#include <TRandom.h>

#include <TGButton.h>
#include <TGFrame.h>
#include <TRootEmbeddedCanvas.h>
#include <RQ_OBJECT.h>

#define PI 3.14159265

class MyMainFrame {
TGMainFrame *fMain;
TRootEmbeddedCanvas *fEcanvas;
TCanvas *fCanvas;
TFile *input;
TTree *tree;
int nentries;
int entry;

MyMainFrame(const TGWindow *p,UInt_t w,UInt_t h);
virtual ~MyMainFrame();
void DoDraw();
void DoExit();
void CreateCanvas(void);
void ReadTree();

MyMainFrame::MyMainFrame(const TGWindow *p,UInt_t w,UInt_t h) {
// Create a main frame
fMain = new TGMainFrame(p,w,h);

// Create canvas widget
fEcanvas = new TRootEmbeddedCanvas(“Ecanvas”,fMain,800,800);
fMain->AddFrame(fEcanvas, new TGLayoutHints(kLHintsExpandX| kLHintsExpandY,

// Create a horizontal frame widget with buttons
TGHorizontalFrame *hframe = new TGHorizontalFrame(fMain,800,40);

TGTextButton *draw = new TGTextButton(hframe,"&Random");
hframe->AddFrame(draw, new TGLayoutHints(kLHintsCenterX,5,5,3,4));

TGTextButton *exit = new TGTextButton(hframe,"&Exit");
hframe->AddFrame(exit, new TGLayoutHints(kLHintsCenterX,5,5,3,4));

fMain->AddFrame(hframe, new TGLayoutHints(kLHintsCenterX,2,2,2,2));

// Set a name to the main frame
fMain->SetWindowName(“Cluster Display”);

// Map all subwindows of main frame

// Initialize the layout algorithm

// Map main frame



void MyMainFrame::ReadTree()
input = TFile::Open(“outputMap.root”);

if(!input) {
  std::cerr << "Couldn't read file!\n";

tree = (TTree*) input->Get("tree");
if(!tree) {
      std::cerr << "Couldn't find 'tree'!\n";
nentries = (Int_t)tree->GetEntries();
std::cout << " Tree Entries: " << nentries << std::endl;


void MyMainFrame::CreateCanvas(void)
fCanvas = fEcanvas->GetCanvas();

void MyMainFrame::DoExit()

void MyMainFrame::DoDraw()
std::vector * Positions = 0;
std::vector * Charges = 0;

tree->SetBranchAddress("SVDClusters.m_pos", &Positions);
tree->SetBranchAddress("SVDClusters.m_charge", &Charges);

// get random entry
srand (time(NULL));
entry = rand() % nentries + 1;

if (Positions->empty()) {
  std::cout << " empty vector " << std::endl;
else {
  std::cout << " size is  "<< Positions->size() << std::endl;
  tree->ResetBranchAddresses(); // detach from local variables
 delete Positions;
 delete Charges;


MyMainFrame::~MyMainFrame() {
// Clean up used widgets: frames, buttons, layouthints
delete fMain;
void NewDisplay() {
// Popup the GUI…
new MyMainFrame(gClient->GetRoot(),800,800);


Could you provide your data file (output.root)?

Cheers, Bertrand.

In the end of “void MyMainFrame::DoDraw()”, you need to add:
tree->ResetBranchAddresses(); // detach from local variables
delete Positions;
delete Charges;

Thanks for your answers.

In the meantime I switched to ROOT6, made sure the file compiles fine and added the lines about deleting the pointers (code updated in my first post).

Now the error that I get while I try to use DoDraw is the following ( which I think still means the same thing, vectors are not filled with the tree content and so don’t exist)

I also attach to root file I’m using.

 *** Break *** segmentation violation

There was a crash.
This is the entire stack trace of all threads:
#0  0x00007f48c8a4468e in waitpid () from /lib64/
#1  0x00007f48c89d6609 in do_system () from /lib64/
#2  0x00007f48c99a8d07 in TUnixSystem::StackTrace() () from /cvmfs/
#3  0x00007f48c99aac0c in TUnixSystem::DispatchSignals(ESignals) () from /cvmfs/
#4  <signal handler called>
#5  0x00007f48c9f214bc in ?? ()
#6  0x0000000002c882f0 in ?? ()
#7  0x00007f48c9f2126c in ?? ()
#8  0x0000000000000000 in ?? ()

The lines below might hint at the cause of the crash.
If they do not help you then please submit a bug report at Please post the ENTIRE stack trace
from above as an attachment in addition to anything else
that might help us fixing this issue.
#5  0x00007f48c9f214bc in ?? ()
#6  0x0000000002c882f0 in ?? ()
#7  0x00007f48c9f2126c in ?? ()
#8  0x0000000000000000 in ?? ()

outputMap.root (29.7 KB)

It looks like the root file is being downloaded already 5 times, any ideas if the problem is in the root file or in the code ?


We are investigating…

Cheers, Bertrand.


if you want to read these two branches holding an stl vector in the file you posted, the procedure can be:

   TFile f("outputMap.root");
   TTree* t = nullptr;

   TTreeFormula formula("formula","SVDClusters.m_charge",t);
   for (int entry=0;entry< t->GetEntries();++entry){
      const auto size = formula.GetNdata();
      cout << "  o Size is " << size << endl;
      for(int i = 0; i < size; ++i) {
         auto charge = formula.EvalInstance(i);
         cout << "    * " << charge << endl;


Thanks for your answer. I got some work with higher priority but will report if this method works for me. Cheers