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