DumpGTruth_module.cc
Go to the documentation of this file.
1 /**
2  * @file garsoft/Utilities/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 #include "MCDumpers.h" // sim::dump namespace
12 
13 // nutools libraries
15 
16 // framework libraries
23 #include "fhiclcpp/types/Atom.h"
26 
27 // C/C++ standard libraries
28 #include <vector>
29 #include <string>
30 #include <iterator> // std::back_inserter()
31 #include <algorithm> // std::copy()
32 #include <utility> // std::forward()
33 #include <numeric> // std::accumulate()
34 
35 namespace sim {
36  class DumpGTruth;
37 }
38 
40  public:
41 
42  /// Collection of configuration parameters for the module
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 
67  /// Type to enable module parameters description by _art_.
69 
70  /// Configuration-checking constructor.
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 
84  /// Returns the name of the product in the form `"module_instance_process"`.
85  template <typename Handle>
86  static std::string productName(Handle const& handle);
87 
88  private:
89 
90  std::vector<art::InputTag> fInputTruth; ///< Name of `GTruth` data products.
91  std::string fOutputCategory; ///< Name of the stream for output.
92  bool bAllTruth = false; ///< Whether to process all `GTruth` collections.
93  bool bAllowNoTruth = false; ///< Whether to forgive when no truth is present.
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;
125 
126  ProductInfo_t(art::Handle<Thruths_t> const& handle)
127  : truths(handle.provenance()->isPresent()? handle.product(): nullptr)
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  auto handles = event.getMany<std::vector<simb::GTruth> >();
139  std::copy(handles.begin(), handles.end(), std::back_inserter(AllTruths));
140  }
141  else {
142  for (auto const& inputTag: fInputTruth) {
143  AllTruths.emplace_back
144  (event.getValidHandle<std::vector<simb::GTruth>>(inputTag));
145  } // for
146  }
147 
148  //
149  // sanity check
150  //
151  if (AllTruths.empty() && !bAllowNoTruth) {
153  << "No GENIE truth found to be dumped!\n";
154  }
155 
156  //
157  // print an introduction
158  //
159  unsigned int const nTruths = std::accumulate(
160  AllTruths.begin(), AllTruths.end(), 0U,
161  [](unsigned int total, auto const& info)
162  { return total + (info.truths? info.truths->size(): 0); }
163  );
164 
165  if (bAllTruth) {
166  mf::LogVerbatim(fOutputCategory) << "Event " << event.id()
167  << " contains " << nTruths << " GENIE truth blocks in "
168  << AllTruths.size() << " collections";
169  }
170  else if (AllTruths.size() == 1) {
171  mf::LogVerbatim(fOutputCategory) << "Event " << event.id();
172  }
173  else {
174  mf::LogVerbatim(fOutputCategory) << "Dumping " << nTruths
175  << " GENIE truth blocks from " << AllTruths.size()
176  << " collections in event " << event.id();
177  }
178 
179  //
180  // dump data product by data product
181  //
182  for (ProductInfo_t const& truths_info: AllTruths) {
183 
184  auto const* truths = truths_info.truths;
185  std::string productName = truths_info.name;
186 
187  if (!truths) {
189  << "Data product '" << productName
190  << "' has been dropped. No information available.";
191  }
192 
193  if (AllTruths.size() > 1) {
195  << "Data product '" << productName
196  << "' contains " << truths->size() << " truth blocks:";
197  }
198  else if (truths->size() > 1) {
200  << truths->size() << " truth blocks:";
201  }
202 
203  //
204  // dump each GENIE truth in the data product
205  //
206  unsigned int iTruth = 0;
207  for (auto const& truth: *truths) {
208 
210 
211  if (truths->size() > 1) log << "(#" << iTruth << ") ";
212  sim::dump::DumpGTruth(log, truth, " ", "");
213 
214  //
215  // update counters
216  //
217  ++iTruth;
218 
219  } // for each truth in data product
220 
221  } // for truth data products
222 
223  //
224  // all done
225  //
226 
227 } // sim::DumpGTruth::analyze()
228 
229 
230 //------------------------------------------------------------------------------
231 template <typename Handle>
233  auto const* prov = handle.provenance();
234  return prov->moduleLabel()
235  + '_' + prov->productInstanceName()
236  + '_' + prov->processName()
237  ;
238 } // sim::DumpGTruth::productName()
239 
240 
241 //------------------------------------------------------------------------------
243 
244 //------------------------------------------------------------------------------
static QCString name
Definition: declinfo.cpp:673
MaybeLogger_< ELseverityLevel::ELsev_info, true > LogVerbatim
Collection of configuration parameters for the module.
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
T const * product() const
Definition: Handle.h:323
std::string fOutputCategory
Name of the stream for output.
T const * product() const
Definition: Handle.h:163
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:67
static Config * config
Definition: config.cpp:1054
Provenance const * provenance() const
Definition: Handle.h:205
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.