Storing textual info in TTree->GetUserInfo()

Hi,

I would like to save some labels which are the same for every event in my tree, hence I thought that the TTree userInfo would be a good storage location.

I have the labels available as a std::vectorstd::string initially. Could someone help me formulate a correct way of converting this to a TObject* and then adding it to the userInfo?

I tried:

// std::vector<std::string> vlab contains labels
// TTree *t1 is my TTree

TList *labels = new TList();
labels->SetName("myLabels");
vector<TObjString> os;
for(int i=0; i<(int)vlab.size(); i++) {
  os.push_back(vlab[i].c_str());
  os[i].Print("");
  labels->Add(&os[i]);
}

t1->GetUserInfo()->Add(labels);
f1->Write();
f1->Close();

The os[i].Print("") is coming out fine, but after that I’m getting segmentation violation (I’m running in CMSSW). Is something wrong with this root-wise, or should I look for the problem within CMSSW? Also, if there is a cleaner way of achieving my goal, please let me know.

Thanks,
Sara

[edit]
I managed to get my strings into the userInfo with the following code. However, I still don’t know what was wrong with the above. Someone wants to explain?

working:

// std::vector<std::string> vlab contains labels
// TTree *t1 is my TTree

TList *labels = new TList();
labels->SetName("myLabels");
TObjString os[vlab.size()];
for(int i=0; i<(int)vlab.size(); i++) {
  os[i] = vlab[i].c_str();
  os[i].Print("");
  labels->Add((TObject*)&os[i]);
}

t1->GetUserInfo()->Add(labels);
t1->GetUserInfo()->FindObject("myLabels")->Print();
f1->Write();
f1->Close();

Hi,

The ‘problem’ is that std::vector::push_back can lead to extension of the where and thus a reallocation/move of its content. In particular this means that ‘&os[i]’ might not be invariant during the looping/addition. Use something like:for(int i=0; i<(int)vlab.size(); i++) { os.push_back(vlab[i].c_str()); } for(int i=0; i<(int)vlab.size(); i++) { labels->Add(&os[i]); }
or

os.reserve(vlab.size()); for(int i=0; i<(int)vlab.size(); i++) { os.push_back(vlab[i].c_str()); os[i].Print(""); labels->Add(&os[i]); }

Cheers,
Philippe.