LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
evwgh::WeightManager Class Reference

#include "WeightManager.h"

Public Member Functions

 WeightManager (const std::string name="WeightManager")
 
const std::string & Name () const
 Name getter. More...
 
template<typename EngineCreator >
size_t Configure (fhicl::ParameterSet const &cfg, EngineCreator engineCreator)
 Configuration function. More...
 
MCEventWeight Run (art::Event &e, const int inu)
 Core function (previous call to Configure is needed) More...
 
std::map< std::string, Weight_t * > GetWeightCalcMap ()
 Returns the map between calculator name and Weight_t product. More...
 
void Reset ()
 Reset. More...
 
void PrintConfig ()
 

Private Attributes

std::map< std::string, Weight_t * > fWeightCalcMap
 A set of custom weight calculators. More...
 
bool _configured {false}
 Readiness flag. More...
 
std::string _name
 Name. More...
 

Detailed Description

Definition at line 28 of file WeightManager.h.

Constructor & Destructor Documentation

evwgh::WeightManager::WeightManager ( const std::string  name = "WeightManager")

Definition at line 5 of file WeightManager.cxx.

References _configured.

5  : _name(name)
6  {
7  _configured = false;
8  }
std::string _name
Name.
Definition: WeightManager.h:73
bool _configured
Readiness flag.
Definition: WeightManager.h:72

Member Function Documentation

template<typename EngineCreator >
size_t evwgh::WeightManager::Configure ( fhicl::ParameterSet const &  cfg,
EngineCreator  engineCreator 
)

Configuration function.

Parameters
cfgthe input parameters for settings
theenging creator for the random seed (usually passed with *this) CONFIGURE FUNCTION: created the weights algorithms in the following way:
0) Looks at the weight_functions fcl parameter to get the name of the calculators
1) Creates the Calculators requested in step 0, and assigne a different random seed to each one
3) The future call WeightManager::Run will run the calculators

Definition at line 77 of file WeightManager.h.

References _configured, evwgh::WeightCalcFactory::Create(), evwgh::Weight_t::fNmultisims, evwgh::Weight_t::fWeightCalc, fWeightCalcMap, evwgh::Weight_t::fWeightCalcType, fhicl::ParameterSet::get(), and rndm::NuRandomService::registerAndSeedEngine().

Referenced by evwgh::EventWeight::EventWeight().

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  }
static WeightCalc * Create(const std::string &classname)
T * get() const
Definition: ServiceHandle.h:69
T get(std::string const &key) const
Definition: ParameterSet.h:314
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.
std::map< std::string, Weight_t * > fWeightCalcMap
A set of custom weight calculators.
Definition: WeightManager.h:71
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
std::map<std::string, Weight_t*> evwgh::WeightManager::GetWeightCalcMap ( )
inline

Returns the map between calculator name and Weight_t product.

Definition at line 63 of file WeightManager.h.

References fWeightCalcMap.

Referenced by evwgh::EventWeight::endJob().

63 { return fWeightCalcMap; }
std::map< std::string, Weight_t * > fWeightCalcMap
A set of custom weight calculators.
Definition: WeightManager.h:71
const std::string & evwgh::WeightManager::Name ( ) const

Name getter.

Definition at line 10 of file WeightManager.cxx.

References _name.

11  {
12  return _name;
13  }
std::string _name
Name.
Definition: WeightManager.h:73
void evwgh::WeightManager::PrintConfig ( )

Definition at line 47 of file WeightManager.cxx.

Referenced by Reset().

48  {
49  return;
50  }
void evwgh::WeightManager::Reset ( )
inline

Reset.

Definition at line 66 of file WeightManager.h.

References _configured, and PrintConfig().

66 { _configured = false; }
bool _configured
Readiness flag.
Definition: WeightManager.h:72
MCEventWeight evwgh::WeightManager::Run ( art::Event e,
const int  inu 
)

Core function (previous call to Configure is needed)

Parameters
ethe art event
inuthe index of the simulated neutrino in the event CORE FUNCTION: executes algorithms to assign a weight to the event as requested users.
WeightManager::Configure needs to be called first
The execution takes following steps:
0) Loos over all the previously emplaced calculators
1) For each of them calculates the weights (more weight can be requested per calculator)
3) Returns a map from "calculator name" to vector of weights calculated which is available inside MCEventWeight

Definition at line 18 of file WeightManager.cxx.

References _configured, util::empty(), evwgh::MCEventWeight::fWeight, and fWeightCalcMap.

Referenced by evwgh::EventWeight::produce().

19  {
20 
21  if (!_configured)
22  throw cet::exception(__PRETTY_FUNCTION__) << "Have not configured yet!" << std::endl;
23 
24  //
25  // Loop over all functions ang calculate weights
26  //
27  MCEventWeight mcwgh;
28  for (auto it = fWeightCalcMap.begin(); it != fWeightCalcMap.end(); it++) {
29 
30  auto const& weights = it->second->GetWeight(e);
31 
32  if (weights.size() == 0) {
33  std::vector<double> empty;
34  std::pair<std::string, std::vector<double>> p("empty", empty);
35  mcwgh.fWeight.insert(p);
36  }
37  else {
38  std::pair<std::string, std::vector<double>> p(it->first + "_" + it->second->fWeightCalcType,
39  weights[inu]);
40  mcwgh.fWeight.insert(p);
41  }
42  }
43 
44  return mcwgh;
45  }
bool _configured
Readiness flag.
Definition: WeightManager.h:72
std::map< std::string, Weight_t * > fWeightCalcMap
A set of custom weight calculators.
Definition: WeightManager.h:71
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
decltype(auto) constexpr empty(T &&obj)
ADL-aware version of std::empty.
Definition: StdUtils.h:109

Member Data Documentation

bool evwgh::WeightManager::_configured {false}
private

Readiness flag.

Definition at line 72 of file WeightManager.h.

Referenced by Configure(), Reset(), Run(), and WeightManager().

std::string evwgh::WeightManager::_name
private

Name.

Definition at line 73 of file WeightManager.h.

Referenced by Name().

std::map<std::string, Weight_t*> evwgh::WeightManager::fWeightCalcMap
private

A set of custom weight calculators.

Definition at line 71 of file WeightManager.h.

Referenced by Configure(), GetWeightCalcMap(), and Run().


The documentation for this class was generated from the following files: