# What is the correct way to save a histogram which takes a lot of data from Many input files?

**URL:** <https://root-forum.cern.ch/t/what-is-the-correct-way-to-save-a-histogram-which-takes-a-lot-of-data-from-many-input-files/35254>\
**Category:** Newbie\
**Tags:** tree\
**Created:** [July 18, 2019, 8:31am UTC](https://root-forum.cern.ch/t/what-is-the-correct-way-to-save-a-histogram-which-takes-a-lot-of-data-from-many-input-files/35254 "2019-07-18T08:31:49Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [July 18, 2019, 8:45am UTC](https://root-forum.cern.ch/t/what-is-the-correct-way-to-save-a-histogram-which-takes-a-lot-of-data-from-many-input-files/35254/2 "2019-07-18T08:45:01Z")

</div>

Have you run the “MakeClass” on the TChain (mandatory in your case) or on a single TTree (will create a misbehaving class in your case)?

In order to do it correctly for a TChain, in an interactive ROOT session, try something like this:

```cpp
TChain *t = new TChain("TTreeName");
t->AddFile("SomeFileName.root"); // (at least) one file (is sufficient)
t->LoadTree(0); // just the very first entry
t->MakeClass("NewClassName");
delete t; // cleanup

```

In principle, the “NewClassName::Loop()” method could look like this:

```cpp
void NewClassName::Loop()
{
  if (fChain == 0) return; // just a precaution

  TFile *fout = TFile::Open("output_file.root", "recreate");
  if (fout) fout->cd(); // all new histograms will be connected to "fout"
  // ... create / book / initialize all histograms here ...

  Long64_t nentries = fChain->GetEntriesFast();

  Long64_t nbytes = 0, nb = 0;
  for (Long64_t jentry = 0; jentry < nentries; jentry++) {
    Long64_t ientry = LoadTree(jentry);
    if (ientry < 0) break;
    nb = fChain->GetEntry(jentry); nbytes += nb;
    // if (Cut(ientry) < 0) continue;
    // ... fill all histograms with the current "jentry" data here ...
  }

  if (fout) fout->Write(); // save all histograms
  delete fout; // automatically deletes all histograms, too
  fout = 0; // just a precaution
}

```

See also:

> [@Aux;1 aux;2 aux3;](https://root-forum.cern.ch/t/aux-1-aux-2-aux3/13345/2):
>
> Search for “cycle” and/or “namecycle”, for example in: In a Nutshell: [Input and Output](https://root.cern.ch/d/input-and-output) . ROOT User’s Guide: [Input/Output](https://root.cern.ch/root/htmldoc/guides/users-guide/ROOTUsersGuide.html#inputoutput) : [Retrieving Objects from Disk](https://root.cern.ch/root/htmldoc/guides/users-guide/ROOTUsersGuide.html#retrieving-objects-from-disk) , [Subdirectories and Navigation](https://root.cern.ch/root/htmldoc/guides/users-guide/ROOTUsersGuide.html#subdirectories-and-navigation) . Just for reference: [some additional notes about GetObject, Get, FindObjectAny, FindKeyAny, ReadObj, ReadObjAny](https://root-forum.cern.ch/t/broken-tdirectoryfile-findobjectany-and-tdirectoryfile-findkeyany/20943/16) .

---

_[View the full topic](https://root-forum.cern.ch/t/what-is-the-correct-way-to-save-a-histogram-which-takes-a-lot-of-data-from-many-input-files/35254)._
