CMS 3D CMS Logo

L2MuonSeedGenerator.cc
Go to the documentation of this file.
1 //-------------------------------------------------
2 //
15 //
16 //--------------------------------------------------
17 
18 // Class Header
20 
21 // Framework
27 
35 
38 
39 using namespace std;
40 using namespace edm;
41 using namespace l1extra;
42 
43 // constructors
45  : theSource(iConfig.getParameter<InputTag>("InputObjects")),
46  theL1GMTReadoutCollection(iConfig.getParameter<InputTag>("GMTReadoutCollection")),
47  thePropagatorName(iConfig.getParameter<string>("Propagator")),
48  theL1MinPt(iConfig.getParameter<double>("L1MinPt")),
49  theL1MaxEta(iConfig.getParameter<double>("L1MaxEta")),
50  theL1MinQuality(iConfig.getParameter<unsigned int>("L1MinQuality")),
51  useOfflineSeed(iConfig.getUntrackedParameter<bool>("UseOfflineSeed", false)),
52  useUnassociatedL1(iConfig.existsAs<bool>("UseUnassociatedL1") ? iConfig.getParameter<bool>("UseUnassociatedL1")
53  : true) {
54  gmtToken_ = consumes<L1MuGMTReadoutCollection>(theL1GMTReadoutCollection);
55  muCollToken_ = consumes<L1MuonParticleCollection>(theSource);
56 
57  if (useOfflineSeed) {
58  theOfflineSeedLabel = iConfig.getUntrackedParameter<InputTag>("OfflineSeedLabel");
59  offlineSeedToken_ = consumes<edm::View<TrajectorySeed> >(theOfflineSeedLabel);
60  }
61 
62  // service parameters
63  ParameterSet serviceParameters = iConfig.getParameter<ParameterSet>("ServiceParameters");
64 
65  // the services
66  theService = new MuonServiceProxy(serviceParameters, consumesCollector());
67 
68  // the estimator
70 
71  produces<L2MuonTrajectorySeedCollection>();
72 }
73 
74 // destructor
76  if (theService)
77  delete theService;
78  if (theEstimator)
79  delete theEstimator;
80 }
81 
83  const std::string metname = "Muon|RecoMuon|L2MuonSeedGenerator";
85 
86  auto output = std::make_unique<L2MuonTrajectorySeedCollection>();
87 
88  // Muon particles and GMT readout collection
90  iEvent.getByToken(gmtToken_, gmtrc_handle);
91  L1MuGMTReadoutRecord const& gmtrr = gmtrc_handle.product()->getRecord(0);
92 
94  iEvent.getByToken(muCollToken_, muColl);
95  LogTrace(metname) << "Number of muons " << muColl->size() << endl;
96 
97  edm::Handle<edm::View<TrajectorySeed> > offlineSeedHandle;
98  vector<int> offlineSeedMap;
99  if (useOfflineSeed) {
100  iEvent.getByToken(offlineSeedToken_, offlineSeedHandle);
101  LogTrace(metname) << "Number of offline seeds " << offlineSeedHandle->size() << endl;
102  offlineSeedMap = vector<int>(offlineSeedHandle->size(), 0);
103  }
104 
105  L1MuonParticleCollection::const_iterator it;
106  L1MuonParticleRef::key_type l1ParticleIndex = 0;
107 
108  for (it = muColl->begin(); it != muColl->end(); ++it, ++l1ParticleIndex) {
109  const L1MuGMTExtendedCand muonCand = (*it).gmtMuonCand();
110  unsigned int quality = 0;
111  bool valid_charge = false;
112  ;
113 
114  if (muonCand.empty()) {
115  LogWarning(metname) << "L2MuonSeedGenerator: WARNING, no L1MuGMTCand! " << endl;
116  LogWarning(metname) << "L2MuonSeedGenerator: this should make sense only within MC tests" << endl;
117  // FIXME! Temporary to handle the MC input
118  quality = 7;
119  valid_charge = true;
120  } else {
121  quality = muonCand.quality();
122  valid_charge = muonCand.charge_valid();
123  }
124 
125  float pt = (*it).pt();
126  float eta = (*it).eta();
127  float theta = 2 * atan(exp(-eta));
128  float phi = (*it).phi();
129  int charge = (*it).charge();
130  // Set charge=0 for the time being if the valid charge bit is zero
131  if (!valid_charge)
132  charge = 0;
133  bool barrel = !(*it).isForward();
134 
135  // Get a better eta and charge from regional information
136  // Phi has the same resolution in GMT than regionally, is not it?
137  if (!(muonCand.empty())) {
138  int idx = -1;
139  vector<L1MuRegionalCand> rmc;
140  if (!muonCand.isRPC()) {
141  idx = muonCand.getDTCSCIndex();
142  if (muonCand.isFwd())
143  rmc = gmtrr.getCSCCands();
144  else
145  rmc = gmtrr.getDTBXCands();
146  } else {
147  idx = muonCand.getRPCIndex();
148  if (muonCand.isFwd())
149  rmc = gmtrr.getFwdRPCCands();
150  else
151  rmc = gmtrr.getBrlRPCCands();
152  }
153  if (idx >= 0) {
154  eta = rmc[idx].etaValue();
155  //phi = rmc[idx].phiValue();
156  // Use this charge if the valid charge bit is zero
157  if (!valid_charge)
158  charge = rmc[idx].chargeValue();
159  }
160  }
161 
162  if (pt < theL1MinPt || fabs(eta) > theL1MaxEta)
163  continue;
164 
165  LogTrace(metname) << "New L2 Muon Seed";
166  LogTrace(metname) << "Pt = " << pt << " GeV/c";
167  LogTrace(metname) << "eta = " << eta;
168  LogTrace(metname) << "theta = " << theta << " rad";
169  LogTrace(metname) << "phi = " << phi << " rad";
170  LogTrace(metname) << "charge = " << charge;
171  LogTrace(metname) << "In Barrel? = " << barrel;
172 
173  if (quality <= theL1MinQuality)
174  continue;
175  LogTrace(metname) << "quality = " << quality;
176 
177  // Update the services
178  theService->update(iSetup);
179 
180  const DetLayer* detLayer = nullptr;
181  float radius = 0.;
182 
183  CLHEP::Hep3Vector vec(0., 1., 0.);
184  vec.setTheta(theta);
185  vec.setPhi(phi);
186 
187  // Get the det layer on which the state should be put
188  if (barrel) {
189  LogTrace(metname) << "The seed is in the barrel";
190 
191  // MB2
192  DetId id = DTChamberId(0, 2, 0);
193  detLayer = theService->detLayerGeometry()->idToLayer(id);
194  LogTrace(metname) << "L2 Layer: " << debug.dumpLayer(detLayer);
195 
196  const BoundSurface* sur = &(detLayer->surface());
197  const BoundCylinder* bc = dynamic_cast<const BoundCylinder*>(sur);
198 
199  radius = fabs(bc->radius() / sin(theta));
200 
201  LogTrace(metname) << "radius " << radius;
202 
203  if (pt < 3.5)
204  pt = 3.5;
205  } else {
206  LogTrace(metname) << "The seed is in the endcap";
207 
208  DetId id;
209  // ME2
210  if (theta < Geom::pi() / 2.)
211  id = CSCDetId(1, 2, 0, 0, 0);
212  else
213  id = CSCDetId(2, 2, 0, 0, 0);
214 
215  detLayer = theService->detLayerGeometry()->idToLayer(id);
216  LogTrace(metname) << "L2 Layer: " << debug.dumpLayer(detLayer);
217 
218  radius = fabs(detLayer->position().z() / cos(theta));
219 
220  if (pt < 1.0)
221  pt = 1.0;
222  }
223 
224  vec.setMag(radius);
225 
226  GlobalPoint pos(vec.x(), vec.y(), vec.z());
227 
228  GlobalVector mom(pt * cos(phi), pt * sin(phi), pt * cos(theta) / sin(theta));
229 
230  GlobalTrajectoryParameters param(pos, mom, charge, &*theService->magneticField());
232 
233  mat[0][0] = (0.25 / pt) * (0.25 / pt); // sigma^2(charge/abs_momentum)
234  if (!barrel)
235  mat[0][0] = (0.4 / pt) * (0.4 / pt);
236 
237  //Assign q/pt = 0 +- 1/pt if charge has been declared invalid
238  if (!valid_charge)
239  mat[0][0] = (1. / pt) * (1. / pt);
240 
241  mat[1][1] = 0.05 * 0.05; // sigma^2(lambda)
242  mat[2][2] = 0.2 * 0.2; // sigma^2(phi)
243  mat[3][3] = 20. * 20.; // sigma^2(x_transverse))
244  mat[4][4] = 20. * 20.; // sigma^2(y_transverse))
245 
247 
248  const FreeTrajectoryState state(param, error);
249 
250  LogTrace(metname) << "Free trajectory State from the parameters";
251  LogTrace(metname) << debug.dumpFTS(state);
252 
253  // Propagate the state on the MB2/ME2 surface
254  TrajectoryStateOnSurface tsos = theService->propagator(thePropagatorName)->propagate(state, detLayer->surface());
255 
256  LogTrace(metname) << "State after the propagation on the layer";
257  LogTrace(metname) << debug.dumpLayer(detLayer);
258  LogTrace(metname) << debug.dumpFTS(state);
259 
260  if (tsos.isValid()) {
261  // Get the compatible dets on the layer
262  std::vector<pair<const GeomDet*, TrajectoryStateOnSurface> > detsWithStates =
263  detLayer->compatibleDets(tsos, *theService->propagator(thePropagatorName), *theEstimator);
264  if (!detsWithStates.empty()) {
265  TrajectoryStateOnSurface newTSOS = detsWithStates.front().second;
266  const GeomDet* newTSOSDet = detsWithStates.front().first;
267 
268  LogTrace(metname) << "Most compatible det";
269  LogTrace(metname) << debug.dumpMuonId(newTSOSDet->geographicalId());
270 
271  LogDebug(metname) << "L1 info: Det and State:";
272  LogDebug(metname) << debug.dumpMuonId(newTSOSDet->geographicalId());
273 
274  if (newTSOS.isValid()) {
275  //LogDebug(metname) << "(x, y, z) = (" << newTSOS.globalPosition().x() << ", "
276  // << newTSOS.globalPosition().y() << ", " << newTSOS.globalPosition().z() << ")";
277  LogDebug(metname) << "pos: (r=" << newTSOS.globalPosition().mag()
278  << ", phi=" << newTSOS.globalPosition().phi() << ", eta=" << newTSOS.globalPosition().eta()
279  << ")";
280  LogDebug(metname) << "mom: (q*pt=" << newTSOS.charge() * newTSOS.globalMomentum().perp()
281  << ", phi=" << newTSOS.globalMomentum().phi() << ", eta=" << newTSOS.globalMomentum().eta()
282  << ")";
283 
284  //LogDebug(metname) << "State on it";
285  //LogDebug(metname) << debug.dumpTSOS(newTSOS);
286 
287  //PTrajectoryStateOnDet seedTSOS;
289 
290  if (useOfflineSeed) {
291  const TrajectorySeed* assoOffseed = associateOfflineSeedToL1(offlineSeedHandle, offlineSeedMap, newTSOS);
292 
293  if (assoOffseed != nullptr) {
294  PTrajectoryStateOnDet const& seedTSOS = assoOffseed->startingState();
295  for (auto const& tsci : assoOffseed->recHits()) {
296  container.push_back(tsci);
297  }
298  output->push_back(
299  L2MuonTrajectorySeed(seedTSOS, container, alongMomentum, L1MuonParticleRef(muColl, l1ParticleIndex)));
300  } else {
301  if (useUnassociatedL1) {
302  // convert the TSOS into a PTSOD
303  PTrajectoryStateOnDet const& seedTSOS =
305  output->push_back(L2MuonTrajectorySeed(
306  seedTSOS, container, alongMomentum, L1MuonParticleRef(muColl, l1ParticleIndex)));
307  }
308  }
309  } else {
310  // convert the TSOS into a PTSOD
311  PTrajectoryStateOnDet const& seedTSOS =
313  output->push_back(
314  L2MuonTrajectorySeed(seedTSOS, container, alongMomentum, L1MuonParticleRef(muColl, l1ParticleIndex)));
315  }
316  }
317  }
318  }
319  }
320 
321  iEvent.put(std::move(output));
322 }
323 
324 // FIXME: does not resolve ambiguities yet!
326  std::vector<int>& offseedMap,
327  TrajectoryStateOnSurface& newTsos) {
328  const std::string metlabel = "Muon|RecoMuon|L2MuonSeedGenerator";
329  MuonPatternRecoDumper debugtmp;
330 
331  edm::View<TrajectorySeed>::const_iterator offseed, endOffseed = offseeds->end();
332  const TrajectorySeed* selOffseed = nullptr;
333  double bestDr = 99999.;
334  unsigned int nOffseed(0);
335  int lastOffseed(-1);
336 
337  for (offseed = offseeds->begin(); offseed != endOffseed; ++offseed, ++nOffseed) {
338  if (offseedMap[nOffseed] != 0)
339  continue;
340  GlobalPoint glbPos = theService->trackingGeometry()
341  ->idToDet(offseed->startingState().detId())
342  ->surface()
343  .toGlobal(offseed->startingState().parameters().position());
344  GlobalVector glbMom = theService->trackingGeometry()
345  ->idToDet(offseed->startingState().detId())
346  ->surface()
347  .toGlobal(offseed->startingState().parameters().momentum());
348 
349  // Preliminary check
350  double preDr = deltaR(newTsos.globalPosition().eta(), newTsos.globalPosition().phi(), glbPos.eta(), glbPos.phi());
351  if (preDr > 1.0)
352  continue;
353 
354  const FreeTrajectoryState offseedFTS(
355  glbPos, glbMom, offseed->startingState().parameters().charge(), &*theService->magneticField());
356  TrajectoryStateOnSurface offseedTsos =
357  theService->propagator(thePropagatorName)->propagate(offseedFTS, newTsos.surface());
358  LogDebug(metlabel) << "Offline seed info: Det and State" << std::endl;
359  LogDebug(metlabel) << debugtmp.dumpMuonId(offseed->startingState().detId()) << std::endl;
360  //LogDebug(metlabel) << "(x, y, z) = (" << newTSOS.globalPosition().x() << ", "
361  // << newTSOS.globalPosition().y() << ", " << newTSOS.globalPosition().z() << ")" << std::endl;
362  LogDebug(metlabel) << "pos: (r=" << offseedFTS.position().mag() << ", phi=" << offseedFTS.position().phi()
363  << ", eta=" << offseedFTS.position().eta() << ")" << std::endl;
364  LogDebug(metlabel) << "mom: (q*pt=" << offseedFTS.charge() * offseedFTS.momentum().perp()
365  << ", phi=" << offseedFTS.momentum().phi() << ", eta=" << offseedFTS.momentum().eta() << ")"
366  << std::endl
367  << std::endl;
368  //LogDebug(metlabel) << debugtmp.dumpFTS(offseedFTS) << std::endl;
369 
370  if (offseedTsos.isValid()) {
371  LogDebug(metlabel) << "Offline seed info after propagation to L1 layer:" << std::endl;
372  //LogDebug(metlabel) << "(x, y, z) = (" << offseedTsos.globalPosition().x() << ", "
373  // << offseedTsos.globalPosition().y() << ", " << offseedTsos.globalPosition().z() << ")" << std::endl;
374  LogDebug(metlabel) << "pos: (r=" << offseedTsos.globalPosition().mag()
375  << ", phi=" << offseedTsos.globalPosition().phi()
376  << ", eta=" << offseedTsos.globalPosition().eta() << ")" << std::endl;
377  LogDebug(metlabel) << "mom: (q*pt=" << offseedTsos.charge() * offseedTsos.globalMomentum().perp()
378  << ", phi=" << offseedTsos.globalMomentum().phi()
379  << ", eta=" << offseedTsos.globalMomentum().eta() << ")" << std::endl
380  << std::endl;
381  //LogDebug(metlabel) << debugtmp.dumpTSOS(offseedTsos) << std::endl;
382  double newDr = deltaR(newTsos.globalPosition().eta(),
383  newTsos.globalPosition().phi(),
384  offseedTsos.globalPosition().eta(),
385  offseedTsos.globalPosition().phi());
386  LogDebug(metlabel) << " -- DR = " << newDr << std::endl;
387  if (newDr < 0.3 && newDr < bestDr) {
388  LogDebug(metlabel) << " --> OK! " << newDr << std::endl << std::endl;
389  selOffseed = &*offseed;
390  bestDr = newDr;
391  offseedMap[nOffseed] = 1;
392  if (lastOffseed > -1)
393  offseedMap[lastOffseed] = 0;
394  lastOffseed = nOffseed;
395  } else {
396  LogDebug(metlabel) << " --> Rejected. " << newDr << std::endl << std::endl;
397  }
398  } else {
399  LogDebug(metlabel) << "Invalid offline seed TSOS after propagation!" << std::endl << std::endl;
400  }
401  }
402 
403  return selOffseed;
404 }
virtual const BoundSurface & surface() const =0
The surface of the GeometricSearchDet.
std::string dumpMuonId(const DetId &id) const
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
virtual const Surface::PositionType & position() const
Returns position of the surface.
std::remove_cv< typename std::remove_reference< argument_type >::type >::type key_type
Definition: Ref.h:164
T perp() const
Definition: PV3DBase.h:69
virtual std::vector< DetWithState > compatibleDets(const TrajectoryStateOnSurface &startingState, const Propagator &prop, const MeasurementEstimator &est) const
edm::EDGetTokenT< l1extra::L1MuonParticleCollection > muCollToken_
edm::InputTag theL1GMTReadoutCollection
edm::EDGetTokenT< edm::View< TrajectorySeed > > offlineSeedToken_
T z() const
Definition: PV3DBase.h:61
const std::string metname
RecHitRange recHits() const
Geom::Phi< T > phi() const
Definition: PV3DBase.h:66
void produce(edm::Event &, const edm::EventSetup &) override
Sin< T >::type sin(const T &t)
Definition: Sin.h:22
T eta() const
Definition: PV3DBase.h:73
unsigned getRPCIndex() const
get index of contributing RPC muon
PTrajectoryStateOnDet persistentState(const TrajectoryStateOnSurface &ts, unsigned int detid)
const SurfaceType & surface() const
#define LogTrace(id)
T getUntrackedParameter(std::string const &, T const &) const
unsigned int quality() const
get quality
Definition: L1MuGMTCand.h:90
const TrajectorySeed * associateOfflineSeedToL1(edm::Handle< edm::View< TrajectorySeed > > &, std::vector< int > &, TrajectoryStateOnSurface &)
string quality
const unsigned theL1MinQuality
bool charge_valid() const
is the charge valid ?
Definition: L1MuGMTCand.h:135
void push_back(D *&d)
Definition: OwnVector.h:326
int iEvent
Definition: GenABIO.cc:224
GlobalPoint globalPosition() const
std::vector< L1MuRegionalCand > getDTBXCands() const
get DT candidates vector
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
T mag() const
Definition: PV3DBase.h:64
DetId geographicalId() const
The label of this GeomDet.
Definition: GeomDet.h:64
bool isRPC() const
get RPC bit (true=RPC, false = DT/CSC or matched)
bool empty() const
is it an empty muon candidate?
Definition: L1MuGMTCand.h:61
std::vector< L1MuRegionalCand > getBrlRPCCands() const
get barrel RPC candidates vector
PTrajectoryStateOnDet const & startingState() const
std::vector< L1MuRegionalCand > getCSCCands() const
get CSC candidates vector
Definition: DetId.h:17
#define debug
Definition: HDRShower.cc:19
MeasurementEstimator * theEstimator
T1 phi() const
Definition: Phi.h:78
constexpr uint32_t rawId() const
get the raw id
Definition: DetId.h:57
ROOT::Math::SMatrix< double, 5, 5, ROOT::Math::MatRepSym< double, 5 > > AlgebraicSymMatrix55
GlobalVector globalMomentum() const
~L2MuonSeedGenerator() override
Destructor.
MuonServiceProxy * theService
the event setup proxy, it takes care the services update
HLT enums.
boost::indirect_iterator< typename seq_t::const_iterator > const_iterator
Definition: View.h:86
edm::EDGetTokenT< L1MuGMTReadoutCollection > gmtToken_
Definition: output.py:1
const_iterator begin() const
std::vector< L1MuRegionalCand > getFwdRPCCands() const
get forward RPC candidates vector
Log< level::Warning, false > LogWarning
constexpr double pi()
Definition: Pi.h:31
L2MuonSeedGenerator(const edm::ParameterSet &)
Constructor.
bool isFwd() const
get forward bit (true=forward, false=barrel)
Geom::Theta< T > theta() const
edm::Ref< L1MuonParticleCollection > L1MuonParticleRef
def move(src, dest)
Definition: eostools.py:511
#define LogDebug(id)
unsigned getDTCSCIndex() const
get index of contributing DT/CSC muon
edm::InputTag theOfflineSeedLabel