Code crashes when trying to add a legend

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!

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

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!

Thanks!

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

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.

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

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)

1 Like

That works! Thanks a lot!