THStack and Legends

Hello, I was trying to apply some legends to THStack type histograms (after stacking 10 of them together) and I can’t seem to get it to work. My current code is this:

//C++
#include<iostream>
#include<sstream>
#include<vector>
#include<stdlib.h>

//root 
#include "TH1D.h"
#include "TFile.h"
#include "TString.h"
#include "THStack.h"
#include "TCanvas.h"
#include "TLegend.h"
#include "TPaveLabel.h"

using namespace std;

//function: "main"
int run(){

  TString ext = ".root"; //filename extension

  //vector containing root files
  vector<TString> filename; 
  filename.push_back("DYJetsToLL_HT");
  filename.push_back("Diboson");
  filename.push_back("QCD_HT");
  filename.push_back("TTWJetsToQQ_MC0");
  filename.push_back("TTZToQQ_MC0");
  filename.push_back("TTbarNoHad");
  filename.push_back("Triboson");
  filename.push_back("WJetsToLNu_HT");
  filename.push_back("ZJetsToNuNu_HT");
  filename.push_back("tW");

  //vector containing histograms from previous files
  vector<TString> histoname;
  histoname.push_back( "hMET" );
  histoname.push_back( "hNbJets" );
  histoname.push_back( "hNTops" );
  histoname.push_back( "hMT2" );
  histoname.push_back( "hYields" );
  histoname.push_back( "hNJets" );
  histoname.push_back( "hHT" );
  histoname.push_back( "hdPhi0" );
  histoname.push_back( "hdPhi1" );
  histoname.push_back( "hdPhi2" );

  //vector sizes
  const int histos=histoname.size();
  const int filenum=filename.size();

  //stack vectors
  vector<THStack> stack(histos);
  
  //temporary histogram pointer object for looping
  TH1D* temp;

  //Create legend
  TLegend *leg = new TLegend(0.68,0.72,0.98,0.92);

  //open file for storing stacked histograms
  //  TFile* newfile=new TFile("newHistoStack","RECREATE");

  TCanvas *cv = new TCanvas("cv","cv",700,700);                                                                              
  cv->Divide(1,1);                                                                                                               
  cv->cd(0);                                                                                                                    

  //for loop that creates stacked histogram
  for(int i=0;i<filenum;i++){
    TFile file(filename[i]+ext);
    
    for(int j=0;j<histos;j++){
      file.GetObject(histoname[j],temp);
      temp->SetDirectory(0);
      temp->SetLineColor(i);
      stack[j].Add(temp);
      leg->AddEntry(temp,filename[i],"l");
    }
  }

  //open file for storing stacked histograms                                                                                   
  TFile* newfile=new TFile("newHistoStack","RECREATE");

  //loop for naming and writing stacked histograms
  for(int k=0; k<histos; k++){

    stack[k].SetTitle(histoname[k]);
    stack[k].Write();
  }

  leg->Write();

  return 0;
}
  

Any help would be greatly appreciated. Thanks.

You should Draw the legend in the Canvas

Hi,

I tried your code (adapted to my problem) and I have this error :

Error in <HandleInterpreterException>: Trying to dereference null pointer or trying to call routine taking non-null arguments.
Execution of your code was aborted.
In file included from input_line_10:1:
/home/swuyckens/stackHistos.C:178:7: warning: null passed to a callee that requires a non-null argument [-Wnonnull]
      temp->SetDirectory(0);

Any ideas ? You got the same?

Right before that line, add:

if (!temp) continue; // just a precaution