Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:28:57

0001 #include "VertexBeamspotOrigins.h"
0002 
0003 VertexBeamspotOrigins::VertexBeamspotOrigins(const edm::ParameterSet& regPSet, edm::ConsumesCollector& iC) {
0004   // operation mode
0005   std::string operationModeString = regPSet.getParameter<std::string>("operationMode");
0006   if (operationModeString == "BeamSpotFixed")
0007     m_operationMode = OperationMode::BEAM_SPOT_FIXED;
0008   else if (operationModeString == "BeamSpotSigma")
0009     m_operationMode = OperationMode::BEAM_SPOT_SIGMA;
0010   else if (operationModeString == "VerticesFixed")
0011     m_operationMode = OperationMode::VERTICES_FIXED;
0012   else if (operationModeString == "VerticesSigma")
0013     m_operationMode = OperationMode::VERTICES_SIGMA;
0014   else
0015     throw cms::Exception("Configuration") << "Unknown operation mode string: " << operationModeString;
0016 
0017   token_beamSpot = iC.consumes<reco::BeamSpot>(regPSet.getParameter<edm::InputTag>("beamSpot"));
0018   m_maxNVertices = 1;
0019   if (m_operationMode == OperationMode::VERTICES_FIXED || m_operationMode == OperationMode::VERTICES_SIGMA) {
0020     token_vertex = iC.consumes<reco::VertexCollection>(regPSet.getParameter<edm::InputTag>("vertexCollection"));
0021     m_maxNVertices = regPSet.getParameter<int>("maxNVertices");
0022   }
0023 
0024   // mode-dependent z-halflength of tracking regions
0025   m_zErrorBeamSpot = regPSet.getParameter<double>("zErrorBeamSpot");
0026   if (m_operationMode == OperationMode::VERTICES_SIGMA)
0027     m_nSigmaZVertex = regPSet.getParameter<double>("nSigmaZVertex");
0028   if (m_operationMode == OperationMode::VERTICES_FIXED)
0029     m_zErrorVertex = regPSet.getParameter<double>("zErrorVertex");
0030   m_nSigmaZBeamSpot = -1.;
0031   if (m_operationMode == OperationMode::BEAM_SPOT_SIGMA) {
0032     m_nSigmaZBeamSpot = regPSet.getParameter<double>("nSigmaZBeamSpot");
0033     if (m_nSigmaZBeamSpot < 0.)
0034       throw cms::Exception("Configuration") << "nSigmaZBeamSpot must be positive for BeamSpotSigma mode!";
0035   }
0036 }
0037 
0038 void VertexBeamspotOrigins::fillDescriptions(edm::ParameterSetDescription& desc,
0039                                              const std::string& defaultBeamSpot,
0040                                              const std::string& defaultVertex,
0041                                              int defaultMaxVertices) {
0042   desc.add<std::string>("operationMode", "BeamSpotFixed");
0043   desc.add<edm::InputTag>("beamSpot", defaultBeamSpot);
0044   desc.add<edm::InputTag>("vertexCollection", defaultVertex);
0045   desc.add<int>("maxNVertices", defaultMaxVertices);
0046 
0047   desc.add<double>("nSigmaZBeamSpot", 4.);
0048   desc.add<double>("zErrorBeamSpot", 24.2);
0049   desc.add<double>("nSigmaZVertex", 3.);
0050   desc.add<double>("zErrorVertex", 0.2);
0051 }
0052 
0053 VertexBeamspotOrigins::Origins VertexBeamspotOrigins::origins(const edm::Event& iEvent) const {
0054   Origins ret;
0055 
0056   // always need the beam spot (as a fall back strategy for vertex modes)
0057   edm::Handle<reco::BeamSpot> bs;
0058   iEvent.getByToken(token_beamSpot, bs);
0059   if (!bs.isValid())
0060     return ret;
0061 
0062   // this is a default origin for all modes
0063   GlobalPoint default_origin(bs->x0(), bs->y0(), bs->z0());
0064 
0065   // fill the origins and halfLengths depending on the mode
0066   if (m_operationMode == OperationMode::BEAM_SPOT_FIXED || m_operationMode == OperationMode::BEAM_SPOT_SIGMA) {
0067     ret.emplace_back(
0068         default_origin,
0069         (m_operationMode == OperationMode::BEAM_SPOT_FIXED) ? m_zErrorBeamSpot : m_nSigmaZBeamSpot * bs->sigmaZ());
0070   } else if (m_operationMode == OperationMode::VERTICES_FIXED || m_operationMode == OperationMode::VERTICES_SIGMA) {
0071     edm::Handle<reco::VertexCollection> vertices;
0072     iEvent.getByToken(token_vertex, vertices);
0073     int n_vert = 0;
0074     for (const auto& v : *vertices) {
0075       if (v.isFake() || !v.isValid())
0076         continue;
0077 
0078       ret.emplace_back(
0079           GlobalPoint(v.x(), v.y(), v.z()),
0080           (m_operationMode == OperationMode::VERTICES_FIXED) ? m_zErrorVertex : m_nSigmaZVertex * v.zError());
0081       ++n_vert;
0082       if (m_maxNVertices >= 0 && n_vert >= m_maxNVertices) {
0083         break;
0084       }
0085     }
0086     // no-vertex fall-back case:
0087     if (ret.empty()) {
0088       ret.emplace_back(default_origin, (m_nSigmaZBeamSpot > 0.) ? m_nSigmaZBeamSpot * bs->z0Error() : m_zErrorBeamSpot);
0089     }
0090   }
0091 
0092   return ret;
0093 }