Improve TString-saving methods

I’m attempting to save a (set of) TStrings in a root file. Think of things like a timestamp or the revision that an ntuple was created with, or a username, or anything with a name and a string value. This is now currently totally impossible, it appears. I have found OK’ish half-solutions in these forums and in other documentation, but nothing that meets this (seemingly simple) need.

The usual format to store such a thing in is a string (whether it’s a TString or std::string is irrelevant). Assume I have a class which determines that

std::string m_revision = “v7.0-17-gcbd75df”;

This string needs to be saved in the file which has an ntuple in it, for bookkeeping and versioning purposes. There seem to be two possibilities for this, both not meeting the basic needs of something as simple as storing a string in a file! Then there’s one very dirty hack. Let me go through them:

[ul]
[li]If one creates a TObjString, it doesn’t work. A TObjString has the same name as value (both the class member fString), so rather than something called “revision” in the ntuple, I end up with a string that has both the name and the value “v7.0-17-gcbd75df”. That is useless. It would work if a TObjString had a name and a value.[/li]
[li]The next option is a TNamed. That is also quite useless: it has a name and a title. No value. I don’t care about the title of my object, I care about its value. However, there is no such thing to set. [/li]
[li]The final option is to create a TTree with 1 row and 1 branch. That strikes me as a ridiculous workaround. [/li][/ul]
Is it really true that ROOT provides no use-case whatsoever for something as simple as this?

Cheers,
Geert-Jan

Hi,

I am not sure what you exactly want to persist on file but you start by mentioning a collection of TStrings.
Would writing out a std::vectorstd::string be an option for you?

If not, what do you exactly need?
In any case it is possible to persist instances of any given class with ROOT, even user classes.

Cheers,
Danilo

Hi Danilo,

I’d like to write out one string. An example:

std::string foo = “bar”;

I would like this to end up as “foo” in a TFile, with a value “bar”. If it is in fact possible to do that, could you give me an example? I can’t figure it out without going through classes that inherit from TObject, which leads to the downsides described above.

Cheers,
Geert-Jan

Hi,

now it’s clear.

std::string s="foo";
TFile f("myfile.root","RECREATE");
f.WriteObject(&s,"bar");
f.Close();

and to read

std::string* s;
myfile.GetObject("bar",s)

Cheers,
D

[quote=“dpiparo”]Hi,

now it’s clear.

std::string s="foo";
TFile f("myfile.root","RECREATE");
f.WriteObject(&s,"bar");
f.Close();

and to read

std::string* s;
myfile.GetObject("bar",s)

Cheers,
D[/quote]
Thanks a lot! If I understand the example correctly this would correspond to std::string bar = “foo” though (not the other way around) - which is fine but I just wanted to confirm the syntax.

Do I own the string at this point?

Cheers,
Geert-Jan