Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2025-01-18 03:42:23

0001 /** \class TrackRefitter
0002  *  Refit Tracks: Produce Tracks from TrackCollection. It performs a new final fit on a TrackCollection.
0003  *
0004  *  \author cerati
0005  */
0006 
0007 // system include files
0008 #include <memory>
0009 
0010 // user include files
0011 #include "DataFormats/TrackerCommon/interface/TrackerTopology.h"
0012 #include "FWCore/Framework/interface/Frameworkfwd.h"
0013 #include "FWCore/Framework/interface/stream/EDProducer.h"
0014 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0015 #include "Geometry/Records/interface/TrackerTopologyRcd.h"
0016 #include "Geometry/TrackerGeometryBuilder/interface/TrackerGeometry.h"
0017 #include "RecoTracker/TrackProducer/interface/KfTrackProducerBase.h"
0018 #include "RecoTracker/TrackProducer/interface/TrackProducerAlgorithm.h"
0019 #include "TrackingTools/PatternTools/interface/TrajTrackAssociation.h"
0020 #include "TrackingTools/PatternTools/interface/Trajectory.h"
0021 
0022 class TrackRefitter : public KfTrackProducerBase, public edm::stream::EDProducer<> {
0023 public:
0024   /// Constructor
0025   explicit TrackRefitter(const edm::ParameterSet &iConfig);
0026 
0027   /// Implementation of produce method
0028   void produce(edm::Event &, const edm::EventSetup &) override;
0029 
0030   /// fillDescriptions
0031   static void fillDescriptions(edm::ConfigurationDescriptions &descriptions);
0032 
0033 private:
0034   TrackProducerAlgorithm<reco::Track> theAlgo;
0035   enum Constraint { none, momentum, vertex, trackParameters };
0036   Constraint constraint_;
0037   edm::EDGetToken trkconstrcoll_;
0038 
0039   const edm::ESGetToken<TrackerTopology, TrackerTopologyRcd> ttopoToken_;
0040 };
0041 
0042 TrackRefitter::TrackRefitter(const edm::ParameterSet &iConfig)
0043     : KfTrackProducerBase(iConfig.getParameter<bool>("TrajectoryInEvent"),
0044                           iConfig.getParameter<bool>("useHitsSplitting")),
0045       theAlgo(iConfig),
0046       ttopoToken_(esConsumes()) {
0047   initTrackProducerBase(
0048       iConfig, consumesCollector(), consumes<edm::View<reco::Track>>(iConfig.getParameter<edm::InputTag>("src")));
0049   setAlias(iConfig.getParameter<std::string>("@module_label"));
0050   std::string constraint_str = iConfig.getParameter<std::string>("constraint");
0051   edm::InputTag trkconstrcoll = iConfig.getParameter<edm::InputTag>("srcConstr");
0052 
0053   if (constraint_str.empty())
0054     constraint_ = none;
0055   else if (constraint_str == "momentum") {
0056     constraint_ = momentum;
0057     trkconstrcoll_ = consumes<TrackMomConstraintAssociationCollection>(trkconstrcoll);
0058   } else if (constraint_str == "vertex") {
0059     constraint_ = vertex;
0060     trkconstrcoll_ = consumes<TrackVtxConstraintAssociationCollection>(trkconstrcoll);
0061   } else if (constraint_str == "trackParameters") {
0062     constraint_ = trackParameters;
0063     trkconstrcoll_ = consumes<TrackParamConstraintAssociationCollection>(trkconstrcoll);
0064   } else {
0065     edm::LogError("TrackRefitter")
0066         << "constraint: " << constraint_str
0067         << " not understood. Set it to 'momentum', 'vertex', 'trackParameters' or leave it empty";
0068     throw cms::Exception("TrackRefitter")
0069         << "unknown type of contraint! Set it to 'momentum', 'vertex', 'trackParameters' or leave it empty";
0070   }
0071 
0072   //register your products
0073   produces<reco::TrackCollection>().setBranchAlias(alias_ + "Tracks");
0074   produces<reco::TrackExtraCollection>().setBranchAlias(alias_ + "TrackExtras");
0075   produces<TrackingRecHitCollection>().setBranchAlias(alias_ + "RecHits");
0076   produces<std::vector<Trajectory>>();
0077   produces<std::vector<int>>();
0078   produces<TrajTrackAssociationCollection>();
0079 }
0080 
0081 void TrackRefitter::fillDescriptions(edm::ConfigurationDescriptions &descriptions) {
0082   edm::ParameterSetDescription desc;
0083   desc.add<bool>("TrajectoryInEvent", false);
0084   desc.add<bool>("useHitsSplitting", false);
0085   desc.add<edm::InputTag>("src", edm::InputTag(""));
0086   desc.add<std::string>("constraint", "");
0087   desc.add<edm::InputTag>("srcConstr", edm::InputTag(""));
0088   TrackProducerAlgorithm<reco::Track>::fillPSetDescription(desc);
0089   KfTrackProducerBase::fillPSetDescription(desc);
0090   descriptions.addWithDefaultLabel(desc);
0091 }
0092 
0093 void TrackRefitter::produce(edm::Event &theEvent, const edm::EventSetup &setup) {
0094   LogDebug("TrackRefitter") << "Analyzing event number: " << theEvent.id() << "\n";
0095   //
0096   // create empty output collections
0097   //
0098   std::unique_ptr<TrackingRecHitCollection> outputRHColl(new TrackingRecHitCollection);
0099   std::unique_ptr<reco::TrackCollection> outputTColl(new reco::TrackCollection);
0100   std::unique_ptr<reco::TrackExtraCollection> outputTEColl(new reco::TrackExtraCollection);
0101   std::unique_ptr<std::vector<Trajectory>> outputTrajectoryColl(new std::vector<Trajectory>);
0102   std::unique_ptr<std::vector<int>> outputIndecesInputColl(new std::vector<int>);
0103 
0104   //
0105   //declare and get stuff to be retrieved from ES
0106   //
0107   edm::ESHandle<TrackerGeometry> theG;
0108   edm::ESHandle<MagneticField> theMF;
0109   edm::ESHandle<TrajectoryFitter> theFitter;
0110   edm::ESHandle<Propagator> thePropagator;
0111   edm::ESHandle<MeasurementTracker> theMeasTk;
0112   edm::ESHandle<TransientTrackingRecHitBuilder> theBuilder;
0113   getFromES(setup, theG, theMF, theFitter, thePropagator, theMeasTk, theBuilder);
0114 
0115   TrackerTopology const &ttopo = setup.getData(ttopoToken_);
0116 
0117   //
0118   //declare and get TrackCollection to be retrieved from the event
0119   //
0120   AlgoProductCollection algoResults;
0121   reco::BeamSpot bs;
0122   switch (constraint_) {
0123     case none: {
0124       edm::Handle<edm::View<reco::Track>> theTCollection;
0125       getFromEvt(theEvent, theTCollection, bs);
0126 
0127       LogDebug("TrackRefitter") << "TrackRefitter::produce(none):Number of Trajectories:" << (*theTCollection).size();
0128 
0129       if (bs.position() == math::XYZPoint(0., 0., 0.) && bs.type() == reco::BeamSpot::Unknown) {
0130         edm::LogError("TrackRefitter") << " BeamSpot is (0,0,0), it is probably because is not valid in the event";
0131         break;
0132       }
0133 
0134       if (theTCollection.failedToGet()) {
0135         edm::EDConsumerBase::Labels labels;
0136         labelsForToken(src_, labels);
0137         edm::LogError("TrackRefitter") << "could not get the reco::TrackCollection." << labels.module;
0138         break;
0139       }
0140       LogDebug("TrackRefitter") << "run the algorithm"
0141                                 << "\n";
0142 
0143       try {
0144         theAlgo.runWithTrack(theG.product(),
0145                              theMF.product(),
0146                              *theTCollection,
0147                              theFitter.product(),
0148                              thePropagator.product(),
0149                              theBuilder.product(),
0150                              bs,
0151                              algoResults);
0152       } catch (cms::Exception &e) {
0153         edm::LogError("TrackProducer") << "cms::Exception caught during theAlgo.runWithTrack."
0154                                        << "\n"
0155                                        << e << "\n";
0156         throw;
0157       }
0158       break;
0159     }
0160     case momentum: {
0161       edm::Handle<TrackMomConstraintAssociationCollection> theTCollectionWithConstraint;
0162       theEvent.getByToken(trkconstrcoll_, theTCollectionWithConstraint);
0163 
0164       edm::Handle<reco::BeamSpot> recoBeamSpotHandle;
0165       theEvent.getByToken(bsSrc_, recoBeamSpotHandle);
0166       if (!recoBeamSpotHandle.isValid())
0167         break;
0168       bs = *recoBeamSpotHandle;
0169       if (theTCollectionWithConstraint.failedToGet()) {
0170         //edm::LogError("TrackRefitter")<<"could not get TrackMomConstraintAssociationCollection product.";
0171         break;
0172       }
0173       LogDebug("TrackRefitter") << "run the algorithm"
0174                                 << "\n";
0175       try {
0176         theAlgo.runWithMomentum(theG.product(),
0177                                 theMF.product(),
0178                                 *theTCollectionWithConstraint,
0179                                 theFitter.product(),
0180                                 thePropagator.product(),
0181                                 theBuilder.product(),
0182                                 bs,
0183                                 algoResults);
0184       } catch (cms::Exception &e) {
0185         edm::LogError("TrackProducer") << "cms::Exception caught during theAlgo.runWithMomentum."
0186                                        << "\n"
0187                                        << e << "\n";
0188         throw;
0189       }
0190       break;
0191     }
0192     case vertex: {
0193       edm::Handle<TrackVtxConstraintAssociationCollection> theTCollectionWithConstraint;
0194       theEvent.getByToken(trkconstrcoll_, theTCollectionWithConstraint);
0195       edm::Handle<reco::BeamSpot> recoBeamSpotHandle;
0196       theEvent.getByToken(bsSrc_, recoBeamSpotHandle);
0197       if (!recoBeamSpotHandle.isValid())
0198         break;
0199       bs = *recoBeamSpotHandle;
0200       if (theTCollectionWithConstraint.failedToGet()) {
0201         edm::LogError("TrackRefitter") << "could not get TrackVtxConstraintAssociationCollection product.";
0202         break;
0203       }
0204       LogDebug("TrackRefitter") << "run the algorithm"
0205                                 << "\n";
0206       try {
0207         theAlgo.runWithVertex(theG.product(),
0208                               theMF.product(),
0209                               *theTCollectionWithConstraint,
0210                               theFitter.product(),
0211                               thePropagator.product(),
0212                               theBuilder.product(),
0213                               bs,
0214                               algoResults);
0215       } catch (cms::Exception &e) {
0216         edm::LogError("TrackProducer") << "cms::Exception caught during theAlgo.runWithVertex."
0217                                        << "\n"
0218                                        << e << "\n";
0219         throw;
0220       }
0221       break;
0222     }
0223     case trackParameters: {
0224       edm::Handle<TrackParamConstraintAssociationCollection> theTCollectionWithConstraint;
0225       theEvent.getByToken(trkconstrcoll_, theTCollectionWithConstraint);
0226       edm::Handle<reco::BeamSpot> recoBeamSpotHandle;
0227       theEvent.getByToken(bsSrc_, recoBeamSpotHandle);
0228       if (!recoBeamSpotHandle.isValid())
0229         break;
0230       bs = *recoBeamSpotHandle;
0231       if (theTCollectionWithConstraint.failedToGet()) {
0232         //edm::LogError("TrackRefitter")<<"could not get TrackParamConstraintAssociationCollection product.";
0233         break;
0234       }
0235       LogDebug("TrackRefitter") << "run the algorithm"
0236                                 << "\n";
0237       try {
0238         theAlgo.runWithTrackParameters(theG.product(),
0239                                        theMF.product(),
0240                                        *theTCollectionWithConstraint,
0241                                        theFitter.product(),
0242                                        thePropagator.product(),
0243                                        theBuilder.product(),
0244                                        bs,
0245                                        algoResults);
0246       } catch (cms::Exception &e) {
0247         edm::LogError("TrackProducer") << "cms::Exception caught during theAlgo.runWithTrackParameters."
0248                                        << "\n"
0249                                        << e << "\n";
0250         throw;
0251       }
0252     }
0253       //default... there cannot be any other possibility due to the check in the ctor
0254   }
0255 
0256   //put everything in th event
0257   putInEvt(theEvent,
0258            thePropagator.product(),
0259            theMeasTk.product(),
0260            outputRHColl,
0261            outputTColl,
0262            outputTEColl,
0263            outputTrajectoryColl,
0264            outputIndecesInputColl,
0265            algoResults,
0266            theBuilder.product(),
0267            &ttopo);
0268   LogDebug("TrackRefitter") << "end"
0269                             << "\n";
0270 }
0271 
0272 #include "FWCore/Framework/interface/MakerMacros.h"
0273 DEFINE_FWK_MODULE(TrackRefitter);