# Data plots from root interpreter but not from macro

**URL:** <https://root-forum.cern.ch/t/data-plots-from-root-interpreter-but-not-from-macro/53820>\
**Category:** Newbie\
**Created:** [February 28, 2023, 4:52pm UTC](https://root-forum.cern.ch/t/data-plots-from-root-interpreter-but-not-from-macro/53820 "2023-02-28T16:52:39Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![jcooper](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/258eb7/32.png) [@jcooper](https://root-forum.cern.ch/u/jcooper)\
**Post date:** [February 28, 2023, 4:52pm UTC](https://root-forum.cern.ch/t/data-plots-from-root-interpreter-but-not-from-macro/53820/1 "2023-02-28T16:52:39Z")

</div>

For following simple data file:

```auto
# 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:

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

```

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

```auto
#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

```auto
.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

* * *

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [February 28, 2023, 4:58pm UTC](https://root-forum.cern.ch/t/data-plots-from-root-interpreter-but-not-from-macro/53820/2 "2023-02-28T16:58:00Z")

</div>

```auto
#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");
}

```

---

<div class="post-metadata">

**Author:** ![jcooper](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/258eb7/32.png) [@jcooper](https://root-forum.cern.ch/u/jcooper)\
**Post date:** [February 28, 2023, 5:53pm UTC](https://root-forum.cern.ch/t/data-plots-from-root-interpreter-but-not-from-macro/53820/4 "2023-02-28T17:53:24Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [February 28, 2023, 6:32pm UTC](https://root-forum.cern.ch/t/data-plots-from-root-interpreter-but-not-from-macro/53820/5 "2023-02-28T18:32:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jcooper](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/258eb7/32.png) [@jcooper](https://root-forum.cern.ch/u/jcooper)\
**Post date:** [February 28, 2023, 6:43pm UTC](https://root-forum.cern.ch/t/data-plots-from-root-interpreter-but-not-from-macro/53820/6 "2023-02-28T18:43:54Z")

</div>

> [@Wile\_E\_Coyote](#):
>
> 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.

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [February 28, 2023, 6:49pm UTC](https://root-forum.cern.ch/t/data-plots-from-root-interpreter-but-not-from-macro/53820/7 "2023-02-28T18:49:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jcooper](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/258eb7/32.png) [@jcooper](https://root-forum.cern.ch/u/jcooper)\
**Post date:** [February 28, 2023, 8:49pm UTC](https://root-forum.cern.ch/t/data-plots-from-root-interpreter-but-not-from-macro/53820/8 "2023-02-28T20:49:46Z")

</div>

That fails, too. This is really odd.

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [February 28, 2023, 9:40pm UTC](https://root-forum.cern.ch/t/data-plots-from-root-interpreter-but-not-from-macro/53820/9 "2023-02-28T21:40:31Z")

</div>

So for me, both

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

```

and

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

```

work with ROOT master and ROOT v6.26.10

---

<div class="post-metadata">

**Author:** ![jcooper](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/258eb7/32.png) [@jcooper](https://root-forum.cern.ch/u/jcooper)\
**Post date:** [February 28, 2023, 9:44pm UTC](https://root-forum.cern.ch/t/data-plots-from-root-interpreter-but-not-from-macro/53820/10 "2023-02-28T21:44:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [February 28, 2023, 9:45pm UTC](https://root-forum.cern.ch/t/data-plots-from-root-interpreter-but-not-from-macro/53820/11 "2023-02-28T21:45:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jcooper](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/258eb7/32.png) [@jcooper](https://root-forum.cern.ch/u/jcooper)\
**Post date:** [February 28, 2023, 9:51pm UTC](https://root-forum.cern.ch/t/data-plots-from-root-interpreter-but-not-from-macro/53820/12 "2023-02-28T21:51:45Z")

</div>

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.
