Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:23:48

0001 #include "FWCore/Framework/interface/stream/EDProducer.h"
0002 #include "FWCore/Framework/interface/Event.h"
0003 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0004 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0005 #include "FWCore/Utilities/interface/InputTag.h"
0006 #include "DataFormats/Common/interface/ValueMap.h"
0007 #include "DataFormats/PatCandidates/interface/Muon.h"
0008 #include "DataFormats/TrackReco/interface/Track.h"
0009 #include "DataFormats/PatCandidates/interface/PackedCandidate.h"
0010 #include "DataFormats/Math/interface/libminifloat.h"
0011 
0012 namespace pat {
0013 
0014   class PackedCandidateTrackChi2Producer : public edm::stream::EDProducer<> {
0015     typedef edm::ValueMap<float> FloatMap;
0016 
0017   public:
0018     explicit PackedCandidateTrackChi2Producer(const edm::ParameterSet& iConfig)
0019         : candidateToken_(consumes<pat::PackedCandidateCollection>(iConfig.getParameter<edm::InputTag>("candidates"))),
0020           trackToken_(consumes<reco::TrackCollection>(iConfig.getParameter<edm::InputTag>("trackCollection"))),
0021           doLostTracks_(iConfig.getParameter<bool>("doLostTracks")) {
0022       if (doLostTracks_) {
0023         track2LostTrackToken_ = consumes<edm::Association<pat::PackedCandidateCollection>>(
0024             iConfig.getParameter<edm::InputTag>("candidates"));
0025       } else {
0026         candidate2PFToken_ =
0027             consumes<edm::Association<reco::PFCandidateCollection>>(iConfig.getParameter<edm::InputTag>("candidates"));
0028       }
0029 
0030       produces<FloatMap>();
0031     }
0032 
0033     void produce(edm::Event&, const edm::EventSetup&) override;
0034 
0035     static void fillDescriptions(edm::ConfigurationDescriptions&);
0036 
0037   private:
0038     const edm::EDGetTokenT<pat::PackedCandidateCollection> candidateToken_;
0039     edm::EDGetTokenT<edm::Association<reco::PFCandidateCollection>> candidate2PFToken_;
0040     edm::EDGetTokenT<edm::Association<pat::PackedCandidateCollection>> track2LostTrackToken_;
0041     const edm::EDGetTokenT<reco::TrackCollection> trackToken_;
0042     const bool doLostTracks_;
0043     static const uint8_t roundingPrecision = 8;
0044   };
0045 
0046 }  // namespace pat
0047 
0048 void pat::PackedCandidateTrackChi2Producer::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) {
0049   auto const candidates = iEvent.getHandle(candidateToken_);
0050 
0051   const edm::Association<reco::PFCandidateCollection>* candidate2PF = nullptr;
0052   if (!doLostTracks_) {
0053     candidate2PF = &iEvent.get(candidate2PFToken_);
0054   }
0055 
0056   const edm::Association<pat::PackedCandidateCollection>* tracks2LT = nullptr;
0057   edm::Handle<reco::TrackCollection> trks;
0058   if (doLostTracks_) {
0059     tracks2LT = &iEvent.get(track2LostTrackToken_);
0060     iEvent.getByToken(trackToken_, trks);
0061   }
0062 
0063   const auto nCand = candidates->size();
0064   std::vector<float> trkChi2Map(nCand, 0);
0065 
0066   if (doLostTracks_) {  //for Lost tracks we don't have references to PFCands, so we must loop over tracks and check keys...
0067     for (size_t i = 0; i < trks->size(); i++) {
0068       const auto& trk = reco::TrackRef(trks, i);
0069       const auto& lostTrack = (*tracks2LT)[trk];
0070       if (lostTrack.isNonnull()) {
0071         const float nChi2 = trk->normalizedChi2();
0072         trkChi2Map.at(lostTrack.key()) = MiniFloatConverter::reduceMantissaToNbitsRounding<roundingPrecision>(nChi2);
0073       }
0074     }
0075   } else {  //for the regular PackedPFCands we have direct references...
0076     for (size_t i = 0; i < nCand; i++) {
0077       const auto& cand = pat::PackedCandidateRef(candidates, i);
0078 
0079       // ignore neutral candidates or without track
0080       if (cand->charge() == 0 || !cand->hasTrackDetails())
0081         continue;
0082 
0083       const auto& candTrack = (*candidate2PF)[cand]->trackRef();
0084 
0085       if (candTrack.isNonnull()) {
0086         const float nChi2 = candTrack->normalizedChi2();
0087 
0088         trkChi2Map.at(i) = MiniFloatConverter::reduceMantissaToNbitsRounding<roundingPrecision>(nChi2);
0089       }
0090     }
0091   }
0092 
0093   // fill the value maps
0094   std::unique_ptr<FloatMap> valueMap = std::make_unique<FloatMap>();
0095   FloatMap::Filler filler(*valueMap);
0096   filler.insert(candidates, trkChi2Map.begin(), trkChi2Map.end());
0097   filler.fill();
0098   iEvent.put(std::move(valueMap), "");
0099 }
0100 
0101 // ------------ method fills 'descriptions' with the allowed parameters for the module  ------------
0102 void pat::PackedCandidateTrackChi2Producer::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0103   edm::ParameterSetDescription desc;
0104   desc.add<edm::InputTag>("candidates", edm::InputTag("packedPFCandidates"))
0105       ->setComment("packed candidate input collection");
0106   desc.add<edm::InputTag>("trackCollection", edm::InputTag("generalTracks"))->setComment("track input collection");
0107   desc.add<bool>("doLostTracks", false);
0108   descriptions.add("packedPFCandidateTrackChi2", desc);
0109 }
0110 
0111 #include "FWCore/Framework/interface/MakerMacros.h"
0112 using namespace pat;
0113 DEFINE_FWK_MODULE(PackedCandidateTrackChi2Producer);