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.4 2010/07/01 08:48:10 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  theTimeOffset_(iConfig.getParameter<double>("CSCTimeOffset")),
75  debug(iConfig.getParameter<bool>("debug"))
76 {
77  edm::ParameterSet serviceParameters = iConfig.getParameter<edm::ParameterSet>("ServiceParameters");
78  theService = new MuonServiceProxy(serviceParameters);
79 
80  edm::ParameterSet matchParameters = iConfig.getParameter<edm::ParameterSet>("MatchParameters");
81 
82  theMatcher = new MuonSegmentMatcher(matchParameters, theService);
83 }
84 
85 
87 {
88  if (theService) delete theService;
89  if (theMatcher) delete theMatcher;
90 }
91 
92 
93 //
94 // member functions
95 //
96 
97 // ------------ method called to produce the data ------------
98 void
100 {
101 
102  if (debug)
103  std::cout << " *** CSC Timimng Extractor ***" << std::endl;
104 
105  theService->update(iSetup);
106 
108 
110  iSetup.get<TrackingComponentsRecord>().get("SteppingHelixPropagatorAny", propagator);
111  const Propagator *propag = propagator.product();
112 
113  double invbeta=0;
114  double invbetaerr=0;
115  int totalWeight=0;
116  std::vector<TimeMeasurement> tms;
117 
118  math::XYZPoint pos=muonTrack->innerPosition();
119  math::XYZVector mom=muonTrack->innerMomentum();
120  GlobalPoint posp(pos.x(), pos.y(), pos.z());
121  GlobalVector momv(mom.x(), mom.y(), mom.z());
122  FreeTrajectoryState muonFTS(posp, momv, (TrackCharge)muonTrack->charge(), theService->magneticField().product());
123 
124  // get the CSC segments that were used to construct the muon
125  std::vector<const CSCSegment*> range = theMatcher->matchCSC(*muonTrack,iEvent);
126 
127  // create a collection on TimeMeasurements for the track
128  for (std::vector<const CSCSegment*>::iterator rechit = range.begin(); rechit!=range.end();++rechit) {
129 
130  // Create the ChamberId
131  DetId id = (*rechit)->geographicalId();
132  CSCDetId chamberId(id.rawId());
133  int station = chamberId.station();
134 
135  if (!(*rechit)->specificRecHits().size()) continue;
136 
137  const std::vector<CSCRecHit2D> hits2d = (*rechit)->specificRecHits();
138 
139  // store all the hits from the segment
140  for (std::vector<CSCRecHit2D>::const_iterator hiti=hits2d.begin(); hiti!=hits2d.end(); hiti++) {
141 
142  const GeomDet* cscDet = theTrackingGeometry->idToDet(hiti->geographicalId());
143  TimeMeasurement thisHit;
144 
145  std::pair< TrajectoryStateOnSurface, double> tsos;
146  tsos=propag->propagateWithPath(muonFTS,cscDet->surface());
147 
148  double dist;
149  if (tsos.first.isValid()) dist = tsos.second+posp.mag();
150  else dist = cscDet->toGlobal(hiti->localPosition()).mag();
151 
152  thisHit.distIP = dist;
153  thisHit.station = station;
154  thisHit.timeCorr = hiti->tpeak()+theTimeOffset_;
155  tms.push_back(thisHit);
156 
157 // std::cout << " CSC Hit. Dist= " << dist << " Time= " << thisHit.timeCorr
158 // << " invBeta= " << (1.+thisHit.timeCorr/dist*30.) << std::endl;
159  }
160 
161  } // rechit
162 
163  bool modified = false;
164  std::vector <double> dstnc, dsegm, dtraj, hitWeight;
165 
166  // Now loop over the measurements, calculate 1/beta and cut away outliers
167  do {
168 
169  modified = false;
170  dstnc.clear();
171  dsegm.clear();
172  dtraj.clear();
173  hitWeight.clear();
174 
175  totalWeight=0;
176 
177  for (std::vector<TimeMeasurement>::iterator tm=tms.begin(); tm!=tms.end(); ++tm) {
178  dstnc.push_back(tm->distIP);
179  dsegm.push_back(tm->timeCorr);
180  hitWeight.push_back(1.);
181  totalWeight++;
182  }
183 
184  if (totalWeight==0) break;
185 
186  // calculate the value and error of 1/beta from the complete set of 1D hits
187  if (debug)
188  std::cout << " Points for global fit: " << dstnc.size() << std::endl;
189 
190  // inverse beta - weighted average of the contributions from individual hits
191  invbeta=0;
192  for (unsigned int i=0;i<dstnc.size();i++)
193  invbeta+=(1.+dsegm.at(i)/dstnc.at(i)*30.)*hitWeight.at(i)/totalWeight;
194 
195  double chimax=0.;
196  std::vector<TimeMeasurement>::iterator tmmax;
197 
198  // the dispersion of inverse beta
199  double diff;
200  for (unsigned int i=0;i<dstnc.size();i++) {
201  diff=(1.+dsegm.at(i)/dstnc.at(i)*30.)-invbeta;
202  diff=diff*diff*hitWeight.at(i);
203  invbetaerr+=diff;
204  if (diff/totalWeight>chimax) {
205  tmmax=tms.begin()+i;
206  chimax=diff;
207  }
208  }
209 
210  invbetaerr=sqrt(invbetaerr/totalWeight);
211 
212  // cut away the outliers
213  if (chimax>thePruneCut_) {
214  tms.erase(tmmax);
215  modified=true;
216  }
217 
218  if (debug)
219  std::cout << " Measured 1/beta: " << invbeta << " +/- " << invbetaerr << std::endl;
220 
221  } while (modified);
222 
223  // std::cout << " *** FINAL Measured 1/beta: " << invbeta << " +/- " << invbetaerr << std::endl;
224 
225  for (unsigned int i=0;i<dstnc.size();i++) {
226  tmSequence.dstnc.push_back(dstnc.at(i));
227  tmSequence.local_t0.push_back(dsegm.at(i));
228  tmSequence.weight.push_back(hitWeight.at(i));
229  }
230 
231  tmSequence.totalWeight=totalWeight;
232 
233 }
234 
235 //define this as a plug-in
236 //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
GlobalPoint toGlobal(const Local2DPoint &lp) const
Conversion to the global R.F. from the R.F. of the GeomDet.
Definition: GeomDet.h:49
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
std::vector< double > weight
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:74
int iEvent
Definition: GenABIO.cc:243
T mag() const
The vector magnitude. Equivalent to sqrt(vec.mag2())
MuonSegmentMatcher * theMatcher
T sqrt(T t)
Definition: SSEVec.h:28
edm::ESHandle< GlobalTrackingGeometry > theTrackingGeometry
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
MuonServiceProxy * theService
int station() const
Definition: CSCDetId.h:88
tuple cout
Definition: gather_cfg.py:41
std::vector< double > dstnc
#define debug
Definition: MEtoEDMFormat.h:34
virtual const BoundPlane & surface() const
The nominal surface of the GeomDet.
Definition: GeomDet.h:37
~CSCTimingExtractor()
Destructor.