BmnRoot 26.10.0-18-g4e3414f9
Software framework of the BM@N experiment
Loading...
Searching...
No Matches
BmnHodoReconstructor.cxx
Go to the documentation of this file.
1/** @file BmnHodoReconstructor.cxx
2 ** @author Nikolay Karpushkin <karpushkin@inr.ru>
3 ** @date 03.04.2021
4 **
5 ** Code for BmnHodoEvent reconstruction (creation) from BmnHodoDigi {Data} or BmnHodoDigit {Sim}
6 **/
7
9
10#include "BmnHodoGeo.h"
11
13 : fArrayOfDigits(nullptr)
14 , fBmnHodoEvent(nullptr)
15 , fCuts(nullptr)
16 , fIsExp(isExp)
17 , fworkTime(0.)
18{
19 LOG(detail) << Form("BmnHodoReconstructor: isExp %i", fIsExp);
20}
21
22void BmnHodoReconstructor::SetRecoCutsFile(const std::string& cuts)
23{
24 std::string fullPath = std::string(getenv("VMCWORKDIR")) + "/parameters/hodo/" + cuts;
25 std::ifstream test(fullPath);
26 if (!test.is_open()) {
27 LOG(warn) << Form("BmnHodoReconstructor: cuts file not found: %s. Using default (no cuts).", fullPath.c_str());
28 fCuts = nullptr;
29 return;
30 }
31
32 fCuts = new BmnHodoCuts(BmnHodoCuts::Load(fullPath, fIsExp));
33 LOG(info) << Form("BmnHodoReconstructor: cuts loaded from %s (isExp=%d)", fullPath.c_str(), fIsExp);
34}
35
37{
38 BmnHodoGeo* GeoHandler = new BmnHodoGeo();
39 GeoHandler->ReadGeometryFromGeoManager(true);
40 const auto& positionMap = GeoHandler->GetPositionMap();
41 for (auto it : positionMap) {
42 auto unique_address = it.first;
43 if (unique_address == 0)
44 continue;
45 auto strip_id = BmnHodoAddress::GetStripId(unique_address);
46 auto* strip = fBmnHodoEvent->GetOrAddStrip(strip_id);
47 strip->SetStripId(strip_id);
48 strip->SetPosition(it.second.first);
49 strip->SetPositionError(it.second.second);
50 }
51 delete GeoHandler;
52}
53
61
63{
64 fworkTime = 0.;
65 fpFairRootMgr = FairRootManager::Instance();
66 if (!fpFairRootMgr) {
67 LOG(error) << "BmnHodoReconstructor::Init(): FairRootManager instance is nullptr! Task will be deactivated";
68 SetActive(kFALSE);
69 return kERROR;
70 }
71
72 fArrayOfDigits = (TClonesArray*)fpFairRootMgr->GetObject((fIsExp) ? "HodoDigi" : "HodoDigit");
73 if (!fArrayOfDigits) {
74 LOG(error) << "BmnHodoReconstructor::Init(): branch with Digits not found! Task will be deactivated";
75 SetActive(kFALSE);
76 return kERROR;
77 }
78
80 fBmnHodoEvent->reset();
82 fpFairRootMgr->RegisterAny("HodoEvent", fBmnHodoEvent, kTRUE);
83 fpFairRootMgr->Register("HodoEvent", "Hodo", fBmnHodoEvent, kFALSE);
84
85 LOG(debug) << "Hodo Reconstructor ready";
86 return kSUCCESS;
87}
88
90{
91 BmnHodoDigit* downDigit = nullptr;
92 BmnHodoDigit* upDigit = nullptr;
93
94 int n = 0;
95 double logSum = 0.0;
96 for (auto* digit : strip.GetDigits()) {
97 if (!digit)
98 continue;
99
100 const double s = digit->GetSignal();
101 if (s < 0.0)
102 continue;
103
104 logSum += std::log(s);
105 ++n;
106
107 if (digit->GetStripSide() == 0 && !downDigit)
108 downDigit = digit;
109 else if (digit->GetStripSide() == 1 && !upDigit)
110 upDigit = digit;
111 }
112
113 if (n == 0) {
114 strip.SetSignal(0.f);
115 strip.SetIntegral(0.f);
116 return;
117 }
118
119 const double signal = std::exp(logSum / n);
120 strip.SetSignal(signal);
121
122 if (fIsExp) {
123 double integral = 0.0, dn = 0.0, up = 0.0;
124
125 if (downDigit)
126 dn = static_cast<BmnHodoDigi*>(downDigit)->fSignalIntegral;
127 if (upDigit)
128 up = static_cast<BmnHodoDigi*>(upDigit)->fSignalIntegral;
129
130 if (dn > 0.0 && up > 0.0)
131 integral = std::sqrt(dn * up);
132 else if (dn > 0.0)
133 integral = dn;
134 else if (up > 0.0)
135 integral = up;
136
137 strip.SetIntegral(integral);
138 } else {
139 strip.SetIntegral(signal);
140 }
141}
142
144{
145 if (!IsActive())
146 return;
147
148 TStopwatch sw;
149 sw.Start();
150
151 fBmnHodoEvent->ResetStrips();
153
154 auto* strips = fBmnHodoEvent->GetStrips();
155 if (!strips)
156 return;
157
158 for (int i = 0, n = strips->GetEntriesFast(); i < n; ++i) {
159 auto* strip = static_cast<BmnHodoStrip*>(strips->At(i));
160 if (!strip)
161 continue;
162
163 ProcessStrip(*strip);
164 }
165
166 fBmnHodoEvent->SummarizeEvent();
167
168 sw.Stop();
169 fworkTime += sw.RealTime();
170}
171
172std::vector<BmnHodoDigit*> BmnHodoReconstructor::GetDenoisedDigis()
173{
174 std::vector<BmnHodoDigit*> denoised;
175 denoised.reserve(fArrayOfDigits->GetEntriesFast());
176
177 for (int i = 0, n = fArrayOfDigits->GetEntriesFast(); i < n; ++i) {
178 if (fIsExp) {
179 auto* d = static_cast<BmnHodoDigi*>(fArrayOfDigits->At(i));
180 if (!d)
181 continue;
182 if (d->GetGain() == 1)
183 continue;
184 if (!fCuts || fCuts->PassExp(d))
185 denoised.push_back(d);
186 } else {
187 auto* d = static_cast<BmnHodoDigit*>(fArrayOfDigits->At(i));
188 if (!d)
189 continue;
190 if (!fCuts || fCuts->PassSim(d))
191 denoised.push_back(d);
192 }
193 }
194
195 LOG(debug4) << Form("Hodo denoise: kept %zu / %d (%.1f%%)%s", denoised.size(), fArrayOfDigits->GetEntriesFast(),
196 100.0 * denoised.size() / std::max(1, fArrayOfDigits->GetEntriesFast()),
197 fCuts ? "" : " [no cuts applied]");
198 return denoised;
199}
200
201void BmnHodoReconstructor::MarkPileup(const std::vector<BmnHodoDigit*>& input)
202{
203 std::unordered_map<uint32_t, std::vector<BmnHodoDigi*>> byAddr;
204 byAddr.reserve(input.size());
205
206 for (auto* d : input)
207 byAddr[d->GetAddress()].push_back(static_cast<BmnHodoDigi*>(d));
208
209 for (auto& [addr, vec] : byAddr) {
210 const int strip_id = BmnHodoAddress::GetStripId(addr);
211 auto* strip = fBmnHodoEvent->GetOrAddStrip(strip_id);
212
213 if (vec.size() == 1) {
214 auto* d = vec.front();
215 if (d->fNpeaks > 1)
216 strip->SetPileUpStatus(BmnHodoStrip::PileUp::Resolved);
217 } else {
218 strip->SetPileUpStatus(BmnHodoStrip::PileUp::Unresolved);
219 }
220 }
221 return;
222}
223
224// Build map: strip_id -> vector of digis
226{
227 // 1. denoise
228 auto denoised = GetDenoisedDigis();
229
230 // 2. exp only: mark pileup
231 if (fIsExp)
232 MarkPileup(denoised);
233
234 // 3. build map strip_id -> digis
235 for (auto* digi : denoised)
236 fBmnHodoEvent->GetOrAddStrip(digi->GetStripId())->AddDigit(digi);
237}
238
239void BmnHodoReconstructor::OnlineWrite(const std::unique_ptr<TTree>& resultTree)
240{
241 if (!IsActive())
242 return;
243
244 resultTree->Branch("HodoEvent", &fBmnHodoEvent);
245 resultTree->Fill();
246}
247
249{
250 printf("Work time of BmnHodoReconstructor: %4.2f sec.\n", fworkTime);
251}
int i
Definition P4_F32vec4.h:22
constexpr Bool_t kFALSE
constexpr Bool_t kTRUE
const char Option_t
char * Form(const char *fmt,...)
const auto & GetPositionMap() const
BmnStatus ReadGeometryFromGeoManager(bool getGlobalPosition=true)
static uint32_t GetStripId(uint32_t address)
Return Strip id from address.
Data class for BmnHodo digital signal processing.
Definition BmnHodoDigi.h:25
FairRootManager * fpFairRootMgr
void SetRecoCutsFile(const std::string &cuts)
void MarkPileup(const std::vector< BmnHodoDigit * > &input)
virtual void OnlineWrite(const std::unique_ptr< TTree > &resultTree)
Write task resul to tree.
std::vector< BmnHodoDigit * > GetDenoisedDigis()
virtual InitStatus Init()
void ProcessStrip(BmnHodoStrip &strip)
virtual void Exec(Option_t *opt)
void SetIntegral(float signal)
void SetSignal(float signal)
const std::vector< BmnHodoDigit * > & GetDigits() const
Double_t RealTime()
void Start(Bool_t reset=kTRUE)
void Stop()
*brief user defined string literal for JSON values *sa std::size_t n
Definition json.hpp:20362
static BmnHodoCuts Load(const std::string &file, bool isExp)
Definition BmnHodoCuts.h:50