Segfaults when accessing parameters of a fit

In my code, corr is initialized as:

TNtuple *corr = new TNtuple("data", "Raw and Corrected rates", "run:station:dom:pressure:ratecorr:rateraw");
Then filled in a loop.
The following code will be looped eventually, but I’m trying to get it work for just one piece of data for now.

TH1 *h1; corr->Draw("ratecorr>>h1", "station==40&&dom==61"); h1->Fit("gaus", "Q0"); TF1 *fit = h1->GetFunction("gaus"); fit->GetParameter(1); fit->GetParameter(2);

That compiles but gives me a segfault, as well as working if I just type it in root (I write the ntuple to a file, then open root and experiment with it). So because root likes to initialize things for me, I’m guessing I’m not initializing something correctly.

The output is:

<TCanvas::MakeDefCanvas>: created default TCanvas with name c1 *** Break *** segmentation violation
with the break occuring at the first get parameter. the TCanvas is made at the Draw() call.

I’m assuming I have to initialize a canvas, but I’m not sure if that’s right and even if it is I’m not sure how to do it.
Any insight?


InTH1 *h1; corr->Draw("ratecorr>>h1", "station==40&&dom==61"); h1->Fit("gaus", "Q0"); h1 is initialized for you by CINT after it has been created by TTree::Draw… Use

corr->Draw("ratecorr>>h1", "station==40&&dom==61"); TH1 *h1; gDirectory->GetObject("h1",h1); h1->Fit("gaus", "Q0");

Ahh I see how it works.
Thank you for the explanation and fix!