File indexing completed on 2024-04-06 11:56:12
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021 #include <memory>
0022
0023
0024 #include "FWCore/Framework/interface/Frameworkfwd.h"
0025 #include "FWCore/Framework/interface/stream/EDProducer.h"
0026 #include "FWCore/Framework/interface/Event.h"
0027 #include "FWCore/Framework/interface/EventSetup.h"
0028 #include "FWCore/Framework/interface/MakerMacros.h"
0029 #include "FWCore/Framework/interface/ConsumesCollector.h"
0030 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0031
0032
0033 #include "TrackingTools/PatternTools/interface/Trajectory.h"
0034 #include "TrackingTools/PatternTools/interface/TrajTrackAssociation.h"
0035
0036
0037 #include "DataFormats/MuonReco/interface/Muon.h"
0038 #include "DataFormats/MuonReco/interface/MuonFwd.h"
0039 #include "DataFormats/TrackReco/interface/Track.h"
0040 #include "DataFormats/TrackReco/interface/TrackFwd.h"
0041 #include "TrackingTools/GeomPropagators/interface/Propagator.h"
0042 #include "Geometry/TrackerGeometryBuilder/interface/TrackerGeometry.h"
0043 #include "Geometry/DTGeometry/interface/DTGeometry.h"
0044 #include "Geometry/CSCGeometry/interface/CSCGeometry.h"
0045 #include "DataFormats/MuonDetId/interface/MuonSubdetId.h"
0046 #include "TrackingTools/Records/interface/TrackingComponentsRecord.h"
0047 #include "Geometry/Records/interface/TrackerDigiGeometryRecord.h"
0048 #include "Geometry/Records/interface/MuonGeometryRecord.h"
0049 #include "MagneticField/Engine/interface/MagneticField.h"
0050 #include "MagneticField/Records/interface/IdealMagneticFieldRecord.h"
0051 #include "DataFormats/TrackingRecHit/interface/TrackingRecHitFwd.h"
0052 #include "DataFormats/TrackingRecHit/interface/TrackingRecHit.h"
0053 #include "RecoMuon/TransientTrackingRecHit/interface/MuonTransientTrackingRecHitBuilder.h"
0054 #include "TrackingTools/TrajectoryState/interface/TrajectoryStateTransform.h"
0055 #include "Geometry/CommonDetUnit/interface/GlobalTrackingGeometry.h"
0056 #include "Geometry/Records/interface/GlobalTrackingGeometryRecord.h"
0057 #include "TrackingTools/TrackRefitter/interface/TrackTransformer.h"
0058
0059
0060
0061
0062
0063 class TrackerToMuonPropagator : public edm::stream::EDProducer<> {
0064 public:
0065 explicit TrackerToMuonPropagator(const edm::ParameterSet&);
0066 ~TrackerToMuonPropagator() override;
0067
0068 private:
0069 void produce(edm::Event&, const edm::EventSetup&) override;
0070
0071
0072
0073
0074 const edm::ESGetToken<Propagator, TrackingComponentsRecord> m_esTokenProp;
0075 const edm::ESGetToken<TrackerGeometry, TrackerDigiGeometryRecord> m_esTokenTk;
0076 const edm::ESGetToken<DTGeometry, MuonGeometryRecord> m_esTokenDT;
0077 const edm::ESGetToken<CSCGeometry, MuonGeometryRecord> m_esTokenCSC;
0078 const edm::ESGetToken<MagneticField, IdealMagneticFieldRecord> m_esTokenMF;
0079 const edm::ESGetToken<GlobalTrackingGeometry, GlobalTrackingGeometryRecord> m_esTokenGTGeo;
0080
0081 const edm::InputTag m_globalMuons, m_globalMuonTracks;
0082 const bool m_refitTracker;
0083
0084 const edm::EDGetTokenT<reco::MuonCollection> muonToken_;
0085 const edm::EDGetTokenT<reco::TrackCollection> trackToken_;
0086
0087 TrackTransformer* m_trackTransformer;
0088 };
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101 TrackerToMuonPropagator::TrackerToMuonPropagator(const edm::ParameterSet& iConfig)
0102 : m_esTokenProp(esConsumes(edm::ESInputTag("", iConfig.getParameter<std::string>("propagator")))),
0103 m_esTokenTk(esConsumes()),
0104 m_esTokenDT(esConsumes()),
0105 m_esTokenCSC(esConsumes()),
0106 m_esTokenMF(esConsumes()),
0107 m_esTokenGTGeo(esConsumes()),
0108 m_globalMuons(iConfig.getParameter<edm::InputTag>("globalMuons")),
0109 m_globalMuonTracks(iConfig.getParameter<edm::InputTag>("globalMuonTracks")),
0110 m_refitTracker(iConfig.getParameter<bool>("refitTrackerTrack")),
0111 muonToken_(consumes<reco::MuonCollection>(m_globalMuons)),
0112 trackToken_(consumes<reco::TrackCollection>(m_globalMuonTracks)) {
0113 if (m_refitTracker)
0114 m_trackTransformer =
0115 new TrackTransformer(iConfig.getParameter<edm::ParameterSet>("trackerTrackTransformer"), consumesCollector());
0116 else
0117 m_trackTransformer = nullptr;
0118
0119 produces<std::vector<Trajectory>>();
0120 produces<TrajTrackAssociationCollection>();
0121 }
0122
0123 TrackerToMuonPropagator::~TrackerToMuonPropagator() {
0124
0125
0126 }
0127
0128
0129
0130
0131
0132
0133 void TrackerToMuonPropagator::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) {
0134 if (m_trackTransformer)
0135 m_trackTransformer->setServices(iSetup);
0136
0137 const edm::Handle<reco::MuonCollection>& globalMuons = iEvent.getHandle(muonToken_);
0138 const edm::Handle<reco::TrackCollection>& globalMuonTracks = iEvent.getHandle(trackToken_);
0139
0140 const Propagator* propagator = &iSetup.getData(m_esTokenProp);
0141 const TrackerGeometry* trackerGeometry = &iSetup.getData(m_esTokenTk);
0142 const DTGeometry* dtGeometry = &iSetup.getData(m_esTokenDT);
0143 const CSCGeometry* cscGeometry = &iSetup.getData(m_esTokenCSC);
0144 const MagneticField* magneticField = &iSetup.getData(m_esTokenMF);
0145 const GlobalTrackingGeometry* globalGeometry = &iSetup.getData(m_esTokenGTGeo);
0146
0147
0148
0149 MuonTransientTrackingRecHitBuilder muonTransBuilder;
0150
0151
0152 auto trajectoryCollection = std::make_unique<std::vector<Trajectory>>();
0153
0154
0155 std::map<edm::Ref<std::vector<Trajectory>>::key_type, edm::Ref<reco::TrackCollection>::key_type> reference_map;
0156 edm::Ref<std::vector<Trajectory>>::key_type trajCounter = 0;
0157
0158 for (reco::MuonCollection::const_iterator globalMuon = globalMuons->begin(); globalMuon != globalMuons->end();
0159 ++globalMuon) {
0160
0161 edm::Ref<reco::TrackCollection>::key_type trackCounter = 0;
0162 reco::TrackCollection::const_iterator globalMuonTrack = globalMuonTracks->begin();
0163 for (; globalMuonTrack != globalMuonTracks->end(); ++globalMuonTrack) {
0164 trackCounter++;
0165 if (fabs(globalMuon->combinedMuon()->phi() - globalMuonTrack->phi()) < 1e-10 &&
0166 fabs(globalMuon->combinedMuon()->eta() - globalMuonTrack->eta()) < 1e-10)
0167 break;
0168 }
0169 if (globalMuonTrack == globalMuonTracks->end()) {
0170 throw cms::Exception("BadConfig") << "The tracks label doesn't correspond to the same objects as the muons label"
0171 << std::endl;
0172 }
0173
0174 TrajectoryStateOnSurface tracker_tsos;
0175 DetId outerDetId;
0176 if (m_refitTracker) {
0177 std::vector<Trajectory> trackerTrajectories = m_trackTransformer->transform(*globalMuon->track());
0178 if (trackerTrajectories.size() == 1) {
0179 const Trajectory trackerTrajectory = *(trackerTrajectories.begin());
0180
0181
0182 tracker_tsos = trackerTrajectory.firstMeasurement().forwardPredictedState();
0183 outerDetId = trackerTrajectory.firstMeasurement().recHit()->geographicalId();
0184 } else
0185 continue;
0186 } else {
0187
0188 GlobalPoint outerPosition(globalMuon->track()->outerPosition().x(),
0189 globalMuon->track()->outerPosition().y(),
0190 globalMuon->track()->outerPosition().z());
0191 GlobalVector outerMomentum(globalMuon->track()->outerMomentum().x(),
0192 globalMuon->track()->outerMomentum().y(),
0193 globalMuon->track()->outerMomentum().z());
0194 int charge = globalMuon->track()->charge();
0195 const reco::Track::CovarianceMatrix outerStateCovariance = globalMuon->track()->outerStateCovariance();
0196 outerDetId = DetId(globalMuon->track()->outerDetId());
0197
0198
0199 GlobalTrajectoryParameters globalTrajParams(outerPosition, outerMomentum, charge, magneticField);
0200 CurvilinearTrajectoryError curviError(outerStateCovariance);
0201 FreeTrajectoryState tracker_state(globalTrajParams, curviError);
0202
0203
0204 tracker_tsos =
0205 TrajectoryStateOnSurface(globalTrajParams, curviError, trackerGeometry->idToDet(outerDetId)->surface());
0206 }
0207
0208 TrajectoryStateOnSurface last_tsos = tracker_tsos;
0209
0210
0211 edm::OwnVector<TrackingRecHit> muonHits;
0212 std::vector<TrajectoryStateOnSurface> TSOSes;
0213 for (auto const& hit : globalMuon->combinedMuon()->recHits()) {
0214 DetId id = hit->geographicalId();
0215
0216 TrajectoryStateOnSurface extrapolation;
0217 bool extrapolated = false;
0218 if (id.det() == DetId::Muon && id.subdetId() == MuonSubdetId::DT) {
0219 extrapolation = propagator->propagate(last_tsos, dtGeometry->idToDet(id)->surface());
0220 extrapolated = true;
0221 } else if (id.det() == DetId::Muon && id.subdetId() == MuonSubdetId::CSC) {
0222 extrapolation = propagator->propagate(last_tsos, cscGeometry->idToDet(id)->surface());
0223 extrapolated = true;
0224 }
0225
0226 if (extrapolated && extrapolation.isValid()) {
0227 muonHits.push_back(hit->clone());
0228 TSOSes.push_back(extrapolation);
0229 }
0230 }
0231
0232
0233 if (!muonHits.empty()) {
0234 PTrajectoryStateOnDet const& PTraj = trajectoryStateTransform::persistentState(tracker_tsos, outerDetId.rawId());
0235 TrajectorySeed trajectorySeed(PTraj, muonHits, alongMomentum);
0236 Trajectory trajectory(trajectorySeed, alongMomentum);
0237
0238 for (unsigned int i = 0; i < muonHits.size(); i++) {
0239 TrajectoryMeasurement::ConstRecHitPointer hitPtr(muonTransBuilder.build(&(muonHits[i]), globalGeometry));
0240 TrajectoryStateOnSurface TSOS = TSOSes[i];
0241 trajectory.push(TrajectoryMeasurement(TSOS, TSOS, TSOS, hitPtr));
0242 }
0243
0244 trajectoryCollection->push_back(trajectory);
0245
0246
0247 trajCounter++;
0248 reference_map[trajCounter] = trackCounter;
0249
0250 }
0251
0252 }
0253
0254 unsigned int numTrajectories = trajectoryCollection->size();
0255
0256
0257 edm::OrphanHandle<std::vector<Trajectory>> ohTrajs = iEvent.put(std::move(trajectoryCollection));
0258
0259
0260 auto trajTrackMap = std::make_unique<TrajTrackAssociationCollection>();
0261
0262 for (trajCounter = 0; trajCounter < numTrajectories; trajCounter++) {
0263 edm::Ref<reco::TrackCollection>::key_type trackCounter = reference_map[trajCounter];
0264
0265 trajTrackMap->insert(edm::Ref<std::vector<Trajectory>>(ohTrajs, trajCounter),
0266 edm::Ref<reco::TrackCollection>(globalMuonTracks, trackCounter));
0267 }
0268
0269 iEvent.put(std::move(trajTrackMap));
0270 }
0271
0272
0273 DEFINE_FWK_MODULE(TrackerToMuonPropagator);