CMS 3D CMS Logo

GEMCSCSegFit.h
Go to the documentation of this file.
1 #ifndef GEMCSCSegment_GEMCSCSegFit_h
2 #define GEMCSCSegment_GEMCSCSegFit_h
3 
4 // GEMCSCSegFit.h extension to fit both CSCHits and GEMHits - Piet Verwilligen
5 // Created: 21.04.2015
6 
7 /* Work with general Tracking Rechits, such that GEM and CSC rechits
8  * can be treated at the same level. In case needed the Tracking Rechit can be
9  * cast to the CSCRecHit2D or the GEMRecHit for access to more specific info.
10  */
11 
12 // CSCSegFit.h - Segment fitting factored out of CSC segment builders - Tim Cox
13 // Last mod: 03.02.2015
14 
15 /* This as an object which is initialized by a set of rechits (2 to 6) in a
16  * specific CSC and has the functionality to make a least squares fit to a
17  * straight line in 2-dim for those rechits.
18  * The covariance matrix and chi2 of the fit are calculated.
19  * The original code made use of CLHEP matrices but this version uses
20  * ROOT SMatrices because they are multithreading compatible.
21  * Because of this, the no. of rechits that can be handled is limited to
22  * a maximum of 6, one per layer of a CSC. This means maximum dimensions
23  * can be specified at compile time and hence satisfies SMatrix constraints.
24  * For 2 hits of course there is no fit - just draw a straight line between them.
25  * Details of the algorithm are in the .cc file
26  *
27  */
28 
33 
37 
40 
41 #include <Math/Functions.h>
42 #include <Math/SVector.h>
43 #include <Math/SMatrix.h>
44 
45 #include <vector>
46 
47 class GEMCSCSegFit {
48 public:
49  // TYPES
50 
51  // 16 x 16 Symmetric
52  typedef ROOT::Math::SMatrix<double, 16, 16, ROOT::Math::MatRepSym<double, 16> > SMatrixSym16;
53 
54  // 16 x 4
55  typedef ROOT::Math::SMatrix<double, 16, 4> SMatrix16by4;
56 
57  // 4 x 4 General + Symmetric
58  typedef ROOT::Math::SMatrix<double, 4> SMatrix4;
59  typedef ROOT::Math::SMatrix<double, 4, 4, ROOT::Math::MatRepSym<double, 4> > SMatrixSym4;
60 
61  // 2 x 2 Symmetric
62  typedef ROOT::Math::SMatrix<double, 2, 2, ROOT::Math::MatRepSym<double, 2> > SMatrixSym2;
63 
64  // 4-dim vector
65  typedef ROOT::Math::SVector<double, 4> SVector4;
66 
67  // PUBLIC FUNCTIONS
68 
69  //@@ WANT OBJECT TO CACHE THE SET OF HITS SO CANNOT PASS BY REF
70  GEMCSCSegFit(std::map<uint32_t, const CSCLayer*> csclayermap,
71  std::map<uint32_t, const GEMEtaPartition*> gemrollmap,
72  const std::vector<const TrackingRecHit*> hits)
73  : csclayermap_(csclayermap),
74  gemetapartmap_(gemrollmap),
75  hits_(hits),
76  scaleXError_(1.0),
78  fitdone_(false) {
79  // --- LogDebug info about reading of CSC Layer map and GEM Eta Partition map -----------------------
80  edm::LogVerbatim("GEMCSCSegFit") << "[GEMCSCSegFit::ctor] cached the csclayermap and the gemrollmap";
81 
82  // --- LogDebug for CSC Layer map -------------------------------------------------------------------
83  std::stringstream csclayermapss;
84  csclayermapss << "[GEMCSCSegFit::ctor] :: csclayermap :: elements [" << std::endl;
85  for (std::map<uint32_t, const CSCLayer*>::const_iterator mapIt = csclayermap_.begin(); mapIt != csclayermap_.end();
86  ++mapIt) {
87  csclayermapss << "[CSC DetId " << mapIt->first << " =" << CSCDetId(mapIt->first) << ", CSC Layer "
88  << mapIt->second << " =" << (mapIt->second)->id() << "]," << std::endl;
89  }
90  csclayermapss << "]" << std::endl;
91  std::string csclayermapstr = csclayermapss.str();
92  edm::LogVerbatim("GEMCSCSegFit") << csclayermapstr;
93  // --- End LogDebug -----------------------------------------------------------------------------------
94 
95  // --- LogDebug for GEM Eta Partition map ------------------------------------------------------------
96  std::stringstream gemetapartmapss;
97  gemetapartmapss << "[GEMCSCSegFit::ctor] :: gemetapartmap :: elements [" << std::endl;
98  for (std::map<uint32_t, const GEMEtaPartition*>::const_iterator mapIt = gemetapartmap_.begin();
99  mapIt != gemetapartmap_.end();
100  ++mapIt) {
101  gemetapartmapss << "[GEM DetId " << mapIt->first << " =" << GEMDetId(mapIt->first) << ", GEM EtaPart "
102  << mapIt->second << "]," << std::endl;
103  }
104  gemetapartmapss << "]" << std::endl;
105  std::string gemetapartmapstr = gemetapartmapss.str();
106  edm::LogVerbatim("GEMCSCSegFit") << gemetapartmapstr;
107  // --- End LogDebug -----------------------------------------------------------------------------------
108  }
109 
110  virtual ~GEMCSCSegFit() {}
111 
112  // Least-squares fit
113  void fit(void); // fill uslope_, vslope_, intercept_ @@ FKA fitSlopes()
114  // Calculate covariance matrix of fitted parameters
116 
117  // Change scale factor of rechit x error
118  // - expert use only!
120 
121  // Fit values
122  float xfit(float z) const;
123  float yfit(float z) const;
124 
125  // Deviations from fit for given input (local w.r.t. chamber)
126  float xdev(float x, float z) const;
127  float ydev(float y, float z) const;
128  float Rdev(float x, float y, float z) const;
129 
130  // Other public functions are accessors
131  std::vector<const TrackingRecHit*> hits(void) const { return hits_; }
132  double scaleXError(void) const { return scaleXError_; }
133  size_t nhits(void) const { return hits_.size(); }
134  double chi2(void) const { return chi2_; }
135  int ndof(void) const { return ndof_; }
136  LocalPoint intercept() const { return intercept_; }
137  LocalVector localdir() const { return localdir_; }
138 
139  const CSCChamber* cscchamber(uint32_t id) const {
140  // Test whether id is found
141  if (csclayermap_.find(id) == csclayermap_.end()) { // id is not found
142  throw cms::Exception("InvalidDetId") << "[GEMCSCSegFit] Failed to find CSCChamber in CSCLayerMap" << std::endl;
143  } // chamber is not found and exception is thrown
144  else { // id is found
145  return (csclayermap_.find(id)->second)->chamber();
146  } // chamber found and returned
147  }
148  const CSCLayer* csclayer(uint32_t id) const {
149  if (csclayermap_.find(id) == csclayermap_.end()) {
150  throw cms::Exception("InvalidDetId") << "[GEMCSCSegFit] Failed to find CSCLayer in CSCLayerMap" << std::endl;
151  } else {
152  return csclayermap_.find(id)->second;
153  }
154  }
155  const GEMEtaPartition* gemetapartition(uint32_t id) const {
156  if (gemetapartmap_.find(id) == gemetapartmap_.end()) {
157  throw cms::Exception("InvalidDetId")
158  << "[GEMCSCSegFit] Failed to find GEMEtaPartition in GEMEtaPartMap" << std::endl;
159  } else {
160  return gemetapartmap_.find(id)->second;
161  }
162  }
163  const CSCChamber* refcscchamber() const {
164  if (csclayermap_.find(refid_) == csclayermap_.end()) {
165  throw cms::Exception("InvalidDetId")
166  << "[GEMCSCSegFit] Failed to find Reference CSCChamber in CSCLayerMap" << std::endl;
167  } else {
168  return (csclayermap_.find(refid_)->second)->chamber();
169  }
170  }
171  bool fitdone() const { return fitdone_; }
172 
173 private:
174  // PRIVATE FUNCTIONS
175 
176  void fit2(void); // fit for 2 hits
177  void fitlsq(void); // least-squares fit for 3-6 hits
178  void setChi2(void); // fill chi2_ & ndof_ @@ FKA fillChiSquared()
179 
180 protected:
181  // PROTECTED FUNCTIONS - derived class needs access
182 
183  // Set segment direction 'out' from IP
184  void setOutFromIP(void); // fill localdir_ @@ FKA fillLocalDirection()
185 
186  SMatrix16by4 derivativeMatrix(void);
187  SMatrixSym16 weightMatrix(void);
188  AlgebraicSymMatrix flipErrors(const SMatrixSym4&);
189 
190  // PROTECTED MEMBER VARIABLES - derived class needs access
191 
192  std::map<uint32_t, const CSCLayer*> csclayermap_;
193  std::map<uint32_t, const GEMEtaPartition*> gemetapartmap_;
195 
196  std::vector<const TrackingRecHit*> hits_; //@@ FKA protoSegment
197  float uslope_; //@@ FKA protoSlope_u
198  float vslope_; //@@ FKA protoSlope_v
199  LocalPoint intercept_; //@@ FKA protoIntercept
200  LocalVector localdir_; //@@ FKA protoDirection
201  double chi2_; //@@ FKA protoChi2
202  int ndof_; //@@ FKA protoNDF, which was double!!
203  double scaleXError_;
204  uint32_t refid_;
205  bool fitdone_;
206  // order needs to be the same here when filled by the constructor
207 };
208 
209 #endif
void setChi2(void)
SMatrix16by4 derivativeMatrix(void)
void setScaleXError(double factor)
Definition: GEMCSCSegFit.h:119
float xdev(float x, float z) const
std::vector< const TrackingRecHit * > hits_
Definition: GEMCSCSegFit.h:196
float ydev(float y, float z) const
const CSCLayer * csclayer(uint32_t id) const
Definition: GEMCSCSegFit.h:148
uint32_t refid_
Definition: GEMCSCSegFit.h:204
double scaleXError_
Definition: GEMCSCSegFit.h:203
float Rdev(float x, float y, float z) const
std::map< uint32_t, const GEMEtaPartition * > gemetapartmap_
Definition: GEMCSCSegFit.h:193
void fitlsq(void)
float xfit(float z) const
size_t nhits(void) const
Definition: GEMCSCSegFit.h:133
void fit(void)
Definition: GEMCSCSegFit.cc:19
ROOT::Math::SVector< double, 4 > SVector4
Definition: GEMCSCSegFit.h:65
LocalVector localdir() const
Definition: GEMCSCSegFit.h:137
double scaleXError(void) const
Definition: GEMCSCSegFit.h:132
AlgebraicSymMatrix covarianceMatrix(void)
const CSCChamber * refcscchamber() const
Definition: GEMCSCSegFit.h:163
LocalVector localdir_
Definition: GEMCSCSegFit.h:200
ROOT::Math::SMatrix< double, 2, 2, ROOT::Math::MatRepSym< double, 2 > > SMatrixSym2
Definition: GEMCSCSegFit.h:62
std::vector< const TrackingRecHit * > hits(void) const
Definition: GEMCSCSegFit.h:131
float yfit(float z) const
AlgebraicSymMatrix flipErrors(const SMatrixSym4 &)
ROOT::Math::SMatrix< double, 16, 4 > SMatrix16by4
Definition: GEMCSCSegFit.h:55
ROOT::Math::SMatrix< double, 4 > SMatrix4
Definition: GEMCSCSegFit.h:58
int ndof(void) const
Definition: GEMCSCSegFit.h:135
bool fitdone() const
Definition: GEMCSCSegFit.h:171
double chi2(void) const
Definition: GEMCSCSegFit.h:134
#define begin
Definition: vmac.h:32
LocalPoint intercept_
Definition: GEMCSCSegFit.h:199
void setOutFromIP(void)
virtual ~GEMCSCSegFit()
Definition: GEMCSCSegFit.h:110
CLHEP::HepSymMatrix AlgebraicSymMatrix
SMatrixSym16 weightMatrix(void)
ROOT::Math::SMatrix< double, 16, 16, ROOT::Math::MatRepSym< double, 16 > > SMatrixSym16
Definition: GEMCSCSegFit.h:52
std::map< uint32_t, const CSCLayer * > csclayermap_
Definition: GEMCSCSegFit.h:192
ROOT::Math::SMatrix< double, 4, 4, ROOT::Math::MatRepSym< double, 4 > > SMatrixSym4
Definition: GEMCSCSegFit.h:59
LocalPoint intercept() const
Definition: GEMCSCSegFit.h:136
const CSCChamber * refcscchamber_
Definition: GEMCSCSegFit.h:194
GEMCSCSegFit(std::map< uint32_t, const CSCLayer * > csclayermap, std::map< uint32_t, const GEMEtaPartition * > gemrollmap, const std::vector< const TrackingRecHit * > hits)
Definition: GEMCSCSegFit.h:70
const CSCChamber * cscchamber(uint32_t id) const
Definition: GEMCSCSegFit.h:139
const GEMEtaPartition * gemetapartition(uint32_t id) const
Definition: GEMCSCSegFit.h:155
void fit2(void)
Definition: GEMCSCSegFit.cc:44