-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathfwdsmear.C
426 lines (322 loc) · 13.1 KB
/
fwdsmear.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
// Purpose: d'Agostini ("Bayesian" or Richardson-Lucy) unfolding, including
// response matrix generation from NLO theory and parameterized JER
// Author: [email protected]
// Created: September 2, 2012
// Updated: June 5, 2015
#include "TFile.h"
#include "TDirectory.h"
#include "TList.h"
#include "TObject.h"
#include "TKey.h"
#include "TH1D.h"
#include "TH2D.h"
#include "TGraphErrors.h"
#include "TF1.h"
#include "TMath.h"
#include "TMatrixD.h"
#include "TCanvas.h"
#include "TLine.h"
#include "TStyle.h"
#include "TLegend.h"
#include "RooUnfold/src/RooUnfold.h"
#include "RooUnfold/src/RooUnfoldBayes.h"
#include "RooUnfold/src/RooUnfoldBinByBin.h"
#include "RooUnfold/src/RooUnfoldSvd.h"
#include "RooUnfold/src/RooUnfoldResponse.h"
//#include "RooUnfold.h"
#include "tdrstyle_mod18.C"
#include "ptresolution.h"
#include "settings.h"
#include "tools.h"
#include <iostream>
using namespace std;
// Resolution function
int _jk = 0; // global variable
bool _jet = false; // global variable
Double_t fPtRes(Double_t *x, Double_t *p) {
return ptresolution(x[0], p[0]);
// return effectiveJER(x[0], p[0]);
}
// Ansatz Kernel
int cnt_a = 0;
//const int nk = 4; // number of kernel parameters (excluding pt, eta)
const int nk = 3; // number of kernel parameters (excluding pt, eta)
Double_t smearedAnsatzKernel(Double_t *x, Double_t *p) {
if (++cnt_a%1000000==0) {
cout << "+" << flush;
}
const double pt = x[0]; // true pT
const double ptmeas = p[0]; // measured pT
const double eta = p[1]; // rapidity
//cout << Form("true pt: %10.5f p[0]: %10.5f %10.5f %10.5f %10.5f %10.5f ", pt, p[0], p[1], p[2], p[3], p[4]) << endl;
// double resold = ptresolution(pt, eta+1e-3) * pt;
// double res = effectiveJER(pt, eta) * pt; // Switching this on breaks the integration if the kernel. Resolutions look okay
double res = ptresolution(pt, eta) * pt;
const double s = TMath::Gaus(ptmeas, pt, res, kTRUE);
// const double f = p[2] * exp(p[3]/pt) * pow(pt, p[4])
// * pow(1 - pt*cosh(eta) / jp::emax, p[5]);
const double f = p[2] * pow(pt, p[3])
* pow(1 - pt*cosh(eta) / jp::emax, p[4]);
return (f * s);
}
// Smeared Ansatzz
// double _epsilon = 1e-12; // Tolerance too small when using effective JER...
double _epsilon = 1e-6;
TF1 *_kernel = 0; // global variable, not pretty but works
Double_t smearedAnsatz(Double_t *x, Double_t *p) {
const double pt = x[0];
const double eta = p[0];
//const double eta = 0.0;
if (!_kernel) _kernel = new TF1("_kernel", smearedAnsatzKernel,
1., jp::emax/cosh(eta), nk+2);
double res = ptresolution(pt, eta+1e-3) * pt;
// double res = effectiveJER(pt, eta) * pt;
const double sigma = max(0.10, min(res/pt, 0.30));
double ptmin = pt / (1. + 4.*sigma); // xmin*(1+4*sigma)=x
ptmin = max(1.,ptmin); // safety check
double ptmax = pt / (1. - 3.*sigma); // xmax*(1-3*sigma)=x
// cout << Form("1pt %10.5f sigma %10.5f ptmin %10.5f ptmax %10.5f eta %10.5f",pt, sigma, ptmin, ptmax, eta) << endl << flush;
ptmax = min(jp::emax/cosh(eta), ptmax); // safety check
// cout << Form("2pt %10.5f sigma %10.5f ptmin %10.5f ptmax %10.5f eta %10.5f",pt, sigma, ptmin, ptmax, eta) << endl << flush;
if(ptmin>ptmax)return 1.0;//ptmax=ptmin;
const double par[nk+2] = {pt, eta, p[1], p[2], p[3]};
_kernel->SetParameters(&par[0]);
// Set pT bin limits needed in smearing matrix generation
if (p[4]>0 && p[4]<jp::emax/cosh(eta)) ptmin = p[4];
if (p[5]>0 && p[5]<jp::emax/cosh(eta)) ptmax = p[5];
return ( _kernel->Integral(ptmin, ptmax, _epsilon) );
// return ( 1.0); // integral fails due to nan ptmin ptmax
}
void recurseFile(TDirectory *indir, TDirectory *indir2, TDirectory *outdir,
bool ismc);
void Unfold_histo(TH1D *hpt, TH1D *hpt2, TDirectory *outdir,
bool ismc, bool kscale = false, string id = "");
void fwdsmearUnfold(string type) {
TFile *fin = new TFile(Form("output-%s-2b.root",type.c_str()),"READ");
assert(fin && !fin->IsZombie());
// TFile *fin2 = new TFile(Form("output-%s-2c.root",type.c_str()),"READ");
// TFile *fin2 = new TFile(Form("output-%s-2b.root",type.c_str()),"READ");
// TFile *fin2 = new TFile(Form("output-%s-2c.root","MC"),"READ");
TFile *fin2 = new TFile(jp::dagfile1 ? "output-MC-1.root" : "output-MC-2b.root","READ");
assert(fin2 && !fin2->IsZombie());
TFile *fout = new TFile(Form("output-%s-3.root",type.c_str()),"RECREATE");
assert(fout && !fout->IsZombie());
bool ismc = jp::ismc;
recurseFile(fin, fin2, fout, ismc);
cout << "Output stored in " << fout->GetName() << endl;
fout->Close();
fout->Delete();
fin->Close();
fin->Delete();
}
void recurseFile(TDirectory *indir, TDirectory *indir2, TDirectory *outdir,
bool ismc) {
TDirectory *curdir = gDirectory;
// Automatically go through the list of keys (directories)
TList *keys = indir->GetListOfKeys();
TListIter itkey(keys);
TObject *key, *obj;
while ( (key = itkey.Next()) ) {
obj = ((TKey*)key)->ReadObj(); assert(obj);
// Found a subdirectory: copy it to output and go deeper
if (obj->InheritsFrom("TDirectory")) {
if (jp::debug) cout << key->GetName() << endl;
assert(outdir->mkdir(obj->GetName()));
outdir->mkdir(obj->GetName());
assert(outdir->cd(obj->GetName()));
TDirectory *outdir2 = outdir->GetDirectory(obj->GetName()); assert(outdir2);
outdir2->cd();
assert(indir->cd(obj->GetName()));
TDirectory *indir2a = indir->GetDirectory(obj->GetName()); assert(indir2a);
indir2a->cd();
if (indir2->cd(obj->GetName())) {
TDirectory *indir2b = indir2->GetDirectory(obj->GetName()); assert(indir2b);
recurseFile(indir2a, indir2b, outdir2, ismc);
}
} // inherits from TDirectory
// Found hpt plot: call unfolding routine
if (obj->InheritsFrom("TH1") &&
(string(obj->GetName())=="hpt" /* ||
string(obj->GetName())=="hpt_jet" ||
string(obj->GetName())=="hpt_jk1" ||
string(obj->GetName())=="hpt_jk2" ||
string(obj->GetName())=="hpt_jk3" ||
string(obj->GetName())=="hpt_jk4" ||
string(obj->GetName())=="hpt_jk5" ||
string(obj->GetName())=="hpt_jk6" ||
string(obj->GetName())=="hpt_jk7" ||
string(obj->GetName())=="hpt_jk8" ||
string(obj->GetName())=="hpt_jk9" ||
string(obj->GetName())=="hpt_jk10"*/
)) {
cout << "+" << flush;
_jk = 0;
if (TString(obj->GetName()).Contains("hpt_jk")) {
assert( sscanf(obj->GetName(), "hpt_jk%d", &_jk) == 1);
}
_jet = TString(obj->GetName()).Contains("hpt_jet");
TH1D *hpt = (TH1D*)obj;
//TH1D *hpt2 = (TH1D*)indir2->Get("hnlo"); assert(hpt2);
// TH1D *hpt2 = (TH1D*)indir2->Get(jp::dagfile1 ? "mc/hpt_g" : "hgpt"); assert(hpt2);
TH1D *hpt2 = (TH1D*)indir2->Get(jp::dagfile1 ? "mc/hpt_g" : "hpt"); assert(hpt2);
if (hpt2)
Unfold_histo(hpt, hpt2, outdir, ismc);
} // hpt
// Try to process friends similarly
/*
if (obj->InheritsFrom("TH1") &&
(string(obj->GetName())=="hpt_ak5calo")) {
cout << "-" << flush;
_jk = 0; _jet = false;
TH1D *hpt = (TH1D*)obj;
TH1D *hpt2 = (TH1D*)indir2->Get("hnlo"); assert(hpt2);
if (hpt2)
dagostiniUnfold_histo(hpt, hpt2, outdir, ismc, false, "_ak5calo");
} // hpt
*/
} // while key
curdir->cd();
} // recurseFile
void Unfold_histo(TH1D *hpt, TH1D *hnlo, TDirectory *outdir,
bool ismc, bool kscale, string id) {
float y1, y2;
assert(sscanf(outdir->GetName(),"Eta_%f-%f",&y1,&y2)==2);
sscanf(outdir->GetName(),"Eta_%f-%f",&y1,&y2);
cout << outdir->GetName() << " y1:" << y1 << " y2: " << y2 << endl;
const char *c = id.c_str();
if (_jk) c = Form("_jk%d",_jk);
if (_jet) c = "_jet";
_ismcjer = ismc;
// initial fit of the NLO curve to a histogram
TF1 *fnlo = new TF1(Form("fus%s",c),
// "[0]*exp([1]/x)*pow(x,[2])"
"[0]*pow(x,[1])"
"*pow(1-x*cosh([3])/[4],[2])", //10., 1000.);
jp::unfptminnlo, min(jp::xmax, jp::emax/cosh(y1)));
fnlo->SetParameters(5e10,-5.2,8.9,y1,jp::emax);
// fnlo->SetParameters(2e14*2e-10,-18,-5,10,y1,jp::emax);
fnlo->FixParameter(3,y1);
fnlo->FixParameter(4,jp::emax);
//hnlo->Fit(fnlo,"QRN");
//hnlo->Scale(2e-10); // TEMP PATCH
fnlo->SetRange(max(60.,jp::unfptminnlo), min(jp::xmax, jp::emax/cosh(y1)));
cout << "fit hnlo" << endl;
hnlo->Fit(fnlo,"RN"); // There seems to be abnormal terminations
fnlo->SetRange(jp::unfptminnlo, min(jp::xmax, jp::emax/cosh(y1)));
// Graph of theory points with centered bins
const double minerr = 0.02;
TGraphErrors *gnlo = new TGraphErrors(0);
TGraphErrors *gnlo2 = new TGraphErrors(0); // above + minerr
gnlo->SetName("gnlo");
gnlo2->SetName("gnlo2");
for (int i = 1; i != hnlo->GetNbinsX()+1; ++i) {
double y = hnlo->GetBinContent(i);
double dy = hnlo->GetBinError(i);
double ptmin = hnlo->GetBinLowEdge(i);
double ptmax = hnlo->GetBinLowEdge(i+1);
double y0 = fnlo->Integral(ptmin, ptmax) / (ptmax - ptmin);
double x = fnlo->GetX(y0, ptmin, ptmax);
int n = gnlo->GetN();
tools::SetPoint(gnlo, n, x, y, 0, dy);
tools::SetPoint(gnlo2, n, x, y, 0, tools::oplus(dy, minerr*y));
}
// Second fit to properly centered graph
//gnlo2->Fit(fnlo,"QRN");
fnlo->SetRange(max(60.,jp::unfptminnlo), min(jp::xmax, jp::emax/cosh(y1)));
cout << "fit to gnlo2" << endl;
gnlo2->Fit(fnlo,"RN");
fnlo->SetRange(jp::unfptminnlo, min(jp::xmax, jp::emax/cosh(y1)));
// Bin-centered data points
TGraphErrors *gpt = new TGraphErrors(0);
gpt->SetName(Form("gpt%s",c));
for (int i = 1; i != hpt->GetNbinsX()+1; ++i) {
double ptmin = hpt->GetBinLowEdge(i);
double ptmax = hpt->GetBinLowEdge(i+1);
double y = fnlo->Integral(ptmin, ptmax) / (ptmax - ptmin);
double x = fnlo->GetX(y, ptmin, ptmax);
double ym = hpt->GetBinContent(i);
double ym_err = hpt->GetBinError(i);
if (ym>0) {
tools::SetPoint(gpt, gpt->GetN(), x, ym, 0., ym_err);
}
} // for i
// Create smeared theory curve
double maxpt = jp::emax/cosh(y1);
cout << "y1 "<< y1 << " c "<< c <<endl<<flush;
TF1 *fnlos = new TF1(Form("fs%s",c),smearedAnsatz,jp::unfptminnlo,maxpt,nk+3);
fnlos->SetParameters(y1, fnlo->GetParameter(0), fnlo->GetParameter(1),
fnlo->GetParameter(2), 0, 0);
cout << "par0 "<< fnlos->GetParameter(0) << " y1 "<< y1<<endl<<flush;
// if (jp::debug)
cout << "Calculate forward smearing and unfold hpt" << endl << flush;
TGraphErrors *gfold_fwd = new TGraphErrors(0);
gfold_fwd->SetName(Form("gfold_fwd%s",c));
TGraphErrors *gcorrpt_fwd = new TGraphErrors(0);
gcorrpt_fwd->SetName(Form("gcorrpt_fwd%s",c));
TH1D *hcorrpt_fwd = (TH1D*)hpt->Clone(Form("hcorrpt_fwd%s",c));
for (int i = 0; i != gpt->GetN(); ++i) {
// for (int i = 1; i != gpt->GetN()+1; ++i) {
double x, y, ex, ey;
tools::GetPoint(gpt, i, x, y, ex, ey);
double k = fnlo->Eval(x)/fnlos->Eval(x); // fnlos causes a seg fault
if (!TMath::IsNaN(k)) {
tools::SetPoint(gfold_fwd, gfold_fwd->GetN(), x, k, ex, 0.);
tools::SetPoint(gcorrpt_fwd, gcorrpt_fwd->GetN(), x, k*y, ex, k*ey);
int j = hpt->FindBin(x);
hcorrpt_fwd->SetBinContent(j, k*hpt->GetBinContent(j));
hcorrpt_fwd->SetBinError(j, k*hpt->GetBinError(j));
}
}
cout << "Forward smearing done!" << endl;
outdir->cd();
// Save resolution function
TF1 *fres = new TF1(Form("fres%s",c), fPtRes, jp::xmin, jp::xmax, 1);
fres->SetParameter(0, y1);
// Store NLO ratio to (unsmeared) fit
TGraphErrors *grationlo = new TGraphErrors(0);
grationlo->SetName(Form("grationlo%s",c));
for (int i = 0; i != gnlo2->GetN(); ++i) {
double x, y, ex, ey;
tools::GetPoint(gnlo2, i, x, y, ex, ey);
double ys = fnlo->Eval(x);
if (!TMath::IsNaN(ys))
tools::SetPoint(grationlo, grationlo->GetN(), x, y / ys, ex, ey / ys);
}
// Store data ratio to (smeared) fit (this is also time consuming)
TGraphErrors *gratio = new TGraphErrors(0);
gratio->SetName(Form("gratio%s",c));
for (int i = 0; i != gpt->GetN(); ++i) {
double x, y, ex, ey;
tools::GetPoint(gpt, i, x, y, ex, ey);
double ys = fnlos->Eval(x);
if (!TMath::IsNaN(ys))
tools::SetPoint(gratio, gratio->GetN(), x, y / ys, ex, ey / ys);
}
outdir->cd();
cout << "Smeared ratio stored" << endl;
if (!_jk && !_jet) {
cout << "Saving plots" << endl;
// Inputs and central method results
hpt->Write("hpt");
hnlo->Write("hnlo");
gpt->Write("gpt");
gnlo2->Write("gnlo");
// Fit functions
//cout << "Saving fit functions" << endl;
//uResp->Write();
fnlo->Write();
// Calculating points for fs is taking significant time,
// and even got stuck at some point when too far out of the range
fnlos->SetRange(jp::unfptminnlo, min(jp::xmax, jp::emax/cosh(y1)));
// fnlos->SetNpx(1000); // otherwise ugly on log x-axis after write
fnlos->Write();
// Also calculating fnlos seems to take a lot of time when using effective JER...
fres->Write();
grationlo->Write();
gratio->Write();
gfold_fwd->Write();
gcorrpt_fwd->Write();
hcorrpt_fwd->Write();
cout << "Plots saved" << endl;
}
} //Unfold_histo