From 26454517d43c9b636b458f74f33c8dd5be8c4256 Mon Sep 17 00:00:00 2001
From: "s.zharko@gsi.de" <szharko@lxg1472.gsi.de>
Date: Thu, 13 Jan 2022 14:20:48 +0100
Subject: [PATCH] tmp modifications

---
 reco/L1/CMakeLists.txt                 |   1 +
 reco/L1/L1Algo/L1CATrackFinder.cxx     |  10 ++
 reco/L1/L1Algo/L1Def.h                 |   8 +
 reco/L1/L1Algo/L1Parameters.h          | 206 +++++++++++++++++--------
 reco/L1/L1Algo/L1Vector.h              |   5 +
 reco/L1/L1Algo/utils/L1AlgoFunctions.h |  61 ++++++++
 6 files changed, 225 insertions(+), 66 deletions(-)
 create mode 100644 reco/L1/L1Algo/utils/L1AlgoFunctions.h

diff --git a/reco/L1/CMakeLists.txt b/reco/L1/CMakeLists.txt
index 7e4abe3ced..6c6b0fefc5 100644
--- a/reco/L1/CMakeLists.txt
+++ b/reco/L1/CMakeLists.txt
@@ -271,6 +271,7 @@ Install(FILES CbmL1Counters.h
               L1Algo/L1XYMeasurementInfo.h
               L1Algo/L1BaseStationInfo.h
               L1Algo/L1Parameters.h
+              L1Algo/utils/L1Functions.h
               vectors/vec_arithmetic.h
               vectors/std_alloc.h
         DESTINATION include
diff --git a/reco/L1/L1Algo/L1CATrackFinder.cxx b/reco/L1/L1Algo/L1CATrackFinder.cxx
index e3d5e11b28..c7da109c3d 100644
--- a/reco/L1/L1Algo/L1CATrackFinder.cxx
+++ b/reco/L1/L1Algo/L1CATrackFinder.cxx
@@ -1760,8 +1760,16 @@ void L1Algo::CATrackFinder()
   TStopwatch c_time1;
   c_time1.Start();
 
+  /*********
+   * Debug *
+   *********/
+
+  std::ofstream outTFDEBUG("/u/szharko/frameworks/cbm/pg/DEBUG_TFIterationsCheck.txt");
+  outTFDEBUG << "Tracking mode: " << static_cast<unsigned int>(fTrackingMode) << '\n';
+  // ---- Loop over Track Finder iterations ----------------------------------------------------------------//
   for (isec = 0; isec < fNFindIterations; ++isec)  // all finder
   {
+    outTFDEBUG << "==== New TF iteration: isec ====\n";
     if (fTrackingMode == kMcbm) {
       if (isec > 3) { continue; }
     }
@@ -2475,6 +2483,8 @@ void L1Algo::CATrackFinder()
 #endif  // COUNTERS
     }
   }  // for (int isec
+  outTFDEBUG.close();
+  // ---- Loop over Track Finder iterations: END -----------------------------------------------------------//
 
 #ifdef XXX
   c_timerG.Stop();
diff --git a/reco/L1/L1Algo/L1Def.h b/reco/L1/L1Algo/L1Def.h
index f60d2607d5..f6974adc5d 100644
--- a/reco/L1/L1Algo/L1Def.h
+++ b/reco/L1/L1Algo/L1Def.h
@@ -63,4 +63,12 @@ T finite(T x)
 
 typedef int index_type;
 
+/// Hash for unordered_map with enum class keys
+struct EnumClassHash {
+  template <typename T> size_t operator()(T t) const 
+  {
+    return static_cast<size_t>(t);
+  }
+};
+
 #endif
diff --git a/reco/L1/L1Algo/L1Parameters.h b/reco/L1/L1Algo/L1Parameters.h
index 94de70cf10..70a91a85aa 100644
--- a/reco/L1/L1Algo/L1Parameters.h
+++ b/reco/L1/L1Algo/L1Parameters.h
@@ -15,18 +15,23 @@
 #include <FairLogger.h>
 #include <string>
 #include <unordered_map>
+#include <sstream>
 #include <iomanip>
 
+#include "utils/L1AlgoFunctions.h"
 #include "L1Vector.h"
