LArSoft  v06_85_00
Liquid Argon Software toolkit - http://larsoft.org/
DumpGTruth_module.cc
Go to the documentation of this file.
1 
11 // LArSoft libraries
12 #include "larsim/MCDumpers/MCDumpers.h" // sim::dump namespace
13 
14 // nutools libraries
16 
17 // framework libraries
24 #include "fhiclcpp/types/Atom.h"
27 
28 // C/C++ standard libraries
29 #include <vector>
30 #include <string>
31 #include <iterator> // std::back_inserter()
32 #include <algorithm> // std::copy()
33 #include <utility> // std::forward()
34 
35 namespace sim {
36  class DumpGTruth;
37 }
38 
40  public:
41 
43  struct Config {
44  using Name = fhicl::Name;
46 
48  Name("InputTruth"),
49  Comment("data product with the collection of GENIE truth to be dumped")
50  };
51 
53  Name("OutputCategory"),
54  Comment("name of the output stream (managed by the message facility)"),
55  "DumpGTruth" /* default value */
56  };
57 
59  Name("AllowNoTruth"),
60  Comment("when InputTruth is empty, allow for no truth to be found"),
61  false /* default value */
62  };
63 
64  }; // struct Config
65 
66 
69 
71  explicit DumpGTruth(Parameters const& config);
72 
73  // Plugins should not be copied or assigned.
74  DumpGTruth(DumpGTruth const&) = delete;
75  DumpGTruth(DumpGTruth &&) = delete;
76  DumpGTruth& operator = (DumpGTruth const&) = delete;
77  DumpGTruth& operator = (DumpGTruth &&) = delete;
78 
79 
80  // Operates on the event
81  virtual void analyze(art::Event const& event) override;
82 
83 
85  template <typename Handle>
86  static std::string productName(Handle const& handle);
87 
88  private:
89 
90  std::vector<art::InputTag> fInputTruth;
91  std::string fOutputCategory;
92  bool bAllTruth = false;
93  bool bAllowNoTruth = false;
94 
95 }; // class sim::DumpGTruth
96 
97 
98 //------------------------------------------------------------------------------
99 //--- module implementation
100 //------------------------------------------------------------------------------
102  : EDAnalyzer(config)
103  , fInputTruth()
104  , fOutputCategory(config().OutputCategory())
105  , bAllTruth(!config().InputTruth(fInputTruth)) // true if InputTruth omitted
106  , bAllowNoTruth(config().AllowNoTruth())
107 {
108  if (!bAllTruth && bAllowNoTruth) {
110  << "'AllowNoTruth' is only allowed if no 'InputTruth' is specified.\n";
111  }
112 }
113 
114 
115 //------------------------------------------------------------------------------
117 
118  //
119  // prepare the data products to be dumped
120  //
121  struct ProductInfo_t {
122  using Thruths_t = std::vector<simb::GTruth>;
123  Thruths_t const* truths;
124  std::string name;
125 
126  ProductInfo_t(art::Handle<Thruths_t> const& handle)
127  : truths(handle.provenance()->isPresent()? handle.product(): nullptr)
128  , name(sim::DumpGTruth::productName(handle))
129  {}
130  ProductInfo_t(art::ValidHandle<Thruths_t> const& handle)
131  : truths(handle.product()), name(sim::DumpGTruth::productName(handle))
132  {}
133 
134  }; // ProductInfo_t
135 
136  std::vector<ProductInfo_t> AllTruths;
137  if (bAllTruth) {
138  std::vector<art::Handle<std::vector<simb::GTruth>>> handles;
139  event.getManyByType(handles);
140  std::copy(handles.begin(), handles.end(), std::back_inserter(AllTruths));
141  }
142  else {
143  for (auto const& inputTag: fInputTruth) {
144  AllTruths.emplace_back
145  (event.getValidHandle<std::vector<simb::GTruth>>(inputTag));
146  } // for
147  }
148 
149  //
150  // sanity check
151  //
152  if (AllTruths.empty() && !bAllowNoTruth) {
154  << "No GENIE truth found to be dumped!\n";
155  }
156 
157  //
158  // print an introduction
159  //
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); }
164  );
165 
166  if (bAllTruth) {
167  mf::LogVerbatim(fOutputCategory) << "Event " << event.id()
168  << " contains " << nTruths << " GENIE truth blocks in "
169  << AllTruths.size() << " collections";
170  }
171  else if (AllTruths.size() == 1) {
172  mf::LogVerbatim(fOutputCategory) << "Event " << event.id();
173  }
174  else {
175  mf::LogVerbatim(fOutputCategory) << "Dumping " << nTruths
176  << " GENIE truth blocks from " << AllTruths.size()
177  << " collections in event " << event.id();
178  }
179 
180  //
181  // dump data product by data product
182  //
183  for (ProductInfo_t const& truths_info: AllTruths) {
184 
185  auto const* truths = truths_info.truths;
186  std::string productName = truths_info.name;
187 
188  if (!truths) {
190  << "Data product '" << productName
191  << "' has been dropped. No information available.";
192  }
193 
194  if (AllTruths.size() > 1) {
196  << "Data product '" << productName
197  << "' contains " << truths->size() << " truth blocks:";
198  }
199  else if (truths->size() > 1) {
201  << truths->size() << " truth blocks:";
202  }
203 
204  //
205  // dump each GENIE truth in the data product
206  //
207  unsigned int iTruth = 0;
208  for (auto const& truth: *truths) {
209 
211 
212  if (truths->size() > 1) log << "(#" << iTruth << ") ";
213  sim::dump::DumpGTruth(log, truth, " ", "");
214 
215  //
216  // update counters
217  //
218  ++iTruth;
219 
220  } // for each truth in data product
221 
222  } // for truth data products
223 
224  //
225  // all done
226  //
227 
228 } // sim::DumpGTruth::analyze()
229 
230 
231 //------------------------------------------------------------------------------
232 template <typename Handle>
233 std::string sim::DumpGTruth::productName(Handle const& handle) {
234  auto const* prov = handle.provenance();
235  return prov->moduleLabel()
236  + '_' + prov->productInstanceName()
237  + '_' + prov->processName()
238  ;
239 } // sim::DumpGTruth::productName()
240 
241 
242 //------------------------------------------------------------------------------
244 
245 //------------------------------------------------------------------------------
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:379
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:321
std::string fOutputCategory
Name of the stream for output.
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
bool bAllTruth
Whether to process all GTruth collections.
fhicl::Atom< std::string > OutputCategory
EDAnalyzer(Table< Config > const &config)
Definition: EDAnalyzer.h:100
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.