LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
PPFXOtherWeightCalc.cxx
Go to the documentation of this file.
1 
4 
5 #include "CLHEP/Random/RandGaussQ.h"
6 
7 #include "MakeReweight.h"
8 #include "cetlib/search_path.h"
9 #include "dk2nu/tree/dk2nu.h"
10 #include "dk2nu/tree/dkmeta.h"
12 
13 #include "TSystem.h"
14 
15 namespace evwgh {
17  public:
19  void Configure(fhicl::ParameterSet const& p, CLHEP::HepRandomEngine& engine) override;
20  std::vector<std::vector<double>> GetWeight(art::Event& e) override;
21 
22  private:
23  std::string fGenieModuleLabel;
24 
25  std::vector<std::string> fInputLabels;
26  std::string fPPFXMode;
27  std::string fMode;
28  std::string fHorn;
29  std::string fTarget;
30  int fSeed;
31  int fVerbose;
32  NeutrinoFluxReweight::MakeReweight* fPPFXrw;
33 
35  };
36 
38 
39  void PPFXOtherWeightCalc::Configure(fhicl::ParameterSet const& p, CLHEP::HepRandomEngine& engine)
40  {
41  //get configuration for this function
43  fGenieModuleLabel = p.get<std::string>("genie_module_label");
44 
45  //ppfx setup
46  fInputLabels = pset.get<std::vector<std::string>>("input_labels");
47  fPPFXMode = pset.get<std::string>("ppfx_mode");
48  fVerbose = pset.get<int>("verbose");
49  fMode = pset.get<std::string>("mode");
50  fHorn = pset.get<std::string>("horn_curr");
51  fTarget = pset.get<std::string>("target_config");
52  fSeed = pset.get<int>("random_seed", -1);
53 
54  gSystem->Setenv("MODE", fPPFXMode.c_str());
55 
56  fPPFXrw = NeutrinoFluxReweight::MakeReweight::getInstance();
57  std::cout << "PPFX instance " << fPPFXrw << std::endl;
58 
59  std::string inputOptions; // Full path.
60  std::string mode_file = "inputs_" + fPPFXMode + ".xml"; // Just file name.
61  cet::search_path sp("FW_SEARCH_PATH");
62  sp.find_file(mode_file, inputOptions);
63 
64  if (fSeed != -1) fPPFXrw->setBaseSeed(fSeed); // Set the random seed
65  std::cout << "is PPFX setup : " << fPPFXrw->AlreadyInitialized() << std::endl;
66  std::cout << "Setting PPFX, inputs: " << inputOptions << std::endl;
67  std::cout << "Setting Horn Current Configuration to: " << fHorn << std::endl;
68  std::cout << "Setting Target Configuration to: " << fTarget << std::endl;
69  if (!(fPPFXrw->AlreadyInitialized())) { fPPFXrw->SetOptions(inputOptions); }
70  std::cout << "PPFX just set with mode: " << fPPFXMode << std::endl;
71  }
72 
73  std::vector<std::vector<double>> PPFXOtherWeightCalc::GetWeight(art::Event& e)
74  {
75  std::vector<std::vector<double>> weight;
77  //calculate weight(s) here
78 
79  int nmctruth = 0, nmatched = 0;
80  bool flag = true;
81  int ievt = -1;
82  std::vector<art::Handle<std::vector<bsim::Dk2Nu>>> dk2nus2 =
83  e.getMany<std::vector<bsim::Dk2Nu>>();
84  for (size_t dk2 = 0; dk2 < dk2nus2.size(); ++dk2) {
85  art::Handle<std::vector<bsim::Dk2Nu>> dk2nus = dk2nus2[dk2];
86  }
87  while ((flag = mcitr.Next())) {
88  std::string label = mcitr.GetLabel();
89  const simb::MCTruth* pmctruth = mcitr.GetMCTruth();
90  // const simb::GTruth* pgtruth = mcitr.GetGTruth();
91  //not-used//const simb::MCFlux* pmcflux = mcitr.GetMCFlux();
92  const bsim::Dk2Nu* pdk2nu = mcitr.GetDk2Nu();
93  //not-used//const bsim::NuChoice* pnuchoice = mcitr.GetNuChoice();
94  // art::Ptr<simb::MCTruth> mctruthptr = mcitr.GetMCTruthPtr();
95 
96  ++ievt;
97  ++nmctruth;
98  if (fVerbose > 0) {
99  std::cout << "FluxWeightCalculator [" << std::setw(4) << ievt << "] "
100  << " label \"" << label << "\" MCTruth@ " << pmctruth << " Dk2Nu@ " << pdk2nu
101  << std::endl;
102  }
103  if (!pdk2nu) continue;
104  ++nmatched;
105 
106  //RWH//bsim::Dk2Nu* tmp_dk2nu = fTmpDK2NUConversorAlg.construct_toy_dk2nu( &mctruth,&mcflux);
107  //RWH//bsim::DkMeta* tmp_dkmeta = fTmpDK2NUConversorAlg.construct_toy_dkmeta(&mctruth,&mcflux);
108  //RWH// those appear to have been memory leaks
109  //RWH// herein begins the replacment for the "construct_toy_dkmeta"
110 
111  static bsim::DkMeta
112  dkmeta_obj; //RWH// create this on stack (destroyed when out-of-scope) ... or static
113  dkmeta_obj.tgtcfg = fTarget;
114  dkmeta_obj.horncfg = fHorn;
115  (dkmeta_obj.vintnames).push_back("Index_Tar_In_Ancestry");
116  (dkmeta_obj.vintnames).push_back("Playlist");
117  bsim::DkMeta* tmp_dkmeta = &dkmeta_obj;
118  //RWH// herein ends this block
119 
120  // sigh ....
121  //RWH// this is the signature in NeutrinoFluxReweight::MakeReweight :
122  // //! create an interaction chain from the new dk2nu(dkmeta) format
123  // void calculateWeights(bsim::Dk2Nu* nu, bsim::DkMeta* meta);
124  //RWH// these _should_ be "const <class>*" because we don't need to change them
125  //RWH// and the pointers we get out of the ART record are going to be const.
126  bsim::Dk2Nu* tmp_dk2nu = const_cast<bsim::Dk2Nu*>(pdk2nu); // remove const-ness
127  try {
128  fPPFXrw->calculateWeights(tmp_dk2nu, tmp_dkmeta);
129  }
130  catch (...) {
131  std::cerr << "Failed to calcualte weight" << std::endl;
132  continue;
133  }
134  //Get weights:
135  if (fMode == "reweight") {
136  double ppfx_cv_wgt = fPPFXrw->GetCVWeight();
137  std::vector<double> wvec = {ppfx_cv_wgt};
138  weight.push_back(wvec);
139  }
140  else {
141  std::vector<double> vothers = fPPFXrw->GetWeights("Other");
142 
143  std::vector<double> tmp_vhptot;
144  for (unsigned int iuniv = 0; iuniv < vothers.size(); iuniv++) {
145  tmp_vhptot.push_back(float(vothers[iuniv]));
146  }
147  weight.push_back(tmp_vhptot);
148  }
149  }
150  return weight;
151  }
153 }
const simb::MCTruth * GetMCTruth() const
std::string GetName()
Definition: WeightCalc.h:26
#define DECLARE_WEIGHTCALC(wghcalc)
T get(std::string const &key) const
Definition: ParameterSet.h:314
std::vector< std::string > fInputLabels
double weight
Definition: plottest35.C:25
void Configure(fhicl::ParameterSet const &p, CLHEP::HepRandomEngine &engine) override
std::vector< std::vector< double > > GetWeight(art::Event &e) override
Event generator information.
Definition: MCTruth.h:32
Float_t e
Definition: plot.C:35
std::string GetLabel() const
NeutrinoFluxReweight::MakeReweight * fPPFXrw
const bsim::Dk2Nu * GetDk2Nu() const
std::vector< Handle< PROD > > getMany(SelectorBase const &selector=MatchAllSelector{}) const
#define REGISTER_WEIGHTCALC(wghcalc)