BmnRoot
Loading...
Searching...
No Matches
BmnFHCalReconstructor.cxx
Go to the documentation of this file.
1
9
10#include "BmnDetectorList.h"
11#include "BmnFHCalGeo.h"
12#include "BmnFHCalPoint.h"
13#include "TTreeFormula.h"
14
15BmnFHCalReconstructor::BmnFHCalReconstructor(bool isExp, bool isGlobalCoordinates)
16{
17 fIsExp = isExp;
18 fDoMatchMC = (fIsExp) ? false : true;
19 fIsGlobal = isGlobalCoordinates;
20 LOG(detail) << Form("BmnFHCalReconstructor: isExp %i; isGlobal %i", fIsExp, fIsGlobal);
21}
22
23void BmnFHCalReconstructor::SetRecoCutsFile(TString reco_cuts_file)
24{
25 fRecoCutsFile = reco_cuts_file;
26 ParseCuts();
27 LOG(detail) << Form("BmnFHCalReconstructor: Reco cuts %s", fRecoCutsFile.Data());
28}
29
31{
32 if (fBmnFHCalEvent)
33 delete fBmnFHCalEvent;
34}
35
37{
38 fworkTime = 0.;
39 fpFairRootMgr = FairRootManager::Instance();
40
41 if (fIsExp) {
42 fArrayOfDigits = (TClonesArray*)fpFairRootMgr->GetObject("FHCalDigi");
43 if (!fArrayOfDigits) {
44 LOG(error) << "BmnFHCalReconstructor::Init(): branch with Digits not found! Task will be deactivated";
45 SetActive(kFALSE);
46 return kERROR;
47 }
48 } else {
49 fArrayOfDigits = (TClonesArray*)fpFairRootMgr->GetObject("FHCalDigit");
50 if (!fArrayOfDigits) {
51 LOG(error) << "BmnFHCalReconstructor::Init(): branch with Digits not found! Task will be deactivated";
52 SetActive(kFALSE);
53 return kERROR;
54 }
55
56 fArrayOfPoints = (TClonesArray*)fpFairRootMgr->GetObject("FHCalPoint");
57 if (!fArrayOfPoints) {
58 LOG(error) << "BmnFHCalReconstructor::Init(): branch with FHCalPoints not found! No MC match will be done";
59 fDoMatchMC = false;
60 }
61 }
62
63 fBmnFHCalEvent = new BmnFHCalEvent();
64 fBmnFHCalEvent->reset();
66 fpFairRootMgr->RegisterAny("FHCalEvent", fBmnFHCalEvent, kTRUE);
67 fpFairRootMgr->Register("FHCalEvent", "FHCal", fBmnFHCalEvent, kFALSE);
68
69 LOG(debug) << "FHCal Reconstructor ready";
70 return kSUCCESS;
71}
72
74{
75 namespace po = boost::program_options;
76
77 TString dir = getenv("VMCWORKDIR");
78 TString path = dir + "/parameters/fhcal/";
79
80 float version;
81 std::string comment;
82 std::string cutExp;
83 std::string cutSim;
84
85 // Setup options.
86 po::options_description desc("Options");
87 desc.add_options()("VERSION.id", po::value<float>(&version),
88 "version identificator")("COMMENT.str", po::value<std::string>(&comment), "comment")(
89 "EXPERIMENT.cut", po::value<std::string>(&cutExp),
90 "cut for Experimental data")("SIMULATION.cut", po::value<std::string>(&cutSim), "cut for Simulated data");
91
92 // Load config file.
93 po::variables_map vm;
94 std::ifstream config_file((path + fRecoCutsFile).Data(), std::ifstream::in);
95 if (!config_file.is_open()) {
96 LOG(error) << Form("BmnFHCalReconstructor: Loading Cuts from file: %s - file open error!",
97 fRecoCutsFile.Data());
98 return;
99 }
100 LOG(debug) << Form("BmnFHCalReconstructor: Loading Cuts from file: %s", fRecoCutsFile.Data());
101 po::store(po::parse_config_file(config_file, desc), vm);
102 config_file.close();
103 po::notify(vm);
104
105 if (fIsExp)
106 fSelectionString = cutExp;
107 else
108 fSelectionString = cutSim;
109}
110
112{
113 BmnFHCalGeo* GeoHandler = new BmnFHCalGeo();
114 GeoHandler->ReadGeometryFromGeoManager(fIsGlobal);
115 if (GeoHandler->GetMaxModuleId() > 65)
116 LOG(error) << Form("BmnFHCalReconstructor::Check geometry file. Trying to set number of FHCal modules %d",
117 GeoHandler->GetMaxModuleId());
118 fBmnFHCalEvent->SetTotalModules(GeoHandler->GetMaxModuleId());
119 const auto& positionMap = GeoHandler->GetPositionMap();
120 for (auto it = positionMap.begin(); it != positionMap.end(); ++it) {
121 auto unique_address = it->first;
122 auto position = it->second.first;
123 if (BmnFHCalAddress::GetSectionId(unique_address) != 1)
124 continue;
125 auto mod_id = BmnFHCalAddress::GetModuleId(unique_address);
126 auto this_mod = fBmnFHCalEvent->GetModule(mod_id);
127 this_mod->SetModuleId(mod_id);
128 this_mod->SetPosition(position);
129 if (BmnFHCalAddress::GetModuleType(unique_address) == 1)
130 this_mod->SetNsections(7);
131 if (BmnFHCalAddress::GetModuleType(unique_address) == 2)
132 this_mod->SetNsections(10);
133
134 auto err = it->second.second;
135 this_mod->SetDxyz(err.X(), err.Y(), err.Z() * this_mod->GetNsections());
136 }
137 delete GeoHandler;
138}
139
141{
142 if (!IsActive())
143 return;
144 TStopwatch sw;
145 sw.Start();
146
147 fBmnFHCalEvent->ResetEnergies();
148 auto digiVector = GetSelectedDigiVector(fSelectionString);
149
150 for (auto it : digiVector) {
151 BmnFHCalDigit* ThisDigi = (BmnFHCalDigit*)it;
152 auto mod_id = ThisDigi->GetModuleId();
153 auto sec_id = ThisDigi->GetSectionId();
154 if (mod_id <= 0 || (int)mod_id > fBmnFHCalEvent->GetTotalModules() || sec_id <= 0
155 || (int)sec_id > fBmnFHCalEvent->GetModule(mod_id)->GetNsections())
156 {
157 if (fVerbose)
158 LOG(warning) << Form("FHCal digi ignored. Mod %d Sec %d", mod_id, sec_id);
159 continue;
160 }
161 fBmnFHCalEvent->GetModule(mod_id)->SetSectionEnergy(sec_id, ThisDigi->GetSignal());
162 }
163
164 if (!fIsExp && fDoMatchMC)
165 MatchMCTracks();
166
167 fBmnFHCalEvent->SummarizeEvent();
168 sw.Stop();
169 fworkTime += sw.RealTime();
170}
171
172std::vector<TObject*> BmnFHCalReconstructor::GetSelectedDigiVector(TString formulaString)
173{
174 TTree* temp_tree = new TTree("temp_tree", "temp_tree");
175 TString temp_br_name = (fIsExp) ? "FHCalDigi" : "FHCalDigit";
176 fSelectedDigiVector.clear();
177
178 if (fIsExp) {
179 BmnFHCalDigi* ptr = nullptr;
180 temp_tree->Branch(temp_br_name.Data(), &ptr);
181 for (int i = 0; i < fArrayOfDigits->GetEntriesFast(); i++) {
182 ptr = (BmnFHCalDigi*)fArrayOfDigits->At(i);
183 temp_tree->Fill();
184 }
185 } else {
186 BmnFHCalDigit* ptr = nullptr;
187 temp_tree->Branch(temp_br_name.Data(), &ptr);
188 for (int i = 0; i < fArrayOfDigits->GetEntriesFast(); i++) {
189 ptr = (BmnFHCalDigit*)fArrayOfDigits->At(i);
190 temp_tree->Fill();
191 }
192 }
193
194 TTreeFormula* formula = new TTreeFormula("TTreeformula", formulaString.Data(), temp_tree);
195 if (!formula || formula->GetNdim() == 0)
196 LOG(error) << Form("BmnFHCalReconstructor::GetSelectedDigiVector. Bad cuts. Check formula %s from file %s",
197 formulaString.Data(), fRecoCutsFile.Data());
198 // next line is needed. Reason unclear. without it will lose some digis.
199 TObject* ThisDigi = nullptr;
200 temp_tree->SetBranchAddress(temp_br_name.Data(), &ThisDigi);
201 for (int i = 0; i < temp_tree->GetEntries(); i++) {
202 temp_tree->GetEntry(i);
203 formula->GetNdata();
204 if (formula->EvalInstance() > 0) // passed selection
205 fSelectedDigiVector.push_back(fArrayOfDigits->At(i));
206 }
207 formula->Delete();
208 temp_tree->Delete();
209 return fSelectedDigiVector;
210}
211
212void BmnFHCalReconstructor::MatchMCTracks()
213{
214 // Enable the use of FairLinks in FairRootManager
215 fpFairRootMgr->SetUseFairLinks(kTRUE);
216
217 // Iterate over all modules in the event
218 for (uint imod = 1; imod < (uint)fBmnFHCalEvent->GetTotalModules(); ++imod) {
219 auto* mod = fBmnFHCalEvent->GetModule(imod);
220 assert(mod);
221
222 // Iterate over all points
223 for (int iPt = 0; iPt < fArrayOfPoints->GetEntriesFast(); ++iPt) {
224 auto* point = static_cast<BmnFHCalPoint*>(fArrayOfPoints->At(iPt));
225 assert(point);
226 if (point->GetModuleId() != imod)
227 continue; // Skip if point is from another module
228
229 if (!point->GetPointerToLinks()) {
230 LOG(debug4) << "BmnFHCalReconstructor::MatchMCTracks. Link nullptr";
231 continue;
232 }
233
234 mod->AddLinks(*point->GetPointerToLinks());
235 LOG(debug4) << Form("BmnFHCalReconstructor::MatchMCTracks. Updated module with id "
236 "%d. Now it has %d links",
237 mod->GetModuleId(), mod->GetNLinks());
238 }
239 }
240
241 // Disable the use of FairLinks in FairRootManager
242 fpFairRootMgr->SetUseFairLinks(kFALSE);
243}
244
245void BmnFHCalReconstructor::OnlineWrite(const std::unique_ptr<TTree>& resultTree)
246{
247 if (!IsActive())
248 return;
249
250 resultTree->Branch("FHCalEvent", &fBmnFHCalEvent);
251 resultTree->Fill();
252}
253
255{
256 printf("Work time of BmnFHCalReconstructor: %4.2f sec.\n", fworkTime);
257}
int i
Definition P4_F32vec4.h:22
double GetSignal() const
static uint32_t GetModuleId(uint32_t address)
Return Module id from address.
static uint32_t GetModuleType(uint32_t address)
Return Module id from address.
static uint32_t GetSectionId(uint32_t address)
Return Section id from address.
Data class for Bmn FHCal digital signal processing.
uint32_t GetSectionId() const
uint32_t GetModuleId() const
Class for Bmn FHCal data container in event.
BmnFHCalModule * GetModule(uint8_t mod_id)
Module info.
void ResetEnergies()
void SetTotalModules(int TotalModules)
Set TotalModules externally.
void reset()
Zero all fiels.
int GetTotalModules() const
Total number of modules.
int GetMaxModuleId() const
Definition BmnFHCalGeo.h:72
void SetSectionEnergy(int sec_id, float Energy)
void SetModuleId(int ModId)
int GetNsections() const
Sections number.
virtual void Exec(Option_t *opt)
BmnFHCalReconstructor(bool isExp, bool isGlobalCoordinates)
void SetRecoCutsFile(TString reco_cuts_file)
virtual void OnlineWrite(const std::unique_ptr< TTree > &resultTree)
Write task resul to tree.
const auto & GetPositionMap() const
BmnStatus ReadGeometryFromGeoManager(bool getGlobalPosition=true)