LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
EventWeight_module.cc
Go to the documentation of this file.
1 // Class: EventWeight
3 // Module Type: producer
4 // File: EventWeight_module.cc
5 //
6 // Generated at Fri Mar 20 09:36:11 2015 by Zarko Pavlovic using artmod
7 // from cetpkgsupport v1_08_04.
8 //
9 // Ported from uboonecode to larsim on Feb 14 2018 by Marco Del Tutto
11 
17 #include "fhiclcpp/ParameterSet.h"
19 
20 #include <iomanip>
21 #include <iostream>
22 #include <memory>
23 
27 
30 
31 namespace evwgh {
32 
33  class EventWeight : public art::EDProducer {
34 
35  public:
36  explicit EventWeight(fhicl::ParameterSet const& p);
37  // The destructor generated by the compiler is fine for classes
38  // without bare pointers or other resource use.
39 
40  // Plugins should not be copied or assigned.
41  EventWeight(EventWeight const&) = delete;
42  EventWeight(EventWeight&&) = delete;
43  EventWeight& operator=(EventWeight const&) = delete;
44  EventWeight& operator=(EventWeight&&) = delete;
45 
46  private:
47  // Required functions.
48  void produce(art::Event& e) override;
49 
50  //Optional functions.
51  void endJob() override;
52 
54  std::string fGenieModuleLabel;
55  };
56 
58  : EDProducer{p}, fGenieModuleLabel{p.get<std::string>("genie_module_label", "generator")}
59  {
60  // Configure the appropriate GENIE tune if needed (important for v3+ only)
61  // NOTE: In all normal use cases, relying on the ${GENIE_XSEC_TUNE}
62  // environment variable set by the genie_xsec package should be sufficient.
63  // Only include the "TuneName" FHiCL parameter for EventWeight if you
64  // really know what you're doing! The same goes for the
65  // "EventGeneratorList" parameter.
66  std::string genie_tune_name = p.get<std::string>("TuneName", "${GENIE_XSEC_TUNE}");
67 
68  // The default empty string used here will cause the subsequent call to
69  // evgb::SetEventGeneratorListAndTune() to leave GENIE's current event
70  // generator list name (probably "Default") alone
71  std::string evgen_list_name = p.get<std::string>("EventGeneratorList", "");
72 
73  // Tell GENIE about the event generator list and tune
74  evgb::SetEventGeneratorListAndTune(evgen_list_name, genie_tune_name);
75 
76  auto const n_func = _wgt_manager.Configure(
77  p, [this](std::string const& type, std::string const& instance) -> CLHEP::HepRandomEngine& {
78  return createEngine(0, type, instance);
79  });
80  if (n_func > 0) produces<std::vector<MCEventWeight>>();
81  }
82 
84  {
85  // Implementation of required member function here.
86  auto mcwghvec = std::make_unique<std::vector<MCEventWeight>>();
87 
88  // Get the MC generator information out of the event
89  // these are all handles to mc information.
90  std::vector<art::Ptr<simb::MCTruth>> mclist;
91 
92  // Actually go and get the stuff
93  auto const mcTruthHandle = e.getValidHandle<std::vector<simb::MCTruth>>(fGenieModuleLabel);
94  art::fill_ptr_vector(mclist, mcTruthHandle);
95 
96  // Loop over all neutrinos in this event
97  for (unsigned int inu = 0; inu < mclist.size(); ++inu) {
98  auto const mcwgh = _wgt_manager.Run(e, inu);
99  mcwghvec->push_back(mcwgh);
100  }
101 
102  e.put(std::move(mcwghvec));
103  }
104 
106  {
107  // Get the map from sting to Weight_t from the manager
108  std::map<std::string, Weight_t*> weightCalcMap = _wgt_manager.GetWeightCalcMap();
109 
110  std::stringstream job_summary;
111  job_summary << std::setprecision(2);
112  for (int i = 1; i <= 110; i++)
113  job_summary << "=";
114  job_summary << std::endl;
115  job_summary << std::setw(20) << "WeightCalc" << std::setw(15) << "Type" << std::setw(15)
116  << "#RW neutrinos" << std::setw(15) << "#Multisims" << std::setw(15) << "Min"
117  << std::setw(15) << "Max" << std::setw(15) << "Avg" << std::endl;
118  for (int i = 1; i <= 110; i++)
119  job_summary << "=";
120  job_summary << std::endl;
121  for (auto it = weightCalcMap.begin(); it != weightCalcMap.end(); it++) {
122  job_summary << std::setw(20) << it->first << std::setw(15) << (it->second->fWeightCalcType)
123  << std::setw(15) << (it->second->fNcalls) << std::setw(15)
124  << (it->second->fNmultisims) << std::setw(15) << (it->second->fMinWeight)
125  << std::setw(15) << (it->second->fMaxWeight) << std::setw(15)
126  << (it->second->fAvgWeight) << std::endl;
127  }
128  for (int i = 1; i <= 110; i++)
129  job_summary << "=";
130  job_summary << std::endl;
131  mf::LogInfo("") << job_summary.str();
132  }
133 
134 } // namespace
135 
void produce(art::Event &e) override
base_engine_t & createEngine(seed_t seed)
MaybeLogger_< ELseverityLevel::ELsev_info, false > LogInfo
EDProducer(fhicl::ParameterSet const &pset)
Definition: EDProducer.cc:6
std::string fGenieModuleLabel
Allows to interface to EventWeight calculators.
const std::string instance
EventWeight & operator=(EventWeight const &)=delete
Functions for transforming GENIE objects into ART objects (and back)
PutHandle< PROD > put(std::unique_ptr< PROD > &&edp, std::string const &instance={})
Definition: Event.h:77
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:65
void endJob() override
std::map< std::string, Weight_t * > GetWeightCalcMap()
Returns the map between calculator name and Weight_t product.
Definition: WeightManager.h:63
WeightManager _wgt_manager
EventWeight(fhicl::ParameterSet const &p)
ValidHandle< PROD > getValidHandle(InputTag const &tag) const
void SetEventGeneratorListAndTune(const std::string &evtlistname="", const std::string &tunename="${GENIE_XSEC_TUNE}")
Definition: GENIE2ART.cxx:128
void fill_ptr_vector(std::vector< Ptr< T >> &ptrs, H const &h)
Definition: Ptr.h:306
Float_t e
Definition: plot.C:35
MCEventWeight Run(art::Event &e, const int inu)
Core function (previous call to Configure is needed)
size_t Configure(fhicl::ParameterSet const &cfg, EngineCreator engineCreator)
Configuration function.
Definition: WeightManager.h:77