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
437
438
439
440
441
442
443
|
#include "Calibration/IsolatedParticles/interface/FindCaloHit.h"
#include "Calibration/IsolatedParticles/interface/FindEtaPhi.h"
#include "Calibration/IsolatedParticles/interface/MatrixHCALDetIds.h"
#include "DataFormats/HcalDetId/interface/HcalSubdetector.h"
#include <algorithm>
#include <iostream>
namespace spr {
template <typename T>
std::vector<std::pair<DetId, double> > eHCALmatrixCell(const HcalTopology* topology,
const DetId& det,
edm::Handle<T>& hits,
int ieta,
int iphi,
bool includeHO,
double hbThr,
double heThr,
double hfThr,
double hoThr,
bool debug) {
std::vector<DetId> dets(1, det);
std::vector<DetId> vdets = spr::matrixHCALIds(dets, topology, ieta, iphi, includeHO, debug);
return spr::energyDetIdHCAL(vdets, hits, hbThr, heThr, hfThr, hoThr, debug);
}
//*** Returns energy in NxN Hcal towers
template <typename T>
std::pair<double, int> eHCALmatrixTotal(const HcalTopology* topology,
const DetId& det0,
edm::Handle<T>& hits,
int ieta,
int iphi,
bool includeHO,
double hbThr,
double heThr,
double hfThr,
double hoThr,
bool debug) {
HcalDetId hcid0(det0.rawId());
HcalDetId hcid(hcid0.subdet(), hcid0.ieta(), hcid0.iphi(), 1);
DetId det(hcid.rawId());
if (debug)
edm::LogVerbatim("IsoTrack") << "Inside eHCALmatrixTotal " << ieta << "X" << iphi << " Inclusion of HO Flag "
<< includeHO;
spr::EtaPhi etaphi = spr::getEtaPhi(ieta, iphi, debug);
// Get maximum of all the trials
double energySum = 0;
int itrym = 0;
for (int itry = 0; itry < etaphi.ntrys; itry++) {
double energy = energyHCALmatrixTotal(topology,
det,
hits,
etaphi.ietaE[itry],
etaphi.ietaW[itry],
etaphi.iphiN[itry],
etaphi.iphiS[itry],
includeHO,
hbThr,
heThr,
hfThr,
hoThr,
debug);
if (energy > energySum) {
energySum = energy;
itrym = itry;
}
if (debug)
edm::LogVerbatim("IsoTrack") << "eHCALMatrix::Total energy " << energy << " Max " << energySum << "in trial # "
<< itrym;
}
return std::pair<double, int>(energySum, itrym);
}
//*** Returns vector of hits in 3x3 or 5x5 Hcal towers
template <typename T>
double energyHCALmatrix(const HcalTopology* topology,
const DetId& det0,
edm::Handle<T>& hits,
int ieta,
int iphi,
bool includeHO,
double hbThr,
double heThr,
double hfThr,
double hoThr,
bool debug) {
HcalDetId hcid0(det0.rawId());
HcalDetId hcid(hcid0.subdet(), hcid0.ieta(), hcid0.iphi(), 1);
DetId det(hcid.rawId());
if (debug)
edm::LogVerbatim("IsoTrack") << "Inside energyHCALmatrix " << 2 * ieta + 1 << "X" << 2 * iphi + 1
<< " Inclusion of HO Flag " << includeHO;
double energy = 0;
if (ieta > 2) {
edm::LogVerbatim("IsoTrack") << " no matrix > 5x5 is coded ! ";
return energy;
}
// used dets during building the matrix
std::vector<DetId> dets;
dets.clear();
// central tower
std::vector<DetId> vNeighboursDetId(1, det);
energy += energyHCAL(vNeighboursDetId, dets, topology, hits, includeHO, hbThr, heThr, hfThr, hoThr, debug);
if (ieta > 0) {
// go to east from central tower
vNeighboursDetId.clear();
if (debug)
edm::LogVerbatim("IsoTrack") << "hitHCALMatrix:: det " << (HcalDetId)det;
vNeighboursDetId = topology->east(det);
if (debug) {
std::ostringstream st1;
st1 << "Neighbour:: East";
for (unsigned int i = 0; i < vNeighboursDetId.size(); i++)
st1 << " " << (HcalDetId)vNeighboursDetId[i];
edm::LogVerbatim("IsoTrack") << st1.str();
}
energy += energyHCAL(vNeighboursDetId, dets, topology, hits, includeHO, hbThr, heThr, hfThr, hoThr, debug);
if (ieta == 2) {
for (int ii = 0; ii < (int)vNeighboursDetId.size(); ii++) {
std::vector<DetId> vNeighboursDetIdc = topology->east(vNeighboursDetId[ii]);
if (debug) {
std::ostringstream st1;
st1 << "Neighbour:: East";
for (unsigned int i = 0; i < vNeighboursDetIdc.size(); i++)
st1 << " " << (HcalDetId)vNeighboursDetIdc[i];
edm::LogVerbatim("IsoTrack") << st1.str();
}
energy += energyHCAL(vNeighboursDetIdc, dets, topology, hits, includeHO, hbThr, heThr, hfThr, hoThr, debug);
}
}
// go to west from central tower
vNeighboursDetId.clear();
vNeighboursDetId = topology->west(det);
if (debug) {
std::ostringstream st1;
st1 << "Neighbour:: West";
for (unsigned int i = 0; i < vNeighboursDetId.size(); i++)
st1 << " " << (HcalDetId)vNeighboursDetId[i];
edm::LogVerbatim("IsoTrack") << st1.str();
}
energy += energyHCAL(vNeighboursDetId, dets, topology, hits, includeHO, hbThr, heThr, hfThr, hoThr, debug);
if (ieta == 2) {
for (int ii = 0; ii < (int)vNeighboursDetId.size(); ii++) {
std::vector<DetId> vNeighboursDetIdc = topology->west(vNeighboursDetId[ii]);
if (debug) {
std::ostringstream st1;
st1 << "Neighbour:: West";
for (unsigned int i = 0; i < vNeighboursDetIdc.size(); i++)
st1 << " " << (HcalDetId)vNeighboursDetIdc[i];
edm::LogVerbatim("IsoTrack") << st1.str();
}
energy += energyHCAL(vNeighboursDetIdc, dets, topology, hits, includeHO, hbThr, heThr, hfThr, hoThr, debug);
}
}
}
// do steps in phi to north
DetId detnorth = det;
for (int inorth = 0; inorth < iphi; inorth++) {
if (debug)
edm::LogVerbatim("IsoTrack") << "hitHCALMatrix:: detNorth " << (HcalDetId)detnorth;
std::vector<DetId> NorthDetId = topology->north(detnorth);
if (debug) {
std::ostringstream st1;
st1 << "Neighbour:: North";
for (unsigned int i = 0; i < NorthDetId.size(); i++)
st1 << " " << (HcalDetId)NorthDetId[i];
edm::LogVerbatim("IsoTrack") << st1.str();
}
if (!(NorthDetId.empty())) {
energy += energyHCAL(NorthDetId, dets, topology, hits, includeHO, hbThr, heThr, hfThr, hoThr, debug);
if (ieta > 0) {
// go to east
vNeighboursDetId.clear();
vNeighboursDetId = topology->east(NorthDetId[0]);
if (debug) {
std::ostringstream st1;
st1 << "Neighbour:: East";
for (unsigned int i = 0; i < vNeighboursDetId.size(); i++)
st1 << " " << (HcalDetId)vNeighboursDetId[i];
edm::LogVerbatim("IsoTrack") << st1.str();
}
energy += energyHCAL(vNeighboursDetId, dets, topology, hits, includeHO, hbThr, heThr, hfThr, hoThr, debug);
if (ieta == 2) {
for (int ii = 0; ii < (int)vNeighboursDetId.size(); ii++) {
std::vector<DetId> vNeighboursDetIdc = topology->east(vNeighboursDetId[ii]);
if (debug) {
std::ostringstream st1;
st1 << "Neighbour:: East";
for (unsigned int i = 0; i < vNeighboursDetIdc.size(); i++)
st1 << " " << (HcalDetId)vNeighboursDetIdc[i];
edm::LogVerbatim("IsoTrack") << st1.str();
}
energy +=
energyHCAL(vNeighboursDetIdc, dets, topology, hits, includeHO, hbThr, heThr, hfThr, hoThr, debug);
}
}
// go to west
vNeighboursDetId.clear();
vNeighboursDetId = topology->west(NorthDetId[0]);
if (debug) {
std::ostringstream st1;
st1 << "Neighbour:: West";
for (unsigned int i = 0; i < vNeighboursDetId.size(); i++)
st1 << " " << (HcalDetId)vNeighboursDetId[i];
edm::LogVerbatim("IsoTrack") << st1.str();
}
energy += energyHCAL(vNeighboursDetId, dets, topology, hits, includeHO, hbThr, heThr, hfThr, hoThr, debug);
if (ieta == 2) {
for (int ii = 0; ii < (int)vNeighboursDetId.size(); ii++) {
std::vector<DetId> vNeighboursDetIdc = topology->west(vNeighboursDetId[ii]);
if (debug) {
std::ostringstream st1;
st1 << "Neighbour:: West";
for (unsigned int i = 0; i < vNeighboursDetIdc.size(); i++)
st1 << " " << (HcalDetId)vNeighboursDetIdc[i];
edm::LogVerbatim("IsoTrack") << st1.str();
}
energy +=
energyHCAL(vNeighboursDetIdc, dets, topology, hits, includeHO, hbThr, heThr, hfThr, hoThr, debug);
}
}
}
detnorth = NorthDetId[0];
} else {
break;
}
}
// do steps in phi to south
DetId detsouth = det;
for (int isouth = 0; isouth < iphi; isouth++) {
if (debug)
edm::LogVerbatim("IsoTrack") << "hitHCALMatrix:: detSouth " << (HcalDetId)detsouth;
vNeighboursDetId.clear();
std::vector<DetId> SouthDetId = topology->south(detsouth);
if (!(SouthDetId.empty())) {
energy += energyHCAL(SouthDetId, dets, topology, hits, includeHO, hbThr, heThr, hfThr, hoThr, debug);
if (debug) {
std::ostringstream st1;
st1 << "Neighbour:: South";
for (unsigned int i = 0; i < SouthDetId.size(); i++)
st1 << " " << (HcalDetId)SouthDetId[i];
edm::LogVerbatim("IsoTrack") << st1.str();
}
// go to east
if (ieta > 0) {
// go to east from central tower
vNeighboursDetId.clear();
vNeighboursDetId = topology->east(SouthDetId[0]);
if (debug) {
std::ostringstream st1;
st1 << "Neighbour:: East";
for (unsigned int i = 0; i < vNeighboursDetId.size(); i++)
st1 << " " << (HcalDetId)vNeighboursDetId[i];
edm::LogVerbatim("IsoTrack") << st1.str();
}
energy += energyHCAL(vNeighboursDetId, dets, topology, hits, includeHO, hbThr, heThr, hfThr, hoThr, debug);
if (ieta == 2) {
for (int ii = 0; ii < (int)vNeighboursDetId.size(); ii++) {
std::vector<DetId> vNeighboursDetIdc = topology->east(vNeighboursDetId[ii]);
if (debug) {
std::ostringstream st1;
st1 << "Neighbour:: East";
for (unsigned int i = 0; i < vNeighboursDetIdc.size(); i++)
st1 << " " << (HcalDetId)vNeighboursDetIdc[i];
edm::LogVerbatim("IsoTrack") << st1.str();
}
energy +=
energyHCAL(vNeighboursDetIdc, dets, topology, hits, includeHO, hbThr, heThr, hfThr, hoThr, debug);
}
}
// go to west
vNeighboursDetId.clear();
vNeighboursDetId = topology->west(SouthDetId[0]);
if (debug) {
std::ostringstream st1;
st1 << "Neighbour:: West";
for (unsigned int i = 0; i < vNeighboursDetId.size(); i++)
st1 << " " << (HcalDetId)vNeighboursDetId[i];
edm::LogVerbatim("IsoTrack") << st1.str();
}
energy += energyHCAL(vNeighboursDetId, dets, topology, hits, includeHO, hbThr, heThr, hfThr, hoThr, debug);
if (ieta == 2) {
for (int ii = 0; ii < (int)vNeighboursDetId.size(); ii++) {
std::vector<DetId> vNeighboursDetIdc = topology->west(vNeighboursDetId[ii]);
if (debug) {
std::ostringstream st1;
st1 << "Neighbour:: West";
for (unsigned int i = 0; i < vNeighboursDetIdc.size(); i++)
st1 << " " << (HcalDetId)vNeighboursDetIdc[i];
edm::LogVerbatim("IsoTrack") << st1.str();
}
energy +=
energyHCAL(vNeighboursDetIdc, dets, topology, hits, includeHO, hbThr, heThr, hfThr, hoThr, debug);
}
}
}
detsouth = SouthDetId[0];
} else {
break;
}
}
return energy;
}
template <typename T>
double energyHCAL(std::vector<DetId>& vNeighboursDetId,
std::vector<DetId>& dets,
const HcalTopology* topology,
edm::Handle<T>& hits,
bool includeHO,
double hbThr,
double heThr,
double hfThr,
double hoThr,
bool debug) {
double energySum = 0;
int kHit = 0;
for (int i = 0; i < (int)vNeighboursDetId.size(); i++) {
int n = std::count(dets.begin(), dets.end(), vNeighboursDetId[i]);
if (n != 0)
continue;
dets.push_back(vNeighboursDetId[i]);
std::vector<typename T::const_iterator> hit = spr::findHit(hits, vNeighboursDetId[i]);
double energy = 0;
int subdet = ((HcalDetId)(vNeighboursDetId[i].rawId())).subdet();
double eThr = hbThr;
if (subdet == (int)(HcalEndcap))
eThr = heThr;
else if (subdet == (int)(HcalForward))
eThr = hfThr;
else if (subdet == (int)(HcalOuter))
eThr = hoThr;
for (unsigned int ihit = 0; ihit < hit.size(); ihit++) {
if (hit[ihit] != hits->end()) {
energy += hit[ihit]->energy();
kHit++;
}
}
if (energy > eThr)
energySum += energy;
HcalDetId depth = vNeighboursDetId[i];
// max. three depths can be in endcap and we go to 2nd and 3rd from 1st where we are now
for (int idepth = 0; idepth < 2; idepth++) {
std::vector<DetId> vUpDetId = topology->up(depth);
if (!(vUpDetId.empty())) {
if (includeHO || vUpDetId[0].subdetId() != (int)(HcalOuter)) {
int n = std::count(dets.begin(), dets.end(), vUpDetId[0]);
if (n == 0) {
dets.push_back(vUpDetId[0]);
hit = spr::findHit(hits, vUpDetId[0]);
energy = 0;
subdet = ((HcalDetId)(vUpDetId[0].rawId())).subdet();
eThr = hbThr;
if (subdet == (int)(HcalEndcap))
eThr = heThr;
else if (subdet == (int)(HcalForward))
eThr = hfThr;
else if (subdet == (int)(HcalOuter))
eThr = hoThr;
for (unsigned int ihit = 0; ihit < hit.size(); ihit++) {
if (hit[ihit] != hits->end()) {
energy += hit[ihit]->energy();
kHit++;
}
}
if (energy > eThr)
energySum += energy;
}
}
depth = vUpDetId[0];
}
}
}
if (debug)
edm::LogVerbatim("IsoTrack") << "energyHCAL:: Energy " << energySum << " from " << kHit << " hits";
return energySum;
}
template <typename T>
std::vector<std::pair<DetId, double> > energyDetIdHCAL(std::vector<DetId>& vdets,
edm::Handle<T>& hits,
double hbThr,
double heThr,
double hfThr,
double hoThr,
bool debug) {
std::vector<std::pair<DetId, double> > energyDetId;
int khit = 0;
unsigned int maxsize = vdets.size();
for (unsigned int i = 0; i < maxsize; i++) {
std::vector<typename T::const_iterator> hit = spr::findHit(hits, vdets[i]);
double energy = 0;
bool ok = false;
for (unsigned int ihit = 0; ihit < hit.size(); ihit++) {
if (hit[ihit] != hits->end()) {
energy += hit[ihit]->energy();
khit++;
ok = true;
if (debug)
edm::LogVerbatim("IsoTrack") << "energyDetIdHCALneighbours:: Hit " << khit << " " << (HcalDetId)vdets[i]
<< " energy " << hit[ihit]->energy();
}
}
if (debug)
edm::LogVerbatim("IsoTrack") << "energyDetIdHCALneighbours::Detector " << i << " " << (HcalDetId)vdets[i]
<< " energy " << energy;
if (ok) {
int subdet = ((HcalDetId)(vdets[i].rawId())).subdet();
double eThr = hbThr;
if (subdet == (int)(HcalEndcap))
eThr = heThr;
else if (subdet == (int)(HcalForward))
eThr = hfThr;
else if (subdet == (int)(HcalOuter))
eThr = hoThr;
if (energy > eThr)
energyDetId.push_back(std::pair<DetId, double>(vdets[i], energy));
}
}
if (debug)
edm::LogVerbatim("IsoTrack") << "energyDetIdHCALneighbours::EnergyDetId buffer size " << energyDetId.size();
return energyDetId;
}
} // namespace spr
|