All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DumpGTruth_module.cc
Go to the documentation of this file.
1 /**
2  * @file larsim/MCDumpers/DumpGTruth_module.cc
3  * @brief Module dumping GTruth information from GENIE on screen.
4  * @date November 2, 2017
5  * @author Gianluca Petrillo (petrillo@fnal.gov)
6  *
7  * This modules complements `DumpMCTruth` module for GENIE output.
8  *
9  */
10 
11 // LArSoft libraries
12 #include "lardataalg/MCDumpers/MCDumpers.h" // sim::dump namespace
13 
14 // nusimdata libraries
15 #include "nusimdata/SimulationBase/GTruth.h"
16 
17 // framework libraries
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"
27 
28 // C/C++ standard libraries
29 #include <string>
30 #include <iterator> // std::back_inserter()
31 #include <algorithm> // std::copy()
32 #include <numeric> // std::accumulate()
33 
34 namespace sim {
35  class DumpGTruth;
36 }
37 
38 class sim::DumpGTruth: public art::EDAnalyzer {
39  public:
40 
41  /// Collection of configuration parameters for the module
42  struct Config {
43  using Name = fhicl::Name;
44  using Comment = fhicl::Comment;
45 
46  fhicl::OptionalSequence<art::InputTag> InputTruth {
47  Name("InputTruth"),
48  Comment("data product with the collection of GENIE truth to be dumped")
49  };
50 
51  fhicl::Atom<std::string> OutputCategory {
52  Name("OutputCategory"),
53  Comment("name of the output stream (managed by the message facility)"),
54  "DumpGTruth" /* default value */
55  };
56 
57  fhicl::Atom<bool> AllowNoTruth {
58  Name("AllowNoTruth"),
59  Comment("when InputTruth is empty, allow for no truth to be found"),
60  false /* default value */
61  };
62 
63  }; // struct Config
64 
65 
66  /// Type to enable module parameters description by _art_.
67  using Parameters = art::EDAnalyzer::Table<Config>;
68 
69  /// Configuration-checking constructor.
70  explicit DumpGTruth(Parameters const& config);
71 
72  // Plugins should not be copied or assigned.
73  DumpGTruth(DumpGTruth const&) = delete;
74  DumpGTruth(DumpGTruth &&) = delete;
75  DumpGTruth& operator = (DumpGTruth const&) = delete;
76  DumpGTruth& operator = (DumpGTruth &&) = delete;
77 
78 
79  // Operates on the event
80  virtual void analyze(art::Event const& event) override;
81 
82 
83  /// Returns the name of the product in the form `"module_instance_process"`.
84  template <typename Handle>
85  static std::string productName(Handle const& handle);
86 
87  private:
88 
89  std::vector<art::InputTag> fInputTruth; ///< Name of `GTruth` data products.
90  std::string fOutputCategory; ///< Name of the stream for output.
91  bool bAllTruth = false; ///< Whether to process all `GTruth` collections.
92  bool bAllowNoTruth = false; ///< Whether to forgive when no truth is present.
93 
94 }; // class sim::DumpGTruth
95 
96 
97 //------------------------------------------------------------------------------
98 //--- module implementation
99 //------------------------------------------------------------------------------
101  : EDAnalyzer(config)
102  , fInputTruth()
103  , fOutputCategory(config().OutputCategory())
104  , bAllTruth(!config().InputTruth(fInputTruth)) // true if InputTruth omitted
105  , bAllowNoTruth(config().AllowNoTruth())
106 {
107  if (!bAllTruth && bAllowNoTruth) {
108  throw art::Exception(art::errors::Configuration)
109  << "'AllowNoTruth' is only allowed if no 'InputTruth' is specified.\n";
110  }
111 }
112 
113 
114 //------------------------------------------------------------------------------
115 void sim::DumpGTruth::analyze(art::Event const& event) {
116 
117  //
118  // prepare the data products to be dumped
119  //
120  struct ProductInfo_t {
121  using Thruths_t = std::vector<simb::GTruth>;
122  Thruths_t const* truths;
123  std::string name;
124 
125  ProductInfo_t(art::Handle<Thruths_t> const& handle)
126  : truths(handle.provenance()->isPresent()? handle.product(): nullptr)
128  {}
129  ProductInfo_t(art::ValidHandle<Thruths_t> const& handle)
130  : truths(handle.product()), name(sim::DumpGTruth::productName(handle))
131  {}
132 
133  }; // ProductInfo_t
134 
135  std::vector<ProductInfo_t> AllTruths;
136  if (bAllTruth) {
137  //std::vector<art::Handle<std::vector<simb::GTruth>>> handles;
138  //event.getManyByType(handles);
139  auto handles = event.getMany<std::vector<simb::GTruth>>();
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) {
153  throw art::Exception(art::errors::ProductNotFound)
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) {
189  mf::LogVerbatim(fOutputCategory)
190  << "Data product '" << productName
191  << "' has been dropped. No information available.";
192  }
193 
194  if (AllTruths.size() > 1) {
195  mf::LogVerbatim(fOutputCategory)
196  << "Data product '" << productName
197  << "' contains " << truths->size() << " truth blocks:";
198  }
199  else if (truths->size() > 1) {
200  mf::LogVerbatim(fOutputCategory)
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 
210  mf::LogVerbatim log (fOutputCategory);
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 //------------------------------------------------------------------------------
243 DEFINE_ART_MODULE(sim::DumpGTruth)
244 
245 //------------------------------------------------------------------------------
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.
Definition: MCDumpers.h:379
static std::string productName(Handle const &handle)
Returns the name of the product in the form &quot;module_instance_process&quot;.
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.
T copy(T const &v)
then echo fcl name
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