All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MergeSimSources_module.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 // 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.
8 ////////////////////////////////////////////////////////////////////////
9 
10 #include "art/Framework/Core/EDProducer.h"
11 #include "art/Framework/Core/ModuleMacros.h"
12 #include "art/Framework/Principal/Event.h"
13 #include "art/Framework/Principal/Handle.h"
14 #include "art/Framework/Services/Registry/ServiceHandle.h"
15 #include "canvas/Utilities/InputTag.h"
16 #include "canvas/Utilities/Exception.h"
17 #include "messagefacility/MessageLogger/MessageLogger.h"
18 #include "fhiclcpp/types/Sequence.h"
19 #include "fhiclcpp/types/Atom.h"
20 #include "fhiclcpp/types/OptionalAtom.h"
21 #include "fhiclcpp/ParameterSet.h"
22 
23 #include "art/Persistency/Common/PtrMaker.h"
24 #include "canvas/Persistency/Common/Assns.h"
25 #include "canvas/Persistency/Common/FindOneP.h"
26 
27 #include <memory>
28 #include <optional>
29 #include <vector>
30 #include <string>
31 #include <utility>
32 
38 #include "nusimdata/SimulationBase/MCTruth.h"
39 #include "nusimdata/SimulationBase/MCParticle.h"
44 #include "MergeSimSources.h"
45 
46 namespace sim {
47  class MergeSimSources;
48 }
49 
50 class sim::MergeSimSources : public art::EDProducer {
51 public:
52 
53  struct Config {
54 
55  fhicl::Sequence<art::InputTag> InputSourcesLabels {
56  fhicl::Name{ "InputSourcesLabels" },
57  fhicl::Comment{ "label of the LArG4 products to merge" }
58  };
59 
60  fhicl::Sequence<int> TrackIDOffsets {
61  fhicl::Name{ "TrackIDOffsets" },
62  fhicl::Comment{ "offset to add to the MC particles from each source" }
63  };
64 
65  fhicl::Atom<bool> StoreReflected {
66  fhicl::Name{ "StoreReflected" },
67  fhicl::Comment{ "whether to merge also photons from reflections" },
68  false // default
69  };
70 
71  fhicl::Atom<bool> FillMCParticles {
72  fhicl::Name{ "FillMCParticles" },
73  fhicl::Comment{ "whether to merge MCParticle" },
74  true // default
75  };
76 
77  fhicl::Atom<bool> FillSimPhotons {
78  fhicl::Name{ "FillSimPhotons" },
79  fhicl::Comment{ "whether to merge SimPhotons" },
80  true // default
81  };
82 
83  fhicl::Atom<bool> FillSimChannels {
84  fhicl::Name{ "FillSimChannels" },
85  fhicl::Comment{ "whether to merge SimChannels" },
86  true // default
87  };
88 
89  fhicl::Atom<bool> FillAuxDetSimChannels {
90  fhicl::Name{ "FillAuxDetSimChannels" },
91  fhicl::Comment{ "whether to merge AuxDetSimChannels" },
92  true // default
93  };
94 
95  fhicl::OptionalAtom<bool> FillSimEnergyDeposits {
96  fhicl::Name{ "FillSimEnergyDeposits" },
97  fhicl::Comment
98  { "merges energy deposit collection; if omitted, follows LArG4Parmeters" }
99  };
100 
101  fhicl::Sequence<std::string> EnergyDepositInstanceLabels {
102  fhicl::Name{ "EnergyDepositInstanceLabels" },
103  fhicl::Comment{ "labels of sim::SimEnergyDeposit collections to merge" },
104  std::vector<std::string>{ "TPCActive", "Other" } // default
105  };
106 
107  fhicl::Atom<bool> FillAuxDetHits {
108  fhicl::Name{ "FillAuxDetHits" },
109  fhicl::Comment
110  { "whether to merge aux det hit collections" },
111  false // default
112  };
113 
114  fhicl::Sequence<std::string> AuxDetHitsInstanceLabels {
115  fhicl::Name{ "AuxDetHitsInstanceLabels" },
116  fhicl::Comment{ "labels of AuxDetHits collections to merge" },
117  std::vector<std::string>{ "LArG4DetectorServicevolAuxDetSensitiveCRTStripY", "Other" } // default
118  };
119 
120  }; // struct Config
121 
122  using Parameters = art::EDProducer::Table<Config>;
123 
124  explicit MergeSimSources(Parameters const & config);
125 
126  // Required functions.
127  void produce(art::Event & e) override;
128 
129 private:
130 
131  std::vector<art::InputTag> const fInputSourcesLabels;
132  std::vector<int> const fTrackIDOffsets;
133  bool const fUseLitePhotons;
134  bool const fStoreReflected;
135  bool const fFillMCParticles;
136  bool const fFillSimPhotons;
137  bool const fFillSimChannels;
140  std::vector<std::string> const fEnergyDepositionInstances;
141  bool const fFillAuxDetHits;
142  std::vector<std::string> const fAuxDetHitsInstanceLabels;
143 
144  static std::string const ReflectedLabel;
145 
146  void dumpConfiguration() const;
147 
148 };
149 
150 
151 namespace {
152 
153  template <typename Optional>
154  std::optional<typename Optional::value_type>
155  getOptionalValue(Optional const& parameter) {
156 
157  using Value_t = typename Optional::value_type;
158 
159  if (!parameter.hasValue()) return std::nullopt;
160 
161  Value_t value;
162  parameter(value);
163  return { value };
164 
165  } // getOptionalValue(Optional& parameter)
166 
167 } // local namespace
168 
169 
170 std::string const sim::MergeSimSources::ReflectedLabel { "Reflected" };
171 
172 
174  : EDProducer{params}
175  , fInputSourcesLabels(params().InputSourcesLabels())
176  , fTrackIDOffsets(params().TrackIDOffsets())
177  , fUseLitePhotons(art::ServiceHandle<sim::LArG4Parameters const>()->UseLitePhotons())
178  , fStoreReflected(params().StoreReflected())
179  , fFillMCParticles(params().FillMCParticles())
180  , fFillSimPhotons(params().FillSimPhotons())
181  , fFillSimChannels(params().FillSimChannels())
182  , fFillAuxDetSimChannels(params().FillAuxDetSimChannels())
183  , fFillSimEnergyDeposits(
184  getOptionalValue(params().FillSimEnergyDeposits).value_or
185  (art::ServiceHandle<sim::LArG4Parameters const>()->FillSimEnergyDeposits())
186  )
187  , fEnergyDepositionInstances(params().EnergyDepositInstanceLabels())
188  , fFillAuxDetHits(params().FillAuxDetHits())
189  , fAuxDetHitsInstanceLabels(params().AuxDetHitsInstanceLabels())
190 {
191 
192  if(fInputSourcesLabels.size() != fTrackIDOffsets.size()) {
193  throw art::Exception(art::errors::Configuration)
194  << "Unequal input vector sizes: InputSourcesLabels and TrackIDOffsets.\n";
195  }
196 
197 
198  for (art::InputTag const& tag: fInputSourcesLabels) {
199 
200  if (fFillMCParticles) {
201  consumes<std::vector<simb::MCParticle>>(tag);
202  consumes<art::Assns<simb::MCParticle, simb::MCTruth, sim::GeneratedParticleInfo>>(tag);
203  }
204 
205  if (fFillSimChannels) {
206  consumes<std::vector<sim::SimChannel>>(tag);
207  }
208 
209  if (fFillAuxDetSimChannels) {
210  consumes<std::vector<sim::AuxDetSimChannel>>(tag);
211  }
212 
213  if (fFillSimPhotons) {
214  if (!fUseLitePhotons) consumes<std::vector<sim::SimPhotons>>(tag);
215  else consumes<std::vector<sim::SimPhotonsLite>>(tag);
216 
217  if (fStoreReflected) {
218  art::InputTag const reflected_tag { tag.label(), ReflectedLabel };
219  if (!fUseLitePhotons) consumes<std::vector<sim::SimPhotons>>(reflected_tag);
220  else consumes<std::vector<sim::SimPhotonsLite>>(reflected_tag);
221  }
222  }
223 
224  if (fFillSimEnergyDeposits) {
225  for (std::string const& edep_inst: fEnergyDepositionInstances) {
226  art::InputTag const edep_tag { tag.label(), edep_inst };
227  consumes<std::vector<sim::SimEnergyDeposit>>(edep_tag);
228  }
229  } // if fill energy deposits
230 
231  if (fFillAuxDetHits) {
232  for (std::string const& auxdethit_inst: fAuxDetHitsInstanceLabels) {
233  art::InputTag const auxdethit_tag { tag.label(), auxdethit_inst };
234  consumes<std::vector<sim::AuxDetHit>>(auxdethit_tag);
235  }
236  }
237 
238  } // for input labels
239 
240 
241  if (fFillMCParticles) {
242  produces< std::vector<simb::MCParticle> >();
243  produces< art::Assns<simb::MCTruth, simb::MCParticle, sim::GeneratedParticleInfo> >();
244  }
245  if (fFillSimChannels) {
246  produces< std::vector<sim::SimChannel> >();
247  }
248  if (fFillAuxDetSimChannels) {
249  produces< std::vector<sim::AuxDetSimChannel> >();
250  }
251 
252  if (fFillSimPhotons) {
253  if(!fUseLitePhotons) produces< std::vector<sim::SimPhotons> >();
254  else produces< std::vector<sim::SimPhotonsLite> >();
255 
256  if (fStoreReflected) {
257  if(!fUseLitePhotons) produces< std::vector<sim::SimPhotons> >(ReflectedLabel);
258  else produces< std::vector<sim::SimPhotonsLite> >(ReflectedLabel);
259  }
260  }
261 
262  if (fFillSimEnergyDeposits) {
263  for (std::string const& edep_inst: fEnergyDepositionInstances)
264  produces< std::vector<sim::SimEnergyDeposit> >(edep_inst);
265  } // if
266 
267  if (fFillAuxDetHits) {
268  for (std::string const& auxdethit_inst: fAuxDetHitsInstanceLabels)
269  produces< std::vector<sim::AuxDetHit> >(auxdethit_inst);
270  }
271 
272 
273  dumpConfiguration();
274 
275 }
276 
278 {
279 
280  auto partCol = std::make_unique<std::vector<simb::MCParticle>>();
281  auto scCol = std::make_unique<std::vector<sim::SimChannel>>();
282  auto PhotonCol = std::make_unique<std::vector<sim::SimPhotons>>();
283  auto LitePhotonCol = std::make_unique<std::vector<sim::SimPhotonsLite>>();
284  auto ReflPhotonCol = std::make_unique<std::vector<sim::SimPhotons>>();
285  auto ReflLitePhotonCol = std::make_unique<std::vector<sim::SimPhotonsLite>>();
286  auto tpassn = std::make_unique<art::Assns<simb::MCTruth, simb::MCParticle, sim::GeneratedParticleInfo>>();
287  auto adCol = std::make_unique<std::vector<sim::AuxDetSimChannel>>();
288 
289  using edeps_t = std::vector<sim::SimEnergyDeposit>;
290  std::vector<edeps_t> edepCols;
291  if (fFillSimEnergyDeposits)
292  edepCols.resize(fEnergyDepositionInstances.size());
293 
294  using aux_det_hits_t = std::vector<sim::AuxDetHit>;
295  std::vector<aux_det_hits_t> auxdethitCols;
296  if (fFillAuxDetHits)
297  auxdethitCols.resize(fAuxDetHitsInstanceLabels.size());
298 
299 
300  MergeSimSourcesUtility MergeUtility { fTrackIDOffsets };
301 
302  for(auto const& [ i_source, input_label ]: util::enumerate(fInputSourcesLabels)) {
303 
304  if (fFillMCParticles) {
305  art::PtrMaker<simb::MCParticle> const makePartPtr { e };
306  auto const input_partCol
307  = e.getValidHandle<std::vector<simb::MCParticle>>(input_label);
308  MergeUtility.MergeMCParticles(*partCol,*input_partCol,i_source);
309 
310  //truth-->particle assoc stuff here
311  const std::vector<size_t>& assocVectorPrimitive
312  = MergeUtility.GetMCParticleListMap().at(i_source);
313  art::FindOneP<simb::MCTruth, sim::GeneratedParticleInfo> mctAssn(input_partCol,e,input_label);
314  for(auto const i_p: util::counter(mctAssn.size()))
315  tpassn->addSingle(mctAssn.at(i_p), makePartPtr(assocVectorPrimitive[i_p]), mctAssn.data(i_p).ref());
316  }
317 
318  if (fFillSimChannels) {
319  auto const& input_scCol
320  = e.getProduct<std::vector<sim::SimChannel>>(input_label);
321  MergeUtility.MergeSimChannels(*scCol,input_scCol,i_source);
322  }
323 
324  if (fFillAuxDetSimChannels) {
325  auto const& input_adCol
326  = e.getProduct<std::vector<sim::AuxDetSimChannel>>(input_label);
327  MergeUtility.MergeAuxDetSimChannels(*adCol,input_adCol,i_source);
328  }
329 
330  if (fFillSimPhotons) {
331  if(!fUseLitePhotons){
332  auto const& input_PhotonCol
333  = e.getProduct<std::vector<sim::SimPhotons>>(input_label);
334  MergeUtility.MergeSimPhotons(*PhotonCol,input_PhotonCol);
335  }
336  else{
337  auto const& input_LitePhotonCol
338  = e.getProduct<std::vector<sim::SimPhotonsLite>>(input_label);
339  MergeUtility.MergeSimPhotonsLite(*LitePhotonCol,input_LitePhotonCol);
340  }
341 
342  if (fStoreReflected) {
343  art::InputTag const input_reflected_label
344  { input_label.label(), ReflectedLabel };
345  if(!fUseLitePhotons){
346  auto const& input_PhotonCol
347  = e.getProduct<std::vector<sim::SimPhotons>>(input_reflected_label);
348  MergeUtility.MergeSimPhotons(*ReflPhotonCol,input_PhotonCol);
349  }
350  else{
351  auto const& input_LitePhotonCol
352  = e.getProduct<std::vector<sim::SimPhotonsLite>>(input_reflected_label);
353  MergeUtility.MergeSimPhotonsLite(*ReflLitePhotonCol,input_LitePhotonCol);
354  }
355  }
356  }
357 
358 
359  if (fFillSimEnergyDeposits) {
360  for (auto const& [ edep_inst, edepCol ]
361  : util::zip(fEnergyDepositionInstances, edepCols))
362  {
363  art::InputTag const edep_tag { input_label.label(), edep_inst };
364  MergeUtility.MergeSimEnergyDeposits(edepCol,
365  e.getProduct<edeps_t>(edep_tag),
366  i_source);
367  } // for edep
368  } // if fill energy depositions
369 
370 
371  if (fFillAuxDetHits) {
372  for (auto const& [ auxdethit_inst, auxdethitCol ]
373  : util::zip(fAuxDetHitsInstanceLabels, auxdethitCols))
374  {
375  art::InputTag const auxdethit_tag { input_label.label(), auxdethit_inst };
376  MergeUtility.MergeAuxDetHits(auxdethitCol,
377  e.getProduct<aux_det_hits_t>(auxdethit_tag),
378  i_source);
379  }
380  }
381 
382  }
383 
384  if (fFillMCParticles) {
385  e.put(std::move(partCol));
386  e.put(std::move(tpassn));
387  }
388  if (fFillSimChannels) {
389  e.put(std::move(scCol));
390  }
391  if (fFillAuxDetSimChannels) {
392  e.put(std::move(adCol));
393  }
394  if (fFillSimPhotons) {
395  if(!fUseLitePhotons) e.put(std::move(PhotonCol));
396  else e.put(std::move(LitePhotonCol));
397  if(fStoreReflected) {
398  if(!fUseLitePhotons) e.put(std::move(ReflPhotonCol), ReflectedLabel);
399  else e.put(std::move(ReflLitePhotonCol), ReflectedLabel);
400  }
401  }
402 
403  if(fFillSimEnergyDeposits) {
404  for (auto&& [ edep_inst, edepCol ]
405  : util::zip(fEnergyDepositionInstances, edepCols))
406  {
407  e.put(std::make_unique<edeps_t>(move(edepCol)), edep_inst);
408  } // for
409  } // if fill energy deposits
410 
411  if(fFillAuxDetHits) {
412  for (auto&& [ auxdethit_inst, auxdethitCol ]
413  : util::zip(fAuxDetHitsInstanceLabels, auxdethitCols))
414  {
415  e.put(std::make_unique<aux_det_hits_t>(move(auxdethitCol)), auxdethit_inst);
416  }
417  }
418 
419 }
420 
421 
423 
424  mf::LogInfo log("MergeSimSources");
425  log << "Configuration:"
426  << "\n - " << fInputSourcesLabels.size() << " input sources:";
427  for (auto const& [ i_source, tag, offset ]
428  : util::enumerate(fInputSourcesLabels, fTrackIDOffsets)
429  ) {
430  log << "\n [" << i_source << "] '" << tag.encode()
431  << "' (ID offset: " << offset << ")";
432  } // for
433  if (fFillMCParticles) log << "\n - filling MCParticles";
434 
435  if (fFillSimChannels) log << "\n - filling SimChannels";
436 
437  if (fFillAuxDetSimChannels) log << "\n - filling AuxDetSimChannels";
438 
439  if (fFillSimPhotons) {
440  log << "\n - filling Simulated Photons";
441  if (fUseLitePhotons) log << "\n - using photon summary (`SimPhotonsLite`)";
442  else log << "\n - using detailed photons (`SimPhotons`)";
443  if (fStoreReflected) log << "\n - also merging reflected light";
444  }
445 
446  if (fFillSimEnergyDeposits) {
447  log << "\n - filling simulated energy deposits ("
448  << fEnergyDepositionInstances.size() << " labels:";
449  for (std::string const& label: fEnergyDepositionInstances)
450  log << " '" << label << "'";
451  log << ")";
452  }
453 
454  if (fFillAuxDetHits) {
455  log << "\n - filling auxiliary detector hits ("
456  << fAuxDetHitsInstanceLabels.size() << " labels:";
457  for (std::string const& label: fAuxDetHitsInstanceLabels)
458  log << " '" << label << "'";
459  log << ")";
460  }
461 
462 } // sim::MergeSimSources::dumpConfiguration()
463 
464 
465 DEFINE_ART_MODULE(sim::MergeSimSources)
Store parameters for running LArG4.
MergeSimSources(Parameters const &config)
fhicl::Sequence< int > TrackIDOffsets
Definition of util::zip().
BEGIN_PROLOG TPC Trig offset(g4 rise time) ProjectToHeight
Definition: CORSIKAGen.fcl:7
std::optional< typename Optional::value_type > getOptionalValue(Optional const &parameter)
Returns the value of an optional parameter as std::optional.
Definition: FHiCLutils.h:188
Definition of util::enumerate().
fhicl::Atom< bool > FillAuxDetSimChannels
Contains data associated to particles from detector simulation.
art::EDProducer::Table< Config > Parameters
fhicl::OptionalAtom< bool > FillSimEnergyDeposits
auto enumerate(Iterables &&...iterables)
Range-for loop helper tracking the number of iteration.
Definition: enumerate.h:69
fhicl::Sequence< std::string > EnergyDepositInstanceLabels
std::vector< art::InputTag > const fInputSourcesLabels
process_name larg4outtime TrackIDOffsets
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:285
BEGIN_PROLOG vertical distance to the surface Name
std::vector< int > const fTrackIDOffsets
Test of util::counter and support utilities.
void produce(art::Event &e) override
fhicl::Sequence< std::string > AuxDetHitsInstanceLabels
void MergeMCParticles(std::vector< simb::MCParticle > &, const std::vector< simb::MCParticle > &, size_t)
static std::string const ReflectedLabel
contains information for a single step in the detector simulation
BEGIN_PROLOG larg4outtime StoreReflected
object containing MC truth information necessary for making RawDigits and doing back tracking ...
do i e
auto zip(Iterables &&...iterables)
Range-for loop helper iterating across many collections at the same time.
Definition: zip.h:295
temporary value
fhicl::Sequence< art::InputTag > InputSourcesLabels
services LArG4Parameters UseLitePhotons
std::vector< std::string > const fEnergyDepositionInstances