CMS 3D CMS Logo

MuonSegFit.h
Go to the documentation of this file.
1 #ifndef GEMSegment_MuonSegFit_h
2 #define GEMSegment_MuonSegFit_h
3 
4 // MuonSegFit.h - Segment fitting factored out of MuonRecHit segment builder based on
5 // CSCSegFit.h - Segment fitting factored out of CSC segment builders - Tim Cox
6 // Last mod: 15.04.2016 Jason Lee
7 
8 
9 /* This as an object which is initialized by a set of rechits (2 to 6) in a
10  * specific MuonRecHit and has the functionality to make a least squares fit
11  * to a straight line in 2-dim for those rechits.
12  * The covariance matrix and chi2 of the fit are calculated.
13  * The original code made use of CLHEP matrices but this version uses
14  * ROOT SMatrices because they are multithreading compatible.
15  * Because of this, the no. of rechits that can be handled is limited to
16  * a maximum of 6, one per layer of a Muon chamber. This means maximum dimensions
17  * can be specified at compile time and hence satisfies SMatrix constraints.
18  * This means that if at a later stage we would change the geometry to have
19  * for instance 10 detection layers, we will have to modify this code too.
20  * For 2 hits of course there is no fit - just draw a straight line between them.
21  * Details of the algorithm are in the .cc file
22  *
23  */
24 
26 
28 
29 #include <Math/Functions.h>
30 #include <Math/SVector.h>
31 #include <Math/SMatrix.h>
32 
33 #include <vector>
34 
35 class MuonSegFit {
36 
37  public:
38 
39  // TYPES
40  //typedef std::vector<const TrackingRecHit*> MuonRecHitContainer;
41  typedef std::shared_ptr<TrackingRecHit> MuonRecHitPtr;
42  typedef std::vector<MuonRecHitPtr> MuonRecHitContainer;
43 
44  static const int MaxHits2 = 22; // maxHit2 = 2*max hits
45  // 12 x12 Symmetric
46  typedef ROOT::Math::SMatrix<double,MaxHits2,MaxHits2,ROOT::Math::MatRepSym<double,MaxHits2> > SMatrixSym12;
47 
48  // 12 x 4
49  typedef ROOT::Math::SMatrix<double,MaxHits2,4 > SMatrix12by4;
50 
51  // 4 x 4 General + Symmetric
52  typedef ROOT::Math::SMatrix<double, 4 > SMatrix4;
53  typedef ROOT::Math::SMatrix<double,4,4,ROOT::Math::MatRepSym<double,4> > SMatrixSym4;
54 
55  // 2 x 2 Symmetric
56  typedef ROOT::Math::SMatrix<double,2,2,ROOT::Math::MatRepSym<double,2> > SMatrixSym2;
57 
58  // 4-dim vector
59  typedef ROOT::Math::SVector<double,4> SVector4;
60 
61 
62  // PUBLIC FUNCTIONS
63 
64  //@@ WANT OBJECT TO CACHE THE SET OF HITS SO CANNOT PASS BY REF
65  // NOTE - We need local position of a RecHit w.r.t. the CHAMBER
66  MuonSegFit(MuonRecHitContainer hits) : hits_( hits ), uslope_(.0), vslope_(.0),
67  chi2_(.0), ndof_(0), scaleXError_( 1.0 ), fitdone_( false ) {}
68 
69  virtual ~MuonSegFit() {}
70 
71  // Least-squares fit
72  bool fit( void ); // fill uslope_, vslope_, intercept_ @@ FKA fitSlopes()
73  // Calculate covariance matrix of fitted parameters
75 
76  // Change scale factor of rechit x error
77  // - expert use only!
78  void setScaleXError ( double factor ) { scaleXError_ = factor; }
79 
80  // Fit values
81  float xfit( float z ) const;
82  float yfit( float z ) const;
83 
84  // Deviations from fit for given input (local w.r.t. chamber)
85  float xdev( float x, float z ) const;
86  float ydev ( float y, float z ) const;
87  float Rdev( float x, float y, float z ) const;
88 
89  // Other public functions are accessors
90  MuonRecHitContainer hits(void) const { return hits_; }
91  double scaleXError(void) const { return scaleXError_; }
92  size_t nhits(void) const { return hits_.size(); }
93  double chi2(void) const { return chi2_; }
94  int ndof(void) const { return ndof_; }
95  LocalPoint intercept() const { return intercept_;}
96  LocalVector localdir() const { return localdir_;}
97  bool fitdone() const { return fitdone_; }
98 
99  private:
100 
101  // PRIVATE FUNCTIONS
102 
103  void fit2(void); // fit for 2 hits
104  void fitlsq(void); // least-squares fit for 3-6 hits
105  void setChi2(void); // fill chi2_ & ndof_ @@ FKA fillChiSquared()
106 
107 
108  protected:
109 
110  // PROTECTED FUNCTIONS - derived class needs access
111 
112  // Set segment direction 'out' from IP
113  void setOutFromIP(void); // fill localdir_ @@ FKA fillLocalDirection()
114 
115  SMatrix12by4 derivativeMatrix(void);
116  SMatrixSym12 weightMatrix(void);
117  AlgebraicSymMatrix flipErrors(const SMatrixSym4&);
118 
119  // PROTECTED MEMBER VARIABLES - derived class needs access
120 
121  MuonRecHitContainer hits_; //@@ FKA protoSegment
122  float uslope_; //@@ FKA protoSlope_u
123  float vslope_; //@@ FKA protoSlope_v
124  LocalPoint intercept_; //@@ FKA protoIntercept
125  LocalVector localdir_; //@@ FKA protoDirection
126  double chi2_; //@@ FKA protoChi2
127  int ndof_; //@@ FKA protoNDF, which was double!!
128  double scaleXError_;
129  bool fitdone_;
130 };
131 
132 #endif
133 
std::shared_ptr< TrackingRecHit > MuonRecHitPtr
Definition: MuonSegFit.h:41
static const int MaxHits2
Definition: MuonSegFit.h:44
AlgebraicSymMatrix covarianceMatrix(void)
Definition: MuonSegFit.cc:364
double scaleXError(void) const
Definition: MuonSegFit.h:91
LocalVector localdir_
Definition: MuonSegFit.h:125
double chi2_
Definition: MuonSegFit.h:126
float ydev(float y, float z) const
Definition: MuonSegFit.cc:468
float xfit(float z) const
Definition: MuonSegFit.cc:454
void fitlsq(void)
Definition: MuonSegFit.cc:80
ROOT::Math::SMatrix< double, 4, 4, ROOT::Math::MatRepSym< double, 4 > > SMatrixSym4
Definition: MuonSegFit.h:53
virtual ~MuonSegFit()
Definition: MuonSegFit.h:69
SMatrixSym12 weightMatrix(void)
Definition: MuonSegFit.cc:296
SMatrix12by4 derivativeMatrix(void)
Definition: MuonSegFit.cc:323
LocalPoint intercept_
Definition: MuonSegFit.h:124
float uslope_
Definition: MuonSegFit.h:122
float Rdev(float x, float y, float z) const
Definition: MuonSegFit.cc:472
ROOT::Math::SMatrix< double, MaxHits2, MaxHits2, ROOT::Math::MatRepSym< double, MaxHits2 > > SMatrixSym12
Definition: MuonSegFit.h:46
double chi2(void) const
Definition: MuonSegFit.h:93
MuonSegFit(MuonRecHitContainer hits)
Definition: MuonSegFit.h:66
float xdev(float x, float z) const
Definition: MuonSegFit.cc:464
LocalVector localdir() const
Definition: MuonSegFit.h:96
float vslope_
Definition: MuonSegFit.h:123
void setChi2(void)
Definition: MuonSegFit.cc:235
MuonRecHitContainer hits(void) const
Definition: MuonSegFit.h:90
std::vector< MuonRecHitPtr > MuonRecHitContainer
Definition: MuonSegFit.h:42
void setOutFromIP(void)
Definition: MuonSegFit.cc:344
int ndof(void) const
Definition: MuonSegFit.h:94
bool fitdone() const
Definition: MuonSegFit.h:97
void fit2(void)
Definition: MuonSegFit.cc:32
double scaleXError_
Definition: MuonSegFit.h:128
bool fitdone_
Definition: MuonSegFit.h:129
AlgebraicSymMatrix flipErrors(const SMatrixSym4 &)
Definition: MuonSegFit.cc:399
float yfit(float z) const
Definition: MuonSegFit.cc:460
LocalPoint intercept() const
Definition: MuonSegFit.h:95
MuonRecHitContainer hits_
Definition: MuonSegFit.h:121
ROOT::Math::SMatrix< double, 2, 2, ROOT::Math::MatRepSym< double, 2 > > SMatrixSym2
Definition: MuonSegFit.h:56
CLHEP::HepSymMatrix AlgebraicSymMatrix
ROOT::Math::SVector< double, 4 > SVector4
Definition: MuonSegFit.h:59
size_t nhits(void) const
Definition: MuonSegFit.h:92
void setScaleXError(double factor)
Definition: MuonSegFit.h:78
ROOT::Math::SMatrix< double, 4 > SMatrix4
Definition: MuonSegFit.h:52
ROOT::Math::SMatrix< double, MaxHits2, 4 > SMatrix12by4
Definition: MuonSegFit.h:49
bool fit(void)
Definition: MuonSegFit.cc:14