CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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 // $Id: CSCTimingExtractor.cc,v 1.10 2011/04/16 20:30:57 ptraczyk Exp $
15 //
16 //
17 
19 
20 
21 // user include files
24 
27 
29 
31 
34 
38 
46 
51 
52 
53 // system include files
54 #include <memory>
55 #include <vector>
56 #include <string>
57 #include <iostream>
58 
59 namespace edm {
60  class ParameterSet;
61  class EventSetup;
62  class InputTag;
63 }
64 
65 class MuonServiceProxy;
66 
67 //
68 // constructors and destructor
69 //
71  :
72  CSCSegmentTags_(iConfig.getParameter<edm::InputTag>("CSCsegments")),
73  thePruneCut_(iConfig.getParameter<double>("PruneCut")),
74  theStripTimeOffset_(iConfig.getParameter<double>("CSCStripTimeOffset")),
75  theWireTimeOffset_(iConfig.getParameter<double>("CSCWireTimeOffset")),
76  theStripError_(iConfig.getParameter<double>("CSCStripError")),
77  theWireError_(iConfig.getParameter<double>("CSCWireError")),
78  UseWireTime(iConfig.getParameter<bool>("UseWireTime")),
79  UseStripTime(iConfig.getParameter<bool>("UseStripTime")),
80  debug(iConfig.getParameter<bool>("debug"))
81 {
82  edm::ParameterSet serviceParameters = iConfig.getParameter<edm::ParameterSet>("ServiceParameters");
83  theService = new MuonServiceProxy(serviceParameters);
84 
85  edm::ParameterSet matchParameters = iConfig.getParameter<edm::ParameterSet>("MatchParameters");
86 
87  theMatcher = new MuonSegmentMatcher(matchParameters, theService);
88 }
89 
90 
92 {
93  if (theService) delete theService;
94  if (theMatcher) delete theMatcher;
95 }
96 
97 
98 //
99 // member functions
100 //
101 
102 // ------------ method called to produce the data ------------
103 void
105 {
106 
107  if (debug)
108  std::cout << " *** CSC Timimng Extractor ***" << std::endl;
109 
110  theService->update(iSetup);
111 
113 
115  iSetup.get<TrackingComponentsRecord>().get("SteppingHelixPropagatorAny", propagator);
116  const Propagator *propag = propagator.product();
117 
118  double invbeta=0;
119  double invbetaerr=0;
120  double totalWeightInvbeta=0;
121  double totalWeightVertex=0;
122  std::vector<TimeMeasurement> tms;
123 
124  math::XYZPoint pos=muonTrack->innerPosition();
125  math::XYZVector mom=muonTrack->innerMomentum();
126 
127  if (sqrt(muonTrack->innerPosition().mag2()) > sqrt(muonTrack->outerPosition().mag2())){
128  pos=muonTrack->outerPosition();
129  mom=-1*muonTrack->outerMomentum();
130  }
131 
132  GlobalPoint posp(pos.x(), pos.y(), pos.z());
133  GlobalVector momv(mom.x(), mom.y(), mom.z());
134  FreeTrajectoryState muonFTS(posp, momv, (TrackCharge)muonTrack->charge(), theService->magneticField().product());
135 
136  // get the CSC segments that were used to construct the muon
137  std::vector<const CSCSegment*> range = theMatcher->matchCSC(*muonTrack,iEvent);
138 
139  // create a collection on TimeMeasurements for the track
140  for (std::vector<const CSCSegment*>::iterator rechit = range.begin(); rechit!=range.end();++rechit) {
141 
142  // Create the ChamberId
143  DetId id = (*rechit)->geographicalId();
144  CSCDetId chamberId(id.rawId());
145  // int station = chamberId.station();
146 
147  if (!(*rechit)->specificRecHits().size()) continue;
148 
149  const std::vector<CSCRecHit2D> hits2d = (*rechit)->specificRecHits();
150 
151  // store all the hits from the segment
152  for (std::vector<CSCRecHit2D>::const_iterator hiti=hits2d.begin(); hiti!=hits2d.end(); hiti++) {
153 
154  const GeomDet* cscDet = theTrackingGeometry->idToDet(hiti->geographicalId());
155  TimeMeasurement thisHit;
156 
157  std::pair< TrajectoryStateOnSurface, double> tsos;
158  tsos=propag->propagateWithPath(muonFTS,cscDet->surface());
159 
160  double dist;
161  if (tsos.first.isValid()) dist = tsos.second+posp.mag();
162  else dist = cscDet->toGlobal(hiti->localPosition()).mag();
163 
164  thisHit.distIP = dist;
165  if (UseStripTime) {
166  thisHit.weightInvbeta = dist*dist/(theStripError_*theStripError_*30.*30.);
168  thisHit.timeCorr = hiti->tpeak()-theStripTimeOffset_;
169  tms.push_back(thisHit);
170  }
171 
172  if (UseWireTime) {
173  thisHit.weightInvbeta = dist*dist/(theWireError_*theWireError_*30.*30.);
175  thisHit.timeCorr = hiti->wireTime()-theWireTimeOffset_;
176  tms.push_back(thisHit);
177  }
178 
179 
180 // std::cout << " CSC Hit. Dist= " << dist << " Time= " << thisHit.timeCorr
181 // << " invBeta= " << (1.+thisHit.timeCorr/dist*30.) << std::endl;
182  }
183 
184  } // rechit
185 
186  bool modified = false;
187  std::vector <double> dstnc, dsegm, dtraj, hitWeightInvbeta, hitWeightVertex;
188 
189  // Now loop over the measurements, calculate 1/beta and cut away outliers
190  do {
191 
192  modified = false;
193  dstnc.clear();
194  dsegm.clear();
195  dtraj.clear();
196  hitWeightInvbeta.clear();
197  hitWeightVertex.clear();
198 
199  totalWeightInvbeta=0;
200  totalWeightVertex=0;
201 
202  for (std::vector<TimeMeasurement>::iterator tm=tms.begin(); tm!=tms.end(); ++tm) {
203  dstnc.push_back(tm->distIP);
204  dsegm.push_back(tm->timeCorr);
205  hitWeightInvbeta.push_back(tm->weightInvbeta);
206  hitWeightVertex.push_back(tm->weightVertex);
207  totalWeightInvbeta+=tm->weightInvbeta;
208  totalWeightVertex+=tm->weightVertex;
209  }
210 
211  if (totalWeightInvbeta==0) break;
212 
213  // calculate the value and error of 1/beta from the complete set of 1D hits
214  if (debug)
215  std::cout << " Points for global fit: " << dstnc.size() << std::endl;
216 
217  // inverse beta - weighted average of the contributions from individual hits
218  invbeta=0;
219  for (unsigned int i=0;i<dstnc.size();i++)
220  invbeta+=(1.+dsegm.at(i)/dstnc.at(i)*30.)*hitWeightInvbeta.at(i)/totalWeightInvbeta;
221 
222  double chimax=0.;
223  std::vector<TimeMeasurement>::iterator tmmax;
224 
225  // the dispersion of inverse beta
226  double diff;
227  for (unsigned int i=0;i<dstnc.size();i++) {
228  diff=(1.+dsegm.at(i)/dstnc.at(i)*30.)-invbeta;
229  diff=diff*diff*hitWeightInvbeta.at(i);
230  invbetaerr+=diff;
231  if (diff>chimax) {
232  tmmax=tms.begin()+i;
233  chimax=diff;
234  }
235  }
236 
237  invbetaerr=sqrt(invbetaerr/totalWeightInvbeta);
238 
239  // cut away the outliers
240  if (chimax>thePruneCut_) {
241  tms.erase(tmmax);
242  modified=true;
243  }
244 
245  if (debug)
246  std::cout << " Measured 1/beta: " << invbeta << " +/- " << invbetaerr << std::endl;
247 
248  } while (modified);
249 
250  // std::cout << " *** FINAL Measured 1/beta: " << invbeta << " +/- " << invbetaerr << std::endl;
251 
252  for (unsigned int i=0;i<dstnc.size();i++) {
253  tmSequence.dstnc.push_back(dstnc.at(i));
254  tmSequence.local_t0.push_back(dsegm.at(i));
255  tmSequence.weightInvbeta.push_back(hitWeightInvbeta.at(i));
256  tmSequence.weightVertex.push_back(hitWeightVertex.at(i));
257  }
258 
259  tmSequence.totalWeightInvbeta=totalWeightInvbeta;
260  tmSequence.totalWeightVertex=totalWeightVertex;
261 
262 }
263 
264 //define this as a plug-in
265 //DEFINE_FWK_MODULE(CSCTimingExtractor);
void update(const edm::EventSetup &setup)
update the services each event
T getParameter(std::string const &) const
int i
Definition: DBlmapReader.cc:9
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:47
std::vector< const CSCSegment * > matchCSC(const reco::Track &muon, const edm::Event &event)
CSCTimingExtractor(const edm::ParameterSet &)
Constructor.
edm::ESHandle< MagneticField > magneticField() const
get the magnetic field
int TrackCharge
Definition: TrackCharge.h:4
virtual const GeomDet * idToDet(DetId) const
virtual std::pair< TrajectoryStateOnSurface, double > propagateWithPath(const FreeTrajectoryState &, const Surface &) const
Definition: Propagator.cc:77
int iEvent
Definition: GenABIO.cc:243
MuonSegmentMatcher * theMatcher
T sqrt(T t)
Definition: SSEVec.h:46
edm::ESHandle< GlobalTrackingGeometry > theTrackingGeometry
std::vector< double > weightInvbeta
Definition: DetId.h:20
XYZVectorD XYZVector
spatial vector with cartesian internal representation
Definition: Vector3D.h:31
XYZPointD XYZPoint
point in space with cartesian internal representation
Definition: Point3D.h:13
const T & get() const
Definition: EventSetup.h:55
T const * product() const
Definition: ESHandle.h:62
void fillTiming(TimeMeasurementSequence &tmSequence, reco::TrackRef muonTrack, const edm::Event &iEvent, const edm::EventSetup &iSetup)
edm::ESHandle< GlobalTrackingGeometry > trackingGeometry() const
get the tracking geometry
const BoundPlane & surface() const
The nominal surface of the GeomDet.
Definition: GeomDet.h:35
MuonServiceProxy * theService
tuple cout
Definition: gather_cfg.py:121
std::vector< double > dstnc
#define debug
Definition: MEtoEDMFormat.h:34
std::vector< double > weightVertex
~CSCTimingExtractor()
Destructor.