8 #include <CLI/Timer.hpp> 33 double integralExpCon(
double lo,
double hi) {
return (exp(-lo) - exp(-hi)); }
35 double integralExpLin(
double lo,
double hi) {
return ((lo + 1) * exp(-lo) - (hi + 1) * exp(-hi)); }
38 return ((lo * lo + 2 * lo + 2) * exp(-lo) - (hi * hi + 2 * hi + 2) * exp(-hi));
42 vector<int> &rsEvtVec,
43 vector<int> &wsEvtVec,
47 int eventsToGenerate) {
48 static TRandom donram(24);
52 for(
int i = 0; i < decayTime.
getNumBins(); ++i) {
53 double binStart = i * step;
55 double binFinal = binStart + step;
62 double expectedRSevts = eventsToGenerate * rsIntegral / totalRSintegral;
63 double expectedWSevts = eventsToGenerate * wsIntegral / totalRSintegral;
65 int rsEvts = donram.Poisson(expectedRSevts);
66 int wsEvts = donram.Poisson(expectedWSevts);
71 std::cout <<
"Events in bin " << i <<
" : " << rsEvts <<
" (" << expectedRSevts <<
") " << wsEvts <<
" (" 72 << expectedWSevts <<
")\n";
80 std::string plotName =
"") {
85 for(
unsigned int i = 0; i < wsEvts.size(); ++i) {
86 double ratio = wsEvts[i];
96 double error = wsEvts[i] / pow(rsEvts[i], 2);
97 error += pow(wsEvts[i], 2) / pow(rsEvts[i], 3);
100 ratioData.setBinContent(i, ratio);
101 ratioData.setBinError(i, error);
102 ratioHist.SetBinContent(i + 1, ratio);
103 ratioHist.SetBinError(i + 1, error);
111 CLI::Timer timer_cpu{
"GPU"};
113 std::string timer_str = timer_cpu.to_string();
115 if(!plotName.empty()) {
119 for(
int i = 0; i < values.size(); ++i) {
120 pdfHist.SetBinContent(i + 1, values[i]);
123 ratioHist.SetMarkerStyle(8);
124 ratioHist.SetMarkerSize(0.5);
125 ratioHist.SetStats(
false);
128 TString str1 = fmt::format(
129 "Constant [10^{{-2}}] : {:.3} #pm {:.3}", weights[0].getValue() * 1e2, weights[0].getError() * 1e2);
130 TLatex res1(0.14, 0.83, str1);
133 TString str2 = fmt::format(
134 "Linear [10^{{-4}}] : {:.3} #pm {:.3}", weights[1].getValue() * 1e4, weights[1].getError() * 1e4);
135 TLatex res2(0.14, 0.73, str2);
138 TString str3 = fmt::format(
139 "Quadratic [10^{{-6}}]: {:.3} #pm {:.3}", weights[2].getValue() * 1e6, weights[2].getError() * 1e6);
140 TLatex res3(0.14, 0.63, str3);
147 pdfHist.SetLineColor(kBlue);
148 pdfHist.SetLineWidth(3);
149 pdfHist.SetStats(
false);
150 pdfHist.Draw(
"lsame");
151 foo.SaveAs(plotName.c_str());
157 return make_tuple(
int(fitter), timer_str);
160 void cpvFitFcn(
int &npar,
double *gin,
double &fun,
double *fp,
int iflag) {
161 double conCoef = fp[0];
162 double linCoef = fp[1];
163 double squCoef = fp[2];
168 for(
unsigned int i = 0; i <
ratios.size(); ++i) {
170 double pdfval = conCoef + linCoef * currDTime + squCoef * currDTime * currDTime;
181 ratios.resize(wsEvts.size());
182 errors.resize(wsEvts.size());
184 for(
unsigned int i = 0; i < wsEvts.size(); ++i) {
188 fptype ratio = wsEvts[i] / rsEvts[i];
193 double error = wsEvts[i] / pow(rsEvts[i], 2);
194 error += pow(wsEvts[i], 2) / pow(rsEvts[i], 3);
199 ratioHist->SetBinContent(i + 1, ratio);
200 ratioHist->SetBinError(i + 1, error);
203 TMinuit *minuit =
new TMinuit(3);
204 minuit->DefineParameter(0,
"constaCoef", 0.03, 0.01, -1, 1);
205 minuit->DefineParameter(1,
"linearCoef", 0, 0.01, -1, 1);
206 minuit->DefineParameter(2,
"secondCoef", 0, 0.01, -1, 1);
212 int main(
int argc,
char **argv) {
216 app.add_option(
"-n,--numbins", numbins,
"Number of bins",
true);
218 int eventsToGenerate = 10000000;
219 app.add_option(
"-e,--events", eventsToGenerate,
"Events to generate",
true);
232 double x_mix = 0.0016;
233 double y_mix = 0.0055;
235 double magQP = 1.0 / magPQ;
242 double dZeroLinearCoef = magPQ * sqrt(rSubD) * (y_mix * cos(delta + wpPhi) - x_mix * sin(delta + wpPhi));
243 double d0barLinearCoef = magQP * sqrt(rBarD) * (y_mix * cos(delta - wpPhi) - x_mix * sin(delta - wpPhi));
245 double dZeroSecondCoef = 0.25 * magPQ * magPQ * (x_mix * x_mix + y_mix * y_mix);
246 double d0barSecondCoef = 0.25 * magQP * magQP * (x_mix * x_mix + y_mix * y_mix);
248 generateEvents(
decayTime, dZeroEvtsRS, dZeroEvtsWS, rSubD, dZeroLinearCoef, dZeroSecondCoef, eventsToGenerate);
249 generateEvents(
decayTime, d0barEvtsRS, d0barEvtsWS, rBarD, d0barLinearCoef, d0barSecondCoef, eventsToGenerate);
251 Variable constaCoef(
"constaCoef", 0.03, 0.01, -1, 1);
252 Variable linearCoef(
"linearCoef", 0, 0.01, -1, 1);
253 Variable secondCoef(
"secondCoef", 0, 0.01, -1, 1);
255 vector<Variable>
weights = {constaCoef, linearCoef, secondCoef};
257 int retval1, retval2;
258 std::string fit1, fit2;
259 std::tie(retval1, fit1)
260 =
fitRatio(
decayTime, weights, dZeroEvtsRS, dZeroEvtsWS,
"chisquare_dzeroEvtRatio_goo_cpp.png");
261 std::tie(retval2, fit2)
262 =
fitRatio(
decayTime, weights, d0barEvtsRS, d0barEvtsWS,
"chisquare_dzbarEvtRatio_goo_cpp.png");
264 CLI::Timer timer_cpu{
"Total CPU (2x fits)"};
267 std::string cpu_string = timer_cpu.to_string();
269 std::cout << fit1 <<
"\n" << fit2 <<
"\n" << cpu_string << std::endl;
271 fmt::print(
"Exit codes (should be 0): {} and {}\n", retval1, retval2);
273 return retval1 + retval2;
void setNumBins(size_t num)
Set the number of bins.
std::tuple< int, std::string > fitRatio(Observable decayTime, vector< Variable > weights, vector< int > &rsEvts, vector< int > &wsEvts, std::string plotName="")
double integralExpSqu(double lo, double hi)
std::vector< Variable > weights
void generateEvents(Observable decayTime, vector< int > &rsEvtVec, vector< int > &wsEvtVec, double conCoef, double linCoef, double squCoef, int eventsToGenerate)
__host__ fptype getCoefficient(int coef) const
void setValue(fptype val)
Set the value.
void fitRatioCPU(Observable decayTime, vector< int > &rsEvts, vector< int > &wsEvts)
Special class for observables. Used in DataSets.
size_t getNumBins() const
Get the number of bins.
int main(int argc, char **argv)
__host__ void setFitControl(std::shared_ptr< FitControl > fc) override
__host__ void setData(DataSet *data)
double integralExpCon(double lo, double hi)
void cpvFitFcn(int &npar, double *gin, double &fun, double *fp, int iflag)
__host__ std::vector< fptype > evaluateAtPoints(Observable var)
fptype getLowerLimit() const
Get the lower limit.
fptype getUpperLimit() const
Get the upper limit.
#define GOOFIT_PARSE(app,...)
double integralExpLin(double lo, double hi)