Writing metadata into TFile

_ROOT Version: 6.16/00
_Platform: Ubuntu 20.04 (WSL)
Compiler: Not Provided

I have been trying to save some information that describes the content I am saving into a TFile. But, I do not found an explicit example.
I will an exact part of my problem. Filling the histograms can be splitter into two cases A, B; so I have tan histograms for each case. Either A, or B, are described by a variable that is not linked to any information you can retrieve from the class. These descriptors are counters, that I would use later to “Scale()” the TH1 objects. But, I need these counters to be saved into the TFile. I am using an additional ASCII file to store this metadata, but I found it inefficient.

How can I save this information into the TFile? These running variables can be of different types, but I am mostly interested in saving Int_t, Bool_t.

Thanks in advance.

I think @pcanal can help.
TObjString might be an option ?
Also search on this forum that question was already asked.

Hi Dario,

I’m not saying this is ideal (it’s far from it), but in principle you can store your Int_t in a special 1D histogram. Fill it once, and then do yourHistogram->GetMean().

The same goes for a Bool_t: make a 1D histogram with just two bins, -0.5 < x < 0.5 and 0.5 < x < 1.5, fill it with either 0 (false) or 1 (true), and then

const Bool_t isTrue = yourHistogram2->GetMean()>0.5 ? true:false; 

or even simpler

const Bool_t isTrue = yourHistogram2->GetMean()>0.5; 

Also search on this forum that question was already asked.

I really look for that, but I did not found the exact problem. I found something similar but did not work for me.
Moreover, I am pretty new with ROOT, so I am not sure what to do with the TObjString

This could work in the bool type case but is not feasible for the int type: some of them are in a too wide range, so probably exceed the max number of bins a TH1 can store

This could work in the bool type case but is not feasible for the int type: some of them are in a too wide range, so probably exceed the max number of bins a TH1 can store

In this case I guess you can fill a fixed bin (e.g., bin #1) with your value. And then get this value:

int myValue = 424242;
yourHistogram->Fill(1, myValue);
// write yourHistogram to the file, close the file

// now open your file in another program and read the value back:
const int myValue = static_cast<int>(yourHistogram->GetBinContent(1));

To save individual ints and floats, you can use a TParameter objects.

If you have multiple ‘related’ ints and floats, you may also want to consider a TTree filled with them.

Hope that does the work.
Could you please put me an example of how to use it?

TParameter<int> param("somename", 10);
....
file->WriteTObject(&param);

Thanks a lot.
Could you write down how to retrieve this from the file to int type variable?

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.