Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-05 08:17:56

0001 #ifndef G4EVAL_G4EVALTOOLS_H
0002 #define G4EVAL_G4EVALTOOLS_H
0003 
0004 #include <trackbase/TrkrDefs.h>
0005 
0006 #include <fun4all/Fun4AllReturnCodes.h>
0007 
0008 #include <Eigen/Core>
0009 
0010 #include <array>
0011 #include <cfloat>
0012 #include <set>
0013 #include <tuple>
0014 #include <utility>
0015 #include <vector>
0016 
0017 class ActsGeometry;
0018 class EmbRecoMatchContainer;
0019 class PHCompositeNode;
0020 class SvtxTrack;
0021 class SvtxTrackMap;
0022 class TrkrCluster;
0023 class TrkrClusterContainer;
0024 class TrkrTruthTrack;
0025 
0026 namespace G4Eval
0027 {
0028   // ClusLoc holds layer, location, phi size and z size
0029   using ClusLoc = std::tuple<int, Eigen::Vector3d, int, int>;
0030 
0031   // Following function writes msg to the currently active TFile
0032   // if f_outname is provided, then it will write the message to a new
0033   // TFiles of that name and close it again.
0034   void write_StringToTFile(const std::string& msg_name, const std::string& msg);
0035 
0036   std::vector<int> unmatchedSvtxTrkIds(EmbRecoMatchContainer*, SvtxTrackMap*);
0037 
0038   class TrkrClusterComparer
0039   {
0040     // most members are public for easy access after the node has been used
0041    public:
0042     TrkrClusterComparer(float _nphi_widths = 0.5, float _nz_widths = 0.5);
0043     int init(PHCompositeNode* topNode,
0044              const std::string& name_truth_clusters = "TRKR_TRUTHCLUSTERCONTAINER",
0045              const std::string& name_reco_clusters = "TRKR_CLUSTER");
0046 
0047     TrkrCluster* clus_T{nullptr};
0048     TrkrCluster* clus_R{nullptr};
0049 
0050     /* std::pair<bool, float> is_match_b */
0051     std::pair<bool, float> operator()(TrkrDefs::cluskey key_T, TrkrDefs::cluskey key_R);
0052 
0053     // Members that are set with each set of cluster keys that
0054     // are passed to it.
0055     // z and phi locations of phg4 hit (T) and Svtx hit (R)
0056     bool is_match{false};
0057     int layer{INT_MAX};
0058 
0059     float z_T{FLT_MAX}, z_R{FLT_MAX};
0060     float phi_T{FLT_MAX}, phi_R{FLT_MAX};
0061     float phisize_R{FLT_MAX}, phisize_T{FLT_MAX};  // phisize is in nbins * nwidhts
0062     float zsize_R{FLT_MAX}, zsize_T{FLT_MAX};      // zsize   is in nbins * nwdiths
0063     float phi_delta{FLT_MAX}, z_delta{FLT_MAX};    // deltas are also in nbins
0064 
0065     bool in_tpc{false};
0066     bool in_mvtx{false};
0067     bool in_intt{false};
0068     bool in_tpot{false};
0069 
0070     // z pixel sizes. n.b.: there is no z clustering in the INTT
0071     float m_zstep_tpc{0.};  // from tpc geometry
0072     float m_zstep_mvtx{0.};
0073     // TPOT not implemented yet...
0074 
0075     void set_nz_widths(float val) { m_nz_widths = val; };
0076     void set_nphi_widths(float val) { m_nphi_widths = val; };
0077 
0078     ClusLoc clusloc_PHG4(std::pair<TrkrDefs::hitsetkey, TrkrDefs::cluskey>);
0079     ClusLoc clusloc_SVTX(std::pair<TrkrDefs::hitsetkey, TrkrDefs::cluskey>);
0080 
0081     TrkrClusterContainer* m_TruthClusters{nullptr};
0082     TrkrClusterContainer* m_RecoClusters{nullptr};
0083 
0084    private:
0085     // phi pixel sizes, got for the geometries from the topNode
0086     std::array<double, 56> m_phistep{0.};  // the phistep squared
0087     float m_nphi_widths;
0088     float m_nz_widths;
0089 
0090     ActsGeometry* m_ActsGeometry{nullptr};
0091   };
0092 
0093   // The following is a struct to iterate over the cluster keys for a given
0094   // StvxTrack* tracks, starting with the silicone seed and then returning
0095   // values for the tpc seed. It is used like:
0096   //
0097   // for (auto& cluskey : ClusKeyIter(svtx_track)) {
0098   //    ... // do things with cluster keys
0099   // }
0100   struct ClusKeyIter
0101   {
0102     typedef std::set<TrkrDefs::cluskey> ClusterKeySet;
0103     typedef ClusterKeySet::iterator ClusterKeyIter;
0104 
0105     ClusKeyIter(SvtxTrack* _track);
0106     // data
0107     SvtxTrack* track;
0108     bool in_silicon;
0109     bool has_tpc;
0110     bool no_data;  // neither a tpc nor a silicon seed
0111     ClusterKeyIter iter{};
0112     ClusterKeyIter iter_end_silicon{};
0113 
0114     ClusKeyIter begin();
0115     ClusKeyIter end();
0116 
0117     void operator++();
0118     TrkrDefs::cluskey operator*();
0119     bool operator!=(const ClusKeyIter& rhs);
0120   };
0121 
0122   int trklayer_0123(TrkrDefs::hitsetkey);  // 0:Mvtx 1:Intt 2:Tpc 3:Tpot
0123 
0124   class ClusCntr
0125   {
0126    private:
0127     using Vector = std::vector<std::pair<TrkrDefs::hitsetkey, TrkrDefs::cluskey>>;
0128     using Iter = Vector::iterator;
0129 
0130     TrkrClusterComparer* comp;
0131     std::array<int, 5> cntclus(Vector& keys);
0132     std::array<int, 5> cnt_matchedclus(Vector& keys, std::vector<bool>& matches);
0133 
0134    public:
0135     ClusCntr(TrkrClusterComparer* _ = nullptr)
0136       : comp{_} {};
0137     TrkrClusterContainer* get_PHG4_clusters();
0138     TrkrClusterContainer* get_SVTX_clusters();
0139 
0140     Vector svtx_keys{};
0141     Vector phg4_keys{};
0142 
0143     double match_stat{0};
0144 
0145     void reset();
0146     std::array<int, 3> find_matches();  // populated matches_{svtx,phg4};
0147                                         // return's {n-matched, n-phg4, n-svtx}
0148     std::array<int, 3> find_matches(TrkrTruthTrack* g4_track, SvtxTrack* sv_track);
0149 
0150     int phg4_n_matched();  // also same as phg4_cnt_matchedclus()[4]
0151     int svtx_n_matched();  // should be almost always the same
0152                            // which is ALMOST guaranteed to be same as svtx_cnt_matchedclus()[4]
0153     int phg4_nclus() { return (int) phg4_keys.size(); }
0154     int svtx_nclus() { return (int) svtx_keys.size(); }
0155 
0156     std::vector<bool> svtx_matches;
0157     std::vector<bool> phg4_matches;
0158 
0159     int addClusKeys(SvtxTrack*);       // return number of clusters
0160     int addClusKeys(TrkrTruthTrack*);  // return number of clusters
0161 
0162     std::array<int, 5> svtx_cntclus() { return cntclus(svtx_keys); };  // Mvtx Intt Tpc TPOT Sum
0163     std::array<int, 5> phg4_cntclus() { return cntclus(phg4_keys); };
0164 
0165     std::array<int, 5> svtx_cnt_matchedclus() { return cnt_matchedclus(svtx_keys, svtx_matches); };
0166     std::array<int, 5> phg4_cnt_matchedclus() { return cnt_matchedclus(phg4_keys, phg4_matches); };
0167 
0168     // I need the cluster widths for diagnostics, too
0169     std::vector<ClusLoc> phg4_clusloc_all();
0170     std::vector<ClusLoc> phg4_clusloc_unmatched();
0171     std::vector<ClusLoc> svtx_clusloc_all();
0172     std::vector<ClusLoc> svtx_clusloc_unmatched();
0173     std::vector<ClusLoc> clusloc_matched();
0174 
0175     void set_comparer(TrkrClusterComparer* _comp) { comp = _comp; };
0176   };
0177 }  // namespace G4Eval
0178 
0179 #endif