Writing simple variable in ROOT files

Hello,

I would like to store some quantities in a ROOT file in addition to my tree.
I guess it’s a dumb thing but I don’t know how to store just a simple variable like a double in a root file.
One solution I used was to create a tree and to fill it once but that’s not satisfactory.
I thought of the User Info of the TTree class but that is for storing class and not simple variable.
Does someone have a better solution.

Thank you.

1 Like

You can use a TVectorD or TVectorF to write an array of 1 or more numbers.

root > TFile f("v.root","recreate") root > TVectorD v(1) root > v[0] = 3.14 root > v.Write("v") and to read the file do

root > TFile f("v.root") root > TVectorD *v = (TVectorD*)f.get("v"); root > v->Print()

Note that if you need to store your numbers in the same file as your ROOt Tree, it is may be better to save your information in the Tree User info list, eg

[code] mytree->GetUserInfo()->Add(TObject *) ; //eg your TVectorD object

[/code]Rene

2 Likes

Hi Brun,

I was wondering whether doing the following could lead to any problems when reading TVectorD from a file in the context of a C++ program:

TVectorD xlo;
TFile* f = new TFile(filename,opt);
TVectorD* xlo_ptr =  (TVectorD*) f->Get("xlo");
n_cell = xlo_ptr->GetNoElements();
xlo.ResizeTo(n);
xlo = *(TVectorD*) f->Get("xlo");
f->Close();
delete f;

This appears to keep xlo in memory (as I would expect because it’s neither a Tree or histogram) where I can do things with it, but I am having the issue of it being overwritten by another TTree object when I invoke the object’s TTree::Draw() fucntion.

See my recent post for details: Memory corruption when calling TTree::Draw (const char *varexp, const TCut &selection)

Thank you,
Y

Yes indeed.

[quote], but I am having the issue of it being overwritten by another TTree object when I invoke the object’s TTree::Draw() fucntion.
See my recent post for details: viewtopic.php?f=3&t=22658[/quote]This must be related to other things you do with the object and/or other objects. Let’s continue that discussion on the link your provided.

I’d like to add an update for 2021 for those who find this thread.

It is possible to store an std::vector<int> or other objects directly in a TFile (see example in the manual), use the method

WriteObject(MyObject,"MyObject_1");

For built-in types TParameter<double> (or other templates) can be used.

Thanks to @pcanal for clarifying that.

1 Like