Writing and Reading a TTree : what do I do wrong ?

From: Patois Yannick (patois@ganil.fr)
Date: Fri Nov 05 1999 - 22:04:00 MET


Hi,

I must have missed someting in the way ROOT class I/O can be
implemented. I really dont understand why this code doesnt work.

It does compile, link and run, but the values are not stored and/or read
properly.

I spent a lot of time around this problem, looking into the 'Event'
exemple and such, but I'm really stuck...

Any idea ?

>>> File little_test.cpp

#include <TROOT.h>
#include <TFile.h>
#include <TTree.h>

#include "my_event.H"

main ( int argc , char **argv ) {
  TROOT manip_event("test","Test analysis");
  my_event *the_event = new my_event;
  if (argc<2) {
    TFile *root_file = new TFile("toto.root","RECREATE","Test events");
    TTree *test_tree = new TTree("test_tree","Test manip events");
    test_tree->Branch("brut_tsol","my_event",the_event);
    for (int i=0;i<10;i++) {
      the_event->set(i);
      test_tree->Fill();
    }
    root_file->Write();
    root_file->Close();
    delete root_file;
  }
  else {
    TFile *hfile = new TFile("toto.root");
    TTree *tree = (TTree*)hfile->Get("test_tree");
    TBranch *branch = tree->GetBranch("brut_tsol");
    branch->SetAddress(the_event);
    Int_t nentries = (Int_t)tree->GetEntries();
    for (int ev = 0; ev < nentries; ev++) {
      tree->GetEvent(ev);
      the_event->print();
    }
  }
}


>>> File my_event.cpp

#include <iostream.h>
#include <TROOT.h>
#include <TNtuple.h>
#include "my_event.H"
ClassImp(my_event);
my_event:: my_event(void) {;}
my_event::~my_event(void) {;}
void my_event::set(int a) {   T1=a;   T2=2*a; }
void my_event::print(void) {   cout << T1 << " " << T2 << endl; }

>>> File my_event.H

#ifndef __my_event__
#define __my_event__
class my_event : public TObject {
public:
  my_event (void);
  ~my_event(void);
public:
  Float_t T1;
  Float_t T2;
  void set (int a);
  void print(void);
  ClassDef(my_event,1) // The macro
};
#endif

>>> File Makefile

CPC      =      CC -compat=4
LD       =      CC -compat=4
CPCflags =      $(shell root-config --cflags)
ldflags  =	$(shell root-config --libs)

OBJTEST	=       little_test.o  my_event.o
OBJDICT =	my_event.dict_o
MYTEST     =   mytest

all:		$(MYTEST)

$(MYTEST):      $(OBJTEST) $(OBJDICT)
		@$(LD) $(OBJTEST) $(OBJDICT) $(ldflags) -o $(MYTEST)

.SUFFIXES :
.SUFFIXES : .cpp .H .C .dict_o .o

.cpp.o:
	@echo compilation of $<
	$(CPC) $(CPCflags) $< -c -o $@

.H.C:
	@echo "Generating dictionary ..."
	@rootcint $@ -c $< 

.C.dict_o:
	@echo compilation of dico $<	
	$(CPC) $(CPCflags) $< -c -o $@

-------------
All that was tryed with :

$ uname -a
SunOS gansrx 5.6 Generic_105181-08 sun4u sparc SUNW,Ultra-1

root says:
  *   Version   2.23/08   2 November 1999   *
(btw, why not adding a --version option to the root program ?)

I modified the RConfig.h file as Bruce O'NEEL suggested to be able to
compile with the SUN CC5.0 compiler in compat mode.


 _/ Yannick Patois _________________ Address (home) __________________
| irc(undernet): Garp on #france25+ | La Villa des Sciences            |
| email : patois@ganil.fr           | 12, avenue de Cambridge          |
| web :http://www.sura.org/~patois/ | 14200 Herouville-Saint-Clair     |
| Tel/Fax-home:+33 (0)2 31 94 50 32 | FRANCE                           |
|___________________________________|__________________________________|



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