All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SBNEventWeight_module.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 // Class: SBNEventWeight
3 // Module Type: producer
4 // File: SBNEventWeight_module.cc
5 //
6 // Generated at Fri Mar 20 09:36:11 2015 by Zarko Pavlovic using artmod
7 // from cetpkgsupport v1_08_04.
8 //
9 // Ported from uboonecode to larsim on Feb 14 2018 by Marco Del Tutto
10 //
11 // Ported from larsim to sbncode on Dec 22 2020 by A. Mastbaum
12 //
13 ////////////////////////////////////////////////////////////////////////
14 
15 #include <iostream>
16 
17 #include "art/Framework/Core/EDProducer.h"
18 #include "art/Framework/Core/ModuleMacros.h"
19 #include "art/Framework/Principal/Event.h"
20 #include "art/Framework/Principal/Handle.h"
21 #include "art/Framework/Principal/Run.h"
22 #include "art/Persistency/Common/PtrMaker.h"
23 #include "canvas/Persistency/Common/Assns.h"
24 #include "nusimdata/SimulationBase/MCTruth.h"
25 #include "fhiclcpp/ParameterSet.h"
26 
30 
31 #include "canvas/Persistency/Common/Assns.h"
32 #include "art/Framework/Principal/Run.h"
33 
34 namespace sbn {
35  namespace evwgh {
36 
37 class SBNEventWeight : public art::EDProducer {
38 public:
39  explicit SBNEventWeight(fhicl::ParameterSet const& p);
40 
41  SBNEventWeight(SBNEventWeight const &) = delete;
42  SBNEventWeight(SBNEventWeight &&) = delete;
43  SBNEventWeight& operator = (SBNEventWeight const&) = delete;
45 
46 private:
47  void produce(art::Event& e) override;
48  void beginRun(art::Run& run) override;
49 
50 private:
52  std::string fGenieModuleLabel;
54 };
55 
56 
57 SBNEventWeight::SBNEventWeight(fhicl::ParameterSet const& p)
58  : EDProducer{p},
59  fGenieModuleLabel(p.get<std::string>("generator_module_label", "generator")),
60  fAllowMissingTruth(p.get<bool>("AllowMissingTruth"))
61 {
62  const size_t n_func = fWeightManager.Configure(p, *this);
63  if (n_func > 0) {
64  produces<std::vector<sbn::evwgh::EventWeightMap> >();
65  produces<art::Assns<simb::MCTruth, sbn::evwgh::EventWeightMap> >();
66  produces<std::vector<sbn::evwgh::EventWeightParameterSet>, art::InRun>();
67  }
68 }
69 
70 
71 void SBNEventWeight::produce(art::Event& e) {
72  auto mcwghvec = std::make_unique<std::vector<EventWeightMap> >();
73  auto wghassns = std::make_unique<art::Assns<simb::MCTruth, sbn::evwgh::EventWeightMap> >();
74 
75  art::PtrMaker<sbn::evwgh::EventWeightMap> makeWeightPtr(e);
76 
77  // Get the MC generator information
78  std::vector<art::Ptr<simb::MCTruth> > mclist;
79  art::Handle<std::vector<simb::MCTruth>> mcTruthHandle;
80  if(!fGenieModuleLabel.empty()) e.getByLabel(fGenieModuleLabel, mcTruthHandle);
81  // Prooceed even with missing handle if we want to require the MCTruth to be
82  // found, so that an exception will be thrown explaining the problem.
83  if(mcTruthHandle.isValid() || !fAllowMissingTruth){
84  art::fill_ptr_vector(mclist, mcTruthHandle);
85 
86  // Loop over all truth objects (e.g. neutrinos) in this event
87  for (size_t i=0; i<mclist.size(); i++) {
88  const EventWeightMap mcwgh = fWeightManager.Run(e, i);
89  mcwghvec->push_back(std::move(mcwgh));
90 
91  art::Ptr<sbn::evwgh::EventWeightMap> wghPtr = makeWeightPtr(mcwghvec->size() - 1);
92  wghassns->addSingle(mclist.at(i), wghPtr);
93  }
94  }
95 
96  e.put(std::move(mcwghvec));
97  e.put(std::move(wghassns));
98 }
99 
100 
101 void SBNEventWeight::beginRun(art::Run& run) {
102  auto p = std::make_unique<std::vector<EventWeightParameterSet> >();
103 
104  for (auto const& it : fWeightManager.GetWeightCalcMap()) {
105  p->push_back(it.second->fParameterSet);
106  }
107 
108  run.put(std::move(p));
109 }
110 
111  } // namespace evwgh
112 } // namespace sbn
113 
114 DEFINE_ART_MODULE(sbn::evwgh::SBNEventWeight)
115 
std::map< std::string, std::vector< float > > EventWeightMap
Container for event-level weights.
pdgs p
Definition: selectors.fcl:22
SBNEventWeight(fhicl::ParameterSet const &p)
std::map< std::string, WeightCalc * > GetWeightCalcMap()
SBNEventWeight & operator=(SBNEventWeight const &)=delete
do i e
EventWeightMap Run(art::Event &e, const int inu)
void produce(art::Event &e) override
void beginRun(art::Run &run) override