LArSoft  v07_13_02
Liquid Argon Software toolkit - http://larsoft.org/
DumpMCTruth_module.cc
Go to the documentation of this file.
1 
13 // LArSoft libraries
14 #include "lardataalg/MCDumpers/MCDumpers.h" // sim::dump namespace
15 
16 // nutools libraries
19 
20 // framework libraries
27 #include "fhiclcpp/ParameterSet.h"
28 #include "fhiclcpp/types/Atom.h"
31 
32 namespace sim {
33  class DumpMCTruth;
34 }
35 
36 
38  public:
39 
41  struct Config {
42  using Name = fhicl::Name;
44 
46  Name("InputTruth"),
47  Comment("data product with the collection of MC truth to be dumped")
48  };
49 
51  Name("OutputCategory"),
52  Comment("name of the output stream (managed by the message facility)"),
53  "DumpMCTruth" /* default value */
54  };
55 
57  Name("PointsPerLine"),
58  Comment("trajectory points printed per line (default: 2; 0 = skip them)"),
59  2 /* default value */
60  };
61 
62  }; // struct Config
63 
66 
68  explicit DumpMCTruth(Parameters const& config);
69 
70  // Plugins should not be copied or assigned.
71  DumpMCTruth(DumpMCTruth const&) = delete;
72  DumpMCTruth(DumpMCTruth &&) = delete;
73  DumpMCTruth& operator = (DumpMCTruth const&) = delete;
74  DumpMCTruth& operator = (DumpMCTruth &&) = delete;
75 
76 
77  // Operates on the event
78  void analyze(art::Event const& event) override;
79 
80 
82  template <typename Handle>
83  static std::string productName(Handle const& handle);
84 
85 
86  private:
87 
88  std::vector<art::InputTag> fInputTruth;
89  std::string fOutputCategory;
90  bool bAllTruth = false;
91 
92  unsigned int fPointsPerLine;
93 
94 
95 }; // class sim::DumpMCTruth
96 
97 
98 //------------------------------------------------------------------------------
99 //--- module implementation
100 //---
101 //------------------------------------------------------------------------------
103  : EDAnalyzer(config)
104  , fInputTruth()
105  , fOutputCategory(config().OutputCategory())
106  , bAllTruth(!config().InputTruth(fInputTruth)) // true if InputTruth omitted
107  , fPointsPerLine(config().PointsPerLine())
108  {}
109 
110 
111 //------------------------------------------------------------------------------
113 
114  //
115  // prepare the data products to be dumped
116  //
117  struct ProductInfo_t {
118  using Thruths_t = std::vector<simb::MCTruth>;
119  Thruths_t const* truths;
120  std::string name;
121 
122  ProductInfo_t(art::Handle<Thruths_t> const& handle)
123  : truths(handle.provenance()->isPresent()? handle.product(): nullptr)
124  , name(sim::DumpMCTruth::productName(handle))
125  {}
126  ProductInfo_t(art::ValidHandle<Thruths_t> const& handle)
127  : truths(handle.product()), name(sim::DumpMCTruth::productName(handle))
128  {}
129 
130  }; // ProductInfo_t
131 
132  std::vector<ProductInfo_t> AllTruths;
133  if (bAllTruth) {
134  std::vector<art::Handle<std::vector<simb::MCTruth>>> handles;
135  event.getManyByType(handles);
136  std::copy(handles.begin(), handles.end(), std::back_inserter(AllTruths));
137  }
138  else {
139  for (auto const& inputTag: fInputTruth) {
140  AllTruths.emplace_back
141  (event.getValidHandle<std::vector<simb::MCTruth>>(inputTag));
142  } // for
143  }
144 
145  //
146  // sanity check
147  //
148  if (AllTruths.empty()) {
150  << "No MC truth found to be dumped!\n";
151  }
152 
153  //
154  // print an introduction
155  //
156  unsigned int const nTruths = std::accumulate(
157  AllTruths.begin(), AllTruths.end(), 0U,
158  [](unsigned int total, auto const& info)
159  { return total + (info.truths? info.truths->size(): 0); }
160  );
161 
162  if (bAllTruth) {
163  mf::LogVerbatim(fOutputCategory) << "Event " << event.id()
164  << " contains " << nTruths << " MC truth blocks in "
165  << AllTruths.size() << " collections";
166  }
167  else if (AllTruths.size() == 1) {
168  mf::LogVerbatim(fOutputCategory) << "Event " << event.id();
169  }
170  else {
171  mf::LogVerbatim(fOutputCategory) << "Dumping " << nTruths
172  << " MC truth blocks from " << AllTruths.size()
173  << " collections in event " << event.id();
174  }
175 
176  //
177  // dump data product by data product
178  //
179  unsigned int nParticles = 0, nNeutrinos = 0;
180  for (ProductInfo_t const& truths_info: AllTruths) {
181 
182  auto const* truths = truths_info.truths;
183  std::string productName = truths_info.name;
184 
185  if (!truths) {
187  << "Data product '" << productName
188  << "' has been dropped. No information available.";
189  }
190 
191  if (AllTruths.size() > 1) {
193  << "Data product '" << productName
194  << "' contains " << truths->size() << " truth blocks:";
195  }
196  else if (truths->size() > 1) {
198  << truths->size() << " truth blocks:";
199  }
200 
201  //
202  // dump each MC truth in the data product
203  //
204  unsigned int iTruth = 0;
205  for (auto const& truth: *truths) {
206 
208 
209  if (truths->size() > 1) log << "(#" << iTruth << ") ";
210  sim::dump::DumpMCTruth(log, truth, " ", "");
211 
212  //
213  // update counters
214  //
215  ++iTruth;
216  nParticles += truth.NParticles();
217  if (truth.NeutrinoSet()) ++nNeutrinos;
218 
219  } // for each truth in data product
220 
221  } // for truth data products
222 
223  //
224  // all done
225  //
226  mf::LogVerbatim(fOutputCategory) << nNeutrinos
227  << " neutrinos generated, " << nParticles
228  << " generated particles to be simulated downstream.";
229 
230 } // sim::DumpMCTruth::analyze()
231 
232 
233 //------------------------------------------------------------------------------
234 template <typename Handle>
235 std::string sim::DumpMCTruth::productName(Handle const& handle) {
236  auto const* prov = handle.provenance();
237  return prov->moduleLabel()
238  + '_' + prov->productInstanceName()
239  + '_' + prov->processName()
240  ;
241 } // sim::DumpMCTruth::productName()
242 
243 
244 //------------------------------------------------------------------------------
246 
247 //------------------------------------------------------------------------------
MaybeLogger_< ELseverityLevel::ELsev_info, true > LogVerbatim
fhicl::Atom< unsigned int > PointsPerLine
DumpMCTruth & operator=(DumpMCTruth const &)=delete
fhicl::OptionalSequence< art::InputTag > InputTruth
fhicl::Atom< std::string > OutputCategory
Collection of configuration parameters for the module.
Particle class.
unsigned int fPointsPerLine
trajectory points per output line
T const * product() const
Definition: Handle.h:321
void analyze(art::Event const &event) override
T const * product() const
Definition: Handle.h:167
Utility functions to print MC truth information.
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:42
Provenance const * provenance() const
Definition: Handle.h:204
void DumpMCTruth(Stream &&out, simb::MCTruth const &truth, unsigned int pointsPerLine, std::string indent, std::string firstIndent)
Dumps the content of the specified MC truth in the output stream.
Definition: MCDumpers.h:346
bool bAllTruth
Whether to process all MCTruth collections.
EDAnalyzer(Table< Config > const &config)
Definition: EDAnalyzer.h:100
DumpMCTruth(Parameters const &config)
Configuration-checking constructor.
Monte Carlo Simulation.
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
std::string fOutputCategory
Name of the stream for output.
ValidHandle< PROD > getValidHandle(InputTag const &tag) const
static std::string productName(Handle const &handle)
Returns the name of the product in the form "module_instance_process".
Event finding and building.
std::vector< art::InputTag > fInputTruth
Name of MCTruth data products.