Re: [ROOT] problem with reading and filling of ntuple

From: Fons Rademakers (Fons.Rademakers@cern.ch)
Date: Tue Oct 17 2000 - 20:10:14 MEST


Hoi Pieter,

   the problem is that in analysis() you change the branch addresses of ntuple
so the next time you call fill() the ntuple will use the now obsolete stack
addresses of x,y,z as set in analysis(). Currently there is no simple method
to reset the ntuple branch addresses and we will provide something for this
case. For the time being you can work around it (and verify that in principle
things work by adding in fill():

  Float_t x = 5.0;
  Float_t y = 5.0;
  Float_t z = 5.0;

  ntuple->SetBranchAddress("x",&x);   //add
  ntuple->SetBranchAddress("y",&y);   //add
  ntuple->SetBranchAddress("z",&z);   //add

  //ntuple->Fill(x,y,z);
  (TTree*)ntuple)->Fill();   // cast protection away ?!?!



Groetjes, Fons.



"Meulenhoff, P.J." wrote:
> 
> Hi Rooters,
> 
> Running the following macro (plot.C) gives some unexpected results. The
> macro has two functions: fill() and analysis(). The fill routine fills the
> ntuple: ntuple with the variable x,y and z. x,y, and z are always equal to
> 5. The analysis routine reads all entries in the ntuple created in the fill
> routine. A new ntuple: analysis is created.  The main routine (plot()) calls
> fill() and analysis() several times.
> 
> When the root is started from the command line (of a linux redhat 6.2 box):
> 
> root -l -q plot.C
> 
> the following output is produced
> 
> -- start output
>   *******************************************
>   *                                         *
>   *        W E L C O M E  to  R O O T       *
>   *                                         *
>   *   Version   2.25/03   11 October 2000   *
>   *                                         *
>   *  You are welcome to visit our Web site  *
>   *          http://root.cern.ch            *
>   *                                         *
>   *******************************************
> 
> Compiled with thread support.
> 
> CINT/ROOT C/C++ Interpreter version 5.14.50, Sep 2 2000
> Type ? for help. Commands must be C++ statements.
> Enclose multiple statements between { }.
> 
> Processing plot.C...
> this is fill
> this is analysis
> i = 0 x =    5.000 y =    5.000 z =    5.000
> this is fill
> this is analysis
> i = 0 x =    5.000 y =    5.000 z =    5.000
> i = 1 x =    5.240 y =    5.240 z =    0.000
> -- end of output
> 
> The last line is VERY strange. This values of x,y and z should all be equal
> to 5.
> 
> This is the source code of the macro:
> 
> //-----start of plot.C
> void fill() {
>   printf("this is fill\n");
> 
>   TNtuple *ntuple = (TNtuple *)gDirectory->Get("ntuple");
>   if ( ntuple == NULL ) {
>     ntuple = new TNtuple("ntuple","ntuple","x:y:z");
>   }
> 
>   Float_t x = 5.0;
>   Float_t y = 5.0;
>   Float_t z = 5.0;
> 
>   ntuple->Fill(x,y,z);
> }
> 
> void analysis() {
>   printf("this is analysis\n");
> 
>   TNtuple *analysis = (TNtuple *)gDirectory->Get("analysis");
>   if ( analysis == NULL ) {
>     analysis = new TNtuple("analysis","analysis","x:y:z");
>   }
> 
>   TNtuple *ntuple = (TNtuple *)gDirectory->Get("ntuple");
> 
>   Float_t x,x1;
>   Float_t y,y1;
>   Float_t z,z1;
> 
>   if ( ntuple != NULL ) {
> 
>     ntuple->SetBranchAddress("x",&x);
>     ntuple->SetBranchAddress("y",&y);
>     ntuple->SetBranchAddress("z",&z);
> 
>     int nentries = ntuple->GetEntries();
> 
>     for(int i=0;(i<nentries);i++) {
>       ntuple->GetEntry(i);
>       x1 = x + 1.0;
>       y1 = y + 1.0;
>       z1 = z + 1.0;
>       analysis->Fill(x1,y1,z1);
> 
>       printf("i = %d x = %8.3f y = %8.3f z = %8.3f\n",i,x,y,z);
>     }
>   }
> }
> 
> void plot() {
>   fill();
>   analysis();
>   fill();
>   analysis();
> }
> // end of plot.C
> 
> Pieter Meulenhoff
> p.j.meulenhoff@kpn.com
> KPN Research
> Groningen, The Netherlands

-- 
Org:    CERN, European Laboratory for Particle Physics.
Mail:   1211 Geneve 23, Switzerland
E-Mail: Fons.Rademakers@cern.ch              Phone: +41 22 7679248
WWW:    http://root.cern.ch/~rdm/            Fax:   +41 22 7677910



This archive was generated by hypermail 2b29 : Tue Jan 02 2001 - 11:50:35 MET