Re: Simple tree

From: Rene Brun (Rene.Brun@cern.ch)
Date: Thu Nov 18 1999 - 22:44:01 MET


Hi Maurizio,
I have already posted several times the following example.
I have slightly extended it to show how
to create a Tree in a compiled program and read it with
two small macros. Your short example cannot work because you 
try to create a branch with a pointer to a class not derived
from TObject.

You will find below the following files:
 A.h         the definition file for a small class A
 A.cxx       the corresponding implementation file
 ALinkDef.h  the file containing the rootcint directives
 AMain.cxx   the main program to create the Tree
 ARead.C     a small macro to loop on the file A.root generated by Amain
 Asimple.C   a small macro to plot one Tree variable
 goA         a small script showing how to run rootcint,link,etc on Linux

In this short example, I also illustrate how to create a shared lib.
You should look at the examples in $ROOTSYS/test/MainEvent.cxx for a more
elaborated Tree example.
I also suggest you read the nice tutorials developped by the FNAL Root
support team at URL: http://www-pat.fnal.gov/root/

Rene Brun

I suggest the following steps using Root version 2.23:
 1- execute goA
 2- run A
 3- execute the following Root interactive session
    Root > .x ARead.C
 4- execute the following Root interactive session
    Root > .x Asimple.C

#--------------file goA
rootcint -f Acint.cxx -c A.h ALinkDef.h
g++ -g -fPIC -I$ROOTSYS/include -c Acint.cxx A.cxx Amain.cxx
g++ -g -Wl,-soname,A.so -shared Acint.o A.o -o A.so 
g++ -g -o A Amain.o A.so -L$ROOTSYS/lib \
    -lCore -lCint -lTree  -lm -ldl -rdynamic

//------------------------------ A.h
#ifndef __A_HH__
#define __A_HH__
#include "TObject.h"

class A : public TObject {
public:

  A();
  virtual ~A();
  
  Float_t fA;
  Float_t fB;
  
  ClassDef(A,1)  //My small class example
};

#endif
//------------------------------- A.cxx
#include "A.h"

 ClassImp(A)

A::A() {
  fA = 1;
  fB = 2;
}

A::~A() {
}

//---------------------------file ALinkDef.h

#ifdef __CINT__

#pragma link off all globals;
#pragma link off all classes;
#pragma link off all functions;

#pragma link C++ class A;
#endif

//------------------------------macro ARead.C
void Aread()
{
   //example of macro to read the Tree generated in Amain.cxx
   
   gSystem->Load("A");
   TFile *f = new TFile("A.root");
   TTree *T = (TTree*)f->Get("T");
   A *a=0;
   T->SetBranchAddress("aa",&a);
      
   Int_t nentries = (Int_t)T->GetEntries();
   for (Int_t i=0;i<nentries;i++) {
      T->GetEntry(i);
      if (a->fA >0.001) continue;
      printf("Print entry:%d\n",i);
      a->Dump();
   }
}    

//------------------------------macro Asimple.C
{
   // small macro plotting the distribution of variable fB
   gROOT->Reset();
   gSystem->Load("A");
   TFile *f = new TFile("A.root");
   T->Draw("fB","fA<0.5");
}

On Thu, 18 Nov 1999, Maurizio Ungaro wrote:

> I'm trying to build a very simple tree
> with my class A this way:
> 
> 
> class  A
> {
>  public:
>  Int_t a; 
>  Int_t b; 
> };
> 
> 
> 
> A *CC;
> 
> 
> TROOT simple("simple","trees");
>  
>  main()
>  {
> 
>    Int_t split = 0;
>    Int_t bsize = 32000;
> 
> 
> // Create a ROOT Tree
> 
> TTree *tree = new TTree("T","An example of ROOT tree");
> tree->Branch("aa","A",&CC,bsize,split);
> 
> 
>    return 0;
>  }
>                                                                       
> 
> and I'm getting the message 
> 
> Error in <TTree::BranchObject>: Cannot find class:A
> 
> 
> What am I doing wrong?
> 



This archive was generated by hypermail 2b29 : Tue Jan 04 2000 - 00:43:43 MET