Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:22:47

0001 //
0002 //
0003 
0004 /**
0005   \class    pat::MatcherUsingTracks MatcherUsingTracks.h "MuonAnalysis/MuonAssociators/interface/MatcherUsingTracks.h"
0006   \brief    Matcher of reconstructed objects to other reconstructed objects using the tracks inside them
0007 
0008   \author   Giovanni Petrucciani
0009   \version  $Id: MatcherUsingTracks.cc,v 1.5 2010/07/12 20:56:11 gpetrucc Exp $
0010 */
0011 
0012 #include "FWCore/Framework/interface/stream/EDProducer.h"
0013 #include "FWCore/Framework/interface/Event.h"
0014 #include "FWCore/Utilities/interface/InputTag.h"
0015 
0016 #include "DataFormats/Common/interface/Association.h"
0017 #include "DataFormats/Common/interface/ValueMap.h"
0018 
0019 #include "DataFormats/Candidate/interface/Candidate.h"
0020 
0021 #include "DataFormats/PatCandidates/interface/UserData.h"
0022 
0023 #include "MuonAnalysis/MuonAssociators/interface/MatcherUsingTracksAlgorithm.h"
0024 
0025 // template-related workaround for bug in OwnVector+Ptr
0026 namespace edm {
0027   using std::advance;
0028 }
0029 
0030 namespace pat {
0031 
0032   class MatcherUsingTracks : public edm::stream::EDProducer<> {
0033   public:
0034     explicit MatcherUsingTracks(const edm::ParameterSet &iConfig);
0035     ~MatcherUsingTracks() override {}
0036 
0037     void produce(edm::Event &iEvent, const edm::EventSetup &iSetup) override;
0038 
0039   private:
0040     /// Labels for input collections
0041     edm::EDGetTokenT<edm::View<reco::Candidate> > srcToken_;
0042     edm::EDGetTokenT<edm::View<reco::Candidate> > matchedToken_;
0043 
0044     /// The real workhorse
0045     MatcherUsingTracksAlgorithm algo_;
0046 
0047     /// Some extra configurables
0048     bool dontFailOnMissingInput_;
0049     bool writeExtraPATOutput_;
0050 
0051     /// Store extra information in a ValueMap
0052     template <typename T>
0053     void storeValueMap(edm::Event &iEvent,
0054                        const edm::Handle<edm::View<reco::Candidate> > &handle,
0055                        const std::vector<T> &values,
0056                        const std::string &label) const;
0057   };
0058 
0059 }  // namespace pat
0060 
0061 pat::MatcherUsingTracks::MatcherUsingTracks(const edm::ParameterSet &iConfig)
0062     : srcToken_(consumes<edm::View<reco::Candidate> >(iConfig.getParameter<edm::InputTag>("src"))),
0063       matchedToken_(consumes<edm::View<reco::Candidate> >(iConfig.getParameter<edm::InputTag>("matched"))),
0064       algo_(iConfig, consumesCollector()),
0065       dontFailOnMissingInput_(iConfig.existsAs<bool>("dontFailOnMissingInput")
0066                                   ? iConfig.getParameter<bool>("dontFailOnMissingInput")
0067                                   : false),
0068       writeExtraPATOutput_(
0069           iConfig.existsAs<bool>("writeExtraPATOutput") ? iConfig.getParameter<bool>("writeExtraPATOutput") : false) {
0070   // this is the basic output (edm::Association is not generic)
0071   produces<edm::ValueMap<reco::CandidatePtr> >();
0072   if (writeExtraPATOutput_) {
0073     // this is the crazy stuff to get the same with UserData
0074     produces<edm::OwnVector<pat::UserData> >();
0075     produces<edm::ValueMap<edm::Ptr<pat::UserData> > >();
0076   }
0077   // this is the extra stuff
0078   if (algo_.hasMetrics()) {
0079     produces<edm::ValueMap<float> >("deltaR");
0080     produces<edm::ValueMap<float> >("deltaEta");
0081     produces<edm::ValueMap<float> >("deltaPhi");
0082     produces<edm::ValueMap<float> >("deltaLocalPos");
0083     produces<edm::ValueMap<float> >("deltaPtRel");
0084     if (algo_.hasChi2()) {
0085       produces<edm::ValueMap<float> >("chi2");
0086     }
0087   } else {
0088     produces<edm::ValueMap<int> >("matched");
0089   }
0090 }
0091 
0092 void pat::MatcherUsingTracks::produce(edm::Event &iEvent, const edm::EventSetup &iSetup) {
0093   using namespace edm;
0094   using namespace std;
0095 
0096   algo_.init(iSetup);
0097 
0098   Handle<View<reco::Candidate> > src, matched;
0099 
0100   iEvent.getByToken(srcToken_, src);
0101   iEvent.getByToken(matchedToken_, matched);
0102 
0103   // declare loop variables and some intermediate stuff
0104   View<reco::Candidate>::const_iterator itsrc, edsrc;
0105   int isrc, nsrc = src->size();
0106 
0107   // working and output variables
0108   vector<int> match(nsrc, -1);
0109   vector<float> deltaRs(nsrc, 999);
0110   vector<float> deltaEtas(nsrc, 999);
0111   vector<float> deltaPhis(nsrc, 999);
0112   vector<float> deltaPtRel(nsrc, 999);
0113   vector<float> deltaLocalPos(nsrc, 999);
0114   vector<float> chi2(nsrc, 999999);
0115 
0116   // don't try matching if the input collection is missing and the module is configured to fail silently
0117   if (!(matched.failedToGet() && dontFailOnMissingInput_)) {
0118     // loop on the source collection, and request for the match
0119     for (itsrc = src->begin(), edsrc = src->end(), isrc = 0; itsrc != edsrc; ++itsrc, ++isrc) {
0120       match[isrc] = algo_.match(*itsrc,
0121                                 *matched,
0122                                 deltaRs[isrc],
0123                                 deltaEtas[isrc],
0124                                 deltaPhis[isrc],
0125                                 deltaLocalPos[isrc],
0126                                 deltaPtRel[isrc],
0127                                 chi2[isrc]);
0128     }
0129   }
0130 
0131   std::vector<reco::CandidatePtr> ptrs(nsrc);
0132   for (isrc = 0; isrc < nsrc; ++isrc) {
0133     if (match[isrc] != -1) {
0134       ptrs[isrc] = matched->ptrAt(match[isrc]);
0135     }
0136   }
0137   storeValueMap<reco::CandidatePtr>(iEvent, src, ptrs, "");
0138 
0139   if (writeExtraPATOutput_) {
0140     auto outUDVect = std::make_unique<edm::OwnVector<pat::UserData> >();
0141     std::vector<int> idxUD(nsrc, -1);
0142     for (isrc = 0; isrc < nsrc; ++isrc) {
0143       if (match[isrc] != -1) {
0144         outUDVect->push_back(pat::UserData::make(ptrs[isrc]));
0145         idxUD[isrc] = outUDVect->size() - 1;
0146       }
0147     }
0148     edm::OrphanHandle<edm::OwnVector<pat::UserData> > doneUDVect = iEvent.put(std::move(outUDVect));
0149     std::vector<edm::Ptr<pat::UserData> > ptrUD(nsrc);
0150     for (isrc = 0; isrc < nsrc; ++isrc) {
0151       if (idxUD[isrc] != -1)
0152         ptrUD[isrc] = edm::Ptr<pat::UserData>(doneUDVect, idxUD[isrc]);
0153     }
0154     storeValueMap<edm::Ptr<pat::UserData> >(iEvent, src, ptrUD, "");
0155   }
0156 
0157   if (algo_.hasMetrics()) {
0158     storeValueMap<float>(iEvent, src, deltaRs, "deltaR");
0159     storeValueMap<float>(iEvent, src, deltaEtas, "deltaEta");
0160     storeValueMap<float>(iEvent, src, deltaPhis, "deltaPhi");
0161     storeValueMap<float>(iEvent, src, deltaLocalPos, "deltaLocalPos");
0162     storeValueMap<float>(iEvent, src, deltaPtRel, "deltaPtRel");
0163     if (algo_.hasChi2()) {
0164       storeValueMap<float>(iEvent, src, chi2, "chi2");
0165     }
0166   } else {
0167     std::vector<int> ismatched(nsrc, 0);
0168     for (isrc = 0; isrc < nsrc; ++isrc) {
0169       ismatched[isrc] = (match[isrc] != -1);
0170     }
0171     storeValueMap<int>(iEvent, src, ismatched, "matched");
0172   }
0173 }
0174 
0175 template <typename T>
0176 void pat::MatcherUsingTracks::storeValueMap(edm::Event &iEvent,
0177                                             const edm::Handle<edm::View<reco::Candidate> > &handle,
0178                                             const std::vector<T> &values,
0179                                             const std::string &label) const {
0180   using namespace edm;
0181   using namespace std;
0182   unique_ptr<ValueMap<T> > valMap(new ValueMap<T>());
0183   typename edm::ValueMap<T>::Filler filler(*valMap);
0184   filler.insert(handle, values.begin(), values.end());
0185   filler.fill();
0186   iEvent.put(std::move(valMap), label);
0187 }
0188 
0189 #include "FWCore/Framework/interface/MakerMacros.h"
0190 using namespace pat;
0191 DEFINE_FWK_MODULE(MatcherUsingTracks);