Accessing parameters in FitSlicesY

Hi,
I have tried to follow the example given in the tutorial to fit slices in Y for a TH2F, using the following function:

void Plotting(TH2F* h2, char *filename=“graphs/graph.eps”)
{
TCanvas *c1=new TCanvas("","", 200, 10, 800,800);
c1->Divide(1,2);
c1->cd(1);
h2->Draw();
h2->FitSlicesY();
c1->cd(2);
h2_0->Draw();
c1->SaveAs(filename);
}

I want to plot the mean of each fit using h2_0 but the error I get is that the histogram h2_0 is undeclared. I thought from the example that h2_0 gets automatically declared when you do fitslices. Any ideas?
Ellie

To get a pointer to one of the generated histograms by TH2::FitSlicesY, do, eg
TH1D h2_1 = (TH1D)gDirectory->Get(“h2_1”);

This is indicated in the documentation of the function.

Rene

Hi Rene,
Thanks for your speedy reply :slight_smile:
I have tried as you said but now I unfortunately get a seg fault. My code now reads:

TH1D* h2_1; (declaration in .h file)
h2_1 = new TH1D("", “”,50,-50,50); (constructor in .C file)

TCanvas c1=new TCanvas("","", 200, 10, 800,800);
c1->Divide(1,2);
c1->cd(1);
Miss_had_perp->Draw();
Miss_had_perp->FitSlicesY();
c1->cd(2);
h2_1=(TH1D
)gDirectory->Get(“h2_1”);
h2_1->Draw();

When I run gdb I see the seg fault is in the last line. Any idea why this is?
Cheers
Ellie

The name of the generated histograms are “name of the 2d histogram_0”,etc. If your histogram pointed by Miss_had_perp has the same name, ie “Miss_had_perp”, I suggest changing your code like below

TH1D* h2_1; (declaration in .h file) TCanvas *c1=new TCanvas("","", 200, 10, 800,800); c1->Divide(1,2); c1->cd(1); Miss_had_perp->Draw(); Miss_had_perp->FitSlicesY(); c1->cd(2); h2_1=(TH1D*)gDirectory->Get("Miss_had_perp_1"); h2_1->Draw();

Rene

oops! failed to notice that when I changed the example from a function to the main code.
Thanks for your help
Ellie

Hi Rene,
I have cut and pasted what you suggested but I still am getting a seg fault :frowning:
My code now reads:

TH2F* Miss_had_perp;
TH1F* h2_1; (declarations in h file)

TCanvas c1=new TCanvas("","", 200, 10, 800,800);
c1->Divide(1,2);
c1->cd(1);
Miss_had_perp->Draw();
Miss_had_perp->FitSlicesY();
c1->cd(2);
h2_1 = new TH1F(“test”, “test”,50,-50,50);
h2_1=(TH1F
)gDirectory->Get(“Miss_had_perp_1”);
h2_1->Draw();

and the code is crashing on the last line. Any ideas?
Ellie

[quote]and the code is crashing on the last line. Any ideas? [/quote]Is Miss_had_perp_1 a TH1F or TH2F … it is probably a TH2F and you should cast it as such (instead of TH1F).

Philippe

Philippe,

No, read the mail, it is a TH2F and the projection is a TH1.
What probably happens is that teh name of the 2-D histogram is not
what the user thinks , ie “Miss_had_perp”, and as a result, teh result
of h2_1=(TH1F*)gDirectory->Get(“Miss_had_perp_1”); is h2_1=0.

Rene