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)) {
70  stvc.constrain(ttkb->build(muon.muonBestTrack()), *beamSpotHandle);
71  if (std::get<0>(btft)) {
72  const reco::Track& trkBS = std::get<1>(btft).track();
73  pts.push_back(trkBS.pt());
74  ptErrs.push_back(trkBS.ptError());
75  chi2s.push_back(std::get<2>(btft));
76  tbd = false;
77  }
78  }
79  }
80 
81  if (tbd) {
82  // Invalid BS; use PV instead
84  event.getByToken(PrimaryVertexToken_, pvHandle);
85 
86  if (pvHandle.isValid() && !pvHandle->empty()) {
87  auto pv = pvHandle->at(0);
89 
90  try {
91  SingleTrackVertexConstraint::BTFtuple btft = stvc.constrain(ttkb->build(muon.muonBestTrack()), pvs);
92  if (std::get<0>(btft)) {
93  const reco::Track& trkBS = std::get<1>(btft).track();
94  pts.push_back(trkBS.pt());
95  ptErrs.push_back(trkBS.ptError());
96  chi2s.push_back(std::get<2>(btft));
97  tbd = false;
98  }
99  } catch (const VertexException& exc) {
100  // Update failed; give up.
101  }
102  }
103  }
104 
105  if (tbd) {
106  // Fall-back case, keep the unconstrained values
107  pts.push_back(muon.pt());
108  ptErrs.push_back(muon.bestTrack()->ptError());
109  chi2s.push_back(-1.f);
110  }
111  }
112 
113  {
114  std::unique_ptr<edm::ValueMap<float>> valueMap(new edm::ValueMap<float>());
116  filler.insert(muons, pts.begin(), pts.end());
117  filler.fill();
118  event.put(std::move(valueMap), "muonBSConstrainedPt");
119  }
120 
121  {
122  std::unique_ptr<edm::ValueMap<float>> valueMap(new edm::ValueMap<float>());
124  filler.insert(muons, ptErrs.begin(), ptErrs.end());
125  filler.fill();
126  event.put(std::move(valueMap), "muonBSConstrainedPtErr");
127  }
128 
129  {
130  std::unique_ptr<edm::ValueMap<float>> valueMap(new edm::ValueMap<float>());
132  filler.insert(muons, chi2s.begin(), chi2s.end());
133  filler.fill();
134  event.put(std::move(valueMap), "muonBSConstrainedChi2");
135  }
136  }
137 
143 };
144 
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