Save a vector (or array or TObjArray) of strings (or TStrings or TObjStrings) to a TFile

Hi,

I have been trying for hours to create some array like structure of string like objects to a TFile.
I would prefer not to have them in a TTree as a TBranch. I do not see a straightforward way of
using simple vectors or arrays, as they do not have the TObject::Write method. (It would be nice if
TFile had a TFile::WriteThisToFile(what_I_want_to_write_to_file) method, although I see how this might be difficult.) I moved to try and make a TObjArray of TObjStrings, but I cannot get TObjString to return a TString with TObjString::GetString(). TObjString::GetName returns the TString, but when I try to get the TString from the TObjArray with TObjArray::GetName, I get the name of the TObjArray. For example:

[code]root [0] TFile* temp = new TFile(“Temp.root”,“RECREATE”)
root [1] temp->cd()
(Bool_t)1
root [2] const int ns = 10;
root [3] TObjArray* x = new TObjArray();
root [4] TObjString* y[ns];
root [5] for(int i=0; i<ns; i++){
end with ‘}’, ‘@’:abort > y[i] = new TObjString(Form(“Name_%d”,i));
end with ‘}’, ‘@’:abort > y[i]->SetString(Form(“String_%d”,i));
end with ‘}’, ‘@’:abort > x->Add(y[i]);
end with ‘}’, ‘@’:abort > }
root [6] y[0]
(class TObjString*)0x288a050
root [7] y[0]->GetName()
(const char* 0x288a069)"String_0"
root [8] y[0]->GetString()

*** Break *** segmentation violation
(class TString)"

===========================================================
There was a crash.
This is the entire stack trace of all threads:

#0 0x000000333beac61e in waitpid () from /lib64/libc.so.6
#1 0x000000333be3e609 in do_system () from /lib64/libc.so.6
#2 0x00007fe22ecf83b8 in TUnixSystem::StackTrace() () from /lbne/app/users/lblpwg_tools/SOFTWARE/root-5.34.05-sl6/lib/libCore.so
#3 0x00007fe22ecf7233 in TUnixSystem::DispatchSignals(ESignals) () from /lbne/app/users/lblpwg_tools/SOFTWARE/root-5.34.05-sl6/lib/libCore.so
#4
#5 0x000000333be845c2 in mempcpy () from /lib64/libc.so.6
#6 0x000000333be718ff in _IO_new_file_xsputn () from /lib64/libc.so.6
#7 0x000000333be67a4d in fwrite () from /lib64/libc.so.6
#8 0x0000003340a92d4d in std::basic_ostream<char, std::char_traits >::write(char const*, long) () from /usr/lib64/libstdc++.so.6
#9 0x00007fe22ec237b3 in operator<<(std::basic_ostream<char, std::char_traits >&, TString const&) () from /lbne/app/users/lblpwg_tools/SOFTWARE/root-5.34.05-sl6/lib/libCore.so
#10 0x00007fe22edaa19e in G__G__Base1__0_309(G__value*, char const*, G__param*, int) () from /lbne/app/users/lblpwg_tools/SOFTWARE/root-5.34.05-sl6/lib/libCore.so
#11 0x00007fe22defdb59 in Cint::G__ExceptionWrapper(int ()(G__value, char const*, G__param*, int), G__value*, char*, G__param*, int) () from /lbne/app/users/lblpwg_tools/SOFTWARE/root-5.34.05-sl6/lib/libCint.so
#12 0x00007fe22dfa6011 in G__execute_call () from /lbne/app/users/lblpwg_tools/SOFTWARE/root-5.34.05-sl6/lib/libCint.so
#13 0x00007fe22dfa6e72 in G__call_cppfunc () from /lbne/app/users/lblpwg_tools/SOFTWARE/root-5.34.05-sl6/lib/libCint.so
#14 0x00007fe22df869a8 in G__interpret_func () from /lbne/app/users/lblpwg_tools/SOFTWARE/root-5.34.05-sl6/lib/libCint.so
#15 0x00007fe22df7475b in G__getfunction () from /lbne/app/users/lblpwg_tools/SOFTWARE/root-5.34.05-sl6/lib/libCint.so
#16 0x00007fe22dfc4a70 in G__overloadopr () from /lbne/app/users/lblpwg_tools/SOFTWARE/root-5.34.05-sl6/lib/libCint.so
#17 0x00007fe22dfc64c1 in G__bstore () from /lbne/app/users/lblpwg_tools/SOFTWARE/root-5.34.05-sl6/lib/libCint.so
#18 0x00007fe22df5f174 in G__getexpr () from /lbne/app/users/lblpwg_tools/SOFTWARE/root-5.34.05-sl6/lib/libCint.so
#19 0x00007fe22dfcf4c2 in G__exec_statement () from /lbne/app/users/lblpwg_tools/SOFTWARE/root-5.34.05-sl6/lib/libCint.so
#20 0x00007fe22df87d85 in G__interpret_func () from /lbne/app/users/lblpwg_tools/SOFTWARE/root-5.34.05-sl6/lib/libCint.so
#21 0x00007fe22df7475b in G__getfunction () from /lbne/app/users/lblpwg_tools/SOFTWARE/root-5.34.05-sl6/lib/libCint.so
#22 0x00007fe22dfdce70 in G__atevaluate () from /lbne/app/users/lblpwg_tools/SOFTWARE/root-5.34.05-sl6/lib/libCint.so
#23 0x00007fe22dfdfcc2 in G__process_cmd () from /lbne/app/users/lblpwg_tools/SOFTWARE/root-5.34.05-sl6/lib/libCint.so
#24 0x00007fe22ecbcc06 in TCint::ProcessLine(char const*, TInterpreter::EErrorCode*) () from /lbne/app/users/lblpwg_tools/SOFTWARE/root-5.34.05-sl6/lib/libCore.so
#25 0x00007fe22ec2567a in TApplication::ProcessLine(char const*, bool, int*) () from /lbne/app/users/lblpwg_tools/SOFTWARE/root-5.34.05-sl6/lib/libCore.so
#26 0x00007fe22e879dab in TRint::HandleTermInput() () from /lbne/app/users/lblpwg_tools/SOFTWARE/root-5.34.05-sl6/lib/libRint.so
#27 0x00007fe22ecf58ce in TUnixSystem::CheckDescriptors() () from /lbne/app/users/lblpwg_tools/SOFTWARE/root-5.34.05-sl6/lib/libCore.so
#28 0x00007fe22ecf5bb3 in TUnixSystem::DispatchOneEvent(bool) () from /lbne/app/users/lblpwg_tools/SOFTWARE/root-5.34.05-sl6/lib/libCore.so
#29 0x00007fe22ec7d7d6 in TSystem::InnerLoop() () from /lbne/app/users/lblpwg_tools/SOFTWARE/root-5.34.05-sl6/lib/libCore.so
#30 0x00007fe22ec7f1bb in TSystem::Run() () from /lbne/app/users/lblpwg_tools/SOFTWARE/root-5.34.05-sl6/lib/libCore.so
#31 0x00007fe22ec2391f in TApplication::Run(bool) () from /lbne/app/users/lblpwg_tools/SOFTWARE/root-5.34.05-sl6/lib/libCore.so
#32 0x00007fe22e87b324 in TRint::Run(bool) () from /lbne/app/users/lblpwg_tools/SOFTWARE/root-5.34.05-sl6/lib/libRint.so
#33 0x000000000040103c in main ()

