CMS 3D CMS Logo

HiBadParticleCleaner.cc
Go to the documentation of this file.
1 // system include files
2 #include <memory>
3 
4 // user include files
7 
10 
12 
22 //
23 // class declaration
24 //
25 
27 public:
28  explicit HiBadParticleCleaner(const edm::ParameterSet&);
29  ~HiBadParticleCleaner() override = default;
30 
31 private:
32  void produce(edm::StreamID, edm::Event&, const edm::EventSetup&) const override;
33  // ----------member data ---------------------------
34 
37 
38  const double minMuonPt_;
39  const double minChargedHadronPt_;
40  const double minMuonTrackRelPtErr_;
41  const double maxSigLoose_;
42  const double maxSigTight_;
43  const double minCaloCompatibility_;
44  const unsigned minTrackNHits_;
45  const unsigned minPixelNHits_;
48  const bool reMiniAODBugFix_;
49 };
50 
51 //
52 // constructors and destructor
53 //
55  : tokenPFCandidates_(consumes<edm::View<reco::PFCandidate>>(iConfig.getParameter<edm::InputTag>("PFCandidates"))),
56  tokenPV_(consumes<reco::VertexCollection>(iConfig.getParameter<edm::InputTag>("offlinePV"))),
57  minMuonPt_(iConfig.getParameter<double>("minMuonPt")),
58  minChargedHadronPt_(iConfig.getParameter<double>("minChargedHadronPt")),
59  minMuonTrackRelPtErr_(iConfig.getParameter<double>("minMuonTrackRelPtErr")),
60  maxSigLoose_(iConfig.getParameter<double>("maxSigLoose")),
61  maxSigTight_(iConfig.getParameter<double>("maxSigTight")),
62  minCaloCompatibility_(iConfig.getParameter<double>("minCaloCompatibility")),
63  minTrackNHits_(iConfig.getParameter<uint>("minTrackNHits")),
64  minPixelNHits_(iConfig.getParameter<uint>("minPixelNHits")),
65  minTrackerLayersForMuonLoose_(iConfig.getParameter<int>("minTrackerLayersForMuonLoose")),
66  minTrackerLayersForMuonTight_(iConfig.getParameter<int>("minTrackerLayersForMuonTight")),
67  reMiniAODBugFix_(iConfig.getParameter<bool>("reMiniAODBugFix")) {
68  produces<bool>();
69  produces<reco::PFCandidateCollection>();
70  produces<reco::PFCandidateCollection>("removed");
71  produces<edm::ValueMap<reco::PFCandidateRef>>();
72 }
73 
74 //
75 // member functions
76 //
77 
78 // ------------ method called on each new Event ------------
80  using namespace std;
81  using namespace edm;
82 
86 
87  const reco::VertexCollection* recoVertices;
89  iEvent.getByToken(tokenPV_, vertexCollection);
90  recoVertices = vertexCollection.product();
91 
92  auto pOutputCandidateCollection = std::make_unique<reco::PFCandidateCollection>();
93  auto pBadCandidateCollection = std::make_unique<reco::PFCandidateCollection>();
94 
95  bool foundBadCandidate = false;
96  size_t n = pfCandidates->size();
97  std::vector<int> candidateIndexMapper(n, 0); // mapping between the original PF and post-cleaning PF
98  size_t iPF;
99  for (iPF = 0; iPF < n; iPF++) {
100  const reco::PFCandidate& pfCandidate = pfCandidates->at(iPF);
101 
102  if (pfCandidate.particleId() == reco::PFCandidate::ParticleType::mu) // muon cleaning
103  {
104  if (pfCandidate.pt() > minMuonPt_) {
105  if (!pfCandidate.muonRef()->isGlobalMuon() || !pfCandidate.muonRef()->isTrackerMuon() ||
106  !pfCandidate.trackRef().isNonnull()) {
107  pBadCandidateCollection->push_back(pfCandidate);
108  candidateIndexMapper[iPF] = -1 * (pBadCandidateCollection->size());
109  foundBadCandidate = true;
110  continue;
111  }
112  reco::TrackRef track = pfCandidate.trackRef();
113 
114  if (track->ptError() / track->pt() > minMuonTrackRelPtErr_ || track->pt() < pfCandidate.pt() / 2.) {
115  pBadCandidateCollection->push_back(pfCandidate);
116  candidateIndexMapper[iPF] = -1 * (pBadCandidateCollection->size());
117  foundBadCandidate = true;
118  continue;
119  }
120 
123  track->originalAlgo() == reco::TrackBase::muonSeededStepInOut ||
124  track->originalAlgo() == reco::TrackBase::muonSeededStepOutIn ||
125  track->hitPattern().trackerLayersWithMeasurement() < minTrackerLayersForMuonLoose_) {
126  const reco::Vertex& vtx = (*recoVertices)[0];
127  float bestVzError = vtx.zError();
128  const math::XYZPoint& bestVtx(vtx.position());
129  math::Error<3>::type vtx_cov = vtx.covariance();
130  float dz = std::abs(track->dz(bestVtx));
131  float dxy = std::abs(track->dxy(bestVtx));
132  float dzError2 = track->dzError() * track->dzError() + bestVzError * bestVzError;
133  float dxyError = track->dxyError(bestVtx, vtx_cov);
134 
135  float dzSig2 = dz * dz / dzError2;
136  float dxySig2 = dxy * dxy / dxyError / dxyError;
137 
138  float sig3d = sqrt(dzSig2 + dxySig2);
139 
140  if (sig3d > maxSigLoose_) {
141  pBadCandidateCollection->push_back(pfCandidate);
142  candidateIndexMapper[iPF] = -1 * (pBadCandidateCollection->size());
143  foundBadCandidate = true;
144  continue;
145  }
146 
147  if (track->pt() < pfCandidate.pt() / 1.5 || track->pt() > pfCandidate.pt() * 1.5) {
148  foundBadCandidate = true;
149  pBadCandidateCollection->push_back(pfCandidate);
150  candidateIndexMapper[iPF] = -1 * (pBadCandidateCollection->size());
151  continue;
152  }
153  if (track->originalAlgo() == reco::TrackBase::muonSeededStepOutIn &&
154  track->hitPattern().trackerLayersWithMeasurement() < minTrackerLayersForMuonTight_) {
155  foundBadCandidate = true;
156  pBadCandidateCollection->push_back(pfCandidate);
157  candidateIndexMapper[iPF] = -1 * (pBadCandidateCollection->size());
158  continue;
159  }
160  }
161  }
162  } else if (pfCandidate.particleId() == reco::PFCandidate::ParticleType::h) //charged hadron cleaning
163  {
164  if (pfCandidate.pt() > minChargedHadronPt_) {
165  reco::TrackRef track = pfCandidate.trackRef();
166 
167  unsigned nHits = track->numberOfValidHits();
168  unsigned nPixelHits = track->hitPattern().numberOfValidPixelHits();
169 
170  if ((nHits < minTrackNHits_ && nPixelHits < minPixelNHits_) || nHits == 3) {
171  foundBadCandidate = true;
172  pBadCandidateCollection->push_back(pfCandidate);
173  candidateIndexMapper[iPF] = -1 * (pBadCandidateCollection->size());
174  continue;
175  }
176 
177  const reco::Vertex& vtx = (*recoVertices)[0];
178  float bestVzError = vtx.zError();
179  const math::XYZPoint& bestVtx(vtx.position());
180  math::Error<3>::type vtx_cov = vtx.covariance();
181  float dz = std::abs(track->dz(bestVtx));
182  float dxy = std::abs(track->dxy(bestVtx));
183  float dzError2 = track->dzError() * track->dzError() + bestVzError * bestVzError;
184  float dxyError = track->dxyError(bestVtx, vtx_cov);
185 
186  float dzSig2 = dz * dz / dzError2;
187  float dxySig2 = dxy * dxy / dxyError / dxyError;
188 
189  float sig3d = sqrt(dzSig2 + dxySig2);
190 
191  if (sig3d > maxSigLoose_) {
192  foundBadCandidate = true;
193  pBadCandidateCollection->push_back(pfCandidate);
194  candidateIndexMapper[iPF] = -1 * (pBadCandidateCollection->size());
195  continue;
196  }
197 
198  if (sig3d > maxSigTight_ && nHits < minTrackNHits_) {
199  foundBadCandidate = true;
200  pBadCandidateCollection->push_back(pfCandidate);
201  candidateIndexMapper[iPF] = -1 * (pBadCandidateCollection->size());
202  continue;
203  }
204 
207  track->originalAlgo() == reco::TrackBase::muonSeededStepInOut ||
208  track->originalAlgo() == reco::TrackBase::muonSeededStepOutIn) {
209  if (sig3d > maxSigLoose_) {
210  foundBadCandidate = true;
211  pBadCandidateCollection->push_back(pfCandidate);
212  candidateIndexMapper[iPF] = -1 * (pBadCandidateCollection->size());
213  continue;
214  }
215 
216  if (nHits < minTrackNHits_) {
217  foundBadCandidate = true;
218  pBadCandidateCollection->push_back(pfCandidate);
219  candidateIndexMapper[iPF] = -1 * (pBadCandidateCollection->size());
220  continue;
221  }
222  }
223 
224  double caloEnergy = pfCandidate.ecalEnergy() + pfCandidate.hcalEnergy();
225 
226  if (caloEnergy < track->p() * minCaloCompatibility_) {
227  if (sig3d > maxSigTight_) {
228  foundBadCandidate = true;
229  pBadCandidateCollection->push_back(pfCandidate);
230  candidateIndexMapper[iPF] = -1 * (pBadCandidateCollection->size());
231  continue;
232  }
233 
234  if (nHits < minTrackNHits_) {
235  foundBadCandidate = true;
236  pBadCandidateCollection->push_back(pfCandidate);
237  candidateIndexMapper[iPF] = -1 * (pBadCandidateCollection->size());
238  continue;
239  }
240 
241  if (nPixelHits < minPixelNHits_) {
242  foundBadCandidate = true;
243  pBadCandidateCollection->push_back(pfCandidate);
244  candidateIndexMapper[iPF] = -1 * (pBadCandidateCollection->size());
245  continue;
246  }
247  }
248  }
249  }
250 
251  pOutputCandidateCollection->push_back(pfCandidate);
252  candidateIndexMapper[iPF] = (pOutputCandidateCollection->size());
253  } // end loop over pf candidates
254 
255  bool pass = !foundBadCandidate;
256  edm::OrphanHandle<std::vector<reco::PFCandidate>> newpf = iEvent.put(std::move(pOutputCandidateCollection));
257  edm::OrphanHandle<std::vector<reco::PFCandidate>> badpf = iEvent.put(std::move(pBadCandidateCollection), "removed");
258 
259  iEvent.put(std::make_unique<bool>(pass));
260 
261  if (reMiniAODBugFix_) {
262  std::unique_ptr<edm::ValueMap<reco::PFCandidateRef>> pf2pf(new edm::ValueMap<reco::PFCandidateRef>());
264 
265  std::vector<reco::PFCandidateRef> refs;
266  refs.reserve(n);
267 
268  for (iPF = 0; iPF < n; ++iPF) {
269  if (candidateIndexMapper[iPF] > 0) {
270  refs.push_back(reco::PFCandidateRef(newpf, candidateIndexMapper[iPF] - 1));
271  } else if (candidateIndexMapper[iPF] < 0) {
272  refs.push_back(reco::PFCandidateRef(badpf, -candidateIndexMapper[iPF] - 1));
273  }
274  }
275  filler.insert(pfCandidates, refs.begin(), refs.end());
276 
277  filler.fill();
278  iEvent.put(std::move(pf2pf));
279  }
280 }
281 
282 //define this as a plug-in
reco::PFCandidate::trackRef
reco::TrackRef trackRef() const
Definition: PFCandidate.cc:408
HiBadParticleCleaner::minMuonPt_
const double minMuonPt_
Definition: HiBadParticleCleaner.cc:38
edm::StreamID
Definition: StreamID.h:30
zmumugammaAnalyzer_cfi.pfCandidates
pfCandidates
Definition: zmumugammaAnalyzer_cfi.py:11
reco::CandidateView
edm::View< Candidate > CandidateView
view of a collection containing candidates
Definition: CandidateFwd.h:23
electrons_cff.bool
bool
Definition: electrons_cff.py:393
HLT_FULL_cff.track
track
Definition: HLT_FULL_cff.py:11713
Muon.h
dqmiodumpmetadata.n
n
Definition: dqmiodumpmetadata.py:28
sistrip::View
View
Definition: ConstantsForView.h:26
HiBadParticleCleaner::minTrackerLayersForMuonLoose_
const int minTrackerLayersForMuonLoose_
Definition: HiBadParticleCleaner.cc:46
PFCandidate.h
amptDefaultParameters_cff.mu
mu
Definition: amptDefaultParameters_cff.py:16
edm::EDGetTokenT
Definition: EDGetToken.h:33
edm
HLT enums.
Definition: AlignableModifier.h:19
AlCaHLTBitMon_ParallelJobs.p
p
Definition: AlCaHLTBitMon_ParallelJobs.py:153
h
FWCore Framework interface EventSetupRecordImplementation h
Helper function to determine trigger accepts.
Definition: L1TUtmAlgorithmRcd.h:4
reco::VertexCollection
std::vector< Vertex > VertexCollection
collection of Vertex objects
Definition: VertexFwd.h:9
HLT_FULL_cff.InputTag
InputTag
Definition: HLT_FULL_cff.py:89287
HiBadParticleCleaner::minChargedHadronPt_
const double minChargedHadronPt_
Definition: HiBadParticleCleaner.cc:39
HiBadParticleCleaner::minPixelNHits_
const unsigned minPixelNHits_
Definition: HiBadParticleCleaner.cc:45
HiBadParticleCleaner::minTrackerLayersForMuonTight_
const int minTrackerLayersForMuonTight_
Definition: HiBadParticleCleaner.cc:47
reco::LeafCandidate::pt
double pt() const final
transverse momentum
Definition: LeafCandidate.h:146
reco
fixed size matrix
Definition: AlignmentAlgorithmBase.h:45
edm::Handle
Definition: AssociativeIterator.h:50
parallelization.uint
uint
Definition: parallelization.py:124
HiBadParticleCleaner::HiBadParticleCleaner
HiBadParticleCleaner(const edm::ParameterSet &)
Definition: HiBadParticleCleaner.cc:54
edm::Ref< TrackCollection >
HiBadParticleCleaner::produce
void produce(edm::StreamID, edm::Event &, const edm::EventSetup &) const override
Definition: HiBadParticleCleaner.cc:79
HiBadParticleCleaner::maxSigTight_
const double maxSigTight_
Definition: HiBadParticleCleaner.cc:42
pfElectronTranslator_cfi.PFCandidate
PFCandidate
Definition: pfElectronTranslator_cfi.py:6
MakerMacros.h
reco::TrackBase::muonSeededStepOutIn
Definition: TrackBase.h:104
reco::PFCandidate::muonRef
reco::MuonRef muonRef() const
Definition: PFCandidate.cc:421
Track.h
TrackFwd.h
DEFINE_FWK_MODULE
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
MuonSelectors.h
MuonFwd.h
HiBadParticleCleaner::minTrackNHits_
const unsigned minTrackNHits_
Definition: HiBadParticleCleaner.cc:44
mathSSE::sqrt
T sqrt(T t)
Definition: SSEVec.h:19
HiBadParticleCleaner
Definition: HiBadParticleCleaner.cc:26
HiBadParticleCleaner::reMiniAODBugFix_
const bool reMiniAODBugFix_
Definition: HiBadParticleCleaner.cc:48
HiBadParticleCleaner::minMuonTrackRelPtErr_
const double minMuonTrackRelPtErr_
Definition: HiBadParticleCleaner.cc:40
packedPFCandidateRefMixer_cfi.pf2pf
pf2pf
Definition: packedPFCandidateRefMixer_cfi.py:5
edm::global::EDProducer
Definition: EDProducer.h:32
Vertex.h
edm::View< reco::PFCandidate >
edm::ParameterSet
Definition: ParameterSet.h:47
math::XYZPoint
XYZPointD XYZPoint
point in space with cartesian internal representation
Definition: Point3D.h:12
Event.h
reco::PFCandidate::particleId
virtual ParticleType particleId() const
Definition: PFCandidate.h:367
trigObjTnPSource_cfi.filler
filler
Definition: trigObjTnPSource_cfi.py:21
edm::Ref::isNonnull
bool isNonnull() const
Checks for non-null.
Definition: Ref.h:238
createfilelist.int
int
Definition: createfilelist.py:10
iEvent
int iEvent
Definition: GenABIO.cc:224
edm::EventSetup
Definition: EventSetup.h:57
reco::PFCandidate::ecalEnergy
double ecalEnergy() const
return corrected Ecal energy
Definition: PFCandidate.h:221
reco::TrackBase::muonSeededStepInOut
Definition: TrackBase.h:103
VertexFwd.h
eostools.move
def move(src, dest)
Definition: eostools.py:511
std
Definition: JetResolutionObject.h:76
edm::OrphanHandle
Definition: EDProductfwd.h:39
HiBadParticleCleaner::tokenPV_
edm::EDGetTokenT< reco::VertexCollection > tokenPV_
Definition: HiBadParticleCleaner.cc:36
PVValHelper::dxy
Definition: PVValidationHelpers.h:47
extraflags_cff.vtx
vtx
Definition: extraflags_cff.py:18
PVValHelper::dz
Definition: PVValidationHelpers.h:50
Frameworkfwd.h
reco::PFCandidate::hcalEnergy
double hcalEnergy() const
return corrected Hcal energy
Definition: PFCandidate.h:233
spclusmultinvestigator_cfi.vertexCollection
vertexCollection
Definition: spclusmultinvestigator_cfi.py:4
edm::ValueMap
Definition: ValueMap.h:107
math::Error::type
ErrorD< N >::type type
Definition: Error.h:32
reco::PFCandidate
Particle reconstructed by the particle flow algorithm.
Definition: PFCandidate.h:41
HiBadParticleCleaner::tokenPFCandidates_
edm::EDGetTokenT< edm::View< reco::PFCandidate > > tokenPFCandidates_
Definition: HiBadParticleCleaner.cc:35
edm::helper::Filler
Definition: ValueMap.h:22
funct::abs
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
ParameterSet.h
EDProducer.h
edm::Event
Definition: Event.h:73
HiBadParticleCleaner::~HiBadParticleCleaner
~HiBadParticleCleaner() override=default
HiBadParticleCleaner::maxSigLoose_
const double maxSigLoose_
Definition: HiBadParticleCleaner.cc:41
HiBadParticleCleaner::minCaloCompatibility_
const double minCaloCompatibility_
Definition: HiBadParticleCleaner.cc:43
reco::Vertex
Definition: Vertex.h:35
PFCandidateFwd.h