CMS 3D CMS Logo

PuppiProducer.cc
Go to the documentation of this file.
1 // system include files
2 #include <memory>
3 
4 // user include files
19 //Main File
22 
23 
24 // ------------------------------------------------------------------------------------------
26  fPuppiDiagnostics = iConfig.getParameter<bool>("puppiDiagnostics");
27  fPuppiForLeptons = iConfig.getParameter<bool>("puppiForLeptons");
28  fUseFromPVLooseTight = iConfig.getParameter<bool>("UseFromPVLooseTight");
29  fUseDZ = iConfig.getParameter<bool>("UseDeltaZCut");
30  fDZCut = iConfig.getParameter<double>("DeltaZCut");
31  fEtaMinUseDZ = iConfig.getParameter<double>("EtaMinUseDeltaZ");
32  fPtMaxCharged = iConfig.getParameter<double>("PtMaxCharged");
33  fEtaMaxCharged = iConfig.getParameter<double>("EtaMaxCharged");
34  fNumOfPUVtxsForCharged = iConfig.getParameter<uint>("NumOfPUVtxsForCharged");
35  fDZCutForChargedFromPUVtxs = iConfig.getParameter<double>("DeltaZCutForChargedFromPUVtxs");
36  fUseExistingWeights = iConfig.getParameter<bool>("useExistingWeights");
37  fUseWeightsNoLep = iConfig.getParameter<bool>("useWeightsNoLep");
38  fClonePackedCands = iConfig.getParameter<bool>("clonePackedCands");
39  fVtxNdofCut = iConfig.getParameter<int>("vtxNdofCut");
40  fVtxZCut = iConfig.getParameter<double>("vtxZCut");
41  fPuppiContainer = std::unique_ptr<PuppiContainer> ( new PuppiContainer(iConfig) );
42 
44  = consumes<CandidateView>(iConfig.getParameter<edm::InputTag>("candName"));
46  = consumes<VertexCollection>(iConfig.getParameter<edm::InputTag>("vertexName"));
47 
48 
49  produces<edm::ValueMap<float> > ();
50  produces<edm::ValueMap<LorentzVector> > ();
51  produces< edm::ValueMap<reco::CandidatePtr> >();
52 
53  if (fUseExistingWeights || fClonePackedCands)
54  produces<pat::PackedCandidateCollection>();
55  else
56  produces<reco::PFCandidateCollection>();
57 
58  if (fPuppiDiagnostics){
59  produces<double> ("PuppiNAlgos");
60  produces<std::vector<double>> ("PuppiRawAlphas");
61  produces<std::vector<double>> ("PuppiAlphas");
62  produces<std::vector<double>> ("PuppiAlphasMed");
63  produces<std::vector<double>> ("PuppiAlphasRms");
64  }
65 }
66 // ------------------------------------------------------------------------------------------
68 }
69 // ------------------------------------------------------------------------------------------
71 
72  // Get PFCandidate Collection
73  edm::Handle<CandidateView> hPFProduct;
74  iEvent.getByToken(tokenPFCandidates_,hPFProduct);
75  const CandidateView *pfCol = hPFProduct.product();
76 
77  // Get vertex collection w/PV as the first entry?
79  iEvent.getByToken(tokenVertices_,hVertexProduct);
80  const reco::VertexCollection *pvCol = hVertexProduct.product();
81 
82  int npv = 0;
83  const reco::VertexCollection::const_iterator vtxEnd = pvCol->end();
84  for (reco::VertexCollection::const_iterator vtxIter = pvCol->begin(); vtxEnd != vtxIter; ++vtxIter) {
85  if (!vtxIter->isFake() && vtxIter->ndof()>=fVtxNdofCut && std::abs(vtxIter->z())<=fVtxZCut)
86  npv++;
87  }
88 
89  //Fill the reco objects
90  fRecoObjCollection.clear();
91  fRecoObjCollection.reserve(pfCol->size());
92  for(auto const& aPF : *pfCol) {
93  RecoObj pReco;
94  pReco.pt = aPF.pt();
95  pReco.eta = aPF.eta();
96  pReco.phi = aPF.phi();
97  pReco.m = aPF.mass();
98  pReco.rapidity = aPF.rapidity();
99  pReco.charge = aPF.charge();
100  const reco::Vertex *closestVtx = nullptr;
101  double pDZ = -9999;
102  double pD0 = -9999;
103  uint pVtxId = 0;
104  const pat::PackedCandidate *lPack = dynamic_cast<const pat::PackedCandidate*>(&aPF);
105  if(lPack == nullptr ) {
106 
107  const reco::PFCandidate *pPF = dynamic_cast<const reco::PFCandidate*>(&aPF);
108  double curdz = 9999;
109  int closestVtxForUnassociateds = -9999;
110  const reco::TrackRef aTrackRef = pPF->trackRef();
111  bool lFirst = true;
112  for(auto const& aV : *pvCol) {
113  if(lFirst) {
114  if ( aTrackRef.isNonnull()) {
115  pDZ = aTrackRef->dz(aV.position());
116  pD0 = aTrackRef->d0();
117  } else if (pPF->gsfTrackRef().isNonnull()) {
118  pDZ = pPF->gsfTrackRef()->dz(aV.position());
119  pD0 = pPF->gsfTrackRef()->d0();
120  }
121  lFirst = false;
122  if(pDZ > -9999) pVtxId = 0;
123  }
124  if(aTrackRef.isNonnull() && aV.trackWeight(pPF->trackRef())>0) {
125  closestVtx = &aV;
126  break;
127  }
128  // in case it's unassocciated, keep more info
129  double tmpdz = 99999;
130  if ( aTrackRef.isNonnull() ) tmpdz = aTrackRef ->dz(aV.position());
131  else if ( pPF->gsfTrackRef().isNonnull() ) tmpdz = pPF->gsfTrackRef()->dz(aV.position());
132  if (std::abs(tmpdz) < curdz){
133  curdz = std::abs(tmpdz);
134  closestVtxForUnassociateds = pVtxId;
135  }
136  pVtxId++;
137 
138  }
139  int tmpFromPV = 0;
140  // mocking the miniAOD definitions
141  if (std::abs(pReco.charge) > 0){
142  if (closestVtx != nullptr && pVtxId > 0) tmpFromPV = 0;
143  if (closestVtx != nullptr && pVtxId == 0) tmpFromPV = 3;
144  if (closestVtx == nullptr && closestVtxForUnassociateds == 0) tmpFromPV = 2;
145  if (closestVtx == nullptr && closestVtxForUnassociateds != 0) tmpFromPV = 1;
146  }
147  pReco.dZ = pDZ;
148  pReco.d0 = pD0;
149  pReco.id = 0;
150  if (std::abs(pReco.charge) == 0){ pReco.id = 0; }
151  else{
152  if (tmpFromPV == 0) {
153  pReco.id = 2;
154  if (fNumOfPUVtxsForCharged > 0 and (pVtxId <= fNumOfPUVtxsForCharged) and
156  pReco.id = 1;
157  } else if (tmpFromPV == 3){ pReco.id = 1; }
158  else if (tmpFromPV == 1 || tmpFromPV == 2){
159  pReco.id = 0;
160  if ((fPtMaxCharged > 0) and (pReco.pt > fPtMaxCharged))
161  pReco.id = 1;
162  else if (std::abs(pReco.eta) > fEtaMaxCharged)
163  pReco.id = 1;
164  else if ((fUseDZ) && (std::abs(pReco.eta) >= fEtaMinUseDZ))
165  pReco.id = (std::abs(pDZ) < fDZCut) ? 1 : 2;
166  else if (fUseFromPVLooseTight && tmpFromPV == 1)
167  pReco.id = 2;
168  else if (fUseFromPVLooseTight && tmpFromPV == 2)
169  pReco.id = 1;
170  }
171  }
172  }
173  else if(lPack->vertexRef().isNonnull() ) {
174  pDZ = lPack->dz();
175  pD0 = lPack->dxy();
176  pReco.dZ = pDZ;
177  pReco.d0 = pD0;
178 
179  pReco.id = 0;
180  if (std::abs(pReco.charge) == 0){ pReco.id = 0; }
181  if (std::abs(pReco.charge) > 0){
182  if (lPack->fromPV() == 0){
183  pReco.id = 2;
185  for (size_t puVtx_idx = 1; puVtx_idx <= fNumOfPUVtxsForCharged && puVtx_idx < pvCol->size();
186  ++puVtx_idx) {
187  if (lPack->fromPV(puVtx_idx) >= 2) {
188  pReco.id = 1;
189  break;
190  }
191  }
192  }
193  } else if (lPack->fromPV() == (pat::PackedCandidate::PVUsedInFit)){ pReco.id = 1; }
194  else if (lPack->fromPV() == (pat::PackedCandidate::PVTight) || lPack->fromPV() == (pat::PackedCandidate::PVLoose)){
195  pReco.id = 0;
196  if ((fPtMaxCharged > 0) and (pReco.pt > fPtMaxCharged))
197  pReco.id = 1;
198  else if (std::abs(pReco.eta) > fEtaMaxCharged)
199  pReco.id = 1;
200  else if ((fUseDZ) && (std::abs(pReco.eta) >= fEtaMinUseDZ))
201  pReco.id = (std::abs(pDZ) < fDZCut) ? 1 : 2;
203  pReco.id = 2;
205  pReco.id = 1;
206  }
207  }
208  }
209 
210  fRecoObjCollection.push_back(pReco);
211 
212  }
213 
214  fPuppiContainer->initialize(fRecoObjCollection);
215  fPuppiContainer->setNPV( npv );
216 
217  std::vector<double> lWeights;
218  std::vector<PuppiCandidate> lCandidates;
219  if (!fUseExistingWeights){
220  //Compute the weights and get the particles
221  lWeights = fPuppiContainer->puppiWeights();
222  lCandidates = fPuppiContainer->puppiParticles();
223  }
224  else{
225  //Use the existing weights
226  int lPackCtr = 0;
227  for(auto const& aPF : *pfCol) {
228  const pat::PackedCandidate *lPack = dynamic_cast<const pat::PackedCandidate*>(&aPF);
229  float curpupweight = -1.;
230  if(lPack == nullptr ) {
231  // throw error
232  throw edm::Exception(edm::errors::LogicError,"PuppiProducer: cannot get weights since inputs are not PackedCandidates");
233  }
234  else{
235  if (fUseWeightsNoLep){ curpupweight = lPack->puppiWeightNoLep(); }
236  else{ curpupweight = lPack->puppiWeight(); }
237  }
238  lWeights.push_back(curpupweight);
239  PuppiCandidate curjet;
240  curjet.px = curpupweight*lPack->px();
241  curjet.py = curpupweight*lPack->py();
242  curjet.pz = curpupweight*lPack->pz();
243  curjet.e = curpupweight*lPack->energy();
244  curjet.pt = curpupweight*lPack->pt();
245  curjet.eta = lPack->eta();
246  curjet.rapidity = lPack->rapidity();
247  curjet.phi = lPack->phi();
248  curjet.m = curpupweight*lPack->mass();
249  lCandidates.push_back(curjet);
250  lPackCtr++;
251  }
252  }
253 
254  //Fill it into the event
255  std::unique_ptr<edm::ValueMap<float> > lPupOut(new edm::ValueMap<float>());
256  edm::ValueMap<float>::Filler lPupFiller(*lPupOut);
257  lPupFiller.insert(hPFProduct,lWeights.begin(),lWeights.end());
258  lPupFiller.fill();
259 
260  // This is a dummy to access the "translate" method which is a
261  // non-static member function even though it doesn't need to be.
262  // Will fix in the future.
263  static const reco::PFCandidate dummySinceTranslateIsNotStatic;
264 
265  // Fill a new PF/Packed Candidate Collection and write out the ValueMap of the new p4s.
266  // Since the size of the ValueMap must be equal to the input collection, we need
267  // to search the "puppi" particles to find a match for each input. If none is found,
268  // the input is set to have a four-vector of 0,0,0,0
271  std::unique_ptr<edm::ValueMap<LorentzVector> > p4PupOut(new edm::ValueMap<LorentzVector>());
272  LorentzVectorCollection puppiP4s;
273  std::vector<reco::CandidatePtr> values(hPFProduct->size());
274 
275  int val = -1;
276  for ( auto const& aCand : *hPFProduct) {
277  val++;
278  std::unique_ptr<pat::PackedCandidate> pCand;
279  std::unique_ptr<reco::PFCandidate> pfCand;
281  const pat::PackedCandidate *cand = dynamic_cast<const pat::PackedCandidate*>(&aCand);
282  if(!cand)
283  throw edm::Exception(edm::errors::LogicError,"PuppiProducer: inputs are not PackedCandidates");
284  pCand.reset( new pat::PackedCandidate(*cand) );
285  } else {
286  auto id = dummySinceTranslateIsNotStatic.translatePdgIdToType(aCand.pdgId());
287  const reco::PFCandidate *cand = dynamic_cast<const reco::PFCandidate*>(&aCand);
288  pfCand.reset( new reco::PFCandidate( cand ? *cand : reco::PFCandidate(aCand.charge(), aCand.p4(), id) ) );
289  }
290  LorentzVector pVec;
291 
292  //get an index to a pup in lCandidates: either fUseExistingWeights with no skips or get from fPuppiContainer
293  int iPuppiMatched = fUseExistingWeights ? val : fPuppiContainer->recoToPup()[val];
294  if ( iPuppiMatched >= 0 ) {
295  auto const& puppiMatched = lCandidates[iPuppiMatched];
296  pVec.SetPxPyPzE(puppiMatched.px,puppiMatched.py,puppiMatched.pz,puppiMatched.e);
298  if(fPuppiForLeptons)
299  pCand->setPuppiWeight(pCand->puppiWeight(),lWeights[val]);
300  else
301  pCand->setPuppiWeight(lWeights[val],pCand->puppiWeightNoLep());
302  }
303  } else {
304  pVec.SetPxPyPzE( 0, 0, 0, 0);
306  pCand->setPuppiWeight(0,0);
307  }
308  }
309  puppiP4s.push_back( pVec );
310 
312  pCand->setP4(pVec);
313  pCand->setSourceCandidatePtr( aCand.sourceCandidatePtr(0) );
314  fPackedPuppiCandidates->push_back(*pCand);
315  } else {
316  pfCand->setP4(pVec);
317  pfCand->setSourceCandidatePtr( aCand.sourceCandidatePtr(0) );
318  fPuppiCandidates->push_back(*pfCand);
319  }
320  }
321 
322  //Compute the modified p4s
323  edm::ValueMap<LorentzVector>::Filler p4PupFiller(*p4PupOut);
324  p4PupFiller.insert(hPFProduct,puppiP4s.begin(), puppiP4s.end() );
325  p4PupFiller.fill();
326 
327  iEvent.put(std::move(lPupOut));
328  iEvent.put(std::move(p4PupOut));
331  for(unsigned int ic=0, nc = oh->size(); ic < nc; ++ic) {
332  reco::CandidatePtr pkref( oh, ic );
333  values[ic] = pkref;
334  }
335  } else {
337  for(unsigned int ic=0, nc = oh->size(); ic < nc; ++ic) {
338  reco::CandidatePtr pkref( oh, ic );
339  values[ic] = pkref;
340  }
341  }
342  std::unique_ptr<edm::ValueMap<reco::CandidatePtr> > pfMap_p(new edm::ValueMap<reco::CandidatePtr>());
344  filler.insert(hPFProduct, values.begin(), values.end());
345  filler.fill();
346  iEvent.put(std::move(pfMap_p));
347 
348 
351 
352  // all the different alphas per particle
353  // THE alpha per particle
354  std::unique_ptr<std::vector<double> > theAlphas(new std::vector<double>(fPuppiContainer->puppiAlphas()));
355  std::unique_ptr<std::vector<double> > theAlphasMed(new std::vector<double>(fPuppiContainer->puppiAlphasMed()));
356  std::unique_ptr<std::vector<double> > theAlphasRms(new std::vector<double>(fPuppiContainer->puppiAlphasRMS()));
357  std::unique_ptr<std::vector<double> > alphas(new std::vector<double>(fPuppiContainer->puppiRawAlphas()));
358  std::unique_ptr<double> nalgos(new double(fPuppiContainer->puppiNAlgos()));
359 
360  iEvent.put(std::move(alphas),"PuppiRawAlphas");
361  iEvent.put(std::move(nalgos),"PuppiNAlgos");
362  iEvent.put(std::move(theAlphas),"PuppiAlphas");
363  iEvent.put(std::move(theAlphasMed),"PuppiAlphasMed");
364  iEvent.put(std::move(theAlphasRms),"PuppiAlphasRms");
365  }
366 
367 }
368 
369 // ------------------------------------------------------------------------------------------
371 }
372 // ------------------------------------------------------------------------------------------
374 }
375 // ------------------------------------------------------------------------------------------
378  desc.add<bool>("puppiDiagnostics", false);
379  desc.add<bool>("puppiForLeptons", false);
380  desc.add<bool>("UseFromPVLooseTight", false);
381  desc.add<bool>("UseDeltaZCut", true);
382  desc.add<double>("DeltaZCut", 0.3);
383  desc.add<double>("EtaMinUseDeltaZ", 0.);
384  desc.add<double>("PtMaxCharged", 0.);
385  desc.add<double>("EtaMaxCharged", 99999.);
386  desc.add<double>("PtMaxNeutrals", 200.);
387  desc.add<double>("PtMaxNeutralsStartSlope", 0.);
388  desc.add<uint>("NumOfPUVtxsForCharged", 0);
389  desc.add<double>("DeltaZCutForChargedFromPUVtxs", 0.2);
390  desc.add<bool>("useExistingWeights", false);
391  desc.add<bool>("useWeightsNoLep", false);
392  desc.add<bool>("clonePackedCands", false);
393  desc.add<int>("vtxNdofCut", 4);
394  desc.add<double>("vtxZCut", 24);
395  desc.add<edm::InputTag>("candName", edm::InputTag("particleFlow"));
396  desc.add<edm::InputTag>("vertexName", edm::InputTag("offlinePrimaryVertices"));
397  desc.add<bool>("applyCHS", true);
398  desc.add<bool>("invertPuppi", false);
399  desc.add<bool>("useExp", false);
400  desc.add<double>("MinPuppiWeight", .01);
401 
403 
404  descriptions.add("PuppiProducer", desc);
405 }
406 //define this as a plug-in
float puppiWeight() const
virtual float dz(size_t ipv=0) const
dz with respect to the PV[ipv]
T getParameter(std::string const &) const
bool fUseFromPVLooseTight
Definition: PuppiProducer.h:51
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:125
bool isNonnull() const
Checks for non-null.
Definition: Ref.h:251
double px() const override
x coordinate of momentum vector
float puppiWeightNoLep() const
Weight from full PUPPI.
float d0
Definition: RecoObj.h:28
double fDZCutForChargedFromPUVtxs
Definition: PuppiProducer.h:58
virtual void endJob()
bool fUseWeightsNoLep
Definition: PuppiProducer.h:60
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:517
std::vector< reco::PFCandidate > PFOutputCollection
Definition: PuppiProducer.h:35
static void fillDescriptionsPuppiAlgo(edm::ParameterSetDescription &desc)
Definition: PuppiAlgo.cc:216
Definition: RecoObj.h:4
bool fPuppiDiagnostics
Definition: PuppiProducer.h:49
void insert(const H &h, I begin, I end)
Definition: ValueMap.h:53
std::vector< pat::PackedCandidate > PackedOutputCollection
Definition: PuppiProducer.h:36
std::vector< RecoObj > fRecoObjCollection
Definition: PuppiProducer.h:65
size_type size() const
std::vector< Vertex > VertexCollection
collection of Vertex objects
Definition: VertexFwd.h:9
std::vector< LorentzVector > LorentzVectorCollection
Definition: PuppiProducer.h:31
virtual void beginJob()
math::XYZTLorentzVector LorentzVector
Definition: PuppiProducer.h:30
const reco::VertexRef vertexRef() const
double fEtaMaxCharged
Definition: PuppiProducer.h:56
std::unique_ptr< PuppiContainer > fPuppiContainer
Definition: PuppiProducer.h:64
int charge
Definition: RecoObj.h:29
double pz() const override
z coordinate of momentum vector
edm::EDGetTokenT< VertexCollection > tokenVertices_
Definition: PuppiProducer.h:45
float dZ
Definition: RecoObj.h:27
double py() const override
y coordinate of momentum vector
reco::TrackRef trackRef() const
Definition: PFCandidate.cc:442
float rapidity
Definition: RecoObj.h:17
float pt
Definition: RecoObj.h:17
int iEvent
Definition: GenABIO.cc:224
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
int id
Definition: RecoObj.h:18
double rapidity() const override
rapidity
float phi
Definition: RecoObj.h:17
const PVAssoc fromPV(size_t ipv=0) const
std::unique_ptr< PackedOutputCollection > fPackedPuppiCandidates
Definition: PuppiProducer.h:67
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
bool fClonePackedCands
Definition: PuppiProducer.h:61
double energy() const override
energy
double pt() const override
transverse momentum
void produce(edm::Event &, const edm::EventSetup &) override
bool fPuppiForLeptons
Definition: PuppiProducer.h:50
ParameterDescriptionBase * add(U const &iLabel, T const &value)
PuppiProducer(const edm::ParameterSet &)
double eta() const override
momentum pseudorapidity
double phi() const override
momentum azimuthal angle
float m
Definition: RecoObj.h:17
T const * product() const
Definition: Handle.h:74
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
double fPtMaxCharged
Definition: PuppiProducer.h:55
def uint(string)
void add(std::string const &label, ParameterSetDescription const &psetDescription)
ParticleType translatePdgIdToType(int pdgid) const
Definition: PFCandidate.cc:233
float eta
Definition: RecoObj.h:17
uint fNumOfPUVtxsForCharged
Definition: PuppiProducer.h:57
bool fUseExistingWeights
Definition: PuppiProducer.h:59
double mass() const override
mass
Particle reconstructed by the particle flow algorithm.
Definition: PFCandidate.h:40
reco::GsfTrackRef gsfTrackRef() const
Definition: PFCandidate.cc:480
edm::EDGetTokenT< CandidateView > tokenPFCandidates_
Definition: PuppiProducer.h:44
virtual float dxy() const
dxy with respect to the PV ref
~PuppiProducer() override
std::unique_ptr< PFOutputCollection > fPuppiCandidates
Definition: PuppiProducer.h:66
def move(src, dest)
Definition: eostools.py:511
double fEtaMinUseDZ
Definition: PuppiProducer.h:54