The lines below might hint at the cause of the crash.
If they do not help you then please submit a bug report at
http://root.cern.ch/bugs. Please post the ENTIRE stack trace
from above as an attachment in addition to anything else
that might help us fixing this issue.

#5 0x000000333be845c2 in mempcpy () from /lib64/libc.so.6
#6 0x000000333be718ff in _IO_new_file_xsputn () from /lib64/libc.so.6
#7 0x000000333be67a4d in fwrite () from /lib64/libc.so.6
#8 0x0000003340a92d4d in std::basic_ostream<char, std::char_traits >::write(char const*, long) () from /usr/lib64/libstdc++.so.6
#9 0x00007fe22ec237b3 in operator<<(std::basic_ostream<char, std::char_traits >&, TString const&) () from /lbne/app/users/lblpwg_tools/SOFTWARE/root-5.34.05-sl6/lib/libCore.so

Root > Function G__ateval() busy flag cleared
x[0]->GetName()
(const char* 0x27fcb39)"TObjArray"
root [10] ((TObjString*)x[0])->GetName()
(const char* 0x27fcb39)"TObjArray"
root [11] x[0]->GetString()
Error: Can’t call TObjArray::GetString() in current scope (tmpfile):1:
Possible candidates are…
*** Interpreter error recovered ***
root [12] ((TObjString*)x[0])->GetString()
(class TString)"�▒<3 P(((TObjString*)x[0])->▒<3Q,"
root [13] [/code]

So obviously I am missing something. Once I get the TObjArray full of TObjStrings I think it should be easy to save to a TFile, but I am open to any straightforward method to get this done. Can you provide me with a code snippet that will accomplish my goal, and maybe a snippet to read this from file too?

Thanks,

Dan

PS Why are the constructors no longer given in the list of methods, even though the constructor syntax and descriptions are given below?

Hi,

to write:

vector<string> myStrings {"a","b"};
TFile f("ofile.root","RECREATE");
f.WriteObjectAny(&myStrings,"vector<string>","myStrings");
f.Close();

to read:

TFile f("ofile.root");
std::vector<string>* vp(nullptr);
f.GetObject("myStrings",vp)
for (auto&& s : *vp) cout << s << endl;

Cheers,
Danilo