CMS 3D CMS Logo

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 
29 template <class ParticlesCollection>
30 
32  public:
33 
37 
38  typedef ParticlesCollection PFCollection;
39 
40  explicit PrimaryVertexSorter(const edm::ParameterSet&);
41 
42  ~PrimaryVertexSorter() override {}
43 
44  void produce(edm::Event&, const edm::EventSetup&) override;
45 
46  private:
47 
50 
53 
59 
65  bool useMET_;
66  bool useTiming_;
67 
68  void doConsumesForTiming(const edm::ParameterSet &iConfig) ;
69  bool needsProductsForTiming() ;
70  std::pair<int,PrimaryVertexAssignment::Quality> runAlgo( const reco::VertexCollection& vertices, const typename ParticlesCollection::value_type & pf, const edm::ValueMap<float> *trackTimeTag,
72 };
73 
74 
75 
80 
81 // #include "FWCore/MessageLogger/interface/MessageLogger.h"
84 
85 
86 
87 template <class ParticlesCollection>
89  assignmentAlgo_(iConfig.getParameterSet("assignment")),
90  sortingAlgo_(iConfig.getParameterSet("sorting")),
91  tokenCandidates_(consumes<ParticlesCollection>(iConfig.getParameter<edm::InputTag>("particles"))),
92  tokenVertices_(consumes<reco::VertexCollection>(iConfig.getParameter<edm::InputTag>("vertices"))),
93  tokenJets_(consumes<edm::View<reco::Candidate> > (iConfig.getParameter<edm::InputTag>("jets"))),
94  produceOriginalMapping_(iConfig.getParameter<bool>("produceAssociationToOriginalVertices")),
95  produceSortedVertices_(iConfig.getParameter<bool>("produceSortedVertices")),
96  producePFPileUp_(iConfig.getParameter<bool>("producePileUpCollection")),
97  producePFNoPileUp_(iConfig.getParameter<bool>("produceNoPileUpCollection")),
98  qualityCut_(iConfig.getParameter<int>("qualityForPrimary")),
99  useMET_(iConfig.getParameter<bool>("usePVMET")),
100  useTiming_(iConfig.getParameterSet("assignment").getParameter<bool>("useTiming"))
101 {
102 
103 using namespace std;
104 using namespace edm;
105 using namespace reco;
106 
108  produces< CandToVertex> ("original");
109  produces< CandToVertexQuality> ("original");
110  produces< VertexScore> ("original");
111  }
113  produces< reco::VertexCollection> ();
114  produces< CandToVertex> ();
115  produces< CandToVertexQuality> ();
116  produces< VertexScore> ();
117  }
118 
119  if(producePFPileUp_){
121  produces< PFCollection> ("originalPileUp");
123  produces< PFCollection> ("PileUp");
124  }
125 
126  if(producePFNoPileUp_){
128  produces< PFCollection> ("originalNoPileUp");
130  produces< PFCollection> ("NoPileUp");
131  }
132 
133  if (useTiming_) doConsumesForTiming(iConfig);
134 
135 }
136 
137 
138 
139 
140 
141 template <class ParticlesCollection>
143 
144 using namespace std;
145 using namespace edm;
146 using namespace reco;
147 
149  iEvent.getByToken( tokenJets_, jets);
150 
152  iSetup.get<TransientTrackRecord>().get("TransientTrackBuilder", builder);
153 
154 
156  iEvent.getByToken( tokenVertices_, vertices);
157 
158  Handle<ParticlesCollection> particlesHandle;
159  iEvent.getByToken( tokenCandidates_, particlesHandle);
160 
161  Handle<edm::ValueMap<float> > trackTimeTagHandle;
162  Handle<edm::ValueMap<float> > trackTimeResoTagHandle;
163 
164  const edm::ValueMap<float> *trackTimeTag = nullptr;
165  const edm::ValueMap<float> *trackTimeResoTag = nullptr;
167  iEvent.getByToken(tokenTrackTimeTag_, trackTimeTagHandle);
168  iEvent.getByToken(tokenTrackTimeResoTag_, trackTimeResoTagHandle);
169 
170  trackTimeTag = trackTimeTagHandle.product();
171  trackTimeResoTag = trackTimeResoTagHandle.product();
172  }
173 
174  ParticlesCollection particles = *particlesHandle.product();
175  std::vector<int> pfToPVVector;
176  std::vector<PrimaryVertexAssignment::Quality> pfToPVQualityVector;
177  //reverse mapping
178  std::vector< std::vector<int> > pvToPFVector(vertices->size());
179  std::vector< std::vector<const reco::Candidate *> > pvToCandVector(vertices->size());
180  std::vector< std::vector<PrimaryVertexAssignment::Quality> > pvToPFQualityVector(vertices->size());
181  std::vector<float> vertexScoreOriginal(vertices->size());
182  std::vector<float> vertexScore(vertices->size());
183 
184  for(auto const & pf : particles) {
185  std::pair<int,PrimaryVertexAssignment::Quality> vtxWithQuality = runAlgo(*vertices,pf,trackTimeTag,trackTimeResoTag,*jets,*builder);
186  pfToPVVector.push_back(vtxWithQuality.first);
187  pfToPVQualityVector.push_back(vtxWithQuality.second);
188  }
189 
190  //Invert the mapping
191  for(size_t i = 0; i < pfToPVVector.size();i++)
192  {
193  auto pv = pfToPVVector[i];
194  auto qual = pfToPVQualityVector[i];
195  if(pv >=0 and qual >= qualityCut_){
196  pvToPFVector[pv].push_back(i);
197 // std::cout << i << std::endl;
198 // const typename ParticlesCollection::value_type & cp = particles[i];
199 // std::cout << "CP " << &cp << std::endl;
200  pvToCandVector[pv].push_back( &particles[i] );
201  pvToPFQualityVector[pv].push_back(qual);
202  }
203  }
204 
205  //Use multimap for sorting of indices
206  std::multimap<float,int> scores;
207  for(unsigned int i=0;i<vertices->size();i++){
208  float s=sortingAlgo_.score((*vertices)[i],pvToCandVector[i],useMET_);
209  vertexScoreOriginal[i]=s;
210  scores.insert(std::pair<float,int>(-s,i));
211  }
212 
213  //create indices
214  std::vector<int> oldToNew(vertices->size()), newToOld(vertices->size());
215  size_t newIdx=0;
216  for(auto const & idx : scores)
217  {
218 // std::cout << newIdx << " score: " << idx.first << " oldidx: " << idx.second << " "<< producePFPileUp_ << std::endl;
219  vertexScore[newIdx]=-idx.first;
220  oldToNew[idx.second]=newIdx;
221  newToOld[newIdx]=idx.second;
222  newIdx++;
223  }
224 
225 
226 
227 
229  unique_ptr< CandToVertex> pfCandToOriginalVertexOutput( new CandToVertex(vertices) );
230  unique_ptr< CandToVertexQuality> pfCandToOriginalVertexQualityOutput( new CandToVertexQuality() );
231  CandToVertex::Filler cand2VertexFiller(*pfCandToOriginalVertexOutput);
232  CandToVertexQuality::Filler cand2VertexQualityFiller(*pfCandToOriginalVertexQualityOutput);
233 
234  cand2VertexFiller.insert(particlesHandle,pfToPVVector.begin(),pfToPVVector.end());
235  cand2VertexQualityFiller.insert(particlesHandle,pfToPVQualityVector.begin(),pfToPVQualityVector.end());
236 
237  cand2VertexFiller.fill();
238  cand2VertexQualityFiller.fill();
239  iEvent.put(std::move(pfCandToOriginalVertexOutput) ,"original");
240  iEvent.put(std::move(pfCandToOriginalVertexQualityOutput) ,"original");
241 
242  unique_ptr< VertexScore> vertexScoreOriginalOutput( new VertexScore );
243  VertexScore::Filler vertexScoreOriginalFiller(*vertexScoreOriginalOutput);
244  vertexScoreOriginalFiller.insert(vertices,vertexScoreOriginal.begin(),vertexScoreOriginal.end());
245  vertexScoreOriginalFiller.fill();
246  iEvent.put(std::move(vertexScoreOriginalOutput) ,"original");
247 
248  }
249 
251  std::vector<int> pfToSortedPVVector;
252 // std::vector<int> pfToSortedPVQualityVector;
253  for(size_t i=0;i<pfToPVVector.size();i++) {
254  pfToSortedPVVector.push_back(oldToNew[pfToPVVector[i]]);
255 // pfToSortedPVQualityVector.push_back(pfToPVQualityVector[i]); //same as old!
256  }
257 
258  unique_ptr< reco::VertexCollection> sortedVerticesOutput( new reco::VertexCollection );
259  for(size_t i=0;i<vertices->size();i++){
260  sortedVerticesOutput->push_back((*vertices)[newToOld[i]]);
261  }
262  edm::OrphanHandle<reco::VertexCollection> oh = iEvent.put(std::move(sortedVerticesOutput));
263  unique_ptr< CandToVertex> pfCandToVertexOutput( new CandToVertex(oh) );
264  unique_ptr< CandToVertexQuality> pfCandToVertexQualityOutput( new CandToVertexQuality() );
265  CandToVertex::Filler cand2VertexFiller(*pfCandToVertexOutput);
266  CandToVertexQuality::Filler cand2VertexQualityFiller(*pfCandToVertexQualityOutput);
267 
268  cand2VertexFiller.insert(particlesHandle,pfToSortedPVVector.begin(),pfToSortedPVVector.end());
269  cand2VertexQualityFiller.insert(particlesHandle,pfToPVQualityVector.begin(),pfToPVQualityVector.end());
270 
271  cand2VertexFiller.fill();
272  cand2VertexQualityFiller.fill();
273  iEvent.put(std::move(pfCandToVertexOutput ));
274  iEvent.put(std::move(pfCandToVertexQualityOutput ));
275 
276  unique_ptr< VertexScore> vertexScoreOutput( new VertexScore );
277  VertexScore::Filler vertexScoreFiller(*vertexScoreOutput);
278  vertexScoreFiller.insert(oh,vertexScore.begin(),vertexScore.end());
279  vertexScoreFiller.fill();
280  iEvent.put(std::move(vertexScoreOutput));
281 
282 
283  }
284 
285 
286  unique_ptr< PFCollection > pfCollectionNOPUOriginalOutput( new PFCollection );
287  unique_ptr< PFCollection > pfCollectionNOPUOutput( new PFCollection );
288  unique_ptr< PFCollection > pfCollectionPUOriginalOutput( new PFCollection );
289  unique_ptr< PFCollection > pfCollectionPUOutput( new PFCollection );
290 
291  for(size_t i=0;i<particles.size();i++) {
292  auto pv = pfToPVVector[i];
293  auto qual = pfToPVQualityVector[i];
294 
295 
297  if(pv == newToOld[0] and qual >= qualityCut_)
298  pfCollectionNOPUOutput->push_back(particles[i]);
299 
301  if(pv != newToOld[0] and qual >= qualityCut_)
302  pfCollectionPUOutput->push_back(particles[i]);
303 
305  if(pv == 0 and qual >= qualityCut_)
306  pfCollectionNOPUOriginalOutput->push_back(particles[i]);
307 
309  if(pv != 0 and qual >= qualityCut_)
310  pfCollectionPUOriginalOutput->push_back(particles[i]);
311 
312  }
313  if(producePFNoPileUp_ && produceSortedVertices_) iEvent.put(std::move(pfCollectionNOPUOutput),"NoPileUp" );
314  if(producePFPileUp_ && produceSortedVertices_) iEvent.put(std::move(pfCollectionPUOutput), "PileUp");
315  if(producePFNoPileUp_ && produceOriginalMapping_) iEvent.put(std::move(pfCollectionNOPUOriginalOutput),"originalNoPileUp" );
316  if(producePFPileUp_ && produceOriginalMapping_) iEvent.put(std::move(pfCollectionPUOriginalOutput),"originalPileUp" );
317 
318 
319 }
320 
321 
322 template<>
324 {
325  tokenTrackTimeTag_ = consumes<edm::ValueMap<float> > (iConfig.getParameter<edm::InputTag>("trackTimeTag"));
326  tokenTrackTimeResoTag_ = consumes<edm::ValueMap<float> > (iConfig.getParameter<edm::InputTag>("trackTimeResoTag"));
327 }
328 
329 template<>
331 {
332 }
333 
334 template<>
336 {
337  return true;
338 }
339 
340 template<>
342 {
343  return false;
344 }
345 
346 template<>
347 std::pair<int,PrimaryVertexAssignment::Quality>
350 {
351  return assignmentAlgo_.chargedHadronVertex( vertices, pf, trackTimeTag, trackTimeResoTag, jets, builder);
352 }
353 
354 template<>
355 std::pair<int,PrimaryVertexAssignment::Quality>
358 {
359  return assignmentAlgo_.chargedHadronVertex( vertices, pf, jets, builder);
360 }
361 
362 #endif
T getParameter(std::string const &) const
void doConsumesForTiming(const edm::ParameterSet &iConfig)
edm::Association< reco::VertexCollection > CandToVertex
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:125
PrimaryVertexSorting sortingAlgo_
void produce(edm::Event &, const edm::EventSetup &) override
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:517
ParameterSet const & getParameterSet(ParameterSetID const &id)
void insert(const H &h, I begin, I end)
Definition: ValueMap.h:53
std::vector< Vertex > VertexCollection
collection of Vertex objects
Definition: VertexFwd.h:9
edm::EDGetTokenT< PFCollection > tokenCandidates_
Candidates to be analyzed.
int iEvent
Definition: GenABIO.cc:224
ParticlesCollection PFCollection
vector< PseudoJet > jets
def oldToNew(schema)
Definition: lumidbDDL.py:402
std::pair< int, PrimaryVertexAssignment::Quality > chargedHadronVertex(const reco::VertexCollection &vertices, const reco::TrackRef &trackRef, const reco::Track *track, float trackTime, float trackTimeResolution, const edm::View< reco::Candidate > &jets, const TransientTrackBuilder &builder) const
def pv(vc)
Definition: MetAnalyzer.py:7
bool needsProductsForTiming()
edm::EDGetTokenT< edm::ValueMap< float > > tokenTrackTimeTag_
edm::ValueMap< float > VertexScore
T const * product() const
Definition: Handle.h:74
PrimaryVertexAssignment assignmentAlgo_
std::pair< int, PrimaryVertexAssignment::Quality > runAlgo(const reco::VertexCollection &vertices, const typename ParticlesCollection::value_type &pf, const edm::ValueMap< float > *trackTimeTag, const edm::ValueMap< float > *trackTimeResoTag, const edm::View< reco::Candidate > &jets, const TransientTrackBuilder &builder)
float score(const reco::Vertex &pv, const std::vector< const reco::Candidate * > &candidates, bool useMet) const
edm::ValueMap< int > CandToVertexQuality
Particle reconstructed by the particle flow algorithm.
Definition: PFCandidate.h:40
fixed size matrix
HLT enums.
T get() const
Definition: EventSetup.h:71
edm::EDGetTokenT< edm::ValueMap< float > > tokenTrackTimeResoTag_
def newToOld(schema)
Definition: lumidbDDL.py:416
edm::EDGetTokenT< reco::VertexCollection > tokenVertices_
vertices
def move(src, dest)
Definition: eostools.py:511
PrimaryVertexSorter(const edm::ParameterSet &)
edm::EDGetTokenT< edm::View< reco::Candidate > > tokenJets_