Belle II Software  release-06-00-14
VXDCDCTrackMergerModule.cc
1 /**************************************************************************
2  * basf2 (Belle II Analysis Software Framework) *
3  * Author: The Belle II Collaboration *
4  * *
5  * See git log for contributors and copyright holders. *
6  * This file is licensed under LGPL-3.0, see LICENSE.md. *
7  **************************************************************************/
8 #include <tracking/modules/vxdCDCTrackMerger/VXDCDCTrackMergerModule.h>
9 
10 #include <tracking/trackFitting/fitter/base/TrackFitter.h>
11 
12 using namespace Belle2;
13 
14 REG_MODULE(VXDCDCTrackMerger)
15 
17  Module(), m_CDC_wall_radius(16.25)
18 {
19  setDescription(
20  "This module merges tracks which are reconstructed, separately, in the silicon (PXD+VXD) and in the CDC");
21  setPropertyFlags(c_ParallelProcessingCertified);
22 
23  //input
24  addParam("CDCRecoTrackColName", m_CDCRecoTrackColName, "CDC Reco Tracks");
25  addParam("VXDRecoTrackColName", m_VXDRecoTrackColName, "VXD Reco Tracks");
26 
27  //merging parameters
28  addParam("merge_radius", m_merge_radius,
29  "Maximum distance between extrapolated tracks on the CDC wall",
30  double(2.0));
31 }
32 
34 {
37 
38  m_CDCRecoTracks.registerRelationTo(m_VXDRecoTracks);
39  m_VXDRecoTracks.registerRelationTo(m_CDCRecoTracks);
40 }
41 
43 {
44  //get CDC tracks
45  unsigned int nCDCTracks = m_CDCRecoTracks.getEntries();
46  B2DEBUG(9, "VXDCDCTrackMerger: input Number of CDC Tracks: " << nCDCTracks);
47 
48  //get VXD tracks
49  unsigned int nVXDTracks = m_VXDRecoTracks.getEntries();
50  B2DEBUG(9,
51  "VXDCDCTrackMerger: input Number of VXD Tracks: " << nVXDTracks);
52 
53  // position and momentum used for extrapolations to the CDC Wall
54  TVector3 position(0., 0., 0.);
55  TVector3 momentum(0., 0., 1.);
56  // position and momentum of the track extrapolated from the CDC fit to the CDC Wall
57  TVector3 cdcpos;
58  TVector3 cdcmom;
59  // position and momentum of the track extrapolated from the VXD fit to the CDC Wall
60  TVector3 vxdpos;
61  TVector3 vxdmom;
62 
63  // Fit all cdc and vxd tracks
64  TrackFitter fitter;
65  for (auto& cdcTrack : m_CDCRecoTracks) {
66  fitter.fit(cdcTrack);
67  }
68  for (auto& vxdTrack : m_VXDRecoTracks) {
69  fitter.fit(vxdTrack);
70  }
71 
72  for (auto& cdcTrack : m_CDCRecoTracks) {
73  // skip CDC Tracks which were not properly fitted
74  if (!cdcTrack.wasFitSuccessful())
75  continue;
76 
77  // skip CDC if it has already a match
78  if (cdcTrack.getRelated<RecoTrack>(m_VXDRecoTrackColName)) {
79  continue;
80  }
81 
82  bool matched_track = false;
83 
84  // start search with worst chi^2 imaginable
85  double this_chi_2 = std::numeric_limits<double>::max();
86  double chi_2_max_this_cdc_track = std::numeric_limits<double>::max();
87  try {
89  cdcTrack.getMeasuredStateOnPlaneFromFirstHit();
90  cdc_sop.extrapolateToCylinder(m_CDC_wall_radius, position, momentum);
91  cdcpos = cdc_sop.getPos();
92  cdcmom = cdc_sop.getMom();
93  } catch (...) {
94  B2DEBUG(9, "CDCTrack extrapolation to cylinder failed!");
95  continue;
96  }
97 
98  // TODO: this can be done independent of each other ....
99  int currentVxdTrack = -1;
100  int bestMatchedVxdTrack = 0;
101  for (auto& vxdTrack : m_VXDRecoTracks) {
102  currentVxdTrack++;
103  // skip VXD Tracks which were not properly fitted
104  if (!vxdTrack.wasFitSuccessful()) {
105  continue;
106  }
107 
108  // skip VXD if it has already a match
109  if (vxdTrack.getRelated<RecoTrack>(m_CDCRecoTrackColName)) {
110  continue;
111  }
112 
113  try {
115  vxdTrack.getMeasuredStateOnPlaneFromLastHit();
116  vxd_sop.extrapolateToCylinder(m_CDC_wall_radius, position, momentum);
117  vxdpos = vxd_sop.getPos();
118  vxdmom = vxd_sop.getMom();
119  } catch (genfit::Exception const&) {
120  // extrapolation not possible, skip this track
121  B2DEBUG(9, "VXDTrack extrapolation to cylinder failed!");
122  continue;
123  }
124 
125  // Extrapolate tracks to same plane & Match Tracks
126  if (TMath::Sqrt((cdcpos - vxdpos) * (cdcpos - vxdpos)) < m_merge_radius) {
129  try {
130  vxd_sop = vxdTrack.getMeasuredStateOnPlaneFromLastHit();
131  cdc_sop = cdcTrack.getMeasuredStateOnPlaneFromFirstHit();
132  cdc_sop.extrapolateToCylinder(m_CDC_wall_radius, position, momentum);
133  vxd_sop.extrapolateToPlane(cdc_sop.getPlane());
134  } catch (genfit::Exception const&) {
135  // extrapolation not possible, skip this track
136  B2DEBUG(9, "VXDTrack extrapolation to plane failed!");
137  continue;
138  }
139 
140  try {
141  TMatrixDSym inv_covmtrx = (vxd_sop.getCov() + cdc_sop.getCov()).Invert();
142  TVectorD state_diff = cdc_sop.getState() - vxd_sop.getState();
143  state_diff *= inv_covmtrx;
144  this_chi_2 = state_diff * (cdc_sop.getState() - vxd_sop.getState());
145  } catch (...) {
146  B2WARNING("Matrix is singular!");
147  continue;
148  }
149 
150  if ((this_chi_2 < chi_2_max_this_cdc_track) && (this_chi_2 > 0)) {
151  matched_track = true;
152  chi_2_max_this_cdc_track = this_chi_2;
153  bestMatchedVxdTrack = currentVxdTrack;
154  }
155  } //If on radius
156  } //end loop on VXD tracks
157 
158  if (matched_track) {
159  // -1 is the convention for "before the CDC track" in the related tracks combiner
160  m_VXDRecoTracks[bestMatchedVxdTrack]->addRelationTo(&cdcTrack, -1);
161  }
162  }
163 }
Base class for Modules.
Definition: Module.h:72
This is the Reconstruction Event-Data Model Track.
Definition: RecoTrack.h:76
Algorithm class to handle the fitting of RecoTrack objects.
Definition: TrackFitter.h:114
VXDCDCTrackMergerModule a module to merge VXD and CDC tracks.
StoreArray< RecoTrack > m_VXDRecoTracks
StoreArray of the VXD Track collection.
StoreArray< RecoTrack > m_CDCRecoTracks
StoreArray of the CDC Track collection.
void initialize() override
Use this to initialize resources or memory your module needs.
void event() override
Called once for each event.
std::string m_VXDRecoTrackColName
StoreArray name of the VXD Track collection.
double m_merge_radius
Maximum distance between extrapolated tracks on the CDC wall.
std::string m_CDCRecoTrackColName
StoreArray name of the CDC Track collection.
double m_CDC_wall_radius
Radius of the inner CDC wall in centimeters.
Exception class for error handling in GENFIT (provides storage for diagnostic information)
Definition: Exception.h:48
#StateOnPlane with additional covariance matrix.
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition: Module.h:650
Abstract base class for different kinds of events.