# Error of undeclared identifier of root file

**URL:** <https://root-forum.cern.ch/t/error-of-undeclared-identifier-of-root-file/49662>\
**Category:** Newbie\
**Created:** [April 21, 2022, 7:15am UTC](https://root-forum.cern.ch/t/error-of-undeclared-identifier-of-root-file/49662 "2022-04-21T07:15:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Roy](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/roy/32/30362_2.png) [@Roy](https://root-forum.cern.ch/u/Roy)\
**Post date:** [April 21, 2022, 7:15am UTC](https://root-forum.cern.ch/t/error-of-undeclared-identifier-of-root-file/49662/1 "2022-04-21T07:15:51Z")

</div>

I am trying to plot stacked histogram but Canvas doesn’t show any plot.

```auto
void Analysis(TString file1, TString file2)
{
    THStack hs("hs", "Filtered data for december and January");
    TFile f1(file1);
    TTree* t1 =(TTree*) f1.Get("current_ntuple");
    float_t curr;
    t1->SetBranchAddress("Current", &curr);
    Double_t MinBound = 0;
    Double_t MaxBound = 100;
    Int_t Numbins = 100;

    ULong64_t n = t1->GetEntries();
    TH1D* curr_vals_raw = new TH1D("curr_vals_raw", "Raw current distribution", Numbins, MinBound, MaxBound);
    curr_vals_raw->GetXaxis()->SetTitle("Current [#muA]");
    curr_vals_raw->GetYaxis()->SetTitle("Percentage");
    for (Int_t i = 0; i< n;i++)
    {
        t1->GetEntry(i);
        curr_vals_raw->Fill(curr);
    }
    curr_vals_raw->SetName("Dec Current");
    curr_vals_raw->SetDirectory(0);
    hs.Add(curr_vals_raw);
f1.Close();
    TFile f2(file2);
    TTree* t2= (TTree*) f2.Get("current_ntuple");
    float_t curr2;
    t2 -> SetBranchAddress("Current", &curr2);
    ULong64_t m= t2->GetEntries();
    TH1D* curr_vals_stable = new TH1D("curr_vals_stable", "Stable data Current Distribution", Numbins, MinBound, MaxBound);
    curr_vals_stable->GetXaxis()->SetTitle("Current [#muA]");
    curr_vals_stable->GetYaxis()->SetTitle("Percentage");
    curr_vals_stable->SetLineColor(kRed);
    curr_vals_stable->SetMarkerStyle(7);
    curr_vals_stable->SetName("Stable Current Distribution");
    for (Int_t f = 0; f<m;f++)
    {
        t2->GetEntry(f);
        curr_vals_stable->Fill(curr2);
    }
    curr_vals_stable->SetDirectory(0);
    hs.Add(curr_vals_stable);
f2.Close();

    TCanvas* c1= new TCanvas();
    curr_vals_raw->Draw();
    curr_vals_stable->Draw("same");
    c1->Divide(1,1);
    hs.Draw("nostack");
    TFile outputFile("Decvsjan.root", "recreate");
        curr_vals_raw->Write("Current December");
        curr_vals_stable->Write("Current January");
    outputFile.Close();
}

```

---

<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:** [April 21, 2022, 7:21am UTC](https://root-forum.cern.ch/t/error-of-undeclared-identifier-of-root-file/49662/2 "2022-04-21T07:21:21Z")

</div>

Try: `hs.DrawClone("nostack");`

---

<div class="post-metadata">

**Author:** ![Roy](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/roy/32/30362_2.png) [@Roy](https://root-forum.cern.ch/u/Roy)\
**Post date:** [April 21, 2022, 7:25am UTC](https://root-forum.cern.ch/t/error-of-undeclared-identifier-of-root-file/49662/3 "2022-04-21T07:25:21Z")

</div>

> [@Wile\_E\_Coyote](#):
>
> hs.DrawClone(“nostack”);

Thank You.

---

<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:** [April 21, 2022, 7:34am UTC](https://root-forum.cern.ch/t/error-of-undeclared-identifier-of-root-file/49662/4 "2022-04-21T07:34:54Z")

</div>

Just to clarify, the problem is that the `hs` variable is declare in the local scope of the `Analysis` function, so it goes out of scope at the end of the function execution, and gets destroyed (and deregistered from the canvas where it was drawn for a moment).

`DrawClone` creates a copy of the `THStack` object the lifetime of which is managed by the ROOT session itself (so it survives the scope of `Analysis`) and draws that.

An alternative solution is returning the THStack from the `Analysis` function and call it as `auto hs = Analysis("f1.root", "f2.root")`.

Cheers,  
Enrico
