BmnRoot 26.10.0-18-g4e3414f9
Software framework of the BM@N experiment
Loading...
Searching...
No Matches
BmnNdetReconstructor.cxx
Go to the documentation of this file.
1/** @file BmnNdetReconstructor.cxx
2 ** @author Nikolay Karpushkin <karpushkin@inr.ru>
3 ** @date 03.04.2021
4 **
5 ** Code for BmnNdetEvent reconstruction (creation) from BmnNdetDigi {Data} or BmnNdetDigit {Sim}
6 **/
7
9
10#include "BmnNdetDigi.h"
11#include "BmnNdetDigit.h"
12#include "BmnNdetGeo.h"
13#include "BmnNdetPoint.h"
14#include "BmnNdetSurfacePoint.h"
15#include "FairLogger.h"
16#include "TTreeFormula.h"
17
18#include <TStopwatch.h>
19
20BmnNdetReconstructor::BmnNdetReconstructor(bool isExp, bool isGlobalCoordinates)
21 : fBmnNdetEvent(nullptr)
22 , fCuts(nullptr)
23 , fIsExp(isExp)
24 , fIsGlobal(isGlobalCoordinates)
25 , fworkTime(0.)
26{
27 LOG(detail) << Form("BmnNdetReconstructor: isExp %i; isGlobal %i", fIsExp, fIsGlobal);
28 fDoMatchMC = (fIsExp) ? false : true;
29}
30
31void BmnNdetReconstructor::SetRecoCutsFile(const std::string& cuts)
32{
33 std::string fullPath = std::string(getenv("VMCWORKDIR")) + "/parameters/ndet/" + cuts;
34 std::ifstream test(fullPath);
35 if (!test.is_open()) {
36 LOG(warn) << Form("BmnNdetReconstructor: cuts file not found: %s. Using default (no cuts).", fullPath.c_str());
37 fCuts = nullptr;
38 return;
39 }
40
41 fCuts = new BmnNdetCuts(BmnNdetCuts::Load(fullPath, fIsExp));
42 LOG(info) << Form("BmnNdetReconstructor: cuts loaded from %s (isExp=%d)", fullPath.c_str(), fIsExp);
43}
44
46{
47 if (fBmnNdetEvent)
48 delete fBmnNdetEvent;
49 if (fGeoHandler)
50 delete fGeoHandler;
51 if (fCuts)
52 delete fCuts;
53}
54
56{
57 fworkTime = 0.;
58 fpFairRootMgr = FairRootManager::Instance();
59
60 if (fIsExp) {
61 if (auto* a = (TClonesArray*)fpFairRootMgr->GetObject("HgndDigi"))
62 fDigits.push_back(a);
63 if (auto* a = (TClonesArray*)fpFairRootMgr->GetObject("NdetDigi"))
64 fDigits.push_back(a);
65
66 if (fDigits.empty()) {
67 LOG(error) << "BmnNdetReconstructor::Init(): no experimental digi branches (HgndDigi/NdetDigi) found! "
68 "Task will be deactivated";
69 SetActive(kFALSE);
70 return kERROR;
71 }
72 LOG(info) << Form("BmnNdetReconstructor::Init(): using %zu experimental branch(es)", fDigits.size());
73 } else {
74 if (auto* a = (TClonesArray*)fpFairRootMgr->GetObject("NdetDigit"))
75 fDigits.push_back(a);
76 else {
77 LOG(error) << "BmnNdetReconstructor::Init(): branch with Digits not found! Task will be deactivated";
78 SetActive(kFALSE);
79 return kERROR;
80 }
81
82 fArrayOfPoints = (TClonesArray*)fpFairRootMgr->GetObject("NdetPoint");
83 if (!fArrayOfPoints) {
84 LOG(error) << "BmnNdetReconstructor::Init(): branch with NdetPoints not found! Task will be deactivated";
85 SetActive(kFALSE);
86 return kERROR;
87 }
88
89 fArrayOfSurfacePoints = (TClonesArray*)fpFairRootMgr->GetObject("NdetSurfacePoint");
91 LOG(error)
92 << "BmnNdetReconstructor::Init(): branch with NdetSurfacePoint not found! Task will be deactivated";
93 SetActive(kFALSE);
94 return kERROR;
95 }
96 fpFairRootMgr->Register("NdetSurfacePoint", "NDET", fArrayOfSurfacePoints, kTRUE);
97 }
98
100 fBmnNdetEvent->reset();
102 fpFairRootMgr->RegisterAny("NdetEvent", fBmnNdetEvent, kTRUE);
103 fpFairRootMgr->Register("NdetEvent", "NDET", fBmnNdetEvent, kFALSE);
104
105 LOG(debug) << "Ndet Reconstructor ready";
106 return kSUCCESS;
107}
108
110{
111 fGeoHandler = new BmnNdetGeo();
112 fGeoHandler->ReadGeometryFromGeoManager(fIsGlobal);
113}
114
116{
117 if (!IsActive())
118 return;
119 TStopwatch sw;
120 sw.Start();
121 fBmnNdetEvent->reset();
122
123 auto digiVector = GetSelectedDigis();
124 for (auto* ThisDigi : digiVector) {
125 auto address = ThisDigi->GetAddress();
126 const auto& posMap = fGeoHandler->GetPositionMap();
127 auto pos = posMap.find(address);
128 if (pos == posMap.end()) {
129 LOG(debug2) << Form("BmnNdetReconstructor : Exec. Digi address %d not found in geometry. Skipping",
130 address);
131 if (FairLogger::GetLogger()->IsLogNeeded(fair::Severity::debug2))
132 ThisDigi->Print();
133 continue;
134 }
135 BmnNdetCell* this_cell = new BmnNdetCell();
136 this_cell->SetAddress(address);
137 this_cell->SetPosition(pos->second.first);
138 this_cell->SetPositionError(pos->second.second);
139 this_cell->SetSignal(ThisDigi->GetSignal());
140 this_cell->SetTime(ThisDigi->GetTime());
141 fBmnNdetEvent->AddCell(this_cell);
142 }
143
144 if (!fIsExp && fDoMatchMC)
146
147 sw.Stop();
148 fworkTime += sw.RealTime();
149}
150
151std::vector<BmnNdetDigit*> BmnNdetReconstructor::GetSelectedDigis()
152{
153 std::vector<BmnNdetDigit*> denoised;
154
155 size_t total = 0;
156 for (auto* arr : fDigits)
157 total += arr->GetEntriesFast();
158 denoised.reserve(total);
159
160 for (auto* arr : fDigits) {
161 for (int i = 0, n = arr->GetEntriesFast(); i < n; ++i) {
162 TObject* obj = arr->At(i);
163 if (!obj)
164 continue;
165
166 if (fIsExp) {
167 if (auto* dexp = dynamic_cast<BmnHgndDigi*>(obj)) {
168 if (!fCuts || fCuts->PassExp(dexp))
169 denoised.push_back(dexp);
170 } else if (auto* dprt = dynamic_cast<BmnNdetDigi*>(obj)) {
171 if (!fCuts || fCuts->PassPrototype(dprt))
172 denoised.push_back(dprt);
173 }
174 } else {
175 auto* d = static_cast<BmnNdetDigit*>(obj);
176 if (!fCuts || fCuts->PassSim(d))
177 denoised.push_back(d);
178 }
179 }
180 }
181
182 LOG(debug4) << Form("BmnNdetReconstructor::GetSelectedDigis: kept %zu / %zu", denoised.size(), total);
183 return denoised;
184}
185
187{
188 // Enable the use of FairLinks in FairRootManager
189 fpFairRootMgr->SetUseFairLinks(kTRUE);
190
191 // Iterate over all cells in the event
192 auto* cells = fBmnNdetEvent->GetModifiableCells();
193 for (int ice = 0, nce = cells->GetEntriesFast(); ice < nce; ++ice) {
194 auto* cell = static_cast<BmnNdetCell*>(cells->At(ice));
195 if (!cell)
196 continue;
197
198 // Iterate over all points
199 for (int iPt = 0, nPt = fArrayOfPoints->GetEntriesFast(); iPt < nPt; ++iPt) {
200 auto* point = static_cast<BmnNdetPoint*>(fArrayOfPoints->At(iPt));
201 if (!point)
202 continue;
203 if (point->GetAddress() != cell->GetAddress())
204 continue; // Skip if point is from another cell
205 cell->AddLink(FairLink(static_cast<int>(BmnNdetCell::LinkType::MCPoint), iPt, point->GetEnergyLoss()));
206 for (auto link : point->GetLinks()) {
207 link.SetWeight(point->GetEnergyLoss());
208 cell->AddLink(link);
209 }
210 LOG(debug) << Form("BmnNdetReconstructor::MatchMCTracks. Matched MC point and MC track. Updated cell with "
211 "address %d. Now it has %d links",
212 cell->GetAddress(), cell->GetNLinks());
213 }
214 }
215
216 // Disable the use of FairLinks in FairRootManager
217 fpFairRootMgr->SetUseFairLinks(kFALSE);
218}
219
220void BmnNdetReconstructor::OnlineWrite(const std::unique_ptr<TTree>& resultTree)
221{
222 if (!IsActive())
223 return;
224
225 resultTree->Branch("NdetEvent", &fBmnNdetEvent);
226 if (!fIsExp)
227 resultTree->Branch("NdetSurfacePoint", &fArrayOfSurfacePoints);
228 resultTree->Fill();
229}
230
232{
233 printf("Work time of BmnNdetReconstructor: %4.2f sec.\n", fworkTime);
234}
int i
Definition P4_F32vec4.h:22
constexpr Bool_t kFALSE
constexpr Bool_t kTRUE
const char Option_t
char * Form(const char *fmt,...)
Data structure for a single HGND digit.
Definition BmnHgndDigi.h:20
void SetAddress(uint32_t address)
Definition BmnNdetCell.h:39
void SetSignal(double signal)
Definition BmnNdetCell.h:40
void SetTime(double time)
Definition BmnNdetCell.h:41
Data class for Bmn Ndet digital signal processing.
Definition BmnNdetDigi.h:24
Class for Bmn Ndet data container in event.
virtual InitStatus Init()
virtual void Exec(Option_t *opt)
std::vector< BmnNdetDigit * > GetSelectedDigis()
void SetRecoCutsFile(const std::string &cuts)
TClonesArray * fArrayOfSurfacePoints
FairRootManager * fpFairRootMgr
virtual void OnlineWrite(const std::unique_ptr< TTree > &resultTree)
Write task resul to tree.
std::vector< TClonesArray * > fDigits
BmnNdetReconstructor(bool isExp, bool isGlobalCoordinates=true)
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 BmnNdetCuts Load(const std::string &file, bool isExp)
Definition BmnNdetCuts.h:47