All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
sbncode/sbncode/SBNEventWeight/Base/WeightManager.h
Go to the documentation of this file.
1 #ifndef _SBN_WEIGHTMANAGER_H_
2 #define _SBN_WEIGHTMANAGER_H_
3 
4 /**
5  * \file WeightManager.h
6  * \brief Allows to interface to EventWeight calculators
7  *
8  * Adapted from LArSoft's larsim EventWeight by A. Mastbaum
9  * Original author: Marco Del Tutto <marco.deltutto@physics.ox.ac.uk>
10  */
11 
12 #include "art/Framework/Principal/fwd.h"
13 #include "art/Framework/Services/Registry/ServiceHandle.h"
14 #include "nurandom/RandomUtils/NuRandomService.h"
16 #include "fhiclcpp/ParameterSet.h"
18 #include "WeightCalc.h"
19 #include "WeightCalcFactory.h"
20 
21 namespace sbn {
22  namespace evwgh {
23 
25 public:
28 
29  /**
30  * CONFIGURE FUNCTION
31  *
32  * 0) Looks at the weight_functions fcl parameter to get the name of the calculators \n
33  * 1) Creates the Calculators requested in step 0, and assigne a different random seed to each one \n
34  * 3) The future call WeightManager::Run will run the calculators \n
35  *
36  * @param cfg the input parameters for settings
37  * @param the enging creator for the random seed (usually passed with *this)
38  */
39  template <typename Module>
40  size_t Configure(fhicl::ParameterSet const& cfg, Module& module);
41 
42  /**
43  * CORE FUNCTION: executes algorithms to assign a weight to the event as requested users. \n
44  * WeightManager::Configure needs to be called first \n
45  *
46  * 0) Loos over all the previously emplaced calculators \n
47  * 1) For each of them calculates the weights (more weight can be requested per calculator) \n
48  * 3) Returns a map from "calculator name" to vector of weights calculated which is available inside EventWeightMap
49  *
50  * @param e the art event
51  * @param inu the index of the simulated neutrino in the event
52  */
53  EventWeightMap Run(art::Event &e, const int inu);
54 
55  /**
56  * Returns the map between calculator name and WeightCalcs
57  */
58  std::map<std::string, WeightCalc*> GetWeightCalcMap() { return fWeightCalcMap; }
59 
60 private:
61  std::map<std::string, WeightCalc*> fWeightCalcMap; ///< A set of custom weight calculators
62 };
63 
64 
65 template <typename Module>
66 size_t WeightManager::Configure(fhicl::ParameterSet const& p, Module& module) {
67  ::art::ServiceHandle<rndm::NuRandomService> seedservice;
68 
69  // Get list of weight functions
70  auto const rw_func = p.get<std::vector<std::string> >("weight_functions");
71  auto const module_label = p.get<std::string>("module_label");
72 
73  // Loop over all the functions and register them
74  for (auto const& func : rw_func) {
75  auto const ps_func = p.get<fhicl::ParameterSet>(func);
76  std::string func_type = ps_func.get<std::string>("type");
77 
78  WeightCalc* wcalc = WeightCalcFactory::Create(func_type + "WeightCalc");
79  if (wcalc == nullptr)
80  throw cet::exception(__FUNCTION__) << "Function " << func << " requested in fcl file has not been registered!" << std::endl;
81 
82  if (fWeightCalcMap.find(func) != fWeightCalcMap.end())
83  throw cet::exception(__FUNCTION__) << "Function " << func << " has been requested multiple times in fcl file!" << std::endl;
84 
85  // Create random engine for each rw function (name=func) (and seed it with random_seed set in the fcl)
86  CLHEP::HepRandomEngine& engine = seedservice->createEngine(module, "HepJamesRandom", func, ps_func, "random_seed");
87 
88  wcalc->SetName(func);
89  wcalc->SetType(func_type);
90  wcalc->Configure(p, engine);
91 
92  fWeightCalcMap.emplace(func, wcalc);
93  }
94 
95  return fWeightCalcMap.size();
96 }
97 
98  } // namespace evwgh
99 } // namespace sbn
100 
101 #endif // _SBN_WEIGHTMANAGER_H_
102 
std::map< std::string, std::vector< float > > EventWeightMap
Container for event-level weights.
pdgs p
Definition: selectors.fcl:22
size_t Configure(fhicl::ParameterSet const &cfg, Module &module)
std::map< std::string, WeightCalc * > fWeightCalcMap
A set of custom weight calculators.
std::map< std::string, WeightCalc * > GetWeightCalcMap()
static WeightCalc * Create(const std::string &classname)
do i e
EventWeightMap Run(art::Event &e, const int inu)
Tools and modules for checking out the basics of the Monte Carlo.