# Missing Legend in ROOT File hist

**URL:** <https://root-forum.cern.ch/t/missing-legend-in-root-file-hist/46191>\
**Category:** Newbie\
**Created:** [August 5, 2021, 7:32pm UTC](https://root-forum.cern.ch/t/missing-legend-in-root-file-hist/46191 "2021-08-05T19:32:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![TheScientist](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/82dd89/32.png) [@TheScientist](https://root-forum.cern.ch/u/TheScientist)\
**Post date:** [August 5, 2021, 7:32pm UTC](https://root-forum.cern.ch/t/missing-legend-in-root-file-hist/46191/1 "2021-08-05T19:32:45Z")

</div>

I am trying to create a ROOT file with the histogram I generated. However, when I view the histogram from the file, there is a legend missing. Can you please tell how can I fix this? Thank you.

```auto
void histo()
  4 {
  5 Int_t nBins = 120000; //Bin Size = 0.001 GeV 
  6 Int_t x_lo = 0;
  7 Int_t x_hi = 120;
  8 
  9 //Stacking Histogram with THStack       
 10 THStack *histos = new THStack("histos", "Stacked Neutrino Histograms for Target Radius 1 cm; Energy [GeV]; Entries/0.001 GeV");
 11 TH1D* hist1 = new TH1D("nu_e_Hist", "Histogram Title", nBins, x_lo, x_hi);
 12 hist1->SetMarkerStyle(20);
 13 hist1->SetLineColor(kRed);
 14 
 15 TH1D* hist2 = new TH1D("nu_mu_Hist", "Histogram Title", nBins, x_lo, x_hi);
 16 hist2->SetMarkerStyle(21);
 17 hist2->SetLineColor(kBlue);
 18 
 19 TH1D* hist3 = new TH1D("anti_nu_e_Hist", "Histogram Title", nBins, x_lo, x_hi);
 20 hist3->SetMarkerStyle(22);
 21 hist3->SetLineColor(kGreen-3);
 22 
 23 TH1D* hist4 = new TH1D("anti_nu_mu_Hist", "Histogram Title", nBins, x_lo, x_hi);
 24 hist4->SetMarkerStyle(23);
 25 hist4->SetLineColor(kBlack);
 26 
 27 histos->Add(hist1);
 28 histos->Add(hist2);
 29 histos->Add(hist3);
 30 histos->Add(hist4);
 31 
 32 //Working with ROOT File
 33 TFile* fInput = new TFile("TargetVStudy_1cm_POT1000.root", "READ"); //READ
 34 TTree* tree = (TTree*)fInput->Get("DUNETargetSim");
 35 
 36 Char_t pid[32]; //Particle Identification  
 37 Double_t Energy; //Kinetic Energy
 38 
 39 tree->SetBranchAddress("pid", &pid);
 40 tree->SetBranchAddress("kineticEnergy", &Energy);
 41 
 42 //Looping over to get the data for specific particle and filling the selected histogram
 43 Int_t nEntries = tree->GetEntries("kineticEnergy");
 44 for (Int_t i = 0; i < nEntries; i++)
{
 46 tree->GetEntry(i);
 47 
 48 for (Int_t j = 0; j < 4; j++)
 49 {
 50 switch(j) {
 51 case 0:
 52 if(strcmp(pid, "nu_e") == 0) { hist1->Fill(Energy); }
 53 break;
 54 case 1:
 55 if(strcmp(pid, "nu_mu") == 0) { hist2->Fill(Energy); }
 56 break;
 57 case 2:
 58 if(strcmp(pid, "anti_nu_e") == 0) { hist3->Fill(Energy); }
 59 break;
 60 case 3:
 61 if(strcmp(pid, "anti_nu_mu") == 0) { hist4->Fill(Energy); }
 62 break;
 63 default:
 64 // cout << "Hey There!" << endl;
 65 ;
 66 
 67 }
 68 }
 69 }
 70 
 71 TCanvas* c1 = new TCanvas();
 72 c1->SetTicks();
 73 c1->SetGrid();
 74 c1->SetLogy();
 75 histos->Draw("nostack");
 76 
 77 //Drawing Legend
 78 TLegend *leg = new TLegend();
 79 leg->SetHeader("#nu types", "C"); //C means center
 80 leg->AddEntry(hist1, "nu_e", "f"); //f gives a square box
 81 leg->AddEntry(hist2, "nu_mu", "f");
 82 leg->AddEntry(hist3, "anti_nu_e", "f");
 83 leg->AddEntry(hist4, "anti_nu_mu", "f");
 84 leg->Draw();
 85 
 86 TFile* output = new TFile("all_nu_stacked_1cm.root", "RECREATE");
 87 histos->Write();
 88 output->Close();
 89 }

```

 ![image](https://root-forum.cern.ch/uploads/default/original/3X/b/b/bb6632d81d70ca9b7e8686e793f950b4d6b8a387.png)

---

<div class="post-metadata">

**Author:** ![TheScientist](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/82dd89/32.png) [@TheScientist](https://root-forum.cern.ch/u/TheScientist)\
**Post date:** [August 7, 2021, 4:15pm UTC](https://root-forum.cern.ch/t/missing-legend-in-root-file-hist/46191/2 "2021-08-07T16:15:45Z")

</div>

Legend shows up when I get the histogram by simply Draw(). But when I open my histogram from the ROOT file I created, the legend is missing.

---

<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:** [August 7, 2021, 4:53pm UTC](https://root-forum.cern.ch/t/missing-legend-in-root-file-hist/46191/3 "2021-08-07T16:53:52Z")

</div>

Try:

```cpp
histos->GetHistogram()->GetListOfFunctions()->Add(leg); // it's weird, but it works
histos->Write();

```

---

<div class="post-metadata">

**Author:** ![TheScientist](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/82dd89/32.png) [@TheScientist](https://root-forum.cern.ch/u/TheScientist)\
**Post date:** [August 7, 2021, 6:44pm UTC](https://root-forum.cern.ch/t/missing-legend-in-root-file-hist/46191/4 "2021-08-07T18:44:48Z")

</div>

Thank you again. It’s only weird when it doesn’t work. 😃
