Problem using .x command

Hello,

Hope I’m in the right place.
I’m using version 5.34.34 on my mac and
according to the user guide, .x should load and execute the script, but I always get the error message that the script is not in the macro.

My question is, say I have a script(file.C) on my Desktop directory, how do I execute it in my root session step by step ? How do a save a script in the macro ?

Thanks a lot
Annie

Hi,

how does your script look like? Is the “principal” function in it called “file” ?

Cheers,
D

#include "TCanvas.h"
#include "TH1.h"
#include "TH1F.h"

void getrandom() {
	
	TH1F *source = new TH1F("source", "source hist", 100,-3,3);

	for (Int_t i=0; i<10000;i++) {
	final-> Fill(source->GetRandom());

	}
    TCanvas *c1 = new TCanvas("c1", "c1", 800, 1000);
    c1->Divide(1,2);
    c1->cd(1);
    source-> Draw();
    c1-> cd(2);
    final->Draw();
    c1->cd();
}

Here is what I’m trying to execute, the script is called getrandom.C, it’s what I copy and paste from the user’s guide, not sure what’s wrong ?

I guess you get this:

root [0] .x getrandom.C
In file included from input_line_12:1:
/Users/couet/roottest/getrandom.C:10:4: error: use of undeclared identifier 'final'
   final-> Fill(source->GetRandom());
   ^
/Users/couet/roottest/getrandom.C:18:5: error: use of undeclared identifier 'final'
    final->Draw();
    ^

it is because you have not declared the histogram “final”.

Hi Annie,

…and to run a script in your Desktop directory on MacOS just specify the full path:
.x /Users/YourUserID/Desktop/getrandom.C

You can see with “gSystem->pwd()” which directory you are currently at; you can change that using e.g. gSystem->cd("/Users/YourUserID/Desktop")

Cheers, Axel.

Thank you Axel and Olivier
Olivier, I believe that’s another problem.

just copy/paste here the errors you get and we will help you further…

Hi Olivier,

Oh, actually I missed this two lines of code…for some reason…

source-> FillRandom(“gaus”,1000);
TH1F *final = new TH1F (“final”, “final hist”,100,-3,3);

And it worked ! thank you, your guys are super helpful !

Best,
Annie

Ah yes those will help :slight_smile: