CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
PrimaryVertexSorter.h
Go to the documentation of this file.
1 #ifndef CommonTools_RecoAlgos_PrimaryVertexSorter_
2 #define CommonTools_RecoAlgos_PrimaryVertexSorter_
3 
4 // system include files
5 #include <memory>
6 #include <string>
7 
8 // user include files
12 
15 
18 
20 
23 
30 template <class ParticlesCollection>
31 
33  public:
34 
38 
39  typedef ParticlesCollection PFCollection;
40 
41  explicit PrimaryVertexSorter(const edm::ParameterSet&);
42 
44 
45  virtual void produce(edm::Event&, const edm::EventSetup&) override;
46 
47  private:
48 
51 
54 
58 
64  bool useMET_;
65 };
66 
67 
68 
73 
74 // #include "FWCore/MessageLogger/interface/MessageLogger.h"
77 
78 
79 
80 template <class ParticlesCollection>
82  assignmentAlgo_(iConfig.getParameterSet("assignment")),
83  sortingAlgo_(iConfig.getParameterSet("sorting")),
84  tokenCandidates_(consumes<ParticlesCollection>(iConfig.getParameter<edm::InputTag>("particles"))),
85  tokenVertices_(consumes<reco::VertexCollection>(iConfig.getParameter<edm::InputTag>("vertices"))),
86  tokenJets_(consumes<edm::View<reco::Candidate> > (iConfig.getParameter<edm::InputTag>("jets"))),
87  produceOriginalMapping_(iConfig.getParameter<bool>("produceAssociationToOriginalVertices")),
88  produceSortedVertices_(iConfig.getParameter<bool>("produceSortedVertices")),
89  producePFPileUp_(iConfig.getParameter<bool>("producePileUpCollection")),
90  producePFNoPileUp_(iConfig.getParameter<bool>("produceNoPileUpCollection")),
91  qualityCut_(iConfig.getParameter<int>("qualityForPrimary")),
92  useMET_(iConfig.getParameter<bool>("usePVMET"))
93 {
94 
95 using namespace std;
96 using namespace edm;
97 using namespace reco;
98 
100  produces< CandToVertex> ("original");
101  produces< CandToVertexQuality> ("original");
102  produces< VertexScore> ("original");
103  }
105  produces< reco::VertexCollection> ();
106  produces< CandToVertex> ();
107  produces< CandToVertexQuality> ();
108  produces< VertexScore> ();
109  }
110 
111  if(producePFPileUp_){
113  produces< PFCollection> ("originalPileUp");
115  produces< PFCollection> ("PileUp");
116  }
117 
118  if(producePFNoPileUp_){
120  produces< PFCollection> ("originalNoPileUp");
122  produces< PFCollection> ("NoPileUp");
123  }
124 
125 
126 }
127 
128 
129 
130 
131 
132 template <class ParticlesCollection>
134 
135 using namespace std;
136 using namespace edm;
137 using namespace reco;
138 
140  iEvent.getByToken( tokenJets_, jets);
141 
143  iSetup.get<TransientTrackRecord>().get("TransientTrackBuilder", builder);
144 
145 
148 
149  Handle<ParticlesCollection> particlesHandle;
150  iEvent.getByToken( tokenCandidates_, particlesHandle);
151 
152  ParticlesCollection particles = *particlesHandle.product();
153  std::vector<int> pfToPVVector;
154  std::vector<PrimaryVertexAssignment::Quality> pfToPVQualityVector;
155  //reverse mapping
156  std::vector< std::vector<int> > pvToPFVector(vertices->size());
157  std::vector< std::vector<const reco::Candidate *> > pvToCandVector(vertices->size());
158  std::vector< std::vector<PrimaryVertexAssignment::Quality> > pvToPFQualityVector(vertices->size());
159  std::vector<float> vertexScoreOriginal(vertices->size());
160  std::vector<float> vertexScore(vertices->size());
161 
162  for(auto const & pf : particles) {
163  std::pair<int,PrimaryVertexAssignment::Quality> vtxWithQuality=assignmentAlgo_.chargedHadronVertex(*vertices,pf,*jets,*builder);
164  pfToPVVector.push_back(vtxWithQuality.first);
165  pfToPVQualityVector.push_back(vtxWithQuality.second);
166  }
167 
168  //Invert the mapping
169  for(size_t i = 0; i < pfToPVVector.size();i++)
170  {
171  auto pv = pfToPVVector[i];
172  auto qual = pfToPVQualityVector[i];
173  if(pv >=0 and qual >= qualityCut_){
174  pvToPFVector[pv].push_back(i);
175 // std::cout << i << std::endl;
176 // const typename ParticlesCollection::value_type & cp = particles[i];
177 // std::cout << "CP " << &cp << std::endl;
178  pvToCandVector[pv].push_back( &particles[i] );
179  pvToPFQualityVector[pv].push_back(qual);
180  }
181  }
182 
183  //Use multimap for sorting of indices
184  std::multimap<float,int> scores;
185  for(unsigned int i=0;i<vertices->size();i++){
186  float s=sortingAlgo_.score((*vertices)[i],pvToCandVector[i],useMET_);
187  vertexScoreOriginal[i]=s;
188  scores.insert(std::pair<float,int>(-s,i));
189  }
190 
191  //create indices
192  std::vector<int> oldToNew(vertices->size()), newToOld(vertices->size());
193  size_t newIdx=0;
194  for(auto const & idx : scores)
195  {
196 // std::cout << newIdx << " score: " << idx.first << " oldidx: " << idx.second << " "<< producePFPileUp_ << std::endl;
197  vertexScore[newIdx]=-idx.first;
198  oldToNew[idx.second]=newIdx;
199  newToOld[newIdx]=idx.second;
200  newIdx++;
201  }
202 
203 
204 
205 
207  auto_ptr< CandToVertex> pfCandToOriginalVertexOutput( new CandToVertex(vertices) );
208  auto_ptr< CandToVertexQuality> pfCandToOriginalVertexQualityOutput( new CandToVertexQuality() );
209  CandToVertex::Filler cand2VertexFiller(*pfCandToOriginalVertexOutput);
210  CandToVertexQuality::Filler cand2VertexQualityFiller(*pfCandToOriginalVertexQualityOutput);
211 
212  cand2VertexFiller.insert(particlesHandle,pfToPVVector.begin(),pfToPVVector.end());
213  cand2VertexQualityFiller.insert(particlesHandle,pfToPVQualityVector.begin(),pfToPVQualityVector.end());
214 
215  cand2VertexFiller.fill();
216  cand2VertexQualityFiller.fill();
217  iEvent.put( pfCandToOriginalVertexOutput ,"original");
218  iEvent.put( pfCandToOriginalVertexQualityOutput ,"original");
219 
220  auto_ptr< VertexScore> vertexScoreOriginalOutput( new VertexScore );
221  VertexScore::Filler vertexScoreOriginalFiller(*vertexScoreOriginalOutput);
222  vertexScoreOriginalFiller.insert(vertices,vertexScoreOriginal.begin(),vertexScoreOriginal.end());
223  vertexScoreOriginalFiller.fill();
224  iEvent.put( vertexScoreOriginalOutput ,"original");
225 
226  }
227 
229  std::vector<int> pfToSortedPVVector;
230 // std::vector<int> pfToSortedPVQualityVector;
231  for(size_t i=0;i<pfToPVVector.size();i++) {
232  pfToSortedPVVector.push_back(oldToNew[pfToPVVector[i]]);
233 // pfToSortedPVQualityVector.push_back(pfToPVQualityVector[i]); //same as old!
234  }
235 
236  auto_ptr< reco::VertexCollection> sortedVerticesOutput( new reco::VertexCollection );
237  for(size_t i=0;i<vertices->size();i++){
238  sortedVerticesOutput->push_back((*vertices)[newToOld[i]]);
239  }
240  edm::OrphanHandle<reco::VertexCollection> oh = iEvent.put( sortedVerticesOutput);
241  auto_ptr< CandToVertex> pfCandToVertexOutput( new CandToVertex(oh) );
242  auto_ptr< CandToVertexQuality> pfCandToVertexQualityOutput( new CandToVertexQuality() );
243  CandToVertex::Filler cand2VertexFiller(*pfCandToVertexOutput);
244  CandToVertexQuality::Filler cand2VertexQualityFiller(*pfCandToVertexQualityOutput);
245 
246  cand2VertexFiller.insert(particlesHandle,pfToSortedPVVector.begin(),pfToSortedPVVector.end());
247  cand2VertexQualityFiller.insert(particlesHandle,pfToPVQualityVector.begin(),pfToPVQualityVector.end());
248 
249  cand2VertexFiller.fill();
250  cand2VertexQualityFiller.fill();
251  iEvent.put( pfCandToVertexOutput );
252  iEvent.put( pfCandToVertexQualityOutput );
253 
254  auto_ptr< VertexScore> vertexScoreOutput( new VertexScore );
255  VertexScore::Filler vertexScoreFiller(*vertexScoreOutput);
256  vertexScoreFiller.insert(oh,vertexScore.begin(),vertexScore.end());
257  vertexScoreFiller.fill();
258  iEvent.put( vertexScoreOutput);
259 
260 
261  }
262 
263 
264  auto_ptr< PFCollection > pfCollectionNOPUOriginalOutput( new PFCollection );
265  auto_ptr< PFCollection > pfCollectionNOPUOutput( new PFCollection );
266  auto_ptr< PFCollection > pfCollectionPUOriginalOutput( new PFCollection );
267  auto_ptr< PFCollection > pfCollectionPUOutput( new PFCollection );
268 
269  for(size_t i=0;i<particles.size();i++) {
270  auto pv = pfToPVVector[i];
271  auto qual = pfToPVQualityVector[i];
272 
273 
275  if(pv == newToOld[0] and qual >= qualityCut_)
276  pfCollectionNOPUOutput->push_back(particles[i]);
277 
279  if(pv != newToOld[0] and qual >= qualityCut_)
280  pfCollectionPUOutput->push_back(particles[i]);
281 
283  if(pv == 0 and qual >= qualityCut_)
284  pfCollectionNOPUOriginalOutput->push_back(particles[i]);
285 
287  if(pv != 0 and qual >= qualityCut_)
288  pfCollectionPUOriginalOutput->push_back(particles[i]);
289 
290  }
291  if(producePFNoPileUp_ && produceSortedVertices_) iEvent.put(pfCollectionNOPUOutput,"NoPileUp" );
292  if(producePFPileUp_ && produceSortedVertices_) iEvent.put(pfCollectionPUOutput, "PileUp");
293  if(producePFNoPileUp_ && produceOriginalMapping_) iEvent.put(pfCollectionNOPUOriginalOutput,"originalNoPileUp" );
294  if(producePFPileUp_ && produceOriginalMapping_) iEvent.put(pfCollectionPUOriginalOutput,"originalPileUp" );
295 
296 
297 }
298 
299 
300 #endif
int i
Definition: DBlmapReader.cc:9
edm::Association< reco::VertexCollection > CandToVertex
def oldToNew
Definition: lumidbDDL.py:399
PrimaryVertexSorting sortingAlgo_
virtual void produce(edm::Event &, const edm::EventSetup &) override
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:464
ParameterSet const & getParameterSet(ParameterSetID const &id)
void insert(const H &h, I begin, I end)
Definition: ValueMap.h:52
std::vector< Vertex > VertexCollection
collection of Vertex objects
Definition: VertexFwd.h:9
def newToOld
Definition: lumidbDDL.py:413
edm::EDGetTokenT< PFCollection > tokenCandidates_
Candidates to be analyzed.
int iEvent
Definition: GenABIO.cc:230
ParticlesCollection PFCollection
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:120
vector< PseudoJet > jets
edm::ValueMap< float > VertexScore
tuple idx
DEBUGGING if hasattr(process,&quot;trackMonIterativeTracking2012&quot;): print &quot;trackMonIterativeTracking2012 D...
PrimaryVertexAssignment assignmentAlgo_
const T & get() const
Definition: EventSetup.h:56
edm::ValueMap< int > CandToVertexQuality
edm::EDGetTokenT< reco::VertexCollection > tokenVertices_
vertices
PrimaryVertexSorter(const edm::ParameterSet &)
std::pair< int, PrimaryVertexAssignment::Quality > chargedHadronVertex(const reco::VertexCollection &vertices, const reco::TrackRef &trackRef, const reco::Track *track, const edm::View< reco::Candidate > &jets, const TransientTrackBuilder &builder) const
edm::EDGetTokenT< edm::View< reco::Candidate > > tokenJets_