LArSoft  v07_13_02
Liquid Argon Software toolkit - http://larsoft.org/
WeightManager.cxx
Go to the documentation of this file.
1 #ifndef WEIGHTMANAGER_CXX
2 #define WEIGHTMANAGER_CXX
3 
4 #include <sstream>
5 #include <map>
6 #include <set>
7 #include "WeightManager.h"
8 
9 
10 namespace evwgh {
11 
12  WeightManager::WeightManager(const std::string name)
13  : _name(name)
14  {
15  _configured = false;
16  }
17 
18  const std::string& WeightManager::Name() const
19  { return _name; }
20 
22  {
23 
25 
26  // Get list of weight functions
27  std::vector<std::string> rw_func = p.get<std::vector<std::string>>("weight_functions");
28 
29  // Loop over all the functions and register them
30  for (auto ifunc=rw_func.begin(); ifunc!=rw_func.end(); ifunc++)
31  {
32  fhicl::ParameterSet const &ps_func = p.get<fhicl::ParameterSet> (*ifunc);
33  std::string func_type = ps_func.get<std::string>("type");
34 
35  WeightCalc* wcalc=WeightCalcFactory::Create(func_type+"WeightCalc");
36  if ( wcalc == NULL )
37  throw cet::exception(__FUNCTION__) << "Function " << *ifunc << " requested in fcl file has not been registered!" << std::endl;
38  if ( fWeightCalcMap.find(*ifunc)!= fWeightCalcMap.end() )
39  throw cet::exception(__FUNCTION__) << "Function " << *ifunc << " has been requested multiple times in fcl file!" << std::endl;
40 
41  //mf::LogInfo("") << "Configuring weight calculator " << *ifunc;
42 
43  // Create random engine for each rw function (name=*ifunc) (and seed it with random_seed set in the fcl)
44  seedservice->createEngine(engine, "HepJamesRandom", *ifunc, ps_func, "random_seed");
45 
46  wcalc->SetName(*ifunc);
47  wcalc->Configure(p);
48  Weight_t* winfo=new Weight_t();
49  winfo->fWeightCalcType=func_type;
50  winfo->fWeightCalc=wcalc;
51  winfo->fNmultisims=ps_func.get<int>("number_of_multisims");
52 
53  std::pair<std::string, Weight_t*> pwc(*ifunc,winfo);
54  fWeightCalcMap.insert(pwc);
55  }
56 
57  _configured = true;
58 
59  return fWeightCalcMap.size();
60  }
61 
62 
63 
64  //
65  // CORE FUNCTION
66  //
68  {
69 
70  if (!_configured)
71  throw cet::exception(__PRETTY_FUNCTION__) << "Have not configured yet!" << std::endl;
72 
73  //
74  // Loop over all functions ang calculate weights
75  //
76  MCEventWeight mcwgh;
77  for (auto it = fWeightCalcMap.begin() ;it != fWeightCalcMap.end(); it++) {
78 
79  auto const & weights = it->second->GetWeight(e);
80 
81  if(weights.size() == 0){
82  std::vector<double> empty;
83  std::pair<std::string, std::vector <double> > p("empty",empty);
84  mcwgh.fWeight.insert(p);
85  }
86  else{
87  std::pair<std::string, std::vector<double> >
88  p(it->first+"_"+it->second->fWeightCalcType,
89  weights[inu]);
90  mcwgh.fWeight.insert(p);
91  }
92  }
93 
94  return mcwgh;
95  }
96 
97 
98 
100 
101  return;
102  }
103 
104 }
105 
106 #endif
size_t Configure(fhicl::ParameterSet const &cfg, art::EngineCreator &ec)
Configuration function.
static WeightCalc * Create(const std::string &classname)
std::string _name
Name.
Definition: WeightManager.h:94
Allows to interface to EventWeight calculators.
WeightCalc * fWeightCalc
Definition: Weight_t.h:31
seed_t createEngine(art::EngineCreator &module, std::string type, std::string instance="")
Creates an engine with RandomNumberGenerator service.
std::string fWeightCalcType
Definition: Weight_t.h:32
T get(std::string const &key) const
Definition: ParameterSet.h:231
std::map< std::string, std::vector< double > > fWeight
Definition: MCEventWeight.h:10
bool _configured
Readiness flag.
Definition: WeightManager.h:92
WeightManager(const std::string name="WeightManager")
Default constructor.
std::map< std::string, Weight_t * > fWeightCalcMap
A set of custom weight calculators.
Definition: WeightManager.h:90
Float_t e
Definition: plot.C:34
MCEventWeight Run(art::Event &e, const int inu)
Core function (previous call to Configure is needed)
const std::string & Name() const
Name getter.
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33