Integrate TF2 function over each bin

Hello All,
I have a TF2 function.I need to integrate it over each bin.I tried to use TH2F->Fit
option “I” but that didn’t work.I mean the histogram is not pop-up.

Is there a way to do it manually?

Here is my few lines of the code(this is not a running code)

``````
Double_t Likelihood(Double_t *val,Double_t *par){
double kBeam=10.6;
const double MpSq=0.9383*0.9383;
Float_t x = val[0];
Float_t y = val[1];
Double_t result=0;
Double_t func=0;
if ((y<QSqMin)||(y>QSqMax)) return result;
if ((x<xBjMin(y))||(x>xBjMax(y))) return result;
if (x>0.0) {
func= par[4]*TMath::Power(x,par[0]) * TMath::Power((1-x),par[1])*TMath::Power((1+ (y*y)/ par[3]),-par[2]);
Double_t y_val   = y/(x*2.0*kBeam*sqrt(MpSq));
func*= (1-y_val+y_val*y_val/2.)/(x*y*y);
result = func;
}
return result;

}

void Fitting(){

const double QSqBinLow[nQ2+1]={1.5,2.5,4.0 ,6.0 ,10.0};
const int nQ2Log =16;
double Q2BinLowLog[nQ2Log+1];
double fQ2 = pow(QSqBinLow[nQ2]/QSqBinLow[0],1./((double)nQ2Log));
double Q2val = QSqBinLow[0];
for (int iQ2=0; iQ2<=nQ2Log; iQ2++)
{
Q2BinLowLog[iQ2]=Q2val;
Q2val *= fQ2;
}

TH2F* h_QSq_xBj_log_bin_data=new TH2F("h_QSq_xBj_log_bin_data", "QSq vs xBj log binning cross section", 50,0.0,1.0,nQ2Log,Q2BinLowLog);
h_QSq_xBj_log_bin_data->Sumw2();

//line 146
TF2 *f2 = new TF2("f2",Likelihood,0.0,1.0,0.0,10.0,5);

//line 231-235
f2->SetParameter(0,-0.5);
f2->SetParameter(1,2.0);
f2->SetParameter(2,0.0);
f2->FixParameter(3,1.0);
f2->SetParameter(4,7e6);

//multiply by acceptance factor
TH2F *h2_Q2_vs_xB_efficiency_factor_acceptance_factor = (TH2F*)h_QSq_xBj_log_bin_data->Clone("h2_Q2_vs_xB_efficiency_factor_acceptance_factor");
TH2F *h_QSq_xBj_log_bin_data_Stat_Sys_err=new TH2F("h_QSq_xBj_log_bin_data_Stat_Sys_err", "Stat+Sys SetBinContent Relative error", 50,0.0,1.0,nQ2Log,Q2BinLowLog);
h2_Q2_vs_xB_efficiency_factor_acceptance_factor->Multiply(h_ratio); //h_ratio histogram is from SIDIS exclusive Simulation
//divide by Iintegrated Luminosity
TH2F *h2_Q2_vs_xB_efficiency_factor_acceptance_factor_Inte_Lumi = (TH2F*)h2_Q2_vs_xB_efficiency_factor_acceptance_factor->Clone("h2_Q2_vs_xB_efficiency_factor_acceptance_factor_Inte_Lumi");
h2_Q2_vs_xB_efficiency_factor_acceptance_factor_Inte_Lumi->Scale(1/integrated_luminosity);
h2_Q2_vs_xB_efficiency_factor_acceptance_factor_Inte_Lumi->Draw("colz");

//line 376-380
h2_Q2_vs_xB_efficiency_factor_acceptance_factor_Inte_Lumi->Draw("LEGO");
f2->SetLineColor(1);
f2->DrawClone("lego same");
f2->SetLineColor(2);
h2_Q2_vs_xB_efficiency_factor_acceptance_factor_Inte_Lumi->Fit("f2","I",""); //This option "I" doesn't work.

}
``````

Thanks
Dil

Hi @Dil,

I am sure either @moneta or @jonas can help you with this.

Cheers,
J.

Hi, thanks for reporting! It is hard to say why the histogram doesn’t pop up without a full running reproducer.

Are you sure it is related to the “I” option? Does the histogram appear if you don’t use that option? Also, which ROOT version are you using?

Hello,

Yes. I used “I” option and histogram doesn’t pop-up.But without an “I” option, It does work.
One reason could be it is a clone histogram??(not sure)

I used root versions 6.12 and 6.20.

Is there a way to do this in manually instead of using “I” option?

Thanks
Dil

Are you trying to replace the contents of the histogram with the function’s integrals?

Hi,
We need a full running script to understand why the histogram is not displayed after fitting.
In principle, if you are not using the `0` or `N` options, after your fit the histogram is drawn in the Canvas together with the fitted function.

Lorenzo

Hello monte,

Yes I can post it.
But I am having problem with upload .root files.They too big.(25MB)
Fitting.cpp (10.3 KB)

Hello,
Any idea how to do the integral over the bin by hand?
Thanks
Dil

Hello,
Actually after around 10min waiting my option"I" works.(It was too slow)
But my results are not good.
Anything that I can do with this?

``````Error in <AdaptiveIntegratorMultiDim::DoIntegral()>: Logic error: idvax0 < 1!
Error in <AdaptiveIntegratorMultiDim::DoIntegral()>: Logic error: idvax0 < 1!
Error in <AdaptiveIntegratorMultiDim::DoIntegral()>: Logic error: idvax0 < 1!
Error in <AdaptiveIntegratorMultiDim::DoIntegral()>: Logic error: idvax0 < 1!
Warning in <Fit>: Abnormal termination of minimization.
FCN=85553 FROM MIGRAD    STATUS=FAILED        199 CALLS         200 TOTAL
EDM=6.93196e+18    STRATEGY= 1      ERR MATRIX NOT POS-DEF
EXT PARAMETER                APPROXIMATE        STEP         FIRST
NO.   NAME      VALUE            ERROR          SIZE      DERIVATIVE
1  p0           3.52224e-01   9.52433e+03   0.00000e+00   5.08159e+03
2  p1           1.79721e+00   1.52492e+04  -0.00000e+00   2.80315e+04
3  p2          -6.19513e-01   1.23385e+04  -0.00000e+00   8.81676e+04
4  p3           1.00000e+00     fixed
5  p4           9.82184e+05   9.99977e-01   0.00000e+00  -3.49486e-02

``````

Note that TH1::Fit tries to find (new) parameters of your function that best “match” / “reproduce” your histogram.
It will never modify the histogram’s contents.
The `"I"` option means that the minimizer uses the integral of the function in the bin (normalized by the bin volume) instead of the function’s value at the bin center (the default behavior).