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