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

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! :frowning: 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 (1.4 KB)

may be:

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");
   }
}

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:

 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");
        
   }

figured it out; was missing the leg->Draw(); at the end :stuck_out_tongue: 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:


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();
   }