Hi.
I am having a problem storing arrays of strings in a tree (in my example: b1). Actually, writing to the tree gives me no problems, but trying to double-click on variable b1 from a browser gives me errors [about accessing index 2 of my variable (with maximum index: 1)].
On the other hand, storing a vector of arrays seems to work just fine (in my example: b2).
Could somebody have a look and let me know what I am doing wrong?
Thanks!
(Using Version 5.14/00d 9 March 2007, on WinXP)
// root stuff
#include <TROOT.h>
#include <TFile.h>
#include <TTree.h>
#include <TBranch.h>
#include <TRandom.h>
// std stuff
#include <iostream>
#include <string>
#include <vector>
/// class containing output root-tuple structure
class Event : public TObject {
public:
// names - simple variable
std::string a;
// strings in an array
std::string b1[2]; // this does not seem to work
// string in a vector
std::vector<std::string> b2; // this works fine
// integer - array
Int_t c[2];
Event(){}
~Event(){}
ClassDef(Event, 1)
};
int saveStrings()
{
TFile * output_file = new TFile("temp.root", "recreate");
TTree * root_tree = new TTree("tree", "root-tuple description");
root_tree->SetDirectory(output_file);
Event * evt = new Event();
TBranch * branch = root_tree->Branch("myTuple", "Event", &evt, 8000, 2);
for(int i = 0; i != 10; ++i)
{ // loop over events
evt->b2.clear();
int i1 = int(gRandom->Gaus(30, 5) );
int i2 = int(gRandom->Gaus(50, 10) );
int i3 = int(gRandom->Gaus(50, 10) );
char temp[128];
sprintf(temp, "a%d", i1);
evt->a = temp;
sprintf(temp, "b%d", i2);
evt->b2.push_back(temp);
evt->b1[0] = temp;
sprintf(temp, "b%d", i3);
evt->b2.push_back(temp);
evt->b1[1] = temp;
evt->c[0] = i2; evt->c[1] = i3;
root_tree->Fill();
} // loop over events
output_file->cd();
root_tree->Write();
output_file->Close();
return 0;
}