Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:50:05

0001 // -*- C++ -*-
0002 //
0003 // Package:    PF_PU_AssoMap
0004 // Class:      PF_PU_FirstVertexTracks
0005 //
0006 /**\class PF_PU_AssoMap PF_PU_FirstVertexTracks.cc CommonTools/RecoUtils/plugins/PF_PU_FirstVertexTracks.cc
0007 
0008   Description: Produces collection of tracks associated to the first vertex based on the pf_pu Association Map
0009 */
0010 //
0011 // Original Author:  Matthias Geisler
0012 //         Created:  Wed Apr 18 14:48:37 CEST 2012
0013 // $Id: PF_PU_FirstVertexTracks.cc,v 1.1 2012/11/21 09:57:30 mgeisler Exp $
0014 //
0015 //
0016 #include "CommonTools/RecoUtils/interface/PF_PU_FirstVertexTracks.h"
0017 
0018 // system include files
0019 #include <vector>
0020 #include <string>
0021 
0022 // user include files
0023 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0024 
0025 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0026 #include "DataFormats/Common/interface/Handle.h"
0027 #include "DataFormats/Common/interface/OneToManyWithQualityGeneric.h"
0028 #include "DataFormats/Common/interface/View.h"
0029 
0030 #include "DataFormats/TrackReco/interface/TrackBase.h"
0031 #include "DataFormats/EgammaCandidates/interface/GsfElectron.h"
0032 #include "DataFormats/EgammaCandidates/interface/GsfElectronFwd.h"
0033 #include "DataFormats/BeamSpot/interface/BeamSpot.h"
0034 
0035 //
0036 // constants, enums and typedefs
0037 //
0038 
0039 using namespace edm;
0040 using namespace std;
0041 using namespace reco;
0042 
0043 typedef vector<pair<TrackRef, int> > TrackQualityPairVector;
0044 
0045 //
0046 // static data member definitions
0047 //
0048 
0049 //
0050 // constructors and destructor
0051 //
0052 PF_PU_FirstVertexTracks::PF_PU_FirstVertexTracks(const edm::ParameterSet& iConfig) {
0053   //now do what ever other initialization is needed
0054 
0055   input_AssociationType_ = iConfig.getParameter<edm::InputTag>("AssociationType");
0056 
0057   token_TrackToVertexAssMap_ = mayConsume<TrackToVertexAssMap>(iConfig.getParameter<InputTag>("AssociationMap"));
0058   token_VertexToTrackAssMap_ = mayConsume<VertexToTrackAssMap>(iConfig.getParameter<InputTag>("AssociationMap"));
0059 
0060   token_generalTracksCollection_ = consumes<TrackCollection>(iConfig.getParameter<InputTag>("TrackCollection"));
0061 
0062   token_VertexCollection_ = mayConsume<VertexCollection>(iConfig.getParameter<InputTag>("VertexCollection"));
0063 
0064   input_MinQuality_ = iConfig.getParameter<int>("MinQuality");
0065 
0066   //register your products
0067 
0068   if (input_AssociationType_.label() == "TracksToVertex") {
0069     produces<TrackCollection>("T2V");
0070   } else {
0071     if (input_AssociationType_.label() == "VertexToTracks") {
0072       produces<TrackCollection>("V2T");
0073     } else {
0074       if (input_AssociationType_.label() == "Both") {
0075         produces<TrackCollection>("T2V");
0076         produces<TrackCollection>("V2T");
0077       } else {
0078         std::cout << "No correct InputTag for AssociationType!" << std::endl;
0079         std::cout << "Won't produce any TrackCollection!" << std::endl;
0080       }
0081     }
0082   }
0083 }
0084 
0085 PF_PU_FirstVertexTracks::~PF_PU_FirstVertexTracks() {
0086   // do anything here that needs to be done at desctruction time
0087   // (e.g. close files, deallocate resources etc.)
0088 }
0089 
0090 //
0091 // member functions
0092 //
0093 
0094 // ------------ method called to produce the data  ------------
0095 void PF_PU_FirstVertexTracks::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) {
0096   unique_ptr<TrackCollection> t2v_firstvertextracks(new TrackCollection());
0097   unique_ptr<TrackCollection> v2t_firstvertextracks(new TrackCollection());
0098 
0099   bool t2vassmap = false;
0100   bool v2tassmap = false;
0101 
0102   //get the input vertex<->general track association map
0103   Handle<TrackToVertexAssMap> t2vAM;
0104   Handle<VertexToTrackAssMap> v2tAM;
0105 
0106   string asstype = input_AssociationType_.label();
0107 
0108   if ((asstype == "TracksToVertex") || (asstype == "Both")) {
0109     if (iEvent.getByToken(token_TrackToVertexAssMap_, t2vAM)) {
0110       t2vassmap = true;
0111     }
0112   }
0113 
0114   if ((asstype == "VertexToTracks") || (asstype == "Both")) {
0115     if (iEvent.getByToken(token_VertexToTrackAssMap_, v2tAM)) {
0116       v2tassmap = true;
0117     }
0118   }
0119 
0120   if (!t2vassmap && !v2tassmap) {
0121     cout << "No input collection could be found" << endl;
0122     return;
0123   }
0124 
0125   //get the input track collection
0126   Handle<TrackCollection> input_trckcollH;
0127   iEvent.getByToken(token_generalTracksCollection_, input_trckcollH);
0128 
0129   if (t2vassmap) {
0130     const TrackQualityPairVector trckcoll = t2vAM->begin()->val;
0131 
0132     //get the tracks associated to the first vertex and store them in a track collection
0133     for (unsigned int trckcoll_ite = 0; trckcoll_ite < trckcoll.size(); trckcoll_ite++) {
0134       float quality = trckcoll[trckcoll_ite].second;
0135 
0136       if (quality >= input_MinQuality_) {
0137         TrackRef AMtrkref = trckcoll[trckcoll_ite].first;
0138 
0139         for (unsigned int index_input_trck = 0; index_input_trck < input_trckcollH->size(); index_input_trck++) {
0140           TrackRef input_trackref = TrackRef(input_trckcollH, index_input_trck);
0141 
0142           if (TrackMatch(*AMtrkref, *input_trackref)) {
0143             t2v_firstvertextracks->push_back(*AMtrkref);
0144             break;
0145           }
0146         }
0147       }
0148     }
0149 
0150     iEvent.put(std::move(t2v_firstvertextracks), "T2V");
0151   }
0152 
0153   if (v2tassmap) {
0154     //get the input vertex collection
0155     Handle<VertexCollection> input_vtxcollH;
0156     iEvent.getByToken(token_VertexCollection_, input_vtxcollH);
0157 
0158     VertexRef firstVertexRef(input_vtxcollH, 0);
0159 
0160     VertexToTrackAssMap::const_iterator v2t_ite;
0161 
0162     for (v2t_ite = v2tAM->begin(); v2t_ite != v2tAM->end(); v2t_ite++) {
0163       TrackRef AMtrkref = v2t_ite->key;
0164 
0165       for (unsigned int index_input_trck = 0; index_input_trck < input_trckcollH->size(); index_input_trck++) {
0166         TrackRef input_trackref = TrackRef(input_trckcollH, index_input_trck);
0167 
0168         if (TrackMatch(*AMtrkref, *input_trackref)) {
0169           for (unsigned v_ite = 0; v_ite < (v2t_ite->val).size(); v_ite++) {
0170             VertexRef vtxref = (v2t_ite->val)[v_ite].first;
0171             float quality = (v2t_ite->val)[v_ite].second;
0172 
0173             if ((vtxref == firstVertexRef) && (quality >= input_MinQuality_)) {
0174               v2t_firstvertextracks->push_back(*AMtrkref);
0175             }
0176           }
0177         }
0178       }
0179     }
0180 
0181     iEvent.put(std::move(v2t_firstvertextracks), "V2T");
0182   }
0183 }
0184 
0185 bool PF_PU_FirstVertexTracks::TrackMatch(const Track& track1, const Track& track2) {
0186   return ((track1).eta() == (track2).eta() && (track1).phi() == (track2).phi() && (track1).chi2() == (track2).chi2() &&
0187           (track1).ndof() == (track2).ndof() && (track1).p() == (track2).p());
0188 }
0189 
0190 // ------------ method fills 'descriptions' with the allowed parameters for the module  ------------
0191 void PF_PU_FirstVertexTracks::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0192   //The following says we do not know what parameters are allowed so do no validation
0193   // Please change this to state exactly what you do use, even if it is no parameters
0194   edm::ParameterSetDescription desc;
0195   desc.setUnknown();
0196   descriptions.addDefault(desc);
0197 }
0198 
0199 //define this as a plug-in
0200 DEFINE_FWK_MODULE(PF_PU_FirstVertexTracks);