Get TFitResult for each slice from TH2::FitSlicesY()

Consider this example:

void fitslicesytest() {
   TString dir = gROOT->GetTutorialDir();
   if (!gInterpreter->IsLoaded(dir.Data())) gInterpreter->LoadMacro(dir.Data());
   TFile *hsimpleFile = (TFile*)gROOT->ProcessLineFast("hsimple(1)");
   if (!hsimpleFile) return;
   TH2F *hpxpy = (TH2F*)hsimpleFile->Get("hpxpy");
   TAxis * Yaxis = hpxpy -> GetYaxis();
   TF1 f1 = TF1("f1", "gaus", Yaxis -> GetXmin(), Yaxis -> GetXmax());
   f1.SetParameters(100, (Yaxis -> GetXmax() - Yaxis -> GetXmin())/2.0, 0.01);
   TObjArray slices;
   hpxpy -> FitSlicesY(& f1, 1, hpxpy -> GetNbinsX(), 0, "QNRLM", &slices);

I would like to get TFitResult and see the fitted function drawn on each slice. What to do?

Please read tips for efficient and successful posting and posting code

ROOT Version: Not Provided
Platform: Not Provided
Compiler: Not Provided


Unfortunately there is now no current way to get the TFitResult object. You would need to modify the TH2::DoFitSlices function.

For plotting the fitted function remove the options Q and N, just use RLM


1 Like

By removing QN I dont’ see any fits drawn anywhere.


   hpxpy -> FitSlicesY(& f1, 1, hpxpy -> GetNbinsX(), 0, "QNRLM", &slices);

equivalent to

   TH1* h = hpxpy -> ProjectionX("proj", 1, 1);
   TFitResultPtr r = h -> Fit(&f1, "SRLMQN");

Hi ,

Not really, because TH2::FitSlicesY will perform in this case 100 fits on each slice.
What is equivalent is the following for the X bin ibin

 hpxpy -> FitSlicesY(& f1, ibin, ibin, 0, "QNRLM", &slices);

is equivalent to:

TH1* h = hpxpy -> ProjectionX("proj", ibin, ibin);
TFitResultPtr r = h -> Fit(&f1, "SRLMQN");type or paste code here

and you can get in the second case the fit result and the fitted plot.


This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.