All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
FilterSimPhotonLiteTime_module.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 // 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).
12 ////////////////////////////////////////////////////////////////////////
13 
14 #include "art/Framework/Core/ModuleMacros.h"
15 #include "art/Framework/Core/SharedFilter.h"
16 #include "art/Framework/Principal/Event.h"
17 #include "art/Framework/Principal/Handle.h"
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 
32 class simfilter::FilterSimPhotonLiteTime : public art::SharedFilter {
33 public:
34  explicit FilterSimPhotonLiteTime(fhicl::ParameterSet const& p, art::ProcessingFrame const&);
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
46  fSimPhotonsLiteCollectionLabel; //!< Label for the sim::SimPhotonsLite data product
47  std::vector<std::pair<int, int>> const
48  fTimeWindows; //!< Time windows used for filtering. Units are the same as in the sim::SimPhotonsLite
49  int const fMinTotalPhotons; //!< Minimum number of photons inside a window to pass the filter
50  bool const fDebug; //!< Set to true to print (a lot of) debug information.
51  std::size_t const fN; //!< Number of time winows.
52  bool const fUseReflectedPhotons; //!< Whether to include reflected photons in the filter.
53  std::string const fReflectedLabel; //!< Label for the reflected photons -- "Reflected" by default.
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 {
69  CheckTimeWindows();
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 
81 void
83 {
84 
85  if (fDebug)
86  std::cout << "\tFilterSimPhotonLiteTime: TimeWindows size is " << fTimeWindows.size()
87  << std::endl;
88 
89  for (auto const& tw : fTimeWindows) {
90  if (fDebug)
91  std::cout << "\t\tTimeWindow "
92  << "[" << tw.first << "," << tw.second << "]" << std::endl;
93 
94  if (tw.first > tw.second)
95  throw cet::exception("FilterSimPhotonLiteTime::CheckTimeWindows")
96  << "Bad time window initialization: tw.first>tw.second. Reverse the order!" << std::endl;
97  }
98 }
99 
100 bool
101 simfilter::FilterSimPhotonLiteTime::filter(art::Event& e, art::ProcessingFrame const&)
102 {
103  auto const& simPhotonsLiteCollection =
104  *e.getValidHandle<std::vector<sim::SimPhotonsLite>>(fSimPhotonsLiteCollectionLabel);
105 
106  std::vector<int> sumNPhotonArray(fN, 0);
107 
108  const std::vector<sim::SimPhotonsLite>& simPhotonsLiteCollectionReflected =
109  fUseReflectedPhotons ? *e.getValidHandle<std::vector<sim::SimPhotonsLite>>(
110  {fSimPhotonsLiteCollectionLabel, fReflectedLabel}) :
111  std::vector<sim::SimPhotonsLite>();
112 
113  size_t n_sim_photons = simPhotonsLiteCollection.size() + simPhotonsLiteCollectionReflected.size();
114 
115  if (fDebug) {
116  std::cout << "New event to filter with total # sim photons: " << n_sim_photons << std::endl;
117  }
118 
119  for (size_t i_pc = 0; i_pc < n_sim_photons; i_pc++) {
120  const sim::SimPhotonsLite& simphotonslite =
121  (i_pc < simPhotonsLiteCollection.size()) ?
122  simPhotonsLiteCollection[i_pc] :
123  simPhotonsLiteCollectionReflected[i_pc - simPhotonsLiteCollection.size()];
124 
125  if (fDebug)
126  std::cout << "\tFilterSimPhotonLiteTime: Processing simphotonslite channel "
127  << simphotonslite.OpChannel << std::endl;
128 
129  for (auto const& photon_pair : simphotonslite.DetectedPhotons) {
130  for (size_t i_tw = 0; i_tw < fN; i_tw++) {
131  auto const& tw(fTimeWindows[i_tw]);
132  if (photon_pair.first >= tw.first && photon_pair.first <= tw.second) {
133 
134  if (fDebug) {
135  std::string photon_string =
136  (i_pc < simPhotonsLiteCollection.size()) ? "Photon" : "Reflected Photon";
137  std::cout << "\t\t" << photon_string << " with number " << photon_pair.second
138  << " at time " << photon_pair.first << " detected." << std::endl;
139  }
140 
141  sumNPhotonArray[i_tw] += photon_pair.second;
142 
143  if (fDebug)
144  std::cout << "\t\tTotal number of photons in this window (" << i_tw << ") is now "
145  << sumNPhotonArray[i_tw] << std::endl;
146 
147  if (sumNPhotonArray[i_tw] >= fMinTotalPhotons) return true;
148  }
149  }
150  }
151  }
152 
153  if (fDebug) {
154  std::cout << "\tFilterSimPhotonLiteTime: Final total numbers are below min of "
155  << fMinTotalPhotons << ":" << std::endl;
156  for (size_t i_tw = 0; i_tw < fN; ++i_tw) {
157  std::cout << "\t\tTimeWindow "
158  << "[" << fTimeWindows[i_tw].first << "," << fTimeWindows[i_tw].second
159  << "]: " << sumNPhotonArray[i_tw] << std::endl;
160  }
161  }
162 
163  return false;
164 }
165 
166 DEFINE_ART_MODULE(simfilter::FilterSimPhotonLiteTime)
FilterSimPhotonLiteTime & operator=(FilterSimPhotonLiteTime const &)=delete
pdgs p
Definition: selectors.fcl:22
bool filter(art::Event &e, art::ProcessingFrame const &) override
int const fMinTotalPhotons
Minimum number of photons inside a window to pass the filter.
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 -&gt; photons.
Definition: SimPhotons.h:117
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:265
Simulation objects for optical detectors.
std::string const fReflectedLabel
Label for the reflected photons – &quot;Reflected&quot; by default.
int OpChannel
Optical detector channel associated to this data.
Definition: SimPhotons.h:114
Compact representation of photons on a channel.
Definition: SimPhotons.h:103
std::size_t const fN
Number of time winows.
FilterSimPhotonLiteTime(fhicl::ParameterSet const &p, art::ProcessingFrame const &)
do i e
std::string const fSimPhotonsLiteCollectionLabel
Label for the sim::SimPhotonsLite data product.
BEGIN_PROLOG could also be cout
std::vector< std::pair< int, int > > const fTimeWindows
Time windows used for filtering. Units are the same as in the sim::SimPhotonsLite.