DumpMCTruth_module.cc
Go to the documentation of this file.
1 /**
2  * @file DumpMCTruth_module.cc
3  * @brief Module dumping MCTruth information on screen.
4  * @date February 22nd, 2017
5  * @author Gianluca Petrillo (petrillo@fnal.gov)
6  *
7  * Dedication: to the memory of DumpMCTruth Sr., whose untimely departure by a
8  * rogue `rm -R` deprived us of a useful tool, and the author of a good deal of
9  * good karma.
10  *
11  */
12 
13 #include <numeric> // std::accumulate
14 #include "MCDumpers.h" // sim::dump namespace
15 
16 // nutools libraries
19 
20 // framework libraries
27 #include "fhiclcpp/ParameterSet.h"
28 #include "fhiclcpp/types/Atom.h"
31 
32 namespace sim {
33  class DumpMCTruth;
34 }
35 
36 
38  public:
39 
40  /// Collection of configuration parameters for the module
41  struct Config {
42  using Name = fhicl::Name;
44 
46  Name("InputTruth"),
47  Comment("data product with the collection of MC truth to be dumped")
48  };
49 
51  Name("OutputCategory"),
52  Comment("name of the output stream (managed by the message facility)"),
53  "DumpMCTruth" /* default value */
54  };
55 
57  Name("PointsPerLine"),
58  Comment("trajectory points printed per line (default: 2; 0 = skip them)"),
59  2 /* default value */
60  };
61 
62  }; // struct Config
63 
64  /// Type to enable module parameters description by _art_.
66 
67  /// Configuration-checking constructor.
68  explicit DumpMCTruth(Parameters const& config);
69 
70  // Plugins should not be copied or assigned.
71  DumpMCTruth(DumpMCTruth const&) = delete;
72  DumpMCTruth(DumpMCTruth &&) = delete;
73  DumpMCTruth& operator = (DumpMCTruth const&) = delete;
74  DumpMCTruth& operator = (DumpMCTruth &&) = delete;
75 
76 
77  // Operates on the event
78  void analyze(art::Event const& event) override;
79 
80 
81  /// Returns the name of the product in the form `"module_instance_process"`.
82  template <typename Handle>
83  static std::string productName(Handle const& handle);
84 
85 
86  private:
87 
88  std::vector<art::InputTag> fInputTruth; ///< Name of MCTruth data products.
89  std::string fOutputCategory; ///< Name of the stream for output.
90  bool bAllTruth = false; ///< Whether to process all MCTruth collections.
91 
92  unsigned int fPointsPerLine; ///< trajectory points per output line
93 
94 
95 }; // class sim::DumpMCTruth
96 
97 
98 //------------------------------------------------------------------------------
99 //--- module implementation
100 //---
101 //------------------------------------------------------------------------------
103  : EDAnalyzer(config)
104  , fInputTruth()
105  , fOutputCategory(config().OutputCategory())
106  , bAllTruth(!config().InputTruth(fInputTruth)) // true if InputTruth omitted
107  , fPointsPerLine(config().PointsPerLine())
108  {}
109 
110 
111 //------------------------------------------------------------------------------
113 
114  //
115  // prepare the data products to be dumped
116  //
117  struct ProductInfo_t {
118  using Thruths_t = std::vector<simb::MCTruth>;
119  Thruths_t const* truths;
121 
122  ProductInfo_t(art::Handle<Thruths_t> const& handle)
123  : truths(handle.provenance()->isPresent()? handle.product(): nullptr)
125  {}
126  ProductInfo_t(art::ValidHandle<Thruths_t> const& handle)
127  : truths(handle.product()), name(sim::DumpMCTruth::productName(handle))
128  {}
129 
130  }; // ProductInfo_t
131 
132  std::vector<ProductInfo_t> AllTruths;
133  if (bAllTruth) {
134  auto handles = event.getMany<std::vector<simb::MCTruth> >();
135  std::copy(handles.begin(), handles.end(), std::back_inserter(AllTruths));
136  }
137  else {
138  for (auto const& inputTag: fInputTruth) {
139  AllTruths.emplace_back
140  (event.getValidHandle<std::vector<simb::MCTruth>>(inputTag));
141  } // for
142  }
143 
144  //
145  // sanity check
146  //
147  if (AllTruths.empty()) {
149  << "No MC truth found to be dumped!\n";
150  }
151 
152  //
153  // print an introduction
154  //
155  unsigned int const nTruths = std::accumulate(
156  AllTruths.begin(), AllTruths.end(), 0U,
157  [](unsigned int total, auto const& info)
158  { return total + (info.truths? info.truths->size(): 0); }
159  );
160 
161  if (bAllTruth) {
162  mf::LogVerbatim(fOutputCategory) << "Event " << event.id()
163  << " contains " << nTruths << " MC truth blocks in "
164  << AllTruths.size() << " collections";
165  }
166  else if (AllTruths.size() == 1) {
167  mf::LogVerbatim(fOutputCategory) << "Event " << event.id();
168  }
169  else {
170  mf::LogVerbatim(fOutputCategory) << "Dumping " << nTruths
171  << " MC truth blocks from " << AllTruths.size()
172  << " collections in event " << event.id();
173  }
174 
175  //
176  // dump data product by data product
177  //
178  unsigned int nParticles = 0, nNeutrinos = 0;
179  for (ProductInfo_t const& truths_info: AllTruths) {
180 
181  auto const* truths = truths_info.truths;
182  std::string productName = truths_info.name;
183 
184  if (!truths) {
186  << "Data product '" << productName
187  << "' has been dropped. No information available.";
188  }
189 
190  if (AllTruths.size() > 1) {
192  << "Data product '" << productName
193  << "' contains " << truths->size() << " truth blocks:";
194  }
195  else if (truths->size() > 1) {
197  << truths->size() << " truth blocks:";
198  }
199 
200  //
201  // dump each MC truth in the data product
202  //
203  unsigned int iTruth = 0;
204  for (auto const& truth: *truths) {
205 
207 
208  if (truths->size() > 1) log << "(#" << iTruth << ") ";
209  sim::dump::DumpMCTruth(log, truth, " ", "");
210 
211  //
212  // update counters
213  //
214  ++iTruth;
215  nParticles += truth.NParticles();
216  if (truth.NeutrinoSet()) ++nNeutrinos;
217 
218  } // for each truth in data product
219 
220  } // for truth data products
221 
222  //
223  // all done
224  //
225  mf::LogVerbatim(fOutputCategory) << nNeutrinos
226  << " neutrinos generated, " << nParticles
227  << " generated particles to be simulated downstream.";
228 
229 } // sim::DumpMCTruth::analyze()
230 
231 
232 //------------------------------------------------------------------------------
233 template <typename Handle>
235  auto const* prov = handle.provenance();
236  return prov->moduleLabel()
237  + '_' + prov->productInstanceName()
238  + '_' + prov->processName()
239  ;
240 } // sim::DumpMCTruth::productName()
241 
242 
243 //------------------------------------------------------------------------------
245 
246 //------------------------------------------------------------------------------
static QCString name
Definition: declinfo.cpp:673
MaybeLogger_< ELseverityLevel::ELsev_info, true > LogVerbatim
DumpMCTruth & operator=(DumpMCTruth const &)=delete
std::string string
Definition: nybbler.cc:12
fhicl::OptionalSequence< art::InputTag > InputTruth
ChannelGroupService::Name Name
Collection of configuration parameters for the module.
EDAnalyzer(fhicl::ParameterSet const &pset)
Definition: EDAnalyzer.h:25
Particle class.
unsigned int fPointsPerLine
trajectory points per output line
T const * product() const
Definition: Handle.h:323
void analyze(art::Event const &event) override
fhicl::Atom< unsigned int > PointsPerLine
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
void DumpMCTruth(Stream &&out, simb::MCTruth const &truth, unsigned int pointsPerLine, std::string indent, std::string firstIndent)
Dumps the content of the specified MC truth in the output stream.
Definition: MCDumpers.h:345
ValidHandle< PROD > getValidHandle(InputTag const &tag) const
Definition: DataViewImpl.h:441
bool bAllTruth
Whether to process all MCTruth collections.
DumpMCTruth(Parameters const &config)
Configuration-checking constructor.
Code to link reconstructed objects back to the MC truth information.
std::vector< art::InputTag > fInputTruth
Name of MCTruth data products.
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
std::string fOutputCategory
Name of the stream for output.
fhicl::Atom< std::string > OutputCategory
#define Comment
T copy(T const &v)
static std::string productName(Handle const &handle)
Returns the name of the product in the form "module_instance_process".
Event finding and building.