LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
DumpGTruth_module.cc
Go to the documentation of this file.
1 
11 // LArSoft libraries
12 #include "lardataalg/MCDumpers/MCDumpers.h" // sim::dump namespace
13 
14 // nusimdata libraries
16 
17 // framework libraries
24 #include "fhiclcpp/types/Atom.h"
27 
28 // C/C++ standard libraries
29 #include <algorithm> // std::copy()
30 #include <iterator> // std::back_inserter()
31 #include <numeric> // std::accumulate()
32 #include <string>
33 
34 namespace sim {
35  class DumpGTruth;
36 }
37 
39 public:
41  struct Config {
42  using Name = fhicl::Name;
44 
46  Name("InputTruth"),
47  Comment("data product with the collection of GENIE truth to be dumped")};
48 
50  Name("OutputCategory"),
51  Comment("name of the output stream (managed by the message facility)"),
52  "DumpGTruth" /* default value */
53  };
54 
56  Name("AllowNoTruth"),
57  Comment("when InputTruth is empty, allow for no truth to be found"),
58  false /* default value */
59  };
60 
61  }; // struct Config
62 
65 
67  explicit DumpGTruth(Parameters const& config);
68 
69  // Plugins should not be copied or assigned.
70  DumpGTruth(DumpGTruth const&) = delete;
71  DumpGTruth(DumpGTruth&&) = delete;
72  DumpGTruth& operator=(DumpGTruth const&) = delete;
73  DumpGTruth& operator=(DumpGTruth&&) = delete;
74 
75  // Operates on the event
76  virtual void analyze(art::Event const& event) override;
77 
79  template <typename Handle>
80  static std::string productName(Handle const& handle);
81 
82 private:
83  std::vector<art::InputTag> fInputTruth;
84  std::string fOutputCategory;
85  bool bAllTruth = false;
86  bool bAllowNoTruth = false;
87 
88 }; // class sim::DumpGTruth
89 
90 //------------------------------------------------------------------------------
91 //--- module implementation
92 //------------------------------------------------------------------------------
94  : EDAnalyzer(config)
95  , fInputTruth()
96  , fOutputCategory(config().OutputCategory())
97  , bAllTruth(!config().InputTruth(fInputTruth)) // true if InputTruth omitted
98  , bAllowNoTruth(config().AllowNoTruth())
99 {
100  if (!bAllTruth && bAllowNoTruth) {
102  << "'AllowNoTruth' is only allowed if no 'InputTruth' is specified.\n";
103  }
104 }
105 
106 //------------------------------------------------------------------------------
108 {
109 
110  //
111  // prepare the data products to be dumped
112  //
113  struct ProductInfo_t {
114  using Thruths_t = std::vector<simb::GTruth>;
115  Thruths_t const* truths;
116  std::string name;
117 
118  ProductInfo_t(art::Handle<Thruths_t> const& handle)
119  : truths(handle.provenance()->isPresent() ? handle.product() : nullptr)
120  , name(sim::DumpGTruth::productName(handle))
121  {}
122  ProductInfo_t(art::ValidHandle<Thruths_t> const& handle)
123  : truths(handle.product()), name(sim::DumpGTruth::productName(handle))
124  {}
125 
126  }; // ProductInfo_t
127 
128  std::vector<ProductInfo_t> AllTruths;
129  if (bAllTruth) {
130  //std::vector<art::Handle<std::vector<simb::GTruth>>> handles;
131  //event.getManyByType(handles);
132  auto handles = event.getMany<std::vector<simb::GTruth>>();
133  std::copy(handles.begin(), handles.end(), std::back_inserter(AllTruths));
134  }
135  else {
136  for (auto const& inputTag : fInputTruth) {
137  AllTruths.emplace_back(event.getValidHandle<std::vector<simb::GTruth>>(inputTag));
138  } // for
139  }
140 
141  //
142  // sanity check
143  //
144  if (AllTruths.empty() && !bAllowNoTruth) {
145  throw art::Exception(art::errors::ProductNotFound) << "No GENIE truth found to be dumped!\n";
146  }
147 
148  //
149  // print an introduction
150  //
151  unsigned int const nTruths = std::accumulate(
152  AllTruths.begin(), AllTruths.end(), 0U, [](unsigned int total, auto const& info) {
153  return total + (info.truths ? info.truths->size() : 0);
154  });
155 
156  if (bAllTruth) {
158  << "Event " << event.id() << " contains " << nTruths << " GENIE truth blocks in "
159  << AllTruths.size() << " collections";
160  }
161  else if (AllTruths.size() == 1) {
162  mf::LogVerbatim(fOutputCategory) << "Event " << event.id();
163  }
164  else {
165  mf::LogVerbatim(fOutputCategory) << "Dumping " << nTruths << " GENIE truth blocks from "
166  << AllTruths.size() << " collections in event " << event.id();
167  }
168 
169  //
170  // dump data product by data product
171  //
172  for (ProductInfo_t const& truths_info : AllTruths) {
173 
174  auto const* truths = truths_info.truths;
175  std::string productName = truths_info.name;
176 
177  if (!truths) {
179  << "Data product '" << productName << "' has been dropped. No information available.";
180  }
181 
182  if (AllTruths.size() > 1) {
184  << "Data product '" << productName << "' contains " << truths->size() << " truth blocks:";
185  }
186  else if (truths->size() > 1) {
187  mf::LogVerbatim(fOutputCategory) << truths->size() << " truth blocks:";
188  }
189 
190  //
191  // dump each GENIE truth in the data product
192  //
193  unsigned int iTruth = 0;
194  for (auto const& truth : *truths) {
195 
197 
198  if (truths->size() > 1) log << "(#" << iTruth << ") ";
199  sim::dump::DumpGTruth(log, truth, " ", "");
200 
201  //
202  // update counters
203  //
204  ++iTruth;
205 
206  } // for each truth in data product
207 
208  } // for truth data products
209 
210  //
211  // all done
212  //
213 
214 } // sim::DumpGTruth::analyze()
215 
216 //------------------------------------------------------------------------------
217 template <typename Handle>
218 std::string sim::DumpGTruth::productName(Handle const& handle)
219 {
220  auto const* prov = handle.provenance();
221  return prov->moduleLabel() + '_' + prov->productInstanceName() + '_' + prov->processName();
222 } // sim::DumpGTruth::productName()
223 
224 //------------------------------------------------------------------------------
226 
227 //------------------------------------------------------------------------------
MaybeLogger_< ELseverityLevel::ELsev_info, true > LogVerbatim
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.
Definition: MCDumpers.h:377
EDAnalyzer(fhicl::ParameterSet const &pset)
Definition: EDAnalyzer.cc:6
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
T const * product() const
Definition: Handle.h:363
std::string fOutputCategory
Name of the stream for output.
T const * product() const
Definition: Handle.h:174
Utility functions to print MC truth information.
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:65
Provenance const * provenance() const
Definition: Handle.h:217
bool bAllTruth
Whether to process all GTruth collections.
fhicl::Atom< std::string > OutputCategory
Monte Carlo Simulation.
DumpGTruth(Parameters const &config)
Configuration-checking constructor.
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
ValidHandle< PROD > getValidHandle(InputTag const &tag) const
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
Event finding and building.