Problem to fill Tree branch with vector of a structure

I am trying to fill the tree branch with a vector of a user defined structure as :

/// bar.hxx
typedef struct bar{
int id, type, charge;
}bar;
vector hit;

/// bar.cxx
… filling the “vector hit” for each event. works.

but filling the tree with the vector is not working:
tree->Branch(“bar”,“vector”,&hit);
tree->Fill();

It’s not showing any compilation error, but runtime error. I know it’s probably not the way I should be filling the tree.
Suggestion please.

[quote]It’s not showing any compilation error, but runtime error.[/quote]What is the runtime error? Most likely you did not generate the dictionary for vector.

Philippe.

It’s not showing any compilation error, but runtime error.What is the runtime error? Most likely you did not generate the dictionary for vector.

Philippe.

Error is :
Error in TTree::Branch: The class requested (vector) for the branch “bar” refer to an stl collection and do not have a compiled CollectionProxy. Please generate the dictionary for this class (vector)

I tried doing :
#include
#ifdef MAKECINT
#pragma link C++ class bar+;
#endif

But bar is not defined as class type. So I get,

Error: link requested for unknown class BarHit /Users/analysis/ana/v0r0/src/loader.C:3:
Warning: Error occurred during reading source files
Warning: Error occurred during dictionary source generation
!!!Removing /Users/analysis/ana/v0r0/src/loader_C_ACLiC_dict.cxx /Users/analysis/ana/v0r0/src/loader_C_ACLiC_dict.h !!!
Error: /Users/work/ROOT/v5r28p00n01/Darwin/bin/rootcint: error loading headers…
Error in : Dictionary generation failed!
Info in : Invoking compiler to check macro’s validity
Info in : The compiler has not found any problem with your macro.
Probably your macro uses something rootcint can’t parse.

Hi,

The linkdef part would need to look like: #include <vector> #ifdef __MAKECINT__ #pragma link C++ class bar+; #pragma link C++ class vector<bar>+; #endifand loader.C would need to also include the header file for bar …

Cheers,
Philippe.

My code :
////////////bar.hxx

#include “loader.C”

typedef struct bar{
int id, type, charge;
}bar;
class ReconCluster:public EventLoopFunction{
private:
TTree fTree;
vector hit;
public:
ReconCluster(){};
virtual ~ReconCluster() {};
void Clear();
void Initialize();
bool operator()(Event&);
void Finalize();
void GetBarHits(Event&, vector
);
};

/////////////////bar.cxx
int main(int argc, char **argv)
{
gROOT->ProcessLine(".L /Users/analysis/ana/v0r0/include/loader.C+");
ReconCluster myCode;
EventLoop(argc,argv,myCode);
return 0;
}
void ReconCluster::Initialize(){
fTree = new TTree(“tr”,“Cluster Tree”);
fTree->Branch(“bar”,“vector”,&hit);
}
void ReconCluster::Clear(){
hit.clear();
}
bool ReconCluster::operator()(Event& ev)
{
GetBarHits(ev,&hit);
fTree->Fill();
Clear();
return;
}
void ReconCluster::Finalize(){
outfile= new TFile (“output.root”,“recreate”);
fTree->Write();
outfile->Close();
}

///////////// loader.C
#include
#ifdef MAKECINT
#pragma link C++ class bar+;
#pragma link C++ class vector+;
#endif

/////Compilation : make -> NO error
//// Runtime : …/Darwin/clst_f.exe …/input/500MeV0degmu.root
Info in TUnixSystem::ACLiC: creating shared library /Users/analysis/ana/v0r0/include/loader_C.so
Error: link requested for unknown class bar /Users/analysis/ana/v0r0/include/loader.C:6:
Error: class,struct,union or type bar not defined /Users/work/ROOT/v5r28p00n01/Darwin/lib/root/cint/cint/lib/prec_stl/memory:37:
Error: class,struct,union or type bar not defined /Users/work/ROOT/v5r28p00n01/Darwin/lib/root/cint/cint/lib/prec_stl/memory:38:
Error: class,struct,union or type bar not defined /Users/work/ROOT/v5r28p00n01/Darwin/lib/root/cint/cint/lib/prec_stl/memory:43:
Error: class,struct,union or type bar not defined /Users/work/ROOT/v5r28p00n01/Darwin/lib/root/cint/cint/lib/prec_stl/memory:44:
Error: class,struct,union or type bar not defined /Users/work/ROOT/v5r28p00n01/Darwin/lib/root/cint/cint/lib/prec_stl/memory:45:
Error: class,struct,union or type bar not defined /Users/work/ROOT/v5r28p00n01/Darwin/lib/root/cint/cint/lib/prec_stl/vector:51:
Error: Function bar() is not defined in current scope /Users/work/ROOT/v5r28p00n01/Darwin/lib/root/cint/cint/lib/prec_stl/vector:442:
Error: Function bar() is not defined in current scope /Users/work/ROOT/v5r28p00n01/Darwin/lib/root/cint/cint/lib/prec_stl/vector:442:
Error: Syntax error /Users/work/ROOT/v5r28p00n01/Darwin/lib/root/cint/cint/lib/prec_stl/vector:522:
Error: void type variable can not be declared /Users/work/ROOT/v5r28p00n01/Darwin/lib/root/cint/cint/lib/prec_stl/vector:522:
Warning: Error occurred during reading source files
Warning: Error occurred during dictionary source generation
!!!Removing /Users/analysis/ana/v0r0/include/loader_C_ACLiC_dict.cxx /Users/analysis/ana/v0r0/include/loader_C_ACLiC_dict.h !!!
Error: /Users/work/ROOT/v5r28p00n01/Darwin/bin/rootcint: error loading headers…
Error in : Dictionary generation failed!
Info in : Invoking compiler to check macro’s validity
Info in : The compiler has not found any problem with your macro.
Probably your macro uses something rootcint can’t parse.
Check root.cern.ch/root/Cint.phtml?limitations for Cint’s limitations.
Error in TTree::Branch: The class requested (vector) for the branch “bar” refer to an stl collection and do not have a compiled CollectionProxy. Please generate the dictionary for this class (vector)


Hi,

The linkdef part would need to look like:
#include
#ifdef MAKECINT
#pragma link C++ class bar+;
#pragma link C++ class vector+;
#endif
and loader.C would need to also include the header file for bar …

Cheers,
Philippe.

Hi,

There is some confusion in where the #include should be. Here is a version of your file that should work:

[code]////////////bar.hxx
// Do no include loader.C.
#include
#include “EventLoopFunction.h”
#include “TTree.h”

typedef struct bar {
int id, type, charge;
} bar; // Note that ‘typedef struct bar {…} bar;’ is a C syntax, in C++ you write 'struct bar { … };'
class ReconCluster:public EventLoopFunction{
private:
TTree fTree;
vector hit;
public:
ReconCluster(){};
virtual ~ReconCluster() {};
void Clear();
void Initialize();
bool operator()(Event&);
void Finalize();
void GetBarHits(Event&, vector
);
};[/code]

[code]
/////////////////bar.cxx
#include “bar.hxx”
#include “TROOT.h”

int main(int argc, char **argv)
{
gROOT->ProcessLine(".L /Users/analysis/ana/v0r0/include/loader.C+");
ReconCluster myCode;
EventLoop(argc,argv,myCode);
return 0;
}
…[/code]

// Loader.C
#include "bar.h"
#include <vector>

#ifdef __MAKECINT__
#pragma link C++ class bar+;
#pragma link C++ class vector<bar>+;
#endif

Cheers,
Philippe.

Thank you very much.
Problem solved.