G’day,
I’m trying to save a variable number of strings in a branch of a root tree. I’m not having much luck. I don’t care whether I save them as const char*, TStrings, or even TObjString (whatever I can get to work).
Here’s what I tried so far:
// for saving the root file
void saveStringsInRoot()
{
char names[10][100] = {"peter", "bob", "fred", "michael"};
int num = 4;
for (int loop = 0; loop < num; ++loop)
{
cout << loop << ") " << names[loop] << endl;
}
cout << "Creating new name tree" << endl;
tree = new TTree("names", "L1 Trigger Names");
tree->Branch("num", &num, "num/i");
tree->Branch("names", names , "names[num]/C");
tree->Fill();
tree->ls();
TFile file ("test1.root", "RECREATE", "XMon Root file");
tree->Write();
file.Close();
}
and
// for reading in the root file
void readStringRootFile()
{
TFile file ("test1.root");
TTree *tree = (TTree*) file.Get("names");
tree->ls();
Long64_t numEntries = tree->GetEntries();
cout << "numEntries " << numEntries << endl;
int num;
char names[10][100];
tree->SetBranchAddress ("num", &num);
tree->SetBranchAddress ("names", names);
tree->GetEntry(0);
cout << "num of names " << num << endl;
for (int loop = 0; loop < num; ++loop)
{
cout << loop << ") " << names[loop] << endl;
} // for loop
}
Here’s the output I get running on root 4.02/00 on cygwin (I get equivalent output on root v4_00_08 on RH7.3:
cplager@PointyIII> root -l -q saveStringsInRoot.c
root [0]
Processing saveStringsInRoot.c...
0) peter
1) bob
2) fred
3) michael
Creating new name tree
OBJ: TTree names L1 Trigger Names : 0 at: 0x10f82f40
************************************
* Row * num * names *
************************************
* 0 * 4 * peter *
************************************
cplager@PointyIII> root -l -q readStringRootFile.c
root [0]
Processing readStringRootFile.c...
OBJ: TTree names L1 Trigger Names : 0 at: 0x1105ad00
numEntries 1
num of names 4
0) peter
1)
2) ¸
3)
Where I would have expected the four names, not just the first one.
So…
What am I doing wrong and how can I fix this?
Cheers,
Charles