Repository navigation
Expand file tree
/
Copy pathRes3Backend.h
More file actions
436 lines (383 loc) · 15.2 KB
/
Copy pathRes3Backend.h
File metadata and controls
436 lines (383 loc) · 15.2 KB
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
427
428
429
430
431
432
433
434
435
436
#ifndef SROTHMAN_EEC_RES3_BACKEND_H
#define SROTHMAN_EEC_RES3_BACKEND_H
#include "Adjacency.h"
#include "SRothman/SimonTools/src/deltaR.h"
#include "SRothman/SimonTools/src/histutil.h"
#include "SRothman/SimonTools/src/util.h"
#include <memory>
#include <array>
template <class ResultType, bool distances_squared>
inline void get_indices_CA(const double dR12, const double dR13, const double dR23,
const double E1, const double eta1, const double phi1,
const double E2, const double eta2, const double phi2,
const double E3, const double eta3, const double phi3,
const double deta12, const double dphi12,
const double deta13, const double dphi13,
const double deta23, const double dphi23,
typename ResultType::T& R_idx,
typename ResultType::T& r_idx,
typename ResultType::T& c_idx,
const EEC::Res3Axes& axes) noexcept {
std::array<std::pair<double, unsigned>, 3> dRs = {
{std::make_pair(dR12, 0), std::make_pair(dR13, 1), std::make_pair(dR23, 2)}
};
if constexpr(distances_squared){
for(auto& dR : dRs){
dR.first = std::sqrt(dR.first);
}
}
std::sort(dRs.begin(), dRs.end(),
[](const std::pair<double, unsigned>& a, const std::pair<double, unsigned>& b){
return a.first > b.first;
}
);
if (dRs[0].first == 0){
R_idx = 0;
r_idx = 0;
c_idx = 0;
} else if (dRs[2].first == 0){
if constexpr(ResultType::SHOULD_BIN){
R_idx = simon::getIndex(dRs[0].first, axes.R);
} else {
R_idx = dRs[0].first;
}
r_idx = 0;
c_idx = 0;
} else {
double etaFar, phiFar;
double etaMid, phiMid;
double detaSmall, dphiSmall;
if (dRs[2].second == 0){//shortest leg is dR12
etaFar = eta3;
phiFar = phi3;
double Esum = E1 + E2;
etaMid = (E1*eta1 + E2*eta2) / Esum;
phiMid = (E1*phi1 + E2*phi2) / Esum;
detaSmall = deta12;
dphiSmall = dphi12;
} else if(dRs[2].second == 1){//shortest leg is dR13
etaFar = eta2;
phiFar = phi2;
double Esum = E1 + E3;
etaMid = (E1*eta1 + E3*eta3) / Esum;
phiMid = (E1*phi1 + E3*phi3) / Esum;
detaSmall = deta13;
dphiSmall = dphi13;
} else {//shortest leg is dR23
etaFar = eta1;
phiFar = phi1;
double Esum = E2 + E3;
etaMid = (E2*eta2 + E3*eta3) / Esum;
phiMid = (E2*phi2 + E3*phi3) / Esum;
detaSmall = deta23;
dphiSmall = dphi23;
}
double detaLong = simon::deltaEta(etaFar, etaMid);
double dphiLong = simon::deltaPhi(phiFar, phiMid);
double R = std::sqrt(simon::square(detaLong) + simon::square(dphiLong));
double r = dRs[2].first / R;
double dot = (detaLong*detaSmall + dphiLong*dphiSmall) / (R * dRs[2].first);
if (dot > 1){
dot = 1;
} else if (dot < -1){
dot = -1;
}
double c = std::acos(dot);
if (c > M_PI/2){
c = M_PI - c;
}
if constexpr(ResultType::SHOULD_BIN){
R_idx = simon::getIndex(R, axes.R);
r_idx = simon::getIndex(r, axes.r);
c_idx = simon::getIndex(c, axes.c);
} else {
R_idx = R;
r_idx = r;
c_idx = c;
}
}
}
template <class ResultType, bool distances_squared>
inline void get_indices(const double dR12, const double dR13, const double dR23,
typename ResultType::T& R_idx,
typename ResultType::T& r_idx,
typename ResultType::T& c_idx,
const EEC::Res3Axes& axes) noexcept {
std::array<double, 3> dRs = {{dR12, dR13, dR23}};
if constexpr(distances_squared){
for(auto& dR : dRs){
dR = std::sqrt(dR);
}
}
std::sort(dRs.begin(), dRs.end(),
[](const double a, const double b){return a > b;});
if (dRs[0] == 0){
R_idx = 0;
r_idx = 0;
c_idx = 0;
} else if (dRs[2] == 0){
if constexpr(ResultType::SHOULD_BIN){
R_idx = simon::getIndex(dRs[0], axes.R);
} else {
R_idx = dRs[0];
}
r_idx = 0;
c_idx = 0;
} else {
double R = dRs[0];
double r = dRs[2]/dRs[1];
double c = std::asin(std::sqrt(1 - simon::square((dRs[0] - dRs[1])/dRs[2])));
if constexpr(ResultType::SHOULD_BIN){
R_idx = simon::getIndex(R, axes.R);
r_idx = simon::getIndex(r, axes.r);
c_idx = simon::getIndex(c, axes.c);
} else {
R_idx = R;
r_idx = r;
c_idx = c;
}
}
}
template <bool isCA, class JetType, class TransferResultType>
inline void res3_transferloop(
TransferResultType& transfer,
const std::shared_ptr<const JetType> thisjet_reco,
const EEC::Res3Axes& axes_reco,
const typename TransferResultType::T R_idx_gen,
const typename TransferResultType::T r_idx_gen,
const typename TransferResultType::T c_idx_gen,
const EEC::neighborhood& n1,
const EEC::neighborhood& n2,
const EEC::neighborhood& n3,
const double wt_gen) noexcept {
for(const EEC::neighbor& j1 : n1){
const auto& p1 = thisjet_reco->singles.get(j1.idx);
const double twt1 = wt_gen * j1.wt;
for(const EEC::neighbor& j2 : n2){
const auto& p2 = thisjet_reco->singles.get(j2.idx);
const double twt2 = twt1 * j2.wt;
const auto& pair12 = thisjet_reco->pairs.get(j1.idx, j2.idx);
for(const EEC::neighbor& j3 : n3){
const auto& p3 = thisjet_reco->singles.get(j3.idx);
const double twt3 = twt2 * j3.wt;
const auto& pair13 = thisjet_reco->pairs.get(j1.idx, j3.idx);
const auto& pair23 = thisjet_reco->pairs.get(j2.idx, j3.idx);
typename TransferResultType::T R_idx_reco, r_idx_reco, c_idx_reco;
if constexpr (isCA){
get_indices_CA<TransferResultType, JetType::pairType::distances_squared>(
pair12.floatDR, pair13.floatDR, pair23.floatDR,
p1.pt, p1.eta, p1.phi,
p2.pt, p2.eta, p2.phi,
p3.pt, p3.eta, p3.phi,
pair12.deta, pair12.dphi,
pair13.deta, pair13.dphi,
pair23.deta, pair23.dphi,
R_idx_reco, r_idx_reco, c_idx_reco,
axes_reco);
} else {
get_indices<TransferResultType, JetType::pairType::distances_squared>(
pair12.floatDR, pair13.floatDR, pair23.floatDR,
R_idx_reco, r_idx_reco, c_idx_reco,
axes_reco);
}
#ifdef CHECK_BY_HAND
printf("(%g, %g), (%g, %g), (%g, %g): [transfer]\n",
p1.eta, p1.phi, p2.eta, p2.phi, p3.eta, p3.phi);
printf("\tE1*E2*E3 = %g\n", p1.pt*p2.pt*p3.pt);
std::cout << "\tR_idx = " << R_idx_reco << std::endl;
std::cout << "\tc_idx = " << c_idx_reco << std::endl;
printf("\tE1 = %g, E2 = %g, E3 = %g\n", p1.pt, p2.pt, p3.pt);
#endif
transfer.fill(R_idx_reco, r_idx_reco, c_idx_reco,
R_idx_gen, r_idx_gen, c_idx_gen,
twt3, wt_gen);
}
}
}
}
template <bool isCA, class ResultType, class JetType, bool doUnmatched, class TransferResultType, bool doTransfer>
inline void res3_mainloop(
ResultType& result,
[[maybe_unused]] ResultType* unmatched_gen,
[[maybe_unused]] TransferResultType* transfer,
[[maybe_unused]] const std::shared_ptr<const JetType> thisjet_reco,
const std::shared_ptr<const JetType> thisjet_gen,
[[maybe_unused]] const std::vector<bool>* const matched,
[[maybe_unused]] const std::shared_ptr<const EEC::Adjacency> adj,
[[maybe_unused]] const EEC::Res3Axes* const axes_reco,
const EEC::Res3Axes& axes_gen) noexcept {
for(unsigned i1=0; i1<thisjet_gen->N; ++i1){
const auto& p1 = thisjet_gen->singles.get(i1);
[[maybe_unused]] bool matched1;
if constexpr(doUnmatched){
matched1 = matched->at(i1);
}
[[maybe_unused]] EEC::neighborhood const * n1 = nullptr;
if constexpr(doTransfer){
n1 = &(adj->get_neighborhood(i1));
}
/*
* One-particle part
* This has symmetry factor 1
* And coordinates (R, r, c) = (0, 0, 0)
*/
double wt = p1.pt*p1.pt*p1.pt;
#ifdef CHECK_BY_HAND
printf("(%g, %g): [1-particle]\n", p1.eta, p1.phi);
printf("\tE1^3 = %g\n", p1.pt*p1.pt*p1.pt);
printf("\tR_idx = 0\n");
printf("\tr_idx = 0\n");
printf("\tc_idx = 0\n");
if constexpr(doUnmatched){
printf("\tmatched = %d\n", matched1);
}
#endif
result.fill(0, 0, 0, wt);
if constexpr(doUnmatched){
if(!matched1){
unmatched_gen->fill(0, 0, 0, wt);
}
}
if constexpr(doTransfer){
res3_transferloop<isCA>(
*transfer,
thisjet_reco,
*axes_reco,
0, 0, 0,
*n1,
*n1,
*n1,
wt);
}
for(unsigned i2=i1+1; i2<thisjet_gen->N; ++i2){
const auto& p2 = thisjet_gen->singles.get(i2);
[[maybe_unused]] bool matched2;
if constexpr(doUnmatched){
matched2 = matched1 && matched->at(i2);
}
[[maybe_unused]] EEC::neighborhood const * n2 = nullptr;
if constexpr(doTransfer){
n2 = &(adj->get_neighborhood(i2));
}
const double E12 = p1.pt*p2.pt;
const auto& pair12 = thisjet_gen->pairs.get(i1, i2);
/*
* Two-particle part
* This has symmetry factor 3!/2! = 3
* And RL = R12, r=c=0
*/
typename ResultType::T R_idx;
if constexpr(JetType::pairType::distances_squared){
R_idx = simon::getIndex(std::sqrt(pair12.floatDR), axes_gen.R);
} else {
R_idx = simon::getIndex(pair12.floatDR, axes_gen.R);
}
wt = 3 * E12 * (p1.pt + p2.pt);
result.fill(R_idx, 0, 0, wt);
#ifdef CHECK_BY_HAND
printf("(%g, %g), (%g, %g): [2-particle]\n", p1.eta, p1.phi, p2.eta, p2.phi);
printf("\tE1*E2*(E1+E2) = %g\n", E12*(p1.pt+p2.pt));
std::cout << "\tR = " << R_idx << std::endl;
printf("\tr_idx = 0\n");
printf("\tc_idx = 0\n");
if constexpr(doUnmatched){
printf("\tmatched = %d\n", matched2);
}
#endif
if constexpr(doUnmatched){
if(!matched2){
unmatched_gen->fill(R_idx, 0, 0, wt);
}
}
if constexpr(doTransfer){
double wt1 = 3*E12*p1.pt;
res3_transferloop<isCA>(
*transfer,
thisjet_reco,
*axes_reco,
R_idx, 0, 0,
*n1,
*n1,
*n2,
wt1);
double wt2 = 3*E12*p2.pt;
res3_transferloop<isCA>(
*transfer,
thisjet_reco,
*axes_reco,
R_idx, 0, 0,
*n1,
*n2,
*n2,
wt2);
}
for(unsigned i3=i2+1; i3<thisjet_gen->N; ++i3){
const auto& p3 = thisjet_gen->singles.get(i3);
[[maybe_unused]] bool matched3;
if constexpr(doUnmatched){
matched3 = matched2 && matched->at(i3);
}
[[maybe_unused]] EEC::neighborhood const * n3 = nullptr;
if constexpr(doTransfer){
n3 = &(adj->get_neighborhood(i3));
}
const double E123 = E12*p3.pt;
const auto& pair13 = thisjet_gen->pairs.get(i1, i3);
const auto& pair23 = thisjet_gen->pairs.get(i2, i3);
/*
* Three-particle part
* This has symmetry factor 3! = 6
* and full coordinates
*/
typename ResultType::T r_idx, c_idx;
if constexpr (isCA){
get_indices_CA<ResultType, JetType::pairType::distances_squared>(
pair12.floatDR, pair13.floatDR, pair23.floatDR,
p1.pt, p1.eta, p1.phi,
p2.pt, p2.eta, p2.phi,
p3.pt, p3.eta, p3.phi,
pair12.deta, pair12.dphi,
pair13.deta, pair13.dphi,
pair23.deta, pair23.dphi,
R_idx, r_idx, c_idx,
axes_gen);
} else {
get_indices<ResultType, JetType::pairType::distances_squared>(
pair12.floatDR, pair13.floatDR, pair23.floatDR,
R_idx, r_idx, c_idx,
axes_gen);
}
#ifdef CHECK_BY_HAND
printf("(%g, %g), (%g, %g), (%g, %g): [3-particle]\n",
p1.eta, p1.phi, p2.eta, p2.phi, p3.eta, p3.phi);
printf("\tE1*E2*E3 = %g\n", p1.pt*p2.pt*p3.pt);
std::cout << "\tR_idx = " << R_idx << std::endl;
std::cout << "\tr_idx = " << r_idx << std::endl;
std::cout << "\tc_idx = " << c_idx << std::endl;
if constexpr(doUnmatched){
printf("\tmatched = %d\n", matched3);
}
#endif
wt = E123*6;
result.fill(R_idx, r_idx, c_idx, wt);
if constexpr(doUnmatched){
if(!matched3){
unmatched_gen->fill(R_idx, r_idx, c_idx, wt);
}
}
if constexpr(doTransfer){
res3_transferloop<isCA>(
*transfer,
thisjet_reco,
*axes_reco,
R_idx, r_idx, c_idx,
*n1,
*n2,
*n3,
wt);
}
}
}
}
}
#endif