Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-10-01 22:41:04

0001 #ifndef CandidateSeededTrackingRegionsProducer_h
0002 #define CandidateSeededTrackingRegionsProducer_h
0003 
0004 #include "RecoTracker/TkTrackingRegions/interface/TrackingRegionProducer.h"
0005 #include "RecoTracker/TkTrackingRegions/interface/RectangularEtaPhiTrackingRegion.h"
0006 #include "RecoTracker/MeasurementDet/interface/MeasurementTrackerEvent.h"
0007 
0008 #include "FWCore/Framework/interface/Event.h"
0009 #include "FWCore/Framework/interface/EventSetup.h"
0010 #include "FWCore/Utilities/interface/InputTag.h"
0011 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0012 #include "FWCore/Framework/interface/ConsumesCollector.h"
0013 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0014 #include "DataFormats/Common/interface/Handle.h"
0015 #include "DataFormats/BeamSpot/interface/BeamSpot.h"
0016 #include "DataFormats/VertexReco/interface/Vertex.h"
0017 #include "DataFormats/VertexReco/interface/VertexFwd.h"
0018 #include "DataFormats/Candidate/interface/Candidate.h"
0019 #include "MagneticField/Engine/interface/MagneticField.h"
0020 #include "MagneticField/Records/interface/IdealMagneticFieldRecord.h"
0021 #include "RecoTracker/Record/interface/TrackerMultipleScatteringRecord.h"
0022 #include "RecoTracker/TkMSParametrization/interface/MultipleScatteringParametrisationMaker.h"
0023 
0024 /** class CandidateSeededTrackingRegionsProducer
0025  *
0026  * eta-phi TrackingRegions producer in directions defined by Candidate-based objects of interest
0027  * from a collection defined by the "input" parameter.
0028  *
0029  * Four operational modes are supported ("mode" parameter):
0030  *
0031  *   BeamSpotFixed:
0032  *     origin is defined by the beam spot
0033  *     z-half-length is defined by a fixed zErrorBeamSpot parameter
0034  *   BeamSpotSigma:
0035  *     origin is defined by the beam spot
0036  *     z-half-length is defined by nSigmaZBeamSpot * beamSpot.sigmaZ
0037  *   VerticesFixed:
0038  *     origins are defined by vertices from VertexCollection (use maximum MaxNVertices of them)
0039  *     z-half-length is defined by a fixed zErrorVetex parameter
0040  *   VerticesSigma:
0041  *     origins are defined by vertices from VertexCollection (use maximum MaxNVertices of them)
0042  *     z-half-length is defined by nSigmaZVertex * vetex.zError
0043  *
0044  *   If, while using one of the "Vertices" modes, there's no vertices in an event, we fall back into
0045  *   either BeamSpotSigma or BeamSpotFixed mode, depending on the positiveness of nSigmaZBeamSpot.
0046  *
0047  *   \author Vadim Khotilovich
0048  */
0049 class CandidateSeededTrackingRegionsProducer : public TrackingRegionProducer {
0050 public:
0051   typedef enum { BEAM_SPOT_FIXED, BEAM_SPOT_SIGMA, VERTICES_FIXED, VERTICES_SIGMA } Mode;
0052 
0053   explicit CandidateSeededTrackingRegionsProducer(const edm::ParameterSet& conf, edm::ConsumesCollector&& iC)
0054       : token_field(iC.esConsumes()) {
0055     edm::ParameterSet regPSet = conf.getParameter<edm::ParameterSet>("RegionPSet");
0056 
0057     // operation mode
0058     std::string modeString = regPSet.getParameter<std::string>("mode");
0059     if (modeString == "BeamSpotFixed")
0060       m_mode = BEAM_SPOT_FIXED;
0061     else if (modeString == "BeamSpotSigma")
0062       m_mode = BEAM_SPOT_SIGMA;
0063     else if (modeString == "VerticesFixed")
0064       m_mode = VERTICES_FIXED;
0065     else if (modeString == "VerticesSigma")
0066       m_mode = VERTICES_SIGMA;
0067     else
0068       edm::LogError("CandidateSeededTrackingRegionsProducer") << "Unknown mode string: " << modeString;
0069 
0070     // basic inputs
0071     token_input = iC.consumes<reco::CandidateView>(regPSet.getParameter<edm::InputTag>("input"));
0072     m_maxNRegions = regPSet.getParameter<int>("maxNRegions");
0073     token_beamSpot = iC.consumes<reco::BeamSpot>(regPSet.getParameter<edm::InputTag>("beamSpot"));
0074     m_maxNVertices = 1;
0075     if (m_mode == VERTICES_FIXED || m_mode == VERTICES_SIGMA) {
0076       token_vertex = iC.consumes<reco::VertexCollection>(regPSet.getParameter<edm::InputTag>("vertexCollection"));
0077       m_maxNVertices = regPSet.getParameter<int>("maxNVertices");
0078     }
0079 
0080     // RectangularEtaPhiTrackingRegion parameters:
0081     m_ptMin = regPSet.getParameter<double>("ptMin");
0082     m_originRadius = regPSet.getParameter<double>("originRadius");
0083     m_zErrorBeamSpot = regPSet.getParameter<double>("zErrorBeamSpot");
0084     m_deltaEta = regPSet.getParameter<double>("deltaEta");
0085     m_deltaPhi = regPSet.getParameter<double>("deltaPhi");
0086     m_precise = regPSet.getParameter<bool>("precise");
0087     m_whereToUseMeasurementTracker = RectangularEtaPhiTrackingRegion::stringToUseMeasurementTracker(
0088         regPSet.getParameter<std::string>("whereToUseMeasurementTracker"));
0089     if (m_whereToUseMeasurementTracker != RectangularEtaPhiTrackingRegion::UseMeasurementTracker::kNever) {
0090       token_measurementTracker =
0091           iC.consumes<MeasurementTrackerEvent>(regPSet.getParameter<edm::InputTag>("measurementTrackerName"));
0092     }
0093     m_searchOpt = false;
0094     if (regPSet.exists("searchOpt"))
0095       m_searchOpt = regPSet.getParameter<bool>("searchOpt");
0096 
0097     // mode-dependent z-halflength of tracking regions
0098     if (m_mode == VERTICES_SIGMA)
0099       m_nSigmaZVertex = regPSet.getParameter<double>("nSigmaZVertex");
0100     if (m_mode == VERTICES_FIXED)
0101       m_zErrorVetex = regPSet.getParameter<double>("zErrorVetex");
0102     m_nSigmaZBeamSpot = -1.;
0103     if (m_mode == BEAM_SPOT_SIGMA) {
0104       m_nSigmaZBeamSpot = regPSet.getParameter<double>("nSigmaZBeamSpot");
0105       if (m_nSigmaZBeamSpot < 0.)
0106         edm::LogError("CandidateSeededTrackingRegionsProducer")
0107             << "nSigmaZBeamSpot must be positive for BeamSpotSigma mode!";
0108     }
0109     if (m_precise) {
0110       token_msmaker = iC.esConsumes();
0111     }
0112   }
0113 
0114   ~CandidateSeededTrackingRegionsProducer() override = default;
0115 
0116   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0117     edm::ParameterSetDescription desc;
0118 
0119     desc.add<std::string>("mode", "BeamSpotFixed");
0120 
0121     desc.add<edm::InputTag>("input", edm::InputTag(""));
0122     desc.add<int>("maxNRegions", 10);
0123     desc.add<edm::InputTag>("beamSpot", edm::InputTag("hltOnlineBeamSpot"));
0124     desc.add<edm::InputTag>("vertexCollection", edm::InputTag("hltPixelVertices"));
0125     desc.add<int>("maxNVertices", 1);
0126 
0127     desc.add<double>("ptMin", 0.9);
0128     desc.add<double>("originRadius", 0.2);
0129     desc.add<double>("zErrorBeamSpot", 24.2);
0130     desc.add<double>("deltaEta", 0.5);
0131     desc.add<double>("deltaPhi", 0.5);
0132     desc.add<bool>("precise", true);
0133 
0134     desc.add<double>("nSigmaZVertex", 3.);
0135     desc.add<double>("zErrorVetex", 0.2);
0136     desc.add<double>("nSigmaZBeamSpot", 4.);
0137 
0138     desc.add<std::string>("whereToUseMeasurementTracker", "ForSiStrips");
0139     desc.add<edm::InputTag>("measurementTrackerName", edm::InputTag(""));
0140 
0141     desc.add<bool>("searchOpt", false);
0142 
0143     // Only for backwards-compatibility
0144     edm::ParameterSetDescription descRegion;
0145     descRegion.add<edm::ParameterSetDescription>("RegionPSet", desc);
0146 
0147     descriptions.add("seededTrackingRegionsFromBeamSpotFixedZLength", descRegion);
0148   }
0149 
0150   std::vector<std::unique_ptr<TrackingRegion> > regions(const edm::Event& e, const edm::EventSetup& es) const override {
0151     std::vector<std::unique_ptr<TrackingRegion> > result;
0152 
0153     // pick up the candidate objects of interest
0154     edm::Handle<reco::CandidateView> objects;
0155     e.getByToken(token_input, objects);
0156     size_t n_objects = objects->size();
0157     if (n_objects == 0)
0158       return result;
0159 
0160     // always need the beam spot (as a fall back strategy for vertex modes)
0161     edm::Handle<reco::BeamSpot> bs;
0162     e.getByToken(token_beamSpot, bs);
0163     if (!bs.isValid())
0164       return result;
0165 
0166     // this is a default origin for all modes
0167     GlobalPoint default_origin(bs->x0(), bs->y0(), bs->z0());
0168 
0169     // vector of origin & halfLength pairs:
0170     std::vector<std::pair<GlobalPoint, float> > origins;
0171 
0172     // fill the origins and halfLengths depending on the mode
0173     if (m_mode == BEAM_SPOT_FIXED || m_mode == BEAM_SPOT_SIGMA) {
0174       origins.push_back(std::make_pair(
0175           default_origin, (m_mode == BEAM_SPOT_FIXED) ? m_zErrorBeamSpot : m_nSigmaZBeamSpot * bs->sigmaZ()));
0176     } else if (m_mode == VERTICES_FIXED || m_mode == VERTICES_SIGMA) {
0177       edm::Handle<reco::VertexCollection> vertices;
0178       e.getByToken(token_vertex, vertices);
0179       int n_vert = 0;
0180       for (reco::VertexCollection::const_iterator v = vertices->begin();
0181            v != vertices->end() && n_vert < m_maxNVertices;
0182            ++v) {
0183         if (v->isFake() || !v->isValid())
0184           continue;
0185 
0186         origins.push_back(std::make_pair(GlobalPoint(v->x(), v->y(), v->z()),
0187                                          (m_mode == VERTICES_FIXED) ? m_zErrorVetex : m_nSigmaZVertex * v->zError()));
0188         ++n_vert;
0189       }
0190       // no-vertex fall-back case:
0191       if (origins.empty()) {
0192         origins.push_back(std::make_pair(
0193             default_origin, (m_nSigmaZBeamSpot > 0.) ? m_nSigmaZBeamSpot * bs->z0Error() : m_zErrorBeamSpot));
0194       }
0195     }
0196 
0197     const MeasurementTrackerEvent* measurementTracker = nullptr;
0198     if (!token_measurementTracker.isUninitialized()) {
0199       edm::Handle<MeasurementTrackerEvent> hmte;
0200       e.getByToken(token_measurementTracker, hmte);
0201       measurementTracker = hmte.product();
0202     }
0203 
0204     const auto& field = es.getData(token_field);
0205     const MultipleScatteringParametrisationMaker* msmaker = nullptr;
0206     if (m_precise) {
0207       msmaker = &es.getData(token_msmaker);
0208     }
0209 
0210     // create tracking regions (maximum MaxNRegions of them) in directions of the
0211     // objects of interest (we expect that the collection was sorted in decreasing pt order)
0212     int n_regions = 0;
0213     for (size_t i = 0; i < n_objects && n_regions < m_maxNRegions; ++i) {
0214       const reco::Candidate& object = (*objects)[i];
0215       GlobalVector direction(object.momentum().x(), object.momentum().y(), object.momentum().z());
0216 
0217       for (size_t j = 0; j < origins.size() && n_regions < m_maxNRegions; ++j) {
0218         result.push_back(std::make_unique<RectangularEtaPhiTrackingRegion>(direction,
0219                                                                            origins[j].first,
0220                                                                            m_ptMin,
0221                                                                            m_originRadius,
0222                                                                            origins[j].second,
0223                                                                            m_deltaEta,
0224                                                                            m_deltaPhi,
0225                                                                            field,
0226                                                                            msmaker,
0227                                                                            m_precise,
0228                                                                            m_whereToUseMeasurementTracker,
0229                                                                            measurementTracker,
0230                                                                            m_searchOpt));
0231         ++n_regions;
0232       }
0233     }
0234     //std::cout<<"n_seeded_regions = "<<n_regions<<std::endl;
0235     edm::LogInfo("CandidateSeededTrackingRegionsProducer") << "produced " << n_regions << " regions";
0236 
0237     return result;
0238   }
0239 
0240 private:
0241   Mode m_mode;
0242 
0243   int m_maxNRegions;
0244   edm::EDGetTokenT<reco::VertexCollection> token_vertex;
0245   edm::EDGetTokenT<reco::BeamSpot> token_beamSpot;
0246   edm::EDGetTokenT<reco::CandidateView> token_input;
0247   int m_maxNVertices;
0248 
0249   float m_ptMin;
0250   float m_originRadius;
0251   float m_zErrorBeamSpot;
0252   float m_deltaEta;
0253   float m_deltaPhi;
0254   bool m_precise;
0255   edm::EDGetTokenT<MeasurementTrackerEvent> token_measurementTracker;
0256   RectangularEtaPhiTrackingRegion::UseMeasurementTracker m_whereToUseMeasurementTracker;
0257   edm::ESGetToken<MagneticField, IdealMagneticFieldRecord> token_field;
0258   edm::ESGetToken<MultipleScatteringParametrisationMaker, TrackerMultipleScatteringRecord> token_msmaker;
0259   bool m_searchOpt;
0260 
0261   float m_nSigmaZVertex;
0262   float m_zErrorVetex;
0263   float m_nSigmaZBeamSpot;
0264 };
0265 
0266 #endif