4 #ifndef __bayesian_estimator_H
5 #define __bayesian_estimator_H
8 #include "impact_parameter_distribution.h"
16 #include <KVValueRange.h>
17 #include <KVNameValueList.h>
43 double operator()(
double X,
double mean,
double reduced_variance)
69 double operator()(
double X,
double mean,
double reduced_variance)
95 double operator()(
double X,
double mean,
double reduced_variance)
220 <
class FittingFunction,
class FluctuationKernel>
223 FittingFunction theFitter;
224 FluctuationKernel theKernel;
228 std::vector<double> sel_rapp;
229 TF1 p_X_cb_integrator;
230 TF1 P_X_fit_function;
231 TF1 mean_X_vs_cb_function;
232 TF1 mean_X_vs_b_function;
233 TF1 p_X_X_integrator;
234 TF1 p_X_X_integrator_with_selection;
236 TF1 Cb_dist_for_X_select;
237 TF1 Cb_dist_for_arb_X_select;
238 TF1 B_dist_for_X_select;
239 TF1 B_dist_for_arb_X_select;
240 TF1 B_dist_for_arb_X_select_from_histo;
246 double mean_X_vs_cb(
double*
x,
double* par)
253 theFitter.fill_params_from_array(par);
254 return theFitter.meanX(
x[0]);
256 double mean_X_vs_b(
double*
x,
double* par)
263 theFitter.fill_params_from_array(par);
266 double P_X_cb(
double X,
double cb)
271 return fIntegerVariable ? theKernel(
TMath::Nint(
X), theFitter.meanX(cb), theFitter.redVar(cb))
272 : theKernel(
X, theFitter.meanX(cb), theFitter.redVar(cb));
274 double P_X_cb_for_TF2_obs_vs_b(
double*
x,
double*)
282 return x[0] * theKernel(
X, theFitter.meanX(cb), theFitter.redVar(cb));
284 double P_X_cb_for_integral(
double*
x,
double* par)
292 return P_X_cb(par[0],
x[0]);
294 double P_X_cb_for_X_integral(
double*
x,
double* par)
303 return P_X_cb(
x[0], par[0]);
305 double P_X_cb_for_X_integral_with_selection(
double*
x,
double* par)
316 if (bin < 1 || bin > histo->
GetNbinsX())
return 0;
317 return sel_rapp[bin - 1] * P_X_cb(
x[0], par[0]);
319 double cb_integrated_P_X(
double*
x,
double*
p)
329 theFitter.fill_params_from_array(
p);
331 return p_X_cb_integrator.
Integral(0, 1, 1.e-4);
333 double P_X_from_fit(
double*
x,
double* par)
340 return par[0] * p_X_cb_integrator.
Integral(0, 1, 1.e-4);
342 double cb_dist_for_X_selection(
double*
x,
double*
p)
355 double num = p_X_X_integrator.
Integral(
p[0],
p[1], 1.e-4);
356 double den = fitted_P_X.
Integral(
p[0],
p[1], 1.e-4);
357 if (den > 0)
return num / den;
360 double cb_dist_for_arb_X_selection(
double*
x,
double*
p)
372 double num = p_X_X_integrator_with_selection.
Integral(
p[0],
p[1], 1.e-4);
377 double b_dist_for_X_selection(
double*
x,
double*
p)
390 double num = p_X_X_integrator.
Integral(
p[0],
p[1], 1.e-4);
393 double b_dist_for_arb_X_selection(
double*
x,
double*
p)
405 double num = p_X_X_integrator_with_selection.
Integral(
p[0],
p[1], 1.e-4);
408 double b_dist_for_arb_X_selection_from_histo(
double*
x,
double*
p)
422 double num = p_X_X_integrator_with_selection.
Integral(
p[0],
p[1], 1.e-4);
430 p_X_cb_integrator(
"p_X_cb_integrator", this, &
bayesian_estimator::P_X_cb_for_integral, 0, 1, 1),
431 P_X_fit_function(
"P_X_fit_function", this, &
bayesian_estimator::cb_integrated_P_X, 0, 1, theFitter.npar()),
432 mean_X_vs_cb_function(
"mean_X_vs_cb", this, &
bayesian_estimator::mean_X_vs_cb, 0, 1, theFitter.npar()),
433 mean_X_vs_b_function(
"mean_X_vs_b", this, &
bayesian_estimator::mean_X_vs_b, 0, 20, theFitter.npar()),
434 p_X_X_integrator(
"p_X_X_integrator", this, &
bayesian_estimator::P_X_cb_for_X_integral, 0, 1000, 1),
435 p_X_X_integrator_with_selection(
"p_X_X_integrator_with_selection", this, &
bayesian_estimator::P_X_cb_for_X_integral_with_selection, 0, 1000, 1),
437 Cb_dist_for_X_select(
"Cb_dist_for_X_select", this, &
bayesian_estimator::cb_dist_for_X_selection, 0, 1, 2),
438 Cb_dist_for_arb_X_select(
"Cb_dist_for_arb_X_select", this, &
bayesian_estimator::cb_dist_for_arb_X_selection, 0, 1, 2),
439 B_dist_for_X_select(
"b_dist_for_X_select", this, &
bayesian_estimator::b_dist_for_X_selection, 0, 20, 2),
440 B_dist_for_arb_X_select(
"b_dist_for_arb_X_select", this, &
bayesian_estimator::b_dist_for_arb_X_selection, 0, 20, 2),
441 B_dist_for_arb_X_select_from_histo(
"b_dist_for_arb_X_select_from_histo", this, &
bayesian_estimator::b_dist_for_arb_X_selection_from_histo, 0, 20, 2),
442 fIntegerVariable(integer_variable)
448 theFitter.set_par_names(P_X_fit_function);
449 theFitter.set_par_names(mean_X_vs_cb_function);
450 theFitter.set_par_names(mean_X_vs_b_function);
455 theFitter(previous_fit),
456 p_X_cb_integrator(
"p_X_cb_integrator", this, &
bayesian_estimator::P_X_cb_for_integral, 0, 1, 1),
457 P_X_fit_function(
"P_X_fit_function", this, &
bayesian_estimator::cb_integrated_P_X, 0, 1, theFitter.npar()),
458 mean_X_vs_cb_function(
"mean_X_vs_cb", this, &
bayesian_estimator::mean_X_vs_cb, 0, 1, theFitter.npar()),
459 mean_X_vs_b_function(
"mean_X_vs_b", this, &
bayesian_estimator::mean_X_vs_b, 0, 20, theFitter.npar()),
460 p_X_X_integrator(
"p_X_X_integrator", this, &
bayesian_estimator::P_X_cb_for_X_integral, 0, 1000, 1),
461 p_X_X_integrator_with_selection(
"p_X_X_integrator_with_selection", this, &
bayesian_estimator::P_X_cb_for_X_integral_with_selection, 0, 1000, 1),
463 Cb_dist_for_X_select(
"Cb_dist_for_X_select", this, &
bayesian_estimator::cb_dist_for_X_selection, 0, 1, 2),
464 Cb_dist_for_arb_X_select(
"Cb_dist_for_arb_X_select", this, &
bayesian_estimator::cb_dist_for_arb_X_selection, 0, 1, 2),
465 B_dist_for_X_select(
"b_dist_for_X_select", this, &
bayesian_estimator::b_dist_for_X_selection, 0, 20, 2),
466 B_dist_for_arb_X_select(
"b_dist_for_arb_X_select", this, &
bayesian_estimator::b_dist_for_arb_X_selection, 0, 20, 2),
467 B_dist_for_arb_X_select_from_histo(
"b_dist_for_arb_X_select_from_histo", this, &
bayesian_estimator::b_dist_for_arb_X_selection_from_histo, 0, 20, 2),
468 fIntegerVariable(integer_variable)
475 theFitter.set_par_names(P_X_fit_function);
476 theFitter.set_par_names(mean_X_vs_cb_function);
477 theFitter.set_par_names(mean_X_vs_b_function);
511 theFitter.set_initial_parameters(histo, P_X_fit_function);
512 histo->
Fit(&P_X_fit_function);
590 theFitter.fill_array_from_params(par);
592 return mean_X_vs_cb_function;
607 theFitter.fill_array_from_params(par);
626 auto obs_vs_b =
new TH2F(
"obs_vs_b", obs_vs_X->
GetTitle(),
627 baxis_nbins, baxis_bmin, baxis_bmax, Ny,
ymin,
ymax);
628 for (
int binx = 1; binx <= Nx; ++binx) {
631 for (
int biny = 1; biny <= Ny; ++biny) {
634 for (
int i = 0; i < stat; ++i) {
635 obs_vs_b->Fill(B_dist_for_X_select.
GetRandom(0., 1.),
646 KVError::Error(
this,
"GraphMeanbvsX",
"Need to give histogram of X observable to NormalizeHisto() first");
654 std::cout <<
N << std::endl;
656 for (
int i = 1; i <=
N; ++i) {
660 std::cout << i <<
" " <<
X;
662 #if ROOT_VERSION_CODE >= ROOT_VERSION(6,24,0)
663 g->AddPoint(
X, mean);
665 g->SetPoint(
g->GetN(),
X, mean);
667 g->SetPointError(
g->GetN() - 1, 0,
sigma);
668 std::cout <<
" " << mean <<
" " <<
sigma << std::endl;
681 KVError::Warning(
this,
"update_fit_params",
"no histogram set with FitHisto(TH1*)");
686 KVError::Warning(
this,
"update_fit_params",
"no fit function found in histogram");
689 theFitter.fill_params_from_array(
fit->GetParameters());
708 f->SetLineColor(color);
709 f->SetMarkerColor(color);
712 return f->GetMaximum();
735 sel_rapp.assign((std::vector<double>::size_type)incl->
GetNbinsX(), 0.0);
736 int first_bin(0), last_bin(0);
737 for (
int i = 1; i <= incl->
GetNbinsX(); ++i) {
740 if (
sel->GetBinContent(i) > 0) {
741 if (!first_bin) first_bin = i;
754 Cb_dist_for_arb_X_select.
SetRange(0., 1.);
758 double cb_mean(0), cb_sqrmean(0), sum_pcb(0);
760 for (
int i = 0; i < 500; ++i) {
761 double cb = i / 499.;
762 double p_cb = Cb_dist_for_arb_X_select.
Eval(cb);
764 cb_mean += p_cb * cb;
765 cb_sqrmean += p_cb * cb * cb;
768 f->SetPoint(i, cb, p_cb);
772 cb_sqrmean /= sum_pcb;
775 sigma_cb =
TMath::Sqrt(cb_sqrmean - cb_mean * cb_mean);
778 f->SetLineColor(color);
779 f->SetMarkerColor(color);
782 if (
TString(opt) ==
"same")
f->Draw(
"l");
807 for (
int i = 0; i < 500; ++i) {
809 double sig = B_dist_for_X_select.
Eval(
b);
810 if (sig > maxS) maxS = sig;
811 f->SetPoint(i,
b, sig);
813 f->SetLineColor(color);
814 f->SetMarkerColor(color);
817 if (
TString(opt) ==
"same")
f->Draw(
"l");
823 return B_dist_for_X_select;
843 for (
int i = 0; i < npts; ++i) {
845 double sig = B_dist_for_X_select.
Eval(
b);
846 f->SetPoint(i,
b, sig);
876 sel_rapp.assign((std::vector<double>::size_type)incl->
GetNbinsX(), 0.0);
877 int first_bin(0), last_bin(0);
878 for (
int i = 1; i <= incl->
GetNbinsX(); ++i) {
881 if (
sel->GetBinContent(i) > 0) {
882 if (!first_bin) first_bin = i;
897 double bmean(0), bsqrmean(0), sigtot(0);
899 for (
int i = 0; i < 500; ++i) {
901 double sig = B_dist_for_arb_X_select.
Eval(
b);
903 bsqrmean += sig *
b *
b;
905 f->SetPoint(i,
b, sig);
907 f->SetLineColor(color);
908 f->SetMarkerColor(color);
911 mean = bmean / sigtot;
914 if (
TString(opt) ==
"same")
f->Draw(
"l");
940 sel_rapp.assign((std::vector<double>::size_type)incl->
GetNbinsX(), 0.0);
941 int first_bin(0), last_bin(0);
942 for (
int i = 1; i <= incl->
GetNbinsX(); ++i) {
945 if (
sel->GetBinContent(i) > 0) {
946 if (!first_bin) first_bin = i;
957 B_dist_for_arb_X_select_from_histo.
SetParameters(Xmin, Xmax);
961 double bmean(0), bsqrmean(0), sigtot(0);
963 for (
int i = 0; i < 500; ++i) {
965 double sig = B_dist_for_arb_X_select_from_histo.
Eval(
b);
967 bsqrmean += sig *
b *
b;
969 f->SetPoint(i,
b, sig);
971 f->SetLineColor(color);
972 f->SetMarkerColor(color);
975 mean = bmean / sigtot;
978 if (
TString(opt) ==
"same")
f->Draw(
"l");
1004 sel_rapp.assign((std::vector<double>::size_type)incl->
GetNbinsX(), 0.0);
1005 int first_bin(0), last_bin(0);
1006 for (
int i = 1; i <= incl->
GetNbinsX(); ++i) {
1009 if (
sel->GetBinContent(i) > 0) {
1010 if (!first_bin) first_bin = i;
1022 mean = B_dist_for_arb_X_select.
Mean(0, 20);
1023 double var = B_dist_for_arb_X_select.
Variance(0, 20);
1047 for (
int i = 0; i < 500; ++i) {
1049 double sig = B_dist_for_X_select.
Eval(
b);
1055 KVError::Warning(
this,
"GetMeanAndSigmaBDistForXSelection",
"Total cross-section = 0!");
1073 f->SetLineColor(color);
1101 theFitter.print_fit_params();
1106 return theFitter.get_params();
1127 theFitter.backup_params();
1128 theFitter.normalise_shape_function();
1130 theFitter.fill_array_from_params(par);
1136 theFitter.restore_params();
1144 auto jpd =
new TF2(
"joint_proba_dist",
this, &bayesian_estimator::P_X_cb_for_TF2_obs_vs_b,
1145 b_range.
Min(), b_range.
Max(), X_range.
Min(), X_range.
Max(), 0);
#define ClassDef(name, id)
winID h TVirtualViewer3D TVirtualGLPainter p
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 Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t sel
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 b
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 g
R__EXTERN TRandom * gRandom
Base class for KaliVeda framework.
Fluctuation kernel using binomial distribution for use with bayesian_estimator.
double operator()(double X, double mean, double reduced_variance)
Fluctuation kernel using negative binomial distribution for use with bayesian_estimator.
double operator()(double X, double mean, double reduced_variance)
Impact parameter distribution reconstruction from experimental data.
void DrawBDistForSelection(TH1 *sel, TH1 *incl, double &mean, double &sigma, Option_t *opt="", Color_t color=kRed, const TString &title="")
void DrawCbDistForSelection(TH1 *sel, TH1 *incl, double &mean_cb, double &sigma_cb, Option_t *opt="", Color_t color=kRed, const TString &title="")
void RenormaliseHisto(TH1 *h)
KVNameValueList GetFitParameters() const
virtual ~bayesian_estimator()
double DrawBDistForXSelection(KVValueRange< double > Xrange, Option_t *opt="", Color_t color=kRed, const TString &title="")
void GetMeanAndSigmaBDistForSelection(TH1 *sel, TH1 *incl, double &mean, double &sigma)
TF1 & GetB_dist_for_X_select()
bayesian_estimator(Bool_t integer_variable=false)
TGraph * GraphMeanXvsb(int npts=500)
void SetIPDistParams(double sigmaR, double deltab)
void SetIPDistFromHisto(TH1 *ip_histo)
TF1 * GetFittedP_X(double norm=1.0)
double DrawCbDistForXSelection(double X1, double X2, Option_t *opt="", Color_t color=kRed, const TString &title="")
void FitHisto(TH1 *h=nullptr)
TGraphErrors * GraphMeanbvsX()
bayesian_estimator(const FittingFunction &previous_fit, Bool_t integer_variable=false)
impact_parameter_distribution & GetIPDist()
void GetMeanAndSigmaBDistForXSelection(KVValueRange< double > Xrange, double &mean, double &sigma)
void DrawNormalisedMeanXvsb(const TString &title, Color_t color, Option_t *opt)
void Print(Option_t *="") const
TGraph * GraphBDistForXSelection(KVValueRange< double > Xrange, int npts=500)
void DrawBDistForSelectionFromIPHisto(TH1 *sel, TH1 *incl, double &mean, double &sigma, Option_t *opt="", Color_t color=kRed, const TString &title="")
TGraph * GraphP_XForGivenB(double b, KVValueRange< double > Xrange, int npts=500)
TF2 * GetJointProbabilityDistribution(KVValueRange< double > b_range, KVValueRange< double > X_range)
void DrawMeanXvsCb(const TString &title="", Color_t color=-1, Option_t *opt="")
TH2 * TransformXaxisToBaxis(const TH2 *obs_vs_X, int baxis_nbins=100, double baxis_bmin=0, double baxis_bmax=1)
void DrawFittedP_X(double norm=1.0, Option_t *opt="", Color_t color=kRed, const TString &title="")
Fluctuation kernel using gamma distribution for use with bayesian_estimator.
double operator()(double X, double mean, double reduced_variance)
Class implementing parametrizable impact parameter distributions.
Double_t GetDifferentialCrossSection(double b) const
Double_t GetDifferentialCrossSectionFromHisto(double bb)
TH1 * GetCentralityFromHisto()
const TF1 & GetCentrality()
void SetDeltaB_WithConstantCrossSection(Double_t deltab, Double_t sigmaR=0)
Handles lists of named parameters with different types, a list of KVNamedParameter objects.
Range of values specified by minimum, maximum.
ValueType ValueIofN(Int_t i, Int_t n) const
virtual void SetLineColor(Color_t lcolor)
virtual Double_t GetBinCenter(Int_t bin) const
virtual Double_t GetBinLowEdge(Int_t bin) const
virtual Double_t GetBinWidth(Int_t bin) const
virtual Double_t GetBinUpEdge(Int_t bin) const
virtual Double_t Mean(Double_t a, Double_t b, const Double_t *params=nullptr, Double_t epsilon=0.000001)
virtual TH1 * GetHistogram() const
virtual void SetRange(Double_t xmin, Double_t xmax)
virtual Double_t Integral(Double_t a, Double_t b, Double_t epsrel=1.e-12)
void SetTitle(const char *title="") override
void Draw(Option_t *option="") override
virtual Double_t Variance(Double_t a, Double_t b, const Double_t *params=nullptr, Double_t epsilon=0.000001)
virtual TF1 * DrawCopy(Option_t *option="") const
virtual Double_t GetRandom(Double_t xmin, Double_t xmax, TRandom *rng=nullptr, Option_t *opt=nullptr)
virtual void SetParameters(const Double_t *params)
virtual void SetParNames(const char *name0="p0", const char *name1="p1", const char *name2="p2", const char *name3="p3", const char *name4="p4", const char *name5="p5", const char *name6="p6", const char *name7="p7", const char *name8="p8", const char *name9="p9", const char *name10="p10")
virtual Double_t Eval(Double_t x, Double_t y=0, Double_t z=0, Double_t t=0) const
virtual void SetParameter(const TString &name, Double_t value)
virtual void SetPoint(Int_t i, Double_t x, Double_t y)
virtual Double_t GetBinCenter(Int_t bin) const
virtual Int_t GetNbinsY() const
TObject * FindObject(const char *name) const override
virtual TFitResultPtr Fit(const char *formula, Option_t *option="", Option_t *goption="", Double_t xmin=0, Double_t xmax=0)
virtual Int_t GetNbinsX() const
virtual Double_t GetBinLowEdge(Int_t bin) const
virtual Double_t Integral(Int_t binx1, Int_t binx2, Option_t *option="") const
virtual Double_t GetBinContent(Int_t bin) const
virtual Double_t GetBinWidth(Int_t bin) const
virtual void Scale(Double_t c1=1, Option_t *option="")
virtual Int_t FindBin(Double_t x, Double_t y=0, Double_t z=0)
virtual Double_t GetBinContent(Int_t bin) const
const char * GetTitle() const override
virtual Double_t Uniform(Double_t x1, Double_t x2)
double binomial_pdf(unsigned int k, double p, unsigned int n)
double negative_binomial_pdf(unsigned int k, double p, double n)
double gamma_pdf(double x, double alpha, double theta, double x0=0)
RVec< PromoteTypes< T0, T1 > > pow(const T0 &x, const RVec< T1 > &v)
void Error(UserClass p, const char *location, const char *va_(fmt),...)
void Warning(UserClass p, const char *location, const char *va_(fmt),...)
fit(model, train_loader, val_loader, num_epochs, batch_size, optimizer, criterion, save_best, scheduler)
Double_t Sqrt(Double_t x)