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:
[code]#include
#include
#include
#include
#include
#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 {
RQ_OBJECT(“MyMainFrame”)
private:
TGMainFrame *fMain;
TRootEmbeddedCanvas *fEcanvas;
TCanvas *fCanvas;
TFile *input;
TTree *tree;
int nentries;
int entry;
public:
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,
10,10,10,1));
// Create a horizontal frame widget with buttons
TGHorizontalFrame *hframe = new TGHorizontalFrame(fMain,800,40);
TGTextButton *draw = new TGTextButton(hframe,"&Random");
draw->Connect(“Clicked()”,“MyMainFrame”,this,“DoDraw()”);
hframe->AddFrame(draw, new TGLayoutHints(kLHintsCenterX,5,5,3,4));
TGTextButton *exit = new TGTextButton(hframe,"&Exit");
exit->Connect(“Clicked()”,“MyMainFrame”,this,“DoExit()”);
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
fMain->MapSubwindows();
// Initialize the layout algorithm
fMain->Resize(fMain->GetDefaultSize());
// Map main frame
fMain->MapWindow();
ReadTree();
CreateCanvas();
}
void MyMainFrame::ReadTree()
{
input = TFile::Open(“outputMap.root”);
if(!input) {
std::cerr << "Couldn't read file!\n";
exit(1);
}
tree = (TTree*) input->Get("tree");
if(!tree) {
std::cerr << "Couldn't find 'tree'!\n";
exit(1);
}
nentries = (Int_t)tree->GetEntries();
std::cout << " Tree Entries: " << nentries << std::endl;
}
void MyMainFrame::CreateCanvas(void)
{
fCanvas = fEcanvas->GetCanvas();
fCanvas->Divide(1,2);
}
void MyMainFrame::DoExit()
{
input->Close();
gApplication->Terminate(0);
}
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;
tree->GetEntry(entry);
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
fMain->Cleanup();
delete fMain;
}
void NewDisplay() {
// Popup the GUI…
new MyMainFrame(gClient->GetRoot(),800,800);
}
[/code]