Error creating output file

Hello everyone,

I am new using ROOT and we are trying to create a histogram but the output file I want to create (“gaus.root”) is not being created and I can’t figure out why. Help me please.

Here it is the core we are using:

#include<TH1.h>
#include<TFile.h>
#include<TRandom.h>
int main(){
TH1F *h = new TH1F(“hgaus”, “A Gauss Function”, 100,-5.0,5.0);
TRandom rnd;
for (int i=0; i<10000; ++i){
double x= rnd.Gaus(1.5,1.0);
h->Fill(x);
}
TFile *myfile= new TFile(“gaus.root”,“new”);
myfile->Write();
myfile->Close();
return 0;
}

Regards.

Please read tips for efficient and successful posting and posting code

ROOT Version: 6.18/04
Platform: Ubuntu 18.04 o 16.04
Compiler: C++


TFile *myfile = TFile::Open("gaus.root", "recreate");
h->Write();
delete myfile;

Hi!
Thank you for your quick answer.
We changed the code as you suggested to us, but I still does not create the gaus.root file. Is there any other thing we could do to fix it? any package that we could try? thanks in advance. Here it is the updated code:

#include<TH1.h>
#include<TFile.h>
#include<TRandom.h>
int main(){
TH1F *h = new TH1F(“hgaus”, “A Gauss Function”, 100,-5.0,5.0);
TRandom rnd;
for (int i=0; i<10000; ++i){
double x= rnd.Gaus(1.5,1.0);
h->Fill(x);
}
TFile *myfile = TFile::Open(“gaus.root”, “new”);
h->Write();
delete myfile;
return 0;
}

How do you use your macro with ROOT and what error do you get?

This is how I run it in the terminal and the not found error that appears. Is it possible that the error is due to we are not specifying in the script where we want to save the file?

jenn@jenn:~ source /home/jenn/mybuild/bin/thisroot.sh jenn@jenn:~ root

| Welcome to ROOT 6.18/04 https://root.cern |
| © 1995-2019, The ROOT Team |
| Built for linuxx8664gcc on Nov 08 2019, 19:52:00 |
| From tag , 11 September 2019 |

Try ‘.help’, ‘.demo’, ‘.license’, ‘.credits’, ‘.quit’/’.q’

root [0] .L /home/jenn/gausexample_2.C
root [1] .q
jenn@jenn:~$ root -l gaus.root
Warning in TApplication::GetOptions: file gaus.root not found
root [0]

Well, “.L” only loads your macro (it does not execute functions inside).
Search for all examples of “.L” usage in the “ROOT Primer”.

Oh I see, then, which steps should I follow to compile the script successfully?

ROOT Primer
ROOT User’s Guide