CMS 3D CMS Logo

MuonTimingFiller.cc
Go to the documentation of this file.
1 //
2 // Package: MuonTimingFiller
3 // Class: MuonTimingFiller
4 //
12 //
13 // Original Author: Piotr Traczyk, CERN
14 // Created: Mon Mar 16 12:27:22 CET 2009
15 //
16 //
17 
18 
19 // system include files
20 #include <memory>
21 
22 // user include files
25 
28 
30 
36 
40 
41 //
42 // constructors and destructor
43 //
45 {
46  // Load parameters for the DTTimingExtractor
47  edm::ParameterSet dtTimingParameters = iConfig.getParameter<edm::ParameterSet>("DTTimingParameters");
48 
49  // Load parameters for the CSCTimingExtractor
50  edm::ParameterSet cscTimingParameters = iConfig.getParameter<edm::ParameterSet>("CSCTimingParameters");
51 
52  // Fallback mechanism for old configs (there the segment matcher was built inside the timing extractors)
53  edm::ParameterSet matchParameters;
54  if (iConfig.existsAs<edm::ParameterSet>("MatchParameters"))
55  matchParameters = iConfig.getParameter<edm::ParameterSet>("MatchParameters");
56  else
57  matchParameters = dtTimingParameters.getParameter<edm::ParameterSet>("MatchParameters");
58 
59  theMatcher_ = std::make_unique<MuonSegmentMatcher>(matchParameters, iC);
60  theDTTimingExtractor_ = std::make_unique<DTTimingExtractor>(dtTimingParameters,theMatcher_.get());
61  theCSCTimingExtractor_ = std::make_unique<CSCTimingExtractor>(cscTimingParameters,theMatcher_.get());
62 
63  errorEB_ = iConfig.getParameter<double>("ErrorEB");
64  errorEE_ = iConfig.getParameter<double>("ErrorEE");
65  ecalEcut_ = iConfig.getParameter<double>("EcalEnergyCut");
66 
67  useDT_ = iConfig.getParameter<bool>("UseDT");
68  useCSC_ = iConfig.getParameter<bool>("UseCSC");
69  useECAL_ = iConfig.getParameter<bool>("UseECAL");
70 
71 }
72 
73 
75 {
76 }
77 
78 
79 //
80 // member functions
81 //
82 
83 void
85  reco::MuonTimeExtra& dtTime,
86  reco::MuonTimeExtra& cscTime,
87  reco::MuonTime& rpcTime,
88  reco::MuonTimeExtra& combinedTime,
90  const edm::EventSetup& iSetup )
91 {
92  TimeMeasurementSequence dtTmSeq,cscTmSeq;
93 
94  if ( !(muon.combinedMuon().isNull()) ) {
95  theDTTimingExtractor_->fillTiming(dtTmSeq, muon.combinedMuon(), iEvent, iSetup);
96  theCSCTimingExtractor_->fillTiming(cscTmSeq, muon.combinedMuon(), iEvent, iSetup);
97  } else {
98  if ( !(muon.standAloneMuon().isNull()) ) {
99  theDTTimingExtractor_->fillTiming(dtTmSeq, muon.standAloneMuon(), iEvent, iSetup);
100  theCSCTimingExtractor_->fillTiming(cscTmSeq, muon.standAloneMuon(), iEvent, iSetup);
101  } else {
102  if ( muon.isTrackerMuon() ) {
103  std::vector<const DTRecSegment4D*> dtSegments;
104  std::vector<const CSCSegment*> cscSegments;
105  for( auto& chamber: muon.matches() ){
106  for ( auto& segment : chamber.segmentMatches ){
107  // Use only the segments that passed arbitration to avoid mixing
108  // segments from in-time and out-of-time muons that may bias the result
109  // SegmentAndTrackArbitration
110  if(segment.isMask(reco::MuonSegmentMatch::BestInStationByDR) &&
112  if ( !(segment.dtSegmentRef.isNull()))
113  dtSegments.push_back(segment.dtSegmentRef.get());
114  if ( !(segment.cscSegmentRef.isNull()))
115  cscSegments.push_back(segment.cscSegmentRef.get());
116  }
117  }
118  }
119  theDTTimingExtractor_->fillTiming(dtTmSeq, dtSegments, muon.innerTrack(), iEvent, iSetup);
120  theCSCTimingExtractor_->fillTiming(cscTmSeq, cscSegments, muon.innerTrack(), iEvent, iSetup);
121  }
122  }
123  }
124 
125  // Fill DT-specific timing information block
126  fillTimeFromMeasurements(dtTmSeq, dtTime);
127 
128  // Fill CSC-specific timing information block
129  fillTimeFromMeasurements(cscTmSeq, cscTime);
130 
131  // Fill RPC-specific timing information block
132  fillRPCTime(muon, rpcTime, iEvent);
133 
134  // Combine the TimeMeasurementSequences from DT/CSC subdetectors
135  TimeMeasurementSequence combinedTmSeq;
136  combineTMSequences(muon,dtTmSeq,cscTmSeq,combinedTmSeq);
137  // add ECAL info
138  if (useECAL_) addEcalTime(muon,combinedTmSeq);
139 
140  // Fill the master timing block
141  fillTimeFromMeasurements(combinedTmSeq, combinedTime);
142 
143  LogTrace("MuonTime") << "Global 1/beta: " << combinedTime.inverseBeta() << " +/- " << combinedTime.inverseBetaErr()<<std::endl;
144  LogTrace("MuonTime") << " Free 1/beta: " << combinedTime.freeInverseBeta() << " +/- " << combinedTime.freeInverseBetaErr()<<std::endl;
145  LogTrace("MuonTime") << " Vertex time (in-out): " << combinedTime.timeAtIpInOut() << " +/- " << combinedTime.timeAtIpInOutErr()
146  << " # of points: " << combinedTime.nDof() <<std::endl;
147  LogTrace("MuonTime") << " Vertex time (out-in): " << combinedTime.timeAtIpOutIn() << " +/- " << combinedTime.timeAtIpOutInErr()<<std::endl;
148  LogTrace("MuonTime") << " direction: " << combinedTime.direction() << std::endl;
149 
150 }
151 
152 
153 void
155  std::vector <double> x,y;
156  double invbeta(0), invbetaerr(0);
157  double vertexTime(0), vertexTimeErr(0), vertexTimeR(0), vertexTimeRErr(0);
158  double freeBeta(0), freeBetaErr(0), freeTime(0), freeTimeErr(0);
159 
160  if (tmSeq.dstnc.size()<=1) return;
161 
162  for (unsigned int i=0;i<tmSeq.dstnc.size();i++) {
163  invbeta+=(1.+tmSeq.local_t0.at(i)/tmSeq.dstnc.at(i)*30.)*tmSeq.weightInvbeta.at(i)/tmSeq.totalWeightInvbeta;
164  x.push_back(tmSeq.dstnc.at(i)/30.);
165  y.push_back(tmSeq.local_t0.at(i)+tmSeq.dstnc.at(i)/30.);
166  vertexTime+=tmSeq.local_t0.at(i)*tmSeq.weightTimeVtx.at(i)/tmSeq.totalWeightTimeVtx;
167  vertexTimeR+=(tmSeq.local_t0.at(i)+2*tmSeq.dstnc.at(i)/30.)*tmSeq.weightTimeVtx.at(i)/tmSeq.totalWeightTimeVtx;
168  }
169 
170  double diff;
171  for (unsigned int i=0;i<tmSeq.dstnc.size();i++) {
172  diff=(1.+tmSeq.local_t0.at(i)/tmSeq.dstnc.at(i)*30.)-invbeta;
173  invbetaerr+=diff*diff*tmSeq.weightInvbeta.at(i);
174  diff=tmSeq.local_t0.at(i)-vertexTime;
175  vertexTimeErr+=diff*diff*tmSeq.weightTimeVtx.at(i);
176  diff=tmSeq.local_t0.at(i)+2*tmSeq.dstnc.at(i)/30.-vertexTimeR;
177  vertexTimeRErr+=diff*diff*tmSeq.weightTimeVtx.at(i);
178  }
179 
180  double cf = 1./(tmSeq.dstnc.size()-1);
181  invbetaerr=sqrt(invbetaerr/tmSeq.totalWeightInvbeta*cf);
182  vertexTimeErr=sqrt(vertexTimeErr/tmSeq.totalWeightTimeVtx*cf);
183  vertexTimeRErr=sqrt(vertexTimeRErr/tmSeq.totalWeightTimeVtx*cf);
184 
185  muTime.setInverseBeta(invbeta);
186  muTime.setInverseBetaErr(invbetaerr);
187  muTime.setTimeAtIpInOut(vertexTime);
188  muTime.setTimeAtIpInOutErr(vertexTimeErr);
189  muTime.setTimeAtIpOutIn(vertexTimeR);
190  muTime.setTimeAtIpOutInErr(vertexTimeRErr);
191 
192  rawFit(freeBeta, freeBetaErr, freeTime, freeTimeErr, x, y);
193 
194  muTime.setFreeInverseBeta(freeBeta);
195  muTime.setFreeInverseBetaErr(freeBetaErr);
196 
197  muTime.setNDof(tmSeq.dstnc.size());
198 }
199 
200 
201 void
203 
204  double trpc=0,trpc2=0;
205 
206  reco::TrackRef staTrack = muon.standAloneMuon();
207  if (staTrack.isNull()) return;
208 
209  const std::vector<const RPCRecHit*> rpcHits = theMatcher_->matchRPC(*staTrack,iEvent);
210  const int nrpc = rpcHits.size();
211  for ( const auto& hitRPC : rpcHits ) {
212  const double time = hitRPC->timeError() < 0 ? hitRPC->BunchX()*25. : hitRPC->time();
213  trpc += time;
214  trpc2 += time*time;
215  }
216 
217  if (nrpc==0) return;
218 
219  trpc2 = trpc2/nrpc;
220  trpc = trpc/nrpc;
221  const double trpcerr = sqrt(std::max(0., trpc2-trpc*trpc));
222 
223  rpcTime.timeAtIpInOut=trpc;
224  rpcTime.timeAtIpInOutErr=trpcerr;
225  rpcTime.nDof=nrpc;
226 
227  // currently unused
228  rpcTime.timeAtIpOutIn=0.;
229  rpcTime.timeAtIpOutInErr=0.;
230 }
231 
232 
233 void
235  const TimeMeasurementSequence& dtSeq,
236  const TimeMeasurementSequence& cscSeq,
237  TimeMeasurementSequence &cmbSeq ) {
238 
239  if (useDT_) for (unsigned int i=0;i<dtSeq.dstnc.size();i++) {
240  cmbSeq.dstnc.push_back(dtSeq.dstnc.at(i));
241  cmbSeq.local_t0.push_back(dtSeq.local_t0.at(i));
242  cmbSeq.weightTimeVtx.push_back(dtSeq.weightTimeVtx.at(i));
243  cmbSeq.weightInvbeta.push_back(dtSeq.weightInvbeta.at(i));
244 
245  cmbSeq.totalWeightTimeVtx+=dtSeq.weightTimeVtx.at(i);
246  cmbSeq.totalWeightInvbeta+=dtSeq.weightInvbeta.at(i);
247  }
248 
249  if (useCSC_) for (unsigned int i=0;i<cscSeq.dstnc.size();i++) {
250  cmbSeq.dstnc.push_back(cscSeq.dstnc.at(i));
251  cmbSeq.local_t0.push_back(cscSeq.local_t0.at(i));
252  cmbSeq.weightTimeVtx.push_back(cscSeq.weightTimeVtx.at(i));
253  cmbSeq.weightInvbeta.push_back(cscSeq.weightInvbeta.at(i));
254 
255  cmbSeq.totalWeightTimeVtx+=cscSeq.weightTimeVtx.at(i);
256  cmbSeq.totalWeightInvbeta+=cscSeq.weightInvbeta.at(i);
257  }
258 }
259 
260 
261 void
263  TimeMeasurementSequence &cmbSeq ) {
264 
265  reco::MuonEnergy muonE;
266  if (muon.isEnergyValid())
267  muonE = muon.calEnergy();
268 
269  // Cut on the crystal energy and restrict to the ECAL barrel for now
270 // if (muonE.emMax<ecalEcut_ || fabs(muon.eta())>1.5) return;
271  if (muonE.emMax<ecalEcut_) return;
272 
273  // A simple parametrization of the error on the ECAL time measurement
274  double emErr;
275  if (muonE.ecal_id.subdetId()==EcalBarrel) emErr= errorEB_/muonE.emMax; else
276  emErr=errorEE_/muonE.emMax;
277  double hitWeight = 1/(emErr*emErr);
278  double hitDist=muonE.ecal_position.r();
279 
280  cmbSeq.local_t0.push_back(muonE.ecal_time);
281  cmbSeq.weightTimeVtx.push_back(hitWeight);
282  cmbSeq.weightInvbeta.push_back(hitDist*hitDist*hitWeight/(30.*30.));
283 
284  cmbSeq.dstnc.push_back(hitDist);
285 
286  cmbSeq.totalWeightTimeVtx+=hitWeight;
287  cmbSeq.totalWeightInvbeta+=hitDist*hitDist*hitWeight/(30.*30.);
288 
289 }
290 
291 
292 
293 void
294 MuonTimingFiller::rawFit(double &freeBeta, double &freeBetaErr, double &freeTime, double &freeTimeErr,
295  const std::vector<double>& hitsx, const std::vector<double>& hitsy) {
296 
297  double s=0,sx=0,sy=0,x,y;
298  double sxx=0,sxy=0;
299 
300  freeBeta = 0;
301  freeBetaErr = 0;
302  freeTime = 0;
303  freeTimeErr = 0;
304 
305  if (hitsx.empty()) return;
306  if (hitsx.size()==1) {
307  freeTime=hitsy[0];
308  } else {
309  for (unsigned int i = 0; i != hitsx.size(); i++) {
310  x=hitsx[i];
311  y=hitsy[i];
312  sy += y;
313  sxy+= x*y;
314  s += 1.;
315  sx += x;
316  sxx += x*x;
317  }
318 
319  double d = s*sxx - sx*sx;
320  freeTime = (sxx*sy- sx*sxy)/ d;
321  freeBeta = (s*sxy - sx*sy) / d;
322  freeBetaErr = sqrt(sxx/d);
323  freeTimeErr = sqrt(s/d);
324  }
325 }
326 
T getParameter(std::string const &) const
void setTimeAtIpOutInErr(const float timeErr)
Definition: MuonTimeExtra.h:51
void addEcalTime(const reco::Muon &muon, TimeMeasurementSequence &cmbSeq)
float inverseBetaErr() const
Definition: MuonTimeExtra.h:28
bool existsAs(std::string const &parameterName, bool trackiness=true) const
checks if a parameter exists as a given type
Definition: ParameterSet.h:186
void combineTMSequences(const reco::Muon &muon, const TimeMeasurementSequence &dtSeq, const TimeMeasurementSequence &cscSeq, TimeMeasurementSequence &cmbSeq)
std::vector< double > local_t0
std::unique_ptr< DTTimingExtractor > theDTTimingExtractor_
float inverseBeta() const
Definition: MuonTimeExtra.h:27
virtual TrackRef innerTrack() const
Definition: Muon.h:48
void fillRPCTime(const reco::Muon &muon, reco::MuonTime &muTime, edm::Event &iEvent)
Direction direction() const
direction estimation based on time dispersion
Definition: MuonTimeExtra.h:54
void fillTiming(const reco::Muon &muon, reco::MuonTimeExtra &dtTime, reco::MuonTimeExtra &cscTime, reco::MuonTime &rpcTime, reco::MuonTimeExtra &combinedTime, edm::Event &iEvent, const edm::EventSetup &iSetup)
float timeAtIpOutInErr() const
Definition: MuonTimeExtra.h:49
int nDof() const
number of measurements used in timing calculation
Definition: MuonTimeExtra.h:22
float timeAtIpOutInErr
Definition: MuonTime.h:18
MuonTimingFiller(const edm::ParameterSet &, edm::ConsumesCollector &&iC)
static const unsigned int BestInStationByDR
float ecal_time
Calorimeter timing.
Definition: MuonEnergy.h:37
bool isTrackerMuon() const override
Definition: Muon.h:277
void setTimeAtIpOutIn(const float timeIp)
Definition: MuonTimeExtra.h:50
int iEvent
Definition: GenABIO.cc:230
T sqrt(T t)
Definition: SSEVec.h:18
void rawFit(double &a, double &da, double &b, double &db, const std::vector< double > &hitsx, const std::vector< double > &hitsy)
std::unique_ptr< MuonSegmentMatcher > theMatcher_
float timeAtIpInOutErr
Definition: MuonTime.h:15
constexpr int subdetId() const
get the contents of the subdetector field (not cast into any detector&#39;s numbering enum) ...
Definition: DetId.h:41
float timeAtIpOutIn
b) particle is moving from outside in
Definition: MuonTime.h:17
int nDof
number of muon stations used
Definition: MuonTime.h:10
float timeAtIpInOutErr() const
Definition: MuonTimeExtra.h:44
std::vector< double > weightInvbeta
void setNDof(const int nDof)
Definition: MuonTimeExtra.h:23
float freeInverseBetaErr() const
Definition: MuonTimeExtra.h:37
bool isNull() const
Checks for null.
Definition: Ref.h:250
#define LogTrace(id)
void fillTimeFromMeasurements(const TimeMeasurementSequence &tmSeq, reco::MuonTimeExtra &muTime)
bool isEnergyValid() const
Definition: Muon.h:109
TrackRef standAloneMuon() const override
reference to a stand-alone muon Track
Definition: Muon.h:52
std::unique_ptr< CSCTimingExtractor > theCSCTimingExtractor_
MuonEnergy calEnergy() const
get energy deposition information
Definition: Muon.h:111
float emMax
maximal energy of ECAL crystal in the 5x5 shape
Definition: MuonEnergy.h:22
float timeAtIpInOut() const
Definition: MuonTimeExtra.h:43
void setInverseBetaErr(const float iBetaErr)
Definition: MuonTimeExtra.h:30
std::vector< double > weightTimeVtx
TrackRef combinedMuon() const override
reference to a stand-alone muon Track
Definition: Muon.h:55
std::vector< MuonChamberMatch > & matches()
get muon matching information
Definition: Muon.h:144
DetId ecal_id
DetId of the central ECAL crystal.
Definition: MuonEnergy.h:47
static const unsigned int BelongsToTrackByDR
float freeInverseBeta() const
Definition: MuonTimeExtra.h:36
void setFreeInverseBetaErr(const float iBetaErr)
Definition: MuonTimeExtra.h:39
float timeAtIpOutIn() const
b) particle is moving from outside in
Definition: MuonTimeExtra.h:48
std::vector< double > dstnc
math::XYZPointF ecal_position
Trajectory position at the calorimeter.
Definition: MuonEnergy.h:43
void setFreeInverseBeta(const float iBeta)
Definition: MuonTimeExtra.h:38
float timeAtIpInOut
Definition: MuonTime.h:14
void setTimeAtIpInOut(const float timeIp)
Definition: MuonTimeExtra.h:45
void setTimeAtIpInOutErr(const float timeErr)
Definition: MuonTimeExtra.h:46
void setInverseBeta(const float iBeta)
Definition: MuonTimeExtra.h:29