BmnRoot
Loading...
Searching...
No Matches
BmnNdetReconstructor.cxx
Go to the documentation of this file.
1
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{
22 fIsExp = isExp;
23 fIsExp ? fDoMatchMC = false : fDoMatchMC = true;
24 fIsGlobal = isGlobalCoordinates;
25 LOG(detail) << Form("BmnNdetReconstructor: isExp %i; isGlobal %i", fIsExp, fIsGlobal);
26}
27
28void BmnNdetReconstructor::SetRecoCutsFile(TString reco_cuts_file)
29{
30 fRecoCutsFile = reco_cuts_file;
31 ParseCuts();
32 LOG(detail) << Form("BmnNdetReconstructor: Reco cuts %s", fRecoCutsFile.Data());
33}
34
36{
37 if (fBmnNdetEvent)
38 delete fBmnNdetEvent;
39 if (fGeoHandler)
40 delete fGeoHandler;
41}
42
44{
45 fworkTime = 0.;
46 fpFairRootMgr = FairRootManager::Instance();
47
48 if (fIsExp) {
49 fArrayOfDigits = (TClonesArray*)fpFairRootMgr->GetObject("NdetDigi");
50 if (!fArrayOfDigits) {
51 LOG(error) << "BmnNdetReconstructor::Init(): branch with Digits not found! Task will be deactivated";
52 SetActive(kFALSE);
53 return kERROR;
54 }
55 } else {
56 fArrayOfDigits = (TClonesArray*)fpFairRootMgr->GetObject("NdetDigit");
57 if (!fArrayOfDigits) {
58 LOG(error) << "BmnNdetReconstructor::Init(): branch with Digits not found! Task will be deactivated";
59 SetActive(kFALSE);
60 return kERROR;
61 }
62
63 fArrayOfPoints = (TClonesArray*)fpFairRootMgr->GetObject("NdetPoint");
64 if (!fArrayOfPoints) {
65 LOG(error) << "BmnNdetReconstructor::Init(): branch with NdetPoints not found! Task will be deactivated";
66 SetActive(kFALSE);
67 return kERROR;
68 }
69
70 fArrayOfSurfacePoints = (TClonesArray*)fpFairRootMgr->GetObject("NdetSurfacePoint");
71 if (!fArrayOfSurfacePoints) {
72 LOG(error)
73 << "BmnNdetReconstructor::Init(): branch with NdetSurfacePoint not found! Task will be deactivated";
74 SetActive(kFALSE);
75 return kERROR;
76 }
77 fpFairRootMgr->Register("NdetSurfacePoint", "NDET", fArrayOfSurfacePoints, kTRUE);
78 }
79
80 fBmnNdetEvent = new BmnNdetEvent();
81 fBmnNdetEvent->reset();
83 fpFairRootMgr->RegisterAny("NdetEvent", fBmnNdetEvent, kTRUE);
84 fpFairRootMgr->Register("NdetEvent", "NDET", fBmnNdetEvent, kFALSE);
85
86 LOG(debug) << "Ndet Reconstructor ready";
87 return kSUCCESS;
88}
89
91{
92 namespace po = boost::program_options;
93
94 TString dir = getenv("VMCWORKDIR");
95 TString path = dir + "/parameters/ndet/";
96
97 float version;
98 std::string comment;
99 std::string cutExp;
100 std::string cutSim;
101
102 // Setup options.
103 po::options_description desc("Options");
104 desc.add_options()("VERSION.id", po::value<float>(&version),
105 "version identificator")("COMMENT.str", po::value<std::string>(&comment), "comment")(
106 "EXPERIMENT.cut", po::value<std::string>(&cutExp),
107 "cut for Experimental data")("SIMULATION.cut", po::value<std::string>(&cutSim), "cut for Simulated data");
108
109 // Load config file.
110 po::variables_map vm;
111 std::ifstream config_file((path + fRecoCutsFile).Data(), std::ifstream::in);
112 if (!config_file.is_open()) {
113 LOG(error) << Form("BmnNdetReconstructor : Loading Cuts from file: %s - file open error!",
114 fRecoCutsFile.Data());
115 return;
116 }
117 LOG(debug) << Form("BmnNdetReconstructor : Loading Cuts from file: %s", fRecoCutsFile.Data());
118 po::store(po::parse_config_file(config_file, desc), vm);
119 config_file.close();
120 po::notify(vm);
121
122 fSelectionString = (fIsExp) ? cutExp : cutSim;
123}
124
126{
127 fGeoHandler = new BmnNdetGeo();
128 fGeoHandler->ReadGeometryFromGeoManager(fIsGlobal);
129}
130
131void BmnNdetReconstructor::Exec(Option_t* opt)
132{
133
134 if (!IsActive())
135 return;
136 fpFairRootMgr->SetUseFairLinks(kTRUE);
137 TStopwatch sw;
138 sw.Start();
139 fBmnNdetEvent->reset();
140 auto digiVector = GetSelectedDigiVector(fSelectionString);
141 for (auto it : digiVector) {
142 BmnNdetDigit* ThisDigi = (BmnNdetDigit*)it;
143 auto address = ThisDigi->GetAddress();
144 const auto& posMap = fGeoHandler->GetPositionMap();
145 auto pos = posMap.find(address);
146 if (pos == posMap.end()) {
147 LOG(debug2) << Form("BmnNdetReconstructor : Exec. Digi address %d not found in geometry. Skipping",
148 address);
149 if (FairLogger::GetLogger()->IsLogNeeded(fair::Severity::debug2))
150 ThisDigi->Print();
151 continue;
152 }
153 BmnNdetCell* this_cell = new BmnNdetCell();
154 this_cell->SetAddress(address);
155 this_cell->SetPosition(pos->second.first);
156 this_cell->SetPositionError(pos->second.second);
157 this_cell->SetSignal(ThisDigi->GetSignal());
158 this_cell->SetTime(ThisDigi->GetTime());
159 fBmnNdetEvent->AddCell(this_cell);
160 }
161
162 if (!fIsExp && fDoMatchMC)
163 MatchMCTracks();
164
165 sw.Stop();
166 fworkTime += sw.RealTime();
167 fpFairRootMgr->SetUseFairLinks(kFALSE);
168}
169
170std::vector<TObject*> BmnNdetReconstructor::GetSelectedDigiVector(TString formulaString)
171{
172 TTree* temp_tree = new TTree("temp_tree", "temp_tree");
173 TString temp_br_name = (fIsExp) ? "NdetDigi" : "NdetDigit";
174 fSelectedDigiVector.clear();
175
176 if (fIsExp) {
177 BmnNdetDigi* ptr = nullptr;
178 temp_tree->Branch(temp_br_name.Data(), &ptr);
179 for (int i = 0; i < fArrayOfDigits->GetEntriesFast(); i++) {
180 ptr = (BmnNdetDigi*)fArrayOfDigits->At(i);
181 temp_tree->Fill();
182 }
183 } else {
184 BmnNdetDigit* ptr = nullptr;
185 temp_tree->Branch(temp_br_name.Data(), &ptr);
186 for (int i = 0; i < fArrayOfDigits->GetEntriesFast(); i++) {
187 ptr = (BmnNdetDigit*)fArrayOfDigits->At(i);
188 temp_tree->Fill();
189 }
190 }
191
192 TTreeFormula* formula = new TTreeFormula("TTreeformula", formulaString.Data(), temp_tree);
193 if (!formula || formula->GetNdim() == 0)
194 LOG(error) << Form("BmnNdetReconstructor::GetSelectedDigiVector. Bad cuts. Check formula %s from file %s",
195 formulaString.Data(), fRecoCutsFile.Data());
196 // next line is needed. Reason unclear. without it will lose some digis.
197 TObject* ThisDigi = nullptr;
198 temp_tree->SetBranchAddress(temp_br_name.Data(), &ThisDigi);
199 for (int i = 0; i < temp_tree->GetEntries(); i++) {
200 temp_tree->GetEntry(i);
201 formula->GetNdata();
202 if (formula->EvalInstance() > 0) // passed selection
203 fSelectedDigiVector.push_back(fArrayOfDigits->At(i));
204 }
205 formula->Delete();
206 temp_tree->Delete();
207 return fSelectedDigiVector;
208}
209
210void BmnNdetReconstructor::MatchMCTracks()
211{
212 // Enable the use of FairLinks in FairRootManager
213 fpFairRootMgr->SetUseFairLinks(kTRUE);
214
215 // Iterate over all cells in the event
216 auto* cells = fBmnNdetEvent->GetModifiableCells();
217 for (int ice = 0, nce = cells->GetEntriesFast(); ice < nce; ++ice) {
218 auto* cell = static_cast<BmnNdetCell*>(cells->At(ice));
219 if (!cell)
220 continue;
221
222 // Iterate over all points
223 for (int iPt = 0, nPt = fArrayOfPoints->GetEntriesFast(); iPt < nPt; ++iPt) {
224 auto* point = static_cast<BmnNdetPoint*>(fArrayOfPoints->At(iPt));
225 if (!point)
226 continue;
227 if (point->GetAddress() != cell->GetAddress())
228 continue; // Skip if point is from another cell
229 cell->AddLink(FairLink(static_cast<int>(BmnNdetCell::LinkType::MCPoint), iPt, point->GetEnergyLoss()));
230 for (auto link : point->GetLinks()) {
231 link.SetWeight(point->GetEnergyLoss());
232 cell->AddLink(link);
233 }
234 LOG(debug) << Form("BmnNdetReconstructor::MatchMCTracks. Matched MC point and MC track. Updated cell with "
235 "address %d. Now it has %d links",
236 cell->GetAddress(), cell->GetNLinks());
237 }
238 }
239
240 // Disable the use of FairLinks in FairRootManager
241 fpFairRootMgr->SetUseFairLinks(kFALSE);
242}
243
244void BmnNdetReconstructor::OnlineWrite(const std::unique_ptr<TTree>& resultTree)
245{
246 if (!IsActive())
247 return;
248
249 resultTree->Branch("NdetEvent", &fBmnNdetEvent);
250 if (!fIsExp)
251 resultTree->Branch("NdetSurfacePoint", &fArrayOfSurfacePoints);
252 resultTree->Fill();
253}
254
256{
257 printf("Work time of BmnNdetReconstructor: %4.2f sec.\n", fworkTime);
258}
int i
Definition P4_F32vec4.h:22
double GetTime() const
double GetSignal() const
uint32_t GetAddress() const
const auto & GetPositionMap() const
BmnStatus ReadGeometryFromGeoManager(bool getGlobalPosition=true)
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
virtual void Print(const Option_t *opt="")
Class for Bmn Ndet data container in event.
TClonesArray * GetModifiableCells()
Modifiable interface to the cells set.
void reset()
Zero all fields.
void AddCell(BmnNdetCell *cell)
Add a cell to the event.
virtual InitStatus Init()
BmnNdetReconstructor(bool isExp, bool isGlobalCoordinates)
virtual void Exec(Option_t *opt)
void SetRecoCutsFile(TString reco_cuts_file)
virtual void OnlineWrite(const std::unique_ptr< TTree > &resultTree)
Write task resul to tree.