File indexing completed on 2025-01-18 03:42:13
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #include "DataFormats/CaloRecHit/interface/CaloCluster.h"
0011 #include "DataFormats/EgammaTrackReco/interface/TrackCaloClusterAssociation.h"
0012 #include "DataFormats/EgammaTrackReco/interface/TrackCandidateCaloClusterAssociation.h"
0013 #include "DataFormats/TrackReco/interface/Track.h"
0014 #include "DataFormats/TrackReco/interface/TrackFwd.h"
0015 #include "FWCore/Framework/interface/stream/EDProducer.h"
0016 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0017 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0018 #include "Geometry/TrackerGeometryBuilder/interface/TrackerGeometry.h"
0019 #include "RecoTracker/TrackProducer/interface/TrackProducerAlgorithm.h"
0020 #include "RecoTracker/TrackProducer/interface/TrackProducerBase.h"
0021 #include "TrackingTools/GeomPropagators/interface/Propagator.h"
0022 #include "TrackingTools/PatternTools/interface/TrajTrackAssociation.h"
0023 #include "TrackingTools/PatternTools/interface/Trajectory.h"
0024 #include "TrackingTools/TrackFitters/interface/TrajectoryFitter.h"
0025 #include "TrackingTools/TrajectoryState/interface/TrajectoryStateTransform.h"
0026 #include "TrackingTools/TransientTrack/interface/TransientTrack.h"
0027 #include "TrackingTools/TransientTrackingRecHit/interface/TransientTrackingRecHitBuilder.h"
0028
0029 class TrackProducerWithSCAssociation : public TrackProducerBase<reco::Track>, public edm::stream::EDProducer<> {
0030 public:
0031 explicit TrackProducerWithSCAssociation(const edm::ParameterSet& iConfig);
0032
0033 void produce(edm::Event&, const edm::EventSetup&) override;
0034
0035 std::vector<reco::TransientTrack> getTransient(edm::Event&, const edm::EventSetup&);
0036
0037
0038 static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0039
0040 private:
0041 std::string myname_;
0042 TrackProducerAlgorithm<reco::Track> theAlgo;
0043 std::string conversionTrackCandidateProducer_;
0044 std::string trackCSuperClusterAssociationCollection_;
0045 std::string trackSuperClusterAssociationCollection_;
0046 edm::EDGetTokenT<reco::TrackCandidateCaloClusterPtrAssociation> assoc_token;
0047 edm::OrphanHandle<reco::TrackCollection> rTracks_;
0048 edm::EDGetTokenT<MeasurementTrackerEvent> measurementTrkToken_;
0049 const edm::ESGetToken<TrackerTopology, TrackerTopologyRcd> ttopoToken_;
0050 bool myTrajectoryInEvent_;
0051 bool validTrackCandidateSCAssociationInput_;
0052
0053
0054
0055 void putInEvt(edm::Event& evt,
0056 const Propagator* thePropagator,
0057 const MeasurementTracker* theMeasTk,
0058 std::unique_ptr<TrackingRecHitCollection> selHits,
0059 std::unique_ptr<reco::TrackCollection> selTracks,
0060 std::unique_ptr<reco::TrackExtraCollection> selTrackExtras,
0061 std::unique_ptr<std::vector<Trajectory>> selTrajectories,
0062 AlgoProductCollection& algoResults,
0063 TransientTrackingRecHitBuilder const* hitBuilder,
0064 const TrackerTopology* ttopo);
0065 };
0066
0067 #include "FWCore/Framework/interface/MakerMacros.h"
0068 DEFINE_FWK_MODULE(TrackProducerWithSCAssociation);
0069
0070 TrackProducerWithSCAssociation::TrackProducerWithSCAssociation(const edm::ParameterSet& iConfig)
0071 : TrackProducerBase<reco::Track>(iConfig.getParameter<bool>("TrajectoryInEvent")),
0072 theAlgo(iConfig),
0073 ttopoToken_(esConsumes()) {
0074 initTrackProducerBase(
0075 iConfig, consumesCollector(), consumes<TrackCandidateCollection>(iConfig.getParameter<edm::InputTag>("src")));
0076 setAlias(iConfig.getParameter<std::string>("@module_label"));
0077
0078 myname_ = iConfig.getParameter<std::string>("ComponentName");
0079 conversionTrackCandidateProducer_ = iConfig.getParameter<std::string>("producer");
0080 trackCSuperClusterAssociationCollection_ = iConfig.getParameter<std::string>("trackCandidateSCAssociationCollection");
0081 trackSuperClusterAssociationCollection_ = iConfig.getParameter<std::string>("recoTrackSCAssociationCollection");
0082 myTrajectoryInEvent_ = iConfig.getParameter<bool>("TrajectoryInEvent");
0083
0084 assoc_token = consumes<reco::TrackCandidateCaloClusterPtrAssociation>(
0085 edm::InputTag(conversionTrackCandidateProducer_, trackCSuperClusterAssociationCollection_));
0086 measurementTrkToken_ = consumes<MeasurementTrackerEvent>(
0087 edm::InputTag("MeasurementTrackerEvent"));
0088
0089
0090 produces<reco::TrackCollection>().setBranchAlias(alias_ + "Tracks");
0091 produces<reco::TrackExtraCollection>().setBranchAlias(alias_ + "TrackExtras");
0092 produces<TrackingRecHitCollection>().setBranchAlias(alias_ + "RecHits");
0093 produces<std::vector<Trajectory>>();
0094 produces<TrajTrackAssociationCollection>();
0095
0096 produces<reco::TrackCaloClusterPtrAssociation>(trackSuperClusterAssociationCollection_);
0097 }
0098
0099 void TrackProducerWithSCAssociation::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0100 edm::ParameterSetDescription desc;
0101 desc.add<bool>("TrajectoryInEvent", false);
0102 desc.add<edm::InputTag>("src", edm::InputTag("conversionTrackCandidates", "inOutTracksFromConversions"));
0103 desc.add<std::string>("ComponentName", "ckfInOutTracksFromConversions");
0104 desc.add<std::string>("producer", "conversionTrackCandidates");
0105 desc.add<std::string>("trackCandidateSCAssociationCollection", "inOutTrackCandidateSCAssociationCollection");
0106 desc.add<std::string>("recoTrackSCAssociationCollection", "inOutTrackSCAssociationCollection");
0107 TrackProducerAlgorithm<reco::Track>::fillPSetDescription(desc);
0108 TrackProducerBase::fillPSetDescription(desc);
0109 descriptions.addWithDefaultLabel(desc);
0110 }
0111
0112 void TrackProducerWithSCAssociation::produce(edm::Event& theEvent, const edm::EventSetup& setup) {
0113
0114
0115
0116
0117
0118
0119
0120
0121 auto outputRHColl = std::make_unique<TrackingRecHitCollection>();
0122 auto outputTColl = std::make_unique<reco::TrackCollection>();
0123 auto outputTEColl = std::make_unique<reco::TrackExtraCollection>();
0124 auto outputTrajectoryColl = std::make_unique<std::vector<Trajectory>>();
0125
0126 auto scTrkAssoc_p = std::make_unique<reco::TrackCaloClusterPtrAssociation>();
0127
0128
0129
0130
0131 edm::ESHandle<TrackerGeometry> theG;
0132 edm::ESHandle<MagneticField> theMF;
0133 edm::ESHandle<TrajectoryFitter> theFitter;
0134 edm::ESHandle<Propagator> thePropagator;
0135 edm::ESHandle<TransientTrackingRecHitBuilder> theBuilder;
0136 edm::ESHandle<MeasurementTracker> theMeasTk;
0137 getFromES(setup, theG, theMF, theFitter, thePropagator, theMeasTk, theBuilder);
0138
0139 const TrackerTopology* ttopo = &setup.getData(ttopoToken_);
0140
0141
0142
0143 edm::Handle<TrackCandidateCollection> theTCCollection;
0144
0145 validTrackCandidateSCAssociationInput_ = true;
0146 edm::Handle<reco::TrackCandidateCaloClusterPtrAssociation> trkCandidateSCAssocHandle;
0147 theEvent.getByToken(assoc_token, trkCandidateSCAssocHandle);
0148 if (!trkCandidateSCAssocHandle.isValid()) {
0149
0150 edm::LogError("TrackProducerWithSCAssociation")
0151 << "Error! Can't get the product " << trackCSuperClusterAssociationCollection_.c_str()
0152 << " but keep running. Empty collection will be produced "
0153 << "\n";
0154 validTrackCandidateSCAssociationInput_ = false;
0155 }
0156 reco::TrackCandidateCaloClusterPtrAssociation scTrkCandAssCollection = *(trkCandidateSCAssocHandle.product());
0157 if (scTrkCandAssCollection.empty())
0158 validTrackCandidateSCAssociationInput_ = false;
0159
0160 std::vector<int> tccLocations;
0161 AlgoProductCollection algoResults;
0162 reco::BeamSpot bs;
0163
0164 getFromEvt(theEvent, theTCCollection, bs);
0165
0166 if (theTCCollection.failedToGet()) {
0167 edm::LogError("TrackProducerWithSCAssociation")
0168 << "TrackProducerWithSCAssociation could not get the TrackCandidateCollection.";
0169 } else {
0170
0171
0172
0173
0174
0175
0176
0177
0178
0179
0180
0181 try {
0182 int cont = 0;
0183 int tcc = 0;
0184
0185 for (TrackCandidateCollection::const_iterator i = theTCCollection->begin(); i != theTCCollection->end(); i++) {
0186 const TrackCandidate* theTC = &(*i);
0187 PTrajectoryStateOnDet state = theTC->trajectoryStateOnDet();
0188 const TrajectorySeed& seed = theTC->seed();
0189
0190
0191
0192 DetId detId(state.detId());
0193 TrajectoryStateOnSurface theTSOS = trajectoryStateTransform::transientState(
0194 state, &(theG.product()->idToDet(detId)->surface()), theMF.product());
0195
0196
0197
0198
0199
0200 TransientTrackingRecHit::RecHitContainer hits;
0201
0202 float ndof = 0;
0203
0204 for (auto const& recHit : theTC->recHits()) {
0205 hits.push_back(theBuilder.product()->build(&recHit));
0206 }
0207
0208
0209
0210 FitterCloner fc(theFitter.product(), theBuilder.product());
0211 bool ok = theAlgo.buildTrack(
0212 fc.fitter.get(), thePropagator.product(), algoResults, hits, theTSOS, seed, ndof, bs, theTC->seedRef());
0213
0214 if (ok) {
0215 cont++;
0216 tccLocations.push_back(tcc);
0217 }
0218 tcc++;
0219 }
0220 edm::LogInfo("TrackProducerWithSCAssociation") << "Number of Tracks found: " << cont << "\n";
0221
0222
0223
0224 } catch (cms::Exception& e) {
0225 edm::LogInfo("TrackProducerWithSCAssociation") << "cms::Exception caught!!!"
0226 << "\n"
0227 << e << "\n";
0228 }
0229
0230
0231
0232 putInEvt(theEvent,
0233 thePropagator.product(),
0234 theMeasTk.product(),
0235 std::move(outputRHColl),
0236 std::move(outputTColl),
0237 std::move(outputTEColl),
0238 std::move(outputTrajectoryColl),
0239 algoResults,
0240 theBuilder.product(),
0241 ttopo);
0242
0243
0244 if (validTrackCandidateSCAssociationInput_) {
0245 int itrack = 0;
0246 std::vector<edm::Ptr<reco::CaloCluster>> caloPtrVec;
0247 for (AlgoProductCollection::iterator i = algoResults.begin(); i != algoResults.end(); i++) {
0248 edm::Ref<TrackCandidateCollection> trackCRef(theTCCollection, tccLocations[itrack]);
0249 const edm::Ptr<reco::CaloCluster>& aClus = (*trkCandidateSCAssocHandle)[trackCRef];
0250 caloPtrVec.push_back(aClus);
0251 itrack++;
0252 }
0253
0254 edm::ValueMap<reco::CaloClusterPtr>::Filler filler(*scTrkAssoc_p);
0255 filler.insert(rTracks_, caloPtrVec.begin(), caloPtrVec.end());
0256 filler.fill();
0257 }
0258
0259 theEvent.put(std::move(scTrkAssoc_p), trackSuperClusterAssociationCollection_);
0260 }
0261 }
0262
0263 std::vector<reco::TransientTrack> TrackProducerWithSCAssociation::getTransient(edm::Event& theEvent,
0264 const edm::EventSetup& setup) {
0265 edm::LogInfo("TrackProducerWithSCAssociation") << "Analyzing event number: " << theEvent.id() << "\n";
0266
0267
0268
0269 std::vector<reco::TransientTrack> ttks;
0270
0271
0272
0273
0274 edm::ESHandle<TrackerGeometry> theG;
0275 edm::ESHandle<MagneticField> theMF;
0276 edm::ESHandle<TrajectoryFitter> theFitter;
0277 edm::ESHandle<Propagator> thePropagator;
0278 edm::ESHandle<TransientTrackingRecHitBuilder> theBuilder;
0279 edm::ESHandle<MeasurementTracker> theMeasTk;
0280 getFromES(setup, theG, theMF, theFitter, thePropagator, theMeasTk, theBuilder);
0281
0282
0283
0284
0285 AlgoProductCollection algoResults;
0286 reco::BeamSpot bs;
0287
0288 try {
0289 edm::Handle<TrackCandidateCollection> theTCCollection;
0290 getFromEvt(theEvent, theTCCollection, bs);
0291
0292
0293
0294
0295
0296 theAlgo.runWithCandidate(theG.product(),
0297 theMF.product(),
0298 *theTCCollection,
0299 theFitter.product(),
0300 thePropagator.product(),
0301 theBuilder.product(),
0302 bs,
0303 algoResults);
0304
0305 } catch (cms::Exception& e) {
0306 edm::LogInfo("TrackProducerWithSCAssociation") << "cms::Exception caught!!!"
0307 << "\n"
0308 << e << "\n";
0309 }
0310
0311 for (auto& prod : algoResults) {
0312 ttks.emplace_back(*prod.track, thePropagator.product()->magneticField());
0313 }
0314
0315
0316
0317 return ttks;
0318 }
0319
0320 #include "RecoTracker/TransientTrackingRecHit/interface/Traj2TrackHits.h"
0321
0322 void TrackProducerWithSCAssociation::putInEvt(edm::Event& evt,
0323 const Propagator* thePropagator,
0324 const MeasurementTracker* theMeasTk,
0325 std::unique_ptr<TrackingRecHitCollection> selHits,
0326 std::unique_ptr<reco::TrackCollection> selTracks,
0327 std::unique_ptr<reco::TrackExtraCollection> selTrackExtras,
0328 std::unique_ptr<std::vector<Trajectory>> selTrajectories,
0329 AlgoProductCollection& algoResults,
0330 TransientTrackingRecHitBuilder const* hitBuilder,
0331 const TrackerTopology* ttopo) {
0332 TrackingRecHitRefProd rHits = evt.getRefBeforePut<TrackingRecHitCollection>();
0333 reco::TrackExtraRefProd rTrackExtras = evt.getRefBeforePut<reco::TrackExtraCollection>();
0334
0335 edm::Ref<reco::TrackExtraCollection>::key_type idx = 0;
0336 edm::Ref<reco::TrackCollection>::key_type iTkRef = 0;
0337 edm::Ref<std::vector<Trajectory>>::key_type iTjRef = 0;
0338 std::map<unsigned int, unsigned int> tjTkMap;
0339
0340 for (auto& i : algoResults) {
0341 Trajectory* theTraj = i.trajectory;
0342 if (myTrajectoryInEvent_) {
0343 selTrajectories->push_back(*theTraj);
0344 iTjRef++;
0345 }
0346
0347 reco::Track* theTrack = i.track;
0348 PropagationDirection seedDir = i.pDir;
0349
0350
0351
0352 reco::Track t = *theTrack;
0353 selTracks->push_back(t);
0354 iTkRef++;
0355
0356
0357 if (trajectoryInEvent_)
0358 tjTkMap[iTjRef - 1] = iTkRef - 1;
0359
0360
0361
0362 TrajectoryStateOnSurface outertsos;
0363 TrajectoryStateOnSurface innertsos;
0364 unsigned int innerId, outerId;
0365
0366
0367
0368 if (theTraj->direction() == alongMomentum) {
0369 outertsos = theTraj->lastMeasurement().updatedState();
0370 innertsos = theTraj->firstMeasurement().updatedState();
0371 outerId = theTraj->lastMeasurement().recHit()->geographicalId().rawId();
0372 innerId = theTraj->firstMeasurement().recHit()->geographicalId().rawId();
0373 } else {
0374 outertsos = theTraj->firstMeasurement().updatedState();
0375 innertsos = theTraj->lastMeasurement().updatedState();
0376 outerId = theTraj->firstMeasurement().recHit()->geographicalId().rawId();
0377 innerId = theTraj->lastMeasurement().recHit()->geographicalId().rawId();
0378 }
0379
0380
0381 GlobalPoint v = outertsos.globalParameters().position();
0382 GlobalVector p = outertsos.globalParameters().momentum();
0383 math::XYZVector outmom(p.x(), p.y(), p.z());
0384 math::XYZPoint outpos(v.x(), v.y(), v.z());
0385 v = innertsos.globalParameters().position();
0386 p = innertsos.globalParameters().momentum();
0387 math::XYZVector inmom(p.x(), p.y(), p.z());
0388 math::XYZPoint inpos(v.x(), v.y(), v.z());
0389
0390 reco::TrackExtraRef teref = reco::TrackExtraRef(rTrackExtras, idx++);
0391 reco::Track& track = selTracks->back();
0392 track.setExtra(teref);
0393
0394
0395 if (theSchool.isValid()) {
0396 edm::Handle<MeasurementTrackerEvent> mte;
0397 evt.getByToken(measurementTrkToken_, mte);
0398 setSecondHitPattern(theTraj, track, thePropagator, &*mte, ttopo);
0399 }
0400
0401
0402 selTrackExtras->push_back(reco::TrackExtra(outpos,
0403 outmom,
0404 true,
0405 inpos,
0406 inmom,
0407 true,
0408 outertsos.curvilinearError(),
0409 outerId,
0410 innertsos.curvilinearError(),
0411 innerId,
0412 seedDir,
0413 theTraj->seedRef()));
0414
0415 reco::TrackExtra& tx = selTrackExtras->back();
0416
0417
0418 reco::TrackExtra::TrajParams trajParams;
0419 reco::TrackExtra::Chi2sFive chi2s;
0420 Traj2TrackHits t2t;
0421 auto ih = selHits->size();
0422 t2t(*theTraj, *selHits, trajParams, chi2s);
0423 auto ie = selHits->size();
0424 tx.setHits(rHits, ih, ie - ih);
0425 tx.setTrajParams(std::move(trajParams), std::move(chi2s));
0426 for (; ih < ie; ++ih) {
0427 auto const& hit = (*selHits)[ih];
0428 track.appendHitPattern(hit, *ttopo);
0429 }
0430
0431
0432 delete theTrack;
0433 delete theTraj;
0434 }
0435
0436
0437
0438
0439
0440
0441
0442
0443
0444
0445
0446
0447 rTracks_ = evt.put(std::move(selTracks));
0448
0449 evt.put(std::move(selTrackExtras));
0450 evt.put(std::move(selHits));
0451
0452 if (myTrajectoryInEvent_) {
0453 edm::OrphanHandle<std::vector<Trajectory>> rTrajs = evt.put(std::move(selTrajectories));
0454
0455
0456 auto trajTrackMap = std::make_unique<TrajTrackAssociationCollection>(rTrajs, rTracks_);
0457 for (std::map<unsigned int, unsigned int>::iterator i = tjTkMap.begin(); i != tjTkMap.end(); i++) {
0458 edm::Ref<std::vector<Trajectory>> trajRef(rTrajs, (*i).first);
0459 edm::Ref<reco::TrackCollection> tkRef(rTracks_, (*i).second);
0460 trajTrackMap->insert(edm::Ref<std::vector<Trajectory>>(rTrajs, (*i).first),
0461 edm::Ref<reco::TrackCollection>(rTracks_, (*i).second));
0462 }
0463 evt.put(std::move(trajTrackMap));
0464 }
0465 }