LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
MergeSimSources_module.cc
Go to the documentation of this file.
1 // Class: MergeSimSources
3 // Module Type: producer
4 // File: MergeSimSources_module.cc
5 //
6 // Generated at Tue Feb 17 12:16:35 2015 by Wesley Ketchum using artmod
7 // from cetpkgsupport v1_08_02.
9 
17 #include "fhiclcpp/ParameterSet.h"
18 #include "fhiclcpp/types/Atom.h"
22 
26 
27 #include <memory>
28 #include <optional>
29 #include <string>
30 #include <utility>
31 #include <vector>
32 
33 #include "MergeSimSources.h"
45 
46 namespace sim {
47  class MergeSimSources;
48 }
49 
51 public:
52  struct Config {
53 
55  fhicl::Name{"InputSourcesLabels"},
56  fhicl::Comment{"label of the LArG4 products to merge"}};
57 
59  fhicl::Name{"TrackIDOffsets"},
60  fhicl::Comment{"offset to add to the MC particles from each source"}};
61 
63  fhicl::Name{"StoreReflected"},
64  fhicl::Comment{"whether to merge also photons from reflections"},
65  false // default
66  };
67 
69  fhicl::Name{"FillMCParticles"},
70  fhicl::Comment{"whether to merge MCParticle"},
71  true // default
72  };
73 
75  fhicl::Name{"FillSimPhotons"},
76  fhicl::Comment{"whether to merge SimPhotons"},
77  true // default
78  };
79 
81  fhicl::Name{"FillSimChannels"},
82  fhicl::Comment{"whether to merge SimChannels"},
83  true // default
84  };
85 
87  fhicl::Name{"FillAuxDetSimChannels"},
88  fhicl::Comment{"whether to merge AuxDetSimChannels"},
89  true // default
90  };
91 
93  fhicl::Name{"FillSimEnergyDeposits"},
94  fhicl::Comment{"merges energy deposit collection; if omitted, follows LArG4Parmeters"}};
95 
97  fhicl::Name{"EnergyDepositInstanceLabels"},
98  fhicl::Comment{"labels of sim::SimEnergyDeposit collections to merge"},
99  std::vector<std::string>{"TPCActive", "Other"} // default
100  };
101 
103  fhicl::Name{"FillAuxDetHits"},
104  fhicl::Comment{"whether to merge aux det hit collections"},
105  false // default
106  };
107 
109  fhicl::Name{"AuxDetHitsInstanceLabels"},
110  fhicl::Comment{"labels of AuxDetHits collections to merge"},
111  std::vector<std::string>{"LArG4DetectorServicevolAuxDetSensitiveCRTStripY",
112  "Other"} // default
113  };
114 
116  fhicl::Name{"FillParticleAncestryMaps"},
117  fhicl::Comment{"whether to merge particle ancestry maps"},
118  true};
119 
120  }; // struct Config
121 
123 
124  explicit MergeSimSources(Parameters const& config);
125 
126  // Required functions.
127  void produce(art::Event& e) override;
128 
129 private:
130  std::vector<art::InputTag> const fInputSourcesLabels;
131  std::vector<int> const fTrackIDOffsets;
132  bool const fUseLitePhotons;
133  bool const fStoreReflected;
134  bool const fFillMCParticles;
135  bool const fFillSimPhotons;
136  bool const fFillSimChannels;
139  std::vector<std::string> const fEnergyDepositionInstances;
140  bool const fFillAuxDetHits;
141  std::vector<std::string> const fAuxDetHitsInstanceLabels;
143 
144  static std::string const ReflectedLabel;
145 
146  void dumpConfiguration() const;
147 };
148 
149 namespace {
150 
151  template <typename Optional>
152  std::optional<typename Optional::value_type> getOptionalValue(Optional const& parameter)
153  {
154 
155  using Value_t = typename Optional::value_type;
156 
157  if (!parameter.hasValue()) return std::nullopt;
158 
159  Value_t value;
160  parameter(value);
161  return {value};
162 
163  } // getOptionalValue(Optional& parameter)
164 
165 } // local namespace
166 
167 std::string const sim::MergeSimSources::ReflectedLabel{"Reflected"};
168 
170  : EDProducer{params}
171  , fInputSourcesLabels(params().InputSourcesLabels())
172  , fTrackIDOffsets(params().TrackIDOffsets())
174  , fStoreReflected(params().StoreReflected())
175  , fFillMCParticles(params().FillMCParticles())
176  , fFillSimPhotons(params().FillSimPhotons())
177  , fFillSimChannels(params().FillSimChannels())
178  , fFillAuxDetSimChannels(params().FillAuxDetSimChannels())
180  getOptionalValue(params().FillSimEnergyDeposits)
181  .value_or(art::ServiceHandle<sim::LArG4Parameters const>()->FillSimEnergyDeposits()))
182  , fEnergyDepositionInstances(params().EnergyDepositInstanceLabels())
183  , fFillAuxDetHits(params().FillAuxDetHits())
184  , fAuxDetHitsInstanceLabels(params().AuxDetHitsInstanceLabels())
185  , fFillParticleAncestryMaps(params().FillParticleAncestryMaps())
186 {
187 
188  if (fInputSourcesLabels.size() != fTrackIDOffsets.size()) {
190  << "Unequal input vector sizes: InputSourcesLabels and TrackIDOffsets.\n";
191  }
192 
193  for (art::InputTag const& tag : fInputSourcesLabels) {
194 
195  if (fFillMCParticles) {
196  consumes<std::vector<simb::MCParticle>>(tag);
197  consumes<art::Assns<simb::MCParticle, simb::MCTruth, sim::GeneratedParticleInfo>>(tag);
198  }
199 
200  if (fFillSimChannels) { consumes<std::vector<sim::SimChannel>>(tag); }
201 
202  if (fFillAuxDetSimChannels) { consumes<std::vector<sim::AuxDetSimChannel>>(tag); }
203 
204  if (fFillSimPhotons) {
205  if (!fUseLitePhotons)
206  consumes<std::vector<sim::SimPhotons>>(tag);
207  else
208  consumes<std::vector<sim::SimPhotonsLite>>(tag);
209 
210  if (fStoreReflected) {
211  art::InputTag const reflected_tag{tag.label(), ReflectedLabel};
212  if (!fUseLitePhotons)
213  consumes<std::vector<sim::SimPhotons>>(reflected_tag);
214  else
215  consumes<std::vector<sim::SimPhotonsLite>>(reflected_tag);
216  }
217  }
218 
220  for (std::string const& edep_inst : fEnergyDepositionInstances) {
221  art::InputTag const edep_tag{tag.label(), edep_inst};
222  consumes<std::vector<sim::SimEnergyDeposit>>(edep_tag);
223  }
224  } // if fill energy deposits
225 
226  if (fFillAuxDetHits) {
227  for (std::string const& auxdethit_inst : fAuxDetHitsInstanceLabels) {
228  art::InputTag const auxdethit_tag{tag.label(), auxdethit_inst};
229  consumes<std::vector<sim::AuxDetHit>>(auxdethit_tag);
230  }
231  }
232 
233  if (fFillParticleAncestryMaps) { consumes<sim::ParticleAncestryMap>(tag); }
234 
235  } // for input labels
236 
237  if (fFillMCParticles) {
238  produces<std::vector<simb::MCParticle>>();
239  produces<art::Assns<simb::MCTruth, simb::MCParticle, sim::GeneratedParticleInfo>>();
240  }
241  if (fFillSimChannels) { produces<std::vector<sim::SimChannel>>(); }
242  if (fFillAuxDetSimChannels) { produces<std::vector<sim::AuxDetSimChannel>>(); }
243 
244  if (fFillSimPhotons) {
245  if (!fUseLitePhotons)
246  produces<std::vector<sim::SimPhotons>>();
247  else
248  produces<std::vector<sim::SimPhotonsLite>>();
249 
250  if (fStoreReflected) {
251  if (!fUseLitePhotons)
252  produces<std::vector<sim::SimPhotons>>(ReflectedLabel);
253  else
254  produces<std::vector<sim::SimPhotonsLite>>(ReflectedLabel);
255  }
256  }
257 
259  for (std::string const& edep_inst : fEnergyDepositionInstances)
260  produces<std::vector<sim::SimEnergyDeposit>>(edep_inst);
261  } // if
262 
263  if (fFillAuxDetHits) {
264  for (std::string const& auxdethit_inst : fAuxDetHitsInstanceLabels)
265  produces<std::vector<sim::AuxDetHit>>(auxdethit_inst);
266  }
267 
268  if (fFillParticleAncestryMaps) { produces<std::vector<sim::ParticleAncestryMap>>(); }
269 
271 }
272 
274 {
275 
276  auto partCol = std::make_unique<std::vector<simb::MCParticle>>();
277  auto scCol = std::make_unique<std::vector<sim::SimChannel>>();
278  auto PhotonCol = std::make_unique<std::vector<sim::SimPhotons>>();
279  auto LitePhotonCol = std::make_unique<std::vector<sim::SimPhotonsLite>>();
280  auto ReflPhotonCol = std::make_unique<std::vector<sim::SimPhotons>>();
281  auto ReflLitePhotonCol = std::make_unique<std::vector<sim::SimPhotonsLite>>();
282  auto tpassn =
283  std::make_unique<art::Assns<simb::MCTruth, simb::MCParticle, sim::GeneratedParticleInfo>>();
284  auto adCol = std::make_unique<std::vector<sim::AuxDetSimChannel>>();
285  auto pamCol = std::make_unique<std::vector<sim::ParticleAncestryMap>>();
286 
287  using edeps_t = std::vector<sim::SimEnergyDeposit>;
288  std::vector<edeps_t> edepCols;
289  if (fFillSimEnergyDeposits) edepCols.resize(fEnergyDepositionInstances.size());
290 
291  using aux_det_hits_t = std::vector<sim::AuxDetHit>;
292  std::vector<aux_det_hits_t> auxdethitCols;
293  if (fFillAuxDetHits) auxdethitCols.resize(fAuxDetHitsInstanceLabels.size());
294 
296 
297  for (auto const& [i_source, input_label] : util::enumerate(fInputSourcesLabels)) {
298 
299  if (fFillMCParticles) {
300  art::PtrMaker<simb::MCParticle> const makePartPtr{e};
301  auto const input_partCol = e.getValidHandle<std::vector<simb::MCParticle>>(input_label);
302  MergeUtility.MergeMCParticles(*partCol, *input_partCol, i_source);
303 
304  //truth-->particle assoc stuff here
305  const std::vector<size_t>& assocVectorPrimitive =
306  MergeUtility.GetMCParticleListMap().at(i_source);
308  input_partCol, e, input_label);
309  for (auto const i_p : util::counter(mctAssn.size()))
310  tpassn->addSingle(
311  mctAssn.at(i_p), makePartPtr(assocVectorPrimitive[i_p]), mctAssn.data(i_p).ref());
312  }
313 
314  if (fFillSimChannels) {
315  auto const& input_scCol = e.getProduct<std::vector<sim::SimChannel>>(input_label);
316  MergeUtility.MergeSimChannels(*scCol, input_scCol, i_source);
317  }
318 
320  auto const& input_adCol = e.getProduct<std::vector<sim::AuxDetSimChannel>>(input_label);
321  MergeUtility.MergeAuxDetSimChannels(*adCol, input_adCol, i_source);
322  }
323 
324  if (fFillSimPhotons) {
325  if (!fUseLitePhotons) {
326  auto const& input_PhotonCol = e.getProduct<std::vector<sim::SimPhotons>>(input_label);
327  MergeUtility.MergeSimPhotons(*PhotonCol, input_PhotonCol);
328  }
329  else {
330  auto const& input_LitePhotonCol =
331  e.getProduct<std::vector<sim::SimPhotonsLite>>(input_label);
332  MergeUtility.MergeSimPhotonsLite(*LitePhotonCol, input_LitePhotonCol);
333  }
334 
335  if (fStoreReflected) {
336  art::InputTag const input_reflected_label{input_label.label(), ReflectedLabel};
337  if (!fUseLitePhotons) {
338  auto const& input_PhotonCol =
339  e.getProduct<std::vector<sim::SimPhotons>>(input_reflected_label);
340  MergeUtility.MergeSimPhotons(*ReflPhotonCol, input_PhotonCol);
341  }
342  else {
343  auto const& input_LitePhotonCol =
344  e.getProduct<std::vector<sim::SimPhotonsLite>>(input_reflected_label);
345  MergeUtility.MergeSimPhotonsLite(*ReflLitePhotonCol, input_LitePhotonCol);
346  }
347  }
348  }
349 
351  for (auto const& [edep_inst, edepCol] : util::zip(fEnergyDepositionInstances, edepCols)) {
352  art::InputTag const edep_tag{input_label.label(), edep_inst};
353  MergeUtility.MergeSimEnergyDeposits(edepCol, e.getProduct<edeps_t>(edep_tag), i_source);
354  } // for edep
355  } // if fill energy depositions
356 
357  if (fFillAuxDetHits) {
358  for (auto const& [auxdethit_inst, auxdethitCol] :
359  util::zip(fAuxDetHitsInstanceLabels, auxdethitCols)) {
360  art::InputTag const auxdethit_tag{input_label.label(), auxdethit_inst};
361  MergeUtility.MergeAuxDetHits(
362  auxdethitCol, e.getProduct<aux_det_hits_t>(auxdethit_tag), i_source);
363  }
364 
366  auto const& input_pamCol = e.getProduct<sim::ParticleAncestryMap>(input_label);
367  MergeUtility.MergeParticleAncestryMaps(*pamCol, input_pamCol, i_source);
368  }
369  }
370  }
371 
372  if (fFillMCParticles) {
373  e.put(std::move(partCol));
374  e.put(std::move(tpassn));
375  }
376  if (fFillSimChannels) { e.put(std::move(scCol)); }
377  if (fFillAuxDetSimChannels) { e.put(std::move(adCol)); }
378  if (fFillSimPhotons) {
379  if (!fUseLitePhotons)
380  e.put(std::move(PhotonCol));
381  else
382  e.put(std::move(LitePhotonCol));
383  if (fStoreReflected) {
384  if (!fUseLitePhotons)
385  e.put(std::move(ReflPhotonCol), ReflectedLabel);
386  else
387  e.put(std::move(ReflLitePhotonCol), ReflectedLabel);
388  }
389  }
390 
392  for (auto&& [edep_inst, edepCol] : util::zip(fEnergyDepositionInstances, edepCols)) {
393  e.put(std::make_unique<edeps_t>(move(edepCol)), edep_inst);
394  } // for
395  } // if fill energy deposits
396 
397  if (fFillAuxDetHits) {
398  for (auto&& [auxdethit_inst, auxdethitCol] :
399  util::zip(fAuxDetHitsInstanceLabels, auxdethitCols)) {
400  e.put(std::make_unique<aux_det_hits_t>(move(auxdethitCol)), auxdethit_inst);
401  }
402  }
403 
404  if (fFillParticleAncestryMaps) { e.put(std::move(pamCol)); }
405 }
406 
408 {
409 
410  mf::LogInfo log("MergeSimSources");
411  log << "Configuration:"
412  << "\n - " << fInputSourcesLabels.size() << " input sources:";
413  for (auto const& [i_source, tag, offset] :
415  log << "\n [" << i_source << "] '" << tag.encode() << "' (ID offset: " << offset << ")";
416  } // for
417  if (fFillMCParticles) log << "\n - filling MCParticles";
418 
419  if (fFillSimChannels) log << "\n - filling SimChannels";
420 
421  if (fFillAuxDetSimChannels) log << "\n - filling AuxDetSimChannels";
422 
423  if (fFillSimPhotons) {
424  log << "\n - filling Simulated Photons";
425  if (fUseLitePhotons)
426  log << "\n - using photon summary (`SimPhotonsLite`)";
427  else
428  log << "\n - using detailed photons (`SimPhotons`)";
429  if (fStoreReflected) log << "\n - also merging reflected light";
430  }
431 
433  log << "\n - filling simulated energy deposits (" << fEnergyDepositionInstances.size()
434  << " labels:";
435  for (std::string const& label : fEnergyDepositionInstances)
436  log << " '" << label << "'";
437  log << ")";
438  }
439 
440  if (fFillAuxDetHits) {
441  log << "\n - filling auxiliary detector hits (" << fAuxDetHitsInstanceLabels.size()
442  << " labels:";
443  for (std::string const& label : fAuxDetHitsInstanceLabels)
444  log << " '" << label << "'";
445  log << ")";
446  }
447 
448  if (fFillParticleAncestryMaps) log << "\n - filling ParticleAncestryMaps";
449 
450 } // sim::MergeSimSources::dumpConfiguration()
451 
Store parameters for running LArG4.
MergeSimSources(Parameters const &config)
fhicl::Sequence< int > TrackIDOffsets
Definition of util::zip().
Definition of util::enumerate().
EDProducer(fhicl::ParameterSet const &pset)
Definition: EDProducer.cc:6
fhicl::Atom< bool > FillAuxDetSimChannels
Contains data associated to particles from detector simulation.
Particle class.
fhicl::OptionalAtom< bool > FillSimEnergyDeposits
auto enumerate(Iterables &&...iterables)
Range-for loop helper tracking the number of iteration.
Definition: enumerate.h:65
fhicl::Sequence< std::string > EnergyDepositInstanceLabels
std::vector< art::InputTag > const fInputSourcesLabels
PutHandle< PROD > put(std::unique_ptr< PROD > &&edp, std::string const &instance={})
Definition: Event.h:77
std::string const & label() const noexcept
Definition: InputTag.cc:79
Simulation objects for optical detectors.
std::vector< std::string > const fAuxDetHitsInstanceLabels
auto counter(T begin, T end)
Returns an object to iterate values from begin to end in a range-for loop.
Definition: counter.h:295
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:65
std::vector< int > const fTrackIDOffsets
Test of util::counter and support utilities.
Monte Carlo Simulation.
double value
Definition: spectrum.C:18
void produce(art::Event &e) override
fhicl::Sequence< std::string > AuxDetHitsInstanceLabels
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
static std::string const ReflectedLabel
ValidHandle< PROD > getValidHandle(InputTag const &tag) const
contains information for a single step in the detector simulation
object containing MC truth information necessary for making RawDigits and doing back tracking ...
auto zip(Iterables &&...iterables)
Range-for loop helper iterating across many collections at the same time.
Definition: zip.h:293
cet::maybe_ref< Data const > data(size_type i) const
Definition: FindOneP.h:491
fhicl::Atom< bool > FillParticleAncestryMaps
Float_t e
Definition: plot.C:35
PROD const & getProduct(InputTag const &tag) const
fhicl::Sequence< art::InputTag > InputSourcesLabels
std::vector< std::string > const fEnergyDepositionInstances