NAGASH 0.9.8
Next Generation Analysis System
Loading...
Searching...
No Matches
Classes | Public Member Functions | Private Member Functions | Private Attributes | List of all members
NAGASH::TemplateFitter Class Reference

Fit the contributions use template histograms to a target histogram. More...

#include "NAGASH/TemplateFitter.h"

Inheritance diagram for NAGASH::TemplateFitter:
NAGASH::Tool

Classes

class  TemplateFitterFCN
 

Public Member Functions

 TemplateFitter (std::shared_ptr< MSGTool > MSG)
 
void Clear ()
 Clear this tool.
 
void Fit ()
 Run the fit.
 
double GetChi2 ()
 Get the fit \(\chi^2\).
 
double GetSF (uint64_t i)
 Get the scale factors for ith template.
 
double GetSFError (uint64_t i)
 Get the uncertainty of the scale factors for ith template.
 
void SetFitRange (int min, int max)
 Set the fit range.
 
void SetRelation (std::function< double(const std::vector< double > &, const std::vector< double > &)>, std::function< double(const std::vector< double > &, const std::vector< double > &, const std::vector< double > &)>)
 Set the relation between the target histogram and the template histograms. if not called, the default relation is: \( t=\sum s_i \cdot h_i\), here \( t\) is the target histogram, \( s_i\) is the scale factor and \( h_i\) is the corresponding template histogram.
 
void SetTarget (TH1D *h)
 Set the target histogram.
 
void SetTemplate (TH1D *h, double SF, double min, double max)
 Set the template histogram.
 
void SetTemplate (TH1D *h, double SF=1, bool fix=false)
 Set the template histogram.
 

Private Member Functions

bool Check ()
 

Private Attributes

double chi2 = 0
 
std::function< double(const std::vector< double > &, const std::vector< double > &)> contentfunction
 
std::function< double(const std::vector< double > &, const std::vector< double > &, const std::vector< double > &)> errorfunction
 
int FitRangeMax = 0
 
int FitRangeMin = 0
 
TH1D * ht = nullptr
 
std::vector< bool > isfixed
 
std::vector< double > sf
 
std::vector< double > sferr
 
std::vector< double > sfmax
 
std::vector< double > sfmin
 
std::vector< TH1D * > vh
 

Additional Inherited Members

- Protected Member Functions inherited from NAGASH::Tool
 Tool ()=delete
 
 Tool (const Tool &tool)=delete
 
 Tool (std::shared_ptr< MSGTool > MSG)
 Constructor.
 
 Tool (Tool &&tool)=delete
 
virtual ~Tool ()=default
 
std::shared_ptr< MSGToolMSGUser ()
 return the MSGTool inside.
 
Tooloperator= (const Tool &tool)=delete
 
Tooloperator= (Tool &&tool)=delete
 

Detailed Description

Fit the contributions use template histograms to a target histogram.

For example, if you know the target distribution is composed of two sub distributions like:

\[ f(x) = \alpha\cdot\frac{\exp\left(-(x-1)^2/2\right)}{\sqrt{2\pi}}+\beta\cdot\frac{2\exp\left(-2(x-2)^2\right)}{{\sqrt{2\pi}}} \]

Of course \(\alpha = 1-\beta\), but in real world we are more interested in the ratio \(\alpha/\beta\). And now you have a histogram filled with distribution \((x)\), then you can get \(\alpha/\beta\) using this tool like:

// Demonstration of using NAGASH::TemplatFitter
#include "NAGASH.h"
using namespace NAGASH;
int main()
{
// prepare for target histogram and sub histograms
TRandom3 myrnd;
TH1D *htarget = new TH1D("htarget", "htarget", 50, -3, 5);
TH1D *hsub1 = new TH1D("hsub1", "hsub1", 50, -3, 5);
TH1D *hsub2 = new TH1D("hsub2", "hsub2", 50, -3, 5);
for (int i = 0; i < 1e6; i++)
{
hsub1->Fill(myrnd.Gaus(1, 1));
hsub2->Fill(myrnd.Gaus(2, 0.5));
// the contribution is 2:1
htarget->Fill(myrnd.Gaus(1, 1));
if (i % 2 == 0)
htarget->Fill(myrnd.Gaus(2, 0.5));
}
// start fitting
TemplateFitter fitter(std::make_shared<MSGTool>());
fitter.SetTarget(htarget);
fitter.SetTemplate(hsub1);
fitter.SetTemplate(hsub2);
fitter.Fit();
std::cout << "The ratio is : " << fitter.GetSF(0) / fitter.GetSF(1) << std::endl;
std::cout << "Fit chi2 is " << fitter.GetChi2() << std::endl;
}
int main(int argc, char **argv)
Fit the contributions use template histograms to a target histogram.

The output will be like:

The ratio is : 2.01191
Fit chi2 is 39.7431
void Fit()
Run the fit.

Definition at line 24 of file TemplateFitter.h.

Constructor & Destructor Documentation

◆ TemplateFitter()

