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 
8 
12 
13 
15  // vector of InputTag; more than 1 is not for RECO, it is for analysis
17  = iConfig.getParameter<std::vector<edm::InputTag> >("PFCandidate");
19  = iConfig.getParameter<edm::InputTag>("GsfElectrons");
21  = iConfig.getParameter<edm::InputTag>("Photons");
23  = iConfig.getParameter<edm::InputTag>("Muons");
24 
26  = iConfig.getParameter<std::string>("OutputPF");
27 
29  = iConfig.getParameter<std::string>("ValueMapElectrons");
30 
32  = iConfig.getParameter<std::string>("ValueMapPhotons");
33 
35  = iConfig.getParameter<bool>("ProducePFCandidates");
36 
38  = iConfig.getParameter<std::string>("ValueMapMerged");
39 
41  = iConfig.getParameter<bool>("FillMuonRefs");
42 
43  // should not produce PFCandidates and read seve
44  if(producePFCandidates_ && inputTagPFCandidates_.size()>1) {
45  edm::LogError("PFLinker") << " cannot read several collections of PFCandidates and produce a new collection at the same time. " << std::endl;
46  assert(0);
47  }
48  if(producePFCandidates_) {
49  produces<reco::PFCandidateCollection>(nameOutputPF_);
50  }
51  produces<edm::ValueMap<reco::PFCandidatePtr> > (nameOutputElectronsPF_);
52  produces<edm::ValueMap<reco::PFCandidatePtr> > (nameOutputPhotonsPF_);
53  produces<edm::ValueMap<reco::PFCandidatePtr> > (nameOutputMergedPF_);
54  if(fillMuonRefs_) produces<edm::ValueMap<reco::PFCandidatePtr> > (inputTagMuons_.label());
55 
56 }
57 
59 
61 
62  std::auto_ptr<reco::PFCandidateCollection>
63  pfCandidates_p(new reco::PFCandidateCollection);
64 
66  bool status=fetchCollection<reco::GsfElectronCollection>(gsfElectrons,
68  iEvent );
69  std::map<reco::GsfElectronRef,reco::PFCandidatePtr> electronCandidateMap;
70 
71  if(!status) {
72  std::ostringstream err;
73  err << " Problem in PFLinker: no electron collection called " << inputTagGsfElectrons_ << std::endl;
74  edm::LogError("PFLinker") << err.str();
75  }
76 
77 
79  status=fetchCollection<reco::PhotonCollection>(photons,
81  iEvent );
82  if(!status) {
83  std::ostringstream err;
84  err << " Problem in PFLinker: no photon collection called " << inputTagPhotons_ << std::endl;
85  edm::LogError("PFLinker") << err.str();
86  }
87 
88 
89  std::map<reco::PhotonRef,reco::PFCandidatePtr> photonCandidateMap;
90 
91 
93  if(fillMuonRefs_)
94  status=fetchCollection<reco::MuonToMuonMap>(muonMap,
96  iEvent);
97  std::map<reco::MuonRef,reco::PFCandidatePtr> muonCandidateMap;
98 
99  unsigned nColPF=inputTagPFCandidates_.size();
100 
101  if(!status) {
102  std::ostringstream err;
103  err << " Problem in PFLinker: no muon collection called " << inputTagMuons_ << std::endl;
104  edm::LogError("PFLinker") << err.str();
105  }
106 
107 
109  for(unsigned icol=0;icol<nColPF;++icol) {
110 
111  bool status=fetchCollection<reco::PFCandidateCollection>(pfCandidates,
112  inputTagPFCandidates_[icol],
113  iEvent );
114 
115  unsigned ncand=(status)?pfCandidates->size():0;
116 
117  for( unsigned i=0; i<ncand; ++i) {
118  edm::Ptr<reco::PFCandidate> candPtr(pfCandidates,i);
119  reco::PFCandidate cand(candPtr);
120 
121  bool isphoton = cand.particleId() == reco::PFCandidate::gamma && cand.mva_nothing_gamma()>0.;
122  bool iselectron = cand.particleId() == reco::PFCandidate::e;
123  // PFCandidates may have a valid MuonRef though they are not muons.
124  bool hasNonNullMuonRef = cand.muonRef().isNonnull() && fillMuonRefs_;
125 
126  // if not an electron or a photon or a muon just fill the PFCandidate collection
127  if ( !(isphoton || iselectron || hasNonNullMuonRef)){pfCandidates_p->push_back(cand); continue;}
128 
129 
130  if (hasNonNullMuonRef) {
131  reco::MuonRef muRef = (*muonMap)[cand.muonRef()];
132  cand.setMuonRef(muRef);
133  muonCandidateMap[muRef] = candPtr;
134  }
135 
136 
137  // if it is an electron. Find the GsfElectron with the same GsfTrack
138  if (iselectron) {
139  const reco::GsfTrackRef & gsfTrackRef(cand.gsfTrackRef());
140  GsfElectronEqual myEqual(gsfTrackRef);
141  std::vector<reco::GsfElectron>::const_iterator itcheck=find_if(gsfElectrons->begin(),gsfElectrons->end(),myEqual);
142  if(itcheck==gsfElectrons->end()) {
143  std::ostringstream err;
144  err << " Problem in PFLinker: no GsfElectron " << std::endl;
145  edm::LogError("PFLinker") << err.str();
146  continue; // Watch out ! Continue
147  }
148  reco::GsfElectronRef electronRef(gsfElectrons,itcheck-gsfElectrons->begin());
149  cand.setGsfElectronRef(electronRef);
150  cand.setSuperClusterRef(electronRef->superCluster());
151  electronCandidateMap[electronRef] = candPtr;
152  }
153 
154  // if it is a photon, find the one with the same PF super-cluster
155  if (isphoton) {
156  const reco::SuperClusterRef & scRef(cand.superClusterRef());
157  PhotonEqual myEqual(scRef);
158  std::vector<reco::Photon>::const_iterator itcheck=find_if(photons->begin(),photons->end(),myEqual);
159  if(itcheck==photons->end()) {
160  std::ostringstream err;
161  err << " Problem in PFLinker: no Photon " << std::endl;
162  edm::LogError("PFLinker") << err.str();
163  continue; // Watch out ! Continue
164  }
165  reco::PhotonRef photonRef(photons,itcheck-photons->begin());
166  cand.setPhotonRef(photonRef);
167  cand.setSuperClusterRef(photonRef->superCluster());
168  photonCandidateMap[photonRef] = candPtr;
169  }
170 
171  pfCandidates_p->push_back(cand);
172  }
173  // save the PFCandidates and get a valid handle
174  }
175  const edm::OrphanHandle<reco::PFCandidateCollection> pfCandidateRefProd = (producePFCandidates_) ? iEvent.put(pfCandidates_p,nameOutputPF_) :
177 
178 
179  // now make the valuemaps
180 
181  edm::ValueMap<reco::PFCandidatePtr> pfMapGsfElectrons = fillValueMap<reco::GsfElectronCollection>(iEvent,
183  gsfElectrons,
184  electronCandidateMap,
185  pfCandidateRefProd);
186 
187  edm::ValueMap<reco::PFCandidatePtr> pfMapPhotons = fillValueMap<reco::PhotonCollection>(iEvent,
189  photons,
190  photonCandidateMap,
191  pfCandidateRefProd);
192 
193 
195 
196  if(fillMuonRefs_){
198  iEvent.getByLabel(inputTagMuons_.label(), muons);
199 
200  pfMapMuons = fillValueMap<reco::MuonCollection>(iEvent,
201  inputTagMuons_.label(),
202  muons,
203  muonCandidateMap,
204  pfCandidateRefProd);
205  }
206 
207  std::auto_ptr<edm::ValueMap<reco::PFCandidatePtr> >
208  pfMapMerged(new edm::ValueMap<reco::PFCandidatePtr>());
209  edm::ValueMap<reco::PFCandidatePtr>::Filler pfMapMergedFiller(*pfMapMerged);
210 
211  *pfMapMerged += pfMapGsfElectrons;
212  *pfMapMerged += pfMapPhotons;
213  if(fillMuonRefs_) *pfMapMerged += pfMapMuons;
214 
215  iEvent.put(pfMapMerged,nameOutputMergedPF_);
216 }
217 
218 template<typename T>
220  const edm::InputTag& tag,
221  const edm::Event& iEvent) const {
222 
223  bool found = iEvent.getByLabel(tag, c);
224 
225  if(!found )
226  {
227  std::ostringstream err;
228  err<<" cannot get " <<tag<<std::endl;
229  edm::LogError("PFLinker")<<err.str();
230  }
231  return found;
232 }
233 
234 
235 
236 template<typename TYPE>
239  edm::Handle<TYPE>& inputObjCollection,
240  const std::map<edm::Ref<TYPE>, reco::PFCandidatePtr> & mapToTheCandidate,
241  const edm::OrphanHandle<reco::PFCandidateCollection> & newPFCandColl) const {
242 
243  std::auto_ptr<edm::ValueMap<reco::PFCandidatePtr> > pfMap_p(new edm::ValueMap<reco::PFCandidatePtr>());
245 
246  typedef typename std::map<edm::Ref<TYPE>, reco::PFCandidatePtr>::const_iterator MapTYPE_it;
247 
248  unsigned nObj=inputObjCollection->size();
249  std::vector<reco::PFCandidatePtr> values(nObj);
250 
251  for(unsigned iobj=0; iobj < nObj; ++iobj) {
252 
253  edm::Ref<TYPE> objRef(inputObjCollection, iobj);
254  MapTYPE_it itcheck = mapToTheCandidate.find(objRef);
255 
256  reco::PFCandidatePtr candPtr;
257 
258  if(itcheck != mapToTheCandidate.end())
259  candPtr = producePFCandidates_ ? reco::PFCandidatePtr(newPFCandColl,itcheck->second.key()) : itcheck->second;
260 
261  values[iobj] = candPtr;
262  }
263 
264  filler.insert(inputObjCollection,values.begin(),values.end());
265  filler.fill();
266  edm::ValueMap<reco::PFCandidatePtr> returnValue = *pfMap_p;
267  event.put(pfMap_p,label);
268  return returnValue;
269 }
T getParameter(std::string const &) const
int i
Definition: DBlmapReader.cc:9
void setGsfElectronRef(const reco::GsfElectronRef &ref)
set GsfElectronRef
Definition: PFCandidate.cc:565
float mva_nothing_gamma() const
mva for gamma detection
Definition: PFCandidate.h:314
void insert(const H &h, I begin, I end)
Definition: ValueMap.h:52
virtual void produce(edm::Event &, const edm::EventSetup &) override
Definition: PFLinker.cc:60
bool producePFCandidates_
Flags - if true: References will be towards new collection ; if false to the original one...
Definition: PFLinker.h:79
~PFLinker()
Definition: PFLinker.cc:58
PFLinker(const edm::ParameterSet &)
Definition: PFLinker.cc:14
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:237
std::vector< PFCandidatePtr > pfCandidates(const PFJet &jet, int particleId, bool sort=true)
edm::InputTag inputTagMuons_
Input Muons.
Definition: PFLinker.h:64
bool isNonnull() const
Checks for non-null.
Definition: Ref.h:250
int iEvent
Definition: GenABIO.cc:243
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:116
std::string nameOutputElectronsPF_
name of output ValueMap electrons
Definition: PFLinker.h:70
std::string nameOutputPhotonsPF_
name of output ValueMap photons
Definition: PFLinker.h:73
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
bool fetchCollection(edm::Handle< T > &c, const edm::InputTag &tag, const edm::Event &iEvent) const
Definition: PFLinker.cc:219
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:390
reco::MuonRef muonRef() const
Definition: PFCandidate.cc:446
edm::InputTag inputTagPhotons_
Input Photons.
Definition: PFLinker.h:61
std::vector< reco::PFCandidate > PFCandidateCollection
collection of PFCandidates
bool fillMuonRefs_
Set muon refs and produce the value map?
Definition: PFLinker.h:82
edm::InputTag inputTagGsfElectrons_
Input GsfElectrons.
Definition: PFLinker.h:58
void setPhotonRef(const reco::PhotonRef &phRef)
set ref to the corresponding reco::Photon if any
Definition: PFCandidate.cc:600
std::string const & label() const
Definition: InputTag.h:42
Particle reconstructed by the particle flow algorithm.
Definition: PFCandidate.h:38
void setMuonRef(const reco::MuonRef &ref)
set muon reference
Definition: PFCandidate.cc:432
tuple muons
Definition: patZpeak.py:38
reco::GsfTrackRef gsfTrackRef() const
Definition: PFCandidate.cc:467
void setSuperClusterRef(const reco::SuperClusterRef &scRef)
Definition: PFCandidate.cc:616
std::vector< edm::InputTag > inputTagPFCandidates_
Input PFCandidates.
Definition: PFLinker.h:55
tuple status
Definition: ntuplemaker.py:245
virtual ParticleType particleId() const
Definition: PFCandidate.h:355
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:596