THStack doesn't keep histograms

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.


   //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
      //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

          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
    delete fout;
    delete fin;

ROOT Version: 6.22/02
Platform: CentOS 7

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.


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

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. :slight_smile:

1 Like