/* Copyright (C) 2023 GSI Helmholtzzentrum fuer Schwerionenforschung, Darmstadt
   SPDX-License-Identifier: GPL-3.0-only
   Authors: Sergei Zharko [committer] */

/// \file   TrackingChain.h
/// \date   13.09.2023
/// \brief  A chain class to execute CA tracking algorithm in online reconstruction (header)
/// \author S.Zharko <s.zharko@gsi.de>

#pragma once

#include "TrackingDefs.h"
#include "tof/Hit.h"

#include <vector>

#include "CaDataManager.h"
#include "CaFramework.h"
#include "CaTrack.h"
#include "CaTrackingMonitor.h"
#include "CaVector.h"
#include "PartitionedSpan.h"
#include "RecoResults.h"
#include "SubChain.h"
#include "sts/Hit.h"

namespace cbm::algo
{
  /// \class cbm::algo::TrackingChain
  /// \brief A chain for tracking algorithm
  ///
  /// The class executes a tracking algorithm in the online data reconstruction chain.
  class TrackingChain : public SubChain {
  public:
    struct Input_t {
      PartitionedSpan<sts::Hit> stsHits;
      PartitionedSpan<tof::Hit> tofHits;
    };

    using Return_t = std::pair<ca::Vector<ca::Track>, ca::TrackingMonitorData>;

    /// \brief  Gets internal monitor
    const ca::TrackingMonitor& GetCaMonitor() const { return fCaMonitor; }

    /// \brief  Provides action in the initialization of the run
    void Init();

    /// \brief  Provides action for a given time-slice
    /// \param  recoResults  Structure of reconstruction results
    /// \return A pair (vector of tracks, tracking monitor)
    Return_t Run(Input_t recoResults);

    /// \brief  Provides action in the end of the run
    void Finalize();

  private:
    // *********************
    // **  Utility functions

    /// \brief  Prepares input data
    /// \param  recoResults  Structure of reconstruction results
    void PrepareInput(Input_t recoResults);

    /// \brief  Reads from different detector subsystems
    /// \tparam DetID Detector ID
    /// \param  hits  Hits vector
    template<ca::EDetectorID DetID>
    void ReadHits(PartitionedSpan<const ca::HitTypes_t::at<DetID>> hits);

    /// \brief


    // *************************
    // **  Framework variables

    ca::TrackingMonitor fCaMonitor {};  ///< CA internal monitor (debug purposes)
    ca::Framework fCaFramework {};      ///< CA framework instance
    ca::DataManager fCaDataManager {};  ///< CA data manager


    // ************************
    // **  Auxilary variables

    ca::HitKeyIndex_t fNofHitKeys = 0;  ///< Current number of hit keys (aux)
  };


}  // namespace cbm::algo