Skip to content

Commit 0b2b3bd

Browse files
committed
introduce random engine, seed, params struct and randomization function
1 parent f77124e commit 0b2b3bd

2 files changed

Lines changed: 44 additions & 1 deletion

File tree

PWGHF/D2H/Macros/HFInvMassFitter.cxx

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#include <TDatabasePDG.h>
4040
#include <TLine.h>
4141
#include <TPaveText.h>
42+
#include <TRandom3.h>
4243
#include <TStyle.h>
4344
#include <TVirtualPad.h>
4445

@@ -128,12 +129,18 @@ HFInvMassFitter::HFInvMassFitter(TH1* histoToFit,
128129
mIntegralSgn(0),
129130
mHistoTemplateRefl(nullptr),
130131
mDrawBgPrefit(false),
131-
mHighlightPeakRegion(false)
132+
mHighlightPeakRegion(false),
133+
mRandomSeed(-1),
134+
mRandomGen(nullptr)
132135
{
133136
// standard constructor
134137
mHistoInvMass = histoToFit;
135138
mHistoInvMass->SetName("mHistoInvMass");
136139
mHistoInvMass->SetDirectory(nullptr);
140+
if (mRandomSeed >= 0) {
141+
mRandomGen = new TRandom3();
142+
mRandomGen->SetSeed(mRandomSeed);
143+
}
137144
}
138145

139146
HFInvMassFitter::~HFInvMassFitter()
@@ -988,3 +995,27 @@ void HFInvMassFitter::setTemplateReflections(TH1* histoRefl)
988995
mHistoTemplateRefl = histoRefl;
989996
mHistoTemplateRefl->SetName("mHistoTemplateRefl");
990997
}
998+
999+
double HFInvMassFitter::randomizeInitialParameter(const ParameterRanges& parameterRanges)
1000+
{
1001+
constexpr double DefaultSigmaFraction{10.};
1002+
constexpr int MaximalNumberOfIterations{20};
1003+
1004+
if (mRandomSeed < 0) {
1005+
return parameterRanges.initial;
1006+
}
1007+
const auto sigma = parameterRanges.sigma < 0 ? (parameterRanges.upper - parameterRanges.lower) / DefaultSigmaFraction : parameterRanges.sigma;
1008+
1009+
double result;
1010+
int nIter{0};
1011+
do {
1012+
result = mRandomGen->Gaus(parameterRanges.initial, sigma);
1013+
++nIter;
1014+
if (nIter > MaximalNumberOfIterations) {
1015+
printf("randomizeInitialFitParameter() - long while loop with lower = %f upper = %f initial = %f sigma = %f\n", parameterRanges.lower, parameterRanges.upper, parameterRanges.initial, sigma);
1016+
throw std::runtime_error("");
1017+
}
1018+
} while (result < parameterRanges.lower || result > parameterRanges.upper);
1019+
1020+
return result;
1021+
}

PWGHF/D2H/Macros/HFInvMassFitter.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <TF1.h>
2929
#include <TH1.h>
3030
#include <TNamed.h>
31+
#include <TRandom3.h>
3132
#include <TVirtualPad.h>
3233

3334
#include <Rtypes.h>
@@ -39,6 +40,14 @@
3940

4041
class HFInvMassFitter : public TNamed
4142
{
43+
private:
44+
struct ParameterRanges {
45+
double lower{};
46+
double upper{};
47+
double initial{};
48+
double sigma{-999.};
49+
};
50+
4251
public:
4352
enum TypeOfBkgPdf {
4453
Expo = 0,
@@ -141,6 +150,7 @@ class HFInvMassFitter : public TNamed
141150
HFInvMassFitter& operator=(const HFInvMassFitter& source);
142151
void fillWorkspace(RooWorkspace& w) const;
143152
void highlightPeakRegion(const RooPlot* plot, Color_t color = kGray + 1, Width_t width = 1, Style_t style = 2) const;
153+
double randomizeInitialParameter(const ParameterRanges& parameterRanges);
144154

145155
TH1* mHistoInvMass; // histogram to fit
146156
std::string mFitOption;
@@ -212,6 +222,8 @@ class HFInvMassFitter : public TNamed
212222
TH1* mHistoTemplateRefl; /// reflection histogram
213223
bool mDrawBgPrefit; /// draw background after fitting the sidebands
214224
bool mHighlightPeakRegion; /// draw vertical lines showing the peak region (usually +- 3 sigma)
225+
int mRandomSeed; /// seed for random engine for fit's initial parameters randomization
226+
TRandom3* mRandomGen; /// engine for fit's initial parameters randomization
215227

216228
ClassDefOverride(HFInvMassFitter, 1);
217229
};

0 commit comments

Comments
 (0)