+#include "L1Def.h" // For EnumClassHash
+
+//#define L1ALGODEV
 
 //---------------------------------------------------------------------------------------------------------//
 //    Track finder iterations definition
 //---------------------------------------------------------------------------------------------------------//
 
-// TODO: May be it is reasonable to create TrackFinderIterations class 
+// TODO: Create independent class of L1CATrackFinderIterations ???
 
-/// Different iterations for Track Finder running
-enum class TrackFinderIter {
+/// All possible iteration species of Track Finder algorithm
+enum class L1CATrackFinderIter {
   kFastPrim,       ///< primary fast tracks
   kAllPrim,        ///< primary all tracks
   kAllPrimJump,    ///< primary all tracks with jumped triplets
@@ -37,20 +42,32 @@ enum class TrackFinderIter {
   kAllSecJump      ///< secondary all tracks with jumped triplets
 };
 
+// Type for mapping L1CATrackFinder parameters, which are dependent from the current track finder iteration
+template <typename T>
+using L1CATrackFinderIterParameterMap_t = std::unordered_map<L1CATrackFinderIter, T, EnumClassHash>;
+
 
-const std::unordered_map<TrackFinderIter, std::string> kTrackFinderIterNames {
-  { TrackFinderIter::kFastPrim,     "Primary tracks, fast" },
-  { TrackFinderIter::kAllPrim,      "Primary tracks, all" },
-  { TrackFinderIter::kAllPrimJump,  "Primary tracks with jumped triplets, all" },
-  { TrackFinderIter::kAllSec,       "Secondary tracks, all" },
-  { TrackFinderIter::kAllPrimE,     "Primary electron tracks, all" },
-  { TrackFinderIter::kAllSecE,      "Secondary electron tracks, all" },
-  { TrackFinderIter::kFastPrimJump, "Primary tracks with jumped triplets, fast" },
-  { TrackFinderIter::kAllSecJump,   "Secondary tracks with jumped triplets, all" }
+const L1CATrackFinderIterParameterMap_t<std::string> kTrackFinderIterNames = {
+  { L1CATrackFinderIter::kFastPrim,     "Primary tracks, fast" },
+  { L1CATrackFinderIter::kAllPrim,      "Primary tracks, all" },
+  { L1CATrackFinderIter::kAllPrimJump,  "Primary tracks with jumped triplets, all" },
+  { L1CATrackFinderIter::kAllSec,       "Secondary tracks, all" },
+  { L1CATrackFinderIter::kAllPrimE,     "Primary electron tracks, all" },
+  { L1CATrackFinderIter::kAllSecE,      "Secondary electron tracks, all" },
+  { L1CATrackFinderIter::kFastPrimJump, "Primary tracks with jumped triplets, fast" },
+  { L1CATrackFinderIter::kAllSecJump,   "Secondary tracks with jumped triplets, all" }
 };
 
-template <typename T>
-using TrackFinderIterParameter_t = std::unordered_map<TrackFinderIter, T>;
+const L1CATrackFinderIterParameterMap_t<std::string> kTrackFinderIterNamesShort = {
+  { L1CATrackFinderIter::kFastPrim,     "FastPrim" },
+  { L1CATrackFinderIter::kAllPrim,      "AllPrim" },
+  { L1CATrackFinderIter::kAllPrimJump,  "AllPrimJump" },
+  { L1CATrackFinderIter::kAllSec,       "AllSec" },
+  { L1CATrackFinderIter::kAllPrimE,     "AllPrimE" },
+  { L1CATrackFinderIter::kAllSecE,      "AllSecE" },
+  { L1CATrackFinderIter::kFastPrimJump, "FastPrimJump" },
+  { L1CATrackFinderIter::kAllSecJump,   "AllSecJump" }
+};
 
 class L1Parameters {
 public:
@@ -70,6 +87,8 @@ public:
   static constexpr unsigned int kMaxNthreads {1 << kThreadBits};  ///> Max number of threads, 2^6  = 64
   static constexpr unsigned int kMaxNtriplets {1 << kTripletBits};  ///> Max number of triplets, 2^20 = 1,048,576
 
+  static constexpr short kStandardIOWidth = 15;  ///> Width of one output entry, passed to the std::setw()
+
 public:
   //-------------------------------------------------------------------------------------------------------//
   //    Basic methods                                                                                      //
@@ -92,103 +111,158 @@ public:
     LOG(INFO) << "== L1Algo parameters ==============================================================";
     LOG(INFO) << "";
     LOG(INFO) << "  COMPILE TIME CONSTANTS";
-    LOG(INFO) << "    Bits to code one station:          " << static_cast<unsigned int>(kStationBits);
-    LOG(INFO) << "    Bits to code one thread:           " << static_cast<unsigned int>(kThreadBits);
-    LOG(INFO) << "    Bits to code one triplet:          " << static_cast<unsigned int>(kTripletBits);
-    LOG(INFO) << "    Max number of stations:            " << static_cast<unsigned int>(kMaxNstations);
-    LOG(INFO) << "    Max number of threads:             " << static_cast<unsigned int>(kMaxNthreads);
-    LOG(INFO) << "    Max number of triplets:            " << static_cast<unsigned int>(kMaxNtriplets);
+    LOG(INFO) << "    Bits to code one station:          " << kStationBits;
+    LOG(INFO) << "    Bits to code one thread:           " << kThreadBits;
+    LOG(INFO) << "    Bits to code one triplet:          " << kTripletBits;
+    LOG(INFO) << "    Max number of stations:            " << kMaxNstations;
+    LOG(INFO) << "    Max number of threads:             " << kMaxNthreads;
+    LOG(INFO) << "    Max number of triplets:            " << kMaxNtriplets;
     LOG(INFO) << "";
     LOG(INFO) << "  RUNTIME CONSTANTS (CUTS)";
     LOG(INFO) << "    Max number of doublets per singlet: " << fMaxDoubletsPerSinglet;
     LOG(INFO) << "    Max number of triplets per doublet: " << fMaxTripletPerDoublets;
+    LOG(INFO) << "";
+    LOG(INFO) << "  TRACK FINDER ITERATION DEPENDENT CONSTANTS";
+    
+
     LOG(INFO) << "===================================================================================";
   }
+
   // TODO: change constant names with actual (human) names
-  // TODO: create a map with parameters, their description etc.
 
 public:
   //-------------------------------------------------------------------------------------------------------//
   //    Runtime constants                                                                                  //
   //-------------------------------------------------------------------------------------------------------//
+  
+  // ***** Setters ***** //
   /// Sets upper-bound cut on max number of doublets per one singlet
   void SetMaxDoubletsPerSinglet(unsigned int value) { fMaxDoubletsPerSinglet = value; }
   /// Sets upper-bound cut on max number of triplets per one doublet
   void SetMaxTripletPerDoublets(unsigned int value) { fMaxTripletPerDoublets = value; }
 
   /// Sets track finder iteration sequence
-  void SetTrackFinderIterSequence(const L1Vector<TrackFinderIter>& iterations) {
+  void SetTrackFinderIterSequence(const L1Vector<L1CATrackFinderIter>& iterations) {
     fTrackFinderIterSequence = iterations;
   }
   
-
+  /// Sets track Chi2 upper cut
+  /// \param tfIteration Track Finder iteration of the L1 algorithm run
+  /// \param chi2Cut     Upper cut on track chi2 during selected iteration 
+  void SetTrackChi2Cut(L1CATrackFinderIter tfIteration, float chi2Cut) { fTrackChi2Cut[tfIteration] = chi2Cut; }
+  void SetTrackChi2Cut(float chi2Cut) { SetSameValueToMap(chi2Cut, fTrackChi2Cut); }
+  void SetTrackChi2Cut(const L1CATrackFinderIterParameterMap_t<float>& newValues) 
+  { 
+    SetMappedValuesToMap(newValues, fTrackChi2Cut); 
+  }
+  /// Sets triplet Chi2 upper cut
+  /// \param tfIteration Track Finder iteration of the L1 algorithm run
+  /// \param chi2Cut     Upper cut on triplet chi2 during selected iteration 
+  void SetTripletChi2Cut(L1CATrackFinderIter tfIteration, float chi2Cut) { fTripletChi2Cut[tfIteration] = chi2Cut; }
+  void SetTripletChi2Cut(float chi2Cut) { SetSameValueToMap(chi2Cut, fTripletChi2Cut); }
+  void SetTripletChi2Cut(const L1CATrackFinderIterParameterMap_t<float>& newValues) 
+  {
+    SetMappedValuesToMap(newValues, fTripletChi2Cut);
+  }
+  /// Sets triplet Chi2 upper cut 
+  /// \param tfIteration Track Finder iteration of the L1 algorithm run
+  /// \param chi2Cut     Upper cut on triplet chi2 during selected iteration 
+  void SetDoubletChi2Cut(L1CATrackFinderIter tfIteration, float chi2Cut) { fDoubletChi2Cut[tfIteration] = chi2Cut; }
+  void SetDoubletChi2Cut(float chi2Cut) { SetSameValueToMap(chi2Cut, fDoubletChi2Cut); }
+  void SetDoubletChi2Cut(const L1CATrackFinderIterParameterMap_t<float>& newValues) 
+  { 
+    SetMappedValuesToMap(newValues, fDoubletChi2Cut);
+  }
+  
+  // ***** Getters ***** //
   unsigned int GetMaxDoubletsPerSinglet() const { return fMaxDoubletsPerSinglet; }
   unsigned int GetMaxTripletPerDoublets() const { return fMaxTripletPerDoublets; }
-
+  
+  /// Gets track Chi2 upper cut
+  /// \param tfIteration Track Finder iteration of the L1 algorithm run
+  /// \return            Upper cut on track chi2 during selected iteration 
+  float GetTrackChi2Cut(L1CATrackFinderIter tfIteration)   const { return fTrackChi2Cut.at(tfIteration); }
+  /// Gets triplet Chi2 upper cut
+  /// \param tfIteration Track Finder iteration of the L1 algorithm run
+  /// \return            Upper cut on triplet chi2 during selected iteration 
+  float GetTripletChi2Cut(L1CATrackFinderIter tfIteration) const { return fTripletChi2Cut.at(tfIteration); }
+  /// Gets triplet Chi2 upper cut
+  /// \param tfIteration Track Finder iteration of the L1 algorithm run
+  /// \return            Upper cut on triplet chi2 during selected iteration 
+  float GetDoubletChi2Cut(L1CATrackFinderIter tfIteration) const { return fDoubletChi2Cut.at(tfIteration); }
+  
+  
   //-------------------------------------------------------------------------------------------------------//
   //    Auxilary methods                                                                                   //
   //-------------------------------------------------------------------------------------------------------//
-  
-  void PrintTrackFinderIterationsSequence() const
+public:
+  void PrintTrackFinderIterSequence() const
   {  
-    LOG(INFO) << "== L1Algo track finder iterations =================================================";
+    LOG(INFO) << "== L1Algo track finder iterations sequence ==========";
     LOG(INFO) << "";
     int idx = 0;
     for (auto& iteration: fTrackFinderIterSequence) {
-      LOG(INFO) << "  " << std::setw(2) << std::setfill(' ') << idx << ' ' << kTrackFinderIterNames.at(iteration);
+      LOG(INFO) << "  " << std::setw(2) << std::setfill(' ') << idx << ") " << kTrackFinderIterNames.at(iteration);
       ++idx;
     }
     LOG(INFO) << "";
-    LOG(INFO) << "===================================================================================";
-
+    LOG(INFO) << "=====================================================";
   }
 
+private:
+  /// Aux method for printing mapped Track Finder parameters
+  ///template <typename T> // NOTE: moved to L1AlgoFunctions.h
+  ///void PrintTrackFinderIterParameters(const std::string& parName, const L1CATrackFinderIterParameterMap_t<T>& parMap)
+  ///{
+  ///  std::stringstream token; // Is used because LOG() automatically sets '\n' to the end of the string
+  ///  token << "\t\t";
+  ///  token << std::setw(35) << std::setfill(' ') << parName << ' ';
+  ///  for ()
+  ///}
+
 private:
   unsigned int fMaxDoubletsPerSinglet {150};  ///<
   unsigned int fMaxTripletPerDoublets {15};  ///<
 
-  L1Vector<TrackFinderIter> fTrackFinderIterSequence {}; ///< Sequence of iterations in the track finder algorithm 
+  L1Vector<L1CATrackFinderIter> fTrackFinderIterSequence {}; ///< Sequence of iterations in the track finder algorithm 
 
   // TODO: Develope naming system for these constants
-
-  std::unordered_map<TrackFinderIter, float> fTrackChi2Cut {
-    { TrackFinderIter::kFastPrim,     10.f },
-    { TrackFinderIter::kAllPrim,      10.f },
-    { TrackFinderIter::kAllPrimJump,  10.f },
-    { TrackFinderIter::kAllSec,       10.f },
-    { TrackFinderIter::kAllPrimE,     10.f },
-    { TrackFinderIter::kAllSecE,      10.f },
-    { TrackFinderIter::kFastPrimJump, 10.f },
-    { TrackFinderIter::kAllSecJump,   10.f }
+  L1CATrackFinderIterParameterMap_t<float> fTrackChi2Cut {
+    { L1CATrackFinderIter::kFastPrim,     10.f },
+    { L1CATrackFinderIter::kAllPrim,      10.f },
+    { L1CATrackFinderIter::kAllPrimJump,  10.f },
+    { L1CATrackFinderIter::kAllSec,       10.f },
+    { L1CATrackFinderIter::kAllPrimE,     10.f },
+    { L1CATrackFinderIter::kAllSecE,      10.f },
+    { L1CATrackFinderIter::kFastPrimJump, 10.f },
+    { L1CATrackFinderIter::kAllSecJump,   10.f }
   };
-
-  std::unordered_map<TrackFinderIter, float> fTripletChi2Cut {
-    { TrackFinderIter::kFastPrim,     5.f },
-    { TrackFinderIter::kAllPrim,      5.f },
-    { TrackFinderIter::kAllPrimJump,  5.f },
-    { TrackFinderIter::kAllSec,       5.f },
-    { TrackFinderIter::kAllPrimE,     5.f },
-    { TrackFinderIter::kAllSecE,      5.f },
-    { TrackFinderIter::kFastPrimJump, 5.f },
-    { TrackFinderIter::kAllSecJump,   5.f }
+  // TODO: What is the meaning of the following numbers?
+  //   23.4450 <- 7.815 * 3
+  //   18.7560 <- 6.252 * 3
+  //   11.3449 * 2. / 3.
+  //   
+  L1CATrackFinderIterParameterMap_t<float> fTripletChi2Cut {
+    { L1CATrackFinderIter::kFastPrim,     23.4450f }, // kFastPrimIter
+    { L1CATrackFinderIter::kAllPrim,      23.4450f },
+    { L1CATrackFinderIter::kAllPrimE,     23.4450f },
+    { L1CATrackFinderIter::kAllPrimJump,  18.7560f },
+    { L1CATrackFinderIter::kAllSec,       18.7560f },
+    { L1CATrackFinderIter::kAllSecE,      18.7560f },
+    { L1CATrackFinderIter::kFastPrimJump, 21.1075f },
+    { L1CATrackFinderIter::kAllSecJump,   21.1075f }
   };
 
-  std::unordered_map<TrackFinderIter, float> fDoubletChi2Cut {
-    { TrackFinderIter::kFastPrim,     5.f },
-    { TrackFinderIter::kAllPrim,      5.f },
-    { TrackFinderIter::kAllPrimJump,  5.f },
-    { TrackFinderIter::kAllSec,       5.f },
-    { TrackFinderIter::kAllPrimE,     5.f },
-    { TrackFinderIter::kAllSecE,      5.f },
-    { TrackFinderIter::kFastPrimJump, 5.f },
-    { TrackFinderIter::kAllSecJump,   5.f }
+  L1CATrackFinderIterParameterMap_t<float> fDoubletChi2Cut {
+    { L1CATrackFinderIter::kFastPrim,     11.3449 * 2.f / 3.f },
+    { L1CATrackFinderIter::kAllPrim,      11.3449 * 2.f / 3.f },
+    { L1CATrackFinderIter::kAllPrimJump,  11.3449 * 2.f / 3.f },
+    { L1CATrackFinderIter::kAllSec,       11.3449 * 2.f / 3.f },
+    { L1CATrackFinderIter::kAllPrimE,     11.3449 * 2.f / 3.f },
+    { L1CATrackFinderIter::kAllSecE,      11.3449 * 2.f / 3.f },
+    { L1CATrackFinderIter::kFastPrimJump, 11.3449 * 2.f / 3.f },
+    { L1CATrackFinderIter::kAllSecJump,   11.3449 * 2.f / 3.f }
   };
-
-  
-
-  //float fTripletChi2Cut {5.f};
-  //float fTripletChi2Cut {5.f};
-
 };
 
 #endif  // L1Parameters_h
diff --git a/reco/L1/L1Algo/L1Vector.h b/reco/L1/L1Algo/L1Vector.h
index 5e2b3db0a6..81b88d25c1 100644
--- a/reco/L1/L1Algo/L1Vector.h
+++ b/reco/L1/L1Algo/L1Vector.h
@@ -26,6 +26,9 @@
 /// 5. blocks usage of boolean vectors, as they have a special
 ///    space-optimized but slow implementation in std. (Use L1Vector<char> instead).
 ///
+
+
+
 template<class T>
 class L1Vector : private std::vector<T> {
 public:
@@ -42,6 +45,8 @@ public:
   {
   }
 
+
+
   L1Vector(const L1Vector& v) : Tbase() { *this = v; }
 
   L1Vector& operator=(const L1Vector& v)
diff --git a/reco/L1/L1Algo/utils/L1AlgoFunctions.h b/reco/L1/L1Algo/utils/L1AlgoFunctions.h
new file mode 100644
index 0000000000..54a15b6a5b
--- /dev/null
+++ b/reco/L1/L1Algo/utils/L1AlgoFunctions.h
@@ -0,0 +1,61 @@
+/* Copyright (C) 2016-2022 GSI Helmholtzzentrum fuer Schwerionenforschung, Darmstadt
+   SPDX-License-Identifier: GPL-3.0-only
+   Authors: Sergey Gorbunov, Sergei Zharko [committer] */
+
+/************************************************************************************************************
+ * @file  L1AlgoFunctions.h
+ * @brief File contains some general purpose functions
+ * @since 12.01.2022
+ ***********************************************************************************************************/
+
+#include <unordered_map>
+#include <map>
+#include <iomanip>
+#include <sstream>
+#include <string>
+
+/// Template function, which sets a value to an element of the map with a particular key 
+/// \param key    Key of the element to be modified
+/// \param value  New value of the element under the selected key
+/// \param aMap   A reference to the map, which element is to be modified
+template <class Key, class T, class Hash = std::hash<Key> >
+void SetSingleValueToMap(Key key, T value, std::unordered_map<Key, T, Hash>& aMap)
+{
+  aMap[key] = value;
+}
+
+/// Template function, which sets a value to ALL elements of the map
+/// \param value  New value of the element under the selected key
+/// \param aMap   A reference to the map, which element is to be modified
+template <class Key, class T, class Hash = std::hash<Key> >
+void SetSameValueToMap(T value, std::unordered_map<Key, T, Hash>& aMap)
+{
+  for (auto it = aMap.begin(); it != aMap.end(); ++it) {
+    it->second = value;
+  }
+}
+
+/// Template function, which resets the elements of one map with the values defined in another map
+/// \param inMap  A constant reference to the map containing new parameters
+/// \param aMap   A reference to the map, which is going to be modified
+template <class Key, class T, class Hash = std::hash<Key> >
+void SetMappedValuesToMap(const std::unordered_map<Key, T, Hash>& inMap, std::unordered_map<Key, T, Hash>& aMap )
+{
+  for (auto it = aMap.begin(); it != aMap.end(); ++it) {
+    if (inMap.find(it->first) != inMap.end()) {
+      it->second = inMap.at(it->first);
+    }
+  }
+}
+
+/// Template function to represent mapped contents into std::string
+/// NOTE: operator<< must be defined for value of the map
+template <class Key, class T, class Hash = std::hash<Key> >
+std::string RepresentMapWithString(const std::unordered_map<Key, T, Hash>& aMap, int entryWidth = 15)
+{
+  std::map
+  std::stringstream token;
+  for (auto it = aMap.begin(); it != aMap.end(); ++it) {
+    token >> std::setw(entryWidth) >> std::setfill(' ') >> it->second >>  
+  }
+}
-- 
GitLab