CMS 3D CMS Logo

MuonBeamspotConstraintValueMapProducer.cc
Go to the documentation of this file.
1 
17 
19 public:
21  : muonToken_(consumes<pat::MuonCollection>(config.getParameter<edm::InputTag>("src"))),
22  beamSpotToken_(consumes<reco::BeamSpot>(config.getParameter<edm::InputTag>("beamspot"))),
23  PrimaryVertexToken_(consumes<reco::VertexCollection>(config.getParameter<edm::InputTag>("vertices"))),
24  ttbToken_(esConsumes(edm::ESInputTag("", "TransientTrackBuilder"))) {
25  produces<edm::ValueMap<float>>("muonBSConstrainedPt");
26  produces<edm::ValueMap<float>>("muonBSConstrainedPtErr");
27  produces<edm::ValueMap<float>>("muonBSConstrainedChi2");
28  }
29 
30  ~MuonBeamspotConstraintValueMapProducer() override = default;
31 
32  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
34  desc.add<edm::InputTag>("src", edm::InputTag("muons"))->setComment("Muon collection");
35  desc.add<edm::InputTag>("beamspot", edm::InputTag("offlineBeamSpot"))->setComment("Beam spot collection");
36  desc.add<edm::InputTag>("vertices", edm::InputTag("offlineSlimmedPrimaryVertices"))
37  ->setComment("Primary vertex collection");
38 
39  descriptions.addWithDefaultLabel(desc);
40  }
41 
42 private:
43  void produce(edm::StreamID streamID, edm::Event& event, const edm::EventSetup& setup) const override {
45  event.getByToken(muonToken_, muons);
46 
47  edm::Handle<reco::BeamSpot> beamSpotHandle;
48  event.getByToken(beamSpotToken_, beamSpotHandle);
49 
51 
52  std::vector<float> pts, ptErrs, chi2s;
53  pts.reserve(muons->size());
54  ptErrs.reserve(muons->size());
55  chi2s.reserve(muons->size());
56 
57  for (const auto& muon : *muons) {
58  bool tbd = true;
59  if (beamSpotHandle.isValid()) {
60  double BeamWidthX = beamSpotHandle->BeamWidthX();
61  double BeamWidthXError = beamSpotHandle->BeamWidthXError();
62  double BeamWidthY = beamSpotHandle->BeamWidthY();
63  double BeamWidthYError = beamSpotHandle->BeamWidthYError();
64  // Protect for mis-reconstructed beamspots (note that
65  // SingleTrackVertexConstraint uses the width for the constraint,
66  // not the error)
67  if ((BeamWidthXError / BeamWidthX < 0.3) && (BeamWidthYError / BeamWidthY < 0.3)) {
69  stvc.constrain(ttkb->build(muon.muonBestTrack()), *beamSpotHandle);
70  if (std::get<0>(btft)) {
71  const reco::Track& trkBS = std::get<1>(btft).track();
72  pts.push_back(trkBS.pt());
73  ptErrs.push_back(trkBS.ptError());
74  chi2s.push_back(std::get<2>(btft));
75  tbd = false;
76  }
77  }
78  }
79 
80  if (tbd) {
81  // Invalid BS; use PV instead
83  event.getByToken(PrimaryVertexToken_, pvHandle);
84 
85  if (pvHandle.isValid() && !pvHandle->empty()) {
86  auto pv = pvHandle->at(0);
88 
89  SingleTrackVertexConstraint::BTFtuple btft = stvc.constrain(ttkb->build(muon.muonBestTrack()), pvs);
90  if (std::get<0>(btft)) {
91  const reco::Track& trkBS = std::get<1>(btft).track();
92  pts.push_back(trkBS.pt());
93  ptErrs.push_back(trkBS.ptError());
94  chi2s.push_back(std::get<2>(btft));
95  tbd = false;
96  }
97  }
98  }
99 
100  if (tbd) {
101  // Fall-back case, keep the unconstrained values
102  pts.push_back(muon.pt());
103  ptErrs.push_back(muon.bestTrack()->ptError());
104  chi2s.push_back(-1.f);
105  }
106  }
107 
108  {
109  std::unique_ptr<edm::ValueMap<float>> valueMap(new edm::ValueMap<float>());
111  filler.insert(muons, pts.begin(), pts.end());
112  filler.fill();
113  event.put(std::move(valueMap), "muonBSConstrainedPt");
114  }
115 
116  {
117  std::unique_ptr<edm::ValueMap<float>> valueMap(new edm::ValueMap<float>());
119  filler.insert(muons, ptErrs.begin(), ptErrs.end());
120  filler.fill();
121  event.put(std::move(valueMap), "muonBSConstrainedPtErr");
122  }
123 
124  {
125  std::unique_ptr<edm::ValueMap<float>> valueMap(new edm::ValueMap<float>());
127  filler.insert(muons, chi2s.begin(), chi2s.end());
128  filler.fill();
129  event.put(std::move(valueMap), "muonBSConstrainedChi2");
130  }
131  }
132 
138 };
139 
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
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