RooPlot won't draw after reading from .root file

In another post I had problems graphing a TH2F after reading it from a .root file. I was able to get that working using SetDirectory(gROOT), but now I’m trying to draw a RooPlot and for whatever reason it’s still refusing to draw it. I tried using another SetDirectory statement, but the compiler is telling me that RooPlot doesn’t have a SetDirectory() function, which is strange considering the documentation says it does! Does anyone know what’s going on here?

I’ve attached code below:

#include "TPythia8.h"
#include "TClonesArray.h"
#include "TParticle.h"
#include "TCanvas.h"
#include "TH2F.h"
#include "TMath.h"
#include "TLorentzVector.h"
#include "TVector3.h"

#include <cmath>
#include <vector>
#include <math.h>

using namespace Pythia8;
using namespace std;
using namespace RooFit;

void drellyan_read(Double_t dilution_factor=0.250) {
	cout << "Starting code\n";
	gSystem->Load("libEG");
	gSystem->Load("libEGPythia8");

	//print graphing results
	gStyle->SetOptFit(1111);

	cout << "Reading data \n";

	TFile allbinsfile("allbinsfile.root");
	TH2F * eq_9_hist = (TH2F *) allbinsfile.Get("equation_9_fit");

	cout << "Eq 9 N: " << eq_9_hist->GetEntries() << "\n";

	cout << "Data read!\n";

	// ******************
	// *** GRAPH DATA ***
	// ******************

	//drawing the histogram for equation 9
	cout << "Making the canvas\n";
	TCanvas * ca = new TCanvas("Eq 9 Fit", "Eq 9 Fit", 1200, 600);
	cout << "Canvas made!\n";

	TH2F * eq_9_hist = (TH2F *) allbinsfile.Get("equation_9_fit");
	eq_9_hist->SetDirectory(gROOT);
	eq_9_hist->Draw("COLZ");

	//**** Everything here is just fitting an equation to the graph *****

	//define the variables we're measuring on the histogram
	RooRealVar cos_theta("cos_theta", "cos_theta", -1, 1);
	RooRealVar roo_phi("roo_phi", "roo_phi", 0, 6.28);
	//the roofit histogram we're using
	eq_9_hist->SetDirectory(gROOT);
	RooDataHist cos_theta_v_phi("Cos(theta_d) v Phi_d", "Cos(theta_d) v Phi_d", RooArgList(cos_theta, roo_phi), eq_9_hist);
	//the A0-A7 parameters for eq9
	RooRealVar A0("A0","A0",0.,-1000.,1000.);
	RooRealVar A1("A1","A1",0.,-1000.,1000.);
	RooRealVar A2("A2","A2",0.,-1000.,1000.);
	RooRealVar A3("A3","A3",0.,-1000.,1000.);
	RooRealVar A4("A4","A4",0.,-1000.,1000.);
	RooRealVar A5("A5","A5",0.,-1000.,1000.);
	RooRealVar A6("A6","A6",0.,-1000.,1000.);
	RooRealVar A7("A7","A7",0.,-1000.,1000.);
	//the dilution factor
	RooRealVar f("f", "f", dilution_factor, 0, 1);
	//a constant to multiply the expression by
	RooRealVar Const("Const","Const",1.,-1000.,1000.); //multiplicative constant
	RooRealVar Const2("Const2","Const2",0.,-1000.,1000.); //additional constant

	//equation 9 itself
	RooGenericPdf eq_9("Eq 9", "Eq 9", 
	"Const * ((1+(cos_theta)^2) + 0.5*A0*(1-3*((cos_theta)^2)) + A1*sin(2*acos(cos_theta))*cos(roo_phi) + 0.5*A2*(sin(2*acos(cos_theta))^2)*cos(2*roo_phi) + (1-2*f)*A3*sin(acos(cos_theta))*cos(roo_phi) + (1-2*f)*A4*cos_theta + (1-2*f)*A5*(sin(acos(cos_theta))^2)*(sin(2*roo_phi)) + (1-2*f)*A6*sin(2*acos(cos_theta))*sin(roo_phi) + A7*sin(acos(cos_theta))*sin(roo_phi)) + Const2", RooArgSet(RooArgList(A0, A1, A2, A3, A4, A5, A6, A7), RooArgList(Const, Const2, roo_phi, cos_theta, f)));

	//do the fit!
	eq_9.fitTo(cos_theta_v_phi, "qr");

	//***** Fitting complete! *****

	//actually draw the fits (where I'm having problems)
	TCanvas * cfit = new TCanvas("cfit");
	cfit->Divide(1,2);
	cfit->cd(1);
	eq_9_hist->SetDirectory(gROOT);
	eq_9_hist->Draw("COLZ");
	RooPlot * cosframe = cos_theta.frame(Title("Cos(Theta_D)"));
	cos_theta_v_phi.plotOn(cosframe);
	eq_9.plotOn(cosframe);
	//cosframe->SetDirectory(gROOT);
	cosframe->Draw("COLZ");
	all_cos_chi = cosframe->chiSquare();
	cfit->cd(2);
	RooPlot * phiframe = roo_phi.frame(Title("Phi"));
	cos_theta_v_phi.plotOn(phiframe);
	eq_9.plotOn(phiframe);
	//phiframe->SetDirectory(gROOT);
	phiframe->Draw("COLZ");
	all_phi_chi = phiframe->chiSquare();
} //macro function

ROOT Version: 6.22
Platform: Ubuntu 18.04.5
Compiler: Not Provided


This should work: it just grabs the histogram from the TFile, and then closes it so no other histograms can up in there:

Is that any better?

Hello!

Unfortunately, when I add those brackets, the rest of the code no longer recognizes eq_9_hist.

I should also clarify that right now my problem is getting the RooPlots to graph, not the TH2F.

Hi @Jacob_Repucci,

any chance you can share the allbinsfile.root file here on the forum? That would greatly help us to find the problem. If you prefer, you can also share it with me privately (e.g. via cernbox or AFS if you happen to have a CERN account).

Cheers,
Jonas