Skip to content

Commit

Permalink
create R3B::UcesbSource2
Browse files Browse the repository at this point in the history
  • Loading branch information
YanzhaoW committed Jan 7, 2024
1 parent 494f869 commit f5af579
Show file tree
Hide file tree
Showing 11 changed files with 1,062 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,8 @@ input/
build-r3broot.sh
cmake-build-debug/

#conan:
CMakeUserPresets.json

# Local configuration
c3w.conf
5 changes: 5 additions & 0 deletions r3bsource/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ set(INCLUDE_DIRECTORIES
#put here all directories where header files are located
${R3BROOT_SOURCE_DIR}/r3bsource
${R3BROOT_SOURCE_DIR}/r3bsource/base
${R3BROOT_SOURCE_DIR}/r3bsource/base/utils
${R3BROOT_SOURCE_DIR}/r3bsource/wr
${R3BROOT_SOURCE_DIR}/r3bsource/trloii
${R3BROOT_SOURCE_DIR}/r3bsource/beammonitor
Expand Down Expand Up @@ -91,7 +92,10 @@ include_directories(SYSTEM ${SYSTEM_INCLUDE_DIRECTORIES})
link_directories( ${LINK_DIRECTORIES})

set(SRCS
./base/utils/R3BUcesbLauncher.cxx
./base/utils/R3BUcesbStructInfo.cxx
./base/R3BUcesbSource.cxx
./base/R3BUcesbSource2.cxx
./base/R3BReader.cxx
./base/R3BUnpackReader.cxx
./wr/R3BWhiterabbitMasterReader.cxx
Expand Down Expand Up @@ -237,3 +241,4 @@ set(DEPENDENCIES
set(LIBRARY_NAME R3Bsource)

GENERATE_LIBRARY()
target_include_directories(R3Bsource PUBLIC neuland base base/utils trloii wr ${SYSTEM_INCLUDE_DIRECTORIES})
15 changes: 15 additions & 0 deletions r3bsource/base/R3BReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include "TNamed.h"
#include "TString.h"
#include <R3BUcesbMappingFlag.h>

extern "C"
{
Expand All @@ -30,6 +31,14 @@ class R3BReader : public TNamed
public:
R3BReader(TString const&);
virtual ~R3BReader();
[[nodiscard]] virtual bool MismappedItemRequired(std::string_view /*item_name*/) const { return false; }
bool AllowExtraCondition(R3B::UcesbMap map_flag, R3B::UcesbMap success_condition)
{
return (map_flag & ~(success_condition | extra_conditions_)) == R3B::UcesbMap::zero;
}

void SetExtraConditions(R3B::UcesbMap conditions) { extra_conditions_ = conditions; }
void AddExtraConditions(R3B::UcesbMap conditions) { extra_conditions_ |= conditions; }

/* Setup structure information */
virtual Bool_t Init(ext_data_struct_info*) = 0;
Expand All @@ -41,6 +50,12 @@ class R3BReader : public TNamed
virtual void Reset() = 0;
/* Return actual name of the reader */

// actions when closed
virtual void Close(){};

private:
R3B::UcesbMap extra_conditions_ = R3B::UcesbMap::zero;

public:
ClassDef(R3BReader, 0);
};
Expand Down
218 changes: 218 additions & 0 deletions r3bsource/base/R3BUcesbSource2.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
/******************************************************************************
* Copyright (C) 2019 GSI Helmholtzzentrum für Schwerionenforschung GmbH *
* Copyright (C) 2019-2023 Members of R3B Collaboration *
* *
* This software is distributed under the terms of the *
* GNU General Public Licence (GPL) version 3, *
* copied verbatim in the file "LICENSE". *
* *
* In applying this license GSI does not waive the privileges and immunities *
* granted to it by virtue of its status as an Intergovernmental Organization *
* or submit itself to any jurisdiction. *
******************************************************************************/

#include "R3BUcesbSource2.h"
#include <FairRootManager.h>
#include <FairRun.h>
#include <R3BEventHeader.h>
#include <R3BException.h>
#include <R3BLogger.h>
#include <R3BUcesbDecl.h>
#include <boost/core/span.hpp>
#include <ext_data_client.h>
#include <fmt/format.h>

namespace R3B
{
UcesbSource::UcesbSource(std::string_view lmdfile_name,
std::string_view ntuple_options,
std::string_view ucesb_path,
EventStructType* event_struct,
size_t event_struct_size)
: event_struct_size_{ event_struct_size }
, event_struct_{ event_struct }
, lmdfile_name_{ lmdfile_name }
, ntuple_options_{ ntuple_options }
, ucesb_path_{ ucesb_path }
{
}

bool UcesbSource::Init()
{
init_runID();
init_ucesb();
return true;
}

UcesbSource::~UcesbSource() { ucesb_server_launcher_.Close(); }

void UcesbSource::init_ucesb()
{
auto command_string = fmt::format("{0} {1} --ntuple={2},STRUCT,-", ucesb_path_, lmdfile_name_, ntuple_options_);
if (max_event_num_ > 0)
{
command_string = fmt::format("{} --max-events={}", command_string, max_event_num_);
}
R3BLOG(info, fmt::format("Calling ucesb with command: {}", command_string));

ucesb_server_launcher_.Launch(std::move(command_string));
}

bool UcesbSource::InitUnpackers()
{
if (auto* frm = FairRootManager::Instance(); frm != nullptr)
{
R3BLOG(debug, "Checking the register of R3BEventHeader");
if (event_header_ = dynamic_cast<R3BEventHeader*>(frm->GetObject("EventHeader.")); event_header_ == nullptr)
{
throw R3B::runtime_error("EventHeader. was not defined properly!");
}
}
R3BLOG(debug, "EventHeader. was defined properly");

init_readers();
setup_ucesb();

return true;
}

void UcesbSource::setup_ucesb()
{
// TODO: convert to std::bitset
// could be initialzed in type UcesbMap. But C++ doesn't allow static cast of enum class pointer to its
// underlying type
auto is_struct_map_success = uint32_t{};
if (ucesb_client_.setup(
nullptr, 0, ucesb_client_struct_info_.Get(), &is_struct_map_success, event_struct_size_) == 0)
{
ucesb_client_struct_info_.CheckStructMapping(this);
}
else
{
R3BLOG(error, "ext_data_clnt::setup() failed");
const auto* msg = (ucesb_client_.last_error() == nullptr) ? UCESB_NULL_STR_MSG : ucesb_client_.last_error();
throw R3B::runtime_error(fmt::format("UCESB error: {}", msg));
}
}

int UcesbSource::ReadEvent(unsigned int /*eventID*/)
{
auto ret_val = ucesb_client_.fetch_event(event_struct_, event_struct_size_);
if (ret_val > 0)
{
ForEachReader([](auto& reader) { reader->R3BRead(); });
}
else if (ret_val == 0)
{
R3BLOG(info, "Reached the maximal event num on the ucesb server.");
// ending event loop here
return 1;
}
else
{
R3BLOG(error, "ext_data_clnt::fetch_event() failed");
const auto* msg = (ucesb_client_.last_error() == nullptr) ? UCESB_NULL_STR_MSG : ucesb_client_.last_error();
throw R3B::runtime_error(fmt::format("UCESB error: {}", msg));
}

return 0;
}

void print_uint32_with_size(const uint32_t* data, ssize_t size)
{
// TODO: use ranges library instead of reinterpret_cast
constexpr auto column_size = 8;
using SubDataType = const std::array<uint32_t, column_size>;

auto data_span = std::span<SubDataType>(reinterpret_cast<SubDataType*>(data), size / column_size);
R3BLOG(info, "Raw data:");
auto index = uint32_t{};
for (const auto& row_data : data_span)
{
fmt::print("RAW{0:04x}: {1:08x}\n", index, fmt::join(row_data, " "));
index += column_size;
}
}

void UcesbSource::print_raw_data()
{
// TODO: what's the best way to deal with this void** monstrosity
const void* raw_data = nullptr;
ssize_t raw_data_size = 0;
auto ret_val = ucesb_client_.get_raw_data(&raw_data, &raw_data_size);

if (ret_val == 0)
{
if (raw_data != nullptr)
{
const auto* data = reinterpret_cast<const uint32_t*>(raw_data);
print_uint32_with_size(data, raw_data_size);
}
}
else
{
R3BLOG(error, "ext_data_clnt::get_raw_data()");
throw R3B::runtime_error("Failed to get raw data.");
}
}

void UcesbSource::init_runID()
{
auto* run = FairRun::Instance();
if (run == nullptr)
{
throw R3B::runtime_error("FairRun is not available!");
}

if (run_id_ != 0)
{
R3BLOG(info, fmt::format("Setting the run ID of the FairRun to be {} from FairSource", run_id_));
run->SetRunId(run_id_);
}
else if (auto run_id = run->GetRunId(); run_id != 0)
{
R3BLOG(info, fmt::format("Setting the run ID of the FairSource to be {} from FairRun", run_id));
run_id_ = run_id;
}
else
{
R3BLOG(warn, "Run ID of neither FairRun nor FairSource is set!");
}
}

void UcesbSource::FillEventHeader(FairEventHeader* feh) { feh->SetRunId(run_id_); }

int UcesbSource::CheckMaxEventNo(int EvtEnd)
{
max_event_num_ = (EvtEnd == 0) ? max_event_num_ : EvtEnd;
return static_cast<int>(max_event_num_);
}

// readers looping methods:
void UcesbSource::init_readers()
{
ForEachReader(
[this](auto& reader)
{
if (not reader->Init(ucesb_client_struct_info_.Get()))
{
const auto* msg =
(ucesb_client_.last_error() == nullptr) ? UCESB_NULL_STR_MSG : ucesb_client_.last_error();
throw R3B::runtime_error(fmt::format("UCESB error: {}", msg));
}
});
}

bool UcesbSource::ReInitUnpackers()
{
ForEachReader(
[](auto& reader)
{
if (!reader->ReInit())
{
throw R3B::runtime_error("ReInit of a reader failed.");
}
});
return true;
}
} // namespace R3B
Loading

0 comments on commit f5af579

Please sign in to comment.