Re: [ROOT] Problems with filling/reading tree

From: Rene Brun (Rene.Brun@cern.ch)
Date: Thu Dec 16 2004 - 10:24:25 MET


What you do is not valid.
You cannot have a struct with elements of different size and pass the address
of the first element to build a branch.
If you want to use a struct, you should create two branches in the case of your
example.
 -a first branch with "id"
 -a second branch with all the other elements
The clean solution is to create a class instead of a struct and pass the
the address of a pointer to a class instance like we do in many examples.
Using a class, you do not need to spell the individual members, they are
know to the system.

Rene Brun


Matej Batic wrote:
> 
> Hi all!
> 
> I have some problems with filling tree. If I fill tree with struct with
> one int and four doubles, then reading tree it seems that structure of
> an event is not aligned properly. In case I fill tree with two ints and
> four doubles, the problem dissappears.
> 
> Please have a look at attached code. I use root 4.00/07 on Linux
> 2.4.27-pre6 (x86_64 and i386 platforms)
> 
> Thanks,
> matej
> 
> --
> Matej Batic ~~~~~~~~~~~~~~~~~~~~~~~~~~
> Dept. of Experimantal Particle Physics
> Jozef Stefan Institute
> Jamova 39, SI-1001 Ljubljana
> Slovenia
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> 
>   --------------------------------------------------------------------------------
> #include "TFile.h"
> #include "TTree.h"
> 
> struct MData {
>   int id;
>   double posX;
>   double posY;
>   double posZ;
>   double energy;
> };
> 
> int spooky(char* rootfn){
> 
>   MData data;
> 
>   dataFile = new TFile(rootfn,"RECREATE");
>   dataTree = new TTree("tree","simulation data");
>   dataTree->Branch("Events",&data.id,"id/I:posX/D:posY/D:posZ:energy");
> 
> 
> //fill with some numbers ...
>   for(int i=0; i<250; i++){
>     data.id=i;
>     data.posX=i;
>     data.posY=0.52345*i;
>     data.posZ=i;
>     data.energy=234.123557*i;
>     dataTree->Fill();
>   }
> 
>   dataFile->Write();
>   dataFile->Close();
> 
>   spookyread(rootfn);
> 
> }
> 
> int spookyread(char* rootfn){
> 
>   dataFile = new TFile(rootfn,"READ");
>   tree=(TTree*)gROOT->FindObject("tree");
> 
>   for(int i=1;i<12;i++) tree->Show(i);
> 
>   dataFile->Close();
> }



This archive was generated by hypermail 2b29 : Sun Jan 02 2005 - 05:50:10 MET