I’m trying to store a list of strings in a rootfile. As TList or similar objects seem not very straight forward, I decided to use a Tree storing a null terminating string. But I fail. Maybe one could point out what my mistake is? Most probably I’ve missed something stupid. I guess that the address space for a char* must be available to the tree?! That’s why I’ve initialized
char wavename[1000];
The following lines compile (ALIC compiler) and run but do not work.
#include <TFile.h>
#include <TTree.h>
#include <iostream>
#include <map>
using namespace std;
void testmacro(){
map<string, int> testlist;
testlist["abc"]++;
testlist["an other test"]++;
//testlist["hallo1++0-K*892"]++;
//testlist["0-2-+rho770"]++;
//testlist["3-2+K*892.amp_vs_1-2++rho770.amp"]++;
{
cout << " writing the wavelist " << endl;
TFile testfile("testfile.root", "Recreate");
if (testfile.IsZombie()) return;
TTree* testtree = new TTree("tree_wavelist", "wavelist");
char wavename[1000];
testtree->Branch("wavename", testtree, "C");
for (map<string, int>::iterator it = testlist.begin(); it != testlist.end(); it++){
strcpy(wavename,it->first.c_str());
cout << " Filling " << it->first.c_str() << " as " << wavename << endl;
testtree->Fill();
}
testtree->Write();
//testfile.Write();
testfile.Close(); // this should also delete the tree
}
testlist.clear();
{
cout << " reading the wavelist " << endl;
TFile testfile("testfile.root", "Read");
if (testfile.IsZombie()) return;
TTree* testtree = (TTree*) testfile.Get("tree_wavelist");
if (!testtree) return;
char wavename[1000];
testtree->SetBranchAddress("wavename", wavename);
for (int iwave = 0; iwave < testtree->GetEntries(); iwave++){
testtree->GetEntry(iwave);
string wavenamestring(wavename);
testlist[wavenamestring]++;
cout << " retrieving " << wavename << " as " << wavenamestring << endl;
}
testfile.Close(); // this should also delete the tree
cout << " done " << endl;
}
cout << " retrieving results " << endl;
for (map<string, int>::iterator it = testlist.begin(); it != testlist.end(); it++){
cout << it->first.c_str() << endl;
}
}