/home/docs/checkouts/readthedocs.org/user_builds/ratpac/checkouts/latest/src/fit/include/RAT/FitterInputHandler.hh Source File

Ratpac-two: /home/docs/checkouts/readthedocs.org/user_builds/ratpac/checkouts/latest/src/fit/include/RAT/FitterInputHandler.hh Source File
Ratpac-two
FitterInputHandler.hh
1 
11 #ifndef __RAT_FitterInputHandler__
12 #define __RAT_FitterInputHandler__
13 
14 #include <RAT/DB.hh>
15 #include <RAT/DS/EV.hh>
16 #include <RAT/Log.hh>
17 #include <algorithm>
18 #include <string>
19 
20 #include "RAT/DS/DigitPMT.hh"
21 #include "RAT/DS/FitResult.hh"
22 #include "RAT/DS/PMT.hh"
23 
24 namespace RAT {
25 
27  public:
28  enum class Mode {
29  kPMT = 0,
30  kDigitPMT = 1,
31  kWaveformAnalysis = 2,
32  };
33 
34  Mode mode;
35  std::string wfm_ana_name;
36  uint64_t hit_cleaning_mask;
37  std::string vertex_seed, direction_seed, energy_seed;
38 
43  FitterInputHandler(const std::string& index) { Configure(index); };
44 
49  void Configure(const std::string& index) {
50  DBLinkPtr tbl = DB::Get()->GetLink("FIT_COMMON", index);
51  mode = static_cast<Mode>(tbl->GetI("mode"));
52  if (mode == Mode::kWaveformAnalysis) wfm_ana_name = tbl->GetS("waveform_analyzer");
53  hit_cleaning_mask = static_cast<uint64_t>(tbl->GetI("hit_cleaning_mask"));
54  vertex_seed = tbl->GetS("vertex_seed");
55  direction_seed = tbl->GetS("direction_seed");
56  energy_seed = tbl->GetS("energy_seed");
57  }
58 
64  DS::FitResult* FindFitResult(const std::string& name) const {
65  if (!ev) Log::Die("FitterInputHandler: Trying to acccess event info without registering the event.");
66  std::vector<DS::FitResult*> fit_results = ev->GetFitResults();
67  for (auto& fit_result : fit_results) {
68  if (fit_result->GetFullName() == name) return fit_result;
69  }
70  return nullptr;
71  }
72 
79  TVector3 GetSeedPosition(const std::string& _fitter_name = "") const {
80  std::string fitter_name = _fitter_name;
81  if (_fitter_name.empty()) {
82  fitter_name = vertex_seed;
83  }
84  DS::FitResult* fit_result = FindFitResult(fitter_name);
85  if (!ValidSeedPosition(fitter_name)) {
86  warn << "The Requested Seed Position (from " << fitter_name << ") is not valid." << newline;
87  return TVector3(0, 0, 0);
88  }
89  return fit_result->GetPosition();
90  }
91 
97  bool ValidSeedPosition(const std::string& _fitter_name = "") const {
98  std::string fitter_name = _fitter_name;
99  if (_fitter_name.empty()) {
100  fitter_name = vertex_seed;
101  }
102  DS::FitResult* fit_result = FindFitResult(fitter_name);
103  if (!fit_result) return false;
104  return (fit_result->GetEnablePosition() && fit_result->GetValidPosition());
105  }
106 
113  double GetSeedTime(const std::string& _fitter_name = "") const {
114  std::string fitter_name = _fitter_name;
115  if (_fitter_name.empty()) {
116  fitter_name = vertex_seed;
117  }
118  DS::FitResult* fit_result = FindFitResult(fitter_name);
119  if (!ValidSeedTime(fitter_name)) {
120  warn << "The Requested Seed Time (from " << fitter_name << ") is not valid." << newline;
121  return 0;
122  }
123  return fit_result->GetTime();
124  }
125 
131  bool ValidSeedTime(const std::string& _fitter_name = "") const {
132  std::string fitter_name = _fitter_name;
133  if (_fitter_name.empty()) {
134  fitter_name = vertex_seed;
135  }
136  DS::FitResult* fit_result = FindFitResult(fitter_name);
137  if (!fit_result) return false;
138  return (fit_result->GetEnableTime() && fit_result->GetValidTime());
139  }
140 
146  TVector3 GetSeedDirection(const std::string& _fitter_name = "") const {
147  std::string fitter_name = _fitter_name;
148  if (_fitter_name.empty()) {
149  fitter_name = direction_seed;
150  }
151  DS::FitResult* fit_result = FindFitResult(fitter_name);
152  if (!ValidSeedDirection(fitter_name)) {
153  warn << "The Requested Seed Direction (from " << fitter_name << ") is not valid." << newline;
154  return TVector3(0, 0, 0);
155  }
156  return fit_result->GetDirection();
157  }
158 
164  bool ValidSeedDirection(const std::string& _fitter_name = "") const {
165  std::string fitter_name = _fitter_name;
166  if (_fitter_name.empty()) {
167  fitter_name = direction_seed;
168  }
169  DS::FitResult* fit_result = FindFitResult(fitter_name);
170  if (!fit_result) return false;
171  return (fit_result->GetEnableDirection() && fit_result->GetValidDirection());
172  }
173 
174  double GetSeedEnergy(const std::string& _fitter_name = "") const {
175  std::string fitter_name = _fitter_name;
176  if (_fitter_name.empty()) {
177  fitter_name = energy_seed;
178  }
179  DS::FitResult* fit_result = FindFitResult(fitter_name);
180  if (!ValidSeedEnergy(fitter_name)) {
181  warn << "The Requested Seed Energy (from " << fitter_name << ") is not valid." << newline;
182  return 0;
183  }
184  return fit_result->GetEnergy();
185  }
186 
187  bool ValidSeedEnergy(const std::string& _fitter_name = "") const {
188  std::string fitter_name = _fitter_name;
189  if (_fitter_name.empty()) {
190  fitter_name = energy_seed;
191  }
192  DS::FitResult* fit_result = FindFitResult(fitter_name);
193  if (!fit_result) return false;
194  return (fit_result->GetEnableEnergy() && fit_result->GetValidEnergy());
195  }
196 
201  void RegisterEvent(DS::EV* _ev) {
202  ev = _ev;
203  hitPMTChannels.clear();
204  // NOTE: class implementation assumes GetAllPMTIDs and GetAllDigitPMTIDs returns _sorted_ results.
205  // This is true since ev->digitpmt and ev->pmt are both std::maps.
206  hitPMTChannels = mode == Mode::kPMT ? ev->GetAllPMTIDs() : ev->GetAllDigitPMTIDs();
207  // Only use channels that pass the chosen hit cleaning bit
208  if (hit_cleaning_mask != 0) {
209  std::vector<Int_t> hitPMTChannelsCleaned;
210  for (int id : hitPMTChannels) {
211  DS::DigitPMT* digitpmt = ev->GetOrCreateDigitPMT(id);
212  uint64_t mask = digitpmt->GetHitCleaningMask();
213  // passes if none of the bits specified in the cuts are set in the mask
214  if ((mask & hit_cleaning_mask) == 0) {
215  hitPMTChannelsCleaned.push_back(id);
216  }
217  }
218  hitPMTChannels = hitPMTChannelsCleaned;
219  }
220  }
221 
229  const std::vector<Int_t>& GetAllHitPMTIDs() const {
230  if (!ev) Log::Die("FitterInputHandler: Trying to acccess event info without registering the event.");
231  return hitPMTChannels;
232  }
233 
238  size_t GetNHits() const {
239  if (!ev) Log::Die("FitterInputHandler: Trying to acccess event info without registering the event.");
240  return hitPMTChannels.size();
241  }
242 
251  double GetCharge(Int_t id) const {
252  if (!ev) Log::Die("FitterInputHandler: Trying to acccess event info without registering the event.");
253  if (!std::binary_search(hitPMTChannels.begin(), hitPMTChannels.end(), id))
254  Log::Die("FitterInputHandler: Trying to access a channel with no hit registered!");
255 
256  switch (mode) {
257  case Mode::kPMT:
258  return ev->GetOrCreatePMT(id)->GetCharge();
259  case Mode::kDigitPMT:
260  return ev->GetOrCreateDigitPMT(id)->GetDigitizedCharge();
261  case Mode::kWaveformAnalysis: {
262  std::vector<double> charges = GetCharges(id);
263  double charge = 0;
264  for (size_t ic = 0; ic < charges.size(); ic++) charge += charges[ic];
265  return charge;
266  }
267  default:
268  Log::Die("INVALID TYPE! Should never reach here.");
269  }
270  }
271 
279  std::vector<double> GetCharges(Int_t id) const {
280  if (mode != Mode::kWaveformAnalysis) return std::vector<double>{GetCharge(id)};
281  if (!ev) Log::Die("FitterInputHandler: Trying to acccess event info without registering the event.");
282  if (!std::binary_search(hitPMTChannels.begin(), hitPMTChannels.end(), id))
283  Log::Die("FitterInputHandler: Trying to access a channel with no hit registered!");
284  DS::DigitPMT* digitpmt = ev->GetOrCreateDigitPMT(id);
285  std::vector<std::string> fitterNames = digitpmt->GetFitterNames();
286  if (std::find(fitterNames.begin(), fitterNames.end(), wfm_ana_name) == fitterNames.end()) {
287  info << "FitResult not found for pmt id " << id << " " << wfm_ana_name << newline;
288  }
289  return digitpmt->GetOrCreateWaveformAnalysisResult(wfm_ana_name)->getCharges();
290  }
291 
300  double GetTime(Int_t id) const {
301  if (!ev) Log::Die("FitterInputHandler: Trying to acccess event info without registering the event.");
302  if (!std::binary_search(hitPMTChannels.begin(), hitPMTChannels.end(), id))
303  Log::Die("FitterInputHandler: Trying to access a channel with no hit registered!");
304 
305  switch (mode) {
306  case Mode::kPMT:
307  return ev->GetOrCreatePMT(id)->GetTime();
308  case Mode::kDigitPMT:
309  return ev->GetOrCreateDigitPMT(id)->GetDigitizedTime();
310  case Mode::kWaveformAnalysis: {
311  DS::DigitPMT* digitpmt = ev->GetOrCreateDigitPMT(id);
312  std::vector<std::string> fitterNames = digitpmt->GetFitterNames();
313  if (std::find(fitterNames.begin(), fitterNames.end(), wfm_ana_name) == fitterNames.end()) {
314  info << "FitResult not found for pmt id " << id << " " << wfm_ana_name << newline;
315  }
316  return digitpmt->GetOrCreateWaveformAnalysisResult(wfm_ana_name)->getTime(0);
317  }
318  default:
319  Log::Die("INVALID TYPE! Should never reach here.");
320  }
321  }
322 
330  std::vector<double> GetTimes(Int_t id) const {
331  if (mode != Mode::kWaveformAnalysis) return std::vector<double>{GetTime(id)};
332  if (!ev) Log::Die("FitterInputHandler: Trying to acccess event info without registering the event.");
333  if (!std::binary_search(hitPMTChannels.begin(), hitPMTChannels.end(), id))
334  Log::Die("FitterInputHandler: Trying to access a channel with no hit registered!");
335  DS::DigitPMT* digitpmt = ev->GetOrCreateDigitPMT(id);
336  std::vector<std::string> fitterNames = digitpmt->GetFitterNames();
337  if (std::find(fitterNames.begin(), fitterNames.end(), wfm_ana_name) == fitterNames.end()) {
338  info << "FitResult not found for pmt id " << id << " " << wfm_ana_name << newline;
339  }
340  return digitpmt->GetOrCreateWaveformAnalysisResult(wfm_ana_name)->getTimes();
341  }
342 
352  unsigned int GetNPEs(Int_t id) const {
353  if (!ev) Log::Die("FitterInputHandler: Trying to acccess event info without registering the event.");
354  if (!std::binary_search(hitPMTChannels.begin(), hitPMTChannels.end(), id))
355  Log::Die("FitterInputHandler: Trying to access a channel with no hit registered!");
356 
357  switch (mode) {
358  case Mode::kPMT:
359  return 1; // no nhit information
360  case Mode::kDigitPMT:
361  return ev->GetOrCreateDigitPMT(id)->GetNCrossings(); // approximate
362  case Mode::kWaveformAnalysis: {
363  DS::DigitPMT* digitpmt = ev->GetOrCreateDigitPMT(id);
364  std::vector<std::string> fitterNames = digitpmt->GetFitterNames();
365  if (std::find(fitterNames.begin(), fitterNames.end(), wfm_ana_name) == fitterNames.end()) {
366  info << "FitResult not found for pmt id " << id << " " << wfm_ana_name << newline;
367  }
368  return digitpmt->GetOrCreateWaveformAnalysisResult(wfm_ana_name)->getNPEs();
369  }
370  default:
371  Log::Die("INVALID TYPE! Should never reach here.");
372  }
373  }
374 
375  protected:
376  DS::EV* ev = nullptr;
377  std::vector<Int_t> hitPMTChannels;
378 };
379 
380 } // namespace RAT
381 
382 #endif
DBLinkPtr GetLink(std::string tblname, std::string index="")
Definition: DB.cc:200
static DB * Get()
Definition: DB.hh:135
Definition: DigitPMT.hh:23
virtual HCMask GetHitCleaningMask() const
Definition: DigitPMT.hh:127
virtual WaveformAnalysisResult *const GetOrCreateWaveformAnalysisResult(std::string analyzer_name)
Definition: DigitPMT.hh:80
Definition: EV.hh:33
virtual PMT * GetOrCreatePMT(Int_t id)
Definition: EV.hh:51
virtual std::vector< FitResult * > GetFitResults()
Definition: EV.hh:146
virtual DigitPMT * GetOrCreateDigitPMT(Int_t id)
Definition: EV.hh:66
Definition: FitResult.hh:15
Definition: FitterInputHandler.hh:26
void RegisterEvent(DS::EV *_ev)
Definition: FitterInputHandler.hh:201
bool ValidSeedPosition(const std::string &_fitter_name="") const
Definition: FitterInputHandler.hh:97
double GetTime(Int_t id) const
Get the earliest time for a hit PMT. In the case where a waveoform analyzer created multiple hits on ...
Definition: FitterInputHandler.hh:300
void Configure(const std::string &index)
Definition: FitterInputHandler.hh:49
const std::vector< Int_t > & GetAllHitPMTIDs() const
Get PMTIDs for all pmts in the event. PMT will not be in the list if it never created a hit on DS::PM...
Definition: FitterInputHandler.hh:229
std::vector< double > GetCharges(Int_t id) const
Get the charge of each hit registered on a PMT. To get the integrated charge a PMT,...
Definition: FitterInputHandler.hh:279
TVector3 GetSeedDirection(const std::string &_fitter_name="") const
Definition: FitterInputHandler.hh:146
size_t GetNHits() const
Get number of hit channels in the event.
Definition: FitterInputHandler.hh:238
double GetCharge(Int_t id) const
Get the integrated charge of a PMT. This method returns the integrated charge of all hits on a PMT....
Definition: FitterInputHandler.hh:251
double GetSeedTime(const std::string &_fitter_name="") const
Definition: FitterInputHandler.hh:113
DS::FitResult * FindFitResult(const std::string &name) const
Definition: FitterInputHandler.hh:64
bool ValidSeedDirection(const std::string &_fitter_name="") const
Definition: FitterInputHandler.hh:164
unsigned int GetNPEs(Int_t id) const
Return the (approximate) number of hits registered on a PMT. Behavior is different depending on the m...
Definition: FitterInputHandler.hh:352
bool ValidSeedTime(const std::string &_fitter_name="") const
Definition: FitterInputHandler.hh:131
TVector3 GetSeedPosition(const std::string &_fitter_name="") const
Definition: FitterInputHandler.hh:79
std::vector< double > GetTimes(Int_t id) const
Get the time of each hit registered on a PMT. To get the time of only the first hit,...
Definition: FitterInputHandler.hh:330
FitterInputHandler()
Definition: FitterInputHandler.hh:42
static void Die(std::string message, int return_code=1)
Definition: Log.cc:90
Definition: CCCrossSecMessenger.hh:29