test
CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
PFLinker.cc
Go to the documentation of this file.
2 
3 
7 
8 
10  // vector of InputTag; more than 1 is not for RECO, it is for analysis
11 
12  std::vector<edm::InputTag> tags = iConfig.getParameter<std::vector<edm::InputTag> >("PFCandidate");
13  for (unsigned int i=0;i<tags.size();++i)
14  inputTagPFCandidates_.push_back(consumes<reco::PFCandidateCollection>(tags[i]));
15 
16 
17  inputTagGsfElectrons_=consumes<reco::GsfElectronCollection>(
18  iConfig.getParameter<edm::InputTag>("GsfElectrons"));
19 
20  inputTagPhotons_=consumes<reco::PhotonCollection>(iConfig.getParameter<edm::InputTag>("Photons"));
21 
22 
23  muonTag_ = iConfig.getParameter<edm::InputTag>("Muons");
24  inputTagMuons_=consumes<reco::MuonCollection>(edm::InputTag(muonTag_.label()));
25  inputTagMuonMap_=consumes<reco::MuonToMuonMap>(muonTag_);
26 
28  = iConfig.getParameter<std::string>("OutputPF");
29 
31  = iConfig.getParameter<std::string>("ValueMapElectrons");
32 
34  = iConfig.getParameter<std::string>("ValueMapPhotons");
35 
37  = iConfig.getParameter<bool>("ProducePFCandidates");
38 
40  = iConfig.getParameter<std::string>("ValueMapMerged");
41 
43  = iConfig.getParameter<bool>("FillMuonRefs");
44 
45  // should not produce PFCandidates and read seve
46  if(producePFCandidates_ && inputTagPFCandidates_.size()>1) {
47  edm::LogError("PFLinker") << " cannot read several collections of PFCandidates and produce a new collection at the same time. " << std::endl;
48  assert(0);
49  }
50  if(producePFCandidates_) {
51  produces<reco::PFCandidateCollection>(nameOutputPF_);
52  }
53  produces<edm::ValueMap<reco::PFCandidatePtr> > (nameOutputElectronsPF_);
54  produces<edm::ValueMap<reco::PFCandidatePtr> > (nameOutputPhotonsPF_);
55  produces<edm::ValueMap<reco::PFCandidatePtr> > (nameOutputMergedPF_);
56  if(fillMuonRefs_) produces<edm::ValueMap<reco::PFCandidatePtr> > (muonTag_.label());
57 
58 }
59 
61 
63 
64  auto pfCandidates_p = std::make_unique<reco::PFCandidateCollection>();
65 
67  iEvent.getByToken(inputTagGsfElectrons_,gsfElectrons);
68 
69  std::map<reco::GsfElectronRef,reco::PFCandidatePtr> electronCandidateMap;
70 
71 
73  iEvent.getByToken(inputTagPhotons_,photons);
74  std::map<reco::PhotonRef,reco::PFCandidatePtr> photonCandidateMap;
75 
76 
78  if(fillMuonRefs_)
79  iEvent.getByToken(inputTagMuonMap_,muonMap);
80  std::map<reco::MuonRef,reco::PFCandidatePtr> muonCandidateMap;
81 
82  unsigned nColPF=inputTagPFCandidates_.size();
83 
85  for(unsigned icol=0;icol<nColPF;++icol) {
86  iEvent.getByToken(inputTagPFCandidates_[icol],pfCandidates);
87  unsigned ncand=pfCandidates->size();
88 
89  for( unsigned i=0; i<ncand; ++i) {
90  edm::Ptr<reco::PFCandidate> candPtr(pfCandidates,i);
91  reco::PFCandidate cand(candPtr);
92 
93  bool isphoton = cand.particleId() == reco::PFCandidate::gamma && cand.mva_nothing_gamma()>0.;
94  bool iselectron = cand.particleId() == reco::PFCandidate::e;
95  // PFCandidates may have a valid MuonRef though they are not muons.
96  bool hasNonNullMuonRef = cand.muonRef().isNonnull() && fillMuonRefs_;
97 
98  // if not an electron or a photon or a muon just fill the PFCandidate collection
99  if ( !(isphoton || iselectron || hasNonNullMuonRef)){pfCandidates_p->push_back(cand); continue;}
100 
101 
102  if (hasNonNullMuonRef) {
103  reco::MuonRef muRef = (*muonMap)[cand.muonRef()];
104  cand.setMuonRef(muRef);
105  muonCandidateMap[muRef] = candPtr;
106  }
107 
108 
109  // if it is an electron. Find the GsfElectron with the same GsfTrack
110  if (iselectron) {
111  const reco::GsfTrackRef & gsfTrackRef(cand.gsfTrackRef());
112  GsfElectronEqual myEqual(gsfTrackRef);
113  std::vector<reco::GsfElectron>::const_iterator itcheck=find_if(gsfElectrons->begin(),gsfElectrons->end(),myEqual);
114  if(itcheck==gsfElectrons->end()) {
115  std::ostringstream err;
116  err << " Problem in PFLinker: no GsfElectron " << std::endl;
117  edm::LogError("PFLinker") << err.str();
118  continue; // Watch out ! Continue
119  }
120  reco::GsfElectronRef electronRef(gsfElectrons,itcheck-gsfElectrons->begin());
121  cand.setGsfElectronRef(electronRef);
122  cand.setSuperClusterRef(electronRef->superCluster());
123  // update energy information since now it is done post-particleFlowTmp
124  cand.setEcalEnergy(electronRef->superCluster()->rawEnergy(),electronRef->ecalEnergy());
125  cand.setDeltaP(electronRef->p4Error(reco::GsfElectron::P4_COMBINATION));
126  cand.setP4(electronRef->p4(reco::GsfElectron::P4_COMBINATION));
127  electronCandidateMap[electronRef] = candPtr;
128  }
129 
130  // if it is a photon, find the one with the same PF super-cluster
131  if (isphoton) {
132  const reco::SuperClusterRef & scRef(cand.superClusterRef());
133  PhotonEqual myEqual(scRef);
134  std::vector<reco::Photon>::const_iterator itcheck=find_if(photons->begin(),photons->end(),myEqual);
135  if(itcheck==photons->end()) {
136  std::ostringstream err;
137  err << " Problem in PFLinker: no Photon " << std::endl;
138  edm::LogError("PFLinker") << err.str();
139  continue; // Watch out ! Continue
140  }
141  reco::PhotonRef photonRef(photons,itcheck-photons->begin());
142  cand.setPhotonRef(photonRef);
143  cand.setSuperClusterRef(photonRef->superCluster());
144  // update energy information since now it is done post-particleFlowTmp
145  cand.setEcalEnergy(photonRef->superCluster()->rawEnergy(),
146  photonRef->getCorrectedEnergy(reco::Photon::regression2));
147  cand.setDeltaP(photonRef->getCorrectedEnergyError(reco::Photon::regression2));
148  cand.setP4(photonRef->p4(reco::Photon::regression2));
149  photonCandidateMap[photonRef] = candPtr;
150  }
151 
152  pfCandidates_p->push_back(cand);
153  }
154  // save the PFCandidates and get a valid handle
155  }
156  const edm::OrphanHandle<reco::PFCandidateCollection> pfCandidateRefProd = (producePFCandidates_) ? iEvent.put(std::move(pfCandidates_p),nameOutputPF_) :
158 
159 
160  // now make the valuemaps
161 
162  edm::ValueMap<reco::PFCandidatePtr> pfMapGsfElectrons = fillValueMap<reco::GsfElectronCollection>(iEvent,
164  gsfElectrons,
165  electronCandidateMap,
166  pfCandidateRefProd);
167 
168  edm::ValueMap<reco::PFCandidatePtr> pfMapPhotons = fillValueMap<reco::PhotonCollection>(iEvent,
170  photons,
171  photonCandidateMap,
172  pfCandidateRefProd);
173 
174 
176 
177  if(fillMuonRefs_){
179  iEvent.getByToken(inputTagMuons_, muons);
180 
181  pfMapMuons = fillValueMap<reco::MuonCollection>(iEvent,
182  muonTag_.label(),
183  muons,
184  muonCandidateMap,
185  pfCandidateRefProd);
186  }
187 
188  auto pfMapMerged = std::make_unique<edm::ValueMap<reco::PFCandidatePtr>>();
189  edm::ValueMap<reco::PFCandidatePtr>::Filler pfMapMergedFiller(*pfMapMerged);
190 
191  *pfMapMerged += pfMapGsfElectrons;
192  *pfMapMerged += pfMapPhotons;
193  if(fillMuonRefs_) *pfMapMerged += pfMapMuons;
194 
195  iEvent.put(std::move(pfMapMerged),nameOutputMergedPF_);
196 }
197 
198 
199 
200 template<typename TYPE>
203  edm::Handle<TYPE>& inputObjCollection,
204  const std::map<edm::Ref<TYPE>, reco::PFCandidatePtr> & mapToTheCandidate,
205  const edm::OrphanHandle<reco::PFCandidateCollection> & newPFCandColl) const {
206 
207  auto pfMap_p = std::make_unique<edm::ValueMap<reco::PFCandidatePtr>>();
209 
210  typedef typename std::map<edm::Ref<TYPE>, reco::PFCandidatePtr>::const_iterator MapTYPE_it;
211 
212  unsigned nObj=inputObjCollection->size();
213  std::vector<reco::PFCandidatePtr> values(nObj);
214 
215  for(unsigned iobj=0; iobj < nObj; ++iobj) {
216 
217  edm::Ref<TYPE> objRef(inputObjCollection, iobj);
218  MapTYPE_it itcheck = mapToTheCandidate.find(objRef);
219 
220  reco::PFCandidatePtr candPtr;
221 
222  if(itcheck != mapToTheCandidate.end())
223  candPtr = producePFCandidates_ ? reco::PFCandidatePtr(newPFCandColl,itcheck->second.key()) : itcheck->second;
224 
225  values[iobj] = candPtr;
226  }
227 
228  filler.insert(inputObjCollection,values.begin(),values.end());
229  filler.fill();
230  edm::ValueMap<reco::PFCandidatePtr> returnValue = *pfMap_p;
231  event.put(std::move(pfMap_p),label);
232  return returnValue;
233 }
T getParameter(std::string const &) const
int i
Definition: DBlmapReader.cc:9
edm::InputTag muonTag_
Input Muons.
Definition: PFLinker.h:63
void setDeltaP(double dp)
set uncertainty on momentum
Definition: PFCandidate.h:294
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:122
bool isNonnull() const
Checks for non-null.
Definition: Ref.h:252
void setGsfElectronRef(const reco::GsfElectronRef &ref)
set GsfElectronRef
Definition: PFCandidate.cc:574
edm::EDGetTokenT< reco::MuonCollection > inputTagMuons_
Definition: PFLinker.h:64
float mva_nothing_gamma() const
mva for gamma detection
Definition: PFCandidate.h:332
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:457
assert(m_qm.get())
virtual void produce(edm::Event &, const edm::EventSetup &) override
Definition: PFLinker.cc:62
bool producePFCandidates_
Flags - if true: References will be towards new collection ; if false to the original one...
Definition: PFLinker.h:79
edm::EDGetTokenT< reco::PhotonCollection > inputTagPhotons_
Input Photons.
Definition: PFLinker.h:60
~PFLinker()
Definition: PFLinker.cc:60
PFLinker(const edm::ParameterSet &)
Definition: PFLinker.cc:9
edm::ValueMap< reco::PFCandidatePtr > fillValueMap(edm::Event &event, std::string label, edm::Handle< TYPE > &inputObjCollection, const std::map< edm::Ref< TYPE >, reco::PFCandidatePtr > &mapToTheCandidate, const edm::OrphanHandle< reco::PFCandidateCollection > &newPFCandColl) const
Definition: PFLinker.cc:201
edm::EDGetTokenT< reco::MuonToMuonMap > inputTagMuonMap_
Definition: PFLinker.h:65
std::vector< PFCandidatePtr > pfCandidates(const PFJet &jet, int particleId, bool sort=true)
int iEvent
Definition: GenABIO.cc:230
std::string nameOutputElectronsPF_
name of output ValueMap electrons
Definition: PFLinker.h:70
std::string nameOutputPhotonsPF_
name of output ValueMap photons
Definition: PFLinker.h:73
def move
Definition: eostools.py:510
std::string nameOutputPF_
name of output collection of PFCandidate
Definition: PFLinker.h:67
How EventSelector::AcceptEvent() decides whether to accept an event for output otherwise it is excluding the probing of A single or multiple positive and the trigger will pass if any such matching triggers are PASS or EXCEPTION[A criterion thatmatches no triggers at all is detected and causes a throw.] A single negative with an expectation of appropriate bit checking in the decision and the trigger will pass if any such matching triggers are FAIL or EXCEPTION A wildcarded negative criterion that matches more than one trigger in the trigger but the state exists so we define the behavior If all triggers are the negative crieriion will lead to accepting the event(this again matches the behavior of"!*"before the partial wildcard feature was incorporated).The per-event"cost"of each negative criterion with multiple relevant triggers is about the same as!*was in the past
reco::MuonRef muonRef() const
Definition: PFCandidate.cc:455
void setEcalEnergy(float eeRaw, float eeCorr)
set corrected Ecal energy
Definition: PFCandidate.h:217
tuple tags
Definition: o2o.py:248
bool fillMuonRefs_
Set muon refs and produce the value map?
Definition: PFLinker.h:82
void setPhotonRef(const reco::PhotonRef &phRef)
set ref to the corresponding reco::Photon if any
Definition: PFCandidate.cc:609
virtual void setP4(const LorentzVector &p4) final
set 4-momentum
std::string const & label() const
Definition: InputTag.h:36
Particle reconstructed by the particle flow algorithm.
Definition: PFCandidate.h:39
void setMuonRef(const reco::MuonRef &ref)
set muon reference
Definition: PFCandidate.cc:441
tuple muons
Definition: patZpeak.py:38
reco::GsfTrackRef gsfTrackRef() const
Definition: PFCandidate.cc:476
void setSuperClusterRef(const reco::SuperClusterRef &scRef)
Definition: PFCandidate.cc:625
virtual ParticleType particleId() const
Definition: PFCandidate.h:373
edm::EDGetTokenT< reco::GsfElectronCollection > inputTagGsfElectrons_
Input GsfElectrons.
Definition: PFLinker.h:57
std::vector< edm::EDGetTokenT< reco::PFCandidateCollection > > inputTagPFCandidates_
Input PFCandidates.
Definition: PFLinker.h:54
std::string nameOutputMergedPF_
name of output merged ValueMap
Definition: PFLinker.h:76
reco::SuperClusterRef superClusterRef() const
return a reference to the corresponding SuperCluster if any
Definition: PFCandidate.cc:605