ROOT Canvas for C++ files

I have writen a C++ file with ROOT libraries and a Makefile for compiling that, the executable file is good, but doesn’t show the graphics, only shows the numbers in terminal.

Are you using TApplication in your program ?

I have a program for a gui, in this program I use TApplication, but the program related to this topic doesn’t use TApplication, only TCanvas, TMath, and TGraph.

To render graphics in a canvas in a standalone program you need one.

The file is called curva.cxx, when I compile root -l curva.cxx do all. Shows the numbers in terminal and shows the graphics on canvas, when I make the executable using compiler g++ only shows the numbers in terminal. I use this line g++ root-config --glibs --cflags -o curva curva.cxx

Yes this is expected. To run your program standalone outside root ( your second case) you need to have a TApplication in your program.

Of course I include TApplication, do the same, shows de numbers in terminal but doesn’t plot the function. This the curva.cxx file

#include<TMath.h>
#include<iostream>
#include<TGraph.h>
#include<TCanvas.h>
#include<TApplication.h>
using namespace std;
void curva(){


  TCanvas *c;
  TGraph *g;
  Int_t N;
  double x,y,min,max;
  c=new TCanvas("c","Ver",200,100,700,500);
  c->SetGrid();
  //c=new TCanvas("c","Ver");
  //c.Draw();
  g=new TGraph();
  cout<<"ingrese el número de puntos: ";
  cin>>N;
  cout<<"ingrese el mínimo: ";
  cin>>min;
  cout<<"ingrese el máximo: ";
  cin>>max;
  x=min;
  for(int i=0;i<=N;i++){
    
    y=TMath::Exp(-x)*TMath::Sin(x);
    g->SetPoint(i,x,y);
    cout<<x<<"\t"<<y<<endl;
    x=x+((max-min)/(double)N);
  }
  g->SetTitle("Curva");
  //g=new TGraph(N,x,y);
  g->Draw("AC*");
  c-> Update();
  c-> Modified();
 
}
int main(Int_t argc, Char_t** argv){
  curva();
  return 0;
}
int main(Int_t argc, Char_t** argv){
   TApplication theApp("App", &argc, argv);
   curva();
   return 0;
}

Wow, thank you. Process successful. :slight_smile:

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