CMS 3D CMS Logo

LeptonUpdater.cc
Go to the documentation of this file.
10 
15 
16 namespace pat {
17 
18  template <typename T>
20  public:
21  explicit LeptonUpdater(const edm::ParameterSet &iConfig)
22  : src_(consumes<std::vector<T>>(iConfig.getParameter<edm::InputTag>("src"))),
23  vertices_(consumes<std::vector<reco::Vertex>>(iConfig.getParameter<edm::InputTag>("vertices"))),
24  beamLineToken_(consumes<reco::BeamSpot>(iConfig.getParameter<edm::InputTag>("beamspot"))),
25  computeMiniIso_(iConfig.getParameter<bool>("computeMiniIso")),
26  fixDxySign_(iConfig.getParameter<bool>("fixDxySign")) {
27  //for mini-isolation calculation
28  if (computeMiniIso_) {
29  readMiniIsoParams(iConfig);
30  pcToken_ = consumes<pat::PackedCandidateCollection>(iConfig.getParameter<edm::InputTag>("pfCandsForMiniIso"));
31  }
33  if (typeid(T) == typeid(pat::Muon))
34  recomputeMuonBasicSelectors_ = iConfig.getParameter<bool>("recomputeMuonBasicSelectors");
35  produces<std::vector<T>>();
36  }
37 
38  ~LeptonUpdater() override {}
39 
40  void produce(edm::StreamID, edm::Event &, edm::EventSetup const &) const override;
41 
42  static void fillDescriptions(edm::ConfigurationDescriptions &descriptions) {
44  desc.add<edm::InputTag>("src")->setComment("Lepton collection");
45  desc.add<edm::InputTag>("vertices")->setComment("Vertex collection");
46  desc.add<edm::InputTag>("beamspot", edm::InputTag("offlineBeamSpot"))->setComment("Beam spot");
47  desc.add<bool>("computeMiniIso", false)->setComment("Recompute miniIsolation");
48  desc.add<bool>("fixDxySign", false)->setComment("Fix the IP sign");
49  desc.addOptional<edm::InputTag>("pfCandsForMiniIso", edm::InputTag("packedPFCandidates"))
50  ->setComment("PackedCandidate collection used for miniIso");
51  if (typeid(T) == typeid(pat::Muon)) {
52  desc.add<bool>("recomputeMuonBasicSelectors", false)
53  ->setComment("Recompute basic cut-based muon selector flags");
54  desc.addOptional<std::vector<double>>("miniIsoParams")
55  ->setComment("Parameters used for miniIso (as in PATMuonProducer)");
56  descriptions.add("muonsUpdated", desc);
57  } else if (typeid(T) == typeid(pat::Electron)) {
58  desc.addOptional<std::vector<double>>("miniIsoParamsB")
59  ->setComment("Parameters used for miniIso in the barrel (as in PATElectronProducer)");
60  desc.addOptional<std::vector<double>>("miniIsoParamsE")
61  ->setComment("Parameters used for miniIso in the endcap (as in PATElectronProducer)");
62  descriptions.add("electronsUpdated", desc);
63  }
64  }
65 
66  void setDZ(T &lep, const reco::Vertex &pv) const {}
67 
68  void readMiniIsoParams(const edm::ParameterSet &iConfig) {
69  miniIsoParams_[0] = iConfig.getParameter<std::vector<double>>("miniIsoParams");
70  if (miniIsoParams_[0].size() != 9)
71  throw cms::Exception("ParameterError", "miniIsoParams must have exactly 9 elements.\n");
72  }
73  const std::vector<double> &miniIsoParams(const T &lep) const { return miniIsoParams_[0]; }
74 
75  void recomputeMuonBasicSelectors(T &, const reco::Vertex &, const bool) const;
76 
77  private:
78  // configurables
85  std::vector<double> miniIsoParams_[2];
87  };
88 
89  // must do the specialization within the namespace otherwise gcc complains
90  //
91  template <>
93  auto track = anElectron.gsfTrack();
94  anElectron.setDB(track->dz(pv.position()), std::hypot(track->dzError(), pv.zError()), pat::Electron::PVDZ);
95  }
96 
97  template <>
99  auto track = aMuon.muonBestTrack();
100  aMuon.setDB(track->dz(pv.position()), std::hypot(track->dzError(), pv.zError()), pat::Muon::PVDZ);
101  }
102 
103  template <>
105  miniIsoParams_[0] = iConfig.getParameter<std::vector<double>>("miniIsoParamsB");
106  miniIsoParams_[1] = iConfig.getParameter<std::vector<double>>("miniIsoParamsE");
107  if (miniIsoParams_[0].size() != 9)
108  throw cms::Exception("ParameterError", "miniIsoParamsB must have exactly 9 elements.\n");
109  if (miniIsoParams_[1].size() != 9)
110  throw cms::Exception("ParameterError", "miniIsoParamsE must have exactly 9 elements.\n");
111  }
112  template <>
113  const std::vector<double> &LeptonUpdater<pat::Electron>::miniIsoParams(const pat::Electron &lep) const {
114  return miniIsoParams_[lep.isEE()];
115  }
116 
117  template <typename T>
119  const reco::Vertex &pv,
120  const bool do_hip_mitigation_2016) const {}
121 
122  template <>
124  const reco::Vertex &pv,
125  const bool do_hip_mitigation_2016) const {
126  lep.setSelectors(muon::makeSelectorBitset(lep, &pv, do_hip_mitigation_2016));
127  }
128 
129 } // namespace pat
130 
131 template <typename T>
134  iEvent.getByToken(src_, src);
135 
137  iEvent.getByToken(vertices_, vertices);
138  const reco::Vertex &pv = vertices->front();
139 
141  if (computeMiniIso_)
142  iEvent.getByToken(pcToken_, pc);
143 
144  edm::Handle<reco::BeamSpot> beamSpotHandle;
145  iEvent.getByToken(beamLineToken_, beamSpotHandle);
147  bool beamSpotIsValid = false;
148  if (beamSpotHandle.isValid()) {
149  beamSpot = *beamSpotHandle;
150  beamSpotIsValid = true;
151  } else {
152  edm::LogError("DataNotAvailable") << "No beam spot available \n";
153  }
154 
155  std::unique_ptr<std::vector<T>> out(new std::vector<T>(*src));
156 
157  const bool do_hip_mitigation_2016 =
158  recomputeMuonBasicSelectors_ && (272728 <= iEvent.run() && iEvent.run() <= 278808);
159 
160  for (unsigned int i = 0, n = src->size(); i < n; ++i) {
161  T &lep = (*out)[i];
162  setDZ(lep, pv);
163  if (computeMiniIso_) {
164  const auto &params = miniIsoParams(lep);
166  lep.polarP4(),
167  params[0],
168  params[1],
169  params[2],
170  params[3],
171  params[4],
172  params[5],
173  params[6],
174  params[7],
175  params[8]);
176  lep.setMiniPFIsolation(miniiso);
177  }
178  if (recomputeMuonBasicSelectors_)
179  recomputeMuonBasicSelectors(lep, pv, do_hip_mitigation_2016);
180  //Fixing the sign of impact parameters
181  if (fixDxySign_) {
182  float signPV = 1.;
183  float signBS = 1.;
184  if (beamSpotIsValid) {
185  if constexpr (std::is_same_v<T, pat::Electron>)
186  signBS = copysign(1., lep.gsfTrack()->dxy(beamSpot));
187  else
188  signBS = copysign(1., lep.bestTrack()->dxy(beamSpot));
189  }
190  if constexpr (std::is_same_v<T, pat::Electron>)
191  signPV = copysign(1., lep.gsfTrack()->dxy(pv.position()));
192  else
193  signPV = copysign(1., lep.bestTrack()->dxy(pv.position()));
194  lep.setDB(abs(lep.dB(T::PV2D)) * signPV, lep.edB(T::PV2D), T::PV2D);
195  lep.setDB(abs(lep.dB(T::BS2D)) * signBS, lep.edB(T::BS2D), T::BS2D);
196  }
197  }
198 
199  iEvent.put(std::move(out));
200 }
201 
204 
size
Write out results.
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
void produce(edm::StreamID, edm::Event &, edm::EventSetup const &) const override
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
void setDZ(T &lep, const reco::Vertex &pv) const
pat::LeptonUpdater< pat::Electron > PATElectronUpdater
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
T const * product() const
Definition: Handle.h:70
LeptonUpdater(const edm::ParameterSet &iConfig)
reco::GsfTrackRef gsfTrack() const override
override the reco::GsfElectron::gsfTrack method, to access the internal storage of the supercluster ...
Log< level::Error, false > LogError
reco::Muon::Selector makeSelectorBitset(reco::Muon const &muon, reco::Vertex const *vertex=nullptr, bool run2016_hip_mitigation=false)
edm::EDGetTokenT< std::vector< T > > src_
pat::LeptonUpdater< pat::Muon > PATMuonUpdater
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
Definition: HeavyIon.h:7
edm::EDGetTokenT< reco::BeamSpot > beamLineToken_
void setDB(double dB, double edB, IPTYPE type)
Set impact parameter of a certain type and its uncertainty.
int iEvent
Definition: GenABIO.cc:224
void setDB(double dB, double edB, IPTYPE type=PV2D)
Definition: Muon.h:247
bool recomputeMuonBasicSelectors_
void setSelectors(uint64_t selectors)
Definition: Muon.h:243
~LeptonUpdater() override
recomputeMuonBasicSelectors
Definition: muons_cff.py:15
def pv(vc)
Definition: MetAnalyzer.py:7
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
edm::EDGetTokenT< pat::PackedCandidateCollection > pcToken_
void recomputeMuonBasicSelectors(T &, const reco::Vertex &, const bool) const
bool isEE() const
Definition: GsfElectron.h:329
Analysis-level electron class.
Definition: Electron.h:51
void add(std::string const &label, ParameterSetDescription const &psetDescription)
bool isValid() const
Definition: HandleBase.h:70
void readMiniIsoParams(const edm::ParameterSet &iConfig)
fixed size matrix
HLT enums.
const std::vector< double > & miniIsoParams(const T &lep) const
std::vector< double > miniIsoParams_[2]
PFIsolation getMiniPFIsolation(const pat::PackedCandidateCollection *pfcands, const reco::Candidate::PolarLorentzVector &p4, float mindr=0.05, float maxdr=0.2, float kt_scale=10.0, float ptthresh=0.5, float deadcone_ch=0.0001, float deadcone_pu=0.01, float deadcone_ph=0.01, float deadcone_nh=0.01, float dZ_cut=0.0)
reco::TrackRef muonBestTrack() const override
Track selected to be the best measurement of the muon parameters (including PFlow global information)...
long double T
Analysis-level muon class.
Definition: Muon.h:51
edm::EDGetTokenT< std::vector< reco::Vertex > > vertices_
def move(src, dest)
Definition: eostools.py:511