How to run a macro?

I paste an instance from the primer and form a .C file, then I run it on terminal. But nothing happened?!
There is nothing in the canvas. I swear the code is right.

#include “TCanvas.h”
#include “TROOT.h”
#include “TGraphErrors.h”

void macro1(){
const int n_points=10;
double x_vals[n_points]={1,2,3,4,5,6,7,8,9,10};
double y_vals[n_points]={6,12,14,20,22,24,35,45,44,53};
double y_errs[n_points]={5,5,4.7,4.5,4.2,5.1,2.9,4.1,4.8,5.43};

TGraphErrors graph(n_points,x_vals,y_vals,nullptr,y_errs);
graph.SetTitle(“Measurement XYZ;lenght [cm];Arb.Units”);
graph.SetMarkerStyle(kOpenCircle);
graph.SetMarkerColor(kBlue);
graph.SetLineColor(kBlue);

auto mycanvas = new TCanvas();
graph.DrawClone(“APE”);

mycanvas->Print(“graph_with_law.pdf”);
}
int main(){
macro1();
}

Hi,

How exactly do you run it?

I can’t reproduce the issue, everything works fine for me. Try running it in a new ROOT session. Then copy and paste here everything you see in the terminal. Beginning right from the moment you start root anew.

1 Like

Fine,thanks.

Hi @mainer ,
and welcome to the ROOT forum.

Let us know if you are still having issues, and here are some possibly related topics:

void macro1(){
   const int n_points=10;
   double x_vals[n_points]={1,2,3,4,5,6,7,8,9,10};
   double y_vals[n_points]={6,12,14,20,22,24,35,45,44,53};
   double y_errs[n_points]={5,5,4.7,4.5,4.2,5.1,2.9,4.1,4.8,5.43};

   auto graph = new TGraphErrors(n_points,x_vals,y_vals,nullptr,y_errs);
   graph->SetTitle("Measurement XYZ;lenght [cm];Arb.Units");
   graph->SetMarkerStyle(kOpenCircle);
   graph->SetMarkerColor(kBlue);
   graph->SetLineColor(kBlue);

   auto mycanvas = new TCanvas();
   graph->Draw("APE");

   mycanvas->Print("graph_with_law.pdf");
}

runs with:

% root macro1.C
   ------------------------------------------------------------------
  | Welcome to ROOT 6.23/01                        https://root.cern |
  | (c) 1995-2020, The ROOT Team; conception: R. Brun, F. Rademakers |
  | Built for macosx64 on Jan 27 2021, 09:29:51                      |
  | From heads/master@v6-23-01-RF-binSampling-620-g4dc6647045        |
  | With Apple clang version 12.0.0 (clang-1200.0.32.28)             |
  | Try '.help', '.demo', '.license', '.credits', '.quit'/'.q'       |
   ------------------------------------------------------------------

root [0] 
Processing macro1.C...
Info in <TCanvas::Print>: pdf file graph_with_law.pdf has been created
root [1] .q

gives:

graph_with_law.pdf (13.7 KB)

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