-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
DTSegmentUpdator.cc
683 lines (511 loc) · 22.7 KB
/
DTSegmentUpdator.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
/** \file
*
* \author Stefano Lacaprara - INFN Legnaro <stefano.lacaprara@pd.infn.it>
* \author Riccardo Bellan - INFN TO <riccardo.bellan@cern.ch>
* \ A.Meneguzzo - Padova University <anna.meneguzzo@pd.infn.it>
* \ M.Pelliccioni - INFN TO <pellicci@cern.ch>
* \ M.Meneghelli - INFN BO <marco.meneghelli@cern.ch>
*/
/* This Class Header */
#include "RecoLocalMuon/DTSegment/src/DTSegmentUpdator.h"
/* Collaborating Class Header */
//mene
#include "DataFormats/DTRecHit/interface/DTChamberRecSegment2D.h"
#include "DataFormats/DTRecHit/interface/DTRecSegment2D.h"
#include "DataFormats/DTRecHit/interface/DTSLRecSegment2D.h"
#include "DataFormats/DTRecHit/interface/DTChamberRecSegment2D.h"
#include "DataFormats/DTRecHit/interface/DTRecSegment4D.h"
#include "DataFormats/DTRecHit/interface/DTRecHit1D.h"
#include "DataFormats/GeometryCommonDetAlgo/interface/ErrorFrameTransformer.h"
#include "RecoLocalMuon/DTSegment/src/DTSegmentCand.h"
#include "RecoLocalMuon/DTRecHit/interface/DTRecHitAlgoFactory.h"
#include "RecoLocalMuon/DTSegment/src/DTLinearFit.h"
#include "Geometry/Records/interface/MuonGeometryRecord.h"
#include "Geometry/DTGeometry/interface/DTGeometry.h"
#include "Geometry/DTGeometry/interface/DTLayer.h"
#include "FWCore/Utilities/interface/Exception.h"
#include "FWCore/MessageLogger/interface/MessageLogger.h"
#include "FWCore/Framework/interface/EventSetup.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"
/* C++ Headers */
#include <string>
using namespace std;
using namespace edm;
/* ====================================================================== */
/// Constructor
DTSegmentUpdator::DTSegmentUpdator(const ParameterSet& config) :
theFitter(new DTLinearFit()) ,
vdrift_4parfit(config.getParameter<bool>("performT0_vdriftSegCorrection")),
T0_hit_resolution(config.getParameter<double>("hit_afterT0_resolution")),
perform_delta_rejecting(config.getParameter<bool>("perform_delta_rejecting")),
debug(config.getUntrackedParameter<bool>("debug",false))
{
string theAlgoName = config.getParameter<string>("recAlgo");
theAlgo = DTRecHitAlgoFactory::get()->create(theAlgoName,
config.getParameter<ParameterSet>("recAlgoConfig"));
if(debug)
cout << "[DTSegmentUpdator] Constructor called" << endl;
}
/// Destructor
DTSegmentUpdator::~DTSegmentUpdator() {
delete theFitter;
}
/* Operations */
void DTSegmentUpdator::setES(const EventSetup& setup){
setup.get<MuonGeometryRecord>().get(theGeom);
theAlgo->setES(setup);
}
void DTSegmentUpdator::update(DTRecSegment4D* seg, const bool calcT0, bool allow3par) const {
if(debug) cout << "[DTSegmentUpdator] Starting to update the DTRecSegment4D" << endl;
const bool hasPhi = seg->hasPhi();
const bool hasZed = seg->hasZed();
//reject the bad hits (due to delta rays)
if(perform_delta_rejecting && hasPhi) rejectBadHits(seg->phiSegment());
int step = (hasPhi && hasZed) ? 3 : 2;
if(calcT0) step = 4;
if(debug) cout << "Step of update is " << step << endl;
GlobalPoint pos = theGeom->idToDet(seg->geographicalId())->toGlobal(seg->localPosition());
GlobalVector dir = theGeom->idToDet(seg->geographicalId())->toGlobal(seg->localDirection());
if(calcT0) calculateT0corr(seg);
if(hasPhi) updateHits(seg->phiSegment(),pos,dir,step);
if(hasZed) updateHits(seg->zSegment() ,pos,dir,step);
fit(seg,allow3par);
}
void DTSegmentUpdator::update(DTRecSegment2D* seg, bool allow3par) const {
if(debug) cout << "[DTSegmentUpdator] Starting to update the DTRecSegment2D" << endl;
GlobalPoint pos = (theGeom->idToDet(seg->geographicalId()))->toGlobal(seg->localPosition());
GlobalVector dir = (theGeom->idToDet(seg->geographicalId()))->toGlobal(seg->localDirection());
updateHits(seg,pos,dir);
fit(seg,allow3par);
}
void DTSegmentUpdator::fit(DTRecSegment4D* seg, bool allow3par) const {
if(debug) cout << "[DTSegmentUpdator] Fit DTRecSegment4D:" << endl;
// after the update must refit the segments
if (debug) {
if(seg->hasPhi()) cout << " 4D Segment contains a Phi segment. t0= " << seg->phiSegment()->t0() << " chi2= " << seg->phiSegment()->chi2() << endl;
if(seg->hasZed()) cout << " 4D Segment contains a Zed segment. t0= " << seg->zSegment()->t0() << " chi2= " << seg->zSegment()->chi2() << endl;
}
// use the 2-par fit UNLESS the 4D segment only has the Phi projection or is out of time by more than 20ns
// - in these cases use the 3-par fit
if(seg->hasPhi()) {
if(seg->hasZed()) {
// fit in-time Phi segments with the 2par fit and out-of-time segments with the 3par fit
if (fabs(seg->phiSegment()->t0())<20.) {
fit(seg->phiSegment(),0);
fit(seg->zSegment(),0);
} else {
fit(seg->phiSegment(),1);
fit(seg->zSegment(),1);
}
} else fit(seg->phiSegment(),1);
} else fit(seg->zSegment(),1);
const DTChamber* theChamber = theGeom->chamber(seg->chamberId());
if(seg->hasPhi() && seg->hasZed() ) {
DTChamberRecSegment2D *segPhi=seg->phiSegment();
DTSLRecSegment2D *segZed=seg->zSegment();
// NB Phi seg is already in chamber ref
LocalPoint posPhiInCh = segPhi->localPosition();
LocalVector dirPhiInCh= segPhi->localDirection();
// Zed seg is in SL one
const DTSuperLayer* zSL = theChamber->superLayer(segZed->superLayerId());
LocalPoint zPos(segZed->localPosition().x(),
(zSL->toLocal(theChamber->toGlobal(segPhi->localPosition()))).y(),
0.);
LocalPoint posZInCh = theChamber->toLocal(zSL->toGlobal(zPos));
LocalVector dirZInCh = theChamber->toLocal(zSL->toGlobal(segZed->localDirection()));
LocalPoint posZAt0 = posZInCh + dirZInCh*(-posZInCh.z())/cos(dirZInCh.theta());
// given the actual definition of chamber refFrame, (with z poiniting to IP),
// the zed component of direction is negative.
LocalVector dir=LocalVector(dirPhiInCh.x()/fabs(dirPhiInCh.z()),dirZInCh.y()/fabs(dirZInCh.z()),-1.);
seg->setPosition(LocalPoint(posPhiInCh.x(),posZAt0.y(),0.));
seg->setDirection(dir.unit());
AlgebraicSymMatrix mat(4);
// set cov matrix
mat[0][0] = segPhi->parametersError()[0][0]; //sigma (dx/dz)
mat[0][2] = segPhi->parametersError()[0][1]; //cov(dx/dz,x)
mat[2][2] = segPhi->parametersError()[1][1]; //sigma (x)
seg->setCovMatrix(mat);
seg->setCovMatrixForZed(posZInCh);
}
else if (seg->hasPhi()) {
DTChamberRecSegment2D *segPhi=seg->phiSegment();
seg->setPosition(segPhi->localPosition());
seg->setDirection(segPhi->localDirection());
AlgebraicSymMatrix mat(4);
// set cov matrix
mat[0][0] = segPhi->parametersError()[0][0]; //sigma (dx/dz)
mat[0][2] = segPhi->parametersError()[0][1]; //cov(dx/dz,x)
mat[2][2] = segPhi->parametersError()[1][1]; //sigma (x)
seg->setCovMatrix(mat);
}
else if (seg->hasZed()) {
DTSLRecSegment2D *segZed = seg->zSegment();
// Zed seg is in SL one
GlobalPoint glbPosZ = ( theGeom->superLayer(segZed->superLayerId()) )->toGlobal(segZed->localPosition());
LocalPoint posZInCh = ( theGeom->chamber(segZed->superLayerId().chamberId()) )->toLocal(glbPosZ);
GlobalVector glbDirZ = (theGeom->superLayer(segZed->superLayerId()) )->toGlobal(segZed->localDirection());
LocalVector dirZInCh = (theGeom->chamber(segZed->superLayerId().chamberId()) )->toLocal(glbDirZ);
LocalPoint posZAt0 = posZInCh+
dirZInCh*(-posZInCh.z())/cos(dirZInCh.theta());
seg->setPosition(posZAt0);
seg->setDirection(dirZInCh);
AlgebraicSymMatrix mat(4);
// set cov matrix
seg->setCovMatrix(mat);
seg->setCovMatrixForZed(posZInCh);
}
}
bool DTSegmentUpdator::fit(DTSegmentCand* seg, bool allow3par, const bool fitdebug) const {
// if (debug && fitdebug) cout << "[DTSegmentUpdator] Fit DTRecSegment2D" << endl;
if (!seg->good()) return false;
// DTSuperLayerId DTid = (DTSuperLayerId)seg->superLayer()->id();
// if (DTid.superlayer()==2)
// allow3par = 0;
vector<float> x;
vector<float> y;
vector<float> sigy;
vector <int> lfit;
vector <double> dist;
int i=0;
x.reserve(8);
y.reserve(8);
sigy.reserve(8);
lfit.reserve(8);
dist.reserve(8);
DTSegmentCand::AssPointCont hits=seg->hits();
for (DTSegmentCand::AssPointCont::const_iterator iter=hits.begin(); iter!=hits.end(); ++iter) {
LocalPoint pos = (*iter).first->localPosition((*iter).second);
float xwire = (((*iter).first)->localPosition(DTEnums::Left).x() + ((*iter).first)->localPosition(DTEnums::Right).x()) /2.;
float distance = pos.x() - xwire;
if ((*iter).second==DTEnums::Left) lfit.push_back(1);
else lfit.push_back(-1);
dist.push_back(distance);
sigy.push_back(sqrt((*iter).first->localPositionError().xx()));
x.push_back(pos.z());
y.push_back(pos.x());
i++;
}
LocalPoint pos;
LocalVector dir;
AlgebraicSymMatrix covMat(2);
float cminf =0.;
float vminf =0.;
double chi2 = 0.;
double t0_corr = 0.;
fit(x,y,lfit,dist,sigy,pos,dir,cminf,vminf,covMat,chi2,allow3par);
if (cminf!=0) t0_corr=-cminf/0.00543; // convert drift distance to time
if (debug && fitdebug) cout << " DTcand chi2: " << chi2 << endl;
if (debug && fitdebug) cout << " DTcand t0: " << t0_corr << endl;
seg->setPosition(pos);
seg->setDirection(dir);
seg->sett0(t0_corr);
seg->setCovMatrix(covMat);
seg->setChi2(chi2);
// cout << "pos " << pos << endl;
// cout << "dir " << dir << endl;
// cout << "Mat " << covMat << endl;
return true;
}
void DTSegmentUpdator::fit(DTRecSegment2D* seg, bool allow3par) const {
if(debug) cout << "[DTSegmentUpdator] Fit DTRecSegment2D - 3par: " << allow3par << endl;
vector<float> x;
vector<float> y;
vector<float> sigy;
vector <int> lfit;
vector <double> dist;
x.reserve(8);
y.reserve(8);
sigy.reserve(8);
lfit.reserve(8);
dist.reserve(8);
// DTSuperLayerId DTid = (DTSuperLayerId)seg->geographicalId();
// if (DTid.superlayer()==2)
// allow3par = 0;
vector<DTRecHit1D> hits=seg->specificRecHits();
for (vector<DTRecHit1D>::const_iterator hit=hits.begin();
hit!=hits.end(); ++hit) {
// I have to get the hits position (the hit is in the layer rf) in SL frame...
GlobalPoint glbPos = ( theGeom->layer( hit->wireId().layerId() ) )->toGlobal(hit->localPosition());
LocalPoint pos = ( theGeom->idToDet(seg->geographicalId()) )->toLocal(glbPos);
x.push_back(pos.z());
y.push_back(pos.x());
const DTLayer* layer = theGeom->layer( hit->wireId().layerId() );
float xwire = layer->specificTopology().wirePosition(hit->wireId().wire());
float distance = fabs(hit->localPosition().x() - xwire);
dist.push_back(distance);
int ilc = ( hit->lrSide() == DTEnums::Left ) ? 1 : -1;
lfit.push_back(ilc);
// Get local error in SL frame
//RB: is it right in this way?
ErrorFrameTransformer tran;
GlobalError glbErr = tran.transform( hit->localPositionError(),(theGeom->layer( hit->wireId().layerId() ))->surface());
LocalError slErr = tran.transform( glbErr, (theGeom->idToDet(seg->geographicalId()))->surface());
sigy.push_back(sqrt(slErr.xx()));
}
LocalPoint pos;
LocalVector dir;
AlgebraicSymMatrix covMat(2);
double chi2 = 0.;
float cminf=0.;
float vminf=0.;
double t0_corr = 0.;
fit(x,y,lfit,dist,sigy,pos,dir,cminf,vminf,covMat,chi2,allow3par);
if (cminf!=0) t0_corr=-cminf/0.00543; // convert drift distance to time
if (debug) cout << " DTSeg2d chi2: " << chi2 << endl;
if (debug) cout << " DTSeg2d Fit t0: " << t0_corr << endl;
// cout << "pos " << segPosition << endl;
// cout << "dir " << segDirection << endl;
// cout << "Mat " << mat << endl;
seg->setPosition(pos);
seg->setDirection(dir);
seg->setCovMatrix(covMat);
seg->setChi2(chi2);
seg->setT0(t0_corr);
}
void DTSegmentUpdator::fit(const vector<float>& x,
const vector<float>& y,
const vector<int>& lfit,
const vector<double>& dist,
const vector<float>& sigy,
LocalPoint& pos,
LocalVector& dir,
float& cminf,
float& vminf,
AlgebraicSymMatrix& covMatrix,
double& chi2,
const bool allow3par) const {
float slope = 0.;
float intercept = 0.;
float covss = 0.;
float covii = 0.;
float covsi = 0.;
cminf=0;
vminf=0;
int leftHits=0, rightHits=0;
for (unsigned int i=0; i<lfit.size(); i++)
if (lfit[i]==1) leftHits++; else rightHits++;
theFitter->fit(x,y,x.size(),sigy,slope,intercept,chi2,covss,covii,covsi);
// If we have at least one left and one right hit we can try the 3 parameter fit (if it is switched on)
// FIXME: currently the covariance matrix from the 2-par fit is kept
if (leftHits && rightHits && (leftHits+rightHits>3) && allow3par) {
theFitter->fitNpar(3,x,y,lfit,dist,sigy,slope,intercept,cminf,vminf,chi2,debug);
double t0_corr=-cminf/0.00543;
// if (fabs(t0_corr)<20. && !allow3par) {
if (fabs(t0_corr)<20.) {
theFitter->fit(x,y,x.size(),sigy,slope,intercept,chi2,covss,covii,covsi);
cminf=0;
}
}
// cout << "slope " << slope << endl;
// cout << "intercept " << intercept << endl;
// intercept is the x() in chamber frame when the segment cross the chamber
// plane (at z()=0), the y() is not measured, so let's put the center of the
// chamber.
pos = LocalPoint(intercept,0.,0.);
// slope is dx()/dz(), while dy()/dz() is by definition 0, finally I want the
// segment to point outward, so opposite to local z
dir = LocalVector(-slope,0.,-1.).unit();
covMatrix = AlgebraicSymMatrix(2);
covMatrix[0][0] = covss; // this is var(dy/dz)
covMatrix[1][1] = covii; // this is var(y)
covMatrix[1][0] = covsi; // this is cov(dy/dz,y)
}
// The GlobalPoint and the GlobalVector can be either the glb position and the direction
// of the 2D-segment itself or the glb position and direction of the 4D segment
void DTSegmentUpdator::updateHits(DTRecSegment2D* seg, GlobalPoint &gpos,
GlobalVector &gdir, const int step) const{
// it is not necessary to have DTRecHit1D* to modify the obj in the container
// but I have to be carefully, since I cannot make a copy before the iteration!
vector<DTRecHit1D> toBeUpdatedRecHits = seg->specificRecHits();
vector<DTRecHit1D> updatedRecHits;
for (vector<DTRecHit1D>::iterator hit= toBeUpdatedRecHits.begin();
hit!=toBeUpdatedRecHits.end(); ++hit) {
const DTLayer* layer = theGeom->layer( hit->wireId().layerId() );
LocalPoint segPos=layer->toLocal(gpos);
LocalVector segDir=layer->toLocal(gdir);
// define impact angle needed by the step 2
const float angle = atan(segDir.x()/-segDir.z());
// define the local position (extr.) of the segment. Needed by the third step
LocalPoint segPosAtLayer=segPos+segDir*(-segPos.z())/cos(segDir.theta());
DTRecHit1D newHit1D = (*hit);
bool ok = true;
if (step == 2) {
ok = theAlgo->compute(layer,*hit,angle,newHit1D);
} else if (step == 3) {
LocalPoint hitPos(hit->localPosition().x(),+segPosAtLayer.y(),0.);
GlobalPoint glbpos= theGeom->layer( hit->wireId().layerId() )->toGlobal(hitPos);
newHit1D.setPosition(hitPos);
ok = theAlgo->compute(layer,*hit,angle,glbpos,newHit1D);
} else if (step == 4) {
//const double vminf = seg->vDrift(); // vdrift correction are recorded in the segment
double vminf =0.;
if(vdrift_4parfit) vminf = seg->vDrift(); // use vdrift recorded in the segment only if vdrift_4parfit=True
double cminf = 0.;
if(seg->ist0Valid()) cminf = - seg->t0()*0.00543;
//cout << "In updateHits: t0 = " << seg->t0() << endl;
//cout << "In updateHits: vminf = " << vminf << endl;
//cout << "In updateHits: cminf = " << cminf << endl;
const float xwire = layer->specificTopology().wirePosition(hit->wireId().wire());
const float distance = fabs(hit->localPosition().x() - xwire);
const int ilc = ( hit->lrSide() == DTEnums::Left ) ? 1 : -1;
const double dy_corr = (vminf*ilc*distance-cminf*ilc );
LocalPoint point(hit->localPosition().x() + dy_corr, +segPosAtLayer.y(), 0.);
//double final_hit_resol = T0_hit_resolution;
//if(newHit1D.wireId().layerId().superlayerId().superLayer() != 2) final_hit_resol = final_hit_resol * 0.8;
//LocalError error(final_hit_resol * final_hit_resol,0.,0.);
LocalError error(T0_hit_resolution*T0_hit_resolution,0.,0.);
newHit1D.setPositionAndError(point, error);
//FIXME: check that the hit is still inside the cell
ok = true;
} else throw cms::Exception("DTSegmentUpdator")<<" updateHits called with wrong step " << endl;
if (ok) updatedRecHits.push_back(newHit1D);
else {
LogError("DTSegmentUpdator")<<"DTSegmentUpdator::updateHits failed update" << endl;
throw cms::Exception("DTSegmentUpdator")<<"updateHits failed update"<<endl;
}
}
seg->update(updatedRecHits);
}
void DTSegmentUpdator::rejectBadHits(DTChamberRecSegment2D* phiSeg) const {
vector<float> x;
vector<float> y;
if(debug) cout << " Inside the segment updator, now loop on hits: ( x == z_loc , y == x_loc) " << endl;
vector<DTRecHit1D> hits = phiSeg->specificRecHits();
for (vector<DTRecHit1D>::const_iterator hit=hits.begin();
hit!=hits.end(); ++hit) {
// I have to get the hits position (the hit is in the layer rf) in SL frame...
GlobalPoint glbPos = ( theGeom->layer( hit->wireId().layerId() ) )->toGlobal(hit->localPosition());
LocalPoint pos = ( theGeom->idToDet(phiSeg->geographicalId()) )->toLocal(glbPos);
x.push_back(pos.z());
y.push_back(pos.x());
}
if(debug){
cout << " end of segment! " << endl;
cout << " size = Number of Hits: " << x.size() << " " << y.size() << endl;
}
// Perform the 2 par fit:
float par[2]={0.,0.}; // q , m
//variables to perform the fit:
float Sx = 0.;
float Sy = 0.;
float Sx2 = 0.;
float Sy2 = 0.;
float Sxy = 0.;
size_t N = x.size();
if (N == 0)
return;
for(size_t i = 0; i < N;++i){
Sx += x.at(i);
Sy += y.at(i);
Sx2 += x.at(i)*x.at(i);
Sy2 += y.at(i)*y.at(i);
Sxy += x.at(i)*y.at(i);
}
const float delta = N*Sx2 - Sx*Sx;
par[0] = ( Sx2*Sy - Sx*Sxy )/delta;
par[1] = ( N*Sxy - Sx*Sy )/delta;
if(debug) cout << "fit 2 parameters done ----> par0: "<< par[0] << " par1: "<< par[1] << endl;
// Calc residuals:
float residuals[N];
for(size_t i = 0; i < N;++i)
residuals[i] = 0;
for(size_t i = 0; i < N;++i){
residuals[i] = y.at(i) - par[1]*x.at(i) - par[0];
if(debug){
cout<<" i: "<<i<<" y_i "<<y.at(i)<<" x_i "<<x.at(i)<<" res_i "<<residuals[i];
if (i==N-1) cout<<endl;
}
}
if(debug) cout << " Residuals computed! "<< endl;
// Perform bad hit rejecting -- update hits
vector<DTRecHit1D> updatedRecHits;
float mean_residual = 0.; //mean of the absolute values of residuals
for (size_t i = 0; i < N; ++i)
mean_residual += fabs(residuals[i]);
mean_residual = mean_residual/(N - 2);
if(debug) cout << " mean_residual: "<< mean_residual << endl;
int i = 0;
for (vector<DTRecHit1D>::const_iterator hit=hits.begin();
hit!=hits.end(); ++hit) {
DTRecHit1D newHit1D = (*hit);
float normResidual = mean_residual > 0 ? fabs(residuals[i])/mean_residual : 0;
if(normResidual < 1.5){
updatedRecHits.push_back(newHit1D);
if(debug) cout << " accepted "<< i+1 << "th hit" <<" Irej: " << normResidual << endl;
++i;
}
else {
if(debug) cout << " rejected "<< i+1 << "th hit" <<" Irej: " << normResidual << endl;
++i;
continue;
}
}
phiSeg->update(updatedRecHits);
//final check!
if(debug){
vector<float> x_upd;
vector<float> y_upd;
cout << " Check the update action: " << endl;
vector<DTRecHit1D> hits_upd = phiSeg->specificRecHits();
for (vector<DTRecHit1D>::const_iterator hit=hits_upd.begin();
hit!=hits_upd.end(); ++hit) {
// I have to get the hits position (the hit is in the layer rf) in SL frame...
GlobalPoint glbPos = ( theGeom->layer( hit->wireId().layerId() ) )->toGlobal(hit->localPosition());
LocalPoint pos = ( theGeom->idToDet(phiSeg->geographicalId()) )->toLocal(glbPos);
x_upd.push_back(pos.z());
y_upd.push_back(pos.x());
cout << " x_upd: "<< pos.z() << " y_upd: "<< pos.x() << endl;
}
cout << " end of segment! " << endl;
cout << " size = Number of Hits: " << x_upd.size() << " " << y_upd.size() << endl;
}// end debug
return;
} //end DTSegmentUpdator::rejectBadHits
void DTSegmentUpdator::calculateT0corr(DTRecSegment4D* seg) const {
if(seg->hasPhi()) calculateT0corr(seg->phiSegment());
if(seg->hasZed()) calculateT0corr(seg->zSegment());
}
void DTSegmentUpdator::calculateT0corr(DTRecSegment2D* seg) const {
// WARNING: since this method is called both with a 2D and a 2DPhi as argument
// seg->geographicalId() can be a superLayerId or a chamberId
if(debug) cout << "[DTSegmentUpdator] CalculateT0corr DTRecSegment4D" << endl;
vector<double> d_drift;
vector<float> x;
vector<float> y;
vector<int> lc;
vector<DTRecHit1D> hits=seg->specificRecHits();
DTWireId wireId;
int nptfit = 0;
for (vector<DTRecHit1D>::const_iterator hit=hits.begin();
hit!=hits.end(); ++hit) {
// I have to get the hits position (the hit is in the layer rf) in SL frame...
GlobalPoint glbPos = ( theGeom->layer( hit->wireId().layerId() ) )->toGlobal(hit->localPosition());
LocalPoint pos = ( theGeom->idToDet(seg->geographicalId()) )->toLocal(glbPos);
const DTLayer* layer = theGeom->layer( hit->wireId().layerId() );
float xwire = layer->specificTopology().wirePosition(hit->wireId().wire());
float distance = fabs(hit->localPosition().x() - xwire);
int ilc = ( hit->lrSide() == DTEnums::Left ) ? 1 : -1;
nptfit++;
x.push_back(pos.z());
y.push_back(pos.x());
lc.push_back(ilc);
d_drift.push_back(distance);
// cout << " d_drift "<<distance <<" npt= " <<npt<<endl;
}
double chi2fit = 0.;
float cminf = 0.;
float vminf = 0.;
float a,b;
if ( nptfit > 2 ) {
//NB chi2fit is normalized
theFitter->fit4Var(x,y,lc,d_drift,nptfit,a,b,cminf,vminf,chi2fit,vdrift_4parfit,debug);
double t0cor = -999.;
if(cminf > -998.) t0cor = - cminf/0.00543 ; // in ns
//cout << "In calculateT0corr: t0 = " << t0cor << endl;
//cout << "In calculateT0corr: vminf = " << vminf << endl;
//cout << "In calculateT0corr: cminf = " << cminf << endl;
//cout << "In calculateT0corr: chi2 = " << chi2fit << endl;
seg->setT0(t0cor); // time and
seg->setVdrift(vminf); // vdrift correction are recorded in the segment
}
}