Newer
Older
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
/***********************************************************************************\
* (c) Copyright 1998-2019 CERN for the benefit of the LHCb and ATLAS collaborations *
* *
* This software is distributed under the terms of the Apache version 2 licence, *
* copied verbatim in the file "LICENSE". *
* *
* In applying this licence, CERN does not waive the privileges and immunities *
* granted to it by virtue of its status as an Intergovernmental Organization *
* or submit itself to any jurisdiction. *
\***********************************************************************************/
/* The algorithm is created by Mingrui ZHAO.
* It is transplanted from Marlin to Gaudi by Shunan ZHANG and Mingrui ZHAO.
* It is now maintained by Mingrui ZHAO (mingrui.zhao@mail.labz0.org)
* please contact if you have any question.
*
* ------------------------------------
* The algorithm inspects the MCParticles and the correponding tracks.
* The output of the algorithm is a tuple.
* In each element of the tuple, it contains:
* The information of the MCParticle,
* The information of the corresponding tracks.
* If the number of reconstructed track candidates is larger than 1,
* each reconstructed track will occupy an element of the tuple, and they
* will be differred through the "nCandidates",
*
* Establishment of the correspondance:
* MCParticle -(1)-> SimTrackerHit -(2)-> TrackerHits -(3)-> Track
*
*
*
*/
// Include files
#include "TrackInspectAlg.h"
#include "GaudiKernel/DataObject.h"
#include "GaudiKernel/IHistogramSvc.h"
#include "GaudiKernel/MsgStream.h"
#include "GaudiKernel/SmartDataPtr.h"
#include "DataHelper/HelixClass.h"
#include "podio/podioVersion.h"
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
#include "CLHEP/Units/SystemOfUnits.h"
#include <math.h>
#include <TTree.h>
#include <TFile.h>
#include <TLorentzVector.h>
#include <TVector3.h>
#include <vector>
#include <iostream>
DECLARE_COMPONENT( TrackInspectAlg )
//------------------------------------------------------------------------------
TrackInspectAlg::TrackInspectAlg( const std::string& name, ISvcLocator* pSvcLocator )
: Algorithm( name, pSvcLocator ) {
declareProperty("TrackCollection", _inTrackColHdl, "Handle of the Input Track collection");
declareProperty("MCParticleCollection", _inMCParticleColHdl, "Handle of the Input MC particle collection");
declareProperty("TPCTrackerHitRelations", _TPCRelColHdl, "Handle of the TPC Tracker Hit relations");
declareProperty("VXDTrackerHitRelations", _VXDRelColHdl, "Handle of the TPC Tracker Hit relations");
declareProperty("SITTrackerHitRelations", _SITRelColHdl, "Handle of the TPC Tracker Hit relations");
declareProperty("SETTrackerHitRelations", _SETRelColHdl, "Handle of the TPC Tracker Hit relations");
declareProperty("FTDTrackerHitRelations", _FTDRelColHdl, "Handle of the TPC Tracker Hit relations");
declareProperty("useTPC", _useTPC, "flag whether to use TPC hits");
declareProperty("useVXD", _useVXD, "flag whether to use VXD hits");
declareProperty("useSIT", _useSIT, "flag whether to use SIT hits");
declareProperty("useSET", _useSET, "flag whether to use SET hits");
declareProperty("useFTD", _useFTD, "flag whether to use FTD hits");
m_thisName = name;
}
//------------------------------------------------------------------------------
StatusCode TrackInspectAlg::initialize(){
info() << "Booking Ntuple" << endmsg;
NTuplePtr nt1(ntupleSvc(), "MyTuples/Track"+m_thisName);
if ( !nt1 ) {
m_tuple = ntupleSvc()->book("MyTuples/Track"+m_thisName,CLID_ColumnWiseTuple,"Tracking result");
if ( 0 != m_tuple ) {
m_tuple->addItem ("nmc", m_nParticles, 0, 1000 ).ignore();
m_tuple->addIndexedItem ("vx", m_nParticles, vx ).ignore();
m_tuple->addIndexedItem ("vy", m_nParticles, vy ).ignore();
m_tuple->addIndexedItem ("vz", m_nParticles, vz ).ignore();
m_tuple->addIndexedItem ("ex", m_nParticles, ex ).ignore();
m_tuple->addIndexedItem ("ey", m_nParticles, ey ).ignore();
m_tuple->addIndexedItem ("ez", m_nParticles, ez ).ignore();
m_tuple->addIndexedItem ("Omega", m_nParticles, Omega ).ignore();
m_tuple->addIndexedItem ("D0", m_nParticles, D0 ).ignore();
m_tuple->addIndexedItem ("Z0", m_nParticles, Z0 ).ignore();
m_tuple->addIndexedItem ("Phi", m_nParticles, Phi ).ignore();
m_tuple->addIndexedItem ("TanLambda", m_nParticles, TanLambda ).ignore();
m_tuple->addIndexedItem ("TRUEPX", m_nParticles, TRUEPX ).ignore();
m_tuple->addIndexedItem ("TRUEPY", m_nParticles, TRUEPY ).ignore();
m_tuple->addIndexedItem ("TRUEPZ", m_nParticles, TRUEPZ ).ignore();
m_tuple->addIndexedItem ("TRUEPE", m_nParticles, TRUEPE ).ignore();
m_tuple->addIndexedItem ("TRUEPT", m_nParticles, TRUEPT ).ignore();
m_tuple->addIndexedItem ("TRUEP", m_nParticles, TRUEP ).ignore();
m_tuple->addIndexedItem ("TRUEETA", m_nParticles, TRUEETA ).ignore();
m_tuple->addIndexedItem ("TRUEY", m_nParticles, TRUEY ).ignore();
m_tuple->addIndexedItem ("TRUETHETA", m_nParticles, TRUETHETA ).ignore();
m_tuple->addIndexedItem ("eventNumber", m_nParticles, eventNumber ).ignore();
m_tuple->addIndexedItem ("particleNumber", m_nParticles, particleNumber ).ignore();
m_tuple->addIndexedItem ("totalCandidates", m_nParticles, totalCandidates ).ignore();
m_tuple->addIndexedItem ("nCandidate", m_nParticles, nCandidate ).ignore();
m_tuple->addIndexedItem ("nHits", m_nParticles, nHits ).ignore();
m_tuple->addIndexedItem ("pid", m_nParticles, pid ).ignore();
m_tuple->addIndexedItem ("WMSelectionVariable", m_nParticles, WMSelectionVariable).ignore();
}
else { // did not manage to book the N tuple....
fatal() << "Cannot book MyTuples/Track"+m_thisName <<endmsg;
return StatusCode::FAILURE;
}
}
else{
m_tuple = nt1;
}
_nEvt = 0;
return StatusCode::SUCCESS;
}
//------------------------------------------------------------------------------
StatusCode TrackInspectAlg::execute(){
debug() << "TrackInspectAlg::execute() ------ start ------" << endmsg;
hitmap.clear();
mcpHitMap.clear();
matchvec.clear();
std::vector<const edm4hep::MCRecoTrackerAssociationCollection*> relCols;
for (auto relCol: relCols) {
if (relCol){
for (auto rel: *relCol){
std::pair<edm4hep::TrackerHit, edm4hep::MCParticle> p = std::make_pair(rel.getRec(), rel.getSim().getMCParticle());
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
if (hitmap.find(p) == hitmap.end()) hitmap[p] = 0.;
hitmap[p] += rel.getWeight();
}
}
}
// Establish the relation of MCParticle --> Track
// Put the relation of MCParticle --> Track to matchvec
const edm4hep::TrackCollection* trkCol = nullptr;
try {
trkCol = _inTrackColHdl.get();
}
catch ( GaudiException &e ) {
debug() << "Collection " << _inTrackColHdl.fullKey() << " is unavailable in event " << _nEvt << endmsg;
}
const edm4hep::MCParticleCollection* mcpCol = nullptr;
try {
mcpCol = _inMCParticleColHdl.get();
}
catch ( GaudiException &e ) {
debug() << "Collection " << _inMCParticleColHdl.fullKey() << " is unavailable in event " << _nEvt << endmsg;
}
if (trkCol && mcpCol){
for (auto track: *trkCol){
for (auto particle: *mcpCol){
double match_weight = match(particle, track);
if (match_weight > 0.2){
std::tuple<edm4hep::MCParticle, edm4hep::Track, double> tuple = std::make_tuple(particle, track, match_weight);
matchvec.push_back(tuple);
}
}
}
}
if (mcpCol){
// MCParticleHitAssociator(mcpCol);
m_nParticles = 0;
for (auto particle: *mcpCol) {
std::vector<edm4hep::Track> theTracks = MCParticleTrackAssociator(particle);
if (theTracks.size() == 0) {
totalCandidates[m_nParticles] = 0;
nCandidate[m_nParticles] = -1;
#if PODIO_BUILD_VERSION < PODIO_VERSION(0, 17, 4)
Fill(particle, edm4hep::Track(nullptr));
#else
Fill(particle, edm4hep::Track::makeEmpty());
#endif
m_nParticles++;
}
else {
for (unsigned j = 0; j < theTracks.size(); j++) {
totalCandidates[m_nParticles] = theTracks.size();
nCandidate[m_nParticles] = j;
Fill(particle, theTracks[j]);
m_nParticles++;
}
}
}
debug() << "MCParticle: " << m_nParticles << endmsg;
}
m_tuple->write();
_nEvt++;
debug() << "TrackInspectAlg::execute() ------ end ------" << endmsg;
return StatusCode::SUCCESS;
}
double TrackInspectAlg::match(edm4hep::MCParticle particle, edm4hep::Track track){
int NHits = track.trackerHits_size();
double matchedHits = 0;
double usedHits = 0;
for (int i = 0; i < NHits; i++) {
std::pair<edm4hep::TrackerHit, edm4hep::MCParticle> ele = std::make_pair(hit, particle);
//std::cout << "lookup --> " << ele.first << std::endl;
//if (hitmap.find(ele) != hitmap.end() ) {
//std::cout << "find --> " << hitmap[ele] << std::endl;
//}
if (hitmap.find(ele) != hitmap.end() && hitmap[ele] > 0.2) {
matchedHits++;
}
}
// UTIL::BitField64* encoder = new UTIL::BitField64(lcio::ILDCellID0::encoder_string);
// encoder->setValue(hit->getCellID0());
// int detID = (*encoder)[lcio::ILDCellID0::subdet];
// if (detID < 0 || !usedDetectorsArray[detID]) continue;
// delete encoder;
return matchedHits / usedHits;
}
void TrackInspectAlg::Fill(edm4hep::MCParticle particle, edm4hep::Track theTrack) {
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
pid[m_nParticles] = particle.getPDG();
vx[m_nParticles] = particle.getVertex().x;
vy[m_nParticles] = particle.getVertex().y;
vz[m_nParticles] = particle.getVertex().z;
ex[m_nParticles] = particle.getEndpoint().x;
ey[m_nParticles] = particle.getEndpoint().y;
ez[m_nParticles] = particle.getEndpoint().z;
TLorentzVector v2;
v2.SetXYZM(
particle.getMomentum().x,
particle.getMomentum().y,
particle.getMomentum().z,
particle.getMass() );
TRUEPX[m_nParticles] = v2.X();
TRUEPY[m_nParticles] = v2.Y();
TRUEPZ[m_nParticles] = v2.Z();
TRUEPE[m_nParticles] = v2.E();
TRUEPT[m_nParticles] = v2.Pt();
TRUEP[m_nParticles] = v2.P();
TRUEETA[m_nParticles] = v2.PseudoRapidity();
TRUEY[m_nParticles] = v2.Rapidity();
TRUETHETA[m_nParticles] = v2.Theta();
// TVector3 theMomentum = TVector3(
// particle->getMomentum().x,
// particle->getMomentum().y,
// particle->getMomentum().z );
// WMSelectionVariable = (
// theParticle->isDecayedInTracker()==0 &&
// theMomentum.Perp()>1.0 &&
// theParticle->isDecayedInCalorimeter() &&
// theParticle->getGeneratorStatus() == 1 &&
// sin(theMomentum.Theta()) > 0.18 );
if (theTrack.isAvailable()) {
for (std::vector<edm4hep::TrackState>::const_iterator it = theTrack.trackStates_end() - 1; it != theTrack.trackStates_begin() - 1; it--){
edm4hep::TrackState trackState = *it;
Omega[m_nParticles] = trackState.omega;
TanLambda[m_nParticles] = trackState.tanLambda;
Phi[m_nParticles] = trackState.phi;
D0[m_nParticles] = trackState.D0;
Z0[m_nParticles] = trackState.Z0;
nHits[m_nParticles] = theTrack.trackerHits_size();
}
}
else {
Omega[m_nParticles] = -1;
TanLambda[m_nParticles] = -1;
Phi[m_nParticles] = -1;
D0[m_nParticles] = -1;
Z0[m_nParticles] = -1;
nHits[m_nParticles] = 0;
}
}
std::vector<edm4hep::Track> TrackInspectAlg::MCParticleTrackAssociator(edm4hep::MCParticle theParticle) {
std::vector<edm4hep::Track> theTracks;
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
// std::cout << "The particle: " << theParticle.getPDG() << " " << theParticle << std::endl;
for (auto matchtuple: matchvec){
if (std::get<0>(matchtuple) == theParticle){
if (std::get<2>(matchtuple) > _weight){
theTracks.push_back(std::get<1>(matchtuple));
}
}
}
return theTracks;
}
void TrackInspectAlg::initializeRelationCollections(std::vector<const edm4hep::MCRecoTrackerAssociationCollection*> &relCols) {
// Use TPC
if (_useTPC) {
const edm4hep::MCRecoTrackerAssociationCollection* relCol = nullptr;
// Establish the relation of MCParticle --> TrackerHit
try {
relCol = _TPCRelColHdl.get();
}
catch ( GaudiException &e ) {
debug() << "Collection " << _TPCRelColHdl.fullKey() << " is unavailable in event " << _nEvt << endmsg;
}
relCols.push_back(relCol);
}
// Use VXD
if (_useVXD) {
const edm4hep::MCRecoTrackerAssociationCollection* relCol = nullptr;
// Establish the relation of MCParticle --> TrackerHit
try {
relCol = _VXDRelColHdl.get();
}
catch ( GaudiException &e ) {
debug() << "Collection " << _VXDRelColHdl.fullKey() << " is unavailable in event " << _nEvt << endmsg;
}
relCols.push_back(relCol);
}
// Use SIT
if (_useSIT) {
const edm4hep::MCRecoTrackerAssociationCollection* relCol = nullptr;
// Establish the relation of MCParticle --> TrackerHit
try {
relCol = _SITRelColHdl.get();
}
catch ( GaudiException &e ) {
debug() << "Collection " << _SITRelColHdl.fullKey() << " is unavailable in event " << _nEvt << endmsg;
}
relCols.push_back(relCol);
}
// Use SET
if (_useSET) {
const edm4hep::MCRecoTrackerAssociationCollection* relCol = nullptr;
// Establish the relation of MCParticle --> TrackerHit
try {
relCol = _SETRelColHdl.get();
}
catch ( GaudiException &e ) {
debug() << "Collection " << _SETRelColHdl.fullKey() << " is unavailable in event " << _nEvt << endmsg;
}
relCols.push_back(relCol);
}
// Use FTD
if (_useFTD) {
const edm4hep::MCRecoTrackerAssociationCollection* relCol = nullptr;
// Establish the relation of MCParticle --> TrackerHit
try {
relCol = _FTDRelColHdl.get();
}
catch ( GaudiException &e ) {
debug() << "Collection " << _FTDRelColHdl.fullKey() << " is unavailable in event " << _nEvt << endmsg;
}
relCols.push_back(relCol);
}
}
//------------------------------------------------------------------------------
StatusCode TrackInspectAlg::finalize(){
debug() << "Finalizing..." << endmsg;
return StatusCode::SUCCESS;
}