Problem using TTree::Scan reading trees of objects w/ arrays

Hi,

I’ve made a TTree of objects I have defined. When I try to Scan them though, depending on how I do the Scan, I get an “index to high” error. Can someone please tell me what I am doing wrong?

First my class.

TT.h:

#ifndef TT_H
#define TT_H

#include <iostream>

#include "TROOT.h"
#include "TObject.h"

#define NMAX 10

using namespace std;

class TT: public TObject
{
  public:
    Int_t a;
    Int_t b[NMAX];

    TT();
    void Clear();

    Int_t getB(Int_t i) const;

    void setB(Int_t i, Int_t x);

    ClassDef(TT, 1);
};

#endif

TT.cxx:

#include "TT.h"

ClassImp(TT)

TT::TT()
{
  Clear();
}

void TT::Clear()
{
  a = 0;
  for (Int_t i = 0; i < NMAX; i++)
    b[i] = 0;
}

Int_t TT::getB(Int_t i) const
{
  if (i < NMAX)
    return b[i];

  cerr << "Invalid index (" << i << ")" << endl;
  return -1;
}

void TT::setB(Int_t i, Int_t x)
{
  if (i < NMAX)
    b[i] = x;
  else
    cerr << "Invalid index (" << i << ")" << endl;
}

TTLinkDef.h:

#ifdef __CINT__

#pragma link off all globals;
#pragma link off all classes;
#pragma link off all functions;
#pragma link C++ class TT+;

#endif

I compile like this to create a shared library, libTT.so:

g++ -g -c `root-config --cflags` -fPIC -I. TT.cxx
rootcint -f TTDict.C -c -I. TT.h TTLinkDef.h
g++ -g -c TTDict.C `root-config --cflags` -fPIC -I.
g++ -g -shared TT.o TTDict.o -o libTT.so

and finally my test program, t.cxx:

#include <cstdlib>

#include "TROOT.h"
#include "TFile.h"
#include "TTree.h"

#include "TT.h"


int main(int argc, char **argv)
{

  int i, n;

  TFile *rootFile = new TFile("t.root", "recreate");

  TT *tt = new TT();

  TTree *tree = new TTree("tree", "test tree");
  tree->Branch("TTBranch", "TT", &tt);

  for (n = 0; n < NMAX; n++)
  {
    tt->Clear();
    tt->a = n;
    for (i = 0; i < NMAX; i++)
      tt->setB(i, tt->a*10 + i);

    tree->Fill();
  }

  rootFile->Write();
  rootFile->Close();

  exit(0);
}

which I compile like this:

g++ -g `root-config --cflags` -o t t.cxx ./libTT.so `root-config --libs`

My problem is now if I use Scan with no arguments I get “index too high errors”:

root [0] gSystem->Load("./libTT.so");
root [1] TFile f("t.root")
root [2] tree->Scan()
Error in <TTreeFormula::TTreeFormula>: Index 10 for dimension #1 in TTBranch.b[10].b is too high (max is 9)
************************************************************
*    Row   * TTBranch. * TTBranch. * TTBranch. * TTBranch. *
************************************************************
*        0 *         0 *  50331648 *         0 *           *
*        1 *         0 *  50331648 *         1 *           *
*        2 *         0 *  50331648 *         2 *           *
*        3 *         0 *  50331648 *         3 *           *
*        4 *         0 *  50331648 *         4 *           *
*        5 *         0 *  50331648 *         5 *           *
*        6 *         0 *  50331648 *         6 *           *
*        7 *         0 *  50331648 *         7 *           *
*        8 *         0 *  50331648 *         8 *           *
*        9 *         0 *  50331648 *         9 *           *
************************************************************
(Long64_t)10

If I specify the columns to output, the error “goes away” and all rows are printed (here I just “q” but it does work if you want to print them all out):

root [3] tree->Scan("a:b")
***********************************************
*    Row   * Instance *         a *         b *
***********************************************
*        0 *        0 *         0 *         0 *
*        0 *        1 *         0 *         1 *
*        0 *        2 *         0 *         2 *
*        0 *        3 *         0 *         3 *
*        0 *        4 *         0 *         4 *
*        0 *        5 *         0 *         5 *
*        0 *        6 *         0 *         6 *
*        0 *        7 *         0 *         7 *
*        0 *        8 *         0 *         8 *
*        0 *        9 *         0 *         9 *
*        1 *        0 *         1 *        10 *
*        1 *        1 *         1 *        11 *
*        1 *        2 *         1 *        12 *
*        1 *        3 *         1 *        13 *
*        1 *        4 *         1 *        14 *
*        1 *        5 *         1 *        15 *
*        1 *        6 *         1 *        16 *
*        1 *        7 *         1 *        17 *
*        1 *        8 *         1 *        18 *
*        1 *        9 *         1 *        19 *
*        2 *        0 *         2 *        20 *
*        2 *        1 *         2 *        21 *
*        2 *        2 *         2 *        22 *
*        2 *        3 *         2 *        23 *
*        2 *        4 *         2 *        24 *
Type <CR> to continue or q to quit ==> q
***********************************************
(Long64_t)25

Is there something wrong with how I wrote my code? I’m afraid to use these trees when I see an error like this because I wonder if I’m really filling them correctly and if I will read out the correct values. This is just a test case of a more general tree that I’ve written that contains much more data.

Thanks in advance for any advice,
William

I can reproduce the problem. Thanks for posting this simple example.
We are going to investigate.

Rene

Hi,

With the trunk I am unable to reproduce the message:[quote]Error in TTreeFormula::TTreeFormula: Index 10 for dimension #1 in TTBranch.b[10].b is too high (max is 9)[/quote]. Which version of ROOT are you using?

Philippe.

ROOT 5.26/00 (trunk@31882, Dec 14 2009, 20:18:36 on linux)

Hi,

Indeed, this problem was present in v5.26/00 and v5.27/04 but has been fixed by revision 34640 (2010-07-28) of the trunk.

Cheers,
Philippe.