# Code crashes when trying to add a legend

**URL:** <https://root-forum.cern.ch/t/code-crashes-when-trying-to-add-a-legend/39706>\
**Category:** Newbie\
**Created:** [June 1, 2020, 2:19pm UTC](https://root-forum.cern.ch/t/code-crashes-when-trying-to-add-a-legend/39706 "2020-06-01T14:19:38Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![evdv](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/2acd7d/32.png) [@evdv](https://root-forum.cern.ch/u/evdv)\
**Post date:** [June 1, 2020, 2:19pm UTC](https://root-forum.cern.ch/t/code-crashes-when-trying-to-add-a-legend/39706/1 "2020-06-01T14:19:39Z")

</div>

Hi, I am trying to add a legend to my plot. However, for some reason, my code crashes when I to AddEntry. I am really confused, because I am doing the exact same thing I have been doing before and it has always worked. Thanks!

```auto
#include "TFile.h"
#include "TCanvas.h"
#include "TH1.h"
#include "TMultiGraph.h"
#include "TGraphAsymmErrors.h"
#include "TLegend.h"
#include "TString.h"
#include <vector>
#include <iostream>

void Trigger_turnon_pT(){
  
  auto f = new TFile ("/data/field01/users/vorst/HaddOutput_old.root");
  TCanvas *c = new TCanvas("c", "", 500, 500);
  TH1D *k1 = (TH1D*)f->Get("AfterCuts/yCut_0.6/pT1Cut_0/pT2Cut_0/LeadingJetPt");
  
  std::vector<std::string> TriggerCut = {"L1_J20", "L1_J40", "L1_J50", "L1_J75", "L1_J100"};
  int n = TriggerCut.size();
  TGraphAsymmErrors *eff[n];
  TH1D *h[n];  
   for (unsigned i = 0; i < n; i++) {
     f->GetObject(TString::Format("%s/yCut_0.6/pT1Cut_0/pT2Cut_0/LeadingJetPt", TriggerCut[i].c_str()) , h[i]);  
    
    TGraphAsymmErrors *eff[n];
    eff[i] = new TGraphAsymmErrors();
    eff[i]->Divide(h[i], k1);
    eff[i]->GetYaxis()->SetTitle("Efficiency");
    eff[i]->GetXaxis()->SetTitle("Leading jet p_{T} (GeV)");    
    eff[i]->SetMinimum(0);
    eff[i]->SetMaximum(1.8);
    eff[i]->GetXaxis()->SetLimits(0, 250);
    eff[i]->SetMarkerSize(0.8); 
    if (i == 0) {
      eff[i]->SetLineColor(kRed-7); 
      eff[i] ->SetMarkerColor(kRed-7); }
    else if (i == 1) {
      eff[i]->SetLineColor(kBlue+3); 
      eff[i] ->SetMarkerColor(kBlue+3); }
    else if (i == 2) {
      eff[i]->SetLineColor(kViolet+7); 
      eff[i] ->SetMarkerColor(kViolet+7); }
    else if (i == 3) {
      eff[i]->SetLineColor(kAzure+7); 
      eff[i] ->SetMarkerColor(kAzure+7); }
    else {
      eff[i]->SetLineColor(kGreen+1); 
      eff[i] ->SetMarkerColor(kGreen+1); }
    
    if (i==0) eff[i]->Draw("ap");
    else eff[i]->Draw("p same");
     }
     
   auto legend = new TLegend(0.1, 0.5, 0.28, 0.9);
   legend->AddEntry(eff[0], "L1_J20");
   legend->AddEntry(eff[1], "L1_J40");
   legend->AddEntry(eff[2], "L1_J50");
   legend->AddEntry(eff[3], "L1_J75");
   legend->AddEntry(eff[4], "L1_J100");
   legend->Draw("same");
  }

```

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [June 1, 2020, 2:39pm UTC](https://root-forum.cern.ch/t/code-crashes-when-trying-to-add-a-legend/39706/2 "2020-06-01T14:39:47Z")

</div>

```auto
#include "TFile.h"
#include "TCanvas.h"
#include "TH1.h"
#include "TMultiGraph.h"
#include "TGraphAsymmErrors.h"
#include "TLegend.h"
#include "TString.h"
#include <vector>
#include <iostream>

void Trigger_turnon_pT(){
  
  auto f = new TFile ("/data/field01/users/vorst/HaddOutput_old.root");
  TCanvas *c = new TCanvas("c", "", 500, 500);
  TH1D *k1 = (TH1D*)f->Get("AfterCuts/yCut_0.6/pT1Cut_0/pT2Cut_0/LeadingJetPt");
  
  std::vector<std::string> TriggerCut = {"L1_J20", "L1_J40", "L1_J50", "L1_J75", "L1_J100"};
  int n = TriggerCut.size();
  TGraphAsymmErrors *eff[n]; /// *** Moved
  TH1D *h[n];  
  TGraphAsymmErrors *eff[n];
   for (unsigned i = 0; i < n; i++) {
     f->GetObject(TString::Format("%s/yCut_0.6/pT1Cut_0/pT2Cut_0/LeadingJetPt", TriggerCut[i].c_str()) , h[i]);  
     
    if (!h[i]) { /// *** Added 
       std::cerr << "Could not find histogram: " << (TString::Format("%s/yCut_0.6/pT1Cut_0/pT2Cut_0/LeadingJetPt", TriggerCut[i].c_str()) << std::end;
       eff[i] = nullptr;
       continue;
    }

    eff[i] = new TGraphAsymmErrors();
    eff[i]->Divide(h[i], k1);
    eff[i]->GetYaxis()->SetTitle("Efficiency");
    eff[i]->GetXaxis()->SetTitle("Leading jet p_{T} (GeV)");    
    eff[i]->SetMinimum(0);
    eff[i]->SetMaximum(1.8);
    eff[i]->GetXaxis()->SetLimits(0, 250);
    eff[i]->SetMarkerSize(0.8); 
    if (i == 0) {
      eff[i]->SetLineColor(kRed-7); 
      eff[i] ->SetMarkerColor(kRed-7); }
    else if (i == 1) {
      eff[i]->SetLineColor(kBlue+3); 
      eff[i] ->SetMarkerColor(kBlue+3); }
    else if (i == 2) {
      eff[i]->SetLineColor(kViolet+7); 
      eff[i] ->SetMarkerColor(kViolet+7); }
    else if (i == 3) {
      eff[i]->SetLineColor(kAzure+7); 
      eff[i] ->SetMarkerColor(kAzure+7); }
    else {
      eff[i]->SetLineColor(kGreen+1); 
      eff[i] ->SetMarkerColor(kGreen+1); }
    
     if (i==0) eff[i]->Draw("ap");
     else eff[i]->Draw("p same");
  }
     
   auto legend = new TLegend(0.1, 0.5, 0.28, 0.9);
   legend->AddEntry(eff[0], "L1_J20");
   legend->AddEntry(eff[1], "L1_J40");
   legend->AddEntry(eff[2], "L1_J50");
   legend->AddEntry(eff[3], "L1_J75");
   legend->AddEntry(eff[4], "L1_J100");
   legend->Draw("same");
}

```

---

<div class="post-metadata">

**Author:** ![evdv](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/2acd7d/32.png) [@evdv](https://root-forum.cern.ch/u/evdv)\
**Post date:** [June 1, 2020, 3:48pm UTC](https://root-forum.cern.ch/t/code-crashes-when-trying-to-add-a-legend/39706/3 "2020-06-01T15:48:25Z")

</div>

Thanks for the quick reply! Unfortunately, It still doesn’t seem to be working. I have attached my file here, maybe that makes it easier!

> **[HaddOutput\_old.root](https://www.dropbox.com/s/sqk44pete5vu3wv/HaddOutput_old.root?dl=0)**
>
> Shared with Dropbox

Thanks!

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [June 1, 2020, 4:01pm UTC](https://root-forum.cern.ch/t/code-crashes-when-trying-to-add-a-legend/39706/4 "2020-06-01T16:01:41Z")

</div>

How does the new version fail? What does valgrind report if you run the failing example with it?

---

<div class="post-metadata">

**Author:** ![evdv](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/2acd7d/32.png) [@evdv](https://root-forum.cern.ch/u/evdv)\
**Post date:** [June 1, 2020, 4:08pm UTC](https://root-forum.cern.ch/t/code-crashes-when-trying-to-add-a-legend/39706/5 "2020-06-01T16:08:51Z")

</div>

I get the following error with the new version: error:

“reference to overloaded function could not be resolved; did you mean to call it?  
…histogram: " \<\< (TString::Format(”%s/yCut\_0.6/pT1Cut\_0/pT2Cut\_0/LeadingJetPt", TriggerCut[i].c\_str()) \<\< std::end;"

And then I get loads of Notes.

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [June 1, 2020, 4:32pm UTC](https://root-forum.cern.ch/t/code-crashes-when-trying-to-add-a-legend/39706/6 "2020-06-01T16:32:40Z")

</div>

I see. The print statement needs to be corrected. Try:

```auto
TString name = TString::Format("%s/yCut_0.6/pT1Cut_0/pT2Cut_0/LeadingJetPt", TriggerCut[i].c_str());
std::cerr << "Could not find histogram: " << name.Data() << std::endl;

```

(The real mistake is probably the missing ‘l’ at the end of `std::endl`)

---

<div class="post-metadata">

**Author:** ![evdv](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/2acd7d/32.png) [@evdv](https://root-forum.cern.ch/u/evdv)\
**Post date:** [June 2, 2020, 10:18am UTC](https://root-forum.cern.ch/t/code-crashes-when-trying-to-add-a-legend/39706/7 "2020-06-02T10:18:24Z")

</div>

That works! Thanks a lot!
