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
16 
17 // framework libraries
24 #include "fhiclcpp/types/Atom.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;
45 
47  Name("InputTruth"),
48  Comment("data product with the collection of GENIE truth to be dumped")
49  };
50 
52  Name("OutputCategory"),
53  Comment("name of the output stream (managed by the message facility)"),
54  "DumpGTruth" /* default value */
55  };
56 
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_.
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) {
109  << "'AllowNoTruth' is only allowed if no 'InputTruth' is specified.\n";
110  }
111 }
112 
113 
114 //------------------------------------------------------------------------------
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;
124 
125  ProductInfo_t(art::Handle<Thruths_t> const& handle)
126  : truths(handle.provenance()->isPresent()? handle.product(): nullptr)
127  , name(sim::DumpGTruth::productName(handle))
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) {
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 //------------------------------------------------------------------------------
static QCString name
Definition: declinfo.cpp:673
MaybeLogger_< ELseverityLevel::ELsev_info, true > LogVerbatim
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:378
std::string string
Definition: nybbler.cc:12
ChannelGroupService::Name Name
EDAnalyzer(fhicl::ParameterSet const &pset)
Definition: EDAnalyzer.h:25
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
Utility functions to print MC truth information.
typename config_impl< T >::type Config
Definition: ModuleMacros.h:52
std::string fOutputCategory
Name of the stream for output.
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:67
static Config * config
Definition: config.cpp:1054
ValidHandle< PROD > getValidHandle(InputTag const &tag) const
Definition: DataViewImpl.h:441
bool bAllTruth
Whether to process all GTruth collections.
Code to link reconstructed objects back to the MC truth information.
DumpGTruth(Parameters const &config)
Configuration-checking constructor.
fhicl::OptionalSequence< art::InputTag > InputTruth
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
#define Comment
std::vector< art::InputTag > fInputTruth
Name of GTruth data products.
T copy(T const &v)
fhicl::Atom< bool > AllowNoTruth
fhicl::Atom< std::string > OutputCategory
bool bAllowNoTruth
Whether to forgive when no truth is present.
DumpGTruth & operator=(DumpGTruth const &)=delete
Event finding and building.