All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
larsim/larsim/EventWeight/Base/WeightManager.h
Go to the documentation of this file.
1 /**
2  * \file WeightManager.h
3  *
4  *
5  * \brief Allows to interface to EventWeight calculators
6  *
7  * @author Marco Del Tutto <marco.deltutto@physics.ox.ac.uk>
8  */
9 
10 #ifndef WEIGHTMANAGER_H
11 #define WEIGHTMANAGER_H
12 
13 #include "art/Framework/Principal/fwd.h"
14 #include "art/Framework/Services/Registry/ServiceHandle.h"
15 #include "nurandom/RandomUtils/NuRandomService.h"
17 #include "fhiclcpp/ParameterSet.h"
18 
19 #include "Weight_t.h"
20 #include "MCEventWeight.h"
21 #include "WeightCalc.h"
22 #include "WeightCalcFactory.h"
23 
24 namespace evwgh {
25  /**
26  \class WeightManager
27  */
28  class WeightManager {
29 
30  public:
31 
32  /// Default constructor
33  WeightManager(const std::string name="WeightManager");
34 
35  /// Default destructor
37 
38  /// Name getter
39  const std::string& Name() const;
40 
41  /**
42  * @brief Configuration function
43  * @param cfg the input parameters for settings
44  * @param the enging creator for the random seed (usually passed with *this)
45  CONFIGURE FUNCTION: created the weights algorithms in the following way: \n
46  0) Looks at the weight_functions fcl parameter to get the name of the calculators \n
47  1) Creates the Calculators requested in step 0, and assigne a different random seed to each one \n
48  3) The future call WeightManager::Run will run the calculators \n
49  */
50  template <typename Module>
51  size_t Configure(fhicl::ParameterSet const & cfg, Module& module);
52 
53  /**
54  * @brief Core function (previous call to Configure is needed)
55  * @param e the art event
56  * @param inu the index of the simulated neutrino in the event
57  CORE FUNCTION: executes algorithms to assign a weight to the event as requested users. \n
58  WeightManager::Configure needs to be called first \n
59  The execution takes following steps: \n
60  0) Loos over all the previously emplaced calculators \n
61  1) For each of them calculates the weights (more weight can be requested per calculator) \n
62  3) Returns a map from "calculator name" to vector of weights calculated which is available inside MCEventWeight
63  */
64  MCEventWeight Run(art::Event &e, const int inu);
65 
66  /**
67  * @brief Returns the map between calculator name and Weight_t product
68  */
69  std::map<std::string, Weight_t*> GetWeightCalcMap() { return fWeightCalcMap; }
70 
71  /// Reset
72  void Reset()
73  { _configured = false; }
74 
75  void PrintConfig();
76 
77 
78  private:
79  std::map<std::string, Weight_t*> fWeightCalcMap; ///< A set of custom weight calculators
80  bool _configured{false}; ///< Readiness flag
81  std::string _name; ///< Name
82  };
83 
84  template <typename Module>
85  size_t WeightManager::Configure(fhicl::ParameterSet const & p, Module& module)
86  {
87 
88  ::art::ServiceHandle<rndm::NuRandomService> seedservice;
89 
90  // Get list of weight functions
91  auto const rw_func = p.get<std::vector<std::string>>("weight_functions");
92 
93  // Loop over all the functions and register them
94  auto const module_label = p.get<std::string>("module_label");
95  for (auto const& func : rw_func) {
96  auto const ps_func = p.get<fhicl::ParameterSet>(func);
97  std::string func_type = ps_func.get<std::string>("type");
98 
99  WeightCalc* wcalc=WeightCalcFactory::Create(func_type+"WeightCalc");
100  if (wcalc == nullptr)
101  throw cet::exception(__FUNCTION__) << "Function " << func << " requested in fcl file has not been registered!" << std::endl;
102  if (fWeightCalcMap.find(func) != fWeightCalcMap.end())
103  throw cet::exception(__FUNCTION__) << "Function " << func << " has been requested multiple times in fcl file!" << std::endl;
104 
105  // Create random engine for each rw function (name=func) (and seed it with random_seed set in the fcl)
106  CLHEP::HepRandomEngine& engine = seedservice->createEngine(module, "HepJamesRandom", func, ps_func, "random_seed");
107  wcalc->SetName(func);
108  wcalc->Configure(p, engine);
109  Weight_t* winfo=new Weight_t();
110  winfo->fWeightCalcType=func_type;
111  winfo->fWeightCalc=wcalc;
112  winfo->fNmultisims=ps_func.get<int>("number_of_multisims", 0);
113 
114  fWeightCalcMap.emplace(func, winfo);
115 }
116 
117  _configured = true;
118  return fWeightCalcMap.size();
119  }
120 
121 }
122 
123 #endif
static WeightCalc * Create(const std::string &classname)
pdgs p
Definition: selectors.fcl:22
WeightCalc * fWeightCalc
Definition: Weight_t.h:33
std::map< std::string, Weight_t * > GetWeightCalcMap()
Returns the map between calculator name and Weight_t product.
std::string fWeightCalcType
Definition: Weight_t.h:34
size_t Configure(fhicl::ParameterSet const &cfg, Module &module)
Configuration function.
WeightManager(const std::string name="WeightManager")
Default constructor.
std::map< std::string, Weight_t * > fWeightCalcMap
A set of custom weight calculators.
do i e
then echo fcl name
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.