13#include "FairRootManager.h"
15#include "FairRunAna.h"
16#include "FairRuntimeDb.h"
17#include "TStopwatch.h"
18#include "TVirtualMC.h"
20#include <nlohmann/json.hpp>
27 : FairTask(
"BmnNdetDigitizer")
31 , fSiPM(160000, 165, 0.003, 0.005)
32 , fPointArray(nullptr)
43 const char* vmc = getenv(
"VMCWORKDIR");
44 if (!vmc || !vmc[0]) {
45 LOG(error) << Form(
"%s::LoadConfig: VMCWORKDIR is not set", GetName());
50 const std::string fullPath = std::string(vmc) +
"/parameters/ndet/" +
config;
51 std::ifstream input(fullPath);
52 if (!input.is_open()) {
53 LOG(error) << Form(
"%s::LoadConfig: cannot open file %s", GetName(), fullPath.c_str());
61 }
catch (
const std::exception& e) {
62 LOG(error) << Form(
"%s::LoadConfig: failed to parse JSON %s: %s", GetName(), fullPath.c_str(), e.what());
67 const std::string version = j.
value(
"version",
"unknown");
68 const std::string comment = j.
value(
"comment",
"none");
71 fScale = j[
"scale"].
get<
double>();
74 fThreshold = j[
"threshold"].get<double>();
77 fTimeCut = j[
"timecut"].get<double>();
80 const auto& jsipm = j[
"sipm"];
87 if (jsipm.contains(
"n_pixels"))
88 nPixels = jsipm[
"n_pixels"].get<int>();
90 if (jsipm.contains(
"mip_pixels"))
91 mipPixels = jsipm[
"mip_pixels"].get<int>();
93 if (jsipm.contains(
"noise"))
94 noise = jsipm[
"noise"].get<double>();
96 if (jsipm.contains(
"mip_energy"))
97 mipEnergy = jsipm[
"mip_energy"].get<double>();
99 fSiPM =
SiPM(nPixels, mipPixels, noise, mipEnergy);
102 LOG(info) << Form(
"%s::LoadConfig: loaded %s (version=%s, comment=%s)", GetName(), fullPath.c_str(),
103 version.c_str(), comment.c_str());
111 fDigiArray->Delete();
119 LOG(detail) <<
"-I- BmnNdetDigitizer: Init started..." << std::endl;
122 FairRootManager* ioman = FairRootManager::Instance();
124 LOG(error) <<
"-E- BmnNdetDigitizer::Init: RootManager not instantiated!" << std::endl;
129 fPointArray =
static_cast<TClonesArray*
>(ioman->GetObject(
"NdetPoint"));
131 LOG(error) <<
"-W- BmnNdetDigitizer::Init: No NdetPoint array!" << std::endl;
136 fDigiArray =
new TClonesArray(
"BmnNdetDigit");
137 ioman->Register(
"NdetDigit",
"Ndet", fDigiArray, kTRUE);
139 LOG(detail) <<
"-I- BmnNdetDigitizer: Initialization successful" << std::endl;
150 LOG(debug) <<
"BmnNdetDigitizer::Exec() started..." << std::endl;
152 Fatal(
"Exec",
"No DigiArray");
157 for (
auto& it : fuoHitMap) {
158 uint32_t address = it.first;
159 const auto& tracks_contributions = it.second;
161 double totalEnergy = 0.0;
162 double crossTime = 0.0;
163 bool reachedThreshold =
false;
164 for (
auto* point : tracks_contributions) {
165 if (point->GetTime() > fTimeCut)
167 if (FairLogger::GetLogger()->IsLogNeeded(fair::Severity::debug2))
170 totalEnergy += point->GetEnergyLoss();
171 if (!reachedThreshold && totalEnergy * fScale > fThreshold) {
172 crossTime = point->GetTime();
173 reachedThreshold =
true;
178 point->GetEnergyLoss()));
179 LOG(debug2) << Form(
"Reached threshold. Energy %.4f Weighted time %.4f", totalEnergy, crossTime);
183 totalEnergy *= fScale;
185 if (!reachedThreshold || totalEnergy < std::numeric_limits<double>::epsilon()) {
189 TClonesArray& ar = *fDigiArray;
190 long entries = fDigiArray->GetEntriesFast();
191 new (ar[entries])
BmnNdetDigit(address, crossTime, totalEnergy);
192 if (FairLogger::GetLogger()->IsLogNeeded(fair::Severity::debug2))
198 fworkTime += sw.RealTime();
201void BmnNdetDigitizer::FillHitMap()
206 for (
int iPoint = 0, nPoints = fPointArray->GetEntriesFast(); iPoint < nPoints; ++iPoint) {
207 auto* point =
static_cast<BmnNdetPoint*
>(fPointArray->At(iPoint));
209 fuoHitMap[address].push_back(point);
213 for (
auto& pair : fuoHitMap) {
214 std::vector<BmnNdetPoint*>& points = pair.second;
215 std::sort(points.begin(), points.end(),
219 LOG(debug) <<
"BmnNdetDigitizer::FillHitMap() event " << gMC->CurrentEvent() <<
" fired cells " << fuoHitMap.size();
224 printf(
"Work time of the Ndet digitizer: %.4f sec.\n", fworkTime);
static std::string GetInfoString(uint32_t address)
Return a formatted string with all address components.
virtual ~BmnNdetDigitizer()
virtual void Exec(Option_t *opt)
InitStatus LoadConfig(const char *config)
BmnNdetDigitizer(const int period)
virtual InitStatus Init()
uint32_t GetAddress() const
a class to store JSON values
bool contains(KeyT &&key) const
check the existence of an element in a JSON object
ValueType value(const typename object_t::key_type &key, const ValueType &default_value) const
access specified object element with default value
constexpr bool is_object() const noexcept
return whether value is an object
auto get() const noexcept(noexcept(std::declval< const basic_json_t & >().template get_impl< ValueType >(detail::priority_tag< 4 > {}))) -> decltype(std::declval< const basic_json_t & >().template get_impl< ValueType >(detail::priority_tag< 4 > {}))
get a (pointer) value (explicit)
double ModelResponse(double pfELoss)