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 
16 #include "nugen/EventGeneratorBase/GENIE/GENIE2ART.h"
17 #include "fhiclcpp/ParameterSet.h"
19 
20 #include <memory>
21 #include <iostream>
22 #include <iomanip>
23 
27 
29 #include "nugen/EventGeneratorBase/GENIE/GENIE2ART.h"
30 
31 namespace evwgh {
32 
33  class EventWeight : public art::EDProducer {
34 
35  public:
36 
37  explicit EventWeight(fhicl::ParameterSet const & p);
38  // The destructor generated by the compiler is fine for classes
39  // without bare pointers or other resource use.
40 
41  // Plugins should not be copied or assigned.
42  EventWeight(EventWeight const &) = delete;
43  EventWeight(EventWeight &&) = delete;
44  EventWeight & operator = (EventWeight const &) = delete;
45  EventWeight & operator = (EventWeight &&) = delete;
46 
47  private:
48 
49  // Required functions.
50  void produce(art::Event & e) override;
51 
52  //Optional functions.
53  void endJob() override;
54 
57  };
58 
60  : EDProducer{p}
61  , fGenieModuleLabel{p.get<std::string>("genie_module_label", "generator")}
62  {
63  // Configure the appropriate GENIE tune if needed (important for v3+ only)
64  // NOTE: In all normal use cases, relying on the ${GENIE_XSEC_TUNE}
65  // environment variable set by the genie_xsec package should be sufficient.
66  // Only include the "TuneName" FHiCL parameter for EventWeight if you
67  // really know what you're doing! The same goes for the
68  // "EventGeneratorList" parameter.
69  std::string genie_tune_name = p.get<std::string>("TuneName",
70  "${GENIE_XSEC_TUNE}");
71 
72  // The default empty string used here will cause the subsequent call to
73  // evgb::SetEventGeneratorListAndTune() to leave GENIE's current event
74  // generator list name (probably "Default") alone
75  std::string evgen_list_name = p.get<std::string>("EventGeneratorList", "");
76 
77  // Tell GENIE about the event generator list and tune
78  evgb::SetEventGeneratorListAndTune( evgen_list_name, genie_tune_name );
79 
80  auto const n_func = _wgt_manager.Configure(p, *this);
81  if ( n_func > 0 )
82  produces<std::vector<MCEventWeight> >();
83  }
84 
86  {
87  // Implementation of required member function here.
88  auto mcwghvec = std::make_unique<std::vector<MCEventWeight>>();
89 
90  // Get the MC generator information out of the event
91  // these are all handles to mc information.
92  std::vector<art::Ptr<simb::MCTruth> > mclist;
93 
94  // Actually go and get the stuff
95  auto const mcTruthHandle = e.getValidHandle<std::vector<simb::MCTruth>>(fGenieModuleLabel);
96  art::fill_ptr_vector(mclist, mcTruthHandle);
97 
98  // Loop over all neutrinos in this event
99  for (unsigned int inu = 0; inu < mclist.size(); ++inu) {
100  auto const mcwgh = _wgt_manager.Run(e, inu);
101  mcwghvec->push_back(mcwgh);
102  }
103 
104  e.put(std::move(mcwghvec));
105  }
106 
108  {
109  // Get the map from sting to Weight_t from the manager
110  std::map<std::string, Weight_t*> weightCalcMap = _wgt_manager.GetWeightCalcMap();
111 
112  std::stringstream job_summary;
113  job_summary << std::setprecision(2);
114  for (int i=1; i <= 110 ;i++) job_summary << "=";
115  job_summary << std::endl;
116  job_summary << std::setw(20) << "WeightCalc"
117  << std::setw(15) << "Type"
118  << std::setw(15) << "#RW neutrinos"
119  << std::setw(15) << "#Multisims"
120  << std::setw(15) << "Min"
121  << std::setw(15) << "Max"
122  << std::setw(15) << "Avg"
123  << std::endl;
124  for (int i=1; i <= 110; i++) job_summary << "=";
125  job_summary << std::endl;
126  for (auto it = weightCalcMap.begin(); it!=weightCalcMap.end(); it++) {
127  job_summary << std::setw(20) << it->first
128  << std::setw(15) << (it->second->fWeightCalcType)
129  << std::setw(15) << (it->second->fNcalls)
130  << std::setw(15) << (it->second->fNmultisims)
131  << std::setw(15) << (it->second->fMinWeight)
132  << std::setw(15) << (it->second->fMaxWeight)
133  << std::setw(15) << (it->second->fAvgWeight)
134  << std::endl;
135  }
136  for (int i=1; i<=110; i++) job_summary << "=";
137  job_summary << std::endl;
138  mf::LogInfo("") << job_summary.str();
139  }
140 
141 } // namespace
142 
void produce(art::Event &e) override
std::string string
Definition: nybbler.cc:12
MaybeLogger_< ELseverityLevel::ELsev_info, false > LogInfo
EDProducer(fhicl::ParameterSet const &pset)
Definition: EDProducer.h:20
std::string fGenieModuleLabel
Allows to interface to EventWeight calculators.
EventWeight & operator=(EventWeight const &)=delete
Q_EXPORT QTSManip setprecision(int p)
Definition: qtextstream.h:343
const double e
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:67
void endJob() override
std::map< std::string, Weight_t * > GetWeightCalcMap()
Returns the map between calculator name and Weight_t product.
Definition: WeightManager.h:69
def move(depos, offset)
Definition: depos.py:107
ValidHandle< PROD > getValidHandle(InputTag const &tag) const
Definition: DataViewImpl.h:441
p
Definition: test.py:223
WeightManager _wgt_manager
ProductID put(std::unique_ptr< PROD > &&edp, std::string const &instance={})
Definition: DataViewImpl.h:686
EventWeight(fhicl::ParameterSet const &p)
size_t Configure(fhicl::ParameterSet const &cfg, Module &module)
Configuration function.
Definition: WeightManager.h:85
Q_EXPORT QTSManip setw(int w)
Definition: qtextstream.h:331
void fill_ptr_vector(std::vector< Ptr< T >> &ptrs, H const &h)
Definition: Ptr.h:297
MCEventWeight Run(art::Event &e, const int inu)
Core function (previous call to Configure is needed)
QTextStream & endl(QTextStream &s)