LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
FilterSimPhotonLiteTime_module.cc
Go to the documentation of this file.
1 // Class: FilterSimPhotonLiteTime
3 // Module Type: filter
4 // File: FilterSimPhotonLiteTime_module.cc
5 //
6 // Author: Gray Putnam -- ported from FilterSimPhotonTime
7 //
8 // Module for filtering events based on the number of true photons
9 // hitting optical detectors inside a time window. Uses the
10 // sim::SimPhotonsLite data product as input (see FilterSimPhotonTime
11 // for filtering using the sim::SimPhotons data product).
13 
18 #include "fhiclcpp/ParameterSet.h"
19 
20 #include <iostream>
21 #include <memory>
22 #include <string>
23 #include <utility>
24 #include <vector>
25 
27 
28 namespace simfilter {
29  class FilterSimPhotonLiteTime;
30 }
31 
33 public:
35 
36  // Plugins should not be copied or assigned.
41 
42 private:
43  bool filter(art::Event& e, art::ProcessingFrame const&) override;
44 
45  std::string const
47  std::vector<std::pair<int, int>> const
49  int const fMinTotalPhotons;
50  bool const fDebug;
51  std::size_t const fN;
52  bool const fUseReflectedPhotons;
53  std::string const fReflectedLabel;
54 
55  void CheckTimeWindows() const;
56 };
57 
59  art::ProcessingFrame const&)
60  : SharedFilter{p}
61  , fSimPhotonsLiteCollectionLabel(p.get<std::string>("SimPhotonsLiteCollectionLabel"))
62  , fTimeWindows(p.get<std::vector<std::pair<int, int>>>("TimeWindows"))
63  , fMinTotalPhotons(p.get<int>("MinTotalPhotons"))
64  , fDebug(p.get<bool>("Debug", false))
65  , fN(fTimeWindows.size())
66  , fUseReflectedPhotons(p.get<bool>("UseReflectedPhotons", false))
67  , fReflectedLabel(p.get<std::string>("ReflectedLabel", "Reflected"))
68 {
70 
71  // For printing out debug messages, we want to serialize the
72  // event-level calls so that the messages are not garbled.
73  // Otherwise, this module works well for asynchronous event-level
74  // calls.
75  if (fDebug) { serialize(); }
76  else {
77  async<art::InEvent>();
78  }
79 }
80 
82 {
83 
84  if (fDebug)
85  std::cout << "\tFilterSimPhotonLiteTime: TimeWindows size is " << fTimeWindows.size()
86  << std::endl;
87 
88  for (auto const& tw : fTimeWindows) {
89  if (fDebug)
90  std::cout << "\t\tTimeWindow "
91  << "[" << tw.first << "," << tw.second << "]" << std::endl;
92 
93  if (tw.first > tw.second)
94  throw cet::exception("FilterSimPhotonLiteTime::CheckTimeWindows")
95  << "Bad time window initialization: tw.first>tw.second. Reverse the order!" << std::endl;
96  }
97 }
98 
100 {
101  auto const& simPhotonsLiteCollection =
102  *e.getValidHandle<std::vector<sim::SimPhotonsLite>>(fSimPhotonsLiteCollectionLabel);
103 
104  std::vector<int> sumNPhotonArray(fN, 0);
105 
106  const std::vector<sim::SimPhotonsLite>& simPhotonsLiteCollectionReflected =
107  fUseReflectedPhotons ? *e.getValidHandle<std::vector<sim::SimPhotonsLite>>(
109  std::vector<sim::SimPhotonsLite>();
110 
111  size_t n_sim_photons = simPhotonsLiteCollection.size() + simPhotonsLiteCollectionReflected.size();
112 
113  if (fDebug) {
114  std::cout << "New event to filter with total # sim photons: " << n_sim_photons << std::endl;
115  }
116 
117  for (size_t i_pc = 0; i_pc < n_sim_photons; i_pc++) {
118  const sim::SimPhotonsLite& simphotonslite =
119  (i_pc < simPhotonsLiteCollection.size()) ?
120  simPhotonsLiteCollection[i_pc] :
121  simPhotonsLiteCollectionReflected[i_pc - simPhotonsLiteCollection.size()];
122 
123  if (fDebug)
124  std::cout << "\tFilterSimPhotonLiteTime: Processing simphotonslite channel "
125  << simphotonslite.OpChannel << std::endl;
126 
127  for (auto const& photon_pair : simphotonslite.DetectedPhotons) {
128  for (size_t i_tw = 0; i_tw < fN; i_tw++) {
129  auto const& tw(fTimeWindows[i_tw]);
130  if (photon_pair.first >= tw.first && photon_pair.first <= tw.second) {
131 
132  if (fDebug) {
133  std::string photon_string =
134  (i_pc < simPhotonsLiteCollection.size()) ? "Photon" : "Reflected Photon";
135  std::cout << "\t\t" << photon_string << " with number " << photon_pair.second
136  << " at time " << photon_pair.first << " detected." << std::endl;
137  }
138 
139  sumNPhotonArray[i_tw] += photon_pair.second;
140 
141  if (fDebug)
142  std::cout << "\t\tTotal number of photons in this window (" << i_tw << ") is now "
143  << sumNPhotonArray[i_tw] << std::endl;
144 
145  if (sumNPhotonArray[i_tw] >= fMinTotalPhotons) return true;
146  }
147  }
148  }
149  }
150 
151  if (fDebug) {
152  std::cout << "\tFilterSimPhotonLiteTime: Final total numbers are below min of "
153  << fMinTotalPhotons << ":" << std::endl;
154  for (size_t i_tw = 0; i_tw < fN; ++i_tw) {
155  std::cout << "\t\tTimeWindow "
156  << "[" << fTimeWindows[i_tw].first << "," << fTimeWindows[i_tw].second
157  << "]: " << sumNPhotonArray[i_tw] << std::endl;
158  }
159  }
160 
161  return false;
162 }
163 
FilterSimPhotonLiteTime & operator=(FilterSimPhotonLiteTime const &)=delete
bool filter(art::Event &e, art::ProcessingFrame const &) override
int const fMinTotalPhotons
Minimum number of photons inside a window to pass the filter.
SharedFilter(fhicl::ParameterSet const &pset)
Definition: SharedFilter.cc:6
bool const fUseReflectedPhotons
Whether to include reflected photons in the filter.
std::map< int, int > DetectedPhotons
Number of photons detected at each given time: time tick -> photons.
Definition: SimPhotons.h:109
bool const fDebug
Set to true to print (a lot of) debug information.
auto vector(Vector const &v)
Returns a manipulator which will print the specified array.
Definition: DumpUtils.h:289
Simulation objects for optical detectors.
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:65
std::string const fReflectedLabel
Label for the reflected photons – "Reflected" by default.
int OpChannel
Optical detector channel associated to this data.
Definition: SimPhotons.h:106
Compact representation of photons on a channel.
Definition: SimPhotons.h:98
std::size_t const fN
Number of time winows.
ValidHandle< PROD > getValidHandle(InputTag const &tag) const
FilterSimPhotonLiteTime(fhicl::ParameterSet const &p, art::ProcessingFrame const &)
void serialize(T const &...)
std::string const fSimPhotonsLiteCollectionLabel
Label for the sim::SimPhotonsLite data product.
Framework includes.
Float_t e
Definition: plot.C:35
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
std::vector< std::pair< int, int > > const fTimeWindows
Time windows used for filtering. Units are the same as in the sim::SimPhotonsLite.