Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-09-10 10:03:11

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