BmnRoot 26.10.0-210-gc52d34b0
Software framework of the BM@N experiment
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
21 : FairTask("BmnHodoDigitizer")
22 , fScale(1337.8) // [Z^2/GeV] set Xe peak to 54*54
23 , fThreshold(0.0) // [Z^2] we see noise below this in data
24 , fTimeCut(1000.0)
25 , fSaturation(std::numeric_limits<double>::max())
26 , fKernel()
27 , fPointArray(nullptr)
28 , fDigiArray(nullptr)
29{
30 if (config && config[0] != '\0') {
31 LoadConfig(config);
32 }
33}
34
35InitStatus BmnHodoDigitizer::LoadConfig(const char* config)
36{
37 const char* vmc = getenv("VMCWORKDIR");
38 if (!vmc || !vmc[0]) {
39 LOG(error) << Form("%s::LoadConfig: VMCWORKDIR is not set", GetName());
40 SetActive(kFALSE);
41 return kERROR;
42 }
43
44 const std::string fullPath = std::string(vmc) + "/parameters/hodo/" + config;
45 std::ifstream input(fullPath);
46 if (!input.is_open()) {
47 LOG(error) << Form("%s::LoadConfig: cannot open file %s", GetName(), fullPath.c_str());
48 SetActive(kFALSE);
49 return kERROR;
50 }
51
52 nlohmann::json j;
53 try {
54 input >> j;
55 } catch (const std::exception& e) {
56 LOG(error) << Form("%s::LoadConfig: failed to parse JSON %s: %s", GetName(), fullPath.c_str(), e.what());
57 SetActive(kFALSE);
58 return kERROR;
59 }
60
61 const std::string version = j.value("version", "unknown");
62 const std::string comment = j.value("comment", "none");
63
64 if (j.contains("scale"))
65 fScale = j["scale"].get<double>();
66
67 if (j.contains("threshold"))
68 fThreshold = j["threshold"].get<double>();
69
70 if (j.contains("timecut"))
71 fTimeCut = j["timecut"].get<double>();
72
73 if (j.contains("stochastic") && j.contains("constant"))
74 fKernel = BmnHodoResolution::Kernel(j["stochastic"].get<double>(), j["constant"].get<double>());
75
76 LOG(info) << Form("%s::LoadConfig: loaded %s (version=%s, comment=%s)", GetName(), fullPath.c_str(),
77 version.c_str(), comment.c_str());
78
79 return kSUCCESS;
80}
81
82// Destructor
84{
85 if (fDigiArray) {
86 fDigiArray->Delete();
87 delete fDigiArray;
88 }
89}
90
91// Initialization method
93{
94 fworkTime = 0.0;
95 LOG(detail) << "-I- BmnHodoDigitizer: Init started..." << std::endl;
96
97 FairRootManager* ioman = FairRootManager::Instance();
98 if (!ioman) {
99 LOG(error) << "-E- BmnHodoDigitizer::Init: RootManager not instantiated!" << std::endl;
100 return kFATAL;
101 }
102
103 fPointArray = static_cast<TClonesArray*>(ioman->GetObject("HodoPoint"));
104 if (!fPointArray) {
105 LOG(error) << "-W- BmnHodoDigitizer::Init: No HodoPoint array!" << std::endl;
106 return kERROR;
107 }
108
109 fDigiArray = new TClonesArray("BmnHodoDigit");
110 ioman->Register("HodoDigit", "Hodo", fDigiArray, kTRUE);
111
112 LOG(detail) << "-I- BmnHodoDigitizer: Initialization successful" << std::endl;
113 return kSUCCESS;
114}
115
116// Execute method (digitization process)
118{
119 if (!IsActive())
120 return;
121 TStopwatch sw;
122 sw.Start();
123
124 fDigiArray->Clear();
125 FillHitMap();
126
127 for (const auto& it : fuoHitMap) {
128 uint32_t strip = it.first;
129 const auto& points = it.second;
130
131 double energyLoss = 0.0;
132 double time = 0.0;
133 for (const auto* point : points) {
134 if (FairLogger::GetLogger()->IsLogNeeded(fair::Severity::debug))
135 point->Print("");
136 energyLoss += point->GetEnergyLoss();
137 time += point->GetTime();
138 }
139 time /= points.size();
140
141 uint32_t address = BmnHodoAddress::GetAddress(strip, 0, 0);
142 double z2 = fKernel(energyLoss * fScale);
143 LOG(debug) << "BmnHodoDigitizer::Exec. Smoothed energy: " << z2;
144
145 if (z2 < fThreshold)
146 continue;
147 else {
149 new (ar[ar.GetEntriesFast()]) BmnHodoDigit(address, time, z2);
150 }
151 }
152
153 sw.Stop();
154 fworkTime += sw.RealTime();
155}
156
157// Fill the hit map with points
159{
160 fuoHitMap.clear();
161
162 for (int i = 0; i < fPointArray->GetEntriesFast(); ++i) {
163 auto* point = static_cast<BmnHodoPoint*>(fPointArray->At(i));
164 int iStrip = point->GetCopyMother();
165 fuoHitMap[iStrip].emplace_back(point);
166 }
167
168 // Sort each vector of points by their time
169 for (auto& pair : fuoHitMap) {
170 std::vector<BmnHodoPoint*>& points = pair.second;
171 std::sort(points.begin(), points.end(),
172 [](BmnHodoPoint* a, BmnHodoPoint* b) { return a->GetTime() < b->GetTime(); });
173 }
174
175 LOG(debug) << "BmnHodoDigitizer::FillHitMap() - Number of fired cells: " << fuoHitMap.size();
176}
177
178// Finish method
180{
181 printf("Work time of the Hodo digitizer: %.4f sec.\n", fworkTime);
182}
int i
Definition P4_F32vec4.h:22
friend F32vec4 max(const F32vec4 &a, const F32vec4 &b)
Definition P4_F32vec4.h:31
constexpr Bool_t kFALSE
constexpr Bool_t kTRUE
const char Option_t
char * Form(const char *fmt,...)
static uint32_t GetAddress(uint32_t StripId, uint32_t StripSide, uint32_t Gain)
Return address from system ID, StripId, StripSide, Gain.
virtual InitStatus Init()
std::unordered_map< uint32_t, std::vector< BmnHodoPoint * > > fuoHitMap
TClonesArray * fPointArray
BmnHodoDigitizer(const int period)
BmnHodoResolution::Kernel fKernel
InitStatus LoadConfig(const char *config)
virtual void Exec(Option_t *opt)
virtual void Finish()
TClonesArray * fDigiArray
Short_t GetCopyMother() const
Int_t GetEntriesFast() const
Double_t RealTime()
void Start(Bool_t reset=kTRUE)
void Stop()