Saving an Array to a Root File

Hello all,

I have a macro that fits hundreds of histograms. I would like to take the mean of each fit and use it in a separate macro. The most straightforward way that I can think of is to:

  1. Create an array: Double_t meaner[900];
  2. Populated the array every time a fit is done: meaner[i] = fitFunction->GetParameter(x);
  3. Write the array to a root file that I can use in a different macro.

I am stuck on step 3. Is it possible to store an array into a root file and then retrieve the array in a different macro to access its elements? If not, what are my alternatives? I have thought about writing the mean results to a text file and using the text file as an input in the other separate macro.

Thank you very much for your help in advance!

Try with the similar type: std::array<Double_t, 900>

@pcanal Thanks a lot for the reply!

In this approach, would writing the array to the file be as follows?

TFile *outputFile = TFile::Open("outputFileName.root", "RECREATE");
std::array<Double_t,900> arr;
//Fill array with stuff
outputFile->WriteObject(&arr,"Name_of_array");

Because I am assuming that the following would not work since arr is not a root object:

outputFile->cd();
arr->Write();

You are correct.

1 Like