CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
PFPileUp.cc
Go to the documentation of this file.
1 // system include files
2 #include <memory>
3 #include <string>
4 
5 // user include files
9 
12 
16 
18 
22 
24 
27 
28 using namespace std;
29 using namespace edm;
30 using namespace reco;
31 
43 public:
44  typedef std::vector<edm::FwdPtr<reco::PFCandidate>> PFCollection;
46  typedef std::vector<reco::PFCandidate> PFCollectionByValue;
48 
49  explicit PFPileUp(const edm::ParameterSet&);
50 
51  ~PFPileUp() override;
52 
53  void produce(edm::Event&, const edm::EventSetup&) override;
54 
55  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
56 
57 private:
59 
64 
67 
69  bool enable_;
70 
72  bool verbose_;
73 
76 
83 };
84 
86  tokenPFCandidates_ = consumes<PFCollection>(iConfig.getParameter<InputTag>("PFCandidates"));
87  tokenPFCandidatesView_ = mayConsume<PFView>(iConfig.getParameter<InputTag>("PFCandidates"));
88 
89  tokenVertices_ = consumes<VertexCollection>(iConfig.getParameter<InputTag>("Vertices"));
90 
91  fUseVertexAssociation = iConfig.getParameter<bool>("useVertexAssociation");
92  vertexAssociationQuality_ = iConfig.getParameter<int>("vertexAssociationQuality");
93  if (fUseVertexAssociation) {
94  tokenVertexAssociation_ = consumes<CandToVertex>(iConfig.getParameter<edm::InputTag>("vertexAssociation"));
95  tokenVertexAssociationQuality_ =
96  consumes<edm::ValueMap<int>>(iConfig.getParameter<edm::InputTag>("vertexAssociation"));
97  }
98  fNumOfPUVtxsForCharged_ = iConfig.getParameter<unsigned int>("NumOfPUVtxsForCharged");
99  fDzCutForChargedFromPUVtxs_ = iConfig.getParameter<double>("DzCutForChargedFromPUVtxs");
100 
101  enable_ = iConfig.getParameter<bool>("enable");
102 
103  verbose_ = iConfig.getUntrackedParameter<bool>("verbose", false);
104 
105  checkClosestZVertex_ = iConfig.getParameter<bool>("checkClosestZVertex");
106 
107  // Configure the algo
108  pileUpAlgo_.setVerbose(verbose_);
109  pileUpAlgo_.setCheckClosestZVertex(checkClosestZVertex_);
110  pileUpAlgo_.setNumOfPUVtxsForCharged(fNumOfPUVtxsForCharged_);
111  pileUpAlgo_.setDzCutForChargedFromPUVtxs(fDzCutForChargedFromPUVtxs_);
112 
113  //produces<reco::PFCandidateCollection>();
114  produces<PFCollection>();
115  // produces< PFCollectionByValue > ();
116 }
117 
119 
120 void PFPileUp::produce(Event& iEvent, const EventSetup& iSetup) {
121  // LogDebug("PFPileUp")<<"START event: "<<iEvent.id().event()
122  // <<" in run "<<iEvent.id().run()<<endl;
123 
124  // get PFCandidates
125 
126  unique_ptr<PFCollection> pOutput(new PFCollection);
127 
128  unique_ptr<PFCollectionByValue> pOutputByValue(new PFCollectionByValue);
129 
130  if (enable_) {
131  // get vertices
133  iEvent.getByToken(tokenVertices_, vertices);
134 
135  // get PF Candidates
137  PFCollection const* pfCandidatesRef = nullptr;
138  PFCollection usedIfNoFwdPtrs;
139  bool getFromFwdPtr = iEvent.getByToken(tokenPFCandidates_, pfCandidates);
140  if (getFromFwdPtr) {
141  pfCandidatesRef = pfCandidates.product();
142  }
143  // Maintain backwards-compatibility.
144  // If there is no vector of FwdPtr<PFCandidate> found, then
145  // make a dummy vector<FwdPtr<PFCandidate> > for the PFPileupAlgo,
146  // set the pointer "pfCandidatesRef" to point to it, and
147  // then we can pass it to the PFPileupAlgo.
148  else {
149  Handle<PFView> pfView;
150  bool getFromView = iEvent.getByToken(tokenPFCandidatesView_, pfView);
151  if (!getFromView) {
152  throw cms::Exception(
153  "PFPileUp is misconfigured. This needs to be either vector<FwdPtr<PFCandidate> >, or View<PFCandidate>");
154  }
155  for (edm::View<reco::PFCandidate>::const_iterator viewBegin = pfView->begin(),
156  viewEnd = pfView->end(),
157  iview = viewBegin;
158  iview != viewEnd;
159  ++iview) {
160  usedIfNoFwdPtrs.push_back(
161  edm::FwdPtr<reco::PFCandidate>(pfView->ptrAt(iview - viewBegin), pfView->ptrAt(iview - viewBegin)));
162  }
163  pfCandidatesRef = &usedIfNoFwdPtrs;
164  }
165 
166  if (pfCandidatesRef == nullptr) {
167  throw cms::Exception(
168  "Something went dreadfully wrong with PFPileUp. pfCandidatesRef should never be zero, so this is a logic "
169  "error.");
170  }
171 
172  if (fUseVertexAssociation) {
173  const edm::Association<reco::VertexCollection>& associatedPV = iEvent.get(tokenVertexAssociation_);
174  const edm::ValueMap<int>& associationQuality = iEvent.get(tokenVertexAssociationQuality_);
175  PFCollection pfCandidatesFromPU;
176  for (auto& p : (*pfCandidatesRef)) {
177  const reco::VertexRef& PVOrig = associatedPV[p];
178  int quality = associationQuality[p];
179  if (PVOrig.isNonnull() && (PVOrig.key() > 0) && (quality >= vertexAssociationQuality_))
180  pfCandidatesFromPU.push_back(p);
181  }
182  pOutput->insert(pOutput->end(), pfCandidatesFromPU.begin(), pfCandidatesFromPU.end());
183  } else {
184  pileUpAlgo_.process(*pfCandidatesRef, *vertices);
185  pOutput->insert(
186  pOutput->end(), pileUpAlgo_.getPFCandidatesFromPU().begin(), pileUpAlgo_.getPFCandidatesFromPU().end());
187  }
188  // for ( PFCollection::const_iterator byValueBegin = pileUpAlgo_.getPFCandidatesFromPU().begin(),
189  // byValueEnd = pileUpAlgo_.getPFCandidatesFromPU().end(), ibyValue = byValueBegin;
190  // ibyValue != byValueEnd; ++ibyValue ) {
191  // pOutputByValue->push_back( **ibyValue );
192  // }
193 
194  } // end if enabled
195  // outsize of the loop to fill the collection anyway even when disabled
196  iEvent.put(std::move(pOutput));
197  // iEvent.put(std::move(pOutputByValue));
198 }
199 
202  desc.add<edm::InputTag>("PFCandidates", edm::InputTag("particleFlowTmpPtrs"));
203  desc.add<edm::InputTag>("Vertices", edm::InputTag("offlinePrimaryVertices"));
204  desc.add<bool>("enable", true);
205  desc.addUntracked<bool>("verbose", false);
206  desc.add<bool>("checkClosestZVertex", true);
207  desc.add<bool>("useVertexAssociation", false);
208  desc.add<int>("vertexAssociationQuality", 0);
209  desc.add<edm::InputTag>("vertexAssociation", edm::InputTag(""));
210  desc.add<unsigned int>("NumOfPUVtxsForCharged", 0);
211  desc.add<double>("DzCutForChargedFromPUVtxs", .2);
212  descriptions.add("pfPileUp", desc);
213 }
214 
T getUntrackedParameter(std::string const &, T const &) const
int vertexAssociationQuality_
Definition: PFPileUp.cc:80
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:133
bool isNonnull() const
Checks for non-null.
Definition: Ref.h:238
Identifies pile-up candidates from a collection of PFCandidates, and produces the corresponding colle...
Definition: PFPileUp.cc:42
edm::Association< reco::VertexCollection > CandToVertex
Definition: PFPileUp.cc:47
ParameterDescriptionBase * addUntracked(U const &iLabel, T const &value)
edm::EDGetTokenT< reco::VertexCollection > tokenVertices_
vertices
Definition: PFPileUp.cc:66
edm::EDGetTokenT< PFView > tokenPFCandidatesView_
fall-back token
Definition: PFPileUp.cc:63
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:539
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
string quality
key_type key() const
Accessor for product key.
Definition: Ref.h:250
unsigned int fNumOfPUVtxsForCharged_
Definition: PFPileUp.cc:81
void produce(edm::Event &, const edm::EventSetup &) override
Definition: PFPileUp.cc:120
int iEvent
Definition: GenABIO.cc:224
bool verbose_
verbose ?
Definition: PFPileUp.cc:72
std::vector< reco::PFCandidate > PFCollectionByValue
Definition: PFPileUp.cc:46
def move
Definition: eostools.py:511
bool get(ProductID const &oid, Handle< PROD > &result) const
Definition: Event.h:346
edm::EDGetTokenT< PFCollection > tokenPFCandidates_
PFCandidates to be analyzed.
Definition: PFPileUp.cc:61
std::vector< edm::FwdPtr< reco::PFCandidate > > PFCollection
Definition: PFPileUp.cc:44
edm::EDGetTokenT< CandToVertex > tokenVertexAssociation_
Definition: PFPileUp.cc:77
bool enable_
enable PFPileUp selection
Definition: PFPileUp.cc:69
ParameterDescriptionBase * add(U const &iLabel, T const &value)
double fDzCutForChargedFromPUVtxs_
Definition: PFPileUp.cc:82
bool fUseVertexAssociation
Definition: PFPileUp.cc:79
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
Definition: PFPileUp.cc:200
edm::EDGetTokenT< edm::ValueMap< int > > tokenVertexAssociationQuality_
Definition: PFPileUp.cc:78
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
T const * product() const
Definition: Handle.h:70
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
void add(std::string const &label, ParameterSetDescription const &psetDescription)
boost::indirect_iterator< typename seq_t::const_iterator > const_iterator
Definition: View.h:86
~PFPileUp() override
Definition: PFPileUp.cc:118
PFPileUpAlgo pileUpAlgo_
Definition: PFPileUp.cc:58
bool checkClosestZVertex_
use the closest z vertex if a track is not in a vertex
Definition: PFPileUp.cc:75
PFPileUp(const edm::ParameterSet &)
Definition: PFPileUp.cc:85
edm::View< reco::PFCandidate > PFView
Definition: PFPileUp.cc:45