CMS 3D CMS Logo

LeptonUpdater.cc
Go to the documentation of this file.
10 
15 
16 #include <type_traits>
17 
18 namespace pat {
19 
20  template <typename T>
22  public:
23  explicit LeptonUpdater(const edm::ParameterSet &iConfig)
24  : src_(consumes<std::vector<T>>(iConfig.getParameter<edm::InputTag>("src"))),
25  vertices_(consumes<std::vector<reco::Vertex>>(iConfig.getParameter<edm::InputTag>("vertices"))),
26  beamLineToken_(consumes<reco::BeamSpot>(iConfig.getParameter<edm::InputTag>("beamspot"))),
27  computeMiniIso_(iConfig.getParameter<bool>("computeMiniIso")),
28  fixDxySign_(iConfig.getParameter<bool>("fixDxySign")) {
29  //for mini-isolation calculation
30  if (computeMiniIso_) {
31  readMiniIsoParams(iConfig);
32  pcToken_ = consumes<pat::PackedCandidateCollection>(iConfig.getParameter<edm::InputTag>("pfCandsForMiniIso"));
33  }
35  if (typeid(T) == typeid(pat::Muon))
36  recomputeMuonBasicSelectors_ = iConfig.getParameter<bool>("recomputeMuonBasicSelectors");
37  produces<std::vector<T>>();
38  }
39 
40  ~LeptonUpdater() override {}
41 
42  void produce(edm::StreamID, edm::Event &, edm::EventSetup const &) const override;
43 
44  static void fillDescriptions(edm::ConfigurationDescriptions &descriptions) {
46  desc.add<edm::InputTag>("src")->setComment("Lepton collection");
47  desc.add<edm::InputTag>("vertices")->setComment("Vertex collection");
48  desc.add<edm::InputTag>("beamspot", edm::InputTag("offlineBeamSpot"))->setComment("Beam spot");
49  desc.add<bool>("computeMiniIso", false)->setComment("Recompute miniIsolation");
50  desc.add<bool>("fixDxySign", false)->setComment("Fix the IP sign");
51  desc.addOptional<edm::InputTag>("pfCandsForMiniIso", edm::InputTag("packedPFCandidates"))
52  ->setComment("PackedCandidate collection used for miniIso");
53  if (typeid(T) == typeid(pat::Muon)) {
54  desc.add<bool>("recomputeMuonBasicSelectors", false)
55  ->setComment("Recompute basic cut-based muon selector flags");
56  desc.addOptional<std::vector<double>>("miniIsoParams")
57  ->setComment("Parameters used for miniIso (as in PATMuonProducer)");
58  descriptions.add("muonsUpdated", desc);
59  } else if (typeid(T) == typeid(pat::Electron)) {
60  desc.addOptional<std::vector<double>>("miniIsoParamsB")
61  ->setComment("Parameters used for miniIso in the barrel (as in PATElectronProducer)");
62  desc.addOptional<std::vector<double>>("miniIsoParamsE")
63  ->setComment("Parameters used for miniIso in the endcap (as in PATElectronProducer)");
64  descriptions.add("electronsUpdated", desc);
65  }
66  }
67 
68  void setDZ(T &lep, const reco::Vertex &pv) const {}
69 
70  void readMiniIsoParams(const edm::ParameterSet &iConfig) {
71  miniIsoParams_[0] = iConfig.getParameter<std::vector<double>>("miniIsoParams");
72  if (miniIsoParams_[0].size() != 9)
73  throw cms::Exception("ParameterError", "miniIsoParams must have exactly 9 elements.\n");
74  }
75  const std::vector<double> &miniIsoParams(const T &lep) const { return miniIsoParams_[0]; }
76 
77  void recomputeMuonBasicSelectors(T &, const reco::Vertex &, const bool) const;
78 
79  private:
80  // configurables
87  std::vector<double> miniIsoParams_[2];
89  };
90 
91  // must do the specialization within the namespace otherwise gcc complains
92  //
93  template <>
95  auto track = anElectron.gsfTrack();
96  anElectron.setDB(track->dz(pv.position()), std::hypot(track->dzError(), pv.zError()), pat::Electron::PVDZ);
97  }
98 
99  template <>
101  auto track = aMuon.muonBestTrack();
102  aMuon.setDB(track->dz(pv.position()), std::hypot(track->dzError(), pv.zError()), pat::Muon::PVDZ);
103  }
104 
105  template <>
107  miniIsoParams_[0] = iConfig.getParameter<std::vector<double>>("miniIsoParamsB");
108  miniIsoParams_[1] = iConfig.getParameter<std::vector<double>>("miniIsoParamsE");
109  if (miniIsoParams_[0].size() != 9)
110  throw cms::Exception("ParameterError", "miniIsoParamsB must have exactly 9 elements.\n");
111  if (miniIsoParams_[1].size() != 9)
112  throw cms::Exception("ParameterError", "miniIsoParamsE must have exactly 9 elements.\n");
113  }
114  template <>
115  const std::vector<double> &LeptonUpdater<pat::Electron>::miniIsoParams(const pat::Electron &lep) const {
116  return miniIsoParams_[lep.isEE()];
117  }
118 
119  template <typename T>
121  const reco::Vertex &pv,
122  const bool do_hip_mitigation_2016) const {}
123 
124  template <>
126  const reco::Vertex &pv,
127  const bool do_hip_mitigation_2016) const {
128  lep.setSelectors(muon::makeSelectorBitset(lep, &pv, do_hip_mitigation_2016));
129  }
130 
131 } // namespace pat
132 
133 template <typename T>
136  iEvent.getByToken(src_, src);
137 
139  iEvent.getByToken(vertices_, vertices);
140  const reco::Vertex &pv = vertices->front();
141 
143  if (computeMiniIso_)
144  iEvent.getByToken(pcToken_, pc);
145 
146  edm::Handle<reco::BeamSpot> beamSpotHandle;
147  iEvent.getByToken(beamLineToken_, beamSpotHandle);
149  bool beamSpotIsValid = false;
150  if (beamSpotHandle.isValid()) {
151  beamSpot = *beamSpotHandle;
152  beamSpotIsValid = true;
153  } else {
154  edm::LogError("DataNotAvailable") << "No beam spot available \n";
155  }
156 
157  std::unique_ptr<std::vector<T>> out(new std::vector<T>(*src));
158 
159  const bool do_hip_mitigation_2016 =
160  recomputeMuonBasicSelectors_ && (272728 <= iEvent.run() && iEvent.run() <= 278808);
161 
162  for (unsigned int i = 0, n = src->size(); i < n; ++i) {
163  T &lep = (*out)[i];
164  setDZ(lep, pv);
165  if (computeMiniIso_) {
166  const auto &params = miniIsoParams(lep);
168  lep.polarP4(),
169  params[0],
170  params[1],
171  params[2],
172  params[3],
173  params[4],
174  params[5],
175  params[6],
176  params[7],
177  params[8]);
178  lep.setMiniPFIsolation(miniiso);
179  }
180  if (recomputeMuonBasicSelectors_)
181  recomputeMuonBasicSelectors(lep, pv, do_hip_mitigation_2016);
182  //Fixing the sign of impact parameters
183  if (fixDxySign_) {
184  float signPV = 1.;
185  float signBS = 1.;
186  if (beamSpotIsValid) {
187  if constexpr (std::is_same_v<T, pat::Electron>)
188  signBS = copysign(1., lep.gsfTrack()->dxy(beamSpot));
189  else
190  signBS = copysign(1., lep.bestTrack()->dxy(beamSpot));
191  }
192  if constexpr (std::is_same_v<T, pat::Electron>)
193  signPV = copysign(1., lep.gsfTrack()->dxy(pv.position()));
194  else
195  signPV = copysign(1., lep.bestTrack()->dxy(pv.position()));
196  lep.setDB(abs(lep.dB(T::PV2D)) * signPV, lep.edB(T::PV2D), T::PV2D);
197  lep.setDB(abs(lep.dB(T::BS2D)) * signBS, lep.edB(T::BS2D), T::BS2D);
198  }
199  }
200 
201  iEvent.put(std::move(out));
202 }
203 
206 
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:307
void setDZ(T &lep, const reco::Vertex &pv) const
MPlex< T, D1, D2, N > hypot(const MPlex< T, D1, D2, N > &a, const MPlex< T, D1, D2, N > &b)
Definition: Matriplex.h:616
pat::LeptonUpdater< pat::Electron > PATElectronUpdater
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:244
~LeptonUpdater() override
recomputeMuonBasicSelectors
Definition: muons_cff.py:17
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
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