Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-10-22 02:30:56

0001 #include "DataFormats/GeometryVector/interface/GlobalVector.h"
0002 #include "DataFormats/GeometryVector/interface/GlobalPoint.h"
0003 #include "FWCore/Framework/interface/ESHandle.h"
0004 #include "FWCore/Framework/interface/EventSetup.h"
0005 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0006 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0007 #include "MagneticField/Records/interface/IdealMagneticFieldRecord.h"
0008 #include "MagneticField/Engine/interface/MagneticField.h"
0009 #include "Alignment/ReferenceTrajectories/interface/TrajectoryFactoryPlugin.h"
0010 #include "Alignment/ReferenceTrajectories/interface/ReferenceTrajectory.h"
0011 
0012 #include "Alignment/ReferenceTrajectories/interface/TrajectoryFactoryBase.h"
0013 
0014 #include "BzeroReferenceTrajectoryFactory.h"
0015 
0016 /// A factory that produces instances of class ReferenceTrajectory from a given TrajTrackPairCollection.
0017 /// If |B| = 0 T and configuration parameter UseBzeroIfFieldOff is True,
0018 /// hand-over to the BzeroReferenceTrajectoryFactory.
0019 
0020 class ReferenceTrajectoryFactory : public TrajectoryFactoryBase {
0021 public:
0022   ReferenceTrajectoryFactory(const edm::ParameterSet &config, edm::ConsumesCollector &iC);
0023   ~ReferenceTrajectoryFactory() override;
0024   const edm::ESGetToken<MagneticField, IdealMagneticFieldRecord> m_MagFieldToken;
0025 
0026   /// Produce the reference trajectories.
0027   const ReferenceTrajectoryCollection trajectories(const edm::EventSetup &setup,
0028                                                    const ConstTrajTrackPairCollection &tracks,
0029                                                    const reco::BeamSpot &beamSpot) const override;
0030 
0031   const ReferenceTrajectoryCollection trajectories(const edm::EventSetup &setup,
0032                                                    const ConstTrajTrackPairCollection &tracks,
0033                                                    const ExternalPredictionCollection &external,
0034                                                    const reco::BeamSpot &beamSpot) const override;
0035 
0036   ReferenceTrajectoryFactory *clone() const override { return new ReferenceTrajectoryFactory(*this); }
0037 
0038 protected:
0039   ReferenceTrajectoryFactory(const ReferenceTrajectoryFactory &other);
0040   const TrajectoryFactoryBase *bzeroFactory() const;
0041   const TrajectoryFactoryBase *bzeroFactory(edm::ConsumesCollector &iC) const;
0042 
0043   double theMass;
0044   bool theUseBzeroIfFieldOff;
0045   //edm::ParameterSet pset;
0046   mutable const TrajectoryFactoryBase *theBzeroFactory;
0047 };
0048 
0049 ////////////////////////////////////////////////////////////////////////////////
0050 ////////////////////////////////////////////////////////////////////////////////
0051 ////////////////////////////////////////////////////////////////////////////////
0052 
0053 ReferenceTrajectoryFactory::ReferenceTrajectoryFactory(const edm::ParameterSet &config, edm::ConsumesCollector &iC)
0054     : TrajectoryFactoryBase(config, iC),
0055       m_MagFieldToken(iC.esConsumes()),
0056       theMass(config.getParameter<double>("ParticleMass")),
0057       theUseBzeroIfFieldOff(config.getParameter<bool>("UseBzeroIfFieldOff")),
0058       theBzeroFactory(nullptr) {
0059   edm::LogInfo("Alignment") << "@SUB=ReferenceTrajectoryFactory"
0060                             << "mass: " << theMass
0061                             << "\nusing Bzero if |B| = 0: " << (theUseBzeroIfFieldOff ? "yes" : "no");
0062   // We take the config of this factory, copy it, replace its name and add
0063   // the momentum parameter as expected by BzeroReferenceTrajectoryFactory and create it:
0064   //
0065   edm::ParameterSet pset;
0066   pset.copyForModify(config);
0067   // next two lines not needed, but may help to better understand log file:
0068   pset.eraseSimpleParameter("TrajectoryFactoryName");
0069   pset.addParameter("TrajectoryFactoryName", std::string("BzeroReferenceTrajectoryFactory"));
0070   pset.addParameter("MomentumEstimate", config.getParameter<double>("MomentumEstimateFieldOff"));
0071   theBzeroFactory = new BzeroReferenceTrajectoryFactory(pset, iC);
0072 }
0073 
0074 ReferenceTrajectoryFactory::ReferenceTrajectoryFactory(const ReferenceTrajectoryFactory &other)
0075     : TrajectoryFactoryBase(other),
0076       theMass(other.theMass),
0077       theUseBzeroIfFieldOff(other.theUseBzeroIfFieldOff),
0078       theBzeroFactory(nullptr)  // copy data members, but no double pointing to same Bzero factory...
0079 {}
0080 
0081 ReferenceTrajectoryFactory::~ReferenceTrajectoryFactory(void) { delete theBzeroFactory; }
0082 
0083 const ReferenceTrajectoryFactory::ReferenceTrajectoryCollection ReferenceTrajectoryFactory::trajectories(
0084     const edm::EventSetup &setup, const ConstTrajTrackPairCollection &tracks, const reco::BeamSpot &beamSpot) const {
0085   const MagneticField *magneticField = &setup.getData(m_MagFieldToken);
0086 
0087   if (theUseBzeroIfFieldOff && magneticField->inTesla(GlobalPoint(0., 0., 0.)).mag2() < 1.e-6) {
0088     return this->bzeroFactory()->trajectories(setup, tracks, beamSpot);
0089   }
0090 
0091   ReferenceTrajectoryCollection trajectories;
0092 
0093   ConstTrajTrackPairCollection::const_iterator itTracks = tracks.begin();
0094 
0095   while (itTracks != tracks.end()) {
0096     TrajectoryInput input = this->innermostStateAndRecHits(*itTracks);
0097 
0098     // Check input: If all hits were rejected, the TSOS is initialized as invalid.
0099     if (input.first.isValid()) {
0100       ReferenceTrajectoryBase::Config config(materialEffects(), propagationDirection(), theMass);
0101       config.useBeamSpot = useBeamSpot_;
0102       config.includeAPEs = includeAPEs_;
0103       config.allowZeroMaterial = allowZeroMaterial_;
0104       // set the flag for reversing the RecHits to false, since they are already in the correct order.
0105       config.hitsAreReverse = false;
0106       trajectories.push_back(
0107           ReferenceTrajectoryPtr(new ReferenceTrajectory(input.first, input.second, magneticField, beamSpot, config)));
0108     }
0109 
0110     ++itTracks;
0111   }
0112 
0113   return trajectories;
0114 }
0115 
0116 const ReferenceTrajectoryFactory::ReferenceTrajectoryCollection ReferenceTrajectoryFactory::trajectories(
0117     const edm::EventSetup &setup,
0118     const ConstTrajTrackPairCollection &tracks,
0119     const ExternalPredictionCollection &external,
0120     const reco::BeamSpot &beamSpot) const {
0121   ReferenceTrajectoryCollection trajectories;
0122 
0123   if (tracks.size() != external.size()) {
0124     edm::LogInfo("ReferenceTrajectories")
0125         << "@SUB=ReferenceTrajectoryFactory::trajectories"
0126         << "Inconsistent input:\n"
0127         << "\tnumber of tracks = " << tracks.size() << "\tnumber of external predictions = " << external.size();
0128     return trajectories;
0129   }
0130   const MagneticField *magneticField = &setup.getData(m_MagFieldToken);
0131 
0132   if (theUseBzeroIfFieldOff && magneticField->inTesla(GlobalPoint(0., 0., 0.)).mag2() < 1.e-6) {
0133     return this->bzeroFactory()->trajectories(setup, tracks, external, beamSpot);
0134   }
0135 
0136   ConstTrajTrackPairCollection::const_iterator itTracks = tracks.begin();
0137   ExternalPredictionCollection::const_iterator itExternal = external.begin();
0138 
0139   while (itTracks != tracks.end()) {
0140     TrajectoryInput input = innermostStateAndRecHits(*itTracks);
0141     // Check input: If all hits were rejected, the TSOS is initialized as invalid.
0142     if (input.first.isValid()) {
0143       if ((*itExternal).isValid() && sameSurface((*itExternal).surface(), input.first.surface())) {
0144         ReferenceTrajectoryBase::Config config(materialEffects(), propagationDirection(), theMass);
0145         config.useBeamSpot = useBeamSpot_;
0146         config.includeAPEs = includeAPEs_;
0147         config.allowZeroMaterial = allowZeroMaterial_;
0148         // set the flag for reversing the RecHits to false, since they are already in the correct order.
0149         config.hitsAreReverse = false;
0150         ReferenceTrajectoryPtr refTraj(
0151             new ReferenceTrajectory(*itExternal, input.second, magneticField, beamSpot, config));
0152 
0153         AlgebraicSymMatrix externalParamErrors(asHepMatrix<5>((*itExternal).localError().matrix()));
0154         refTraj->setParameterErrors(externalParamErrors);
0155         trajectories.push_back(refTraj);
0156       } else {
0157         ReferenceTrajectoryBase::Config config(materialEffects(), propagationDirection(), theMass);
0158         config.useBeamSpot = useBeamSpot_;
0159         config.includeAPEs = includeAPEs_;
0160         config.allowZeroMaterial = allowZeroMaterial_;
0161         config.hitsAreReverse = false;
0162         trajectories.push_back(ReferenceTrajectoryPtr(
0163             new ReferenceTrajectory(input.first, input.second, magneticField, beamSpot, config)));
0164       }
0165     }
0166 
0167     ++itTracks;
0168     ++itExternal;
0169   }
0170 
0171   return trajectories;
0172 }
0173 
0174 const TrajectoryFactoryBase *ReferenceTrajectoryFactory::bzeroFactory() const {
0175   if (!theBzeroFactory) {
0176     edm::LogInfo("Alignment") << "@SUB=ReferenceTrajectoryFactory::bzeroFactory"
0177                               << "Using BzeroReferenceTrajectoryFactory for some (all?) events.";
0178   }
0179   return theBzeroFactory;
0180 }
0181 
0182 DEFINE_EDM_PLUGIN(TrajectoryFactoryPlugin, ReferenceTrajectoryFactory, "ReferenceTrajectoryFactory");