CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
PFPileUp.cc
Go to the documentation of this file.
2 
6 
8 
9 // #include "FWCore/MessageLogger/interface/MessageLogger.h"
12 
13 
14 using namespace std;
15 using namespace edm;
16 using namespace reco;
17 
19 
20  tokenPFCandidates_
21  = consumes<PFCollection>(iConfig.getParameter<InputTag>("PFCandidates"));
22 
23  tokenVertices_
24  = consumes<VertexCollection>(iConfig.getParameter<InputTag>("Vertices"));
25 
26  enable_ = iConfig.getParameter<bool>("Enable");
27 
28  verbose_ =
29  iConfig.getUntrackedParameter<bool>("verbose",false);
30 
31 
32  if ( iConfig.exists("checkClosestZVertex") ) {
33  checkClosestZVertex_ = iConfig.getParameter<bool>("checkClosestZVertex");
34  } else {
35  checkClosestZVertex_ = false;
36  }
37 
38  // Configure the algo
39  pileUpAlgo_.setVerbose(verbose_);
40  pileUpAlgo_.setCheckClosestZVertex(checkClosestZVertex_);
41 
42  //produces<reco::PFCandidateCollection>();
43  produces< PFCollection > ();
44  // produces< PFCollectionByValue > ();
45 }
46 
47 
48 
50 
51 
52 
54  const EventSetup& iSetup) {
55 
56 // LogDebug("PFPileUp")<<"START event: "<<iEvent.id().event()
57 // <<" in run "<<iEvent.id().run()<<endl;
58 
59 
60  // get PFCandidates
61 
62  auto_ptr< PFCollection >
63  pOutput( new PFCollection );
64 
65  auto_ptr< PFCollectionByValue >
66  pOutputByValue ( new PFCollectionByValue );
67 
68  if(enable_) {
69 
70 
71  // get vertices
72  Handle<VertexCollection> vertices;
73  iEvent.getByToken( tokenVertices_, vertices);
74 
75  // get PF Candidates
77  PFCollection const * pfCandidatesRef = 0;
78  PFCollection usedIfNoFwdPtrs;
79  bool getFromFwdPtr = iEvent.getByToken( tokenPFCandidates_, pfCandidates);
80  if ( getFromFwdPtr ) {
81  pfCandidatesRef = pfCandidates.product();
82  }
83  // Maintain backwards-compatibility.
84  // If there is no vector of FwdPtr<PFCandidate> found, then
85  // make a dummy vector<FwdPtr<PFCandidate> > for the PFPileupAlgo,
86  // set the pointer "pfCandidatesRef" to point to it, and
87  // then we can pass it to the PFPileupAlgo.
88  else {
89  Handle<PFView> pfView;
90  bool getFromView = iEvent.getByToken( tokenPFCandidates_, pfView );
91  if ( ! getFromView ) {
92  throw cms::Exception("PFPileUp is misconfigured. This needs to be either vector<FwdPtr<PFCandidate> >, or View<PFCandidate>");
93  }
94  for ( edm::View<reco::PFCandidate>::const_iterator viewBegin = pfView->begin(),
95  viewEnd = pfView->end(), iview = viewBegin;
96  iview != viewEnd; ++iview ) {
97  usedIfNoFwdPtrs.push_back( edm::FwdPtr<reco::PFCandidate>( pfView->ptrAt(iview-viewBegin), pfView->ptrAt(iview-viewBegin) ) );
98  }
99  pfCandidatesRef = &usedIfNoFwdPtrs;
100  }
101 
102  if ( pfCandidatesRef == 0 ) {
103  throw cms::Exception("Something went dreadfully wrong with PFPileUp. pfCandidatesRef should never be zero, so this is a logic error.");
104  }
105 
106 
107 
108  pileUpAlgo_.process(*pfCandidatesRef,*vertices);
109  pOutput->insert(pOutput->end(),pileUpAlgo_.getPFCandidatesFromPU().begin(),pileUpAlgo_.getPFCandidatesFromPU().end());
110 
111  // for ( PFCollection::const_iterator byValueBegin = pileUpAlgo_.getPFCandidatesFromPU().begin(),
112  // byValueEnd = pileUpAlgo_.getPFCandidatesFromPU().end(), ibyValue = byValueBegin;
113  // ibyValue != byValueEnd; ++ibyValue ) {
114  // pOutputByValue->push_back( **ibyValue );
115  // }
116 
117  } // end if enabled
118  // outsize of the loop to fill the collection anyway even when disabled
119  iEvent.put( pOutput );
120  // iEvent.put( pOutputByValue );
121 }
122 
T getParameter(std::string const &) const
T getUntrackedParameter(std::string const &, T const &) const
boost::indirect_iterator< typename seq_t::const_iterator > const_iterator
Definition: View.h:81
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:434
bool exists(std::string const &parameterName) const
checks if a parameter exists
std::vector< PFCandidatePtr > pfCandidates(const PFJet &jet, int particleId, bool sort=true)
virtual void produce(edm::Event &, const edm::EventSetup &) override
Definition: PFPileUp.cc:53
int iEvent
Definition: GenABIO.cc:230
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:116
std::vector< reco::PFCandidate > PFCollectionByValue
Definition: PFPileUp.h:39
std::vector< edm::FwdPtr< reco::PFCandidate > > PFCollection
Definition: PFPileUp.h:37
~PFPileUp()
Definition: PFPileUp.cc:49
T const * product() const
Definition: Handle.h:81
PFPileUp(const edm::ParameterSet &)
Definition: PFPileUp.cc:18