-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
L2MuonSeedGenerator.cc
404 lines (338 loc) · 16.3 KB
/
L2MuonSeedGenerator.cc
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
//-------------------------------------------------
//
/** \class L2MuonSeedGenerator
*
* L2 muon seed generator:
* Transform the L1 informations in seeds for the
* L2 muon reconstruction
*
*
*
* \author A.Everett, R.Bellan, J. Alcaraz
*
* ORCA's author: N. Neumeister
*/
//
//--------------------------------------------------
// Class Header
#include "RecoMuon/L2MuonSeedGenerator/src/L2MuonSeedGenerator.h"
// Framework
#include "FWCore/Framework/interface/ConsumesCollector.h"
#include "FWCore/Framework/interface/EventSetup.h"
#include "FWCore/Framework/interface/Event.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "FWCore/MessageLogger/interface/MessageLogger.h"
#include "TrackingTools/TrajectoryParametrization/interface/GlobalTrajectoryParameters.h"
#include "TrackingTools/TrajectoryParametrization/interface/CurvilinearTrajectoryError.h"
#include "TrackingTools/TrajectoryState/interface/TrajectoryStateOnSurface.h"
#include "TrackingTools/TrajectoryState/interface/FreeTrajectoryState.h"
#include "TrackingTools/TrajectoryState/interface/TrajectoryStateTransform.h"
#include "TrackingTools/KalmanUpdators/interface/Chi2MeasurementEstimator.h"
#include "TrackingTools/DetLayers/interface/DetLayer.h"
#include "RecoMuon/TrackingTools/interface/MuonServiceProxy.h"
#include "RecoMuon/TrackingTools/interface/MuonPatternRecoDumper.h"
using namespace std;
using namespace edm;
using namespace l1extra;
// constructors
L2MuonSeedGenerator::L2MuonSeedGenerator(const edm::ParameterSet& iConfig)
: theSource(iConfig.getParameter<InputTag>("InputObjects")),
theL1GMTReadoutCollection(iConfig.getParameter<InputTag>("GMTReadoutCollection")),
thePropagatorName(iConfig.getParameter<string>("Propagator")),
theL1MinPt(iConfig.getParameter<double>("L1MinPt")),
theL1MaxEta(iConfig.getParameter<double>("L1MaxEta")),
theL1MinQuality(iConfig.getParameter<unsigned int>("L1MinQuality")),
useOfflineSeed(iConfig.getUntrackedParameter<bool>("UseOfflineSeed", false)),
useUnassociatedL1(iConfig.existsAs<bool>("UseUnassociatedL1") ? iConfig.getParameter<bool>("UseUnassociatedL1")
: true) {
gmtToken_ = consumes<L1MuGMTReadoutCollection>(theL1GMTReadoutCollection);
muCollToken_ = consumes<L1MuonParticleCollection>(theSource);
if (useOfflineSeed) {
theOfflineSeedLabel = iConfig.getUntrackedParameter<InputTag>("OfflineSeedLabel");
offlineSeedToken_ = consumes<edm::View<TrajectorySeed> >(theOfflineSeedLabel);
}
// service parameters
ParameterSet serviceParameters = iConfig.getParameter<ParameterSet>("ServiceParameters");
// the services
theService = new MuonServiceProxy(serviceParameters, consumesCollector());
// the estimator
theEstimator = new Chi2MeasurementEstimator(10000.);
produces<L2MuonTrajectorySeedCollection>();
}
// destructor
L2MuonSeedGenerator::~L2MuonSeedGenerator() {
if (theService)
delete theService;
if (theEstimator)
delete theEstimator;
}
void L2MuonSeedGenerator::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) {
const std::string metname = "Muon|RecoMuon|L2MuonSeedGenerator";
MuonPatternRecoDumper debug;
auto output = std::make_unique<L2MuonTrajectorySeedCollection>();
// Muon particles and GMT readout collection
edm::Handle<L1MuGMTReadoutCollection> gmtrc_handle;
iEvent.getByToken(gmtToken_, gmtrc_handle);
L1MuGMTReadoutRecord const& gmtrr = gmtrc_handle.product()->getRecord(0);
edm::Handle<L1MuonParticleCollection> muColl;
iEvent.getByToken(muCollToken_, muColl);
LogTrace(metname) << "Number of muons " << muColl->size() << endl;
edm::Handle<edm::View<TrajectorySeed> > offlineSeedHandle;
vector<int> offlineSeedMap;
if (useOfflineSeed) {
iEvent.getByToken(offlineSeedToken_, offlineSeedHandle);
LogTrace(metname) << "Number of offline seeds " << offlineSeedHandle->size() << endl;
offlineSeedMap = vector<int>(offlineSeedHandle->size(), 0);
}
L1MuonParticleCollection::const_iterator it;
L1MuonParticleRef::key_type l1ParticleIndex = 0;
for (it = muColl->begin(); it != muColl->end(); ++it, ++l1ParticleIndex) {
const L1MuGMTExtendedCand muonCand = (*it).gmtMuonCand();
unsigned int quality = 0;
bool valid_charge = false;
;
if (muonCand.empty()) {
LogWarning(metname) << "L2MuonSeedGenerator: WARNING, no L1MuGMTCand! " << endl;
LogWarning(metname) << "L2MuonSeedGenerator: this should make sense only within MC tests" << endl;
// FIXME! Temporary to handle the MC input
quality = 7;
valid_charge = true;
} else {
quality = muonCand.quality();
valid_charge = muonCand.charge_valid();
}
float pt = (*it).pt();
float eta = (*it).eta();
float theta = 2 * atan(exp(-eta));
float phi = (*it).phi();
int charge = (*it).charge();
// Set charge=0 for the time being if the valid charge bit is zero
if (!valid_charge)
charge = 0;
bool barrel = !(*it).isForward();
// Get a better eta and charge from regional information
// Phi has the same resolution in GMT than regionally, is not it?
if (!(muonCand.empty())) {
int idx = -1;
vector<L1MuRegionalCand> rmc;
if (!muonCand.isRPC()) {
idx = muonCand.getDTCSCIndex();
if (muonCand.isFwd())
rmc = gmtrr.getCSCCands();
else
rmc = gmtrr.getDTBXCands();
} else {
idx = muonCand.getRPCIndex();
if (muonCand.isFwd())
rmc = gmtrr.getFwdRPCCands();
else
rmc = gmtrr.getBrlRPCCands();
}
if (idx >= 0) {
eta = rmc[idx].etaValue();
//phi = rmc[idx].phiValue();
// Use this charge if the valid charge bit is zero
if (!valid_charge)
charge = rmc[idx].chargeValue();
}
}
if (pt < theL1MinPt || fabs(eta) > theL1MaxEta)
continue;
LogTrace(metname) << "New L2 Muon Seed";
LogTrace(metname) << "Pt = " << pt << " GeV/c";
LogTrace(metname) << "eta = " << eta;
LogTrace(metname) << "theta = " << theta << " rad";
LogTrace(metname) << "phi = " << phi << " rad";
LogTrace(metname) << "charge = " << charge;
LogTrace(metname) << "In Barrel? = " << barrel;
if (quality <= theL1MinQuality)
continue;
LogTrace(metname) << "quality = " << quality;
// Update the services
theService->update(iSetup);
const DetLayer* detLayer = nullptr;
float radius = 0.;
CLHEP::Hep3Vector vec(0., 1., 0.);
vec.setTheta(theta);
vec.setPhi(phi);
// Get the det layer on which the state should be put
if (barrel) {
LogTrace(metname) << "The seed is in the barrel";
// MB2
DetId id = DTChamberId(0, 2, 0);
detLayer = theService->detLayerGeometry()->idToLayer(id);
LogTrace(metname) << "L2 Layer: " << debug.dumpLayer(detLayer);
const BoundSurface* sur = &(detLayer->surface());
const BoundCylinder* bc = dynamic_cast<const BoundCylinder*>(sur);
radius = fabs(bc->radius() / sin(theta));
LogTrace(metname) << "radius " << radius;
if (pt < 3.5)
pt = 3.5;
} else {
LogTrace(metname) << "The seed is in the endcap";
DetId id;
// ME2
if (theta < Geom::pi() / 2.)
id = CSCDetId(1, 2, 0, 0, 0);
else
id = CSCDetId(2, 2, 0, 0, 0);
detLayer = theService->detLayerGeometry()->idToLayer(id);
LogTrace(metname) << "L2 Layer: " << debug.dumpLayer(detLayer);
radius = fabs(detLayer->position().z() / cos(theta));
if (pt < 1.0)
pt = 1.0;
}
vec.setMag(radius);
GlobalPoint pos(vec.x(), vec.y(), vec.z());
GlobalVector mom(pt * cos(phi), pt * sin(phi), pt * cos(theta) / sin(theta));
GlobalTrajectoryParameters param(pos, mom, charge, &*theService->magneticField());
AlgebraicSymMatrix55 mat;
mat[0][0] = (0.25 / pt) * (0.25 / pt); // sigma^2(charge/abs_momentum)
if (!barrel)
mat[0][0] = (0.4 / pt) * (0.4 / pt);
//Assign q/pt = 0 +- 1/pt if charge has been declared invalid
if (!valid_charge)
mat[0][0] = (1. / pt) * (1. / pt);
mat[1][1] = 0.05 * 0.05; // sigma^2(lambda)
mat[2][2] = 0.2 * 0.2; // sigma^2(phi)
mat[3][3] = 20. * 20.; // sigma^2(x_transverse))
mat[4][4] = 20. * 20.; // sigma^2(y_transverse))
CurvilinearTrajectoryError error(mat);
const FreeTrajectoryState state(param, error);
LogTrace(metname) << "Free trajectory State from the parameters";
LogTrace(metname) << debug.dumpFTS(state);
// Propagate the state on the MB2/ME2 surface
TrajectoryStateOnSurface tsos = theService->propagator(thePropagatorName)->propagate(state, detLayer->surface());
LogTrace(metname) << "State after the propagation on the layer";
LogTrace(metname) << debug.dumpLayer(detLayer);
LogTrace(metname) << debug.dumpFTS(state);
if (tsos.isValid()) {
// Get the compatible dets on the layer
std::vector<pair<const GeomDet*, TrajectoryStateOnSurface> > detsWithStates =
detLayer->compatibleDets(tsos, *theService->propagator(thePropagatorName), *theEstimator);
if (!detsWithStates.empty()) {
TrajectoryStateOnSurface newTSOS = detsWithStates.front().second;
const GeomDet* newTSOSDet = detsWithStates.front().first;
LogTrace(metname) << "Most compatible det";
LogTrace(metname) << debug.dumpMuonId(newTSOSDet->geographicalId());
LogDebug(metname) << "L1 info: Det and State:";
LogDebug(metname) << debug.dumpMuonId(newTSOSDet->geographicalId());
if (newTSOS.isValid()) {
//LogDebug(metname) << "(x, y, z) = (" << newTSOS.globalPosition().x() << ", "
// << newTSOS.globalPosition().y() << ", " << newTSOS.globalPosition().z() << ")";
LogDebug(metname) << "pos: (r=" << newTSOS.globalPosition().mag()
<< ", phi=" << newTSOS.globalPosition().phi() << ", eta=" << newTSOS.globalPosition().eta()
<< ")";
LogDebug(metname) << "mom: (q*pt=" << newTSOS.charge() * newTSOS.globalMomentum().perp()
<< ", phi=" << newTSOS.globalMomentum().phi() << ", eta=" << newTSOS.globalMomentum().eta()
<< ")";
//LogDebug(metname) << "State on it";
//LogDebug(metname) << debug.dumpTSOS(newTSOS);
//PTrajectoryStateOnDet seedTSOS;
edm::OwnVector<TrackingRecHit> container;
if (useOfflineSeed) {
const TrajectorySeed* assoOffseed = associateOfflineSeedToL1(offlineSeedHandle, offlineSeedMap, newTSOS);
if (assoOffseed != nullptr) {
PTrajectoryStateOnDet const& seedTSOS = assoOffseed->startingState();
for (auto const& tsci : assoOffseed->recHits()) {
container.push_back(tsci);
}
output->push_back(
L2MuonTrajectorySeed(seedTSOS, container, alongMomentum, L1MuonParticleRef(muColl, l1ParticleIndex)));
} else {
if (useUnassociatedL1) {
// convert the TSOS into a PTSOD
PTrajectoryStateOnDet const& seedTSOS =
trajectoryStateTransform::persistentState(newTSOS, newTSOSDet->geographicalId().rawId());
output->push_back(L2MuonTrajectorySeed(
seedTSOS, container, alongMomentum, L1MuonParticleRef(muColl, l1ParticleIndex)));
}
}
} else {
// convert the TSOS into a PTSOD
PTrajectoryStateOnDet const& seedTSOS =
trajectoryStateTransform::persistentState(newTSOS, newTSOSDet->geographicalId().rawId());
output->push_back(
L2MuonTrajectorySeed(seedTSOS, container, alongMomentum, L1MuonParticleRef(muColl, l1ParticleIndex)));
}
}
}
}
}
iEvent.put(std::move(output));
}
// FIXME: does not resolve ambiguities yet!
const TrajectorySeed* L2MuonSeedGenerator::associateOfflineSeedToL1(edm::Handle<edm::View<TrajectorySeed> >& offseeds,
std::vector<int>& offseedMap,
TrajectoryStateOnSurface& newTsos) {
const std::string metlabel = "Muon|RecoMuon|L2MuonSeedGenerator";
MuonPatternRecoDumper debugtmp;
edm::View<TrajectorySeed>::const_iterator offseed, endOffseed = offseeds->end();
const TrajectorySeed* selOffseed = nullptr;
double bestDr = 99999.;
unsigned int nOffseed(0);
int lastOffseed(-1);
for (offseed = offseeds->begin(); offseed != endOffseed; ++offseed, ++nOffseed) {
if (offseedMap[nOffseed] != 0)
continue;
GlobalPoint glbPos = theService->trackingGeometry()
->idToDet(offseed->startingState().detId())
->surface()
.toGlobal(offseed->startingState().parameters().position());
GlobalVector glbMom = theService->trackingGeometry()
->idToDet(offseed->startingState().detId())
->surface()
.toGlobal(offseed->startingState().parameters().momentum());
// Preliminary check
double preDr = deltaR(newTsos.globalPosition().eta(), newTsos.globalPosition().phi(), glbPos.eta(), glbPos.phi());
if (preDr > 1.0)
continue;
const FreeTrajectoryState offseedFTS(
glbPos, glbMom, offseed->startingState().parameters().charge(), &*theService->magneticField());
TrajectoryStateOnSurface offseedTsos =
theService->propagator(thePropagatorName)->propagate(offseedFTS, newTsos.surface());
LogDebug(metlabel) << "Offline seed info: Det and State" << std::endl;
LogDebug(metlabel) << debugtmp.dumpMuonId(offseed->startingState().detId()) << std::endl;
//LogDebug(metlabel) << "(x, y, z) = (" << newTSOS.globalPosition().x() << ", "
// << newTSOS.globalPosition().y() << ", " << newTSOS.globalPosition().z() << ")" << std::endl;
LogDebug(metlabel) << "pos: (r=" << offseedFTS.position().mag() << ", phi=" << offseedFTS.position().phi()
<< ", eta=" << offseedFTS.position().eta() << ")" << std::endl;
LogDebug(metlabel) << "mom: (q*pt=" << offseedFTS.charge() * offseedFTS.momentum().perp()
<< ", phi=" << offseedFTS.momentum().phi() << ", eta=" << offseedFTS.momentum().eta() << ")"
<< std::endl
<< std::endl;
//LogDebug(metlabel) << debugtmp.dumpFTS(offseedFTS) << std::endl;
if (offseedTsos.isValid()) {
LogDebug(metlabel) << "Offline seed info after propagation to L1 layer:" << std::endl;
//LogDebug(metlabel) << "(x, y, z) = (" << offseedTsos.globalPosition().x() << ", "
// << offseedTsos.globalPosition().y() << ", " << offseedTsos.globalPosition().z() << ")" << std::endl;
LogDebug(metlabel) << "pos: (r=" << offseedTsos.globalPosition().mag()
<< ", phi=" << offseedTsos.globalPosition().phi()
<< ", eta=" << offseedTsos.globalPosition().eta() << ")" << std::endl;
LogDebug(metlabel) << "mom: (q*pt=" << offseedTsos.charge() * offseedTsos.globalMomentum().perp()
<< ", phi=" << offseedTsos.globalMomentum().phi()
<< ", eta=" << offseedTsos.globalMomentum().eta() << ")" << std::endl
<< std::endl;
//LogDebug(metlabel) << debugtmp.dumpTSOS(offseedTsos) << std::endl;
double newDr = deltaR(newTsos.globalPosition().eta(),
newTsos.globalPosition().phi(),
offseedTsos.globalPosition().eta(),
offseedTsos.globalPosition().phi());
LogDebug(metlabel) << " -- DR = " << newDr << std::endl;
if (newDr < 0.3 && newDr < bestDr) {
LogDebug(metlabel) << " --> OK! " << newDr << std::endl << std::endl;
selOffseed = &*offseed;
bestDr = newDr;
offseedMap[nOffseed] = 1;
if (lastOffseed > -1)
offseedMap[lastOffseed] = 0;
lastOffseed = nOffseed;
} else {
LogDebug(metlabel) << " --> Rejected. " << newDr << std::endl << std::endl;
}
} else {
LogDebug(metlabel) << "Invalid offline seed TSOS after propagation!" << std::endl << std::endl;
}
}
return selOffseed;
}