CMS 3D CMS Logo

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
PFCand_AssoMapAlgos.cc
Go to the documentation of this file.
1 
3 
5 
6 using namespace edm;
7 using namespace std;
8 using namespace reco;
9 
10 /*************************************************************************************/
11 /* dedicated constructor for the algorithms */
12 /*************************************************************************************/
13 
15 {
16 
17  input_MaxNumAssociations_ = iConfig.getParameter<int>("MaxNumberOfAssociations");
18 
19  input_VertexCollection_= iConfig.getParameter<InputTag>("VertexCollection");
20 
21  input_BeamSpot_= iConfig.getParameter<InputTag>("BeamSpot");
22 
23 }
24 
25 /*************************************************************************************/
26 /* get all needed collections at the beginning */
27 /*************************************************************************************/
28 
29 void
31 {
32 
34 
35  //get the offline beam spot
37 
38  //get the input vertex collection
40 
41  iSetup.get<IdealMagneticFieldRecord>().get(bFieldH);
42 
43 }
44 
45 /*************************************************************************************/
46 /* set pointer to mother candidates */
47 /*************************************************************************************/
48 
49 void
51 {
52 
53  PFCandidateCollection& inCands = (PFCandidateCollection&) *pfCandH;
54 
55  for(unsigned i=0; i<inCands.size(); i++) {
56 
57  const PFCandidatePtr motherPtr( pfCandH, i );
58  inCands[i].setSourceCandidatePtr( motherPtr );
59 
60  }
61 
62 }
63 
64 /*************************************************************************************/
65 /* create the pf candidate to vertex association map */
66 /*************************************************************************************/
67 
68 std::auto_ptr<PFCandToVertexAssMap>
70 {
71 
72  auto_ptr<PFCandToVertexAssMap> pfcand2vertex(new PFCandToVertexAssMap());
73 
74  int num_associations = input_MaxNumAssociations_;
75  int num_vertices = vtxcollH->size();
76  if ( num_vertices < num_associations) num_associations = num_vertices;
77 
78  for( unsigned i=0; i<pfCandH->size(); i++ ) {
79 
80  PFCandidateRef candref(pfCandH, i);
81 
82  vector<VertexRef>* vtxColl_help = CreateVertexVector(vtxcollH);
83  StepDistancePairVector distances;
84 
85  VertexPfcQuality VtxPfcQual;
86 
87  TrackRef PFCtrackref = candref->trackRef();
88 
89  if ( PFCtrackref.isNull() ){
90 
91  for ( int assoc_ite = 0; assoc_ite < num_associations; ++assoc_ite ) {
92 
93  int quality = -1 - assoc_ite;
94 
95  // edm::LogInfo("Prtcl2VtxAssociation") << "associating track with PdgId " << candref->pdgId() << ": Pt = " << candref->pt() << ","
96  // << " eta = " << candref->eta() << ", phi = " << candref->phi()
97  // << " to vertex: z = " << vtxColl_help->at(0)->position().z() << " with quality q = " << quality << std::endl;
98 
99  // Insert the best vertex and the pair of track and the quality of this association in the map
100  pfcand2vertex->insert( vtxColl_help->at(0), make_pair(candref, quality) );
101 
102  PF_PU_AssoMapAlgos::EraseVertex(vtxColl_help, vtxColl_help->at(0));
103 
104  }
105 
106  } else {
107 
108  TransientTrack transtrk(PFCtrackref, &(*bFieldH) );
109  transtrk.setBeamSpot(*beamspotH);
110  transtrk.setES(iSetup);
111 
112  for ( int assoc_ite = 0; assoc_ite < num_associations; ++assoc_ite ) {
113 
114  VertexStepPair assocVtx = FindAssociation(PFCtrackref, vtxColl_help, bFieldH, iSetup, beamspotH, assoc_ite);
115  VertexRef associatedVertex = assocVtx.first;
116  int step = assocVtx.second;
117  double distance = ( IPTools::absoluteImpactParameter3D( transtrk, *associatedVertex ) ).second.value();
118 
119  int quality = DefineQuality(distances, step, distance);
120  distances.push_back( make_pair(step, distance) );
121 
122  // edm::LogInfo("Prtcl2VtxAssociation") << "associating track with PdgId " << candref->pdgId() << ": Pt = " << candref->pt() << ","
123  // << " eta = " << candref->eta() << ", phi = " << candref->phi()
124  // << " to vertex: z = " << vtxColl_help->at(0)->position().z() << " with quality q = " << quality << std::endl;
125 
126  // Insert the best vertex and the pair of track and the quality of this association in the map
127  pfcand2vertex->insert( associatedVertex, make_pair(candref, quality) );
128 
129  PF_PU_AssoMapAlgos::EraseVertex(vtxColl_help, associatedVertex);
130 
131  }
132 
133  }
134 
135  delete vtxColl_help;
136 
137  }
138 
139  return pfcand2vertex;
140 
141 }
142 
143 /*************************************************************************************/
144 /* create the vertex to pf candidate association map */
145 /*************************************************************************************/
146 
147 std::auto_ptr<VertexToPFCandAssMap>
149 {
150 
151  auto_ptr<VertexToPFCandAssMap> vertex2pfcand(new VertexToPFCandAssMap());
152 
153  int num_associations = input_MaxNumAssociations_;
154  int num_vertices = vtxcollH->size();
155  if ( num_vertices < num_associations) num_associations = num_vertices;
156 
157  for( unsigned i=0; i<pfCandH->size(); i++ ) {
158 
159  PFCandidateRef candref(pfCandH, i);
160 
161  vector<VertexRef>* vtxColl_help = CreateVertexVector(vtxcollH);
162  StepDistancePairVector distances;
163 
164  VertexPfcQuality VtxPfcQual;
165 
166  TrackRef PFCtrackref = candref->trackRef();
167 
168  if ( PFCtrackref.isNull() ) {
169 
170  for ( int assoc_ite = 0; assoc_ite < num_associations; ++assoc_ite ) {
171 
172  int quality = -1 - assoc_ite;
173 
174  // edm::LogInfo("Vtx2PrtclAssociation") << "associating track with PdgId " << candref->pdgId() << ": Pt = " << candref->pt() << ","
175  // << " eta = " << candref->eta() << ", phi = " << candref->phi()
176  // << " to vertex: z = " << vtxColl_help->at(0)->position().z() << " with quality q = " << quality << std::endl;
177 
178  // Insert the best vertex and the pair of track and the quality of this association in the map
179  vertex2pfcand->insert( candref, make_pair(vtxColl_help->at(0), quality) );
180 
181  PF_PU_AssoMapAlgos::EraseVertex(vtxColl_help, vtxColl_help->at(0));
182 
183  }
184 
185  } else {
186 
187  TransientTrack transtrk(PFCtrackref, &(*bFieldH) );
188  transtrk.setBeamSpot(*beamspotH);
189  transtrk.setES(iSetup);
190 
191  for ( int assoc_ite = 0; assoc_ite < num_associations; ++assoc_ite ) {
192 
193  VertexStepPair assocVtx = FindAssociation(PFCtrackref, vtxColl_help, bFieldH, iSetup, beamspotH, assoc_ite);
194  VertexRef associatedVertex = assocVtx.first;
195  int step = assocVtx.second;
196  double distance = ( IPTools::absoluteImpactParameter3D( transtrk, *associatedVertex ) ).second.value();
197 
198  int quality = DefineQuality(distances, step, distance);
199  distances.push_back( make_pair(step, distance) );
200 
201  // edm::LogInfo("Vtx2PrtclAssociation") << "associating track with PdgId " << candref->pdgId() << ": Pt = " << candref->pt() << ","
202  // << " eta = " << candref->eta() << ", phi = " << candref->phi()
203  // << " to vertex: z = " << vtxColl_help->at(0)->position().z() << " with quality q = " << quality << std::endl;
204 
205  // Insert the best vertex and the pair of track and the quality of this association in the map
206  vertex2pfcand->insert( candref, make_pair(associatedVertex, quality) );
207 
208  PF_PU_AssoMapAlgos::EraseVertex(vtxColl_help, associatedVertex);
209 
210  }
211 
212  }
213 
214  delete vtxColl_help;
215 
216  }
217 
218  return vertex2pfcand;
219 
220 }
221 
222 /*************************************************************************************/
223 /* create the vertex to pf candidate association map */
224 /*************************************************************************************/
225 
226 std::auto_ptr<PFCandToVertexAssMap>
228 {
229 
230  //create a new PFCandVertexAssMap for the Output which will be sorted
231  auto_ptr<PFCandToVertexAssMap> pfcvertexassOutput(new PFCandToVertexAssMap() );
232 
233  //Create and fill a vector of pairs of vertex and the summed (pT)**2 of the pfcandidates associated to the vertex
234  VertexPtsumVector vertexptsumvector;
235 
236  //loop over all vertices in the association map
237  for(PFCandToVertexAssMap::const_iterator assomap_ite=pfcvertexassInput->begin(); assomap_ite!=pfcvertexassInput->end(); assomap_ite++){
238 
239  const VertexRef assomap_vertexref = assomap_ite->key;
240  const PFCandQualityPairVector pfccoll = assomap_ite->val;
241 
242  float ptsum = 0;
243 
244  PFCandidateRef pfcandref;
245 
246  //get the pfcandidates associated to the vertex and calculate the pT**2
247  for(unsigned int pfccoll_ite=0; pfccoll_ite<pfccoll.size(); pfccoll_ite++){
248 
249  pfcandref = pfccoll[pfccoll_ite].first;
250  int quality = pfccoll[pfccoll_ite].second;
251 
252  if ( (quality<=2) && (quality!=-1) ) continue;
253 
254  double man_pT = pfcandref->pt();
255  if(man_pT>0.) ptsum+=man_pT*man_pT;
256 
257  }
258 
259  vertexptsumvector.push_back(make_pair(assomap_vertexref,ptsum));
260 
261  }
262 
263  while (vertexptsumvector.size()!=0){
264 
265  VertexRef vertexref_highestpT;
266  float highestpT = 0.;
267  int highestpT_index = 0;
268 
269  for(unsigned int vtxptsumvec_ite=0; vtxptsumvec_ite<vertexptsumvector.size(); vtxptsumvec_ite++){
270 
271  if(vertexptsumvector[vtxptsumvec_ite].second>highestpT){
272 
273  vertexref_highestpT = vertexptsumvector[vtxptsumvec_ite].first;
274  highestpT = vertexptsumvector[vtxptsumvec_ite].second;
275  highestpT_index = vtxptsumvec_ite;
276 
277  }
278 
279  }
280 
281  //loop over all vertices in the association map
282  for(PFCandToVertexAssMap::const_iterator assomap_ite=pfcvertexassInput->begin(); assomap_ite!=pfcvertexassInput->end(); assomap_ite++){
283 
284  const VertexRef assomap_vertexref = assomap_ite->key;
285  const PFCandQualityPairVector pfccoll = assomap_ite->val;
286 
287  //if the vertex from the association map the vertex with the highest pT
288  //insert all associated pfcandidates in the output Association Map
289  if (assomap_vertexref==vertexref_highestpT) {
290  for(unsigned int pfccoll_ite=0; pfccoll_ite<pfccoll.size(); pfccoll_ite++) {
291  pfcvertexassOutput->insert(assomap_vertexref,pfccoll[pfccoll_ite]);
292  }
293  }
294 
295  }
296 
297  vertexptsumvector.erase(vertexptsumvector.begin()+highestpT_index);
298 
299  }
300 
301  return pfcvertexassOutput;
302 
303 }
VertexStepPair FindAssociation(const reco::TrackRef &, std::vector< reco::VertexRef > *, edm::ESHandle< MagneticField >, const edm::EventSetup &, edm::Handle< reco::BeamSpot >, int)
T getParameter(std::string const &) const
int i
Definition: DBlmapReader.cc:9
const_iterator end() const
last iterator over the map (read only)
void setBeamSpot(const reco::BeamSpot &beamSpot)
edm::AssociationMap< edm::OneToManyWithQuality< reco::PFCandidateCollection, reco::VertexCollection, int > > VertexToPFCandAssMap
edm::InputTag input_BeamSpot_
std::pair< reco::VertexRef, PFCandQualityPair > VertexPfcQuality
void EraseVertex(std::vector< reco::VertexRef > *, reco::VertexRef)
std::pair< bool, Measurement1D > absoluteImpactParameter3D(const reco::TransientTrack &transientTrack, const reco::Vertex &vertex)
Definition: IPTools.cc:37
edm::AssociationMap< edm::OneToManyWithQuality< reco::VertexCollection, reco::PFCandidateCollection, int > > PFCandToVertexAssMap
std::pair< reco::VertexRef, int > VertexStepPair
U second(std::pair< T, U > const &p)
std::vector< VertexPtsumPair > VertexPtsumVector
int iEvent
Definition: GenABIO.cc:243
bool isNull() const
Checks for null.
Definition: Ref.h:247
std::auto_ptr< VertexToPFCandAssMap > CreateVertexToPFCandMap(edm::Handle< reco::PFCandidateCollection >, const edm::EventSetup &)
std::auto_ptr< PFCandToVertexAssMap > CreatePFCandToVertexMap(edm::Handle< reco::PFCandidateCollection >, const edm::EventSetup &)
edm::InputTag input_VertexCollection_
std::auto_ptr< PFCandToVertexAssMap > SortPFCandAssociationMap(PFCandToVertexAssMap *)
void SetMotherPointer(edm::Handle< reco::PFCandidateCollection > &)
virtual void GetInputCollections(edm::Event &, const edm::EventSetup &)
std::vector< PFCandQualityPair > PFCandQualityPairVector
edm::Handle< reco::BeamSpot > beamspotH
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:390
void setES(const edm::EventSetup &es)
PFCand_AssoMapAlgos(const edm::ParameterSet &)
std::vector< reco::PFCandidate > PFCandidateCollection
collection of PFCandidates
edm::ESHandle< MagneticField > bFieldH
const T & get() const
Definition: EventSetup.h:55
key_type key() const
Accessor for product key.
Definition: Ref.h:266
void GetPFCandInputCollections(edm::Event &, const edm::EventSetup &)
std::vector< reco::VertexRef > * CreateVertexVector(edm::Handle< reco::VertexCollection >)
int DefineQuality(StepDistancePairVector, int, double)
edm::Handle< reco::VertexCollection > vtxcollH
const_iterator begin() const
first iterator over the map (read only)
std::vector< StepDistancePair > StepDistancePairVector