NAGASH::TemplateFitter::TemplateFitter ( std::shared_ptr< MSGTool MSG)
inline

Definition at line 27 of file TemplateFitter.h.

Member Function Documentation

◆ Check()

bool TemplateFitter::Check ( )
private

Definition at line 230 of file TemplateFitter.cxx.

◆ Clear()

void TemplateFitter::Clear ( )

Clear this tool.

Definition at line 122 of file TemplateFitter.cxx.

◆ Fit()

void TemplateFitter::Fit ( )

Run the fit.

Definition at line 258 of file TemplateFitter.cxx.

◆ GetChi2()

double TemplateFitter::GetChi2 ( )

Get the fit \(\chi^2\).

Definition at line 339 of file TemplateFitter.cxx.

◆ GetSF()

double TemplateFitter::GetSF ( uint64_t  i)

Get the scale factors for ith template.

Parameters
itemplate index

Definition at line 306 of file TemplateFitter.cxx.

◆ GetSFError()

double TemplateFitter::GetSFError ( uint64_t  i)

Get the uncertainty of the scale factors for ith template.

Parameters
itemplate index

Definition at line 323 of file TemplateFitter.cxx.

◆ SetFitRange()

void TemplateFitter::SetFitRange ( int  min,
int  max 
)

Set the fit range.

Parameters
minminimum bin index.
maxmaximum bin index.

Definition at line 215 of file TemplateFitter.cxx.

◆ SetRelation()

void TemplateFitter::SetRelation ( std::function< double(const std::vector< double > &, const std::vector< double > &)>  cf,
std::function< double(const std::vector< double > &, const std::vector< double > &, const std::vector< double > &)>  ef 
)

Set the relation between the target histogram and the template histograms. if not called, the default relation is: \( t=\sum s_i \cdot h_i\), here \( t\) is the target histogram, \( s_i\) is the scale factor and \( h_i\) is the corresponding template histogram.

Parameters
cfthe function to calculate the content, first argument is the vector of the scale factors, second argument is the vector of the contents of template histograms.
efthe function to calculate the error, first argument is the vector of the scale factors, second argument is the vector of the contents of template histograms, third argument is the vector of the errors of template histograms.

Definition at line 205 of file TemplateFitter.cxx.

◆ SetTarget()

void TemplateFitter::SetTarget ( TH1D *  h)

Set the target histogram.

Definition at line 133 of file TemplateFitter.cxx.

◆ SetTemplate() [1/2]

void TemplateFitter::SetTemplate ( TH1D *  h,
double  SF,
double  SFMIN,
double  SFMAX 
)

Set the template histogram.

Parameters
htemplate histogram.
SFinitial value of the scale factor.
SFMINminimum value of the scale factor during the fit.
SFMAXmaximum value of the scale factor during the fit.

Definition at line 175 of file TemplateFitter.cxx.

◆ SetTemplate() [2/2]

void TemplateFitter::SetTemplate ( TH1D *  h,
double  SF = 1,
bool  fix = false 
)

Set the template histogram.

Parameters
htemplate histogram.
SFinitial value of the scale factor.
fixwhether to fix the scale factor during the fit.

Definition at line 152 of file TemplateFitter.cxx.

Member Data Documentation

◆ chi2

double NAGASH::TemplateFitter::chi2 = 0
private

Definition at line 67 of file TemplateFitter.h.

◆ contentfunction

std::function<double(const std::vector<double> &, const std::vector<double> &)> NAGASH::TemplateFitter::contentfunction
private

Definition at line 71 of file TemplateFitter.h.

◆ errorfunction

std::function<double(const std::vector<double> &, const std::vector<double> &, const std::vector<double> &)> NAGASH::TemplateFitter::errorfunction
private

Definition at line 72 of file TemplateFitter.h.

◆ FitRangeMax

int NAGASH::TemplateFitter::FitRangeMax = 0
private

Definition at line 69 of file TemplateFitter.h.

◆ FitRangeMin

int NAGASH::TemplateFitter::FitRangeMin = 0
private

Definition at line 68 of file TemplateFitter.h.

◆ ht

TH1D* NAGASH::TemplateFitter::ht = nullptr
private

Definition at line 74 of file TemplateFitter.h.

◆ isfixed

std::vector<bool> NAGASH::TemplateFitter::isfixed
private

Definition at line 66 of file TemplateFitter.h.

◆ sf

std::vector<double> NAGASH::TemplateFitter::sf
private

Definition at line 62 of file TemplateFitter.h.

◆ sferr

std::vector<double> NAGASH::TemplateFitter::sferr
private

Definition at line 65 of file TemplateFitter.h.

◆ sfmax

std::vector<double> NAGASH::TemplateFitter::sfmax
private

Definition at line 64 of file TemplateFitter.h.

◆ sfmin

std::vector<double> NAGASH::TemplateFitter::sfmin
private

Definition at line 63 of file TemplateFitter.h.

◆ vh

std::vector<TH1D *> NAGASH::TemplateFitter::vh
private

Definition at line 75 of file TemplateFitter.h.


The documentation for this class was generated from the following files: