# THStack doesn't keep histograms

**URL:** <https://root-forum.cern.ch/t/thstack-doesnt-keep-histograms/41735>\
**Category:** Newbie\
**Created:** [October 6, 2020, 3:22pm UTC](https://root-forum.cern.ch/t/thstack-doesnt-keep-histograms/41735 "2020-10-06T15:22:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![MartinSerfling](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/77aa72/32.png) [@MartinSerfling](https://root-forum.cern.ch/u/MartinSerfling)\
**Post date:** [October 6, 2020, 3:22pm UTC](https://root-forum.cern.ch/t/thstack-doesnt-keep-histograms/41735/1 "2020-10-06T15:22:51Z")

</div>

Hi there,

I am going crazy with THStack and need some help to find the mistake I’m apparently making. I loop over a couple of files, each filling multiple histograms and adding them to several THStacks `hst[i]`. However, after looping over all files, drawing each of the THStacks to a separate canvas and saving this canvas, only the last histogram is plotted in my canvas.  
I simply cannot find any reason why the THStack doesn’t seem to save the individual histograms although they have different names and different indices. A portion of my code is below, hopefully enough for somebody to find my mistake.  
For better understanding: `fin` and `fout` are pointers to in- and output files and `nchan*j` is some offset (with `j` being the counter of files i have looped over so far) to avoid filling the same histogram again and to add a different histogram to the histogram stack.

Thanks,  
Martin

```auto

   //open output file, remove file extension for name
    foutName.Form("%.*s_henergy.root",len-5,filename[j]); //set name of output file
    fout = new TFile(foutName,"recreate");

    for (int i = 0; i < nchan; i++) {
      //write histograms to output file
      hE[i+(nchan*j)]->Write();
      //if bool "stack" is true, add current histogram to stack
      if (stack) {
        c[i]->cd(); //switch to canvas corresponding to current channel
        hst[i]->Add(hE[i+(nchan*j)]); //add current histogram to stack
        hst[i]->Draw("plc nostack"); //draw histogram stack to compare all histograms	
        //if current file is the last file to be analyzed, write histogram stack to file with legend
        if (j == nFiles-1) {
          gPad->SetLogy(1); //set logarithmic scale on y axis
	  
          TLegend *l1 = (TLegend*)gPad->BuildLegend(0.6,0.8,0.9,0.9,""); //automatically build legend
          l1->SetTextFont(43);
          l1->SetTextSize(16);
          c[i]->Write();
          c[i]->Close();

          delete c[i]; //delete all instances of canvas' created with new
          delete hst[i]; //delete all instances of stacks created with new
        }
      }
    }
    cout << "file created: " << foutName << endl << endl; //output for user to see that file was written

    //close files and delete them from memory
    fout->Close();
    fin->Close();
    delete fout;
    delete fin;

```

_ROOT Version:_ 6.22/02  
_Platform:_ CentOS 7

* * *

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [October 6, 2020, 3:44pm UTC](https://root-forum.cern.ch/t/thstack-doesnt-keep-histograms/41735/2 "2020-10-06T15:44:26Z")

</div>

Hi Martin,  
could you maybe reduce the code to something that we can run and that only contains what is important to reproduce your issue? That would help a lot.

@moneta or @couet might be able to help further.

Cheers,  
Enrico

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [October 7, 2020, 7:37am UTC](https://root-forum.cern.ch/t/thstack-doesnt-keep-histograms/41735/3 "2020-10-07T07:37:46Z")

</div>

It is hard to tell what’s going on without a running example.  
Can you provides something reproducing the problem ?

---

<div class="post-metadata">

**Author:** ![MartinSerfling](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/77aa72/32.png) [@MartinSerfling](https://root-forum.cern.ch/u/MartinSerfling)\
**Post date:** [October 7, 2020, 11:23am UTC](https://root-forum.cern.ch/t/thstack-doesnt-keep-histograms/41735/4 "2020-10-07T11:23:49Z")

</div>

Thank you @eguiraud and @couet for the quick response. Since producing a runnable program without our data would have been quite difficult and even more time consuming for me, I tried some more things and finally found the solution which I want to share.

Although I had created the arrays for my histograms and histogram stacks before any loop, I actually created the histograms and stacks themselves (with the `new` command) **after** opening the corresponding input file. Therefore, when i closed and deleted the pointer to my input file, all histograms and stacks were deleted from memory.

Now I create the individual histograms and stacks **before** opening each input file and they “survive” closing the input files and deleting the pointer.

I guess this is a typical “Know what your current scope is and what is connected to it.” issue.

Thank you anyways for your feedback and will to help. 🙂  
Martin
