Having trouble just making a plot

Hi there,
I am inexperienced at ROOT, but would like to use its libraries in my C++ programs. I am using a g++ compiler on a Linux platform. With the following piece of stand-alone code (below and attached, partially lifted from ROOT examples), I am just trying to plot a histogram. It compiles perfectly but produces a segmentation fault when I run it. Does anyone see what is wrong with it?
Thanks,
Penny

int hsimple();

#ifndef CINT
#include “TH1.h”
#include “TRandom.h”
#include “TApplication.h”
#include “TGraph.h”
#include “TCanvas.h”
#endif

//______________________________________________________________________________

int main()
{
return hsimple();
}

int hsimple()
{

Int_t argc; char** argv=0;
TApplication theApp(“App”, &argc, argv);
TH1F *hpx = new TH1F(“hpx”,“This is the px distribution”,100,-4,4);

// Fill histogram randomly
Float_t px, py, pz;
for ( Int_t i=0; i<10000; i++) {
gRandom->Rannor(px,py); //px and py will be two gaussian random numbers
pz = pxpx + pypy;
hpx->Fill(px);
}

TGraph *gr001 = new TGraph(hpx);
gr001->Draw();

theApp.Run();

return 0;
}
hsimple.cxx (728 Bytes)

You have several problems in your simple test:
-your handling of argc, argv is wrong. see corrected version in hs.cxx
-you do not specify a draw option for the graph
-you do not need to create a TGraph to draw a histogram, you can call directly
hpx->Draw();
-It is a very bad idea (in particular if you are a beginner) to create your own main program.
You should execute your script directly from standard root.exe (see hs2.C)

//file hs.cxx

[code]#ifndef CINT
#include “TH1.h”
#include “TRandom.h”
#include “TApplication.h”
#include “TGraph.h”
#include “TCanvas.h”
#endif

//______________________________________________________________________________

int main(Int_t argc, char** argv=0)
{
TApplication theApp(“App”, &argc, argv);
TH1F *hpx = new TH1F(“hpx”,“This is the px distribution”,100,-4,4);

// Fill histogram randomly
Float_t px, py, pz;
for ( Int_t i=0; i<10000; i++) {
gRandom->Rannor(px,py); //px and py will be two gaussian random numbers
pz = pxpx + pypy;
hpx->Fill(px);
}

TGraph *gr001 = new TGraph(hpx);
gr001->Draw(“al”);
//it is better to call directly hpx->Draw instead of teh 2 lines above

theApp.Run();

return 0;
}
[/code]

file hs2.C

[code]int hs2()
{

TH1F *hpx = new TH1F(“hpx”,“This is the px distribution”,100,-4,4);

// Fill histogram randomly
Float_t px, py, pz;
for ( Int_t i=0; i<10000; i++) {
gRandom->Rannor(px,py); //px and py will be two gaussian random numbers
pz = pxpx + pypy;
hpx->Fill(px);
}

TGraph *gr001 = new TGraph(hpx);
gr001->Draw(“al”);

return 0;
}
[/code]

in this case, you do

root > .x hs2.C
Rene

Hi Rene,
Thank you very much for the corrections! I will give those a try. I also appreciate your advice about sticking with ROOT scripts - it might be the easiest route for now. However I would prefer to keep working with a “mainstream” compiler to make sure I understand and control every detail of my code. Thanks again!
Best regards,
Penny

With what I am proposing you can use what you call a mainstream compiler.

root > .x myscript.C is executed by the CINT interpreter root > .x myscript.C+ is executed by the native compiler (via ACLIC)
When using the mode myscript.C+ root compiles on the fly with the native compiler the file myscript.C. In this case myscript.C must have all the necessary includes at the top.
For more details, read the very first few pages of the Users Guide.

Rene

Thank you. I will certainly be rereading many parts of the User’s Guide. In the meantime I want to be able to compile my programs without going to the interactive ROOT prompt. If there is some way to compile “scripts” without starting up interactive ROOT, I would be interested in doing that. Apologies if I am missing something obvious.

I think I have PAW and IDL fatigue, and just don’t want to work through an interactive platform any more.

Thank you again for your comments.

[quote]In the meantime I want to be able to compile my programs without going to the interactive ROOT prompt. If there is some way to compile “scripts” without starting up interactive ROOT, I would be interested in doing that. Apologies if I am missing something obvious. [/quote]It is indeed possible to do … and is used extensively in the [[root.cern.ch/viewcvs/trunk/?root=roottest][root test suite]].

You can either use a helper [[root.cern.ch/viewcvs/trunk/scripts/build.C?revision=1782&root=roottest&view=markup]script like the one in roottest] or simply try:echo 'gROOT->ProcessLine("myscript.cxx+");' | root.exe -b -q -lorecho 'gSystem->CompileMacro("myscript.cxx","kc");' | root.exe -b -q -l

Cheers,
Philippe.

Thank you Philippe. I will keep this in mind.
Best regards,
Penny