CMS 3D CMS Logo

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