CMS 3D CMS Logo

MuonsFromRefitTracksProducer.cc
Go to the documentation of this file.
1 
13 
20 
22  const reco::TrackToTrackMap& fmsMap,
23  const double cut) {
24  const reco::TrackRef& combinedTrack = muon.globalTrack();
25  const reco::TrackRef& trackerTrack = muon.innerTrack();
26 
27  reco::TrackToTrackMap::const_iterator fmsTrack = fmsMap.find(combinedTrack);
28 
29  double probTK = 0;
30  double probFMS = 0;
31 
32  if (trackerTrack.isAvailable() && trackerTrack->numberOfValidHits())
33  probTK = muon::trackProbability(trackerTrack);
34  if (fmsTrack != fmsMap.end() && fmsTrack->val->numberOfValidHits())
35  probFMS = muon::trackProbability(fmsTrack->val);
36 
37  bool TKok = probTK > 0;
38  bool FMSok = probFMS > 0;
39 
40  if (TKok && FMSok) {
41  if (probFMS - probTK > cut)
42  return make_pair(trackerTrack, reco::Muon::InnerTrack);
43  else
44  return make_pair(fmsTrack->val, reco::Muon::TPFMS);
45  } else if (FMSok)
46  return make_pair(fmsTrack->val, reco::Muon::TPFMS);
47  else if (TKok)
48  return make_pair(trackerTrack, reco::Muon::InnerTrack);
49 
50  return make_pair(combinedTrack, reco::Muon::CombinedTrack);
51 }
52 
54  const reco::TrackRef& combinedTrack = muon.globalTrack();
55  const reco::TrackRef& trackerTrack = muon.innerTrack();
56 
57  if (combinedTrack->pt() < ptThreshold || trackerTrack->pt() < ptThreshold)
58  return make_pair(trackerTrack, reco::Muon::InnerTrack);
59 
60  double delta = fabs(trackerTrack->qoverp() - combinedTrack->qoverp());
61  double threshold = nSigma * trackerTrack->qoverpError();
62 
63  return delta > threshold ? make_pair(trackerTrack, reco::Muon::InnerTrack)
64  : make_pair(combinedTrack, reco::Muon::CombinedTrack);
65 }
66 
68 public:
71 
72 private:
73  void produce(edm::Event&, const edm::EventSetup&) override;
74 
75  // Store the track-to-track map(s) used when using TeV refit tracks.
76  bool storeMatchMaps(const edm::Event& event);
77 
78  // Take the muon passed in, clone it (so that we save all the muon
79  // id information such as isolation, calo energy, etc.) and replace
80  // its combined muon track with the passed in track.
82 
83  // The input muons -- i.e. the merged collection of reco::Muons.
85 
86  // Allow building the muon from just the tracker track. This
87  // functionality should go away after understanding the difference
88  // between the output of option 1 of GlobalMuonProducer and just
89  // looking at the tracker tracks of these muons.
91 
92  // Allow building the muon from just the global track. This option
93  // is introduced since starting from CMSSW 3_1_0, the MuonIdProducer
94  // makes the p4() of the reco::Muon object be what we call the sigma
95  // switch above.
97 
98  // If tevMuonTracks below is not "none", use the TeV refit track as
99  // the combined track of the muon.
101 
102  // Optionally switch out the combined muon track for one of the TeV
103  // muon refit tracks, specified by the input tag here
104  // (e.g. "tevMuons:firstHit").
106 
107  // Whether to make a cocktail muon instead of using just the one
108  // type in tevMuonTracks, where "cocktail" means use the result of
109  // Piotr's tevOptimized().
111 
112  // Whether to use the TMR version of the cocktail function, defined
113  // above. If true, overrides fromCocktail.
114  bool fromTMR;
115 
116  // The cut value for TMR, read from the config file.
117  double TMRcut;
118 
119  // Whether to use Adam Everett's sigma-switch method, choosing
120  // between the global track and the tracker track.
122 
123  // The number of sigma to switch on in the above method.
124  double nSigmaSwitch;
125 
126  // The pT threshold to switch at in the above method.
127  double ptThreshold;
128 
129  // If we're not making cocktail muons, trackMap is the map that maps
130  // global tracks to the desired TeV refit (e.g. from globalMuons to
131  // tevMuons:picky).
133 
134  // All the track maps used in making cocktail muons.
138 
139  // All the tokens
145 };
146 
148  : src(cfg.getParameter<edm::InputTag>("src")),
149  fromTrackerTrack(cfg.getParameter<bool>("fromTrackerTrack")),
150  fromGlobalTrack(cfg.getParameter<bool>("fromGlobalTrack")),
151  tevMuonTracks(cfg.getParameter<std::string>("tevMuonTracks")),
152  fromCocktail(cfg.getParameter<bool>("fromCocktail")),
153  fromTMR(cfg.getParameter<bool>("fromTMR")),
154  TMRcut(cfg.getParameter<double>("TMRcut")),
155  fromSigmaSwitch(cfg.getParameter<bool>("fromSigmaSwitch")),
156  nSigmaSwitch(cfg.getParameter<double>("nSigmaSwitch")),
157  ptThreshold(cfg.getParameter<double>("ptThreshold")) {
158  fromTeVRefit = tevMuonTracks != "none";
159 
160  srcToken_ = consumes<edm::View<reco::Muon> >(src);
161  trackMapToken_ = consumes<reco::TrackToTrackMap>(edm::InputTag(tevMuonTracks, "default"));
162  trackMapDefaultToken_ = consumes<reco::TrackToTrackMap>(edm::InputTag(tevMuonTracks));
163  trackMapFirstHitToken_ = consumes<reco::TrackToTrackMap>(edm::InputTag(tevMuonTracks, "firstHit"));
164  trackMapPickyToken_ = consumes<reco::TrackToTrackMap>(edm::InputTag(tevMuonTracks, "picky"));
165 
166  produces<reco::MuonCollection>();
167 }
168 
170  if (fromCocktail || fromTMR) {
171  event.getByToken(trackMapDefaultToken_, trackMapDefault);
172  event.getByToken(trackMapFirstHitToken_, trackMapFirstHit);
173  event.getByToken(trackMapPickyToken_, trackMapPicky);
174  return !trackMapDefault.failedToGet() && !trackMapFirstHit.failedToGet() && !trackMapPicky.failedToGet();
175  } else {
176  event.getByToken(trackMapToken_, trackMap);
177  return !trackMap.failedToGet();
178  }
179 }
180 
182  const reco::Muon::MuonTrackTypePair& newTrack) const {
183  // Muon mass to make a four-vector out of the new track.
184  static const double muMass = 0.10566;
185 
186  reco::TrackRef tkTrack = muon.innerTrack();
187  reco::TrackRef muTrack = muon.outerTrack();
188 
189  // Make up a real Muon from the tracker track.
190  reco::Particle::Point vtx(newTrack.first->vx(), newTrack.first->vy(), newTrack.first->vz());
192  double p = newTrack.first->p();
193  p4.SetXYZT(newTrack.first->px(), newTrack.first->py(), newTrack.first->pz(), sqrt(p * p + muMass * muMass));
194 
195  reco::Muon* mu = muon.clone();
196  mu->setCharge(newTrack.first->charge());
197  mu->setP4(p4);
198  mu->setVertex(vtx);
199  mu->setGlobalTrack(newTrack.first);
200  mu->setInnerTrack(tkTrack);
201  mu->setOuterTrack(muTrack);
202  mu->setBestTrack(newTrack.second);
203  return mu;
204 }
205 
207  // Get the global muons from the event.
209  event.getByToken(srcToken_, muons);
210 
211  // If we can't get the global muon collection, or below the
212  // track-to-track maps needed, still produce an empty collection of
213  // muons so consumers don't throw an exception.
214  bool ok = !muons.failedToGet();
215 
216  // If we're instructed to use the TeV refit tracks in some way, we
217  // need the track-to-track maps. If we're making a cocktail muon,
218  // get all three track maps (the cocktail ingredients); else just
219  // get the map which takes the above global tracks to the desired
220  // TeV-muon refitted tracks (firstHit or picky).
221  if (ok && fromTeVRefit)
223 
224  // Make the output collection.
225  auto cands = std::make_unique<reco::MuonCollection>();
226 
227  if (ok) {
229  for (muon = muons->begin(); muon != muons->end(); muon++) {
230  // Filter out the so-called trackerMuons and stand-alone muons
231  // (and caloMuons, if they were ever to get into the input muons
232  // collection).
233  if (!muon->isGlobalMuon())
234  continue;
235 
236  if (fromTeVRefit || fromSigmaSwitch) {
237  // Start out with a null TrackRef.
239 
240  // If making a cocktail muon, use tevOptimized() to get the track
241  // desired. Otherwise, get the refit track from the desired track
242  // map.
243  if (fromTMR)
245  else if (fromCocktail)
246  tevTk = muon::tevOptimized(*muon);
247  else if (fromSigmaSwitch)
249  else {
250  reco::TrackToTrackMap::const_iterator tevTkRef = trackMap->find(muon->combinedMuon());
251  if (tevTkRef != trackMap->end())
252  tevTk = make_pair(tevTkRef->val, reco::Muon::CombinedTrack);
253  }
254 
255  // If the TrackRef is valid, make a new Muon that has the same
256  // tracker and stand-alone tracks, but has the refit track as
257  // its global track.
258  if (tevTk.first.isNonnull())
259  cands->push_back(*cloneAndSwitchTrack(*muon, tevTk));
260  } else if (fromTrackerTrack)
261  cands->push_back(*cloneAndSwitchTrack(*muon, make_pair(muon->innerTrack(), reco::Muon::InnerTrack)));
262  else if (fromGlobalTrack)
263  cands->push_back(*cloneAndSwitchTrack(*muon, make_pair(muon->globalTrack(), reco::Muon::CombinedTrack)));
264  else {
265  cands->push_back(*muon->clone());
266 
267  // Just cloning does not work in the case of the source being
268  // a pat::Muon with embedded track references -- these do not
269  // get copied. Explicitly set them.
270  reco::Muon& last = cands->at(cands->size() - 1);
271  if (muon->globalTrack().isTransient())
272  last.setGlobalTrack(muon->globalTrack());
273  if (muon->innerTrack().isTransient())
274  last.setInnerTrack(muon->innerTrack());
275  if (muon->outerTrack().isTransient())
276  last.setOuterTrack(muon->outerTrack());
277  }
278  }
279  } else
280  edm::LogWarning("MuonsFromRefitTracksProducer") << "either " << src << " or the track map(s) " << tevMuonTracks
281  << " not present in the event; producing empty collection";
282 
283  event.put(std::move(cands));
284 }
285 
reco::Muon::MuonTrackTypePair sigmaSwitch(const reco::Muon &muon, const double nSigma, const double ptThreshold)
edm::EDGetTokenT< reco::TrackToTrackMap > trackMapPickyToken_
static double constexpr muMass
Muon mass [GeV].
Definition: Constants.h:14
edm::EDGetTokenT< reco::TrackToTrackMap > trackMapToken_
muons
the two sets of parameters below are mutually exclusive, depending if RECO or ALCARECO is used the us...
Definition: DiMuonV_cfg.py:212
edm::Handle< reco::TrackToTrackMap > trackMapDefault
const_iterator find(const key_type &k) const
find element with specified reference key
edm::EDGetTokenT< reco::TrackToTrackMap > trackMapDefaultToken_
const_iterator end() const
last iterator over the map (read only)
edm::Handle< reco::TrackToTrackMap > trackMap
bool storeMatchMaps(const edm::Event &event)
reco::Muon::MuonTrackTypePair tevOptimized(const reco::TrackRef &combinedTrack, const reco::TrackRef &trackerTrack, const reco::TrackRef &tpfmsTrack, const reco::TrackRef &pickyTrack, const reco::TrackRef &dytTrack, const double ptThreshold=200., const double tune1=17., const double tune2=40., const double dptcut=0.25)
Definition: MuonCocktails.cc:9
void produce(edm::Event &, const edm::EventSetup &) override
T sqrt(T t)
Definition: SSEVec.h:19
edm::Handle< reco::TrackToTrackMap > trackMapFirstHit
bool isAvailable() const
Definition: Ref.h:541
math::XYZPoint Point
point in the space
Definition: Particle.h:25
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
reco::Muon::MuonTrackTypePair tevOptimizedTMR(const reco::Muon &muon, const reco::TrackToTrackMap &fmsMap, const double cut)
MuonsFromRefitTracksProducer(const edm::ParameterSet &)
edm::Handle< reco::TrackToTrackMap > trackMapPicky
reco::Muon * cloneAndSwitchTrack(const reco::Muon &muon, const reco::Muon::MuonTrackTypePair &newTrack) const
edm::EDGetTokenT< edm::View< reco::Muon > > srcToken_
double trackProbability(const reco::TrackRef track)
HLT enums.
boost::indirect_iterator< typename seq_t::const_iterator > const_iterator
Definition: View.h:88
Log< level::Warning, false > LogWarning
std::pair< TrackRef, Muon::MuonTrackType > MuonTrackTypePair
Definition: Muon.h:38
math::XYZTLorentzVector LorentzVector
Lorentz vector.
Definition: Particle.h:21
def move(src, dest)
Definition: eostools.py:511
edm::EDGetTokenT< reco::TrackToTrackMap > trackMapFirstHitToken_
Definition: event.py:1