-
Notifications
You must be signed in to change notification settings - Fork 2
/
OHltPileupRateFitter.cpp
315 lines (267 loc) · 9.95 KB
/
OHltPileupRateFitter.cpp
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
#include <iostream>
#include <iomanip>
#include <fstream>
#include <TMath.h>
#include <TH1.h>
#include <TH2.h>
#include <TH3.h>
#include <TFile.h>
#include <TString.h>
#include <TF1.h>
#include <TProfile2D.h>
#include <TProfile.h>
#include <TLegend.h>
#include <TObjArray.h>
#include <TText.h>
#include <TVectorT.h>
#include <TGraph.h>
#include <TGraphErrors.h>
#include <TCanvas.h>
#include "OHltRatePrinter.h"
#include "OHltTree.h"
#include "OHltPileupRateFitter.h"
using namespace std;
void OHltPileupRateFitter::fitForPileup(
OHltConfig *thecfg,
OHltMenu *themenu,
vector < vector <double> > tRatePerLS,
vector<double> tTotalRatePerLS,
vector<double> tLumiPerLS,
std::vector< std::vector<double> > tCountPerLS,
std::vector<double> ttotalCountPerLS,
TFile *histogramfile)
{
// Individual rates, total rate, and inst lumi.
// At this point we've already applied all prescale, normalization, and
// linear extrapolation factors to the rates
RatePerLS = tRatePerLS;
totalRatePerLS = tTotalRatePerLS;
LumiPerLS = tLumiPerLS;
CountPerLS = tCountPerLS;
totalCountPerLS = ttotalCountPerLS;
vector <TGraphErrors*> vGraph;
int RunLSn = RatePerLS.size();
int nPaths = themenu->GetTriggerSize();
double targetLumi = thecfg->iLumi/1E30;
TString model = thecfg->nonlinearPileupFit;
int lsPerBin = thecfg->lumiBinsForPileupFit;
double minLumi = 999999999.0;
double maxLumi = 0.0;
double lumiMagicNumber = 1.0;
for (int iPath=0; iPath<nPaths; iPath++)
{
vector <double> vRates; //temp vector containing rates
vector <double> vRateErrors;
vector <double> vLumiErrors;
vector <double> vLumi;
for (int iLS=0; iLS<RunLSn; iLS++) {//looping over the entire set of data
double rate = 0;
double rateerr = 0;
double lumierr = 0.0;
double lumi = 0.0;
// Inst rate. Note here we've already applied the linear scale factor for the target lumi
// So cheat and uncorrect this back to the actual online rate before fitting
rate = (double) (RatePerLS[iLS][iPath]) / (thecfg->lumiScaleFactor);
rateerr = (double) rate * sqrt(CountPerLS[iLS][iPath]) / (CountPerLS[iLS][iPath]);
lumierr = 0.0;
lumi = lumiMagicNumber * LumiPerLS[iLS];
vLumi.push_back(lumi);
vRates.push_back(rate);
vRateErrors.push_back(rateerr);
vLumiErrors.push_back(lumierr);
}//end looping over the entire set of data
TGraphErrors* g = new TGraphErrors(RunLSn, &vLumi[0], &vRates[0], &vLumiErrors[0], &vRateErrors[0]);
g->SetTitle(themenu->GetTriggerName(iPath));
vGraph.push_back(g);
}//end looping over paths
// Now for total/PD rate
vector <double> vTotalRate; //temp vector containing rates
vector <double> vTotalRateError;
vector <double> vLumiError;
for (int iLS=0; iLS<RunLSn; iLS++)
{
double lumi = 0;
double rate = 0;
double rateerr = 0;
double lumierr = 0;
// Inst lumi
lumi = lumiMagicNumber * LumiPerLS[iLS];
if(lumi > maxLumi)
maxLumi = lumi;
if(lumi < minLumi)
minLumi = lumi;
// Inst rate. Note here we've already applied the linear scale factor for the target lumi
// So cheat and uncorrect this back to the actual online rate before fitting
rate = (double) (totalRatePerLS[iLS]) / (thecfg->lumiScaleFactor);
rateerr = (double) rate * sqrt(totalCountPerLS[iLS]) / (totalCountPerLS[iLS]);
lumierr = 0.0;
vTotalRate.push_back(rate);
vTotalRateError.push_back(rateerr);
vLumiError.push_back(lumierr);
} //end looping over the entire set of data
TGraphErrors* vTotalRateGraph = new TGraphErrors(RunLSn, &LumiPerLS[0], &vTotalRate[0], &vLumiError[0], &vTotalRateError[0]);
vTotalRateGraph->SetTitle("Total rate");
/* Rebin before fitting */
// Total rate
vector <double> vTotalRateRebinned; //temp vector containing rates
vector <double> vTotalRateRebinnedError;
vector <double> vTotalLumiRebinned;
vector <double> vTotalLumiRebinnedError;
int lsInBin = 0;
int rebinnedBins = RunLSn/(lsPerBin*1.0);
double lumiBin = 0;
double rateBin = 0;
double rateerrBin = 0;
double lumierrBin = 0;
double totalCountsBin = 0;
for (int iLS=0; iLS<RunLSn; iLS++)
{
// Inst lumi
lumiBin += LumiPerLS[iLS];
rateBin += (double) (totalRatePerLS[iLS]) / (thecfg->lumiScaleFactor);
totalCountsBin += totalCountPerLS[iLS];
lumierrBin = 0.0;
lsInBin++;
if(lsInBin == lsPerBin)
{
rateBin = 1.0 * rateBin/lsPerBin;
lumiBin = lumiMagicNumber * 1.0 * lumiBin/lsPerBin;
rateerrBin = (double) rateBin * sqrt(totalCountsBin) / (totalCountsBin);
vTotalRateRebinned.push_back(rateBin);
vTotalRateRebinnedError.push_back(rateerrBin);
vTotalLumiRebinned.push_back(lumiBin);
vTotalLumiRebinnedError.push_back(lumierrBin);
totalCountsBin=0;
rateBin=0;
rateerrBin=0;
lumiBin=0;
lumierrBin=0;
lsInBin = 0;
}
}
TGraphErrors* vTotalRebinnedRateGraph = new TGraphErrors(rebinnedBins,&vTotalLumiRebinned[0],
&vTotalRateRebinned[0], &vTotalLumiRebinnedError[0],
&vTotalRateRebinnedError[0]);
if (thecfg->isCounts) vTotalRebinnedRateGraph->SetTitle("Total count (rebinned)");
else vTotalRebinnedRateGraph->SetTitle("Total rate (rebinned)");
// Individual rates
vector <TGraphErrors*> vGraphRebinned;
for (int iPath=0; iPath<nPaths; iPath++)
{
vector <double> vRatesRebinned; //temp vector containing rates
vector <double> vRatesRebinnedError;
vector <double> vLumiRebinned;
vector <double> vLumiRebinnedError;
lsInBin = 0;
lumiBin = 0;
rateBin = 0;
rateerrBin = 0;
lumierrBin = 0;
totalCountsBin = 0;
for (int iLS=0; iLS<RunLSn; iLS++)
{
// Inst lumi
lumiBin += LumiPerLS[iLS];
rateBin += (double) (RatePerLS[iLS][iPath]) / (thecfg->lumiScaleFactor);
totalCountsBin += CountPerLS[iLS][iPath];
lumierrBin = 0.0;
lsInBin++;
if(lsInBin == lsPerBin)
{
rateBin = 1.0 * rateBin/lsPerBin;
lumiBin = lumiMagicNumber * 1.0 * lumiBin/lsPerBin;
rateerrBin = (double) rateBin * sqrt(totalCountsBin) / (totalCountsBin);
vRatesRebinned.push_back(rateBin);
vRatesRebinnedError.push_back(rateerrBin);
vLumiRebinned.push_back(lumiBin);
vLumiRebinnedError.push_back(lumierrBin);
totalCountsBin=0;
rateBin=0;
rateerrBin=0;
lumiBin=0;
lumierrBin=0;
lsInBin = 0;
}
}
TGraphErrors* g = new TGraphErrors(rebinnedBins, &vLumiRebinned[0], &vRatesRebinned[0], &vLumiRebinnedError[0], &vRatesRebinnedError[0]);
g->SetTitle(themenu->GetTriggerName(iPath));
vGraphRebinned.push_back(g);
}
// Fitting w/ quadratic and cubic
// User should check the Chi2/Ndof
TF1* fp1 = new TF1("fp1", model, 0, 9000);
int ix = TMath::Floor(sqrt(nPaths)); //Choose the proper canvas division
int iy = ix;
if (ix*iy==nPaths);
else if (sqrt(nPaths)*(iy+1)>nPaths) ++iy;
else {++ix; ++iy;}// end of canvas division
TCanvas* cIndividualRateFits = new TCanvas("cIndividualRateFits","cIndividualRateFits",0,0,1200,1000);
cIndividualRateFits->Divide(ix,iy);
cout.setf(ios::floatfield, ios::fixed);
cout<<setprecision(3);
cout << "\n";
if (thecfg->isCounts) {
cout << "Pileup corrected Trigger Counts, using " << model << " fit extrapolation to L=" << targetLumi << ": " << "\n";
}
else {
cout << "Pileup corrected Trigger Rates [Hz], using " << model << " fit extrapolation to L=" << targetLumi << ": " << "\n";
}
cout << "\t(Warning: always check fit qualities!)" << endl;
cout
<< " Name Indiv. Notes \n";
cout
<< "----------------------------------------------------------------------------------------------\n";
// Rate per path - rebinned
for (int jPath=0; jPath<nPaths; ++jPath)
{//looping over paths
cIndividualRateFits->cd(jPath+1);
vGraphRebinned.at(jPath)->SetMarkerColor(4);
vGraphRebinned.at(jPath)->SetMarkerStyle(20);
vGraphRebinned.at(jPath)->Draw("ap");
fp1->SetParLimits(2,0,1000000);
fp1->SetParLimits(3,0,1000000);
vGraphRebinned.at(jPath)->Fit("fp1","QR","",minLumi, maxLumi);
fp1->SetLineColor(2);
fp1->DrawCopy("same");
cout<<setw(50)<<themenu->GetTriggerName(jPath)<<" " <<setw(8)
<<setw(8)<<fp1->Eval(targetLumi)<<" " <<setw(8);
double pchi2 = TMath::Prob(fp1->GetChisquare(),fp1->GetNDF());
if(pchi2>0.01)
cout<<endl;
else
if((fp1->GetNDF())>0)
cout << " chi2/ndof = " << fp1->GetChisquare() << "/" << fp1->GetNDF() << endl;
else
cout << " chi2/ndof = 0/0" << endl;
}
// Total rate
cout
<< "----------------------------------------------------------------------------------------------\n";
TF1* fp2 = new TF1("fp2", model, 0, 9000);
TCanvas* cTotalRebinnedRateFit = new TCanvas("cTotalRebinnedRateFit","cTotalRebinnedRateFit",0,0,1200,800);
vTotalRebinnedRateGraph->SetMarkerColor(4);
vTotalRebinnedRateGraph->SetMarkerStyle(20);
vTotalRebinnedRateGraph->SetLineColor(4);
vTotalRebinnedRateGraph->SetLineWidth(3);
vTotalRebinnedRateGraph->Draw("ap");
fp2->SetParLimits(3,0.0,1.0);
vTotalRebinnedRateGraph->Fit("fp2","QR","",minLumi, maxLumi);
fp2->SetLineColor(2);
fp2->DrawCopy("same");
cout << "\n";
if (thecfg->isCounts) {
cout << setw(60) << "TOTAL COUNT (REBINNED): " << setw(5) << fp2->Eval(targetLumi);
}
else {
cout << setw(60) << "TOTAL RATE (REBINNED): " << setw(5) << fp2->Eval(targetLumi) << " Hz";
}
double pchi2totalrebinned = TMath::Prob(fp2->GetChisquare(),fp2->GetNDF());
if(pchi2totalrebinned>0.01)
cout<<endl;
else
cout << " chi2/ndof = " << fp2->GetChisquare() << "/" << fp2->GetNDF() << endl;
cout
<< "----------------------------------------------------------------------------------------------\n";
cIndividualRateFits->Write();
cTotalRebinnedRateFit->Write();
}