Hi Maurizio,
You fill find below your code slightly modified and a small script
(Linux)
to run it. I join the following files:
- goSTL a short script to generate the shared libs, etc
- EventSTL.h example of class using STL
- EventSTL.cxx the implementation
- EventSTLLinkDef.h
- MainSTL.cxx example of batch main program
- STLw.C example of Root macro to write the Tree interactively
- STLr.C example of Root macro to read the tree interactively
Note that when a class contains STL objects, you cannot use the split
mode for the branch supporting this class.
Rene Brun
//-----------------file goSTL
rootcint -f STLcint.cxx -c EventSTL.h EventSTLLinkDef.h
g++ -g -fPIC -I$ROOTSYS/include -c STLcint.cxx EventSTL.cxx MainSTL.cxx
g++ -g -Wl,-soname,EventSTL.so -shared STLcint.o EventSTL.o -o
EventSTL.so
g++ -g -o STL MainSTL.o EventSTL.so -L$ROOTSYS/lib \
-lCore -lCint -lTree -lm -ldl -rdynamic
//-----------------file EventSTL.h
#include "TObject.h"
#include <vector>
class EventSTL : public TObject
{
public:
EventSTL();
virtual ~EventSTL();
Electron(Double_t x);
VI();
Clear();
vector<double> A; //<<----------------- lenght if elements
may vary
ClassDef(EventSTL,1)
};
//-----------------file EventSTL.cxx
#include "EventSTL.h"
ClassImp(EventSTL)
EventSTL::EventSTL() {}
EventSTL::~EventSTL() {}
EventSTL::Electron(Double_t x)
{
A.push_back(x); //<<----------------- Here I put stuff in A
}
EventSTL::Clear()
{
A.clear(); //<<----------------- Remove from memory A
}
EventSTL::VI() //<<----------------- This to see A while the
// code is running. I'm
// seeing the right
things
{
cout << "Size: " << A.size() << " - Contents : " ;
for (Int_t i=0;i<A.size();i++)
{
cout << " " << A[i];
}
cout << endl;
}
//-----------------file MainSTL.cxx
#include <iostream>
#include "TROOT.h"
#include "TFile.h"
#include "TTree.h"
#include "EventSTL.h"
EventSTL *CC = new EventSTL;
TROOT Mauri("Bos_to_Root","Bos Banks to Root file conversion");
main()
{
TFile *myfile;
myfile = new TFile("STL.root","RECREATE","TTree Dynamic ROOT file");
TTree *mytree = new TTree("T","My tree");
mytree->Branch("abc","EventSTL",&CC,16000,0);
for (Int_t i=0;i<10;++i)
{
for (Int_t j=0;j<i;++j)
{
CC->Electron(i);
}
mytree->Fill();
CC->VI();
CC->Clear();
}
mytree->Write();
mytree->Print();
return 0;
}
//-----------------file STLw.C
{
gSystem->Load("EventSTL");
TFile myfile("STL.root","RECREATE","TTree Dynamic ROOT file");
EventSTL *CC = new EventSTL;
TTree *mytree = new TTree("T","My tree");
mytree->Branch("abc","EventSTL",&CC,16000,0);
for (Int_t i=0;i<20;++i) {
for (Int_t j=0;j<i;++j) {
CC->Electron(i);
}
mytree->Fill();
CC->VI();
CC->Clear();
}
mytree->Write();
mytree->Print();
}
//-----------------file STLr.C
{
gSystem->Load("EventSTL");
TFile myfile("STL.root");
EventSTL *CC = 0;
TTree *T = (TTree*)myfile.Get("T");
T->SetBranchAddress("abc",&CC);
T->GetEntry(15);
CC->VI();
}
Maurizio Ungaro wrote:
>
> Hi Rene,
> thanks to your help I manage to write the code,
> but here's a question I think can be of interest
> for everybody: I want a dynamic tree, id est I tried the following
> code, which compiled and run but doesn't fill the tree.
>
> //--------------------------A.h
> #include "TObject.h"
> #include <vector>
>
> class Clas_event : public TObject
> {
> public:
> Clas_event();
> virtual ~Clas_event();
> Electron(Double_t x);
> VI();
> Clear();
> vector<double> A; <<----------------- lenght if elements may vary
> ClassDef(Clas_event,1)
> };
>
> //------------------------A.cxx
> #include "A.h"
>
> ClassImp(Clas_event)
>
> Clas_event::Clas_event() {}
> Clas_event::~Clas_event() {}
> Clas_event::Electron(Double_t x)
> {
> A.push_back(x); <<----------------- Here I put stuff in A
> }
>
> Clas_event::Clear()
> {
> A.clear(); <<----------------- Remove from memory A
> }
>
> Clas_event::VI() <<----------------- This to see A while the
> code is running. I'm
> seeing the right things
> {
> cout << "Size: " << A.size() << " - Contents : " ;
> for (Int_t i=0;i<A.size();i++)
> {
> cout << " " << A[i];
> }
> cout << endl;
> }
>
> //-----------------------AMain.cxx
> #include <iostream>
> using namespace std;
>
> #include "TROOT.h"
> #include "TFile.h"
> #include "TObject.h"
> #include "TTree.h"
> #include "TBranch.h"
> #include "A.h"
>
> Clas_event *CC = new Clas_event;
>
> TROOT Mauri("Bos_to_Root","Bos Banks to Root file conversion");
>
> main()
> {
>
> TFile *myfile;
> myfile = new TFile("file.root","RECREATE","TTree Dynamic ROOT file");
>
> TTree *mytree = new TTree("T","My tree");
> mytree->Branch("abcBranch","Clas_event",&CC);
>
> for (Int_t i=0;i<10;++i)
> {
> for (Int_t j=0;j<i;++j)
> {
> CC->Electron(i);
> }
> mytree->Fill();
> CC->VI();
> CC->Clear();
> }
>
> mytree->Write();
> mytree->Print();
>
> return 0;
>
> }
>
> Mauri
This archive was generated by hypermail 2b29 : Tue Jan 04 2000 - 00:43:43 MET