Strings into TTree

Hi, I just want to fill a tree with numerical data, but also I need to record one string of two characters per entry. Here is my example code with two numerical entries, which compiles. Recording the string is in line 11 which is commented. When I uncomment compilation fails.

#include<iostream>
#include<TFile.h>
#include<TTree.h>
void example(){
int Z;
int A;
string element;
TTree* tree = new TTree("DATA", "all stuff");
tree->Branch("Z", &Z);
tree->Branch("A", &A);
//tree->Branch("element", &element);
Z = 1;
A = 2;
element = "AA";
tree->Fill();
Z = 10;
A = 20;
element = "BB";
tree->Fill();
tree->Print();
tree->Show(0);
tree->Show(1);
tree->Scan();
tree->GetEntry(0);
cout << Z << endl;
cout << A << endl;
cout << element << endl;
tree->GetEntry(1);
cout << Z << endl;
cout << A << endl;
cout << element << endl;
TFile* f = new TFile("mu.root", "recreate");
tree->Write("tree");
f->Close();}

P.S. For historical reasons I am using root 5.28/00 for windows, and it has to stay that way.

Many thanks in advance!

Try with “TString” instead of “string”.

Indeed TString works till the line:

cout << element << endl;

, where root crashes. How should I access the string at each event?

Try "\n" instead of endl (some ROOT distributions have a problem with the std::endl sometimes).

cout << element << “\n”;

also makes root crash.

Try (some ROOT distributions have the problem that an explicit TString::Data() call is needed sometimes):
std::cout << element.Data() << "\n";

Another thing … first open the output file, then create the (disk resident) tree … so, move the line:
TFile* f = new TFile(...);
to somewhere before the line:
TTree* tree = new TTree(...);

element.Data() works fine.

Many thanks! Feel free to close the topic.