From a3bbc0e3b5e17a05f4497c43c86c900491630915 Mon Sep 17 00:00:00 2001
From: Tobias Boeckh <tobias.boeckh@cern.ch>
Date: Fri, 11 Mar 2022 14:36:25 +0100
Subject: [PATCH] added SummaryPlotTool to PerformanceWriterTool to plot track
 hit info

---
 .../Acts/FaserActsKalmanFilter/CMakeLists.txt |  2 +
 .../PerformanceWriterTool.h                   |  6 +-
 .../FaserActsKalmanFilter/PlotHelpers.h       | 17 ++++
 .../FaserActsKalmanFilter/SummaryPlotTool.h   | 67 +++++++++++++++
 .../src/PerformanceWriterTool.cxx             | 23 +++--
 .../FaserActsKalmanFilter/src/PlotHelpers.cxx | 15 ++++
 .../src/SummaryPlotTool.cxx                   | 86 +++++++++++++++++++
 7 files changed, 202 insertions(+), 14 deletions(-)
 create mode 100644 Tracking/Acts/FaserActsKalmanFilter/FaserActsKalmanFilter/SummaryPlotTool.h
 create mode 100644 Tracking/Acts/FaserActsKalmanFilter/src/SummaryPlotTool.cxx

diff --git a/Tracking/Acts/FaserActsKalmanFilter/CMakeLists.txt b/Tracking/Acts/FaserActsKalmanFilter/CMakeLists.txt
index c06f98154..bb8d6bcb9 100755
--- a/Tracking/Acts/FaserActsKalmanFilter/CMakeLists.txt
+++ b/Tracking/Acts/FaserActsKalmanFilter/CMakeLists.txt
@@ -45,6 +45,7 @@ atlas_add_component(FaserActsKalmanFilter
     FaserActsKalmanFilter/SimWriterTool.h
     FaserActsKalmanFilter/SPSeedBasedInitialParameterTool.h
     FaserActsKalmanFilter/SPSimpleInitialParameterTool.h
+    FaserActsKalmanFilter/SummaryPlotTool.h
     FaserActsKalmanFilter/TrackClassification.h
     FaserActsKalmanFilter/TrackSelection.h
     FaserActsKalmanFilter/TrajectoryWriterTool.h
@@ -71,6 +72,7 @@ atlas_add_component(FaserActsKalmanFilter
     src/TrackFittingFunction.cxx
     src/TrajectoryWriterTool.cxx
     src/TruthBasedInitialParameterTool.cxx
+    src/SummaryPlotTool.cxx
     src/TrackClassification.cxx
     src/TrackSelection.cxx
 #    src/TruthTrackFinderTool.cxx
diff --git a/Tracking/Acts/FaserActsKalmanFilter/FaserActsKalmanFilter/PerformanceWriterTool.h b/Tracking/Acts/FaserActsKalmanFilter/FaserActsKalmanFilter/PerformanceWriterTool.h
index b06b40df1..235604a9f 100644
--- a/Tracking/Acts/FaserActsKalmanFilter/FaserActsKalmanFilter/PerformanceWriterTool.h
+++ b/Tracking/Acts/FaserActsKalmanFilter/FaserActsKalmanFilter/PerformanceWriterTool.h
@@ -4,6 +4,7 @@
 #include "TrackerPrepRawData/FaserSCT_Cluster.h"
 #include "AthenaBaseComps/AthAlgTool.h"
 #include "FaserActsKalmanFilter/ResPlotTool.h"
+#include "FaserActsKalmanFilter/SummaryPlotTool.h"
 #include "TrackerSimData/TrackerSimDataCollection.h"
 #include "GeneratorObjects/McEventCollection.h"
 #include "Acts/Geometry/GeometryContext.hpp"
@@ -14,7 +15,7 @@ using TrajectoriesContainer = std::vector<FaserActsRecMultiTrajectory>;
 class PerformanceWriterTool : public AthAlgTool {
 public:
   PerformanceWriterTool(const std::string& type, const std::string& name, const IInterface* parent);
-  ~PerformanceWriterTool() override;
+  ~PerformanceWriterTool() override =  default;
 
   StatusCode initialize() override;
   StatusCode finalize() override;
@@ -32,6 +33,9 @@ private:
   /// Plot tool for residuals and pulls.
   ResPlotTool m_resPlotTool;
   ResPlotTool::ResPlotCache m_resPlotCache;
+  /// Plot tool for track hit info
+  SummaryPlotTool m_SummaryPlotTool;
+  SummaryPlotTool::SummaryPlotCache m_SummaryPlotCache;
 };
 
 #endif  // FASERACTSKALMANFILTER_PERFORMANCEWRITER_H
diff --git a/Tracking/Acts/FaserActsKalmanFilter/FaserActsKalmanFilter/PlotHelpers.h b/Tracking/Acts/FaserActsKalmanFilter/FaserActsKalmanFilter/PlotHelpers.h
index f4b6b3de9..79141910b 100644
--- a/Tracking/Acts/FaserActsKalmanFilter/FaserActsKalmanFilter/PlotHelpers.h
+++ b/Tracking/Acts/FaserActsKalmanFilter/FaserActsKalmanFilter/PlotHelpers.h
@@ -5,6 +5,7 @@
 #include "TFitResultPtr.h"
 #include "TH1F.h"
 #include "TH2F.h"
+#include "TProfile.h"
 #include <string>
 
 namespace PlotHelpers {
@@ -61,6 +62,22 @@ void fillHisto(TH2F* hist, float xValue, float yValue, float weight = 1.0);
 ///
 void anaHisto(TH1D* inputHist, int j, TH1F* meanHist, TH1F* widthHist);
 
+/// @brief book a TProfile plot
+/// @param profName the name of plot
+/// @param profTitle the title of plot
+/// @param varXBinning the binning info of variable at x axis
+/// @param varYBinning the binning info of variable at y axis
+/// @return TProfile pointer
+TProfile* bookProf(const char* profName, const char* profTitle,
+                   const Binning& varXBinning, const Binning& varYBinning);
+
+/// @brief fill a TProfile plot
+/// @param profile plot to fill
+/// @param xValue  xvalue to fill
+/// @param yValue  yvalue to fill
+/// @param weight weight to fill
+void fillProf(TProfile* profile, float xValue, float yValue, float weight = 1.0);
+
 }  // namespace PlotHelpers
 
 #endif  // FASERACTSKALMANFILTER_PLOTHELPERS_H
diff --git a/Tracking/Acts/FaserActsKalmanFilter/FaserActsKalmanFilter/SummaryPlotTool.h b/Tracking/Acts/FaserActsKalmanFilter/FaserActsKalmanFilter/SummaryPlotTool.h
new file mode 100644
index 000000000..14b9fedfd
--- /dev/null
+++ b/Tracking/Acts/FaserActsKalmanFilter/FaserActsKalmanFilter/SummaryPlotTool.h
@@ -0,0 +1,67 @@
+#ifndef FASERACTSKALMANFILTER_SUMMARYPLOTTOOL_H
+#define FASERACTSKALMANFILTER_SUMMARYPLOTTOOL_H
+
+#include "FaserActsKalmanFilter/PlotHelpers.h"
+#include "Acts/EventData/TrackParameters.hpp"
+#include "TProfile.h"
+#include <map>
+#include <string>
+
+class SummaryPlotTool {
+public:
+  std::map<std::string, PlotHelpers::Binning> m_varBinning {
+      {"Eta", PlotHelpers::Binning("#eta", 40, 5, 10)},
+      {"Phi", PlotHelpers::Binning("#phi", 100, -3.15, 3.15)},
+      {"Pt", PlotHelpers::Binning("pT [GeV/c]", 40, 0, 40)},
+      {"Num", PlotHelpers::Binning("N", 30, -0.5, 29.5)}
+  };
+
+  /// @brief Nested Cache struct
+  struct SummaryPlotCache {
+    TProfile* nStates_vs_eta;        ///< Number of total states vs eta
+    TProfile* nMeasurements_vs_eta;  ///< Number of non-outlier measurements vs eta
+    TProfile* nHoles_vs_eta;         ///< Number of holes vs eta
+    TProfile* nOutliers_vs_eta;      ///< Number of outliers vs eta
+    TProfile* nSharedHits_vs_eta;    ///< Number of Shared Hits vs eta
+    TProfile* nStates_vs_pt;         ///< Number of total states vs pt
+    TProfile* nMeasurements_vs_pt;   ///< Number of non-outlier measurements vs pt
+    TProfile* nHoles_vs_pt;          ///< Number of holes vs pt
+    TProfile* nOutliers_vs_pt;       ///< Number of outliers vs pt
+    TProfile* nSharedHits_vs_pt;     ///< Number of Shared Hits vs pt
+  };
+
+  /// Constructor
+  ///
+  SummaryPlotTool() = default;
+
+  /// @brief book the track info plots
+  ///
+  /// @param trackSummaryPlotCache the cache for track info plots
+  void book(SummaryPlotCache& trackSummaryPlotCache) const;
+
+  /// @brief fill reco track info w.r.t. fitted track parameters
+  ///
+  /// @param trackSummaryPlotCache cache object for track info plots
+  /// @param fittedParameters fitted track parameters of this track
+  /// @param nStates number of track states
+  /// @param nMeasurements number of measurements
+  /// @param nOutliers number of outliers
+  /// @param nHoles number of holes
+  /// @param nSharedHits number of shared hits
+  void fill(SummaryPlotCache& trackSummaryPlotCache,
+            const Acts::BoundTrackParameters& fittedParameters, size_t nStates,
+            size_t nMeasurements, size_t nOutliers, size_t nHoles,
+            size_t nSharedHits) const;
+
+  /// @brief write the track info plots to file
+  ///
+  /// @param trackSummaryPlotCache cache object for track info plots
+  void write(const SummaryPlotCache& trackSummaryPlotCache) const;
+
+  /// @brief delete the track info plots
+  ///
+  /// @param trackSummaryPlotCache cache object for track info plots
+  void clear(SummaryPlotCache& trackSummaryPlotCache) const;
+};
+
+#endif // FASERACTSKALMANFILTER_SUMMARYPLOTTOOL_H
diff --git a/Tracking/Acts/FaserActsKalmanFilter/src/PerformanceWriterTool.cxx b/Tracking/Acts/FaserActsKalmanFilter/src/PerformanceWriterTool.cxx
index 0130a423c..6668cba41 100644
--- a/Tracking/Acts/FaserActsKalmanFilter/src/PerformanceWriterTool.cxx
+++ b/Tracking/Acts/FaserActsKalmanFilter/src/PerformanceWriterTool.cxx
@@ -10,14 +10,6 @@ PerformanceWriterTool::PerformanceWriterTool(
     : AthAlgTool(type, name, parent) {}
 
 
-PerformanceWriterTool::~PerformanceWriterTool() {
-  m_resPlotTool.clear(m_resPlotCache);
-  if (m_outputFile) {
-    m_outputFile->Close();
-  }
-}
-
-
 StatusCode PerformanceWriterTool::initialize() {
   ATH_CHECK(m_mcEventCollectionKey.initialize());
   ATH_CHECK(m_simDataCollectionKey.initialize());
@@ -31,6 +23,7 @@ StatusCode PerformanceWriterTool::initialize() {
 
   // initialize the residual and efficiency plots tool
   m_resPlotTool.book(m_resPlotCache);
+  m_SummaryPlotTool.book(m_SummaryPlotCache);
   return StatusCode::SUCCESS;
 }
 
@@ -42,10 +35,16 @@ StatusCode PerformanceWriterTool::finalize() {
     m_outputFile->cd();
     m_resPlotTool.write(m_resPlotCache);
     // m_effPlotTool.write(m_effPlotCache);
-    // m_trackSummaryPlotTool.write(m_trackSummaryPlotCache);
+     m_SummaryPlotTool.write(m_SummaryPlotCache);
     ATH_MSG_VERBOSE("Wrote performance plots to '" << m_outputFile->GetPath() << "'");
   }
 
+  m_resPlotTool.clear(m_resPlotCache);
+  m_SummaryPlotTool.clear(m_SummaryPlotCache);
+  if (m_outputFile) {
+    m_outputFile->Close();
+  }
+
   return StatusCode::SUCCESS;
 }
 
@@ -129,10 +128,8 @@ StatusCode PerformanceWriterTool::write(const Acts::GeometryContext& geoContext,
     // Collect the trajectory summary info
     auto trajState = Acts::MultiTrajectoryHelpers::trajectoryState(mj, trackTip);
     // Fill the trajectory summary info
-    // m_trackSummaryPlotTool.fill(m_trackSummaryPlotCache, fittedParameters,
-    //                             trajState.nStates, trajState.nMeasurements,
-    //                             trajState.nOutliers, trajState.nHoles,
-    //                             trajState.nSharedHits);
+    m_SummaryPlotTool.fill(m_SummaryPlotCache, fittedParameters, trajState.nStates, trajState.nMeasurements,
+                           trajState.nOutliers, trajState.nHoles, trajState.nSharedHits);
   }
 
   // Fill the efficiency, defined as the ratio between number of tracks with
diff --git a/Tracking/Acts/FaserActsKalmanFilter/src/PlotHelpers.cxx b/Tracking/Acts/FaserActsKalmanFilter/src/PlotHelpers.cxx
index 907749e1f..eedce91e2 100644
--- a/Tracking/Acts/FaserActsKalmanFilter/src/PlotHelpers.cxx
+++ b/Tracking/Acts/FaserActsKalmanFilter/src/PlotHelpers.cxx
@@ -50,4 +50,19 @@ void anaHisto(TH1D* inputHist, int j, TH1F* meanHist, TH1F* widthHist) {
   }
 }
 
+TProfile* bookProf(const char* profName, const char* profTitle,
+                   const Binning& varXBinning, const Binning& varYBinning) {
+  TProfile* prof =
+      new TProfile(profName, profTitle, varXBinning.nBins, varXBinning.min,
+                   varXBinning.max, varYBinning.min, varYBinning.max);
+  prof->GetXaxis()->SetTitle(varXBinning.title.c_str());
+  prof->GetYaxis()->SetTitle(varYBinning.title.c_str());
+  return prof;
+}
+
+void fillProf(TProfile* profile, float xValue, float yValue, float weight) {
+  assert(profile != nullptr);
+  profile->Fill(xValue, yValue, weight);
+}
+
 }  // namespace PlotHelpers
diff --git a/Tracking/Acts/FaserActsKalmanFilter/src/SummaryPlotTool.cxx b/Tracking/Acts/FaserActsKalmanFilter/src/SummaryPlotTool.cxx
new file mode 100644
index 000000000..f216042a6
--- /dev/null
+++ b/Tracking/Acts/FaserActsKalmanFilter/src/SummaryPlotTool.cxx
@@ -0,0 +1,86 @@
+#include "FaserActsKalmanFilter/SummaryPlotTool.h"
+#include <iostream>
+
+void SummaryPlotTool::book(SummaryPlotTool::SummaryPlotCache &trackSummaryPlotCache) const {
+  PlotHelpers::Binning bEta = m_varBinning.at("Eta");
+  PlotHelpers::Binning bPt = m_varBinning.at("Pt");
+  PlotHelpers::Binning bNum = m_varBinning.at("Num");
+  // number of track states versus eta
+  trackSummaryPlotCache.nStates_vs_eta = PlotHelpers::bookProf(
+      "nStates_vs_eta", "Number of total states vs. #eta", bEta, bNum);
+  // number of measurements versus eta
+  trackSummaryPlotCache.nMeasurements_vs_eta = PlotHelpers::bookProf(
+      "nMeasurements_vs_eta", "Number of measurements vs. #eta", bEta, bNum);
+  // number of holes versus eta
+  trackSummaryPlotCache.nHoles_vs_eta = PlotHelpers::bookProf(
+      "nHoles_vs_eta", "Number of holes vs. #eta", bEta, bNum);
+  // number of outliers versus eta
+  trackSummaryPlotCache.nOutliers_vs_eta = PlotHelpers::bookProf(
+      "nOutliers_vs_eta", "Number of outliers vs. #eta", bEta, bNum);
+  // number of Shared Hits versus eta
+  trackSummaryPlotCache.nSharedHits_vs_eta = PlotHelpers::bookProf(
+      "nSharedHits_vs_eta", "Number of Shared Hits vs. #eta", bEta, bNum);
+  // number of track states versus pt
+  trackSummaryPlotCache.nStates_vs_pt = PlotHelpers::bookProf(
+      "nStates_vs_pT", "Number of total states vs. pT", bPt, bNum);
+  // number of measurements versus pt
+  trackSummaryPlotCache.nMeasurements_vs_pt = PlotHelpers::bookProf(
+      "nMeasurements_vs_pT", "Number of measurements vs. pT", bPt, bNum);
+  // number of holes versus pt
+  trackSummaryPlotCache.nHoles_vs_pt = PlotHelpers::bookProf(
+      "nHoles_vs_pT", "Number of holes vs. pT", bPt, bNum);
+  // number of outliers versus pt
+  trackSummaryPlotCache.nOutliers_vs_pt = PlotHelpers::bookProf(
+      "nOutliers_vs_pT", "Number of outliers vs. pT", bPt, bNum);
+  // number of Shared Hits versus pt
+  trackSummaryPlotCache.nSharedHits_vs_pt = PlotHelpers::bookProf(
+      "nSharedHits_vs_pT", "Number of Shared Hits vs. pT", bPt, bNum);
+}
+
+void SummaryPlotTool::fill(SummaryPlotTool::SummaryPlotCache &trackSummaryPlotCache,
+                           const Acts::BoundTrackParameters &fittedParameters, size_t nStates, size_t nMeasurements,
+                           size_t nOutliers, size_t nHoles, size_t nSharedHits) const {
+  using Acts::VectorHelpers::eta;
+  using Acts::VectorHelpers::perp;
+  const auto& momentum = fittedParameters.momentum();
+  const double fit_eta = eta(momentum);
+  const double fit_pT = perp(momentum);
+
+  PlotHelpers::fillProf(trackSummaryPlotCache.nStates_vs_eta, fit_eta, nStates);
+  PlotHelpers::fillProf(trackSummaryPlotCache.nMeasurements_vs_eta, fit_eta, nMeasurements);
+  PlotHelpers::fillProf(trackSummaryPlotCache.nOutliers_vs_eta, fit_eta, nOutliers);
+  PlotHelpers::fillProf(trackSummaryPlotCache.nHoles_vs_eta, fit_eta, nHoles);
+  PlotHelpers::fillProf(trackSummaryPlotCache.nSharedHits_vs_eta, fit_eta, nSharedHits);
+
+  PlotHelpers::fillProf(trackSummaryPlotCache.nStates_vs_pt, fit_pT, nStates);
+  PlotHelpers::fillProf(trackSummaryPlotCache.nMeasurements_vs_pt, fit_pT, nMeasurements);
+  PlotHelpers::fillProf(trackSummaryPlotCache.nOutliers_vs_pt, fit_pT, nOutliers);
+  PlotHelpers::fillProf(trackSummaryPlotCache.nHoles_vs_pt, fit_pT, nHoles);
+  PlotHelpers::fillProf(trackSummaryPlotCache.nSharedHits_vs_pt, fit_pT, nSharedHits);
+}
+
+void SummaryPlotTool::write(const SummaryPlotTool::SummaryPlotCache &trackSummaryPlotCache) const {
+  trackSummaryPlotCache.nStates_vs_eta->Write();
+  trackSummaryPlotCache.nMeasurements_vs_eta->Write();
+  trackSummaryPlotCache.nOutliers_vs_eta->Write();
+  trackSummaryPlotCache.nHoles_vs_eta->Write();
+  trackSummaryPlotCache.nSharedHits_vs_eta->Write();
+  trackSummaryPlotCache.nStates_vs_pt->Write();
+  trackSummaryPlotCache.nMeasurements_vs_pt->Write();
+  trackSummaryPlotCache.nOutliers_vs_pt->Write();
+  trackSummaryPlotCache.nHoles_vs_pt->Write();
+  trackSummaryPlotCache.nSharedHits_vs_pt->Write();
+}
+
+void SummaryPlotTool::clear(SummaryPlotTool::SummaryPlotCache &trackSummaryPlotCache) const {
+  delete trackSummaryPlotCache.nStates_vs_eta;
+  delete trackSummaryPlotCache.nMeasurements_vs_eta;
+  delete trackSummaryPlotCache.nOutliers_vs_eta;
+  delete trackSummaryPlotCache.nHoles_vs_eta;
+  delete trackSummaryPlotCache.nSharedHits_vs_eta;
+  delete trackSummaryPlotCache.nStates_vs_pt;
+  delete trackSummaryPlotCache.nMeasurements_vs_pt;
+  delete trackSummaryPlotCache.nOutliers_vs_pt;
+  delete trackSummaryPlotCache.nHoles_vs_pt;
+  delete trackSummaryPlotCache.nSharedHits_vs_pt;
+}
-- 
GitLab