CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
PuppiProducer.cc
Go to the documentation of this file.
1 // system include files
2 #include <memory>
3 
4 // user include files
24 //Main File
25 #include "fastjet/PseudoJet.hh"
27 
28 
29 
30 // ------------------------------------------------------------------------------------------
32  fUseDZ = iConfig.getParameter<bool>("UseDeltaZCut");
33  fDZCut = iConfig.getParameter<double>("DeltaZCut");
34  fPuppiContainer = std::unique_ptr<PuppiContainer> ( new PuppiContainer(iConfig) );
35 
37  = consumes<CandidateView>(iConfig.getParameter<edm::InputTag>("candName"));
39  = consumes<VertexCollection>(iConfig.getParameter<edm::InputTag>("vertexName"));
40 
41 
42  produces<edm::ValueMap<float> > ();
43  produces<edm::ValueMap<LorentzVector> > ();
44  produces< edm::ValueMap<reco::CandidatePtr> >();
45 
46  produces<PFOutputCollection>();
47 
48 
49 }
50 // ------------------------------------------------------------------------------------------
52 }
53 // ------------------------------------------------------------------------------------------
55 
56  // Get PFCandidate Collection
57  edm::Handle<CandidateView> hPFProduct;
58  iEvent.getByToken(tokenPFCandidates_,hPFProduct);
59  const CandidateView *pfCol = hPFProduct.product();
60 
61  // Get vertex collection w/PV as the first entry?
63  iEvent.getByToken(tokenVertices_,hVertexProduct);
64  const reco::VertexCollection *pvCol = hVertexProduct.product();
65 
66  //Fill the reco objects
67  fRecoObjCollection.clear();
68  for(CandidateView::const_iterator itPF = pfCol->begin(); itPF!=pfCol->end(); itPF++) {
69  RecoObj pReco;
70  pReco.pt = itPF->pt();
71  pReco.eta = itPF->eta();
72  pReco.phi = itPF->phi();
73  pReco.m = itPF->mass();
74  pReco.charge = itPF->charge();
75  const reco::Vertex *closestVtx = 0;
76  double pDZ = -9999;
77  double pD0 = -9999;
78  int pVtxId = -9999;
79  bool lFirst = true;
80  const pat::PackedCandidate *lPack = dynamic_cast<const pat::PackedCandidate*>(&(*itPF));
81  if(lPack == 0 ) {
82  const reco::PFCandidate *pPF = dynamic_cast<const reco::PFCandidate*>(&(*itPF));
83  for(reco::VertexCollection::const_iterator iV = pvCol->begin(); iV!=pvCol->end(); ++iV) {
84  if(lFirst) {
85  if ( pPF->trackRef().isNonnull() ) pDZ = pPF->trackRef() ->dz(iV->position());
86  else if ( pPF->gsfTrackRef().isNonnull() ) pDZ = pPF->gsfTrackRef()->dz(iV->position());
87  if ( pPF->trackRef().isNonnull() ) pD0 = pPF->trackRef() ->d0();
88  else if ( pPF->gsfTrackRef().isNonnull() ) pD0 = pPF->gsfTrackRef()->d0();
89  lFirst = false;
90  if(pDZ > -9999) pVtxId = 0;
91  }
92  if(iV->trackWeight(pPF->trackRef())>0) {
93  closestVtx = &(*iV);
94  break;
95  }
96  pVtxId++;
97  }
98  } else if(lPack->vertexRef().isNonnull() ) {
99  pDZ = lPack->dz();
100  pD0 = lPack->dxy();
101  closestVtx = &(*(lPack->vertexRef()));
102  pVtxId = (lPack->fromPV() != (pat::PackedCandidate::PVUsedInFit));
103  if( (lPack->fromPV() == pat::PackedCandidate::PVLoose) ||
104  (lPack->fromPV() == pat::PackedCandidate::PVTight) )
105  closestVtx = 0;
106  }
107  pReco.dZ = pDZ;
108  pReco.d0 = pD0;
109 
110  if(closestVtx == 0) pReco.vtxId = -1;
111  if(closestVtx != 0) pReco.vtxId = pVtxId;
112  //if(closestVtx != 0) pReco.vtxChi2 = closestVtx->trackWeight(itPF->trackRef());
113  //Set the id for Puppi Algo: 0 is neutral pfCandidate, id = 1 for particles coming from PV and id = 2 for charged particles from non-leading vertex
114  pReco.id = 0;
115 
116  if(closestVtx != 0 && pVtxId == 0 && fabs(pReco.charge) > 0) pReco.id = 1;
117  if(closestVtx != 0 && pVtxId > 0 && fabs(pReco.charge) > 0) pReco.id = 2;
118  //Add a dZ cut if wanted (this helps)
119  if(fUseDZ && pDZ > -9999 && closestVtx == 0 && (fabs(pDZ) < fDZCut) && fabs(pReco.charge) > 0) pReco.id = 1;
120  if(fUseDZ && pDZ > -9999 && closestVtx == 0 && (fabs(pDZ) > fDZCut) && fabs(pReco.charge) > 0) pReco.id = 2;
121 
122  //std::cout << "pVtxId = " << pVtxId << ", and charge = " << itPF->charge() << ", and closestVtx = " << closestVtx << ", and id = " << pReco.id << std::endl;
123 
124  fRecoObjCollection.push_back(pReco);
125  }
126  fPuppiContainer->initialize(fRecoObjCollection);
127 
128  //Compute the weights
129  const std::vector<double> lWeights = fPuppiContainer->puppiWeights();
130  //Fill it into the event
131  std::auto_ptr<edm::ValueMap<float> > lPupOut(new edm::ValueMap<float>());
132  edm::ValueMap<float>::Filler lPupFiller(*lPupOut);
133  lPupFiller.insert(hPFProduct,lWeights.begin(),lWeights.end());
134  lPupFiller.fill();
135 
136 
137  // This is a dummy to access the "translate" method which is a
138  // non-static member function even though it doesn't need to be.
139  // Will fix in the future.
140  static const reco::PFCandidate dummySinceTranslateIsNotStatic;
141 
142  //Fill a new PF Candidate Collection and write out the ValueMap of the new p4s.
143  // Since the size of the ValueMap must be equal to the input collection, we need
144  // to search the "puppi" particles to find a match for each input. If none is found,
145  // the input is set to have a four-vector of 0,0,0,0
146  const std::vector<fastjet::PseudoJet> lCandidates = fPuppiContainer->puppiParticles();
148  std::auto_ptr<edm::ValueMap<LorentzVector> > p4PupOut(new edm::ValueMap<LorentzVector>());
149  LorentzVectorCollection puppiP4s;
150  std::vector<reco::CandidatePtr> values(hPFProduct->size());
151  //std::vector<int> values(hPFProduct->size());
152 
153  for ( auto i0 = hPFProduct->begin(),
154  i0begin = hPFProduct->begin(),
155  i0end = hPFProduct->end(); i0 != i0end; ++i0 ) {
156  //for(unsigned int i0 = 0; i0 < lCandidates.size(); i0++) {
157  //reco::PFCandidate pCand;
158  auto id = dummySinceTranslateIsNotStatic.translatePdgIdToType(i0->pdgId());
159  const reco::PFCandidate *pPF = dynamic_cast<const reco::PFCandidate*>(&(*i0));
160  reco::PFCandidate pCand( pPF ? *pPF : reco::PFCandidate(i0->charge(), i0->p4(), id) );
161  LorentzVector pVec = i0->p4();
162  int val = i0 - i0begin;
163 
164  // Find the Puppi particle matched to the input collection using the "user_index" of the object.
165  auto puppiMatched = find_if( lCandidates.begin(), lCandidates.end(), [&val]( fastjet::PseudoJet const & i ){ return i.user_index() == val; } );
166  if ( puppiMatched != lCandidates.end() ) {
167  pVec.SetPxPyPzE(puppiMatched->px(),puppiMatched->py(),puppiMatched->pz(),puppiMatched->E());
168  } else {
169  pVec.SetPxPyPzE( 0, 0, 0, 0);
170  }
171  pCand.setP4(pVec);
172  puppiP4s.push_back( pVec );
173  fPuppiCandidates->push_back(pCand);
174  }
175 
176  //Compute the modified p4s
177  edm::ValueMap<LorentzVector>::Filler p4PupFiller(*p4PupOut);
178  p4PupFiller.insert(hPFProduct,puppiP4s.begin(), puppiP4s.end() );
179  p4PupFiller.fill();
180 
181  iEvent.put(lPupOut);
182  iEvent.put(p4PupOut);
184  for(unsigned int ic=0, nc = oh->size(); ic < nc; ++ic) {
185  reco::CandidatePtr pkref( oh, ic );
186  values[ic] = pkref;
187 
188  }
189  std::auto_ptr<edm::ValueMap<reco::CandidatePtr> > pfMap_p(new edm::ValueMap<reco::CandidatePtr>());
191  filler.insert(hPFProduct, values.begin(), values.end());
192  filler.fill();
193  iEvent.put(pfMap_p);
194 
195 }
196 
197 // ------------------------------------------------------------------------------------------
199 }
200 // ------------------------------------------------------------------------------------------
202 }
203 // ------------------------------------------------------------------------------------------
205 }
206 // ------------------------------------------------------------------------------------------
208 }
209 // ------------------------------------------------------------------------------------------
211 }
212 // ------------------------------------------------------------------------------------------
214 }
215 // ------------------------------------------------------------------------------------------
217  //The following says we do not know what parameters are allowed so do no validation
218  // Please change this to state exactly what you do use, even if it is no parameters
220  desc.setUnknown();
221  descriptions.addDefault(desc);
222 }
223 //define this as a plug-in
virtual float dz(size_t ipv=0) const
dz with respect to the PV[ipv]
T getParameter(std::string const &) const
int i
Definition: DBlmapReader.cc:9
virtual void produce(edm::Event &, const edm::EventSetup &)
bool isNonnull() const
Checks for non-null.
Definition: Ref.h:250
float d0
Definition: RecoObj.h:28
virtual void endJob()
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:449
std::vector< reco::PFCandidate > PFOutputCollection
Definition: PuppiProducer.h:31
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
Definition: RecoObj.h:4
void insert(const H &h, I begin, I end)
Definition: ValueMap.h:52
std::vector< RecoObj > fRecoObjCollection
Definition: PuppiProducer.h:52
virtual void setP4(const LorentzVector &p4)
set 4-momentum
std::vector< Vertex > VertexCollection
collection of Vertex objects
Definition: VertexFwd.h:9
std::vector< LorentzVector > LorentzVectorCollection
Definition: PuppiProducer.h:27
virtual void beginJob()
math::XYZTLorentzVector LorentzVector
Definition: PuppiProducer.h:26
const reco::VertexRef vertexRef() const
std::unique_ptr< PuppiContainer > fPuppiContainer
Definition: PuppiProducer.h:51
int charge
Definition: RecoObj.h:29
edm::EDGetTokenT< VertexCollection > tokenVertices_
Definition: PuppiProducer.h:45
float dZ
Definition: RecoObj.h:27
reco::TrackRef trackRef() const
Definition: PFCandidate.cc:433
float pt
Definition: RecoObj.h:17
int iEvent
Definition: GenABIO.cc:230
void addDefault(ParameterSetDescription const &psetDescription)
const_iterator begin() const
int id
Definition: RecoObj.h:18
float phi
Definition: RecoObj.h:17
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:113
const PVAssoc fromPV(size_t ipv=0) const
PuppiProducer(const edm::ParameterSet &)
virtual void beginRun(edm::Run &, edm::EventSetup const &)
virtual void endLuminosityBlock(edm::LuminosityBlock &, edm::EventSetup const &)
float m
Definition: RecoObj.h:17
T const * product() const
Definition: Handle.h:81
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
ParticleType translatePdgIdToType(int pdgid) const
Definition: PFCandidate.cc:224
float eta
Definition: RecoObj.h:17
Particle reconstructed by the particle flow algorithm.
Definition: PFCandidate.h:39
boost::indirect_iterator< typename seq_t::const_iterator > const_iterator
Definition: View.h:81
reco::GsfTrackRef gsfTrackRef() const
Definition: PFCandidate.cc:471
edm::EDGetTokenT< CandidateView > tokenPFCandidates_
Definition: PuppiProducer.h:44
std::auto_ptr< PFOutputCollection > fPuppiCandidates
Definition: PuppiProducer.h:53
const_iterator end() const
virtual void endRun(edm::Run &, edm::EventSetup const &)
virtual void beginLuminosityBlock(edm::LuminosityBlock &, edm::EventSetup const &)
virtual float dxy() const
dxy with respect to the PV ref
int vtxId
Definition: RecoObj.h:20
Definition: Run.h:41