Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:31:40

0001 #ifndef BasicTrajectoryState_H
0002 #define BasicTrajectoryState_H
0003 
0004 #include "TrackingTools/TrajectoryState/interface/ProxyBase11.h"
0005 
0006 #include "TrackingTools/TrajectoryParametrization/interface/LocalTrajectoryParameters.h"
0007 #include "TrackingTools/TrajectoryParametrization/interface/LocalTrajectoryError.h"
0008 #include "TrackingTools/TrajectoryParametrization/interface/GlobalTrajectoryParameters.h"
0009 #include "TrackingTools/TrajectoryParametrization/interface/CartesianTrajectoryError.h"
0010 #include "TrackingTools/TrajectoryParametrization/interface/CurvilinearTrajectoryError.h"
0011 #include "TrackingTools/TrajectoryState/interface/SurfaceSideDefinition.h"
0012 
0013 #include <vector>
0014 
0015 #include "TrackingTools/TrajectoryState/interface/FreeTrajectoryState.h"
0016 #include "TrackingTools/TrajectoryParametrization/interface/LocalTrajectoryParameters.h"
0017 #include "TrackingTools/TrajectoryParametrization/interface/LocalTrajectoryError.h"
0018 
0019 #include "DataFormats/GeometrySurface/interface/ReferenceCounted.h"
0020 #include "DataFormats/GeometryCommonDetAlgo/interface/DeepCopyPointer.h"
0021 #include "DataFormats/GeometrySurface/interface/Surface.h"
0022 #include "TrackingTools/TrajectoryParametrization/interface/TrajectoryStateExceptions.h"
0023 #include "FWCore/Utilities/interface/Likely.h"
0024 
0025 /// vvv DEBUG
0026 // #include <iostream>
0027 
0028 class MagneticField;
0029 class TrajectoryStateOnSurface;
0030 
0031 #ifdef DO_BTSCount
0032 class BTSCount {
0033 public:
0034   BTSCount() {}
0035   virtual ~BTSCount();
0036   BTSCount(BTSCount const&) {}
0037 
0038   static unsigned int maxReferences;
0039   static unsigned long long aveReferences;
0040   static unsigned long long toteReferences;
0041 
0042   void addReference() const {
0043     ++referenceCount_;
0044     referenceMax_ = std::max(referenceMax_, referenceCount_);
0045   }
0046   void removeReference() const {
0047     if (0 == --referenceCount_) {
0048       delete const_cast<BTSCount*>(this);
0049     }
0050   }
0051 
0052   unsigned int references() const { return referenceCount_; }
0053 
0054 private:
0055   mutable unsigned int referenceCount_ = 0;
0056   mutable unsigned int referenceMax_ = 0;
0057 };
0058 #endif
0059 
0060 /** No so Abstract (anyore) base class for TrajectoryState.
0061  *  It is ReferenceCounted.
0062  *
0063  * VI 8/12/2011   content of BasicSingleTrajectoryState moved here....
0064  * fully devirtualized
0065  */
0066 class BasicTrajectoryState {
0067 public:
0068   typedef BasicTrajectoryState BTSOS;
0069   typedef ProxyBase11<BTSOS> Proxy;
0070   typedef Proxy::pointer pointer;
0071   typedef SurfaceSideDefinition::SurfaceSide SurfaceSide;
0072   typedef Surface SurfaceType;
0073 
0074 public:
0075   // default constructor : to make root happy
0076   BasicTrajectoryState() : theValid(false), theWeight(0) {}
0077 
0078   /// construct invalid trajectory state (without parameters)
0079   explicit BasicTrajectoryState(const SurfaceType& aSurface);
0080 
0081   virtual ~BasicTrajectoryState();
0082 
0083   virtual pointer clone() const = 0;
0084 
0085   template <typename T, typename... Args>
0086   static std::shared_ptr<BTSOS> build(Args&&... args) {
0087     return std::make_shared<T>(std::forward<Args>(args)...);
0088   }
0089 
0090   template <typename T, typename... Args>
0091   static std::shared_ptr<BTSOS> churn(Args&&... args) {
0092     return std::allocate_shared<T>(churn_allocator<T>(), std::forward<Args>(args)...);
0093   }
0094 
0095   /** Constructor from FTS and surface. For surfaces with material
0096    *  the side of the surface should be specified explicitely.
0097    */
0098   BasicTrajectoryState(const FreeTrajectoryState& fts,
0099                        const SurfaceType& aSurface,
0100                        const SurfaceSide side = SurfaceSideDefinition::atCenterOfSurface)
0101       : theFreeState(fts),
0102         theLocalError(InvalidError()),
0103         theLocalParameters(),
0104         theLocalParametersValid(false),
0105         theValid(true),
0106         theSurfaceSide(side),
0107         theSurfaceP(&aSurface),
0108         theWeight(1.) {}
0109 
0110   /** Constructor from FTS: just a wrapper
0111    */
0112   explicit BasicTrajectoryState(const FreeTrajectoryState& fts)
0113       : theFreeState(fts),
0114         theLocalError(InvalidError()),
0115         theLocalParameters(),
0116         theLocalParametersValid(false),
0117         theValid(true),
0118         theWeight(1.) {}
0119 
0120   /** Constructor from local parameters, errors and surface. For surfaces 
0121    *  with material the side of the surface should be specified explicitely. 
0122    *  For multi-states the weight should be specified explicitely.
0123    */
0124   BasicTrajectoryState(const LocalTrajectoryParameters& par,
0125                        const LocalTrajectoryError& err,
0126                        const SurfaceType& aSurface,
0127                        const MagneticField* field,
0128                        const SurfaceSide side = SurfaceSideDefinition::atCenterOfSurface);
0129 
0130   /** Constructor from local parameters, errors and surface. For surfaces 
0131    *  with material the side of the surface should be specified explicitely.
0132    */
0133   BasicTrajectoryState(const LocalTrajectoryParameters& par,
0134                        const SurfaceType& aSurface,
0135                        const MagneticField* field,
0136                        const SurfaceSide side = SurfaceSideDefinition::atCenterOfSurface)
0137       : BasicTrajectoryState(par, InvalidError(), aSurface, field, side) {}
0138 
0139   /** Constructor from global parameters, errors and surface. For surfaces 
0140    *  with material the side of the surface should be specified explicitely.
0141    */
0142   BasicTrajectoryState(const GlobalTrajectoryParameters& par,
0143                        const CartesianTrajectoryError& err,
0144                        const SurfaceType& aSurface,
0145                        const SurfaceSide side = SurfaceSideDefinition::atCenterOfSurface)
0146       : theFreeState(par, err),
0147         theLocalError(InvalidError()),
0148         theLocalParameters(),
0149         theLocalParametersValid(false),
0150         theValid(true),
0151         theSurfaceSide(side),
0152         theSurfaceP(&aSurface),
0153         theWeight(1.) {}
0154 
0155   /** Constructor from global parameters, errors and surface. For surfaces 
0156    *  with material the side of the surface should be specified explicitely. 
0157    *  For multi-states the weight should be specified explicitely.
0158    */
0159   BasicTrajectoryState(const GlobalTrajectoryParameters& par,
0160                        const CurvilinearTrajectoryError& err,
0161                        const SurfaceType& aSurface,
0162                        const SurfaceSide side = SurfaceSideDefinition::atCenterOfSurface)
0163       : theFreeState(par, err),
0164         theLocalError(InvalidError()),
0165         theLocalParameters(),
0166         theLocalParametersValid(false),
0167         theValid(true),
0168         theSurfaceSide(side),
0169         theSurfaceP(&aSurface),
0170         theWeight(1.) {}
0171 
0172   /** Constructor from global parameters and surface. For surfaces with material
0173    *  the side of the surface should be specified explicitely.
0174    */
0175   BasicTrajectoryState(const GlobalTrajectoryParameters& par,
0176                        const SurfaceType& aSurface,
0177                        const SurfaceSide side = SurfaceSideDefinition::atCenterOfSurface)
0178       : BasicTrajectoryState(par, InvalidError(), aSurface, side) {}
0179 
0180   // as above, with explicit weight
0181   template <typename... Args>
0182   BasicTrajectoryState(double iweight, Args&&... args) : BasicTrajectoryState(std::forward<Args>(args)...) {
0183     theWeight = iweight;
0184   }
0185 
0186   bool isValid() const { return theValid; }
0187 
0188   // access global parameters/errors
0189   const GlobalTrajectoryParameters& globalParameters() const { return theFreeState.parameters(); }
0190   GlobalPoint globalPosition() const { return theFreeState.position(); }
0191   GlobalVector globalMomentum() const { return theFreeState.momentum(); }
0192   GlobalVector globalDirection() const { return theFreeState.momentum().unit(); }
0193   TrackCharge charge() const { return theFreeState.charge(); }
0194   double signedInverseMomentum() const { return theFreeState.signedInverseMomentum(); }
0195   double transverseCurvature() const { return theFreeState.transverseCurvature(); }
0196 
0197   const CartesianTrajectoryError cartesianError() const {
0198     if UNLIKELY (!hasError()) {
0199       missingError(" accesing cartesian error.");
0200       return CartesianTrajectoryError();
0201     }
0202     return freeTrajectoryState(true)->cartesianError();
0203   }
0204   const CurvilinearTrajectoryError& curvilinearError() const {
0205     if UNLIKELY (!hasError()) {
0206       missingError(" accesing curvilinearerror.");
0207       static const CurvilinearTrajectoryError crap;
0208       return crap;
0209     }
0210     return freeTrajectoryState(true)->curvilinearError();
0211   }
0212 
0213   FreeTrajectoryState const* freeTrajectoryState(bool withErrors = true) const {
0214     if UNLIKELY (!isValid())
0215       notValid();
0216     if (withErrors && hasError()) {  // this is the right thing
0217       checkCurvilinError();
0218     }
0219     return &theFreeState;
0220   }
0221 
0222   const MagneticField* magneticField() const { return &theFreeState.parameters().magneticField(); }
0223 
0224   // access local parameters/errors
0225   const LocalTrajectoryParameters& localParameters() const {
0226     if UNLIKELY (!isValid())
0227       notValid();
0228     if UNLIKELY (!theLocalParametersValid)
0229       createLocalParameters();
0230     return theLocalParameters;
0231   }
0232   LocalPoint localPosition() const { return localParameters().position(); }
0233   LocalVector localMomentum() const { return localParameters().momentum(); }
0234   LocalVector localDirection() const { return localMomentum().unit(); }
0235 
0236   const LocalTrajectoryError& localError() const {
0237     if UNLIKELY (!hasError()) {
0238       missingError(" accessing local error.");
0239       return theLocalError;
0240     }
0241     if UNLIKELY (theLocalError.invalid())
0242       createLocalError();
0243     return theLocalError;
0244   }
0245 
0246   const SurfaceType& surface() const { return *theSurfaceP; }
0247 
0248   double weight() const { return theWeight; }
0249 
0250   void rescaleError(double factor);
0251 
0252   /// Position relative to material, defined relative to momentum vector.
0253   SurfaceSide surfaceSide() const { return theSurfaceSide; }
0254 
0255   bool hasError() const { return theFreeState.hasError() || theLocalError.valid(); }
0256 
0257   virtual bool canUpdateLocalParameters() const { return true; }
0258 
0259   virtual void update(const LocalTrajectoryParameters& p,
0260                       const SurfaceType& aSurface,
0261                       const MagneticField* field,
0262                       const SurfaceSide side);
0263 
0264   // update in place and in the very same place
0265   virtual void update(const LocalTrajectoryParameters& p, const SurfaceSide side) final;
0266 
0267   virtual void update(double weight,
0268                       const LocalTrajectoryParameters& p,
0269                       const LocalTrajectoryError& err,
0270                       const SurfaceType& aSurface,
0271                       const MagneticField* field,
0272                       const SurfaceSide side);
0273 
0274   // update in place and in the very same place
0275   virtual void update(const LocalTrajectoryParameters& p,
0276                       const LocalTrajectoryError& err,
0277                       const SurfaceSide side) final;
0278 
0279   CurvilinearTrajectoryError& setCurvilinearError() { return theFreeState.setCurvilinearError(); }
0280 
0281 public:
0282   using Components = std::vector<TrajectoryStateOnSurface>;
0283   virtual Components const& components() const = 0;
0284   virtual bool singleState() const = 0;
0285 
0286 private:
0287   static void notValid();
0288 
0289   void missingError(char const* where) const;  // dso_internal;
0290 
0291   // create global errors from local
0292   void checkCurvilinError() const;  //  dso_internal;
0293 
0294   // create local parameters and errors from global
0295   void createLocalParameters() const;
0296   // create local errors from global
0297   void createLocalError() const;
0298   void createLocalErrorFromCurvilinearError() const dso_internal;
0299 
0300 private:
0301   mutable FreeTrajectoryState theFreeState;
0302 
0303   mutable LocalTrajectoryError theLocalError;
0304   mutable LocalTrajectoryParameters theLocalParameters;
0305 
0306   mutable bool theLocalParametersValid;
0307   mutable bool theValid;
0308 
0309   SurfaceSide theSurfaceSide;
0310   ConstReferenceCountingPointer<SurfaceType> theSurfaceP;
0311 
0312   double theWeight = 0.;
0313 };
0314 
0315 #endif