CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_5/src/Alignment/ReferenceTrajectories/plugins/ReferenceTrajectoryFactory.cc

Go to the documentation of this file.
00001 #include "DataFormats/GeometryVector/interface/GlobalVector.h"
00002 #include "DataFormats/GeometryVector/interface/GlobalPoint.h"
00003 #include "FWCore/Framework/interface/ESHandle.h"
00004 #include "FWCore/Framework/interface/EventSetup.h"
00005 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00006 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00007 #include "MagneticField/Records/interface/IdealMagneticFieldRecord.h" 
00008 #include "MagneticField/Engine/interface/MagneticField.h"
00009 #include "Alignment/ReferenceTrajectories/interface/TrajectoryFactoryPlugin.h"
00010 #include "Alignment/ReferenceTrajectories/interface/ReferenceTrajectory.h" 
00011 
00012 #include "Alignment/ReferenceTrajectories/interface/TrajectoryFactoryBase.h"
00013 
00014 #include "BzeroReferenceTrajectoryFactory.h"
00015 
00019 
00020 class ReferenceTrajectoryFactory : public TrajectoryFactoryBase
00021 {
00022 public:
00023   ReferenceTrajectoryFactory(const edm::ParameterSet &config);
00024   virtual ~ReferenceTrajectoryFactory();
00025 
00027   virtual const ReferenceTrajectoryCollection trajectories(const edm::EventSetup &setup,
00028                                                            const ConstTrajTrackPairCollection &tracks,
00029                                                            const reco::BeamSpot &beamSpot) const;
00030 
00031   virtual const ReferenceTrajectoryCollection trajectories(const edm::EventSetup &setup,
00032                                                            const ConstTrajTrackPairCollection &tracks,
00033                                                            const ExternalPredictionCollection &external,
00034                                                            const reco::BeamSpot &beamSpot) const;
00035 
00036   virtual ReferenceTrajectoryFactory* clone() const { return new ReferenceTrajectoryFactory(*this); }
00037 
00038 protected:
00039   ReferenceTrajectoryFactory(const ReferenceTrajectoryFactory &other);
00040   const TrajectoryFactoryBase *bzeroFactory() const;
00041 
00042   double theMass;
00043   bool   theUseBzeroIfFieldOff;
00044   mutable const TrajectoryFactoryBase *theBzeroFactory;
00045 };
00046 
00050 
00051 ReferenceTrajectoryFactory::ReferenceTrajectoryFactory( const edm::ParameterSet & config ) :
00052   TrajectoryFactoryBase( config ),
00053   theMass(config.getParameter<double>("ParticleMass")),
00054   theUseBzeroIfFieldOff(config.getParameter<bool>("UseBzeroIfFieldOff")),
00055   theBzeroFactory(0)
00056 {
00057   edm::LogInfo("Alignment") << "@SUB=ReferenceTrajectoryFactory"
00058                             << "mass: " << theMass
00059                             << "\nusing Bzero if |B| = 0: " 
00060                             << (theUseBzeroIfFieldOff ? "yes" : "no");
00061 }
00062 
00063 ReferenceTrajectoryFactory::ReferenceTrajectoryFactory(const ReferenceTrajectoryFactory &other) :
00064   TrajectoryFactoryBase(other),
00065   theMass(other.theMass),
00066   theUseBzeroIfFieldOff(other.theUseBzeroIfFieldOff),
00067   theBzeroFactory(0) // copy data members, but no double pointing to same Bzero factory...
00068 {
00069 }
00070  
00071 ReferenceTrajectoryFactory::~ReferenceTrajectoryFactory( void )
00072 {
00073   delete theBzeroFactory;
00074 }
00075 
00076 
00077 const ReferenceTrajectoryFactory::ReferenceTrajectoryCollection
00078 ReferenceTrajectoryFactory::trajectories(const edm::EventSetup &setup,
00079                                          const ConstTrajTrackPairCollection &tracks,
00080                                          const reco::BeamSpot &beamSpot) const
00081 {
00082   edm::ESHandle< MagneticField > magneticField;
00083   setup.get< IdealMagneticFieldRecord >().get( magneticField );
00084   if (theUseBzeroIfFieldOff && magneticField->inTesla(GlobalPoint(0.,0.,0.)).mag2() < 1.e-6) {
00085     return this->bzeroFactory()->trajectories(setup, tracks, beamSpot);
00086   }
00087 
00088   ReferenceTrajectoryCollection trajectories;
00089 
00090   ConstTrajTrackPairCollection::const_iterator itTracks = tracks.begin();
00091   
00092   while ( itTracks != tracks.end() )
00093   { 
00094     TrajectoryInput input = this->innermostStateAndRecHits( *itTracks );
00095     
00096     // Check input: If all hits were rejected, the TSOS is initialized as invalid.
00097     if ( input.first.isValid() )
00098     {
00099       // set the flag for reversing the RecHits to false, since they are already in the correct order.
00100       trajectories.push_back(ReferenceTrajectoryPtr(new ReferenceTrajectory(input.first, input.second, false,
00101                                                                             magneticField.product(), materialEffects(),
00102                                                                             propagationDirection(), theMass, 
00103                                                                             theUseBeamSpot, beamSpot)));
00104     }
00105 
00106     ++itTracks;
00107   }
00108 
00109   return trajectories;
00110 }
00111 
00112 
00113 const ReferenceTrajectoryFactory::ReferenceTrajectoryCollection
00114 ReferenceTrajectoryFactory::trajectories( const edm::EventSetup & setup,
00115                                           const ConstTrajTrackPairCollection& tracks,
00116                                           const ExternalPredictionCollection& external,
00117                                           const reco::BeamSpot &beamSpot) const
00118 {
00119   ReferenceTrajectoryCollection trajectories;
00120 
00121   if ( tracks.size() != external.size() )
00122   {
00123     edm::LogInfo("ReferenceTrajectories") << "@SUB=ReferenceTrajectoryFactory::trajectories"
00124                                           << "Inconsistent input:\n"
00125                                           << "\tnumber of tracks = " << tracks.size()
00126                                           << "\tnumber of external predictions = " << external.size();
00127     return trajectories;
00128   }
00129 
00130   edm::ESHandle< MagneticField > magneticField;
00131   setup.get< IdealMagneticFieldRecord >().get( magneticField );
00132   if (theUseBzeroIfFieldOff && magneticField->inTesla(GlobalPoint(0.,0.,0.)).mag2() < 1.e-6) {
00133     return this->bzeroFactory()->trajectories(setup, tracks, external, beamSpot);
00134   }
00135 
00136   ConstTrajTrackPairCollection::const_iterator itTracks = tracks.begin();
00137   ExternalPredictionCollection::const_iterator itExternal = external.begin();
00138 
00139   while ( itTracks != tracks.end() )
00140   {
00141     TrajectoryInput input = innermostStateAndRecHits( *itTracks );
00142     // Check input: If all hits were rejected, the TSOS is initialized as invalid.
00143     if ( input.first.isValid() )
00144     {
00145       if ( (*itExternal).isValid() && sameSurface( (*itExternal).surface(), input.first.surface() ) )
00146       {
00147         // set the flag for reversing the RecHits to false, since they are already in the correct order.
00148         ReferenceTrajectoryPtr refTraj( new ReferenceTrajectory( *itExternal, input.second, false,
00149                                                                  magneticField.product(), materialEffects(),
00150                                                                  propagationDirection(), theMass,
00151                                                                  theUseBeamSpot, beamSpot) );
00152           
00153         AlgebraicSymMatrix externalParamErrors( asHepMatrix<5>( (*itExternal).localError().matrix() ) );
00154         refTraj->setParameterErrors( externalParamErrors );
00155         trajectories.push_back( refTraj );
00156       }
00157       else
00158       {
00159         trajectories.push_back(ReferenceTrajectoryPtr(new ReferenceTrajectory(input.first, input.second, false,
00160                                                                               magneticField.product(), materialEffects(),
00161                                                                               propagationDirection(), theMass,
00162                                                                               theUseBeamSpot, beamSpot)));
00163       }
00164     }
00165     
00166     ++itTracks;
00167     ++itExternal;
00168   }
00169   
00170   return trajectories;
00171 }
00172 
00173 const TrajectoryFactoryBase *ReferenceTrajectoryFactory::bzeroFactory() const
00174 {
00175   if (!theBzeroFactory) {
00176     const edm::ParameterSet &myPset = this->configuration();
00177     edm::LogInfo("Alignment") << "@SUB=ReferenceTrajectoryFactory::bzeroFactory"
00178                               << "Using BzeroReferenceTrajectoryFactory for some (all?) events.";
00179     // We take the config of this factory, copy it, replace its name and add 
00180     // the momentum parameter as expected by BzeroReferenceTrajectoryFactory and create it:
00181     edm::ParameterSet pset;
00182     pset.copyForModify(myPset);
00183     // next two lines not needed, but may help to better understand log file:
00184     pset.eraseSimpleParameter("TrajectoryFactoryName");
00185     pset.addParameter("TrajectoryFactoryName", std::string("BzeroReferenceTrajectoryFactory"));
00186     pset.addParameter("MomentumEstimate", myPset.getParameter<double>("MomentumEstimateFieldOff"));
00187     theBzeroFactory = new BzeroReferenceTrajectoryFactory(pset);
00188   }
00189   return theBzeroFactory;
00190 }
00191 
00192 DEFINE_EDM_PLUGIN( TrajectoryFactoryPlugin, ReferenceTrajectoryFactory, "ReferenceTrajectoryFactory" );