LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
DumpSimPhotons_module.cc
Go to the documentation of this file.
1 
9 // lar libraries
10 #include "larcorealg/CoreUtils/SortByPointers.h" // util::makePointerVector()
12 
13 // framework libraries
19 #include "fhiclcpp/types/Atom.h"
21 
22 namespace sim {
23  class DumpSimPhotons;
24 } // namespace sim
25 
26 namespace {
27  using namespace fhicl;
28 
30  struct Config {
31  using Name = fhicl::Name;
32  using Comment = fhicl::Comment;
33 
34  fhicl::Atom<art::InputTag> InputPhotons{
35  Name("InputPhotons"),
36  Comment("data product with the SimPhotons to be dumped")};
37 
38  fhicl::Atom<std::string> OutputCategory{
39  Name("OutputCategory"),
40  Comment("name of the output stream (managed by the message facility)"),
41  "DumpSimPhotons" /* default value */
42  };
43 
44  }; // struct Config
45 
59  struct OnePhotonSorter {
60 
62  bool operator()(sim::OnePhoton const& a, sim::OnePhoton const& b) const
63  {
64  auto res = cmp(a.Time, b.Time);
65  if (res < 0) return true;
66  if (res > 0) return false;
67 
68  res = cmp(a.MotherTrackID, b.MotherTrackID);
69  if (res < 0) return true;
70  if (res > 0) return false;
71 
72  res = cmp(a.Energy, b.Energy);
73  if (res < 0) return true;
74  if (res > 0) return false;
75 
76  res = cmp(a.InitialPosition.Z(), b.InitialPosition.Z());
77  if (res < 0) return true;
78  if (res > 0) return false;
79 
80  res = cmp(a.InitialPosition.Y(), b.InitialPosition.Y());
81  if (res < 0) return true;
82  if (res > 0) return false;
83 
84  res = cmp(a.InitialPosition.X(), b.InitialPosition.X());
85  if (res < 0) return true;
86  if (res > 0) return false;
87 
88  return false; //
89  } // operator()
90 
92  bool operator()(sim::OnePhoton const* a, sim::OnePhoton const* b) const
93  {
94  return operator()(*a, *b);
95  }
96 
97  // TODO when C++20 is supported, this becomes `return a <=> b;`
98  template <typename T, typename U>
99  static int cmp(T const& a, U const& b)
100  {
101  if (a < b) return -1;
102  if (a == b)
103  return 0;
104  else
105  return +1;
106  }
107 
108  }; // struct OnePhotonSorter
109 
110 } // local namespace
111 
113 public:
114  // type to enable module parameters description by art
116 
118  explicit DumpSimPhotons(Parameters const& config);
119 
120  // Plugins should not be copied or assigned.
121  DumpSimPhotons(DumpSimPhotons const&) = delete;
122  DumpSimPhotons(DumpSimPhotons&&) = delete;
123  DumpSimPhotons& operator=(DumpSimPhotons const&) = delete;
125 
126  // Operates on the event
127  void analyze(art::Event const& event) override;
128 
142  template <typename Stream>
143  void DumpElement(Stream&& out,
144  sim::SimPhotons const& simphotons,
145  std::string indent = "",
146  bool bIndentFirst = true) const;
147 
149  template <typename Stream>
150  void DumpOnePhoton(Stream&& out, sim::OnePhoton const& photon) const;
151 
152 private:
154  std::string fOutputCategory;
155 
156 }; // class sim::DumpSimPhotons
157 
158 //------------------------------------------------------------------------------
159 //--- module implementation
160 //---
161 //------------------------------------------------------------------------------
163  : EDAnalyzer(config)
164  , fInputPhotons(config().InputPhotons())
165  , fOutputCategory(config().OutputCategory())
166 {}
167 
168 //------------------------------------------------------------------------------
169 template <typename Stream>
170 void sim::DumpSimPhotons::DumpOnePhoton(Stream&& out, sim::OnePhoton const& onephoton) const
171 {
172  out << "E=" << onephoton.Energy << " t=" << onephoton.Time << " from "
173  << onephoton.InitialPosition << " cm"
174  << " to " << onephoton.FinalLocalPosition << " cm";
175  if (onephoton.SetInSD) out << " [in SD]"; // in sensitive detector?
176  out << " from track ID=" << onephoton.MotherTrackID;
177 } // sim::DumpSimPhotons::DumpOnePhoton()
178 
179 //------------------------------------------------------------------------------
180 template <typename Stream>
182  sim::SimPhotons const& simphotons,
183  std::string indent /* = "" */,
184  bool bIndentFirst /* = true */
185 ) const
186 {
187  if (bIndentFirst) out << indent;
188  out << "channel=" << simphotons.OpChannel() << " has ";
189  if (simphotons.empty()) { out << simphotons.size() << " no photons"; }
190  else {
191  out << simphotons.size() << " photons:";
192 
193  auto sortedPhotonPtrs = util::makePointerVector(simphotons);
194  std::sort(sortedPhotonPtrs.begin(), sortedPhotonPtrs.end(), OnePhotonSorter());
195 
196  for (auto const* onephoton : sortedPhotonPtrs) {
197  out << "\n" << indent << " ";
198  DumpOnePhoton(out, *onephoton);
199  } // for
200  }
201 
202 } // sim::DumpSimPhotons::DumpSimPhotons()
203 
204 //------------------------------------------------------------------------------
206 {
207 
208  // get the particles from the event
209  auto const& SimPhotons = *(event.getValidHandle<std::vector<sim::SimPhotons>>(fInputPhotons));
210 
212  << "Event " << event.id() << " : data product '" << fInputPhotons.encode() << "' contains "
213  << SimPhotons.size() << " SimPhotons";
214 
215  unsigned int iPhoton = 0;
216  for (sim::SimPhotons const& photons : SimPhotons) {
217 
219  // a bit of a header
220  log << "[#" << (iPhoton++) << "] ";
221  DumpElement(log, photons, " ", false);
222 
223  } // for
224  mf::LogVerbatim(fOutputCategory) << "\n"; // just an empty line
225 
226 } // sim::DumpSimPhotons::analyze()
227 
228 //------------------------------------------------------------------------------
230 
231 //------------------------------------------------------------------------------
MaybeLogger_< ELseverityLevel::ELsev_info, true > LogVerbatim
int OpChannel() const
Returns the optical channel number this object is associated to.
Definition: SimPhotons.h:239
void DumpElement(Stream &&out, sim::SimPhotons const &simphotons, std::string indent="", bool bIndentFirst=true) const
Dumps the content of the specified SimPhotons in the output stream.
Silly utility to sort vectors indirectly.
All information of a photon entering the sensitive optical detector volume.
Definition: SimPhotons.h:60
art::InputTag fInputPhotons
name of SimPhotons&#39;s data product
auto makePointerVector(Coll &coll)
Creates a STL vector with pointers to data from another collection.
EDAnalyzer(fhicl::ParameterSet const &pset)
Definition: EDAnalyzer.cc:6
std::string encode() const
Definition: InputTag.cc:97
geo::Point_t InitialPosition
Scintillation position in world coordinates [cm].
Definition: SimPhotons.h:63
Simulation objects for optical detectors.
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:65
parameter set interface
std::string indent(std::size_t const i)
std::string fOutputCategory
name of the stream for output
void DumpOnePhoton(Stream &&out, sim::OnePhoton const &photon) const
Dumps a sim::OnePhoton on a single line.
DumpSimPhotons & operator=(DumpSimPhotons const &)=delete
void analyze(art::Event const &event) override
Monte Carlo Simulation.
DumpSimPhotons(Parameters const &config)
Configuration-checking constructor.
Collection of photons which recorded on one channel.
Definition: SimPhotons.h:127
bool SetInSD
Whether the photon reaches the sensitive detector.
Definition: SimPhotons.h:84
geo::OpticalPoint_t FinalLocalPosition
Where photon enters the optical detector in local coordinates [cm].
Definition: SimPhotons.h:71
float Energy
Scintillation photon energy [GeV].
Definition: SimPhotons.h:78
Event finding and building.
int MotherTrackID
ID of the GEANT4 track causing the scintillation.
Definition: SimPhotons.h:81