CMS 3D CMS Logo

CSCTimingExtractor.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: MuonIdentification
4 // Class: CSCTimingExtractor
5 //
11 //
12 // Original Author: Traczyk Piotr
13 // Created: Thu Oct 11 15:01:28 CEST 2007
14 //
15 //
16 
18 
19 
20 // user include files
23 
26 
28 
30 
33 
37 
45 
50 
51 
52 // system include files
53 #include <memory>
54 #include <vector>
55 #include <string>
56 #include <iostream>
57 
58 namespace edm {
59  class ParameterSet;
60  class EventSetup;
61  class InputTag;
62 }
63 
64 class MuonServiceProxy;
65 
66 //
67 // constructors and destructor
68 //
70  :
71  thePruneCut_(iConfig.getParameter<double>("PruneCut")),
72  theStripTimeOffset_(iConfig.getParameter<double>("CSCStripTimeOffset")),
73  theWireTimeOffset_(iConfig.getParameter<double>("CSCWireTimeOffset")),
74  theStripError_(iConfig.getParameter<double>("CSCStripError")),
75  theWireError_(iConfig.getParameter<double>("CSCWireError")),
76  UseWireTime(iConfig.getParameter<bool>("UseWireTime")),
77  UseStripTime(iConfig.getParameter<bool>("UseStripTime")),
78  debug(iConfig.getParameter<bool>("debug"))
79 {
80  edm::ParameterSet serviceParameters = iConfig.getParameter<edm::ParameterSet>("ServiceParameters");
81  theService = std::make_unique<MuonServiceProxy>(serviceParameters);
82  theMatcher = segMatcher;
83 }
84 
85 
87 {
88 }
89 
90 
91 //
92 // member functions
93 //
94 
96  const std::vector<const CSCSegment*> &segments,
97  reco::TrackRef muonTrack,
98  const edm::Event& iEvent, const edm::EventSetup& iSetup)
99 {
100  theService->update(iSetup);
101 
102  const GlobalTrackingGeometry *theTrackingGeometry = &*theService->trackingGeometry();
103 
104  // get the propagator
106  iSetup.get<TrackingComponentsRecord>().get("SteppingHelixPropagatorAny", propagator);
107  const Propagator *propag = propagator.product();
108 
109  math::XYZPoint pos=muonTrack->innerPosition();
110  math::XYZVector mom=muonTrack->innerMomentum();
111  if (sqrt(muonTrack->innerPosition().mag2()) > sqrt(muonTrack->outerPosition().mag2())){
112  pos=muonTrack->outerPosition();
113  mom=-1*muonTrack->outerMomentum();
114  }
115  GlobalPoint posp(pos.x(), pos.y(), pos.z());
116  GlobalVector momv(mom.x(), mom.y(), mom.z());
117  FreeTrajectoryState muonFTS(posp, momv, (TrackCharge)muonTrack->charge(), theService->magneticField().product());
118 
119  // create a collection on TimeMeasurements for the track
120  std::vector<TimeMeasurement> tms;
121  for (const auto& rechit : segments) {
122 
123  // Create the ChamberId
124  DetId id = rechit->geographicalId();
125  CSCDetId chamberId(id.rawId());
126  // int station = chamberId.station();
127 
128  if (rechit->specificRecHits().empty()) continue;
129 
130  const std::vector<CSCRecHit2D>& hits2d(rechit->specificRecHits());
131 
132  // store all the hits from the segment
133  for (const auto& hiti : hits2d) {
134 
135  const GeomDet* cscDet = theTrackingGeometry->idToDet(hiti.geographicalId());
136  TimeMeasurement thisHit;
137 
138  std::pair< TrajectoryStateOnSurface, double> tsos;
139  tsos=propag->propagateWithPath(muonFTS,cscDet->surface());
140 
141  double dist;
142  if (tsos.first.isValid()) dist = tsos.second+posp.mag();
143  else dist = cscDet->toGlobal(hiti.localPosition()).mag();
144 
145  thisHit.distIP = dist;
146  if (UseStripTime) {
147  thisHit.weightInvbeta = dist*dist/(theStripError_*theStripError_*30.*30.);
149  thisHit.timeCorr = hiti.tpeak()-theStripTimeOffset_;
150  tms.push_back(thisHit);
151  }
152 
153  if (UseWireTime) {
154  thisHit.weightInvbeta = dist*dist/(theWireError_*theWireError_*30.*30.);
156  thisHit.timeCorr = hiti.wireTime()-theWireTimeOffset_;
157  tms.push_back(thisHit);
158  }
159 
160 // std::cout << " CSC Hit. Dist= " << dist << " Time= " << thisHit.timeCorr
161 // << " invBeta= " << (1.+thisHit.timeCorr/dist*30.) << std::endl;
162  }
163 
164  } // rechit
165 
166  bool modified = false;
167  std::vector <double> dstnc, local_t0, hitWeightInvbeta, hitWeightTimeVtx;
168  double totalWeightInvbeta=0;
169  double totalWeightTimeVtx=0;
170 
171  // Now loop over the measurements, calculate 1/beta and cut away outliers
172  do {
173 
174  modified = false;
175  dstnc.clear();
176  local_t0.clear();
177  hitWeightInvbeta.clear();
178  hitWeightTimeVtx.clear();
179 
180  totalWeightInvbeta=0;
181  totalWeightTimeVtx=0;
182 
183  for (auto& tm : tms) {
184  dstnc.push_back(tm.distIP);
185  local_t0.push_back(tm.timeCorr);
186  hitWeightInvbeta.push_back(tm.weightInvbeta);
187  hitWeightTimeVtx.push_back(tm.weightTimeVtx);
188  totalWeightInvbeta+=tm.weightInvbeta;
189  totalWeightTimeVtx+=tm.weightTimeVtx;
190  }
191 
192  if (totalWeightInvbeta==0) break;
193 
194  // calculate the value and error of 1/beta and timeVtx from the complete set of 1D hits
195  if (debug)
196  std::cout << " Points for global fit: " << dstnc.size() << std::endl;
197 
198  double invbeta=0,invbetaErr=0;
199  double timeVtx=0,timeVtxErr=0;
200 
201  for (unsigned int i=0;i<dstnc.size();i++) {
202  invbeta+=(1.+local_t0.at(i)/dstnc.at(i)*30.)*hitWeightInvbeta.at(i)/totalWeightInvbeta;
203  timeVtx+=local_t0.at(i)*hitWeightTimeVtx.at(i)/totalWeightTimeVtx;
204  }
205 
206  double chimax=0.;
207  std::vector<TimeMeasurement>::iterator tmmax;
208 
209  // Calculate the inv beta and time at vertex dispersion
210  double diff_ibeta,diff_tvtx;
211  for (unsigned int i=0;i<dstnc.size();i++) {
212  diff_ibeta=(1.+local_t0.at(i)/dstnc.at(i)*30.)-invbeta;
213  diff_ibeta=diff_ibeta*diff_ibeta*hitWeightInvbeta.at(i);
214  diff_tvtx=local_t0.at(i)-timeVtx;
215  diff_tvtx=diff_tvtx*diff_tvtx*hitWeightTimeVtx.at(i);
216  invbetaErr+=diff_ibeta;
217  timeVtxErr+=diff_tvtx;
218 
219  // decide if we cut away time at vertex outliers or inverse beta outliers
220  // currently not configurable.
221  if (diff_tvtx>chimax) {
222  tmmax=tms.begin()+i;
223  chimax=diff_tvtx;
224  }
225  }
226 
227  // cut away the outliers
228  if (chimax>thePruneCut_) {
229  tms.erase(tmmax);
230  modified=true;
231  }
232 
233  if (debug) {
234  double cf = 1./(dstnc.size()-1);
235  invbetaErr=sqrt(invbetaErr/totalWeightInvbeta*cf);
236  timeVtxErr=sqrt(timeVtxErr/totalWeightTimeVtx*cf);
237  std::cout << " Measured 1/beta: " << invbeta << " +/- " << invbetaErr << std::endl;
238  std::cout << " Measured time: " << timeVtx << " +/- " << timeVtxErr << std::endl;
239  }
240 
241  } while (modified);
242 
243  for (unsigned int i=0;i<dstnc.size();i++) {
244  tmSequence.dstnc.push_back(dstnc.at(i));
245  tmSequence.local_t0.push_back(local_t0.at(i));
246  tmSequence.weightInvbeta.push_back(hitWeightInvbeta.at(i));
247  tmSequence.weightTimeVtx.push_back(hitWeightTimeVtx.at(i));
248  }
249 
250  tmSequence.totalWeightInvbeta=totalWeightInvbeta;
251  tmSequence.totalWeightTimeVtx=totalWeightTimeVtx;
252 }
253 
254 
255 // ------------ method called to produce the data ------------
256 void
258  const edm::Event& iEvent, const edm::EventSetup& iSetup)
259 {
260 
261  if (debug)
262  std::cout << " *** CSC Timimng Extractor ***" << std::endl;
263 
264  // get the CSC segments that were used to construct the muon
265  std::vector<const CSCSegment*> range = theMatcher->matchCSC(*muonTrack,iEvent);
266 
267  fillTiming(tmSequence, range, muonTrack, iEvent, iSetup);
268 }
269 
270 //define this as a plug-in
271 //DEFINE_FWK_MODULE(CSCTimingExtractor);
T getParameter(std::string const &) const
std::vector< double > local_t0
T mag() const
The vector magnitude. Equivalent to sqrt(vec.mag2())
GlobalPoint toGlobal(const Local2DPoint &lp) const
Conversion to the global R.F. from the R.F. of the GeomDet.
Definition: GeomDet.h:54
std::vector< const CSCSegment * > matchCSC(const reco::Track &muon, const edm::Event &event)
const Plane & surface() const
The nominal surface of the GeomDet.
Definition: GeomDet.h:42
void fillTiming(TimeMeasurementSequence &tmSequence, const std::vector< const CSCSegment * > &segments, reco::TrackRef muonTrack, const edm::Event &iEvent, const edm::EventSetup &iSetup)
CSCTimingExtractor(const edm::ParameterSet &, MuonSegmentMatcher *segMatcher)
Constructor.
std::unique_ptr< MuonServiceProxy > theService
int TrackCharge
Definition: TrackCharge.h:4
int iEvent
Definition: GenABIO.cc:230
MuonSegmentMatcher * theMatcher
T sqrt(T t)
Definition: SSEVec.h:18
virtual std::pair< TrajectoryStateOnSurface, double > propagateWithPath(const FreeTrajectoryState &, const Surface &) const final
Definition: Propagator.cc:15
edm::ESHandle< GlobalTrackingGeometry > theTrackingGeometry
std::vector< double > weightInvbeta
Definition: DetId.h:18
#define debug
Definition: HDRShower.cc:19
XYZVectorD XYZVector
spatial vector with cartesian internal representation
Definition: Vector3D.h:30
XYZPointD XYZPoint
point in space with cartesian internal representation
Definition: Point3D.h:12
std::vector< double > weightTimeVtx
HLT enums.
T get() const
Definition: EventSetup.h:63
const GeomDet * idToDet(DetId) const override
std::vector< double > dstnc
T const * product() const
Definition: ESHandle.h:86
~CSCTimingExtractor()
Destructor.