# Saving an Array to a Root File

**URL:** <https://root-forum.cern.ch/t/saving-an-array-to-a-root-file/53723>\
**Category:** Newbie\
**Tags:** root\
**Created:** [February 21, 2023, 7:18pm UTC](https://root-forum.cern.ch/t/saving-an-array-to-a-root-file/53723 "2023-02-21T19:18:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bassam](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/b38774/32.png) [@Bassam](https://root-forum.cern.ch/u/Bassam)\
**Post date:** [February 21, 2023, 7:18pm UTC](https://root-forum.cern.ch/t/saving-an-array-to-a-root-file/53723/1 "2023-02-21T19:18:55Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [February 22, 2023, 12:05am UTC](https://root-forum.cern.ch/t/saving-an-array-to-a-root-file/53723/2 "2023-02-22T00:05:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Bassam](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/b38774/32.png) [@Bassam](https://root-forum.cern.ch/u/Bassam)\
**Post date:** [February 22, 2023, 2:15am UTC](https://root-forum.cern.ch/t/saving-an-array-to-a-root-file/53723/3 "2023-02-22T02:15:49Z")

</div>

@pcanal Thanks a lot for the reply!

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

```auto
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:

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

```

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [February 23, 2023, 12:23am UTC](https://root-forum.cern.ch/t/saving-an-array-to-a-root-file/53723/4 "2023-02-23T00:23:29Z")

</div>

You are correct.
