BmnRoot
Loading...
Searching...
No Matches
BmnHodoDigitizer.cxx
Go to the documentation of this file.
1#include "BmnHodoDigitizer.h"
2
3#include "BmnHodoAddress.h"
4#include "FairRootManager.h"
5#include "TMath.h"
6#include "TStopwatch.h"
7
8#include <FairRun.h>
9#include <FairRunSim.h>
10#include <cstdlib>
11#include <fstream>
12#include <nlohmann/json.hpp>
13#include <stdexcept>
14#include <string>
15
17 : BmnHodoDigitizer(Form("HODO_digitizer_period%d.json", period))
18{
19 LoadCalibration(Form("HODO_Q_calibration_period%d.json", period));
20}
21
23 : FairTask("BmnHodoDigitizer")
24 , fScale(1337.8) // [Z^2/GeV] set Xe peak to 54*54
25 , fThreshold(0.0) // [Z^2] we see noise below this in data
26 , fTimeCut(1000.0)
27 , fSaturation(std::numeric_limits<double>::max())
28 , fPointArray(nullptr)
29 , fDigiArray(nullptr)
30{
31 if (config && config[0] != '\0') {
33 }
34}
35
36InitStatus BmnHodoDigitizer::LoadConfig(const char* config)
37{
38 const char* vmc = getenv("VMCWORKDIR");
39 if (!vmc || !vmc[0]) {
40 LOG(error) << Form("%s::LoadConfig: VMCWORKDIR is not set", GetName());
41 SetActive(kFALSE);
42 return kERROR;
43 }
44
45 const std::string fullPath = std::string(vmc) + "/parameters/hodo/" + config;
46 std::ifstream input(fullPath);
47 if (!input.is_open()) {
48 LOG(error) << Form("%s::LoadConfig: cannot open file %s", GetName(), fullPath.c_str());
49 SetActive(kFALSE);
50 return kERROR;
51 }
52
54 try {
55 input >> j;
56 } catch (const std::exception& e) {
57 LOG(error) << Form("%s::LoadConfig: failed to parse JSON %s: %s", GetName(), fullPath.c_str(), e.what());
58 SetActive(kFALSE);
59 return kERROR;
60 }
61
62 const std::string version = j.value("version", "unknown");
63 const std::string comment = j.value("comment", "none");
64
65 if (j.contains("scale"))
66 fScale = j["scale"].get<double>();
67
68 if (j.contains("threshold"))
69 fThreshold = j["threshold"].get<double>();
70
71 if (j.contains("timecut"))
72 fTimeCut = j["timecut"].get<double>();
73
74 LOG(info) << Form("%s::LoadConfig: loaded %s (version=%s, comment=%s)", GetName(), fullPath.c_str(),
75 version.c_str(), comment.c_str());
76
77 return kSUCCESS;
78}
79
80// Destructor
82{
83 if (fDigiArray) {
84 fDigiArray->Delete();
85 delete fDigiArray;
86 }
87}
88
89// Initialization method
91{
92 fworkTime = 0.0;
93 LOG(detail) << "-I- BmnHodoDigitizer: Init started..." << std::endl;
94
95 FairRootManager* ioman = FairRootManager::Instance();
96 if (!ioman) {
97 LOG(error) << "-E- BmnHodoDigitizer::Init: RootManager not instantiated!" << std::endl;
98 return kFATAL;
99 }
100
101 fPointArray = static_cast<TClonesArray*>(ioman->GetObject("HodoPoint"));
102 if (!fPointArray) {
103 LOG(error) << "-W- BmnHodoDigitizer::Init: No HodoPoint array!" << std::endl;
104 return kERROR;
105 }
106
107 fDigiArray = new TClonesArray("BmnHodoDigit");
108 ioman->Register("HodoDigit", "Hodo", fDigiArray, kTRUE);
109
110 LOG(detail) << "-I- BmnHodoDigitizer: Initialization successful" << std::endl;
111 return kSUCCESS;
112}
113
114// Setter for energy resolution configuration
115void BmnHodoDigitizer::LoadCalibration(const char* inputFile)
116{
117 std::string fullPath = std::string(getenv("VMCWORKDIR")) + "/parameters/hodo/" + inputFile;
118 std::ifstream input(fullPath);
119 if (!input.is_open()) {
120 LOG(error) << "BmnHodoDigitizer::LoadCalibration: Cannot open file " << fullPath;
121 return;
122 }
123
125 try {
126 input >> j;
127 } catch (const std::exception& e) {
128 LOG(error) << "BmnHodoDigitizer::LoadCalibration: Failed to parse JSON: " << e.what();
129 return;
130 }
131
132 std::string version = j.value("version", "unknown");
133 std::string comment = j.value("comment", "none");
134
135 LOG(debug) << "BmnHodoDigitizer::LoadCalibration. Version: " << version;
136 LOG(debug) << "BmnHodoDigitizer::LoadCalibration. Comment: " << comment;
137
138 fSaturation = j.at("parameters").at("saturation");
139
140 fuoResolutionMap.clear();
141 for (const auto& entry : j["calibration"]) {
142 int strip = entry["strip"];
143 int side = entry["side"];
144 int gain = entry["gain"];
145 double calib = entry["calib"];
146 double stochastic = entry["stochastic"];
147 double constant = entry["constant"];
148
149 uint32_t address = BmnHodoAddress::GetAddress(strip, side, gain);
150 Kernel resolution(stochastic, constant);
151 fuoResolutionMap.emplace(address, std::make_pair(calib, resolution));
152 }
153}
154
155// Execute method (digitization process)
156void BmnHodoDigitizer::Exec(Option_t* opt)
157{
158 if (!IsActive())
159 return;
160 TStopwatch sw;
161 sw.Start();
162
163 fDigiArray->Clear();
164 FillHitMap();
165
166 for (const auto& it : fuoHitMap) {
167 uint32_t strip = it.first;
168 const auto& points = it.second;
169
170 double energyLoss = 0.0;
171 double time = 0.0;
172 for (const auto* point : points) {
173 if (FairLogger::GetLogger()->IsLogNeeded(fair::Severity::debug))
174 point->Print("");
175 energyLoss += point->GetEnergyLoss();
176 time += point->GetTime();
177 }
178 time /= points.size();
179
180 if (fuoResolutionMap.empty()) {
181 uint32_t address = BmnHodoAddress::GetAddress(strip, 0, 0);
182 double z2 = energyLoss * fScale;
183 LOG(debug) << "BmnHodoDigitizer::Exec Empty resoultion map. Smoothed energy: " << z2;
184
185 if (z2 < fThreshold)
186 continue;
187 else {
188 TClonesArray& ar = *fDigiArray;
189 new (ar[ar.GetEntriesFast()]) BmnHodoDigit(address, time, z2);
190 }
191 } else {
192 for (const auto& jt : fuoResolutionMap) {
193 uint32_t address = jt.first;
194 if (BmnHodoAddress::GetStripId(address) != strip)
195 continue;
196 double z2 = jt.second.second(energyLoss * fScale);
197 double adc = z2 / jt.second.first;
198 LOG(debug) << Form(
199 "BmnHodoDigitizer::Exec Resoultion map for strid %d side %d gain %d. Smoothed energy: %f",
201 BmnHodoAddress::GetGain(address), z2);
202
203 if (z2 < fThreshold)
204 continue;
205 else {
206 TClonesArray& ar = *fDigiArray;
207 BmnHodoDigit* digit = new (ar[ar.GetEntriesFast()]) BmnHodoDigit(address, time, z2);
208 if (adc >= fSaturation) {
209 digit->SetSignal(fSaturation * jt.second.first);
210 digit->SetIsSaturated(true);
211 }
212 }
213 }
214 }
215 }
216
217 sw.Stop();
218 fworkTime += sw.RealTime();
219}
220
221// Fill the hit map with points
222void BmnHodoDigitizer::FillHitMap()
223{
224 fuoHitMap.clear();
225
226 for (int i = 0; i < fPointArray->GetEntriesFast(); ++i) {
227 auto* point = static_cast<BmnHodoPoint*>(fPointArray->At(i));
228 int iStrip = point->GetCopyMother();
229 fuoHitMap[iStrip].emplace_back(point);
230 }
231
232 LOG(debug) << "BmnHodoDigitizer::FillHitMap() - Number of fired cells: " << fuoHitMap.size();
233}
234
235// Finish method
237{
238 printf("Work time of the Hodo digitizer: %.4f sec.\n", fworkTime);
239}
int i
Definition P4_F32vec4.h:22
friend F32vec4 max(const F32vec4 &a, const F32vec4 &b)
Definition P4_F32vec4.h:31
void SetSignal(double signal)
static uint32_t GetStripSide(uint32_t address)
Return Strip side from address.
static uint32_t GetStripId(uint32_t address)
Return Strip id from address.
static uint32_t GetAddress(uint32_t StripId, uint32_t StripSide, uint32_t Gain)
Return address from system ID, StripId, StripSide, Gain.
static uint32_t GetGain(uint32_t address)
Return Gain from address.
void SetIsSaturated(bool satur)
virtual InitStatus Init()
BmnHodoDigitizer(const int period)
InitStatus LoadConfig(const char *config)
virtual void Exec(Option_t *opt)
void LoadCalibration(const char *calib)
virtual void Finish()
Short_t GetCopyMother() const
a class to store JSON values
Definition json.hpp:17282
bool contains(KeyT &&key) const
check the existence of an element in a JSON object
Definition json.hpp:19640
ValueType value(const typename object_t::key_type &key, const ValueType &default_value) const
access specified object element with default value
Definition json.hpp:19319
reference at(size_type idx)
access specified array element with bounds checking
Definition json.hpp:19090
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)
Definition json.hpp:18898
STL namespace.