ca2004
August 16, 2011, 9:29pm
1
Hi everyone,
I want to use ROOT into a C++ code to plot the data generated by C++.
I use this code:
#include <iostream>
#include <iomanip>
// Root
#include "TAxis.h"
#include "TGraph.h"
#include "TMultiGraph.h"
#include "TCanvas.h"
#include "TApplication.h"
#include "TStyle.h"
#include "TPad.h"
#include "TROOT.h"
#include "TColor.h"
#include "TFrame.h"
#include "TVirtualPad.h"
using namespace std;
int main() {
TCanvas *c1 = new TCanvas("c1","A Simple Graph Example",200,10,700,500);
c1->SetFillColor(42);
c1->SetGrid();
const int n = 20;
double x[n], y[n];
for (int i=0;i<n;i++) {
x[i] = i;
y[i] = 2*i;
cout<<setprecision(7)<<x[i]<<"\t"<<y[i]<<endl;
}
TGraph *gr = new TGraph(n,x,y);
gr->SetLineColor(2);
gr->SetLineWidth(4);
gr->SetMarkerColor(4);
gr->SetMarkerStyle(21);
gr->SetTitle("a simple graph");
gr->GetXaxis()->SetTitle("X title");
gr->GetYaxis()->SetTitle("Y title");
gr->Draw("ACP");
c1->Update();
c1->Modified();
return 0;
}
I am compiling in this way:
g++ -c ROOT-CPP.cpp -o root.o -I/usr/local/root/include/
g++ -g root.o -o root.out `/usr/local/root/bin/root-config --libs`
where ROOT-CPP.cpp is the C++ code source and I installed ROOT in /usr/local/root. The compilation is successful, but when I try to run root.out, it do not generate the graphic window with TCanvas.
Any idea??
Thanks in advance;
Manuel
Hi Manuel,
I’m not an expert, but it seems you want a stand alone gui. There is a example of this at http://root.cern.ch/root/html/tutorials/gui/guitest.C.html . It is unfortunately large amount of code, but the very end of it is what you’re interested in.
[code]void guitest()
{
new TestMainFrame(gClient->GetRoot(), 400, 220);
}
//---- Main program ------------------------------------------------------------
#ifdef STANDALONE
int main(int argc, char **argv)
{
TApplication theApp(“App”, &argc, argv);
if (gROOT->IsBatch()) {
fprintf(stderr, “%s: cannot run in batch mode\n”, argv[0]);
return 1;
}
guitest();
theApp.Run();
return 0;
}
#endif [/code]
Surge
Hi Manuel,
I worked on your program again and got it work.
[code]#include
#include
// Root
#include “TAxis.h”
#include “TGraph.h”
#include “TMultiGraph.h”
#include “TCanvas.h”
#include “TApplication.h”
#include “TStyle.h”
#include “TPad.h”
#include “TROOT.h”
#include “TColor.h”
#include “TGFrame.h”
#include “TVirtualPad.h”
using namespace std;
void testing() { //new
TCanvas *c1 = new TCanvas(“c1”,“A Simple Graph Example”,200,10,700,500);
c1->SetFillColor(42);
c1->SetGrid();
const int n = 20;
double x[n], y[n];
for (int i=0;i<n;i++) {
x[i] = i;
y[i] = 2*i;
cout<<setprecision(7)<<x[i]<<"\t"<<y[i]<<endl;
}
TGraph *gr = new TGraph(n,x,y);
gr->SetLineColor(2);
gr->SetLineWidth(4);
gr->SetMarkerColor(4);
gr->SetMarkerStyle(21);
gr->SetTitle("a simple graph");
gr->GetXaxis()->SetTitle("X title");
gr->GetYaxis()->SetTitle("Y title");
gr->Draw("ACP");
c1->Update();
c1->Modified();
c1->Connect("Closed()", "TApplication", gApplication, "Terminate()"); //new
}
int main(int argc, char **argv)
{
// all new
TApplication theApp(“App”, &argc, argv);
if (gROOT->IsBatch()) {
fprintf(stderr, “%s: cannot run in batch mode\n”, argv[0]);
return 1;
}
testing();
theApp.Run();
return 0;
}[/code]
I complied by making a Makefile which just had
[code]R_LDFLAGS = root-config --ldflags
R_LIBS = root-config --libs
R_CFLAGS = root-config --cflags
R_ALL = $(R_LADFLAGS) $(R_LIBS) $(R_CFLAGS)
testing: testing.C
g++ -Wall -fPIC $(R_ALL) -c -o testing.o testing.C
g++ -ansi -W -Wall $(R_ALL) -lGui testing.o -o testing
[/code]
I know on the Makefile I went a bit overkill with which flags were necessary when. However, it worked.
Surge
ca2004
August 17, 2011, 7:17pm
4
Hi Surge,
Thanks so much for your help.
The code worked very well in my computer.
Thanks,
Manuel