CMS 3D CMS Logo

GlobalTrackQualityProducer.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: GlobalTrackingTools
4 // Class: GlobalTrackQualityProducer
5 //
6 //
7 // Original Author: Adam Everett
8 //
9 //
10 
11 // system include files
12 #include <memory>
13 
14 // user include files
18 
21 
26 
28  inputCollection_(iConfig.getParameter<edm::InputTag>("InputCollection")),inputLinksCollection_(iConfig.getParameter<edm::InputTag>("InputLinksCollection")),theService(0),theGlbRefitter(0),theGlbMatcher(0)
29 {
30  // service parameters
31  edm::ParameterSet serviceParameters = iConfig.getParameter<edm::ParameterSet>("ServiceParameters");
32  theService = new MuonServiceProxy(serviceParameters);
33 
34  // TrackRefitter parameters
36  edm::ParameterSet refitterParameters = iConfig.getParameter<edm::ParameterSet>("RefitterParameters");
37  theGlbRefitter = new GlobalMuonRefitter(refitterParameters, theService, iC);
38 
39  edm::ParameterSet trackMatcherPSet = iConfig.getParameter<edm::ParameterSet>("GlobalMuonTrackMatcher");
40  theGlbMatcher = new GlobalMuonTrackMatcher(trackMatcherPSet,theService);
41 
42  double maxChi2 = iConfig.getParameter<double>("MaxChi2");
43  double nSigma = iConfig.getParameter<double>("nSigma");
44  theEstimator = new Chi2MeasurementEstimator(maxChi2,nSigma);
45 
46  glbMuonsToken=consumes<reco::TrackCollection>(inputCollection_);
47  linkCollectionToken=consumes<reco::MuonTrackLinksCollection>(inputLinksCollection_);
48 
49  produces<edm::ValueMap<reco::MuonQuality> >();
50 }
51 
53  if (theService) delete theService;
54  if (theGlbRefitter) delete theGlbRefitter;
55  if (theGlbMatcher) delete theGlbMatcher;
56  if (theEstimator) delete theEstimator;
57 }
58 
59 void
61 {
62  const std::string theCategory = "Muon|RecoMuon|GlobalTrackQualityProducer";
63 
64  theService->update(iSetup);
65 
66  theGlbRefitter->setEvent(iEvent);
67 
69 
70  // Take the GLB muon container(s)
72  iEvent.getByToken(glbMuonsToken,glbMuons);
73 
75  iEvent.getByToken(linkCollectionToken,linkCollectionHandle);
76 
77  //Retrieve tracker topology from geometry
79  iSetup.get<TrackerTopologyRcd>().get(tTopoHand);
80  const TrackerTopology *tTopo=tTopoHand.product();
81 
82 
83  // reserve some space
84  std::vector<reco::MuonQuality> valuesQual;
85  valuesQual.reserve(glbMuons->size());
86 
87  int trackIndex = 0;
88  for (reco::TrackCollection::const_iterator track = glbMuons->begin(); track!=glbMuons->end(); ++track , ++trackIndex) {
89  reco::TrackRef glbRef(glbMuons,trackIndex);
90  reco::TrackRef staTrack = reco::TrackRef();
91 
92  std::vector<Trajectory> refitted=theGlbRefitter->refit(*track,1,tTopo);
93 
94  LogTrace(theCategory)<<"GLBQual N refitted " << refitted.size();
95 
96  std::pair<double,double> thisKink;
97  double relative_muon_chi2 = 0.0;
98  double relative_tracker_chi2 = 0.0;
99  double glbTrackProbability = 0.0;
100  if(refitted.size()>0) {
101  thisKink = kink(refitted.front()) ;
102  std::pair<double,double> chi = newChi2(refitted.front());
103  relative_muon_chi2 = chi.second; //normalized inside to /sum(muHits.dimension)
104  relative_tracker_chi2 = chi.first; //normalized inside to /sum(tkHits.dimension)
105  glbTrackProbability = trackProbability(refitted.front());
106  }
107 
108  LogTrace(theCategory)<<"GLBQual: Kink " << thisKink.first << " " << thisKink.second;
109  LogTrace(theCategory)<<"GLBQual: Rel Chi2 " << relative_tracker_chi2 << " " << relative_muon_chi2;
110  LogTrace(theCategory)<<"GLBQual: trackProbability " << glbTrackProbability;
111 
112  // Fill the STA-TK match information
113  float chi2, d, dist, Rpos;
114  chi2 = d = dist = Rpos = -1.0;
115  bool passTight = false;
116  typedef MuonTrajectoryBuilder::TrackCand TrackCand;
117  if ( linkCollectionHandle.isValid() ) {
118  for ( reco::MuonTrackLinksCollection::const_iterator links = linkCollectionHandle->begin();
119  links != linkCollectionHandle->end(); ++links )
120  {
121  if ( links->trackerTrack().isNull() ||
122  links->standAloneTrack().isNull() ||
123  links->globalTrack().isNull() )
124  {
125  edm::LogWarning(theCategory) << "Global muon links to constituent tracks are invalid. There should be no such object. Muon is skipped.";
126  continue;
127  }
128  if (links->globalTrack() == glbRef) {
129  staTrack = !links->standAloneTrack().isNull() ? links->standAloneTrack() : reco::TrackRef();
130  TrackCand staCand = TrackCand((Trajectory*)(0),links->standAloneTrack());
131  TrackCand tkCand = TrackCand((Trajectory*)(0),links->trackerTrack());
132  chi2 = theGlbMatcher->match(staCand,tkCand,0,0);
133  d = theGlbMatcher->match(staCand,tkCand,1,0);
134  Rpos = theGlbMatcher->match(staCand,tkCand,2,0);
135  dist = theGlbMatcher->match(staCand,tkCand,3,0);
136  passTight = theGlbMatcher->matchTight(staCand,tkCand);
137  }
138  }
139  }
140 
141  if(!staTrack.isNull()) LogTrace(theCategory)<<"GLBQual: Used UpdatedAtVtx : " << (iEvent.getProvenance(staTrack.id()).productInstanceName() == std::string("UpdatedAtVtx"));
142 
143  float maxFloat01 = std::numeric_limits<float>::max()*0.1; // a better solution would be to use float above .. m/be not
144  reco::MuonQuality muQual;
145  if(!staTrack.isNull()) muQual.updatedSta = iEvent.getProvenance(staTrack.id()).productInstanceName() == std::string("UpdatedAtVtx");
146  muQual.trkKink = thisKink.first > maxFloat01 ? maxFloat01 : thisKink.first;
147  muQual.glbKink = thisKink.second > maxFloat01 ? maxFloat01 : thisKink.second;
148  muQual.trkRelChi2 = relative_tracker_chi2 > maxFloat01 ? maxFloat01 : relative_tracker_chi2;
149  muQual.staRelChi2 = relative_muon_chi2 > maxFloat01 ? maxFloat01 : relative_muon_chi2;
150  muQual.tightMatch = passTight;
151  muQual.chi2LocalPosition = dist;
152  muQual.chi2LocalMomentum = chi2;
153  muQual.localDistance = d;
154  muQual.globalDeltaEtaPhi = Rpos;
155  muQual.glbTrackProbability = glbTrackProbability;
156  valuesQual.push_back(muQual);
157  }
158 
159  /*
160  for(int i = 0; i < valuesTkRelChi2.size(); i++) {
161  LogTrace(theCategory)<<"value " << valuesTkRelChi2[i] ;
162  }
163  */
164 
165  // create and fill value maps
166  auto outQual = std::make_unique<edm::ValueMap<reco::MuonQuality>>();
167  edm::ValueMap<reco::MuonQuality>::Filler fillerQual(*outQual);
168  fillerQual.insert(glbMuons, valuesQual.begin(), valuesQual.end());
169  fillerQual.fill();
170 
171  // put value map into event
172  iEvent.put(std::move(outQual));
173 }
174 
175 std::pair<double,double> GlobalTrackQualityProducer::kink(Trajectory& muon) const {
176 
177  const std::string theCategory = "Muon|RecoMuon|GlobalTrackQualityProducer";
178 
179  using namespace std;
180  using namespace edm;
181  using namespace reco;
182 
183  double result = 0.0;
184  double resultGlb = 0.0;
185 
186 
188  typedef ConstRecHitPointer RecHit;
189  typedef std::vector<TrajectoryMeasurement>::const_iterator TMI;
190 
191 
192  vector<TrajectoryMeasurement> meas = muon.measurements();
193 
194  for ( TMI m = meas.begin(); m != meas.end(); m++ ) {
196 
197  //not used double estimate = 0.0;
198 
199  RecHit rhit = (*m).recHit();
200  bool ok = false;
201  if ( rhit->isValid() ) {
202  if(DetId::Tracker == rhit->geographicalId().det()) ok = true;
203  }
204 
205  //if ( !ok ) continue;
206 
207  const TrajectoryStateOnSurface& tsos = (*m).predictedState();
208 
209 
210  if ( tsos.isValid() && rhit->isValid() && rhit->hit()->isValid()
211  && !edm::isNotFinite(rhit->localPositionError().xx()) //this is paranoia induced by reported case
212  && !edm::isNotFinite(rhit->localPositionError().xy()) //it's better to track down the origin of bad numbers
213  && !edm::isNotFinite(rhit->localPositionError().yy())
214  ) {
215 
216  double phi1 = tsos.globalPosition().phi();
217  if ( phi1 < 0 ) phi1 = 2*M_PI + phi1;
218 
219  double phi2 = rhit->globalPosition().phi();
220  if ( phi2 < 0 ) phi2 = 2*M_PI + phi2;
221 
222  double diff = fabs(phi1 - phi2);
223  if ( diff > M_PI ) diff = 2*M_PI - diff;
224 
225  GlobalPoint hitPos = rhit->globalPosition();
226 
227  GlobalError hitErr = rhit->globalPositionError();
228  //LogDebug(theCategory)<<"hitPos " << hitPos;
229  double error = hitErr.phierr(hitPos); // error squared
230 
231  double s = ( error > 0.0 ) ? (diff*diff)/error : (diff*diff);
232 
233  if(ok) result += s;
234  resultGlb += s;
235  }
236 
237  }
238 
239 
240  return std::pair<double,double>(result,resultGlb);
241 
242 }
243 
244 std::pair<double,double> GlobalTrackQualityProducer::newChi2(Trajectory& muon) const {
245  const std::string theCategory = "Muon|RecoMuon|GlobalTrackQualityProducer";
246 
247  using namespace std;
248  using namespace edm;
249  using namespace reco;
250 
251  double muChi2 = 0.0;
252  double tkChi2 = 0.0;
253  unsigned int muNdof = 0;
254  unsigned int tkNdof = 0;
255 
257  typedef ConstRecHitPointer RecHit;
258  typedef vector<TrajectoryMeasurement>::const_iterator TMI;
259 
260  vector<TrajectoryMeasurement> meas = muon.measurements();
261 
262  for ( TMI m = meas.begin(); m != meas.end(); m++ ) {
264  const TrajectoryStateOnSurface& uptsos = (*m).updatedState();
265  // FIXME FIXME CLONE!!!
266  // TrackingRecHit::RecHitPointer preciseHit = hit->clone(uptsos);
267  auto preciseHit = hit;
268  double estimate = 0.0;
269  if (preciseHit->isValid() && uptsos.isValid()) {
270  estimate = theEstimator->estimate(uptsos, *preciseHit ).second;
271  }
272 
273  //LogTrace(theCategory) << "estimate " << estimate << " TM.est " << m->estimate();
274  //UNUSED: double tkDiff = 0.0;
275  //UNUSED: double staDiff = 0.0;
276  if ( hit->isValid() && (hit->geographicalId().det()) == DetId::Tracker ) {
277  tkChi2 += estimate;
278  //UNUSED: tkDiff = estimate - m->estimate();
279  tkNdof += hit->dimension();
280  }
281  if ( hit->isValid() && (hit->geographicalId().det()) == DetId::Muon ) {
282  muChi2 += estimate;
283  //UNUSED staDiff = estimate - m->estimate();
284  muNdof += hit->dimension();
285  }
286  }
287 
288  if (tkNdof < 6 ) tkChi2 = tkChi2; // or should I set it to a large number ?
289  else tkChi2 /= (tkNdof-5.);
290 
291  if (muNdof < 6 ) muChi2 = muChi2; // or should I set it to a large number ?
292  else muChi2 /= (muNdof-5.);
293 
294  return std::pair<double,double>(tkChi2,muChi2);
295 
296 }
297 
298 //
299 // calculate the tail probability (-ln(P)) of a fit
300 //
301 double
303 
304  if ( track.ndof() > 0 && track.chiSquared() > 0 ) {
305  return -LnChiSquaredProbability(track.chiSquared(), track.ndof());
306  } else {
307  return 0.0;
308  }
309 
310 }
311 
312 //#include "FWCore/Framework/interface/MakerMacros.h"
313 //DEFINE_FWK_MODULE(GlobalTrackQualityProducer);
void update(const edm::EventSetup &setup)
update the services each event
float chi2LocalPosition
chi2 value for the STA-TK matching of local position
Definition: MuonQuality.h:19
T getParameter(std::string const &) const
const edm::EventSetup & eventSetup() const
get the whole EventSetup
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:122
float chi2LocalMomentum
chi2 value for the STA-TK matching of local momentum
Definition: MuonQuality.h:21
std::pair< const Trajectory *, reco::TrackRef > TrackCand
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:460
bool updatedSta
bool returns true if standAloneMuon_updatedAtVtx was used in the fit
Definition: MuonQuality.h:9
float glbKink
value of the kink algorithm applied to the global track
Definition: MuonQuality.h:13
float LnChiSquaredProbability(double chiSquared, double nrDOF)
edm::EDGetTokenT< reco::TrackCollection > glbMuonsToken
bool matchTight(const TrackCand &sta, const TrackCand &track) const
check if two tracks are compatible (less than Chi2Cut, DeltaDCut, DeltaRCut)
Geom::Phi< T > phi() const
Definition: PV3DBase.h:69
float glbTrackProbability
the tail probability (-ln(P)) of the global fit
Definition: MuonQuality.h:29
virtual void produce(edm::Event &, const edm::EventSetup &) override
GlobalPoint globalPosition() const
void setServices(const edm::EventSetup &)
set the services needed by the TrackTransformer
float trkKink
value of the kink algorithm applied to the inner track stub
Definition: MuonQuality.h:11
float trkRelChi2
chi2 value for the inner track stub with respect to the global track
Definition: MuonQuality.h:15
virtual std::pair< double, double > newChi2(Trajectory &muon) const
ProductID id() const
Accessor for product ID.
Definition: Ref.h:259
virtual double trackProbability(Trajectory &track) const
T phierr(const GlobalPoint &aPoint) const
TransientTrackingRecHit::ConstRecHitPointer ConstRecHitPointer
virtual std::pair< double, double > kink(Trajectory &muon) const
DataContainer const & measurements() const
Definition: Trajectory.h:196
int iEvent
Definition: GenABIO.cc:230
bool isNotFinite(T x)
Definition: isFinite.h:10
bool tightMatch
if the STA-TK matching passed the tighter matching criteria
Definition: MuonQuality.h:27
std::shared_ptr< TrackingRecHit const > ConstRecHitPointer
ConsumesCollector consumesCollector()
Use a ConsumesCollector to gather consumes information from helper functions.
float globalDeltaEtaPhi
global delta-Eta-Phi of STA-TK matching
Definition: MuonQuality.h:25
double match(const TrackCand &sta, const TrackCand &track, int matchOption=0, int surfaceOption=1) const
bool isValid() const
Definition: HandleBase.h:74
bool isNull() const
Checks for null.
Definition: Ref.h:250
#define LogTrace(id)
#define M_PI
virtual void setEvent(const edm::Event &)
pass the Event to the algo at each event
float staRelChi2
chi2 value for the outer track stub with respect to the global track
Definition: MuonQuality.h:17
int ndof(bool bon=true) const
Definition: Trajectory.cc:102
const T & get() const
Definition: EventSetup.h:55
edm::Ref< TrackCollection > TrackRef
persistent reference to a Track
Definition: TrackFwd.h:20
edm::EDGetTokenT< reco::MuonTrackLinksCollection > linkCollectionToken
float chiSquared() const
Definition: Trajectory.h:262
fixed size matrix
HLT enums.
float localDistance
local distance seperation for STA-TK TSOS matching on same surface
Definition: MuonQuality.h:23
GlobalMuonTrackMatcher * theGlbMatcher
virtual HitReturnType estimate(const TrajectoryStateOnSurface &ts, const TrackingRecHit &hit) const =0
Provenance getProvenance(BranchID const &theID) const
Definition: Event.cc:81
T const * product() const
Definition: ESHandle.h:86
std::vector< Trajectory > refit(const reco::Track &globalTrack, const int theMuonHitsOption, const TrackerTopology *tTopo) const
build combined trajectory from sta Track and tracker RecHits
def move(src, dest)
Definition: eostools.py:510
GlobalTrackQualityProducer(const edm::ParameterSet &iConfig)