CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_5_3_14/src/CommonTools/RecoUtils/src/PFCand_AssoMapAlgos.cc

Go to the documentation of this file.
00001 
00002 #include "CommonTools/RecoUtils/interface/PFCand_AssoMapAlgos.h"
00003 
00004 #include "TrackingTools/IPTools/interface/IPTools.h"
00005 
00006 using namespace edm;
00007 using namespace std;
00008 using namespace reco;
00009 
00010 /*************************************************************************************/
00011 /* dedicated constructor for the algorithms                                          */ 
00012 /*************************************************************************************/
00013 
00014 PFCand_AssoMapAlgos::PFCand_AssoMapAlgos(const edm::ParameterSet& iConfig):PF_PU_AssoMapAlgos(iConfig)
00015 {
00016 
00017         input_MaxNumAssociations_ = iConfig.getParameter<int>("MaxNumberOfAssociations");
00018 
00019         input_VertexCollection_= iConfig.getParameter<InputTag>("VertexCollection");
00020 
00021         input_BeamSpot_= iConfig.getParameter<InputTag>("BeamSpot");
00022 
00023 }
00024 
00025 /*************************************************************************************/
00026 /* get all needed collections at the beginning                                       */ 
00027 /*************************************************************************************/
00028 
00029 void 
00030 PFCand_AssoMapAlgos::GetInputCollections(edm::Event& iEvent, const edm::EventSetup& iSetup)
00031 {
00032 
00033         PF_PU_AssoMapAlgos::GetInputCollections(iEvent, iSetup);
00034   
00035         //get the offline beam spot
00036         iEvent.getByLabel(input_BeamSpot_, beamspotH);
00037           
00038         //get the input vertex collection
00039         iEvent.getByLabel(input_VertexCollection_, vtxcollH);
00040 
00041         iSetup.get<IdealMagneticFieldRecord>().get(bFieldH);
00042 
00043 }
00044 
00045 /*************************************************************************************/
00046 /* create the pf candidate to vertex association map                                 */ 
00047 /*************************************************************************************/
00048 
00049 std::auto_ptr<PFCandToVertexAssMap> 
00050 PFCand_AssoMapAlgos::CreatePFCandToVertexMap(edm::Handle<reco::PFCandidateCollection> pfCandH, const edm::EventSetup& iSetup)
00051 {
00052 
00053         auto_ptr<PFCandToVertexAssMap> pfcand2vertex(new PFCandToVertexAssMap());
00054 
00055         int num_vertices = vtxcollH->size();
00056         if ( num_vertices < input_MaxNumAssociations_) input_MaxNumAssociations_ = num_vertices;
00057    
00058         for( unsigned i=0; i<pfCandH->size(); i++ ) {
00059      
00060           PFCandidateRef candref(pfCandH, i);
00061 
00062           vector<VertexRef>* vtxColl_help = CreateVertexVector(vtxcollH);
00063 
00064           VertexPfcQuality VtxPfcQual;
00065 
00066           TrackRef PFCtrackref = candref->trackRef();
00067 
00068           if ( PFCtrackref.isNull() ){
00069 
00070             for ( int assoc_ite = 0; assoc_ite < input_MaxNumAssociations_; ++assoc_ite ) {
00071 
00072               int quality = -1 - assoc_ite; 
00073 
00074               // Insert the best vertex and the pair of track and the quality of this association in the map
00075               pfcand2vertex->insert( vtxColl_help->at(0), make_pair(candref, quality) );
00076  
00077               PF_PU_AssoMapAlgos::EraseVertex(vtxColl_help, vtxColl_help->at(0));
00078 
00079             }       
00080 
00081           } else {
00082 
00083             TransientTrack transtrk(PFCtrackref, &(*bFieldH) );
00084             transtrk.setBeamSpot(*beamspotH);
00085             transtrk.setES(iSetup);
00086 
00087             for ( int assoc_ite = 0; assoc_ite < input_MaxNumAssociations_; ++assoc_ite ) {
00088 
00089               VertexStepPair assocVtx = FindAssociation(PFCtrackref, vtxColl_help, bFieldH, iSetup, beamspotH, assoc_ite);       
00090               int step = assocVtx.second;
00091               double distance = ( IPTools::absoluteImpactParameter3D( transtrk, *(assocVtx.first) ) ).second.value();
00092            
00093               int quality = DefineQuality(assoc_ite, step, distance);
00094     
00095               // Insert the best vertex and the pair of track and the quality of this association in the map
00096               pfcand2vertex->insert( assocVtx.first, make_pair(candref, quality) );
00097 
00098               PF_PU_AssoMapAlgos::EraseVertex(vtxColl_help, assocVtx.first);
00099 
00100             }
00101 
00102           }
00103 
00104           delete vtxColl_help;    
00105         }
00106 
00107         return pfcand2vertex;
00108 
00109 }
00110 
00111 /*************************************************************************************/
00112 /* create the vertex to pf candidate association map                                 */ 
00113 /*************************************************************************************/
00114 
00115 std::auto_ptr<VertexToPFCandAssMap> 
00116 PFCand_AssoMapAlgos::CreateVertexToPFCandMap(edm::Handle<reco::PFCandidateCollection> pfCandH, const edm::EventSetup& iSetup)
00117 {
00118 
00119         auto_ptr<VertexToPFCandAssMap> vertex2pfcand(new VertexToPFCandAssMap());
00120 
00121         int num_vertices = vtxcollH->size();
00122         if ( num_vertices < input_MaxNumAssociations_) input_MaxNumAssociations_ = num_vertices;
00123    
00124         for( unsigned i=0; i<pfCandH->size(); i++ ) {
00125      
00126           PFCandidateRef candref(pfCandH, i);
00127 
00128           vector<VertexRef>* vtxColl_help = CreateVertexVector(vtxcollH);
00129 
00130           VertexPfcQuality VtxPfcQual;
00131 
00132           TrackRef PFCtrackref = candref->trackRef();
00133 
00134           if ( PFCtrackref.isNull() ){
00135 
00136             for ( int assoc_ite = 0; assoc_ite < input_MaxNumAssociations_; ++assoc_ite ) {
00137 
00138               int quality = -1 - assoc_ite; 
00139 
00140               // Insert the best vertex and the pair of track and the quality of this association in the map
00141               vertex2pfcand->insert( candref, make_pair(vtxColl_help->at(0), quality) );
00142  
00143               PF_PU_AssoMapAlgos::EraseVertex(vtxColl_help, vtxColl_help->at(0));
00144 
00145             }       
00146 
00147           } else {
00148 
00149             TransientTrack transtrk(PFCtrackref, &(*bFieldH) );
00150             transtrk.setBeamSpot(*beamspotH);
00151             transtrk.setES(iSetup);
00152 
00153             for ( int assoc_ite = 0; assoc_ite < input_MaxNumAssociations_; ++assoc_ite ) {
00154 
00155               VertexStepPair assocVtx = FindAssociation(PFCtrackref, vtxColl_help, bFieldH, iSetup, beamspotH, assoc_ite);       
00156               int step = assocVtx.second;
00157               double distance = ( IPTools::absoluteImpactParameter3D( transtrk, *(assocVtx.first) ) ).second.value();
00158            
00159               int quality = DefineQuality(assoc_ite, step, distance);
00160     
00161               // Insert the best vertex and the pair of track and the quality of this association in the map
00162               vertex2pfcand->insert( candref, make_pair(assocVtx.first, quality) );
00163 
00164               PF_PU_AssoMapAlgos::EraseVertex(vtxColl_help, assocVtx.first);
00165 
00166             }
00167 
00168           }
00169 
00170           delete vtxColl_help;
00171         }
00172 
00173         return vertex2pfcand;
00174 }
00175 
00176 /*************************************************************************************/
00177 /* create the vertex to pf candidate association map                                 */ 
00178 /*************************************************************************************/
00179 
00180 std::auto_ptr<PFCandToVertexAssMap> 
00181 PFCand_AssoMapAlgos::SortPFCandAssociationMap(PFCandToVertexAssMap* pfcvertexassInput)
00182 {
00183         //create a new PFCandVertexAssMap for the Output which will be sorted
00184         auto_ptr<PFCandToVertexAssMap> pfcvertexassOutput(new PFCandToVertexAssMap() );
00185 
00186         //Create and fill a vector of pairs of vertex and the summed (pT)**2 of the pfcandidates associated to the vertex 
00187         VertexPtsumVector vertexptsumvector;
00188 
00189         //loop over all vertices in the association map
00190         for(PFCandToVertexAssMap::const_iterator assomap_ite=pfcvertexassInput->begin(); assomap_ite!=pfcvertexassInput->end(); assomap_ite++){
00191 
00192           const VertexRef assomap_vertexref = assomap_ite->key;
00193           const PFCandQualityPairVector pfccoll = assomap_ite->val;
00194 
00195           float ptsum = 0;
00196  
00197           PFCandidateRef pfcandref;
00198 
00199           //get the pfcandidates associated to the vertex and calculate the pT**2
00200           for(unsigned int pfccoll_ite=0; pfccoll_ite<pfccoll.size(); pfccoll_ite++){
00201 
00202             pfcandref = pfccoll[pfccoll_ite].first;
00203             int quality = pfccoll[pfccoll_ite].second;
00204 
00205             if ( (quality<=2) && (quality!=-1) ) continue;       
00206 
00207             double man_pT = pfcandref->pt();
00208             if(man_pT>0.) ptsum+=man_pT*man_pT;
00209 
00210           }
00211 
00212           vertexptsumvector.push_back(make_pair(assomap_vertexref,ptsum));
00213 
00214         }
00215 
00216         while (vertexptsumvector.size()!=0){
00217 
00218           VertexRef vertexref_highestpT;
00219           float highestpT = 0.;
00220           int highestpT_index = 0;
00221 
00222           for(unsigned int vtxptsumvec_ite=0; vtxptsumvec_ite<vertexptsumvector.size(); vtxptsumvec_ite++){
00223  
00224             if(vertexptsumvector[vtxptsumvec_ite].second>highestpT){
00225 
00226               vertexref_highestpT = vertexptsumvector[vtxptsumvec_ite].first;
00227               highestpT = vertexptsumvector[vtxptsumvec_ite].second;
00228               highestpT_index = vtxptsumvec_ite;
00229         
00230             }
00231 
00232           }
00233           
00234           //loop over all vertices in the association map
00235           for(PFCandToVertexAssMap::const_iterator assomap_ite=pfcvertexassInput->begin(); assomap_ite!=pfcvertexassInput->end(); assomap_ite++){
00236 
00237             const VertexRef assomap_vertexref = assomap_ite->key;
00238             const PFCandQualityPairVector pfccoll = assomap_ite->val;
00239 
00240             //if the vertex from the association map the vertex with the highest pT 
00241             //insert all associated pfcandidates in the output Association Map
00242             if(assomap_vertexref==vertexref_highestpT) 
00243               for(unsigned int pfccoll_ite=0; pfccoll_ite<pfccoll.size(); pfccoll_ite++) 
00244                 pfcvertexassOutput->insert(assomap_vertexref,pfccoll[pfccoll_ite]);
00245  
00246           }
00247 
00248           vertexptsumvector.erase(vertexptsumvector.begin()+highestpT_index);   
00249 
00250         }
00251 
00252         return pfcvertexassOutput;
00253 }