BmnRoot
Loading...
Searching...
No Matches
BmnScWallDigitizer.cxx
Go to the documentation of this file.
1/*
2 * File: BmnScWallDigitizer.cxx
3 * Author: Sergey Morozov
4 *
5 * Created on 16.09.2021, 12:00
6 */
7
9
10#include "BmnScWallAddress.h"
11#include "FairRootManager.h"
12#include "TStopwatch.h"
13
14#include <FairRun.h>
15#include <FairRunSim.h>
16#include <nlohmann/json.hpp>
17
19 : BmnScWallDigitizer(Form("SCWALL_digitizer_period%d.json", period))
20{}
21
23 : FairTask("BmnScWallDigitizer")
24 , fScale(1.0)
25 , fThreshold(0.0)
26 , fTimeCut(1000.0)
27 , fSiPM(2668, 55, 0.1, 0.0021)
28 , fPointArray(nullptr)
29 , fDigiArray(nullptr)
30 , fworkTime(0.0f)
31{
32 if (config && config[0] != '\0') {
34 }
35}
36
38{
39 const char* vmc = getenv("VMCWORKDIR");
40 if (!vmc || !vmc[0]) {
41 LOG(error) << Form("%s::LoadConfig: VMCWORKDIR is not set", GetName());
42 SetActive(kFALSE);
43 return kERROR;
44 }
45
46 const std::string fullPath = std::string(vmc) + "/parameters/scwall/" + config;
47 std::ifstream input(fullPath);
48 if (!input.is_open()) {
49 LOG(error) << Form("%s::LoadConfig: cannot open file %s", GetName(), fullPath.c_str());
50 SetActive(kFALSE);
51 return kERROR;
52 }
53
55 try {
56 input >> j;
57 } catch (const std::exception& e) {
58 LOG(error) << Form("%s::LoadConfig: failed to parse JSON %s: %s", GetName(), fullPath.c_str(), e.what());
59 SetActive(kFALSE);
60 return kERROR;
61 }
62
63 const std::string version = j.value("version", "unknown");
64 const std::string comment = j.value("comment", "none");
65
66 if (j.contains("scale"))
67 fScale = j["scale"].get<double>();
68
69 if (j.contains("threshold"))
70 fThreshold = j["threshold"].get<double>();
71
72 if (j.contains("timecut"))
73 fTimeCut = j["timecut"].get<double>();
74
75 if (j.contains("sipm") && j["sipm"].is_object()) {
76 const auto& jsipm = j["sipm"];
77
78 int nPixels = fSiPM.Npixels;
79 int mipPixels = fSiPM.pixPerMIP;
80 double noise = fSiPM.noiseMIP;
81 double mipEnergy = fSiPM.gevPerMIP;
82
83 if (jsipm.contains("n_pixels"))
84 nPixels = jsipm["n_pixels"].get<int>();
85
86 if (jsipm.contains("mip_pixels"))
87 mipPixels = jsipm["mip_pixels"].get<int>();
88
89 if (jsipm.contains("noise"))
90 noise = jsipm["noise"].get<double>();
91
92 if (jsipm.contains("mip_energy"))
93 mipEnergy = jsipm["mip_energy"].get<double>();
94
95 fSiPM = SiPM(nPixels, mipPixels, noise, mipEnergy);
96 }
97
98 LOG(info) << Form("%s::LoadConfig: loaded %s (version=%s, comment=%s)", GetName(), fullPath.c_str(),
99 version.c_str(), comment.c_str());
100
101 return kSUCCESS;
102}
103
104// Destructor
106{
107 if (fDigiArray) {
108 fDigiArray->Delete();
109 delete fDigiArray;
110 }
111}
112
113// Initialization method
115{
116 fworkTime = 0.0;
117 LOG(detail) << "-I- BmnScWallDigitizer: Init started..." << std::endl;
118
119 FairRootManager* ioman = FairRootManager::Instance();
120 if (!ioman) {
121 LOG(error) << "-E- BmnScWallDigitizer::Init: RootManager not instantiated!" << std::endl;
122 return kFATAL;
123 }
124
125 fPointArray = static_cast<TClonesArray*>(ioman->GetObject("ScWallPoint"));
126 if (!fPointArray) {
127 LOG(error) << "-E- BmnScWallDigitizer::Init: No ScWallPoint array!" << std::endl;
128 return kERROR;
129 }
130
131 fDigiArray = new TClonesArray("BmnScWallDigit");
132 ioman->Register("ScWallDigit", "ScWall", fDigiArray, kTRUE);
133
134 LOG(detail) << "-I- BmnScWallDigitizer: Initialization successful" << std::endl;
135 return kSUCCESS;
136}
137
138void BmnScWallDigitizer::Exec(Option_t* opt)
139{
140 if (!IsActive())
141 return;
142 TStopwatch sw;
143 sw.Start();
144
145 LOG(debug2) << "BmnScWallDigitizer::Exec() started..." << std::endl;
146 if (!fDigiArray)
147 Fatal("Exec", "No DigiArray");
148
149 fDigiArray->Clear();
150 FillHitMap(); // One address (key) is now associated with a sorted vector of tracks which fired it
151
152 for (auto& it : fuoHitMap) {
153 uint32_t address = it.first;
154 const auto& tracks_contributions = it.second;
155
156 double totalEnergy = 0.0;
157 double crossTime = 0.0; // time when threshold is crossed
158 bool reachedThreshold = false;
159 for (const auto* point : tracks_contributions) {
160 if (point->GetTime() > fTimeCut)
161 break;
162
163 totalEnergy += point->GetEnergyLoss();
164 if (!reachedThreshold && totalEnergy * fScale > fThreshold) {
165 crossTime = point->GetTime();
166 reachedThreshold = true;
167 LOG(debug2) << Form("Reached threshold. Energy %.4f Weighted time %.4f", totalEnergy, crossTime);
168 }
169 }
170 totalEnergy = fSiPM.ModelResponse(totalEnergy);
171 totalEnergy *= fScale;
172
173 if (!reachedThreshold || totalEnergy < std::numeric_limits<double>::epsilon()) {
174 continue;
175 } else {
176 TClonesArray& ar = *fDigiArray;
177 long entries = fDigiArray->GetEntriesFast();
178 new (ar[entries]) BmnScWallDigit(address, crossTime, totalEnergy);
179 if (FairLogger::GetLogger()->IsLogNeeded(fair::Severity::debug2))
180 static_cast<BmnScWallDigit*>(ar.At(entries))->Print();
181 }
182 }
183
184 sw.Stop();
185 fworkTime += sw.RealTime();
186}
187
188void BmnScWallDigitizer::FillHitMap()
189{
190 fuoHitMap.clear();
191
192 // Fill the hit map with points from the TClonesArray
193 for (int iPoint = 0, nPoint = fPointArray->GetEntriesFast(); iPoint < nPoint; ++iPoint) {
194 auto* point = static_cast<BmnScWallPoint*>(fPointArray->At(iPoint));
195 uint32_t address = BmnScWallAddress::GetAddress(point->GetCopyMother());
196 fuoHitMap[address].push_back(point);
197 }
198
199 // Sort each vector of points by their time
200 for (auto& pair : fuoHitMap) {
201 std::vector<BmnScWallPoint*>& points = pair.second;
202 std::sort(points.begin(), points.end(),
203 [](BmnScWallPoint* a, BmnScWallPoint* b) { return a->GetTime() < b->GetTime(); });
204 }
205
206 LOG(debug2) << "BmnScWallDigitizer::FillHitMap() event " << gMC->CurrentEvent() << " fired sections "
207 << fuoHitMap.size();
208}
209
211{
212 printf("Work time of the ScWall digitizer: %.4f sec.\n", fworkTime);
213}
static uint32_t GetAddress(uint32_t CellId)
Return address from system ID, Cell ID.
virtual void Exec(Option_t *opt)
InitStatus LoadConfig(const char *config)
BmnScWallDigitizer(const int period)
virtual InitStatus Init()
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
constexpr bool is_object() const noexcept
return whether value is an object
Definition json.hpp:18511
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
double ModelResponse(double pfELoss)