Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TruthRecoTrackMatching.h
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file TruthRecoTrackMatching.h
1 #ifndef TRUTHTRKMATCHER__H
2 #define TRUTHTRKMATCHER__H
3 #include "TrackClusEvaluator.h"
4 
5 #include <fun4all/SubsysReco.h>
6 
8 #include <trackbase/TrkrDefs.h>
10 
11 #include <array>
12 #include <iostream>
13 #include <map>
14 #include <set>
15 #include <string>
16 #include <tuple>
17 #include <vector>
18 
20 class PHCompositeNode;
24 class SvtxTrack;
25 class SvtxTrackMap;
26 class TrackSeedContainer;
27 class TrkrCluster;
29 class TrkrTruthTrack;
32 class TTree;
33 class TFile;
34 
36 {
37  //--------------------------------------------------
38  // Standard public interface
39  //--------------------------------------------------
40  public:
41  TruthRecoTrackMatching( // Criteria to match a TrkrClusterContainer and track
42  /*-----------------------------------------------------------------------------------
43  * Input criteria for Truth Track (with nT clusters) to reco track (with nR clusters) :
44  * - nmin_match : minimum number of clusters in Truth and Reco track that must
45  * match for tracks to match
46  * - cutoff_dphi : maximum distance out in |phi_truth-phi_reco| for a matched track
47  * - same_dphi : within this distance, all tracks must be checked
48  * - cutoff_deta : like cutoff_dphi but for eta
49  * - same_deta : like same_dphi but for eta
50  * - cluster_nzwidths : z-distance allowed for the truth center to be from
51  * reco-cluster center:
52  * |z_true-z_reco| must be <= dz_reco * cluster_nzwidths
53  * - cluster_nphiwidths : like cluster_nzwidths for phi
54  *--------------------------------------------------------*/
55  TrkrClusterIsMatcher* _ismatcher
56  , const unsigned short _nmin_match = 4
57  , const float _nmin_ratio = 0.
58  , const float _cutoff_dphi = 0.3
59  , const float _same_dphi = 0.05
60  , const float _cutoff_deta = 0.3
61  , const float _same_deta = 0.05
62  , const unsigned short _max_nreco_per_truth = 4
63  , const unsigned short _max_ntruth_per_reco = 4); // for some output kinematis
64 
65  ~TruthRecoTrackMatching() override = default;
66  int Init(PHCompositeNode*) override { return 0; };
67  int InitRun(PHCompositeNode*) override; //`
68  int process_event(PHCompositeNode*) override; //`
69  int End(PHCompositeNode*) override;
70 
73 
74  int createNodes(PHCompositeNode* topNode);
75 
76  void set_min_cl_match (unsigned short val) { m_nmincluster_match = val; };
77  void set_min_cl_ratio (float val) { m_nmincluster_ratio = val ; };
78  void set_cutoff_deta (float val) { m_cutoff_deta = val; };
79  void set_cutoff_dphi (float val) { m_cutoff_dphi = val; };
81  void set_smallsearch_deta (float val) { m_same_deta = val; };
82  void set_smallsearch_dphi (float val) { m_same_dphi = val; };
83 
84  void set_max_nreco_per_truth(unsigned short val) { m_max_nreco_per_truth = val; };
85  void set_max_ntruth_per_reco(unsigned short val) { m_max_ntruth_per_reco = val; };
86 
87  private:
88  //--------------------------------------------------
89  // Internal functions
90  //--------------------------------------------------
91 
92  //--------------------------------------------------
93  // Constant parameters for track matching
94  //--------------------------------------------------
95 
96  unsigned short m_nmincluster_match; // minimum of matched clustered to keep a truth to emb match
97  float m_nmincluster_ratio; // minimum ratio of truth clustered that must be matched in reconstructed track
98 
99  double m_cutoff_dphi; // how far in |phi_truth-phi_reco| to match
100  double m_same_dphi; // |phi_truth-phi_reco| to auto-evaluate (is < _m_cutoff_dphi)
101  double m_cutoff_deta; // how far in |eta_truth-eta_reco| to match
102  double m_same_deta; // |eta_truth-eta_reco| to auto-evaluate (is < m_cutoff_deta)
103 
104  /* double m_cluster_nzwidths; // cutoff in *getPhiSize() in cluster for |cluster_phi_truth-cluster_phi_reco| to match */
105  /* double m_cluster_nphiwidths; // same for eta */
106 
107  unsigned short m_max_nreco_per_truth;
108  unsigned short m_max_ntruth_per_reco;
109 
110  /* std::array<double, 55> m_phistep {0.}; // the phistep squared */
111  /* double m_zstep {0.}; */
112 
113  std::map<unsigned short, unsigned short> m_nmatched_index_true;
114 
115  std::map<unsigned short, unsigned short>* m_nmatched_id_reco = nullptr;
116  std::map<unsigned short, unsigned short>* m_nmatched_id_true = nullptr;
117 
118  //--------------------------------------------------
119  // Data from input nodes
120  //--------------------------------------------------
121  PHG4TruthInfoContainer* m_PHG4TruthInfoContainer = nullptr; // Get the truth track ids
127 
129 
130  // Output data node:
132 
133  //--------------------------------------------------
134  // RECO data for a "table" of reconstructed tracks
135  //--------------------------------------------------
136  using RECOentry = std::tuple<float, float, float, unsigned short>;
137  static constexpr int RECOphi = 0;
138  static constexpr int RECOeta = 1;
139  static constexpr int RECOpt = 2;
140  static constexpr int RECOid = 3;
141 
142  public:
143  using RECOvec = std::vector<RECOentry>;
144  using RECOiter = RECOvec::iterator;
145  using RECO_pair_iter = std::pair<RECOiter, RECOiter>;
146 
147  private:
149  {
150  bool operator()(const RECOentry& lhs, const double& rhs) { return std::get<RECOphi>(lhs) < rhs; }
151  bool operator()(const double& lhs, const RECOentry& rhs) { return lhs < std::get<RECOphi>(rhs); }
152  bool operator()(const RECOentry& lhs, const RECOentry& rhs) { return std::get<RECOphi>(lhs) < std::get<RECOphi>(rhs); }
153  };
155  {
156  bool operator()(const RECOentry& lhs, const double& rhs) { return std::get<RECOeta>(lhs) < rhs; }
157  bool operator()(const double& lhs, const RECOentry& rhs) { return lhs < std::get<RECOeta>(rhs); }
158  bool operator()(const RECOentry& lhs, const RECOentry& rhs) { return std::get<RECOeta>(lhs) < std::get<RECOeta>(rhs); }
159  };
161  {
162  bool operator()(const RECOentry& lhs, const double& rhs) { return std::get<RECOpt>(lhs) < rhs; }
163  bool operator()(const double& lhs, const RECOentry& rhs) { return lhs < std::get<RECOpt>(rhs); }
164  bool operator()(const RECOentry& lhs, const RECOentry& rhs) { return std::get<RECOpt>(lhs) < std::get<RECOpt>(rhs); }
165  };
166 
167  // sorting tuple is by: phi, eta, pT, index, is_matched
168  //--------------------------------------------------
169  // PossibleMatches (just array<unsinged short, 5>)
170  //--------------------------------------------------
171  public:
172  using PossibleMatch = std::array<unsigned short, 5>;
173 
174  private:
175  static constexpr int PM_nmatch = 0;
176  static constexpr int PM_ntrue = 1;
177  static constexpr int PM_nreco = 2;
178  static constexpr int PM_idtrue = 3;
179  static constexpr int PM_idreco = 4;
181  {
182  // Sort by most matched clusters first, then smallest number of truth clusters, then smallest number of reco clusters
184  {
185  if (lhs[PM_nmatch] != rhs[PM_nmatch]) return lhs[PM_nmatch] > rhs[PM_nmatch];
186  if (lhs[PM_ntrue] != rhs[PM_ntrue]) return lhs[PM_ntrue] < rhs[PM_ntrue];
187  if (lhs[PM_nreco] != rhs[PM_nreco]) return lhs[PM_nreco] < rhs[PM_nreco];
188  return false;
189  }
190  };
191 
192  //--------------------------------------------------
193  // PossibleMatches (just array<unsinged short, 5>)
194  //--------------------------------------------------
195  public:
196  //--------------------------------------------------
197  // non-node member data
198  //--------------------------------------------------
199  RECOvec recoData{}; // "sv" = Sort Vector
200 
201  //--------------------------------------------------
202  // Member functions
203  //--------------------------------------------------
204 
205  // Main functions
206  // -------------------------------------------------------------------
207  std::pair<std::vector<unsigned short>, std::vector<unsigned short>>
208  find_box_matches(float truth_phi, float truth_eta, float truth_pt); // will populate to truth_to_reco_map and
209  void match_tracks_in_box(std::vector<std::pair<unsigned short, unsigned short>>& indices); // pairs of {id_true, id_reco}
210 
211  // Helper functions
212  // -------------------------------------------------------------------
213  float delta_outer_pt(float) const;
214  float delta_inner_pt(float) const;
215  float abs_dphi(float phi0, float phi1);
217  bool skip_match(PossibleMatch& match);
218  bool at_nmax_index_true(unsigned short); // test if the truth track already has maximum matches matches
219  bool at_nmax_id_reco(unsigned short); // " reco "
220 
221  std::pair<bool, float> compare_cluster_pair(TrkrDefs::cluskey key_T,
222  TrkrDefs::cluskey key_R, TrkrDefs::hitsetkey key, bool calc_sigma = false);
223 
224  // ------------------------------------------------------------------
225  // output for diagnositics:
226  // If there is output, put all the clusters into the x, y, z
227  // ------------------------------------------------------------------
228  TTree* m_diag_tree = nullptr;
229  TFile* m_diag_file = nullptr;
230  bool m_write_diag = false;
231  void set_diagnostic_file(const std::string& file_name);
232 
233  std::vector<int> m_trkid_reco_matched{};
234  std::vector<int> m_cnt_reco_matched{};
235  std::vector<unsigned int> m_i0_reco_matched{};
236  std::vector<unsigned int> m_i1_reco_matched{};
237  std::vector<unsigned int> m_layer_reco_matched{};
238  std::vector<float> m_x_reco_matched{};
239  std::vector<float> m_y_reco_matched{};
240  std::vector<float> m_z_reco_matched{};
241 
242  std::vector<int> m_trkid_reco_notmatched{};
243  std::vector<int> m_cnt_reco_notmatched{};
244  std::vector<unsigned int> m_i0_reco_notmatched{};
245  std::vector<unsigned int> m_i1_reco_notmatched{};
246  std::vector<unsigned int> m_layer_reco_notmatched{};
247  std::vector<float> m_x_reco_notmatched{};
248  std::vector<float> m_y_reco_notmatched{};
249  std::vector<float> m_z_reco_notmatched{};
250 
251  std::vector<int> m_trkid_true_matched{};
252  std::vector<int> m_cnt_true_matched{};
253  std::vector<unsigned int> m_i0_true_matched{};
254  std::vector<unsigned int> m_i1_true_matched{};
255  std::vector<unsigned int> m_layer_true_matched{};
256  std::vector<float> m_x_true_matched{};
257  std::vector<float> m_y_true_matched{};
258  std::vector<float> m_z_true_matched{};
259 
260  std::vector<int> m_trkid_true_notmatched{};
261  std::vector<int> m_cnt_true_notmatched{};
262  std::vector<unsigned int> m_i0_true_notmatched{};
263  std::vector<unsigned int> m_i1_true_notmatched{};
264  std::vector<unsigned int> m_layer_true_notmatched{};
265  std::vector<float> m_x_true_notmatched{};
266  std::vector<float> m_y_true_notmatched{};
267  std::vector<float> m_z_true_notmatched{};
268 
269  int m_event = 0;
270 
271  void clear_branch_vectors();
272  void fill_tree();
273 };
274 
275 #endif