CMS 3D CMS Logo

MuonBeamspotConstraintValueMapProducer.cc
Go to the documentation of this file.
1 
18 
20 public:
22  : muonToken_(consumes<pat::MuonCollection>(config.getParameter<edm::InputTag>("src"))),
23  beamSpotToken_(consumes<reco::BeamSpot>(config.getParameter<edm::InputTag>("beamspot"))),
24  PrimaryVertexToken_(consumes<reco::VertexCollection>(config.getParameter<edm::InputTag>("vertices"))),
25  ttbToken_(esConsumes(edm::ESInputTag("", "TransientTrackBuilder"))) {
26  produces<edm::ValueMap<float>>("muonBSConstrainedPt");
27  produces<edm::ValueMap<float>>("muonBSConstrainedPtErr");
28  produces<edm::ValueMap<float>>("muonBSConstrainedChi2");
29  }
30 
31  ~MuonBeamspotConstraintValueMapProducer() override = default;
32 
33  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
35  desc.add<edm::InputTag>("src", edm::InputTag("muons"))->setComment("Muon collection");
36  desc.add<edm::InputTag>("beamspot", edm::InputTag("offlineBeamSpot"))->setComment("Beam spot collection");
37  desc.add<edm::InputTag>("vertices", edm::InputTag("offlineSlimmedPrimaryVertices"))
38  ->setComment("Primary vertex collection");
39 
40  descriptions.addWithDefaultLabel(desc);
41  }
42 
43 private:
44  void produce(edm::StreamID streamID, edm::Event& event, const edm::EventSetup& setup) const override {
46  event.getByToken(muonToken_, muons);
47 
48  edm::Handle<reco::BeamSpot> beamSpotHandle;
49  event.getByToken(beamSpotToken_, beamSpotHandle);
50 
52 
53  std::vector<float> pts, ptErrs, chi2s;
54  pts.reserve(muons->size());
55  ptErrs.reserve(muons->size());
56  chi2s.reserve(muons->size());
57 
58  for (const auto& muon : *muons) {
59  bool tbd = true;
60  if (beamSpotHandle.isValid()) {
61  double BeamWidthX = beamSpotHandle->BeamWidthX();
62  double BeamWidthXError = beamSpotHandle->BeamWidthXError();
63  double BeamWidthY = beamSpotHandle->BeamWidthY();
64  double BeamWidthYError = beamSpotHandle->BeamWidthYError();
65  // Protect for mis-reconstructed beamspots (note that
66  // SingleTrackVertexConstraint uses the width for the constraint,
67  // not the error)
68  if ((BeamWidthXError / BeamWidthX < 0.3) && (BeamWidthYError / BeamWidthY < 0.3)) {
69  try {
71  stvc.constrain(ttkb->build(muon.muonBestTrack()), *beamSpotHandle);
72  if (std::get<0>(btft)) {
73  const reco::Track& trkBS = std::get<1>(btft).track();
74  pts.push_back(trkBS.pt());
75  ptErrs.push_back(trkBS.ptError());
76  chi2s.push_back(std::get<2>(btft));
77  tbd = false;
78  }
79  } catch (const VertexException& exc) {
80  // Update failed; give up.
81  }
82  }
83  }
84 
85  if (tbd) {
86  // Invalid BS; use PV instead
88  event.getByToken(PrimaryVertexToken_, pvHandle);
89 
90  if (pvHandle.isValid() && !pvHandle->empty()) {
91  auto pv = pvHandle->at(0);
93 
94  try {
95  SingleTrackVertexConstraint::BTFtuple btft = stvc.constrain(ttkb->build(muon.muonBestTrack()), pvs);
96  if (std::get<0>(btft)) {
97  const reco::Track& trkBS = std::get<1>(btft).track();
98  pts.push_back(trkBS.pt());
99  ptErrs.push_back(trkBS.ptError());
100  chi2s.push_back(std::get<2>(btft));
101  tbd = false;
102  }
103  } catch (const VertexException& exc) {
104  // Update failed; give up.
105  }
106  }
107  }
108 
109  if (tbd) {
110  // Fall-back case, keep the unconstrained values
111  pts.push_back(muon.pt());
112  ptErrs.push_back(muon.bestTrack()->ptError());
113  chi2s.push_back(-1.f);
114  }
115  }
116 
117  {
118  std::unique_ptr<edm::ValueMap<float>> valueMap(new edm::ValueMap<float>());
120  filler.insert(muons, pts.begin(), pts.end());
121  filler.fill();
122  event.put(std::move(valueMap), "muonBSConstrainedPt");
123  }
124 
125  {
126  std::unique_ptr<edm::ValueMap<float>> valueMap(new edm::ValueMap<float>());
128  filler.insert(muons, ptErrs.begin(), ptErrs.end());
129  filler.fill();
130  event.put(std::move(valueMap), "muonBSConstrainedPtErr");
131  }
132 
133  {
134  std::unique_ptr<edm::ValueMap<float>> valueMap(new edm::ValueMap<float>());
136  filler.insert(muons, chi2s.begin(), chi2s.end());
137  filler.fill();
138  event.put(std::move(valueMap), "muonBSConstrainedChi2");
139  }
140  }
141 
147 };
148 
double BeamWidthX() const
beam width X
Definition: BeamSpot.h:82
void addWithDefaultLabel(ParameterSetDescription const &psetDescription)
void produce(edm::StreamID streamID, edm::Event &event, const edm::EventSetup &setup) const override
double BeamWidthYError() const
error on beam width Y, assume error in X = Y
Definition: BeamSpot.h:101
double ptError() const
error on Pt (set to 1000 TeV if charge==0 for safety)
Definition: TrackBase.h:754
std::tuple< bool, reco::TransientTrack, float > BTFtuple
Common base class.
Global3DPoint GlobalPoint
Definition: GlobalPoint.h:10
GlobalErrorBase< double, ErrorMatrixTag > GlobalError
Definition: GlobalError.h:13
edm::ESGetToken< TransientTrackBuilder, TransientTrackRecord > ttbToken_
Definition: config.py:1
std::vector< Vertex > VertexCollection
Definition: Vertex.h:31
muons
the two sets of parameters below are mutually exclusive, depending if RECO or ALCARECO is used the us...
Definition: DiMuonV_cfg.py:214
std::vector< Muon > MuonCollection
collection of Muon objects
Definition: MuonFwd.h:9
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
Definition: HeavyIon.h:7
BTFtuple constrain(const reco::TransientTrack &track, const GlobalPoint &priorPos, const GlobalError &priorError) const
reco::TransientTrack build(const reco::Track *p) const
static const double pts[33]
Definition: Constants.h:30
double pt() const
track transverse momentum
Definition: TrackBase.h:637
MuonBeamspotConstraintValueMapProducer(const edm::ParameterSet &config)
double BeamWidthY() const
beam width Y
Definition: BeamSpot.h:84
double f[11][100]
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
double BeamWidthXError() const
error on beam width X, assume error in X = Y
Definition: BeamSpot.h:99
bool isValid() const
Definition: HandleBase.h:70
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
fixed size matrix
HLT enums.
edm::EDGetTokenT< reco::VertexCollection > PrimaryVertexToken_
~MuonBeamspotConstraintValueMapProducer() override=default
def move(src, dest)
Definition: eostools.py:511
Definition: event.py:1