Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:28:59

0001 
0002 //
0003 //
0004 
0005 /**
0006   \class    pat::FakeTrackProducer FakeTrackProducer.h "MuonAnalysis/MuonAssociators/interface/FakeTrackProducer.h"
0007   \brief    Matcher of reconstructed objects to other reconstructed objects using the tracks inside them 
0008             
0009   \author   Giovanni Petrucciani
0010 */
0011 
0012 #include "FWCore/Framework/interface/stream/EDProducer.h"
0013 #include "FWCore/Framework/interface/Event.h"
0014 #include "FWCore/Framework/interface/EventSetup.h"
0015 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0016 #include "FWCore/Utilities/interface/InputTag.h"
0017 #include "FWCore/Utilities/interface/ESGetToken.h"
0018 
0019 #include "DataFormats/TrackReco/interface/Track.h"
0020 #include "DataFormats/TrackReco/interface/TrackExtra.h"
0021 #include "DataFormats/TrackCandidate/interface/TrackCandidate.h"
0022 #include "DataFormats/TrackingRecHit/interface/TrackingRecHit.h"
0023 #include "DataFormats/TrackerCommon/interface/TrackerTopology.h"
0024 #include "TrackingTools/TrackRefitter/interface/TrackTransformer.h"
0025 #include "TrackingTools/TrajectoryState/interface/TrajectoryStateTransform.h"
0026 #include "TrackingTools/TrajectoryState/interface/TrajectoryStateOnSurface.h"
0027 #include "CommonTools/Utils/interface/StringCutObjectSelector.h"
0028 #include "TrackingTools/TrajectoryState/interface/TrajectoryStateTransform.h"
0029 #include "TrackingTools/TrajectoryState/interface/TrajectoryStateOnSurface.h"
0030 #include "MagneticField/Engine/interface/MagneticField.h"
0031 #include "Geometry/TrackerGeometryBuilder/interface/TrackerGeometry.h"
0032 #include "FWCore/Framework/interface/ESHandle.h"
0033 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0034 #include "Geometry/Records/interface/TrackerDigiGeometryRecord.h"
0035 #include "Geometry/Records/interface/TrackerTopologyRcd.h"
0036 #include "MagneticField/Records/interface/IdealMagneticFieldRecord.h"
0037 
0038 template <class T>
0039 class FakeTrackProducer : public edm::stream::EDProducer<> {
0040 public:
0041   explicit FakeTrackProducer(const edm::ParameterSet &iConfig);
0042   ~FakeTrackProducer() override {}
0043 
0044   void produce(edm::Event &iEvent, const edm::EventSetup &iSetup) override;
0045 
0046 private:
0047   /// Labels for input collections
0048   edm::EDGetTokenT<std::vector<T>> src_;
0049   edm::ESGetToken<TrackerGeometry, TrackerDigiGeometryRecord> geometryToken_;
0050   edm::ESGetToken<MagneticField, IdealMagneticFieldRecord> magFieldToken_;
0051   edm::ESGetToken<TrackerTopology, TrackerTopologyRcd> trackerTopoToken_;
0052 
0053   /// Muon selection
0054   //StringCutObjectSelector<T> selector_;
0055 
0056   const PTrajectoryStateOnDet &getState(const TrajectorySeed &seed) const { return seed.startingState(); }
0057   const PTrajectoryStateOnDet &getState(const TrackCandidate &seed) const { return seed.trajectoryStateOnDet(); }
0058 };
0059 
0060 template <typename T>
0061 FakeTrackProducer<T>::FakeTrackProducer(const edm::ParameterSet &iConfig)
0062     : src_(consumes<std::vector<T>>(iConfig.getParameter<edm::InputTag>("src"))),
0063       geometryToken_(esConsumes<TrackerGeometry, TrackerDigiGeometryRecord>()),
0064       magFieldToken_(esConsumes<MagneticField, IdealMagneticFieldRecord>()),
0065       trackerTopoToken_(esConsumes<TrackerTopology, TrackerTopologyRcd>())
0066 //,selector_(iConfig.existsAs<std::string>("cut") ? iConfig.getParameter<std::string>("cut") : "", true)
0067 {
0068   produces<std::vector<reco::Track>>();
0069   produces<std::vector<reco::TrackExtra>>();
0070   produces<edm::OwnVector<TrackingRecHit>>();
0071 }
0072 
0073 template <typename T>
0074 void FakeTrackProducer<T>::produce(edm::Event &iEvent, const edm::EventSetup &iSetup) {
0075   using namespace edm;
0076   using namespace std;
0077 
0078   edm::ESHandle<TrackerGeometry> theGeometry = iSetup.getHandle(geometryToken_);
0079   edm::ESHandle<MagneticField> theMagField = iSetup.getHandle(magFieldToken_);
0080   edm::ESHandle<TrackerTopology> httopo = iSetup.getHandle(trackerTopoToken_);
0081   const TrackerTopology &ttopo = *httopo;
0082 
0083   Handle<vector<T>> src;
0084   iEvent.getByToken(src_, src);
0085 
0086   unique_ptr<vector<reco::Track>> out(new vector<reco::Track>());
0087   out->reserve(src->size());
0088   unique_ptr<vector<reco::TrackExtra>> outEx(new vector<reco::TrackExtra>());
0089   outEx->reserve(src->size());
0090   unique_ptr<OwnVector<TrackingRecHit>> outHits(new OwnVector<TrackingRecHit>());
0091 
0092   TrackingRecHitRefProd rHits = iEvent.getRefBeforePut<TrackingRecHitCollection>();
0093   reco::TrackExtraRefProd rTrackExtras = iEvent.getRefBeforePut<reco::TrackExtraCollection>();
0094   for (typename vector<T>::const_iterator it = src->begin(), ed = src->end(); it != ed; ++it) {
0095     const T &mu = *it;
0096     //if (!selector_(mu)) continue;
0097     const PTrajectoryStateOnDet &pstate = getState(mu);
0098     const GeomDet *det = theGeometry->idToDet(DetId(pstate.detId()));
0099     if (det == nullptr) {
0100       std::cerr << "ERROR:  bogus detid " << pstate.detId() << std::endl;
0101       continue;
0102     }
0103     TrajectoryStateOnSurface state = trajectoryStateTransform::transientState(pstate, &det->surface(), &*theMagField);
0104     GlobalPoint gx = state.globalPosition();
0105     GlobalVector gp = state.globalMomentum();
0106     reco::Track::Point x(gx.x(), gx.y(), gx.z());
0107     reco::Track::Vector p(gp.x(), gp.y(), gp.z());
0108     int charge = state.localParameters().charge();
0109     out->push_back(reco::Track(1.0, 1.0, x, p, charge, reco::Track::CovarianceMatrix()));
0110     auto hits = mu.recHits();
0111     out->back().appendHits(hits.begin(), hits.end(), ttopo);
0112     // Now Track Extra
0113     const TrackingRecHit *hit0 = &*hits.begin();
0114     const TrackingRecHit *hit1 = &*(hits.end() - 1);
0115     const GeomDet *det0 = theGeometry->idToDet(hit0->geographicalId());
0116     const GeomDet *det1 = theGeometry->idToDet(hit1->geographicalId());
0117     if (det0 == nullptr || det1 == nullptr) {
0118       std::cerr << "ERROR:  bogus detids at beginning or end of range" << std::endl;
0119       continue;
0120     }
0121     GlobalPoint gx0 = det0->toGlobal(hit0->localPosition());
0122     GlobalPoint gx1 = det1->toGlobal(hit1->localPosition());
0123     reco::Track::Point x0(gx0.x(), gx0.y(), gx0.z());
0124     reco::Track::Point x1(gx1.x(), gx1.y(), gx1.z());
0125     if (x0.R() > x1.R())
0126       std::swap(x0, x1);
0127     outEx->push_back(reco::TrackExtra(x1,
0128                                       p,
0129                                       true,
0130                                       x0,
0131                                       p,
0132                                       true,
0133                                       reco::Track::CovarianceMatrix(),
0134                                       hit0->geographicalId().rawId(),
0135                                       reco::Track::CovarianceMatrix(),
0136                                       hit1->geographicalId().rawId(),
0137                                       alongMomentum));
0138     out->back().setExtra(reco::TrackExtraRef(rTrackExtras, outEx->size() - 1));
0139     reco::TrackExtra &ex = outEx->back();
0140     auto const firstHitIndex = outHits->size();
0141     for (auto const &it2 : hits) {
0142       outHits->push_back(it2);
0143     }
0144     ex.setHits(rHits, firstHitIndex, outHits->size() - firstHitIndex);
0145   }
0146 
0147   iEvent.put(std::move(out));
0148   iEvent.put(std::move(outEx));
0149   iEvent.put(std::move(outHits));
0150 }
0151 
0152 typedef FakeTrackProducer<TrajectorySeed> FakeTrackProducerFromSeed;
0153 typedef FakeTrackProducer<TrackCandidate> FakeTrackProducerFromCandidate;
0154 #include "FWCore/Framework/interface/MakerMacros.h"
0155 DEFINE_FWK_MODULE(FakeTrackProducerFromSeed);
0156 DEFINE_FWK_MODULE(FakeTrackProducerFromCandidate);