Data plots from root interpreter but not from macro

For following simple data file:

# trial data plotdata1
#x   y
1.0 	1.0
3.0 	2.0
3.0 	3.5
5.0 	3.0
5.0 	5.0
7.0  	5.0
7.0 	7.0
8.0 	6.0

I can load and plot it from the interpreter with:

TGraph gr ("plotdata1.txt")
gr.Draw("APL")

However, if I write the following code (macro):

#include "TF1.h"
#include "TApplication.h"
#include "TCanvas.h"
#include "TRootCanvas.h"
#include "TGraph.h"
#include "TLegend.h"
#include "TGFont.h"
#include "TPad.h"

void plotlines() {
	auto c = new TCanvas("c1", "Demo example");
	TString dir = gROOT->GetTutorialDir();
	TGraph gr(dir+"\\plotdata1.txt");
	gr.SetTitle("Demo data");
	gr.SetMarkerStyle(20);
	gr.Draw("APL");
	return 0;
}

and run it in the root interpreter by

.L plotlines.C
plotlines()

I get a window with the title bar labelled “Demo example” but no plot.
Both the plotlines.C and the plotdata1.txt files are in the same directory: the tutorials directory, and root was launched from that directory.

_ROOT Version: 6.26.10
_Platform: Windows 10
_Compiler: root interpreter


#include "TF1.h"
#include "TApplication.h"
#include "TCanvas.h"
#include "TRootCanvas.h"
#include "TGraph.h"
#include "TLegend.h"
#include "TGFont.h"
#include "TPad.h"

void plotlines() {
	auto c = new TCanvas("c1", "Demo example");
	TString dir = gROOT->GetTutorialDir();
	auto gr = new TGraph(dir+"\\plotdata1.txt");
	gr->SetTitle("Demo data");
	gr->SetMarkerStyle(20);
	gr->Draw("APL");
}

I had already tried that. That isn’t the problem.
The files are in C:\root_v6.26.10\tutorials and even using that absolute path, the data are not shown. However, using the absolute path works fine in compiled C++ code in Visual Studio.

Try a debug output:
std::cout << dir << " -> " << dir+"\\plotdata1.txt" << "\n";

Since even an absolute path fails:
auto gr = new TGraph(“c:\temp\plotdata1.txt”);

my mistake must be somewhere else.

Try: auto gr = new TGraph("c:\\temp\\plotdata1.txt");

That fails, too. This is really odd.

So for me, both

   auto gr = new TGraph("C:\\temp\\plotdata1.txt");

and

   auto gr = new TGraph("C:/temp/plotdata1.txt");

work with ROOT master and ROOT v6.26.10

I wish I could see my mistake, though. Thanks for that info.

Can you attach your macro and text file? (and not copy & paste here)

I got it working. I exited the root interpreter and entered it again with
root > .x plotlines.c
and that worked.
Now everything works even if I exit and re-enter directly. It must be some weird initialization problem. Anyway, it works every way I try now. Thank you for your suggestions.

1 Like