# RooPlot won't draw after reading from .root file

**URL:** <https://root-forum.cern.ch/t/rooplot-wont-draw-after-reading-from-root-file/45063>\
**Category:** Newbie\
**Created:** [May 25, 2021, 2:17pm UTC](https://root-forum.cern.ch/t/rooplot-wont-draw-after-reading-from-root-file/45063 "2021-05-25T14:17:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jacob\_Repucci](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jacob_repucci/32/24488_2.png) [@Jacob\_Repucci](https://root-forum.cern.ch/u/Jacob_Repucci)\
**Post date:** [May 25, 2021, 2:17pm UTC](https://root-forum.cern.ch/t/rooplot-wont-draw-after-reading-from-root-file/45063/1 "2021-05-25T14:17:14Z")

</div>

In [another post](https://root-forum.cern.ch/t/cant-get-histogram-to-draw-after-reading-from-root-file/44937) 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:

```auto
#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

* * *

---

<div class="post-metadata">

**Author:** ![Axel](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/axel/32/11084_2.png) [@Axel](https://root-forum.cern.ch/u/Axel)\
**Post date:** [May 25, 2021, 2:31pm UTC](https://root-forum.cern.ch/t/rooplot-wont-draw-after-reading-from-root-file/45063/2 "2021-05-25T14:31:53Z")

</div>

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

> [@Jacob\_Repucci](#):
>
> ```auto
> cout << "Reading data \n";
> {
> TFile allbinsfile("allbinsfile.root");
> // ******************
> // ***GRAPH DATA***
> // ******************
> TH2F * eq_9_hist = (TH2F *) allbinsfile.Get("equation_9_fit");
> eq_9_hist->SetDirectory(gROOT);
> }
> cout << "Eq 9 N: " << eq_9_hist->GetEntries() << "\n";
> cout << "Data read!\n";
> 
> //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";
> 
> eq_9_hist->Draw("COLZ");
> 
> ```

Is that any better?

---

<div class="post-metadata">

**Author:** ![Jacob\_Repucci](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jacob_repucci/32/24488_2.png) [@Jacob\_Repucci](https://root-forum.cern.ch/u/Jacob_Repucci)\
**Post date:** [May 25, 2021, 3:00pm UTC](https://root-forum.cern.ch/t/rooplot-wont-draw-after-reading-from-root-file/45063/3 "2021-05-25T15:00:42Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jonas](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jonas/32/24756_2.png) [@jonas](https://root-forum.cern.ch/u/jonas)\
**Post date:** [June 4, 2021, 3:04pm UTC](https://root-forum.cern.ch/t/rooplot-wont-draw-after-reading-from-root-file/45063/4 "2021-06-04T15:04:15Z")

</div>

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
