CMS 3D CMS Logo

ME0SegmentMatcher.cc
Go to the documentation of this file.
1 
9 
12 
14 
16 
18 
23 
24 
26 
27 
28 #include "TLorentzVector.h"
29 
33 
34 
40 
41 
53 
55 
58 
63 
65 
68 
71 
72 
73 
74 
76 class MagneticField;
78 public:
80  explicit ME0SegmentMatcher(const edm::ParameterSet&);
84  virtual void produce(edm::Event&, const edm::EventSetup&) override;
85 
86 
87  virtual void beginRun(edm::Run const&, edm::EventSetup const&) override;
88 
89 
90 
92  int , const AlgebraicSymMatrix66& ,
93  const MagneticField* );
94 
96  int , const AlgebraicSymMatrix55& ,
97  const MagneticField* );
98 
99  void getFromFTS(const FreeTrajectoryState& ,
101  int& , AlgebraicSymMatrix66& );
102 
103 private:
104 
105 
106 
111 
112 
113 };
114 
115 
117  produces<std::vector<reco::ME0Muon> >();
118  theX_PULL_CUT = pas.getParameter<double>("maxPullX");
119  theX_RESIDUAL_CUT = pas.getParameter<double>("maxDiffX");
120  theY_PULL_CUT = pas.getParameter<double>("maxPullY");
121  theY_RESIDUAL_CUT = pas.getParameter<double>("maxDiffY");
122  thePHIDIR_RESIDUAL_CUT = pas.getParameter<double>("maxDiffPhiDirection");
123  //Might need to replace "OurSegments" with an edm::InputTag of "OurSegments"
124  OurSegmentsTag = pas.getParameter<edm::InputTag>("me0SegmentTag");
125  generalTracksTag = pas.getParameter<edm::InputTag>("tracksTag");
126  OurSegmentsToken_ = consumes<ME0SegmentCollection>(OurSegmentsTag);
127  generalTracksToken_ = consumes<reco::TrackCollection>(generalTracksTag);
128 }
129 
131 
133 
134 
135  //Getting the objects we'll need
136  using namespace edm;
137 
138  ESHandle<ME0Geometry> me0Geom;
139  setup.get<MuonGeometryRecord>().get(me0Geom);
141  setup.get<IdealMagneticFieldRecord>().get(bField);
142  ESHandle<Propagator> ThisshProp;
143  setup.get<TrackingComponentsRecord>().get("SteppingHelixPropagatorAlong", ThisshProp);
144 
145  using namespace reco;
146 
147  Handle<ME0SegmentCollection> OurSegments;
148  //ev.getByLabel("me0Segments","",OurSegments);
149  ev.getByToken(OurSegmentsToken_,OurSegments);
150 
151  auto oc = std::make_unique<std::vector<ME0Muon>>();
152  std::vector<ME0Muon> TempStore;
153 
155  //ev.getByLabel <TrackCollection> ("generalTracks", generalTracks);
156  ev.getByToken(generalTracksToken_,generalTracks);
157 
158 
159  int TrackNumber = 0;
160 
161  for (std::vector<Track>::const_iterator thisTrack = generalTracks->begin();
162  thisTrack != generalTracks->end(); ++thisTrack,++TrackNumber){
163  //Initializing our plane
164 
165  //Remove later
166  if (std::abs(thisTrack->eta()) < 1.8) continue;
167 
168  float zSign = thisTrack->pz() > 0 ? 1.0f : -1.0f;
169 
170  const float zValue = 526.75 * zSign;
171 
172  Plane *plane = new Plane(Surface::PositionType(0,0,zValue),Surface::RotationType());
173 
174  //Getting the initial variables for propagation
175 
176  int chargeReco = thisTrack->charge();
177  GlobalVector p3reco, r3reco;
178 
179  p3reco = GlobalVector(thisTrack->outerPx(), thisTrack->outerPy(), thisTrack->outerPz());
180  r3reco = GlobalVector(thisTrack->outerX(), thisTrack->outerY(), thisTrack->outerZ());
181 
182  AlgebraicSymMatrix66 covReco;
183  //This is to fill the cov matrix correctly
184  AlgebraicSymMatrix55 covReco_curv;
185  covReco_curv = thisTrack->outerStateCovariance();
186  FreeTrajectoryState initrecostate = getFTS(p3reco, r3reco, chargeReco, covReco_curv, &*bField);
187  getFromFTS(initrecostate, p3reco, r3reco, chargeReco, covReco);
188 
189  //Now we propagate and get the propagated variables from the propagated state
190  //SteppingHelixStateInfo startrecostate(initrecostate);
191  //SteppingHelixStateInfo lastrecostate;
192  TrajectoryStateOnSurface lastrecostate;
193 
194  //const SteppingHelixPropagator* ThisshProp =
195  //dynamic_cast<const SteppingHelixPropagator*>(&*shProp);
196 
197 
198 
199  //lastrecostate = ThisshProp->propagate(startrecostate, *plane);
200  //lastrecostate = ThisshProp->propagateWithPath(startrecostate, *plane);
201  //ThisshProp->propagate(startrecostate, *plane,lastrecostate);
202  lastrecostate = ThisshProp->propagate(initrecostate,*plane);
203  if (!lastrecostate.isValid()) continue;
204 
205  FreeTrajectoryState finalrecostate(*lastrecostate.freeTrajectoryState());
206  //lastrecostate.getFreeState(finalrecostate);
207  //finalrecostate = lastrecostate.freeTrajectoryState();
208 
209  AlgebraicSymMatrix66 covFinalReco;
210  GlobalVector p3FinalReco_glob, r3FinalReco_globv;
211  getFromFTS(finalrecostate, p3FinalReco_glob, r3FinalReco_globv, chargeReco, covFinalReco);
212 
213 
214  //To transform the global propagated track to local coordinates
215  int SegmentNumber = 0;
216 
218  double ClosestDelR2 = 999.;
219 
220  for (auto thisSegment = OurSegments->begin(); thisSegment != OurSegments->end();
221  ++thisSegment,++SegmentNumber){
222  ME0DetId id = thisSegment->me0DetId();
223 
224  auto chamber = me0Geom->chamber(id);
225 
226  if ( zSign * chamber->toGlobal(thisSegment->localPosition()).z() < 0 ) continue;
227 
228  GlobalPoint r3FinalReco_glob(r3FinalReco_globv.x(),r3FinalReco_globv.y(),r3FinalReco_globv.z());
229 
230  LocalPoint r3FinalReco = chamber->toLocal(r3FinalReco_glob);
231  LocalVector p3FinalReco=chamber->toLocal(p3FinalReco_glob);
232 
233  LocalPoint thisPosition(thisSegment->localPosition());
234  LocalVector thisDirection(thisSegment->localDirection().x(),thisSegment->localDirection().y(),thisSegment->localDirection().z()); //FIXME
235 
236  //The same goes for the error
237  AlgebraicMatrix thisCov(4,4,0);
238  for (int i = 1; i <=4; i++){
239  for (int j = 1; j <=4; j++){
240  thisCov(i,j) = thisSegment->parametersError()(i,j);
241  }
242  }
243 
245 
246 
247  LocalTrajectoryParameters ltp(r3FinalReco,p3FinalReco,chargeReco);
248  JacobianCartesianToLocal jctl(chamber->surface(),ltp);
249  AlgebraicMatrix56 jacobGlbToLoc = jctl.jacobian();
250 
251  AlgebraicMatrix55 Ctmp = (jacobGlbToLoc * covFinalReco) * ROOT::Math::Transpose(jacobGlbToLoc);
252  AlgebraicSymMatrix55 C; // I couldn't find any other way, so I resort to the brute force
253  for(int i=0; i<5; ++i) {
254  for(int j=0; j<5; ++j) {
255  C[i][j] = Ctmp[i][j];
256 
257  }
258  }
259 
260  Double_t sigmax = sqrt(C[3][3]+thisSegment->localPositionError().xx() );
261  Double_t sigmay = sqrt(C[4][4]+thisSegment->localPositionError().yy() );
262 
263  bool X_MatchFound = false, Y_MatchFound = false, Dir_MatchFound = false;
264 
265 
266  // if ( (std::abs(thisPosition.x()-r3FinalReco.x()) < (3.0 * sigmax)) || (std::abs(thisPosition.x()-r3FinalReco.x()) < 2.0 ) ) X_MatchFound = true;
267  // if ( (std::abs(thisPosition.y()-r3FinalReco.y()) < (3.0 * sigmay)) || (std::abs(thisPosition.y()-r3FinalReco.y()) < 2.0 ) ) Y_MatchFound = true;
268 
269  // if ( std::abs(p3FinalReco_glob.phi()-chamber->toGlobal(thisSegment->localDirection()).phi()) < 0.15) Dir_MatchFound = true;
270 
271 
272  if ( (std::abs(thisPosition.x()-r3FinalReco.x()) < (theX_PULL_CUT * sigmax)) || (std::abs(thisPosition.x()-r3FinalReco.x()) < theX_RESIDUAL_CUT ) ) X_MatchFound = true;
273  if ( (std::abs(thisPosition.y()-r3FinalReco.y()) < (theY_PULL_CUT * sigmay)) || (std::abs(thisPosition.y()-r3FinalReco.y()) < theY_RESIDUAL_CUT ) ) Y_MatchFound = true;
274 
275  if ( std::abs(reco::deltaPhi(p3FinalReco_glob.barePhi(),chamber->toGlobal(thisSegment->localDirection()).barePhi())) < thePHIDIR_RESIDUAL_CUT) Dir_MatchFound = true;
276 
277  //Check for a Match, and if there is a match, check the delR from the segment, keeping only the closest in MuonCandidate
278  if (X_MatchFound && Y_MatchFound && Dir_MatchFound) {
279 
280  TrackRef thisTrackRef(generalTracks,TrackNumber);
281 
282  GlobalPoint SegPos(chamber->toGlobal(thisSegment->localPosition()));
283  GlobalPoint TkPos(r3FinalReco_globv.x(),r3FinalReco_globv.y(),r3FinalReco_globv.z());
284 
285  double thisDelR2 = reco::deltaR2(SegPos,TkPos);
286  if (thisDelR2 < ClosestDelR2){
287  ClosestDelR2 = thisDelR2;
288  MuonCandidate = reco::ME0Muon(thisTrackRef,(*thisSegment),SegmentNumber,chargeReco);
289 
290  MuonCandidate.setGlobalTrackPosAtSurface(r3FinalReco_glob);
291  MuonCandidate.setGlobalTrackMomAtSurface(p3FinalReco_glob);
292  MuonCandidate.setLocalTrackPosAtSurface(r3FinalReco);
293  MuonCandidate.setLocalTrackMomAtSurface(p3FinalReco);
294  MuonCandidate.setGlobalTrackCov(covFinalReco);
295  MuonCandidate.setLocalTrackCov(C);
296  }
297  }
298  }//End loop for (auto thisSegment = OurSegments->begin(); thisSegment != OurSegments->end(); ++thisSegment,++SegmentNumber)
299 
300  //As long as the delR of the MuonCandidate is sensible, store the track-segment pair
301  if (ClosestDelR2 < 500.) {
302  oc->push_back(MuonCandidate);
303  }
304  }
305 
306  // put collection in event
307 
308  ev.put(std::move(oc));
309 }
310 
313  int charge, const AlgebraicSymMatrix55& cov,
314  const MagneticField* field){
315 
316  GlobalVector p3GV(p3.x(), p3.y(), p3.z());
317  GlobalPoint r3GP(r3.x(), r3.y(), r3.z());
318  GlobalTrajectoryParameters tPars(r3GP, p3GV, charge, field);
319 
320  CurvilinearTrajectoryError tCov(cov);
321 
322  return cov.kRows == 5 ? FreeTrajectoryState(tPars, tCov) : FreeTrajectoryState(tPars) ;
323 }
324 
327  int charge, const AlgebraicSymMatrix66& cov,
328  const MagneticField* field){
329 
330  GlobalVector p3GV(p3.x(), p3.y(), p3.z());
331  GlobalPoint r3GP(r3.x(), r3.y(), r3.z());
332  GlobalTrajectoryParameters tPars(r3GP, p3GV, charge, field);
333 
334  CartesianTrajectoryError tCov(cov);
335 
336  return cov.kRows == 6 ? FreeTrajectoryState(tPars, tCov) : FreeTrajectoryState(tPars) ;
337 }
338 
341  int& charge, AlgebraicSymMatrix66& cov){
342  GlobalVector p3GV = fts.momentum();
343  GlobalPoint r3GP = fts.position();
344 
345  GlobalVector p3T(p3GV.x(), p3GV.y(), p3GV.z());
346  GlobalVector r3T(r3GP.x(), r3GP.y(), r3GP.z());
347  p3 = p3T;
348  r3 = r3T;
349  // p3.set(p3GV.x(), p3GV.y(), p3GV.z());
350  // r3.set(r3GP.x(), r3GP.y(), r3GP.z());
351 
352  charge = fts.charge();
353  cov = fts.hasError() ? fts.cartesianError().matrix() : AlgebraicSymMatrix66();
354 
355 }
356 
357 
358 void ME0SegmentMatcher::beginRun(edm::Run const& iRun, edm::EventSetup const& iSetup)
359 {
360 
361 }
362 
363 
const AlgebraicMatrix56 & jacobian() const
T getParameter(std::string const &) const
T barePhi() const
void setLocalTrackMomAtSurface(const LocalVector &localTrackMomAtSurface)
Definition: ME0Muon.h:56
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:122
CartesianTrajectoryError cartesianError() const
ROOT::Math::SMatrix< double, 5, 6, ROOT::Math::MatRepStd< double, 5, 6 > > AlgebraicMatrix56
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:460
FreeTrajectoryState getFTS(const GlobalVector &, const GlobalVector &, int, const AlgebraicSymMatrix66 &, const MagneticField *)
ME0SegmentMatcher(const edm::ParameterSet &)
Constructor.
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
ROOT::Math::SMatrix< double, 6, 6, ROOT::Math::MatRepSym< double, 6 > > AlgebraicSymMatrix66
T y() const
Definition: PV3DBase.h:63
def setup(process, global_tag, zero_tesla=False)
Definition: GeneralSetup.py:1
bool ev
ROOT::Math::SMatrix< double, 5, 5, ROOT::Math::MatRepSym< double, 5 > > AlgebraicSymMatrix55
edm::InputTag generalTracksTag
TrackCharge charge() const
Definition: Plane.h:17
virtual void produce(edm::Event &, const edm::EventSetup &) override
Produce the ME0Segment collection.
void setGlobalTrackMomAtSurface(const GlobalVector &globalTrackMomAtSurface)
Definition: ME0Muon.h:54
edm::EDGetTokenT< reco::TrackCollection > generalTracksToken_
T barePhi() const
Definition: PV3DBase.h:68
~ME0SegmentMatcher()
Destructor.
void setLocalTrackCov(const AlgebraicSymMatrix55 &trackCov)
Definition: ME0Muon.h:59
CLHEP::HepMatrix AlgebraicMatrix
FreeTrajectoryState const * freeTrajectoryState(bool withErrors=true) const
T sqrt(T t)
Definition: SSEVec.h:18
edm::InputTag OurSegmentsTag
T z() const
Definition: PV3DBase.h:64
const ME0Chamber * chamber(ME0DetId id) const
Return a chamber given its id.
Definition: ME0Geometry.cc:73
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
edm::EDGetTokenT< ME0SegmentCollection > OurSegmentsToken_
GlobalVector momentum() const
void setGlobalTrackCov(const AlgebraicSymMatrix66 &trackCov)
Definition: ME0Muon.h:58
const AlgebraicSymMatrix66 & matrix() const
double deltaPhi(double phi1, double phi2)
Definition: deltaPhi.h:22
GlobalPoint position() const
const T & get() const
Definition: EventSetup.h:56
void getFromFTS(const FreeTrajectoryState &, GlobalVector &, GlobalVector &, int &, AlgebraicSymMatrix66 &)
void setGlobalTrackPosAtSurface(const GlobalPoint &globalTrackPosAtSurface)
Definition: ME0Muon.h:53
TrajectoryStateOnSurface propagate(STA const &state, SUR const &surface) const
Definition: Propagator.h:53
T1 deltaR2(T1 eta1, T2 phi1, T3 eta2, T4 phi2)
Definition: deltaR.h:36
fixed size matrix
HLT enums.
T x() const
Definition: PV3DBase.h:62
virtual void beginRun(edm::Run const &, edm::EventSetup const &) override
ROOT::Math::SMatrix< double, 5, 5, ROOT::Math::MatRepStd< double, 5, 5 > > AlgebraicMatrix55
def move(src, dest)
Definition: eostools.py:510
Definition: Run.h:42
Global3DVector GlobalVector
Definition: GlobalVector.h:10
void setLocalTrackPosAtSurface(const LocalPoint &localTrackPosAtSurface)
Definition: ME0Muon.h:55
double p3[4]
Definition: TauolaWrapper.h:91