20 double maxeffsiggoal,
int maxnp) {
21 double max_penalty_best = -1;
23 for (
int np = 3; np <= maxnp; ++np) {
24 ATH_MSG_INFO(
"========== Spline #=" << np <<
" ==============");
26 if (max_penalty_best > 0) {
29 if (max_penalty_best < 0 || max_penalty < max_penalty_best) {
30 max_penalty_best = max_penalty;
36 if (max_penalty_best < 0 || max_penalty < max_penalty_best) {
37 max_penalty_best = max_penalty;
43 if (max_penalty_best < 0 || max_penalty < max_penalty_best) {
44 max_penalty_best = max_penalty;
48 ATH_MSG_INFO(
"========== Spline #=" << np <<
" max_penalty_best="
50 <<
" ==============");
51 ATH_MSG(INFO) <<
"==== Best spline init | ";
52 for (
int i = 0; i < sp_best.GetNp(); ++i) {
54 sp_best.GetKnot(i, p,
x);
55 ATH_MSG(INFO) << i <<
" : p=" << p <<
" x=" <<
x <<
" ; ";
59 if (max_penalty_best < 2)
64 return max_penalty_best;
69 double maxeffsiggoal) {
75 double maxdev =
get_maxdev(hist,
sp, maxeffsig, p_maxdev, p_maxeffsig);
77 if (maxdev / maxdevgoal > maxeffsig / maxeffsiggoal)
80 p_improve = p_maxeffsig;
82 int nsplinepoints =
sp.GetNp();
83 std::vector<double> nprop(nsplinepoints + 1);
85 ATH_MSG(INFO) <<
"Spline init p_improve=" << p_improve <<
" | ";
86 for (
int i = 0; i < nsplinepoints; ++i) {
89 if (i == 0 && p_improve < p) {
90 nprop[ind] = (0 + p) / 2;
91 ATH_MSG(INFO) << ind <<
" : pi=" << nprop[ind] <<
" ; ";
96 ATH_MSG(INFO) << ind <<
" : p=" << nprop[ind] <<
" ; ";
99 if (i == nsplinepoints - 1 && p_improve > p) {
100 nprop[ind] = (1 + p) / 2;
101 ATH_MSG(INFO) << ind <<
" : pi=" << nprop[ind] <<
" ; ";
104 if (i < nsplinepoints - 1) {
106 sp.GetKnot(i + 1, pn, xn);
107 if (p_improve > p && p_improve < pn) {
108 nprop[ind] = (p + pn) / 2;
109 ATH_MSG(INFO) << ind <<
" : pi=" << nprop[ind] <<
" ; ";
116 nprop.resize(nsplinepoints);
121 ATH_MSG_INFO(
"Spline init spline #=" << nsplinepoints <<
" : maxdev="
122 << maxdev <<
" p_maxdev=" << p_maxdev
123 <<
" maxeffsig=" << maxeffsig
124 <<
" p_maxeffsig=" << p_maxeffsig
125 <<
" max_penalty=" << max_penalty);
127 for (
int i = 0; i <
m_spline.GetNp(); ++i) {
130 ATH_MSG(INFO) << i <<
" : p=" << p <<
" x=" <<
x <<
" ; ";
138 double maxeffsiggoal,
144 for (
int i = 1; i <= hist->GetNbinsX(); i++) {
145 xmin = hist->GetXaxis()->GetBinLowEdge(i);
146 if (hist->GetBinContent(i) > 0)
149 for (
int i = hist->GetNbinsX(); i >= 1; i--) {
150 xmax = hist->GetXaxis()->GetBinUpEdge(i);
151 if (hist->GetBinContent(i) > 0)
156 double dx = (
xmax -
xmin) / (nsplinepoints - 1);
158 std::vector<double> nprop(nsplinepoints);
159 std::vector<double> nx(nsplinepoints);
163 for (
int i = 1; i < nsplinepoints; ++i) {
164 nx[i] =
xmin + i * dx;
170 p_test = 0.5 * (p_min + p_max);
176 if ((p_max - p_min) < 0.0000001)
178 }
while (TMath::Abs(tx - nx[i]) > dx / 10);
189 ATH_MSG_INFO(
"Spline init equ. dist. #=" << nsplinepoints <<
" : maxdev="
190 << maxdev <<
" p_maxdev=" << p_maxdev
191 <<
" maxeffsig=" << maxeffsig
192 <<
" p_maxeffsig=" << p_maxeffsig
193 <<
" max_penalty=" << max_penalty);
195 for (
int i = 0; i <
m_spline.GetNp(); ++i) {
198 ATH_MSG(INFO) << i <<
" : p=" << p <<
" x=" <<
x <<
" ; ";
206 double maxeffsiggoal,
210 double dprop = 1.0 / (nsplinepoints - 1);
211 std::vector<double> nprop(nsplinepoints);
212 for (
int i = 0; i < nsplinepoints; ++i) {
213 nprop[i] = i * dprop;
222 ATH_MSG_INFO(
"Spline init equ. prob. #=" << nsplinepoints <<
" : maxdev="
223 << maxdev <<
" p_maxdev=" << p_maxdev
224 <<
" maxeffsig=" << maxeffsig
225 <<
" p_maxeffsig=" << p_maxeffsig
226 <<
" max_penalty=" << max_penalty);
228 for (
int i = 0; i <
m_spline.GetNp(); ++i) {
231 ATH_MSG(INFO) << i <<
" : p=" << p <<
" x=" <<
x <<
" ; ";
238 std::vector<double> &nprop,
241 double maxdevgoal,
double maxeffsiggoal) {
242 int nsplinepoints = (int)nprop.size();
245 std::vector<double> nx(nsplinepoints);
246 std::vector<double> nprop_try = nprop;
247 double max_penalty = -1;
248 double p_gotobest = 0.5 / nsplinepoints;
249 int ntry = 200 / p_gotobest;
251 for (
double dproploop = 0.4 / nsplinepoints; dproploop > 0.02 / nsplinepoints;
253 double dprop = dproploop;
255 for (
int itry = 0; itry < ntry; ++itry) {
257 for (
int i = 0; i < nsplinepoints; ++i) {
260 TSpline3
sp(
"1Dspline", nprop_try.data(), nx.data(), nsplinepoints,
265 double maxdev =
get_maxdev(hist,
sp, maxeffsig, p_maxdev, p_maxeffsig);
266 double penalty = maxdev / maxdevgoal + maxeffsig / maxeffsiggoal;
267 if (max_penalty < 0 || penalty < max_penalty) {
268 max_penalty = penalty;
281 if (gRandom->Rndm() < p_gotobest)
285 if (n_nogain > 20 / p_gotobest)
288 int ip = 1 + gRandom->Rndm() * (nsplinepoints - 1);
289 if (ip > nsplinepoints - 1)
290 ip = nsplinepoints - 1;
292 if (gRandom->Rndm() > 0.5)
294 double nprop_new = nprop_try[ip] + d;
296 if (nprop_try[ip - 1] + dprop / 2 > nprop_new) {
297 nprop_new = nprop_try[ip];
300 if (ip < nsplinepoints - 1)
301 if (nprop_new > nprop_try[ip + 1] - dprop / 2) {
302 nprop_new = nprop_try[ip];
313 nprop_try[ip] = nprop_new;
321 double &maxeffsig,
double &p_maxdev,
322 double &p_maxeffsig,
int ntoy) {
326 TH1 *hist_clone = (TH1 *)hist->Clone(
"hist_clone");
327 hist_clone->SetDirectory(
nullptr);
330 double integral = hist->IntegralAndError(1, hist->GetNbinsX(), interr);
336 double toyweight = 1.0 / ntoy;
337 for (
int itoy = 0; itoy < ntoy; ++itoy) {
338 double prop = itoy * toyweight;
339 hist_clone->Fill(
sp.Eval(prop), toyweight);
344 for (
int i = 0; i <= hist->GetNbinsX() + 1; i++) {
345 int1 += hist->GetBinContent(i) /
integral;
346 int2 += hist_clone->GetBinContent(i);
347 double val = TMath::Abs(int1 - int2);
355 double int1err = TMath::Sqrt(int1 * (1 - int1) / effN);
358 valsig = val / int1err;
359 if (valsig > maxeffsig) {
381 TH1 *histfine =
nullptr;
382 if (hist ==
nullptr) {
388 hist =
new TH1D(
"test1D",
"test1D", nbinsx,
xmin,
xmax);
389 histfine =
new TH1D(
"test1Dfine",
"test1Dfine", 10 * nbinsx,
xmin,
xmax);
391 for (
int i = 1; i <= 100000; ++i) {
392 double x = gRandom->Gaus(xpeak, sigma);
397 histfine->Fill(
x, 10);
404 nbinsx = hist->GetNbinsX();
409 for (
rnd[0] = 0;
rnd[0] < 0.9999;
rnd[0] += 0.25) {
414 TH1 *hist_val = (TH1 *)histfine->Clone(
"hist_val");
415 hist_val->SetTitle(
"toy simulation");
417 hist_val->SetLineColor(2);
418 TH1 *hist_diff = (TH1 *)hist->Clone(
"hist_diff");
419 hist_diff->SetTitle(
"difference");
422 double weight = histfine->Integral() / nrnd;
423 double weightdiff = hist->Integral() / nrnd;
426 for (
int i = 0; i < nrnd; ++i) {
427 rnd[0] = gRandom->Rndm();
429 hist_val->Fill(value[0], weight);
430 hist_diff->Fill(value[0], weightdiff);
432 hist_diff->Add(hist, -1);
434 TH1F *hist_pull =
new TH1F(
"pull",
"pull", 200, -10, 10);
435 for (
int ix = 1; ix <= nbinsx; ++ix) {
436 float val = hist_diff->GetBinContent(ix);
437 float err = hist_diff->GetBinError(ix);
439 hist_pull->Fill(val / err);
445#if defined(__FastCaloSimStandAlone__)
446 new TCanvas(
"input",
"Input");
447 histfine->SetLineColor(kGray);
448 histfine->Draw(
"hist");
450 hist_val->Draw(
"sameshist");
452 new TCanvas(
"spline",
"spline");
455 TGraph *
gr =
new TGraph();
456 for (
int i = 0; i < ngr; ++i) {
457 double r = i * 1.0 / (ngr - 1);
460 gr->SetMarkerStyle(7);
462 TSpline3 *
sp =
new TSpline3(rtof.
spline());
464 sp->SetMarkerColor(2);
465 sp->SetMarkerStyle(2);
468 new TCanvas(
"difference",
"difference");
471 new TCanvas(
"pull",
"Pull");
#define ATH_MSG_NOCLASS(logger_name, x)
#define ATLAS_NOT_THREAD_SAFE
getNoisyStrip() Find noisy strips from hitmaps and write out into xml/db formats
virtual double rnd_to_fct(double rnd) const
Function gets random number rnd in the range [0,1) as argument and returns function value according t...
const TSpline3 & spline() const
double InitializeEqualDistance(TH1 *hist, double maxdevgoal=0.01, double maxeffsiggoal=3, int nsplinepoints=5)
double InitializeEqualProbability(TH1 *hist, double maxdevgoal=0.01, double maxeffsiggoal=3, int nsplinepoints=5)
virtual double rnd_to_fct(double rnd) const
Function gets random number rnd in the range [0,1) as argument and returns function value according t...
double Initialize(TH1 *hist, double maxdevgoal=0.01, double maxeffsiggoal=3, int maxnp=20)
static double get_maxdev(const TH1 *hist, const TSpline3 &sp, double &maxeffsig, double &p_maxdev, double &p_maxeffsig, int ntoy=10000)
double InitializeFromSpline(TH1 *hist, const TSpline3 &sp, double maxdevgoal=0.01, double maxeffsiggoal=3)
static double optimize(TSpline3 &sp_best, std::vector< double > &nprop, const TH1 *hist, TFCS1DFunctionInt32Histogram &hist_fct, double maxdevgoal=0.01, double maxeffsiggoal=3)
static Root::TMsgLogger logger("iLumiCalc")