TTree array of chars

I’m trying to create a branch in a tree that contains an array of char-arrays (see attached). The array is read in correctly (as per the comment statement), but the branch is not filled correctly (when you check the .root file itself). What am I missing?

~Doug
chartest.C (338 Bytes)

In the line:

You have [n] inside the text. This actually gets read in as “n” rather than “2,” so ROOT doesn’t know how big your array is. What you want to do is:

To do this dynamically,

char wordsDescription[32]; // Just large enough to hold the text sprintf(wordsDescription, "words[%d]/C", (int)n); test->Branch("words", words, wordsDescription);
which should allow you to read back the array correctly.

This still isn’t working. Running this I obtain the output:

[code]root [1] chartest->Scan(“n:words”)


  • Row * n * words *

  •    0 *         2 *       one *
    

************************************[/code]

How are you reading it back out? The following code “works,” though it is incorrect and generates a warning. To generate the test file:

[code]{
const Int_t n = 2;
Char_t words[n][256] = {“one”, “two”};

TFile *fout = new TFile(“chartest.root”, “recreate”);
TTree *test = new TTree(“chartest”, “char test”);
test->Branch(“n”, &n, “n/I”);
test->Branch(“words”, words, “words[n]/C”);

for(int i=0;i < n;i++)
cout << words[i] << endl;

test->Fill();
test->Write();
}[/code]

Reading it out:

root [0] .x chartest.C one two root [1] TFile *fin = new TFile("chartest.root", "READ");TTree *test = (TTree*)fin->Get("chartest"); Warning in <TFile::Init>: file chartest.root probably not closed, trying to recover Info in <TFile::Recover>: chartest.root, recovered key TTree:chartest at address 390 Warning in <TFile::Init>: successfully recovered 1 keys root [2] Char_t words[2][256]; root [3] test->SetBranchAddress("words", words) (const Int_t)0 root [4] test->GetEntry() (Int_t)8 root [5] words[0] (Char_t* 0x17aaba0)"one" root [6] words[1] (Char_t* 0x17aaca0)"two"

I believe the correct code to create the output file is:

[code]{
const Int_t n = 2;
Char_t words[n][256] = {“one”, “two”};

TFile *fout = new TFile(“chartest.root”, “recreate”);
TTree *test = new TTree(“chartest”, “char test”);
test->Branch(“n”, &n, “n/I”);
test->Branch(“words”, words, “words[2][256]/C”);

for(int i=0;i < n;i++)
cout << words[i] << endl;

test->Fill();
test->Write();
fout->Close();
}[/code]

Then, read it in:

[code]{
TFile *fin = new TFile(“chartest.root”, “READ”);
TTree test = (TTree)fin->Get(“chartest”);
Char_t words[2][256];
test->SetBranchAddress(“words”, words);
test->GetEntry();

for(int i=0;i < 2;i++)
cout << words[i] << endl;

fin->Close();
}
[/code]
This works for me in ROOT 5.34.04. Both dimensions of the array should be specified when you construct the branch.