# Warning in \<TFile::Append\>: Replacing existing TH1: name (Potential memory leak)

**URL:** <https://root-forum.cern.ch/t/warning-in-tfile-append-replacing-existing-th1-name-potential-memory-leak/53234>\
**Category:** Newbie\
**Tags:** tree, hist\
**Created:** [January 24, 2023, 6:06am UTC](https://root-forum.cern.ch/t/warning-in-tfile-append-replacing-existing-th1-name-potential-memory-leak/53234 "2023-01-24T06:06:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![thegame](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/e495f1/32.png) [@thegame](https://root-forum.cern.ch/u/thegame)\
**Post date:** [January 24, 2023, 6:06am UTC](https://root-forum.cern.ch/t/warning-in-tfile-append-replacing-existing-th1-name-potential-memory-leak/53234/1 "2023-01-24T06:06:24Z")

</div>

Hi everyone,  
I’m trying to plot a bunch of histogram using a for loop. Looks like in every iteration the histogram produced is getting replaced by the new one. Tried to use hstack but still getting same sort of results probably using it wrong way! ☹ Attached is the macro I’m using to plot this in a single canvas with appropiate legends.

To give some context about the data, `F7ICADCcal[i]` is what I’m trying to plot with distinguised color and legends. i takes value from 0 to 5.

Thanks!!

[ICF7.C](https://root-forum.cern.ch/uploads/short-url/mA3yzxQr8Lz1T9UlaIFOwvmnvaK.C) (1.4 KB)

---

<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:** [January 24, 2023, 6:46am UTC](https://root-forum.cern.ch/t/warning-in-tfile-append-replacing-existing-th1-name-potential-memory-leak/53234/2 "2023-01-24T06:46:05Z")

</div>

may be:

```auto
void ICF7()
{
   TStopwatch t;
   TFile *file = new TFile ("cal0061.root");
   TTree *tree = (TTree*)file->Get("cal");
   TChain *ch1 = new TChain("cal");
   ch1 -> Add ("cal0061.root");

   ch1 -> Add ("cal0062.root");
   ch1 -> Add ("cal0063.root");
   ch1 -> Add ("cal0064.root");
   ch1 -> Add ("cal0065.root");
   ch1 -> Add ("cal0066.root");
   ch1 -> Add ("cal0067.root");
   ch1 -> Add ("cal0068.root");
   ch1 -> Add ("cal0069.root");
   ch1 -> Add ("cal0070.root");
   ch1 -> Add ("cal0071.root");
   ch1 -> Add ("cal0072.root");
   ch1 -> Add ("cal0073.root");
   ch1 -> Add ("cal0074.root");

   cout<< ch1->GetEntries()<<endl;

   TH1F *hist[6];
   THStack *hstack = new THStack ("stack", "IC anodes at F7");
   TCanvas *c1 = new TCanvas("c1", "IC anodes at F7 allignment check");

   TLegend *leg = new TLegend(0.7,0.6,0.9,0.9);
   gPad->SetLogy();
   gPad->SetGrid(1,1);

   for (int i = 0; i<6; i++) {
      TString name = Form("hist[%d]",i); 
      hist[i] = new TH1F(TString::Format("name%d",i),"IC anodes at F7",100,-100,1000);
      hist[i]->SetMarkerColor(i+20);
      ch1->Draw(TString::Format("F7ICADCcal[%d]>>name%d",i,i),TString::Format("F7ICADCcal[%d]<1000",i),"GOFF");
      hstack ->Add(hist[i]); 
      leg -> AddEntry(hist[i],name,"p");
   }
}

```

---

<div class="post-metadata">

**Author:** ![thegame](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/e495f1/32.png) [@thegame](https://root-forum.cern.ch/u/thegame)\
**Post date:** [January 24, 2023, 6:53pm UTC](https://root-forum.cern.ch/t/warning-in-tfile-append-replacing-existing-th1-name-potential-memory-leak/53234/3 "2023-01-24T18:53:38Z")

</div>

thanks for your note. This gave a blank canvas probably because of the “GOFF” argument inside TTree::Draw. and Without the “GOFF”, it gave the last i =5 histogram only. However, after a bit of modification, it works now. **BUT** all the histograms **are of the same colour and no legends are drawn on the Canvas**. Here is what it looks like now inside the for loop:

```auto
 for (int i = 0; i<6; i++) {
        TString name = Form("hist[%d]",i); 
        hist[i] = new TH1F(TString::Format("name%d",i),"IC anodes at F7",100,-100,1000);
        hist[i]->SetMarkerColor(i+21);
        hist[i]->SetMarkerStyle(i+1);
        leg -> AddEntry(hist[i],name,"p");
        ch1->Draw(TString::Format("F7ICADCcal[%d]>>name%d",i,i),TString::Format("F7ICADCcal[%d]<1000",i),"");
        hstack ->Add(hist[i]); 
        hstack ->Draw("nostack");
        
   }

```

---

<div class="post-metadata">

**Author:** ![thegame](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/e495f1/32.png) [@thegame](https://root-forum.cern.ch/u/thegame)\
**Post date:** [January 24, 2023, 11:22pm UTC](https://root-forum.cern.ch/t/warning-in-tfile-append-replacing-existing-th1-name-potential-memory-leak/53234/4 "2023-01-24T23:22:15Z")

</div>

figured it out; was missing the `leg->Draw()`; at the end 😛 Also the markercolor from 21 to 26 looks somewhat the same. lighter to darker shades of brown. that’s why could not notice the change in the colour for diff. histo. Now it’s alright. Updated for loop below:

```auto

for (int i = 0; i<6; i++) 
    {
        TString name = Form("F7ICADCcal[%d]",i); 

        hist[i] = new TH1F(TString::Format("name_%d",i),"IC anodes at F7",100,-100,1000);

        leg -> AddEntry(hist[i],name,"p");

        ch1->Draw(TString::Format("F7ICADCcal[%d]>>name_%d",i,i),TString::Format("F7ICADCcal[%d]<1000",i),"");

        hist[i]->SetLineColor(i+1);
        hist[i]->SetLineWidth(2);
        hist[i]->SetMarkerStyle(21);
        hist[i]->SetMarkerSize(1);
        hist[i]->SetMarkerColor(i+1);

        hstack->Add(hist[i]); 
        hstack->Draw("nostack");
        leg->Draw();
   }

```
