Open a TGraph from C++

Hi,

Do you know how I can open my graphs and histograms directly from my C++ code?
Right know I need to open root then TBrowser and select the canvas. I would like to skip this part and show the graph right after compilation, is that possible?

I tried system(“root”) which runs root but I can’t open the canvas then

Thanks

Hi,

I’m not sure to understand what you mean by:

If you want to display your graph or histogram, simply call myGraph->Draw() (or myHistogram->Draw())

Cheers, Bertrand.

When I do my_graph->Draw() nothing appears. I need to open a terminal, run root and then new TBrowser always in the terminal and find my canvas c1 to see my graph.

Cheers

1 Like

Can you post here the commands you are using ?
You can find here a very simple example showing how to draw graphs.

Even with c1->Update(); I can’t show the graph, no frame appears. I can show the graph when I open root in a new terminal but I would like to open the graph after compilation

Could you describe exactly what you are doing, otherwise I’m afraid we won’t be able to help you…

Okay, I do this:

        TCanvas *c1 = new TCanvas("c1", "c1", 200, 10, 1280, 720);
	TH1F h1("h1","Histo from a Gaussian",100,-3,3);
	h1.FillRandom("gaus", 10000);
	h1.Draw();

	TFile *file = new TFile("../Debug/test.root", "RECREATE");
	file->WriteTObject(c1);

I compile, then I open root in a new terminal and with “new TBrowser()”, I browse my file test.root and the graph appears.

I tried to add this code:

   c1->Update();
   c1->GetFrame()->SetBorderSize(12);
   c1->Modified()

But the graph doesn’t appear, I still need to browse it in a new terminal with root

Could you post the full code?

#include <iostream>
#include "TROOT.h"
#include "TCanvas.h"
#include "TFile.h"
#include "TH1F.h"
#include "TBrowser.h"
#include "TFrame.h"

using namespace std;

int main(int argc, char *argv[])
{
	TCanvas *c1 = new TCanvas("c1", "c1", 200, 10, 1280, 720);
	TH1F h1("h1","Histo from a Gaussian",100,-3,3);
	h1.FillRandom("gaus", 10000);
	h1.Draw();

	TFile *file = new TFile("../Debug/test.root", "RECREATE");
	file->WriteTObject(c1);

	c1->Update();
	c1->GetFrame()->SetBorderSize(12);
	c1->Modified();

	//system("root");

	system("pause");
	return 0;
}

Here you are :slight_smile:

When I compile, a terminal appears with “press a key to continue” due to the system(“pause”) and nothing else.
To open the histogram (and it works) I do this:
Windows+R : cmd
root
new TBrowser()
double click on test.root
double click on c1

ROOT Forum -> Search -> “gSystem->ProcessEvents();”

I included TSystem.h and put this code at the end and there is no compilation errors but it’s not working

OK, I see… That cannot work, you nee a TApplication instance to process the events… Here is the proper way:

#include <iostream>
#include "TROOT.h"
#include "TCanvas.h"
#include "TFile.h"
#include "TH1F.h"
#include "TBrowser.h"
#include "TFrame.h"
#include "TApplication.h"

using namespace std;

int main(int argc, char *argv[])
{
	TApplication *myApp = new TApplication("myApp", &argc, argv);
	TCanvas *c1 = new TCanvas("c1", "c1", 200, 10, 1280, 720);
	TH1F h1("h1","Histo from a Gaussian",100,-3,3);
	h1.FillRandom("gaus", 10000);
	h1.Draw();

	TFile *file = new TFile("../Debug/test.root", "RECREATE");
	file->WriteTObject(c1);

	c1->Update();
	c1->GetFrame()->SetBorderSize(12);
	c1->Modified();

	myApp->Run();
	return 0;
}

Cheers, Bertrand.

Thanks it works, you are so efficient!

Cheers

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.