TTree::MakeClass() and TChain bug: old entries get replicated

From: Matthew D. Langston (langston@SLAC.stanford.edu)
Date: Fri Aug 20 1999 - 20:38:10 MEST


                           Executive Summary
                           -----------------

The analysis skeleton generated by "TTree::MakeClass()" doesn't work
when instantiated with a TChain*, even with Rene's TChain fix (see
http://root.cern.ch/root/roottalk/roottalk99/1810.html).

Objects of "class T", when instantiated with a TChain* and looped over
with "T::Loop()", get "stuck" on the last entry in the first file added
to the TChain, even though the number of entries processed for the
TChain is correct.

A package that demonstrates this bug is available at
ftp://ftp.slac.stanford.edu/users/langston/root/root-bug-tchain-0.01.tar.gz.


                                Summary
                                -------

To demonstrate the problem, this package writes a TTree of objects of
"class Row" to four separate files.

The "class Row" is a simple, trivial class with only two data members:

  fN     A counter whose value is incremented after each write to the
         TTree (so it acts like a serial number, or an event number).

  fData  A floating point random number (distributed according to a
         gaussian distribution).

The program `create_example_trees' creates 1000 objects of "class Row"
and writes them to four files (i.e. 250 objects of "class Row" per
file).

If you execute the `draw_tree.sh' shell script (which simply histograms
"fData" for one of the four `.root' files), you will see 250 events
histogrammed, exactly as expected.

If you execute the `draw_chain.sh' shell script (which simply histograms
"fData" for all four of the `.root' files using a TChain), you will see
1000 events histogrammed, again exactly as expected.  This shows that
the four `.root' files were created properly, and that the TChain is
also working properly in this instance.

However, when you instantiate an object of "class T", which was
automatically generated from "TTree::MakeClass()", the situation
changes.  To demonstrate the problem, I added a line to "T::Loop()"
which simply writes out the two data members of "class Row" for each
entry.

When objects of "class T" are instantiated with a TTree* from one of the
four `.root' files, the member function "T::Loop()" works as expected:

  root [0] gSystem.Load( "libRow" );  
  root [1] TFile* file = new TFile( "sample_data-1.root" ); 
  root [2] TTree* tree = file->Get( "T" ); 
  root [3] .L T.C
  root [4] T* t = new T( tree ); 
  root [5] t->Loop();
  0 -1.10228
  [snip]
  248 0.109913
  249 -1.61573
  root [6] 

Here we see that objects 0 through 249 are printed out properly.

However, when objects of "class T" are instantiated with a TChain*, then
"T::Loop()" gets "stuck" on the last entry in the first file added to
the TChain:

  root [0] gSystem.Load( "libRow" );                       
  root [1] TChain* tree = new TChain( "T" ); 
  root [2] tree->Add( "sample_data-1.root" ); 
  root [3] tree->Add( "sample_data-2.root" ); 
  root [4] tree->Add( "sample_data-3.root" ); 
  root [5] tree->Add( "sample_data-4.root" ); 
  root [6] .L T.C
  root [7] T* t = new T( tree );  
  root [8] t->Loop();
  0 -1.10228
  [snip]
  248 0.109913
  249 -1.61573
  249 -1.61573
  249 -1.61573
  249 -1.61573
  [snip]

Here we see that objects 0 through 249 are printed out properly, but
objects 250 though 999 are simply copies of entry 249.

Note that the "T::Loop()" member function *does* print out 1000 entries
(remember, there are four files in the TChain, with 250 entries per
file), but the last 750 entries are only copies of the last entry of the
first file (in the example above, entry 249 is repeated 750 times).

Oliver Mang was having similar problems with code generated by
"TTree::MakeClass()" and TChain (see
http://root.cern.ch/root/roottalk/roottalk99/1808.html).  Rene suggested
some minor edits one can make to the automatically generated code to fix
Oliver's problem (see
http://root.cern.ch/root/roottalk/roottalk99/1810.html).  These changes
have been applied to my automatically generated code.

More specifically, the changes I made to the code automatically
generated by "TTree::MakeClass()" are in the file `T.patch'.

--
Matthew D. Langston
SLD, Stanford Linear Accelerator Center
langston@SLAC.Stanford.EDU



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