Store and draw TF1

Dear ROOT experts,

I would like to store several fitted TF1 in a vector and draw them later.
This is a macro for testing drawFit.C (1.1 KB).

Code (click to view)
#include <TH1.h>
#include <TF1.h>
#include <TROOT.h>

std::vector<TF1*> vfunc;
const int N = 4;
TH1F *h[N];

bool reject;
double fline(double *x, double *par)
{
    if (reject && x[0] > 2.5 && x[0] < 3.5) {
      TF1::RejectPoint();
      return 0;
   }
   return par[0] + par[1]*x[0];
}

void fitExclude() {
   TF1 *f1 = new TF1("f1","[0] +[1]*x +gaus(2)",0,5);
   f1->SetParameters(6,-1,5,3,0.2);

   vfunc.clear();

   for (int i = 0; i < N; ++i) {
     reject = true;
     if (i%2) reject = false;
     
     h[i] = new TH1F(Form("h%d",i),"background + signal",100,0,5);
     h[i]->FillRandom("f1",2000+i*100);

     TF1 *fl = new TF1("fl",fline,0,5,2);
     fl->SetParameters(2,-1);
     h[i]->Fit(fl,"QN");

     std::cout << Form("Fit_%d,  f(3) = %.4f",i,fl->Eval(3)) << std::endl;
     vfunc.emplace_back(fl);
   }
 }

void drawFit() {
  fitExclude();

  TCanvas *c = new TCanvas("c");
  c->Divide(2,2);
  for (int i = 0; i < N; ++i) {
    c->cd(i+1);
    h[i]->DrawCopy();
    vfunc[i]->DrawF1(0,5,"SAME");

    std::cout << Form("Draw_%d, f(3) = %.4f",i,vfunc[i]->Eval(3)) << std::endl;
  }
}

I expect to get something like:

However, with the above macro I got:


Here, the “reject” works in the fitting, but how to store and draw the “reject” effect correctly?

Thanks!
Yeung


ROOT Version: 6.28/06
Platform: CentOS 7
Compiler: Not Provided


Hello Yeung,

Thanks for sharing the code. This seems to be a very custom use case.
However, if I understand the code correctly (and correct me if I am wrong, please!), would saving the reject variable value as the 3rd parameter of your custom function do the job?

Cheers,
D

Hi Danilo,
Thanks for your suggestion! I will think about this.
By the way, the test macro is mostly based on this tutorial https://root.cern.ch/doc/master/fitExclude_8C.html

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