CMS 3D CMS Logo

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