17 #include "nusimdata/SimulationBase/MCTruth.h"
20 #include "art/Framework/Core/EDAnalyzer.h"
21 #include "art/Framework/Core/ModuleMacros.h"
22 #include "art/Framework/Principal/Event.h"
23 #include "art/Framework/Principal/Handle.h"
24 #include "art/Framework/Principal/Provenance.h"
25 #include "canvas/Utilities/InputTag.h"
26 #include "fhiclcpp/types/Atom.h"
27 #include "fhiclcpp/types/OptionalSequence.h"
28 #include "messagefacility/MessageLogger/MessageLogger.h"
47 Comment(
"data product with the collection of MC truth to be dumped")
51 Name(
"OutputCategory"),
52 Comment(
"name of the output stream (managed by the message facility)"),
57 Name(
"PointsPerLine"),
58 Comment(
"trajectory points printed per line (default: 2; 0 = skip them)"),
78 void analyze(art::Event
const& event)
override;
82 template <
typename Handle>
83 static std::string
productName(Handle
const& handle);
105 , fOutputCategory(config().OutputCategory())
106 , bAllTruth(!config().InputTruth(fInputTruth))
107 , fPointsPerLine(config().PointsPerLine())
117 struct ProductInfo_t {
118 using Thruths_t = std::vector<simb::MCTruth>;
119 Thruths_t
const* truths;
122 ProductInfo_t(art::Handle<Thruths_t>
const& handle)
123 : truths(handle.provenance()->isPresent()? handle.product():
nullptr)
126 ProductInfo_t(art::ValidHandle<Thruths_t>
const& handle)
132 std::vector<ProductInfo_t> AllTruths;
136 auto handles =
event.getMany<std::vector<simb::MCTruth>>();
137 std::copy(handles.begin(), handles.end(), std::back_inserter(AllTruths));
140 for (
auto const& inputTag: fInputTruth) {
141 AllTruths.emplace_back
142 (event.getValidHandle<std::vector<simb::MCTruth>>(inputTag));
149 if (AllTruths.empty()) {
150 throw art::Exception(art::errors::ProductNotFound)
151 <<
"No MC truth found to be dumped!\n";
157 unsigned int const nTruths = std::accumulate(
158 AllTruths.begin(), AllTruths.end(), 0U,
159 [](
unsigned int total,
auto const&
info)
160 {
return total + (
info.truths?
info.truths->size(): 0); }
164 mf::LogVerbatim(fOutputCategory) <<
"Event " <<
event.id()
165 <<
" contains " << nTruths <<
" MC truth blocks in "
166 << AllTruths.size() <<
" collections";
168 else if (AllTruths.size() == 1) {
169 mf::LogVerbatim(fOutputCategory) <<
"Event " <<
event.id();
172 mf::LogVerbatim(fOutputCategory) <<
"Dumping " << nTruths
173 <<
" MC truth blocks from " << AllTruths.size()
174 <<
" collections in event " <<
event.id();
180 unsigned int nParticles = 0, nNeutrinos = 0;
181 for (ProductInfo_t
const& truths_info: AllTruths) {
183 auto const* truths = truths_info.truths;
184 std::string productName = truths_info.name;
187 mf::LogVerbatim(fOutputCategory)
188 <<
"Data product '" << productName
189 <<
"' has been dropped. No information available.";
192 if (AllTruths.size() > 1) {
193 mf::LogVerbatim(fOutputCategory)
194 <<
"Data product '" << productName
195 <<
"' contains " << truths->size() <<
" truth blocks:";
197 else if (truths->size() > 1) {
198 mf::LogVerbatim(fOutputCategory)
199 << truths->size() <<
" truth blocks:";
205 unsigned int iTruth = 0;
206 for (
auto const& truth: *truths) {
208 mf::LogVerbatim log (fOutputCategory);
210 if (truths->size() > 1) log <<
"(#" << iTruth <<
") ";
217 nParticles += truth.NParticles();
218 if (truth.NeutrinoSet()) ++nNeutrinos;
227 mf::LogVerbatim(fOutputCategory) << nNeutrinos
228 <<
" neutrinos generated, " << nParticles
229 <<
" generated particles to be simulated downstream.";
235 template <
typename Handle>
237 auto const* prov = handle.provenance();
238 return prov->moduleLabel()
239 +
'_' + prov->productInstanceName()
240 +
'_' + prov->processName()
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.
unsigned int fPointsPerLine
trajectory points per output line
void analyze(art::Event const &event) override
Utility functions to print MC truth information.
art::EDAnalyzer::Table< Config > Parameters
Type to enable module parameters description by art.
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.
BEGIN_PROLOG vertical distance to the surface Name
bool bAllTruth
Whether to process all MCTruth collections.
DumpMCTruth(Parameters const &config)
Configuration-checking constructor.
std::string fOutputCategory
Name of the stream for output.
static std::string productName(Handle const &handle)
Returns the name of the product in the form "module_instance_process".
std::vector< art::InputTag > fInputTruth
Name of MCTruth data products.