All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
EventWeight_module.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 // 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
10 ////////////////////////////////////////////////////////////////////////
11 
12 #include "art/Framework/Core/EDProducer.h"
13 #include "art/Framework/Core/ModuleMacros.h"
14 #include "art/Framework/Principal/Event.h"
15 #include "art/Framework/Principal/Handle.h"
16 #include "messagefacility/MessageLogger/MessageLogger.h"
17 #include "fhiclcpp/ParameterSet.h"
18 
19 #include <memory>
20 #include <iostream>
21 #include <iomanip>
22 
26 
27 #include "nusimdata/SimulationBase/MCTruth.h"
28 #include "nugen/EventGeneratorBase/GENIE/GENIE2ART.h"
29 
30 namespace evwgh {
31 
32  class EventWeight : public art::EDProducer {
33 
34  public:
35 
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 
48  // Required functions.
49  void produce(art::Event & e) override;
50 
51  //Optional functions.
52  void endJob() override;
53 
55  std::string fGenieModuleLabel;
56  };
57 
58  EventWeight::EventWeight(fhicl::ParameterSet const & p)
59  : EDProducer{p}
60  , fGenieModuleLabel{p.get<std::string>("genie_module_label", "generator")}
61  {
62  // Configure the appropriate GENIE tune if needed (important for v3+ only)
63  // NOTE: In all normal use cases, relying on the ${GENIE_XSEC_TUNE}
64  // environment variable set by the genie_xsec package should be sufficient.
65  // Only include the "TuneName" FHiCL parameter for EventWeight if you
66  // really know what you're doing! The same goes for the
67  // "EventGeneratorList" parameter.
68  std::string genie_tune_name = p.get<std::string>("TuneName",
69  "${GENIE_XSEC_TUNE}");
70 
71  // The default empty string used here will cause the subsequent call to
72  // evgb::SetEventGeneratorListAndTune() to leave GENIE's current event
73  // generator list name (probably "Default") alone
74  std::string evgen_list_name = p.get<std::string>("EventGeneratorList", "");
75 
76  // Tell GENIE about the event generator list and tune
77  evgb::SetEventGeneratorListAndTune( evgen_list_name, genie_tune_name );
78 
79  auto const n_func = _wgt_manager.Configure(p, *this);
80  if ( n_func > 0 )
81  produces<std::vector<MCEventWeight> >();
82  }
83 
84  void EventWeight::produce(art::Event & e)
85  {
86  // Implementation of required member function here.
87  auto mcwghvec = std::make_unique<std::vector<MCEventWeight>>();
88 
89  // Get the MC generator information out of the event
90  // these are all handles to mc information.
91  std::vector<art::Ptr<simb::MCTruth> > mclist;
92 
93  // Actually go and get the stuff
94  auto const mcTruthHandle = e.getValidHandle<std::vector<simb::MCTruth>>(fGenieModuleLabel);
95  art::fill_ptr_vector(mclist, mcTruthHandle);
96 
97  // Loop over all neutrinos in this event
98  for (unsigned int inu = 0; inu < mclist.size(); ++inu) {
99  auto const mcwgh = _wgt_manager.Run(e, inu);
100  mcwghvec->push_back(mcwgh);
101  }
102 
103  e.put(std::move(mcwghvec));
104  }
105 
107  {
108  // Get the map from sting to Weight_t from the manager
109  std::map<std::string, Weight_t*> weightCalcMap = _wgt_manager.GetWeightCalcMap();
110 
111  std::stringstream job_summary;
112  job_summary << std::setprecision(2);
113  for (int i=1; i <= 110 ;i++) job_summary << "=";
114  job_summary << std::endl;
115  job_summary << std::setw(20) << "WeightCalc"
116  << std::setw(15) << "Type"
117  << std::setw(15) << "#RW neutrinos"
118  << std::setw(15) << "#Multisims"
119  << std::setw(15) << "Min"
120  << std::setw(15) << "Max"
121  << std::setw(15) << "Avg"
122  << std::endl;
123  for (int i=1; i <= 110; i++) job_summary << "=";
124  job_summary << std::endl;
125  for (auto it = weightCalcMap.begin(); it!=weightCalcMap.end(); it++) {
126  job_summary << std::setw(20) << it->first
127  << std::setw(15) << (it->second->fWeightCalcType)
128  << std::setw(15) << (it->second->fNcalls)
129  << std::setw(15) << (it->second->fNmultisims)
130  << std::setw(15) << (it->second->fMinWeight)
131  << std::setw(15) << (it->second->fMaxWeight)
132  << std::setw(15) << (it->second->fAvgWeight)
133  << std::endl;
134  }
135  for (int i=1; i<=110; i++) job_summary << "=";
136  job_summary << std::endl;
137  mf::LogInfo("") << job_summary.str();
138  }
139 
140 } // namespace
141 
142 DEFINE_ART_MODULE(evwgh::EventWeight)
void produce(art::Event &e) override
pdgs p
Definition: selectors.fcl:22
std::string fGenieModuleLabel
EventWeight & operator=(EventWeight const &)=delete
void endJob() override
std::map< std::string, Weight_t * > GetWeightCalcMap()
Returns the map between calculator name and Weight_t product.
WeightManager _wgt_manager
EventWeight(fhicl::ParameterSet const &p)
do i e
MCEventWeight Run(art::Event &e, const int inu)
Core function (previous call to Configure is needed)