-
Notifications
You must be signed in to change notification settings - Fork 35
/
SiTracker_dEdxProcessor.cc
611 lines (455 loc) · 17.5 KB
/
SiTracker_dEdxProcessor.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
#include "SiTracker_dEdxProcessor.h"
#include <EVENT/LCCollection.h>
#include <EVENT/MCParticle.h>
#include <IMPL/TrackImpl.h>
//#include <EVENT/Track.h>
#include <EVENT/LCRelation.h>
#include <UTIL/LCRelationNavigator.h>
#include <IMPL/TrackStateImpl.h>
#include <IMPL/TrackerHitImpl.h>
// ----- include for verbosity dependent logging ---------
#include "marlin/VerbosityLevels.h"
#include <DD4hep/Detector.h>
#include "DD4hep/DD4hepUnits.h"
#include "DDRec/DDGear.h"
#include "DDRec/DetectorData.h"
#include <DD4hep/DetType.h>
#include <DDRec/Vector3D.h>
#include "marlin/Global.h"
#include <MarlinTrk/IMarlinTrack.h>
#include <MarlinTrk/IMarlinTrkSystem.h>
#include "MarlinTrk/MarlinTrkUtils.h"
#include <TVector3.h>
#include <TROOT.h>
#include <climits>
dEdxPoint::dEdxPoint(const double _dE, const double _dx) :
dE(_dE), dx(_dx), dEdx(_dE/_dx) {}
dEdxPoint::dEdxPoint(const dEdxPoint& orig) :
dE(orig.Get_dE()), dx(orig.Get_dx()), dEdx(orig.Get_dE()/orig.Get_dx()) {}
SiTracker_dEdxProcessor aSiTracker_dEdxProcessor ;
SiTracker_dEdxProcessor::SiTracker_dEdxProcessor() : Processor("SiTracker_dEdxProcessor"),
m_trackCollName(""), m_trkHitCollNames(),
surfMap(NULL), trkSystem(NULL), _bField(0),
layerFinder(NULL),
lastRunHeaderProcessed(-1),
timers(),
lastTP(std::chrono::high_resolution_clock::now()),
newTP(std::chrono::high_resolution_clock::now())
{
// modify processor description
_description = "SiTracker_dEdxProcessor calculates dE/dx for planar silicon trackers" ;
// register steering parameters: name, description, class-variable, default value
registerInputCollection( LCIO::TRACK,
"TrackCollectionName" ,
"Name of the input Track collection" ,
m_trackCollName ,
std::string("SiTracks")
);
StringVec defaultTrkHitCollections;
defaultTrkHitCollections.push_back(std::string("ITrackerHits"));
defaultTrkHitCollections.push_back(std::string("ITrackerEndcapHits"));
defaultTrkHitCollections.push_back(std::string("OTrackerHits"));
defaultTrkHitCollections.push_back(std::string("OTrackerEndcapHits"));
defaultTrkHitCollections.push_back(std::string("VXDTrackerHits"));
defaultTrkHitCollections.push_back(std::string("VXDEndcapTrackerHits"));
registerProcessorParameter("TrkHitCollections" ,
"Tracker hit collections that will be analysed",
m_trkHitCollNames ,
defaultTrkHitCollections ) ;
int elementMask = 0;
for (unsigned ibit=0; ibit<sizeof(int)*CHAR_BIT; ibit++) {
elementMask += 1 << ibit;
}
registerProcessorParameter("CheatSensorThicknesses" ,
"Shall we use the sensitive thicknesses from parameters?",
m_cheatSensorThicknesses ,
false ) ;
FloatVec sensThicknessCheatVals;
for (unsigned i=0; i<defaultTrkHitCollections.size(); i++) {
sensThicknessCheatVals.push_back(-1);
}
registerProcessorParameter("SensorThicknessCheatValues" ,
"Sensor thicknesses to use instead of automatic values from DD4hep (if CheatSensorThicknesses==true).",
m_sensThicknessCheatVals ,
sensThicknessCheatVals ) ;
/* Type of estimator for dEdx
* Available estimators: mean, median, truncMean, harmonic, harmonic-2, weighted-harmonic, weighted-harmonic-2
*/
registerProcessorParameter("dEdxEstimator" ,
"Type of estimator for dEdx.",
m_dEdxEstimator ,
std::string("median") ) ;
}
SiTracker_dEdxProcessor::~SiTracker_dEdxProcessor() {
if (layerFinder) delete layerFinder;
}
void SiTracker_dEdxProcessor::init() {
streamlog_out(DEBUG) << " init called " << std::endl ;
// usually a good idea to
printParameters() ;
if(m_dEdxEstimator.compare("mean") == 0) {
dEdxEval = &SiTracker_dEdxProcessor::dEdxMean;
}
else if(m_dEdxEstimator.compare("median") == 0) {
dEdxEval = &SiTracker_dEdxProcessor::dEdxMedian;
}
else if (m_dEdxEstimator.compare("truncMean") == 0) {
dEdxEval = &SiTracker_dEdxProcessor::dEdxTruncMean;
}
else if (m_dEdxEstimator.compare("harmonic") == 0) {
dEdxEval = &SiTracker_dEdxProcessor::dEdxHarmonic;
}
else if (m_dEdxEstimator.compare("harmonic-2") == 0) {
dEdxEval = &SiTracker_dEdxProcessor::dEdxHarmonic2;
}
else if (m_dEdxEstimator.compare("weighted-harmonic") == 0) {
dEdxEval = &SiTracker_dEdxProcessor::dEdxWgtHarmonic;
}
else if (m_dEdxEstimator.compare("weighted-harmonic-2") == 0) {
dEdxEval = &SiTracker_dEdxProcessor::dEdxWgtHarmonic2;
}
else {
streamlog_out(ERROR) << "Unknown dE/dx evaluation method " << m_dEdxEstimator << ". Exiting.\n";
exit(0);
}
dd4hep::Detector& theDetector = dd4hep::Detector::getInstance();
dd4hep::rec::SurfaceManager& surfMan = *theDetector.extension< dd4hep::rec::SurfaceManager >() ;
surfMap = surfMan.map( "tracker" ) ;
if(!m_cheatSensorThicknesses) {
m_sensThicknessCheatVals.clear();
for(unsigned isub=0; isub<m_trkHitCollNames.size(); isub++) {
// This is how we tell the collection finder that we do not want to cheat any thickness values
m_sensThicknessCheatVals.push_back(-1.);
}
}
layerFinder = new LayerFinder(m_trkHitCollNames, theDetector, m_sensThicknessCheatVals);
// exit(0);
const double pos[3]={0,0,0};
double bFieldVec[3]={0,0,0};
theDetector.field().magneticField(pos,bFieldVec); // get the magnetic field vector from DD4hep
_bField = bFieldVec[2]/dd4hep::tesla; // z component at (0,0,0)
//trksystem for marlin track
trkSystem = MarlinTrk::Factory::createMarlinTrkSystem( "DDKalTest" , marlin::Global::GEAR , "" ) ;
if( trkSystem == 0 ) throw EVENT::Exception( std::string(" Cannot initialize MarlinTrkSystem of Type: ") + std::string("DDKalTest") );
trkSystem->setOption( MarlinTrk::IMarlinTrkSystem::CFG::useQMS, true );
//_MSOn ) ;
trkSystem->setOption( MarlinTrk::IMarlinTrkSystem::CFG::usedEdx, false) ;
trkSystem->setOption( MarlinTrk::IMarlinTrkSystem::CFG::useSmoothing, true) ;
trkSystem->init() ;
gROOT->ProcessLine("#include <vector>");
lastRunHeaderProcessed = -1;
for (unsigned i=0; i<nTimers; i++) {
timers.push_back(std::chrono::duration<double>(std::chrono::duration_values<double>::zero()));
}
streamlog_out(DEBUG) << " init done " << std::endl ;
}
void SiTracker_dEdxProcessor::processRunHeader( LCRunHeader* run) {
lastRunHeaderProcessed = run->getRunNumber();
}
void SiTracker_dEdxProcessor::processEvent( LCEvent * evt ) {
/* if (_lastRunHeaderProcessed < evt->getRunNumber()) {
// streamlog_out(ERROR) << "Run header has not been processed for this run! Exiting.\n";
// exit(0);
}*/
if(evt->getEventNumber()%100 == 0) {
streamlog_out(MESSAGE) << " processing event: " << evt->getEventNumber()
<< " in run: " << evt->getRunNumber() << std::endl ;
}
/************************************/
/*** Get collections ***/
/************************************/
lastTP = std::chrono::high_resolution_clock::now();
LCCollection* tracks = NULL;
try {
tracks = evt->getCollection(m_trackCollName);
}
catch(EVENT::DataNotAvailableException &dataex) {
streamlog_out(MESSAGE) << "Collection " << m_trackCollName << " not found. Skipping event #" << evt->getEventNumber() << ".\n";
tracks = NULL;
return;
}
tracks->getFlag();
addTime(0);
/*** Read collections to find a valid decoder for CELLID encoding ***/
if (layerFinder->ReadCollections(evt) != 0) {
streamlog_out(WARNING) << "None of the requested collections found in event #" << evt->getEventNumber() << ". Skipping event.\n";
return;
}
//layerFinder->ReportHandledDetectors();
addTime(1);
int nTracks = tracks->getNumberOfElements() ;
for (int i = 0; i < nTracks; i++)
{
streamlog_out(DEBUG5) << "Processing track #" << i << ".\n";
TrackImpl * track = dynamic_cast<TrackImpl*>( tracks->getElementAt(i) );
/*** Analyse hits and get dE/dx from each ***/
EVENT::TrackerHitVec trackhits = track->getTrackerHits();
MarlinTrk::IMarlinTrack* marlin_trk = trkSystem->createTrack();
for( auto it : trackhits ){
marlin_trk->addHit(it);
}//end loop on hits
const TrackStateImpl *trackState = dynamic_cast<const TrackStateImpl*>(track->getTrackState(TrackState::AtFirstHit));
if (!trackState) {
streamlog_out(WARNING) << "Cannot get track state for track #" << i
<< " in event " << evt->getEventNumber() << std::endl;
streamlog_out(WARNING) << "Skipping track.\n";
continue;
}
marlin_trk->initialise( *trackState, _bField, MarlinTrk::IMarlinTrack::forward ) ;
addTime(2);
dEdxVec dEdxHitVec;
for(unsigned int ihit = 0; ihit < trackhits.size(); ihit++) {
// Tangent to the track at hit position
dd4hep::rec::Vector3D hitpos(trackhits[ihit]->getPosition());
IMPL::TrackStateImpl ts;
double chi2 = 0.;
int ndf = 0;
marlin_trk->extrapolate(hitpos, ts, chi2, ndf);
addTime(3);
dd4hep::rec::Vector3D rp(ts.getReferencePoint());
float tanLambda = ts.getTanLambda();
float sinTheta = 1. / sqrt(1.+pow(tanLambda,2));
float phi = ts.getPhi();
float trackDirX = cos(phi)*sinTheta;
float trackDirY = sin(phi)*sinTheta;
float trackDirZ = tanLambda*sinTheta;
dd4hep::rec::Vector3D trackDir(trackDirX, trackDirY, trackDirZ);
addTime(4);
// Normal to the surface of hit
unsigned long cellid = trackhits[ihit]->getCellID0();
dd4hep::rec::SurfaceMap::const_iterator surface = surfMap->find(cellid);
if (surface == surfMap->end()) {
streamlog_out(ERROR) << "Cannot find the surface corresponding to track hit ID " << cellid
<< " in event " << evt->getEventNumber() << "!\n";
exit(0);
}
dd4hep::rec::Vector3D surfaceNormal = surface->second->normal();
addTime(5);
double norm = sqrt(trackDir.dot(trackDir)*surfaceNormal.dot(surfaceNormal));
if (norm < FLT_MIN) continue;
double cosAngle = fabs(trackDir.dot(surfaceNormal)) / norm ;
double thickness = layerFinder->SensitiveThickness(dynamic_cast<TrackerHitPlane*>(trackhits[ihit]));
if (thickness < 0.) {
streamlog_out(ERROR) << "Could not find hit collection corresponding to hit CellID " << cellid
<< ", hit ID " << trackhits.at(ihit)->id() << " .\n";
streamlog_out(ERROR) << "Event #" << evt->getEventNumber() << ".\n";
exit(0);
}
if (thickness < 0.00001) {
streamlog_out(ERROR) << "GetThickness returned zero!\n";
exit(0);
}
double effThickness = thickness / cosAngle;
dEdxHitVec.push_back( dEdxPoint(trackhits[ihit]->getEDep(), effThickness) );
addTime(6);
}
if(dEdxHitVec.size() == 0) continue;
double dEdx, dEdxError;
dEdx = dEdxEval(dEdxHitVec, dEdxError);
// Todo: This is read-only if track is read from existing lcio file!
// Is there a way to process tracks that are read from the input file?
track->setdEdx(dEdx);
track->setdEdxError(dEdxError);
addTime(7);
}
}
void SiTracker_dEdxProcessor::check( LCEvent * /*evt*/ ) {
// nothing to check here - could be used to fill checkplots in reconstruction processor
}
/**/
void SiTracker_dEdxProcessor::end(){
for (unsigned i=0; i<timers.size(); i++) {
streamlog_out(MESSAGE) << "Total time in timer #" << i << ": " << timers.at(i).count() << " s\n";
}
// std::cout << "SiTracker_dEdxProcessor::end() " << name()
// << " processed " << _nEvt << " events in " << _nRun << " runs "
// << std::endl ;
delete layerFinder;
layerFinder = NULL;
}
/*************************************************************
*
* Evaluation methods for dE/dx
*
************************************************************/
double SiTracker_dEdxProcessor::truncFractionUp = 0.3;
double SiTracker_dEdxProcessor::truncFractionLo = 0.1;
// Weighted truncated mean with arbitrary truncation
// Weight of a measurement is the material thickness traversed in the hit
double SiTracker_dEdxProcessor::dEdxGeneralTruncMean(dEdxVec hitVec, double &dEdxError,
const double truncLo, const double truncHi) {
const unsigned n = hitVec.size();
const unsigned iStart = static_cast<unsigned>(floor(n*truncLo + 0.5));
const unsigned iEnd = static_cast<unsigned>(floor(n*(1-truncHi) + 0.5));
if(iEnd-iStart == 0) {
dEdxError = 0;
return 0;
}
if(iEnd-iStart == 1) {
dEdxError = hitVec.at(iStart).Get_dEdx() ;
return hitVec.at(iStart).Get_dEdx() ;
}
sort(hitVec.begin(), hitVec.end(), dEdxOrder);
double eDepSum = 0.;
double thickness = 0.;
double mu2dEdx = 0.;
for (unsigned i=iStart; i<iEnd; i++) {
eDepSum += hitVec.at(i).Get_dE();
thickness += hitVec.at(i).Get_dx();
mu2dEdx += pow(hitVec.at(i).Get_dE(), 2) / hitVec.at(i).Get_dx();
}
mu2dEdx /= thickness;
double track_dEdx = eDepSum / thickness;
dEdxError = sqrt((mu2dEdx - pow(track_dEdx, 2))/(iEnd-iStart));
return track_dEdx;
}
// Weighted mean
// Weight of a measurement is the material thickness traversed in the hit
double SiTracker_dEdxProcessor::dEdxMean(dEdxVec hitVec, double &dEdxError) {
return dEdxGeneralTruncMean(hitVec, dEdxError, 0., 0.);
}
// Median
double SiTracker_dEdxProcessor::dEdxMedian(dEdxVec hitVec, double &dEdxError) {
const unsigned n = hitVec.size();
if(n == 0) {
dEdxError = 0;
return 0;
}
if(n == 1) {
dEdxError = hitVec.at(0).Get_dEdx();
return hitVec.at(0).Get_dEdx();
}
sort(hitVec.begin(), hitVec.end(), dEdxOrder);
double median=0.;
if (n%2 ==1) {
median = hitVec.at(n/2).Get_dEdx();
}
else {
median = (hitVec.at(n/2-1).Get_dEdx() + hitVec.at(n/2).Get_dEdx()) / 2;
}
// Substituting error of the mean for dEdxError here
// instead of bootstrapping the error of the median
dEdxMean(hitVec, dEdxError);
return median;
}
// Weighted truncated mean with standard truncation
// Weight of a measurement is the material thickness traversed in the hit
double SiTracker_dEdxProcessor::dEdxTruncMean(dEdxVec hitVec, double &dEdxError) {
return dEdxGeneralTruncMean(hitVec, dEdxError, truncFractionLo, truncFractionUp);
}
// Simple harmonic mean
double SiTracker_dEdxProcessor::dEdxHarmonic(dEdxVec hitVec, double &dEdxError) {
const unsigned n = hitVec.size();
if(n == 0) {
dEdxError = 0;
return 0;
}
if(n == 1) {
dEdxError = hitVec.at(0).Get_dEdx();
return hitVec.at(0).Get_dEdx();
}
// Calculation of the first and the second moment of
// 1 / (dE/dx)
double mu1sum = 0.;
double mu2sum = 0.;
for (unsigned i=0; i<n; i++) {
double inverse = 1/hitVec.at(i).Get_dEdx();
mu1sum += inverse;
mu2sum += pow( inverse, 2 );
}
double mu2 = mu2sum / n;
double mu1 = mu1sum / n;
double sigma = sqrt( (mu2 - pow(mu1, 2)) / n );
double dEdx = 1 / mu1;
dEdxError = sigma / pow(mu1, 2) ;
return dEdx;
}
// Simple harmonic-squared mean
double SiTracker_dEdxProcessor::dEdxHarmonic2(dEdxVec hitVec, double &dEdxError) {
const unsigned n = hitVec.size();
if(n == 0) {
dEdxError = 0;
return 0;
}
if(n == 1) {
dEdxError = hitVec.at(0).Get_dEdx();
return hitVec.at(0).Get_dEdx();
}
// Calculation of the first and the second moment of
// 1 / (dE/dx)^2
double mu1sum = 0.;
double mu2sum = 0.;
for (unsigned i=0; i<n; i++) {
double sqinverse = pow(hitVec.at(i).Get_dEdx(), -2);
mu1sum += sqinverse;
mu2sum += pow( sqinverse, 2 );
}
double mu2 = mu2sum / n;
double mu1 = mu1sum / n;
double sigma = sqrt( (mu2 - pow(mu1, 2)) / n );
double dEdx = 1 / sqrt(mu1);
dEdxError = sigma * pow(dEdx, 3) / 2 ;
return dEdx;
}
// Weighted harmonic mean
// Weight of a measurement is the material thickness traversed in the hit
double SiTracker_dEdxProcessor::dEdxWgtHarmonic(dEdxVec hitVec, double &dEdxError) {
const unsigned n = hitVec.size();
if(n == 0) {
dEdxError = 0;
return 0;
}
if(n == 1) {
dEdxError = hitVec.at(0).Get_dEdx();
return hitVec.at(0).Get_dEdx();
}
// Calculation of the first and the second moment of
// 1 / (dE/dx)
double mu1sum = 0.;
double mu2sum = 0.;
double wgtsum = 0.;
for (unsigned i=0; i<n; i++) {
double inverse = 1/hitVec.at(i).Get_dEdx();
double wgt = hitVec.at(i).Get_dx();
mu1sum += wgt*inverse;
mu2sum += wgt*pow( inverse, 2 );
wgtsum += wgt;
}
double mu2 = mu2sum / wgtsum;
double mu1 = mu1sum / wgtsum;
double sigma = sqrt( (mu2 - pow(mu1, 2)) / n );
double dEdx = 1 / mu1;
dEdxError = sigma * pow(dEdx, 2) ;
return dEdx;
}
// Weighted harmonic-squared mean
// Weight of a measurement is the material thickness traversed in the hit
double SiTracker_dEdxProcessor::dEdxWgtHarmonic2(dEdxVec hitVec, double &dEdxError) {
const unsigned n = hitVec.size();
if(n == 0) {
dEdxError = 0;
return 0;
}
if(n == 1) {
dEdxError = hitVec.at(0).Get_dEdx();
return hitVec.at(0).Get_dEdx();
}
// Calculation of the first and the second moment of
// 1 / (dE/dx)^2
double mu1sum = 0.;
double mu2sum = 0.;
double wgtsum = 0.;
for (unsigned i=0; i<n; i++) {
double sqinverse = pow(hitVec.at(i).Get_dEdx(), -2);
double wgt = hitVec.at(i).Get_dx();
mu1sum += wgt*sqinverse;
mu2sum += wgt*pow( sqinverse, 2 );
wgtsum += wgt;
}
double mu2 = mu2sum / wgtsum;
double mu1 = mu1sum / wgtsum;
double sigma = sqrt( (mu2 - pow(mu1, 2)) / n );
double dEdx = 1 / sqrt(mu1);
dEdxError = sigma * pow(dEdx, 3) / 2 ;
return dEdx;
}