Tutorial for normalized sum of two functions Here: a background exponential and a crystalball function Parameters can be set: 
- with the TF1 object before adding the function (for 3) and 4))
 
- with the TF1NormSum object (first two are the coefficients, then the non constant parameters)
 
- with the TF1 object after adding the function
 
Sum can be constructed by:
- by a string containing the names of the functions and/or the coefficient in front
 
- by a string containg formulas like expo, gaus...
 
- by the list of functions and coefficients (which are 1 by default)
 
- by a std::vector for functions and coefficients
 
  
Time to generate 1050000 events:  Real time 0:00:00, CP time 0.100
****************************************
Minimizer is Minuit2 / Migrad
Chi2                      =      1018.73
NDf                       =          993
Edm                       =  9.65559e-06
NCalls                    =          233
NSignal                   =        50082   +/-   1231.21     
NBackground               =       998899   +/-   1569.86     
Mean                      =      2.99896   +/-   0.0022426   
Sigma                     =     0.297871   +/-   0.00230279  
Alpha                     =      2.12493   +/-   0.1368      
N                         =       1.1562   +/-   0.468136    
Slope                     =    -0.300341   +/-   0.000644187 
Time to fit using ROOT TF1Normsum: Real time 0:00:00, CP time 0.100
   
 
void fitNormSum()
{
 
   TF1 *
f_cb = 
new TF1(
"MyCrystalBall", 
"crystalball", -5., 5.);
 
   TF1 *
f_exp = 
new TF1(
"MyExponential", 
"expo", -5., 5.);
 
 
   
   f_exp->SetParameters(1., -0.3);
 
 
   
   
   
 
 
   
   f_sum->SetParName(1, 
"NBackground");
 
   f_sum->SetParName(0, 
"NSignal");
 
   for (
int i = 2; i < 
f_sum->GetNpar(); ++i)
 
 
   
   TH1D *
h_sum = 
new TH1D(
"h_ExpCB", 
"Exponential Bkg + CrystalBall function", nBins, -5., 5.);
 
   h_sum->FillRandom(
"fsum", nEvents);
 
   printf(
"Time to generate %d events:  ", nEvents);
 
 
   
   h_sum->Scale(1., 
"width");
 
 
   
   new TCanvas(
"Fit", 
"Fit", 800, 1000);
 
   
   printf(
"Time to fit using ROOT TF1Normsum: ");
 
 
   
   for (
unsigned int i = 0; i < 
pref.size(); ++i) {
 
         Error(
"testFitNormSum", 
"Difference found in fitted %s - difference is %g sigma", 
f_sum->GetParName(i),
 
   }
 
   
}
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
 
void Error(const char *location, const char *msgfmt,...)
Use this function in case an error occurred.
 
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t result
 
R__EXTERN TStyle * gStyle
 
static void SetDefaultMinimizer(const char *type, const char *algo=nullptr)
Set the default Minimizer type and corresponding algorithms.
 
Class adding two functions: c1*f1+c2*f2.
 
1-D histogram with a double per channel (see TH1 documentation)
 
To draw Mathematical Formula.
 
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString.
 
void SetOptStat(Int_t stat=1)
The type of information printed in the histogram statistics box can be selected via the parameter mod...
 
Bool_t AreEqualAbs(Double_t af, Double_t bf, Double_t epsilon)
Comparing floating points.
 
- Author
 - Lorenzo Moneta 
 
Definition in file fitNormSum.C.