Array of Strings in TTree

Dear ROOTers,

I would like to store an array of string arrays into a TTree. I do this using

  TTree* tree  = new TTree( "TestTree" , "ntuple") ;
  int    _npar ;
  int    _parIntN[11] ;
  char   _parName[11][10];
  char   _parStringVal[11][11][10] ;

  tree->Branch( "npar",  &_npar,        "npar/I");
  tree->Branch( "pain",  _parIntN,      "pain[npar]/I");
  tree->Branch( "pana",  _parName,      "pana[npar]/C");
  tree->Branch( "pasv",  _parStringVal, "pasv[npar][npar]/C" ) ;

  _npar=11;
  for (int i = 0; i<_npar; i++){
    sprintf(_parName[i],"name%i",i);
    _parIntN[i]=i;
    for (int j=0;j<_npar;j++){
      sprintf(_parStringVal[i][j],"value%i%i",i,j);
    }
  }
  tree->Fill();

If I scan now the tree, I get the following:

root [13]   tree->Scan();
***********************************************************************
*    Row   * Instance *      npar *      pain *      pana *      pasv *
***********************************************************************
*        0 *        0 *        11 *         0 *     name0 *           *
*        0 *        1 *        11 *         1 *     name0 *           *
*        0 *        2 *        11 *         2 *     name0 *           *
*        0 *        3 *        11 *         3 *     name0 *           *
*        0 *        4 *        11 *         4 *     name0 *           *
*        0 *        5 *        11 *         5 *     name0 *           *
*        0 *        6 *        11 *         6 *     name0 *           *
*        0 *        7 *        11 *         7 *     name0 *           *
*        0 *        8 *        11 *         8 *     name0 *           *
*        0 *        9 *        11 *         9 *     name0 *           *
*        0 *       10 *        11 *        10 *     name0 *           *
***********************************************************************

I would have expected, that pana changes with the instance (name0, name1, …).

If I use Show() to look closer into my tree I get the following:

root [14]   tree->Show();
======> EVENT:0
 npar            = 11
 pain            = 0,
                  1, 2, 3, 4, 5, 6, 7, 8, 9, 10
 pana            = name0
 pasv            = value00

Here again I would have expected, that I get a list of names for pana and a list of values for pasv (value00, value01, value02, …).

Everything seems to be even more messed up, when I add more then just one entry to the tree.

Could anybody help me to treat arrays of strings in a tree correctly?

Thanks in advance!

Benedikt

Hi,

tree->Branch( "pasv", _parStringVal, "pasv[npar][npar]/C" ) ; is not well supported (and somewhat hard to use anyway). We recommend that you use std::vectorstd::string instead.

Cheers,
Philippe.

Hi!

Thanks alot for your reply! Would you recommend to use std::vector in every case (also for 1D arrays of ints, floats, …) or just for this special case of a 2D-string array?

Cheers,
Benedikt

Hi,

For ints and floats, the 2 solutions are basically equivalent (one advantage of the std::vector solution is that you can let the compiler detect the ‘type’ rather than having to specify it explicity).

Cheers,
Philippe.