Storing information (string) in a TTree filled with event variables

ROOT Version: 6.14/09

Hello all,

I have a question about storing a string in a TTree.

When I call the function where the tree is created, if have the name of the output file given as well as const std::string &outFile

Let’s say, I want to store this string in my tree.

TTree *tree = new TTree("nominal", "nominal");
TBranch *newbranch = tree->Branch("dataset", &outFile);

This works.
However, later I store variables eventwise in the same tree, such as
tree->Branch("JetPt", &JetPt); which are around 240*10^3 floats filled in a for loop.

My Problem comes when investigating the produce ROOT-FIle, it is very large.
It seems like, that the event variables are stored In the dataset branch as well.

I suspect this is caused by the wish, to keep all branches at the same amount of entries, such that a event is defined well.

If I save the the string only to another tree e.g.

TTree *metadata = new TTree("metadata", "metadata");
metadata->Branch("out", &outFile);

the processing is way faster, and the output file is way smaller.

But then I don’t have the information stored in tree.
I tried a workaround via friends tree->AddFriend("metadata"); but that doesn’t work.

Any help would be appreciated.
If you need more information or code snippets, ask

Hi @frengelk ,
and welcome to the ROOT forum!

If you do add the string branch, then add another branch, and then call TTree::Fill many times, both branches are filled at each call.

It’s possible to create TTrees with unbalanced branches (i.e. with some branches with more entries than others) but I would not recommend it as it makes treatment of that TTree a bit more cumbersome.

Friend trees (possibly with a TTreeIndex to associate entries in the smaller TTree with entries in the larger TTree correctly) are the standard solution for a scenario like yours. It also depends on what you need to do with the non-eventwise data, and how many entries you have for those: is it just one entry, to store some dataset metadata?

Cheers,
Enrico

Hi, thanks for the answer! :slight_smile:

yeah, that is what I suspected.

Just out of curiosity, could I add the branch with a single string after every other step is done (e.g. right before closing the file) without a problem, or does root not like it?

Exactly. Right now, I just want to save the string containing the dataset’s name, so that after usage of many different files later, I can still pinpoint where each tree stems from.

In the end the ideal scenario would be:

  • Reading in the file
  • accessing the tree
  • do the computation with the event-wise data
  • before saving as histograms to somewhere else, extract the information of the dataset-string to be able to connect the produced output to the right physic process, so just read the “metadata”-banch

So it is just one entry in the “metadata” branch that I want to read out.
Could you elaborate on

possibly with a TTreeIndex to associate entries in the smaller TTree with entries in the larger TTree correctly
?

Thanks in advance!

Yes you can, but again I would recommend against having a TTree with branches with differing number of entries.

For your scenario, I would just store the dataset name as a TString in the file (not even in another TTree), next to the TTree.

Cheers,
Enrico