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 
19 #include "fhiclcpp/types/Atom.h"
21 #include "fhiclcpp/ParameterSet.h"
22 
25 #include "canvas/Persistency/Common/FindOneP.h"
26 
27 #include <memory>
28 #include <optional>
29 #include <vector>
30 #include <string>
31 #include <utility>
32 
44 #include "MergeSimSources.h"
45 
46 namespace sim {
47  class MergeSimSources;
48 }
49 
51 public:
52 
53  struct Config {
54 
56  fhicl::Name{ "InputSourcesLabels" },
57  fhicl::Comment{ "label of the LArG4 products to merge" }
58  };
59 
61  fhicl::Name{ "TrackIDOffsets" },
62  fhicl::Comment{ "offset to add to the MC particles from each source" }
63  };
64 
66  fhicl::Name{ "StoreReflected" },
67  fhicl::Comment{ "whether to merge also photons from reflections" },
68  false // default
69  };
70 
72  fhicl::Name{ "FillMCParticles" },
73  fhicl::Comment{ "whether to merge MCParticle" },
74  true // default
75  };
76 
78  fhicl::Name{ "FillSimPhotons" },
79  fhicl::Comment{ "whether to merge SimPhotons" },
80  true // default
81  };
82 
84  fhicl::Name{ "FillSimChannels" },
85  fhicl::Comment{ "whether to merge SimChannels" },
86  true // default
87  };
88 
90  fhicl::Name{ "FillAuxDetSimChannels" },
91  fhicl::Comment{ "whether to merge AuxDetSimChannels" },
92  true // default
93  };
94 
96  fhicl::Name{ "FillSimEnergyDeposits" },
98  { "merges energy deposit collection; if omitted, follows LArG4Parmeters" }
99  };
100 
102  fhicl::Name{ "EnergyDepositInstanceLabels" },
103  fhicl::Comment{ "labels of sim::SimEnergyDeposit collections to merge" },
104  std::vector<std::string>{ "TPCActive", "Other" } // default
105  };
106 
108  fhicl::Name{ "FillAuxDetHits" },
110  { "whether to merge aux det hit collections" },
111  false // default
112  };
113 
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 
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 
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 
171 
172 
174  : EDProducer{params}
175  , fInputSourcesLabels(params().InputSourcesLabels())
176  , fTrackIDOffsets(params().TrackIDOffsets())
178  , fStoreReflected(params().StoreReflected())
179  , fFillMCParticles(params().FillMCParticles())
180  , fFillSimPhotons(params().FillSimPhotons())
181  , fFillSimChannels(params().FillSimChannels())
182  , fFillAuxDetSimChannels(params().FillAuxDetSimChannels())
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()) {
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 
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 
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  }
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 
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 
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;
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 
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 
360  for (auto const& [ edep_inst, edepCol ]
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  }
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 
404  for (auto&& [ edep_inst, edepCol ]
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 ]
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 
447  log << "\n - filling simulated energy deposits ("
448  << fEnergyDepositionInstances.size() << " labels:";
450  log << " '" << label << "'";
451  log << ")";
452  }
453 
454  if (fFillAuxDetHits) {
455  log << "\n - filling auxiliary detector hits ("
456  << fAuxDetHitsInstanceLabels.size() << " labels:";
458  log << " '" << label << "'";
459  log << ")";
460  }
461 
462 } // sim::MergeSimSources::dumpConfiguration()
463 
464 
Store parameters for running LArG4.
MergeSimSources(Parameters const &config)
fhicl::Sequence< int > TrackIDOffsets
Definition of util::zip().
Definition of util::enumerate().
std::string string
Definition: nybbler.cc:12
EDProducer(fhicl::ParameterSet const &pset)
Definition: EDProducer.h:20
fhicl::Atom< bool > FillAuxDetSimChannels
Particle class.
fhicl::OptionalAtom< bool > FillSimEnergyDeposits
static Argument * parameter
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
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:285
const double e
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:67
static Config * config
Definition: config.cpp:1054
def move(depos, offset)
Definition: depos.py:107
ValidHandle< PROD > getValidHandle(InputTag const &tag) const
Definition: DataViewImpl.h:441
ProductID put(std::unique_ptr< PROD > &&edp, std::string const &instance={})
Definition: DataViewImpl.h:686
std::vector< int > const fTrackIDOffsets
Test of util::counter and support utilities.
Code to link reconstructed objects back to the MC truth information.
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
Contains data associated to particles from detector simulation.
contains information for a single step in the detector simulation
auto zip(Iterables &&...iterables)
Range-for loop helper iterating across many collections at the same time.
Definition: zip.h:295
PROD const & getProduct(InputTag const &tag) const
Definition: DataViewImpl.h:367
fhicl::Sequence< art::InputTag > InputSourcesLabels
std::vector< std::string > const fEnergyDepositionInstances