15 #include "nusimdata/SimulationBase/GTruth.h"
18 #include "art/Framework/Core/EDAnalyzer.h"
19 #include "art/Framework/Core/ModuleMacros.h"
20 #include "art/Framework/Principal/Event.h"
21 #include "art/Framework/Principal/Handle.h"
22 #include "art/Framework/Principal/Provenance.h"
23 #include "canvas/Utilities/InputTag.h"
24 #include "fhiclcpp/types/Atom.h"
25 #include "fhiclcpp/types/OptionalSequence.h"
26 #include "messagefacility/MessageLogger/MessageLogger.h"
48 Comment(
"data product with the collection of GENIE truth to be dumped")
52 Name(
"OutputCategory"),
53 Comment(
"name of the output stream (managed by the message facility)"),
59 Comment(
"when InputTruth is empty, allow for no truth to be found"),
80 virtual void analyze(art::Event
const& event)
override;
84 template <
typename Handle>
85 static std::string
productName(Handle
const& handle);
103 , fOutputCategory(config().OutputCategory())
104 , bAllTruth(!config().InputTruth(fInputTruth))
105 , bAllowNoTruth(config().AllowNoTruth())
108 throw art::Exception(art::errors::Configuration)
109 <<
"'AllowNoTruth' is only allowed if no 'InputTruth' is specified.\n";
120 struct ProductInfo_t {
121 using Thruths_t = std::vector<simb::GTruth>;
122 Thruths_t
const* truths;
125 ProductInfo_t(art::Handle<Thruths_t>
const& handle)
126 : truths(handle.provenance()->isPresent()? handle.product():
nullptr)
129 ProductInfo_t(art::ValidHandle<Thruths_t>
const& handle)
135 std::vector<ProductInfo_t> AllTruths;
139 auto handles =
event.getMany<std::vector<simb::GTruth>>();
140 std::copy(handles.begin(), handles.end(), std::back_inserter(AllTruths));
143 for (
auto const& inputTag: fInputTruth) {
144 AllTruths.emplace_back
145 (event.getValidHandle<std::vector<simb::GTruth>>(inputTag));
152 if (AllTruths.empty() && !bAllowNoTruth) {
153 throw art::Exception(art::errors::ProductNotFound)
154 <<
"No GENIE truth found to be dumped!\n";
160 unsigned int const nTruths = std::accumulate(
161 AllTruths.begin(), AllTruths.end(), 0U,
162 [](
unsigned int total,
auto const&
info)
163 {
return total + (
info.truths?
info.truths->size(): 0); }
167 mf::LogVerbatim(fOutputCategory) <<
"Event " <<
event.id()
168 <<
" contains " << nTruths <<
" GENIE truth blocks in "
169 << AllTruths.size() <<
" collections";
171 else if (AllTruths.size() == 1) {
172 mf::LogVerbatim(fOutputCategory) <<
"Event " <<
event.id();
175 mf::LogVerbatim(fOutputCategory) <<
"Dumping " << nTruths
176 <<
" GENIE truth blocks from " << AllTruths.size()
177 <<
" collections in event " <<
event.id();
183 for (ProductInfo_t
const& truths_info: AllTruths) {
185 auto const* truths = truths_info.truths;
186 std::string productName = truths_info.name;
189 mf::LogVerbatim(fOutputCategory)
190 <<
"Data product '" << productName
191 <<
"' has been dropped. No information available.";
194 if (AllTruths.size() > 1) {
195 mf::LogVerbatim(fOutputCategory)
196 <<
"Data product '" << productName
197 <<
"' contains " << truths->size() <<
" truth blocks:";
199 else if (truths->size() > 1) {
200 mf::LogVerbatim(fOutputCategory)
201 << truths->size() <<
" truth blocks:";
207 unsigned int iTruth = 0;
208 for (
auto const& truth: *truths) {
210 mf::LogVerbatim log (fOutputCategory);
212 if (truths->size() > 1) log <<
"(#" << iTruth <<
") ";
232 template <
typename Handle>
234 auto const* prov = handle.provenance();
235 return prov->moduleLabel()
236 +
'_' + prov->productInstanceName()
237 +
'_' + prov->processName()
art::EDAnalyzer::Table< Config > Parameters
Type to enable module parameters description by art.
Collection of configuration parameters for the module.
fhicl::OptionalSequence< art::InputTag > InputTruth
void DumpGTruth(Stream &&out, simb::GTruth const &truth, std::string indent, std::string firstIndent)
Dumps the content of the GENIE truth in the output stream.
static std::string productName(Handle const &handle)
Returns the name of the product in the form "module_instance_process".
virtual void analyze(art::Event const &event) override
std::string fOutputCategory
Name of the stream for output.
Utility functions to print MC truth information.
BEGIN_PROLOG vertical distance to the surface Name
bool bAllTruth
Whether to process all GTruth collections.
fhicl::Atom< std::string > OutputCategory
DumpGTruth(Parameters const &config)
Configuration-checking constructor.
fhicl::Atom< bool > AllowNoTruth
std::vector< art::InputTag > fInputTruth
Name of GTruth data products.
bool bAllowNoTruth
Whether to forgive when no truth is present.
DumpGTruth & operator=(DumpGTruth const &)=delete