CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
PFCandidateMixer.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: PFCandidateMixer
4 // Class: PFCandidateMixer
5 //
13 //
14 // Original Author: Tomasz Maciej Frueboes
15 // Created: Wed Dec 9 16:14:56 CET 2009
16 // $Id: PFCandidateMixer.cc,v 1.6 2012/05/17 23:35:04 aburgmei Exp $
17 //
18 //
19 
20 
21 // system include files
22 #include <memory>
23 
24 // user include files
27 
30 
36 
39 
42 
45 
47 
48 //
49 // class decleration
50 //
51 
53  public:
54  explicit PFCandidateMixer(const edm::ParameterSet&);
56 
57  private:
58  virtual void beginJob() ;
59  virtual void produce(edm::Event&, const edm::EventSetup&);
60  virtual void endJob() ;
61 
63 
66 
70  // ----------member data ---------------------------
71 };
72 
73 //
74 // constants, enums and typedefs
75 //
76 
77 
78 //
79 // static data member definitions
80 //
81 
82 //
83 // constructors and destructor
84 //
86  _col1(iConfig.getUntrackedParameter<edm::InputTag>("col1") ),
87  _col2(iConfig.getUntrackedParameter<edm::InputTag>("col2") ),
88  _trackCol(iConfig.getUntrackedParameter<edm::InputTag>("trackCol") ),
89  _muonCol(iConfig.getUntrackedParameter<edm::InputTag>("muons") ),
90  _electronCol(iConfig.getUntrackedParameter<edm::InputTag>("gsfElectrons"))
91 {
92  produces< std::vector< reco::PFCandidate > >();
93 
94  if(!_electronCol.label().empty())
96 #if 0
97  if(!_muonCol.label().empty())
99 #endif
100 }
101 
103 {
104 
105  // do anything here that needs to be done at desctruction time
106  // (e.g. close files, deallocate resources etc.)
107 
108 }
109 
110 
111 //
112 // member functions
113 //
114 
115 // ------------ method called to produce the data ------------
116 void
118 {
119  const bool muonColGiven = !_muonCol.label().empty();
120  const bool electronColGiven = !_electronCol.label().empty();
121 
125  iEvent.getByLabel( _trackCol, trackCol);
126 
127  if(muonColGiven)
128  if(!iEvent.getByLabel(_muonCol, muonCol))
129  throw cms::Exception("PFCandidateMixer") << "Muon Collection not found!";
130  if(electronColGiven)
131  if(!iEvent.getByLabel(_electronCol, electronCol))
132  throw cms::Exception("PFCandidateMixer") << "GsfElectron Collection not found!";
133 
136  iEvent.getByLabel(_col1, pfIn1);
137  iEvent.getByLabel(_col2, pfIn2);
138 
139  mix(iEvent, trackCol, muonCol, electronCol, *pfIn1, *pfIn2);
140 }
141 
143 {
144  using namespace edm;
145  using namespace reco;
146 
147  std::vector<const reco::PFCandidateCollection*> colVec;
148 
149  colVec.push_back(&pfIn1);
150  colVec.push_back(&pfIn2);
151 
152  std::auto_ptr<std::vector< reco::PFCandidate > > pOut(new std::vector< reco::PFCandidate > );
153 
154  std::vector<const reco::PFCandidateCollection*>::iterator itCol= colVec.begin();
155  std::vector<const reco::PFCandidateCollection*>::iterator itColE= colVec.end();
156 
157  std::map<reco::GsfElectronRef, reco::PFCandidatePtr> electronCandidateMap;
158 
159  int iCol = 0;
160  for (;itCol!=itColE; ++itCol){
161  PFCandidateConstIterator it = (*itCol)->begin();
162  PFCandidateConstIterator itE = (*itCol)->end();
163  for (;it!=itE;++it) {
164  edm::Ptr<reco::PFCandidate> candPtr(*itCol, it - (*itCol)->begin());
165  reco::PFCandidate cand(*it);
166  //reco::PFCandidate cand(candPtr); // This breaks the output module, I'm not sure why
167 
168  //const bool isphoton = cand.particleId() == reco::PFCandidate::gamma && cand.mva_nothing_gamma()>0.;
169  const bool iselectron = cand.particleId() == reco::PFCandidate::e;
170  //const bool hasNonNullMuonRef = cand.muonRef().isNonnull();
171 
172  // if it is an electron. Find the GsfElectron with the same GsfTrack
173  if (electronCol.isValid() && iselectron) {
174  const reco::GsfTrackRef& gsfTrackRef(cand.gsfTrackRef());
175  GsfElectronEqual myEqual(gsfTrackRef);
176  std::vector<reco::GsfElectron>::const_iterator itcheck=find_if(electronCol->begin(), electronCol->end(), myEqual);
177  if(itcheck==electronCol->end())
178  throw cms::Exception("PFCandidateMixer") << "GsfElectron for candidate not found!";
179 
180  reco::GsfElectronRef electronRef(electronCol, itcheck - electronCol->begin());
181  cand.setGsfElectronRef(electronRef);
182  cand.setSuperClusterRef(electronRef->superCluster());
183  electronCandidateMap[electronRef] = candPtr;
184  }
185 
186  // TODO: Do the same for muons -- see PFLinker.cc, we just don't have the MuonToMuonMap available.
187  // But it might work with the pure muons collections as well.
188 
189  size_t i = 0;
190  bool found = false;
191  double minDR = 9999.;
192  int iMinDr = -1;
193  if (it->trackRef().isNonnull()) {
194  for ( i = 0 ; i < trackCol->size(); ++i){
195  if ( reco::deltaR( *(it->trackRef()), (*trackCol)[i] )<0.001 ) {
196  found = true;
197  break;
198  }
199  double dr = reco::deltaR( *(it->trackRef()), (*trackCol)[i] );
200  if ( dr < minDR) {
201  iMinDr = i;
202  minDR = dr;
203  }
204  }
205  }
206  if ( found ){ // ref was found, overwrite in PFCand
207  reco::TrackRef trref(trackCol,i);
208  cand.setTrackRef(trref);
209  //std::cout << " YY track ok"<<std::endl;
210 
211  } else { // keep orginall ref
212  if (it->trackRef().isNonnull()) {
213  std::cout << " XXXXXXXXXXX track not found "
214  << " col " << iCol
215  << " ch " << it->charge()
216  << " id " << it->pdgId()
217  << " pt " << it->pt()
218  << " track: eta " << it->trackRef()->eta()
219  << " pt: " << it->trackRef()->pt()
220  << " charge: " << it->trackRef()->charge()
221  << std::endl;
222  std::cout << " minDR=" << minDR << std::endl;
223  if ( iMinDr >= 0 ) {
224  std::cout
225  << " closest track pt=" << (*trackCol)[iMinDr].pt()
226  << " ch=" << (*trackCol)[iMinDr].charge()
227  << std::endl;
228  }
229  edm::Provenance prov=iEvent.getProvenance(it->trackRef().id());
231  std::cout << " trackref in PFCand came from: " << tag.encode() << std::endl;
232  }
233  }
234  pOut->push_back(cand);
235  }
236  ++iCol;
237  }
238 
240 
241  // Now fixup the references and write the valuemap
242  if(electronCol.isValid())
243  {
244  std::vector<reco::PFCandidatePtr> values(electronCol->size());
245  for(unsigned int i = 0; i < electronCol->size(); ++i)
246  {
247  edm::Ref<reco::GsfElectronCollection> objRef(electronCol, i);
248  std::map<reco::GsfElectronRef, reco::PFCandidatePtr>::const_iterator iter = electronCandidateMap.find(objRef);
249 
250  reco::PFCandidatePtr candPtr;
251  if(iter != electronCandidateMap.end())
252  candPtr = reco::PFCandidatePtr(newColl, iter->second.key());
253  values[i] = candPtr;
254  }
255 
256  std::auto_ptr<edm::ValueMap<reco::PFCandidatePtr> > pfMap_p(new edm::ValueMap<reco::PFCandidatePtr>());
258  filler.insert(electronCol, values.begin(), values.end());
259  filler.fill();
260  iEvent.put(pfMap_p, "electrons");
261  }
262 
263  // TODO: Do the same for muons
264 }
265 
266 // ------------ method called once each job just before starting event loop ------------
267 void
269 {
270 }
271 
272 // ------------ method called once each job just after ending the event loop ------------
273 void
275 }
276 
277 //define this as a plug-in
int i
Definition: DBlmapReader.cc:9
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
virtual void beginJob()
edm::InputTag _electronCol
void insert(const H &h, I begin, I end)
Definition: ValueMap.h:52
double deltaR(const T1 &t1, const T2 &t2)
Definition: deltaR.h:48
virtual void produce(edm::Event &, const edm::EventSetup &)
PFCandidateCollection::const_iterator PFCandidateConstIterator
iterator
std::string const & processName() const
Definition: Provenance.h:61
edm::InputTag _col2
edm::InputTag _muonCol
int iEvent
Definition: GenABIO.cc:230
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:113
TypeLabelItem const & produces()
declare what type of product will make and with which optional label
edm::InputTag _col1
virtual void endJob()
bool isValid() const
Definition: HandleBase.h:75
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:405
std::vector< reco::PFCandidate > PFCandidateCollection
collection of PFCandidates
std::string const & moduleLabel() const
Definition: Provenance.h:60
std::string const & label() const
Definition: InputTag.h:42
Particle reconstructed by the particle flow algorithm.
Definition: PFCandidate.h:39
edm::InputTag _trackCol
PFCandidateMixer(const edm::ParameterSet &)
tuple cout
Definition: gather_cfg.py:121
std::string const & productInstanceName() const
Definition: Provenance.h:62
Provenance getProvenance(BranchID const &theID) const
Definition: Event.cc:73
void mix(edm::Event &iEvent, const edm::Handle< reco::TrackCollection > &trackCol, const edm::Handle< reco::MuonCollection > &muonCol, const edm::Handle< reco::GsfElectronCollection > &electronCol, const reco::PFCandidateCollection &pfIn1, const reco::PFCandidateCollection &pfIn2)