LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
WeightManager.h
Go to the documentation of this file.
1 
10 #ifndef WEIGHTMANAGER_H
11 #define WEIGHTMANAGER_H
12 
15 #include "fhiclcpp/ParameterSet.h"
18 
19 #include "MCEventWeight.h"
20 #include "WeightCalc.h"
21 #include "WeightCalcFactory.h"
22 #include "Weight_t.h"
23 
24 namespace evwgh {
28  class WeightManager {
29  public:
30  WeightManager(const std::string name = "WeightManager");
31 
33  const std::string& Name() const;
34 
44  template <typename EngineCreator>
45  size_t Configure(fhicl::ParameterSet const& cfg, EngineCreator engineCreator);
46 
58  MCEventWeight Run(art::Event& e, const int inu);
59 
63  std::map<std::string, Weight_t*> GetWeightCalcMap() { return fWeightCalcMap; }
64 
66  void Reset() { _configured = false; }
67 
68  void PrintConfig();
69 
70  private:
71  std::map<std::string, Weight_t*> fWeightCalcMap;
72  bool _configured{false};
73  std::string _name;
74  };
75 
76  template <typename EngineCreator>
77  size_t WeightManager::Configure(fhicl::ParameterSet const& p, EngineCreator createEngine)
78  {
79 
81 
82  // Get list of weight functions
83  auto const rw_func = p.get<std::vector<std::string>>("weight_functions");
84 
85  // Loop over all the functions and register them
86  auto const module_label = p.get<std::string>("module_label");
87  for (auto const& func : rw_func) {
88  auto const ps_func = p.get<fhicl::ParameterSet>(func);
89  std::string func_type = ps_func.get<std::string>("type");
90 
91  WeightCalc* wcalc = WeightCalcFactory::Create(func_type + "WeightCalc");
92  if (wcalc == nullptr)
93  throw cet::exception(__FUNCTION__)
94  << "Function " << func << " requested in fcl file has not been registered!" << std::endl;
95  if (fWeightCalcMap.find(func) != fWeightCalcMap.end())
96  throw cet::exception(__FUNCTION__)
97  << "Function " << func << " has been requested multiple times in fcl file!" << std::endl;
98 
99  // Create random engine for each rw function (name=func) (and seed it with random_seed set in the fcl)
100  CLHEP::HepRandomEngine& engine = seedservice->registerAndSeedEngine(
101  createEngine("HepJamesRandom", func), "HepJamesRandom", func, ps_func, "random_seed");
102  wcalc->SetName(func);
103  wcalc->Configure(p, engine);
104  Weight_t* winfo = new Weight_t();
105  winfo->fWeightCalcType = func_type;
106  winfo->fWeightCalc = wcalc;
107  winfo->fNmultisims = ps_func.get<int>("number_of_multisims", 0);
108 
109  fWeightCalcMap.emplace(func, winfo);
110  }
111 
112  _configured = true;
113  return fWeightCalcMap.size();
114  }
115 
116 }
117 
118 #endif
static WeightCalc * Create(const std::string &classname)
std::string _name
Name.
Definition: WeightManager.h:73
WeightCalc * fWeightCalc
Definition: Weight_t.h:32
void Reset()
Reset.
Definition: WeightManager.h:66
std::map< std::string, Weight_t * > GetWeightCalcMap()
Returns the map between calculator name and Weight_t product.
Definition: WeightManager.h:63
std::string fWeightCalcType
Definition: Weight_t.h:33
T get(std::string const &key) const
Definition: ParameterSet.h:314
An art service to assist in the distribution of guaranteed unique seeds to all engines within an art ...
bool _configured
Readiness flag.
Definition: WeightManager.h:72
std::reference_wrapper< NuRandomService::engine_t > registerAndSeedEngine(engine_t &engine, std::string type="", std::string instance="", std::optional< seed_t > const seed=std::nullopt)
Creates an engine with art::RandomNumberGenerator service.
WeightManager(const std::string name="WeightManager")
std::map< std::string, Weight_t * > fWeightCalcMap
A set of custom weight calculators.
Definition: WeightManager.h:71
Float_t e
Definition: plot.C:35
MCEventWeight Run(art::Event &e, const int inu)
Core function (previous call to Configure is needed)
const std::string & Name() const
Name getter.
Tools and modules for checking out the basics of the Monte Carlo.
size_t Configure(fhicl::ParameterSet const &cfg, EngineCreator engineCreator)
Configuration function.
Definition: WeightManager.h:77
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33