LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
MARLEYGen_module.cc
Go to the documentation of this file.
1 
9 // standard library includes
10 #include <memory>
11 #include <string>
12 
13 // framework includes
19 #include "art_root_io/TFileService.h"
20 #include "fhiclcpp/types/Table.h"
21 
22 // art extensions
24 
25 // LArSoft includes
29 
32 
33 // ROOT includes
34 #include "TTree.h"
35 
36 namespace evgen {
37  class MarleyGen;
38 }
39 
41 
42 public:
43  using Name = fhicl::Name;
45 
48  struct KeysToIgnore {
49  std::set<std::string> operator()() { return {"marley_parameters"}; }
50  };
51 
53  struct Config {
54 
56  Name("vertex"),
57  Comment("Configuration for selecting the vertex location(s)")};
58 
60  Name("module_type"),
61  Comment(""),
62  "MARLEYGen" // default value
63  };
64 
65  }; // struct Config
66 
67  // Type to enable FHiCL parameter validation by art
69 
70  // Configuration-checking constructors
71  explicit MarleyGen(const Parameters& p);
72 
73  virtual void produce(art::Event& e) override;
74  virtual void beginRun(art::Run& run) override;
75 
76  virtual void reconfigure(const Parameters& p);
77 
78 private:
79  // Object that provides an interface to the MARLEY event generator
80  std::unique_ptr<evgen::MARLEYHelper> fMarleyHelper;
81 
82  // Algorithm that allows us to sample vertex locations within the active
83  // volume(s) of the detector
84  std::unique_ptr<evgen::ActiveVolumeVertexSampler> fVertexSampler;
85 
86  // unique_ptr to the current event created by MARLEY
87  std::unique_ptr<marley::Event> fEvent;
88 
89  // the MARLEY event TTree
90  TTree* fEventTree;
91 
92  // Run, subrun, and event numbers from the art::Event being processed
93  uint_fast32_t fRunNumber;
94  uint_fast32_t fSubRunNumber;
95  uint_fast32_t fEventNumber;
96 };
97 
98 //------------------------------------------------------------------------------
100  : EDProducer{p.get_PSet()}
101  , fEvent(new marley::Event)
102  , fRunNumber(0)
103  , fSubRunNumber(0)
104  , fEventNumber(0)
105 {
106  // Configure the module (including MARLEY itself) using the FHiCL parameters
107  this->reconfigure(p);
108 
109  // Create a ROOT TTree using the TFileService that will store the MARLEY
110  // event objects (useful for debugging purposes)
112  fEventTree = tfs->make<TTree>("MARLEY_event_tree", "Neutrino events generated by MARLEY");
113  fEventTree->Branch("event", "marley::Event", fEvent.get());
114 
115  // Add branches that give the art::Event run, subrun, and event numbers for
116  // easy match-ups between the MARLEY and art TTrees. All three are recorded
117  // as 32-bit unsigned integers.
118  fEventTree->Branch("run_number", &fRunNumber, "run_number/i");
119  fEventTree->Branch("subrun_number", &fSubRunNumber, "subrun_number/i");
120  fEventTree->Branch("event_number", &fEventNumber, "event_number/i");
121 
122  produces<std::vector<simb::MCTruth>>();
123  produces<sumdata::RunData, art::InRun>();
124 }
125 
126 //------------------------------------------------------------------------------
128 {
130  run.put(std::make_unique<sumdata::RunData>(geo->DetectorName()), art::fullRun());
131 }
132 
133 //------------------------------------------------------------------------------
135 {
136  // Get the run, subrun, and event numbers from the current art::Event
137  fRunNumber = e.run();
138  fSubRunNumber = e.subRun();
139  fEventNumber = e.event();
140 
141  std::unique_ptr<std::vector<simb::MCTruth>> truthcol(new std::vector<simb::MCTruth>);
142 
143  // Get the primary vertex location for this event
145  TLorentzVector vertex_pos = fVertexSampler->sample_vertex_pos(*geo);
146 
147  // Create the MCTruth object, and retrieve the marley::Event object
148  // that was generated as it was created
149  simb::MCTruth truth = fMarleyHelper->create_MCTruth(vertex_pos, fEvent.get());
150 
151  // Write the marley::Event object to the event tree
152  fEventTree->Fill();
153 
154  truthcol->push_back(truth);
155 
156  e.put(std::move(truthcol));
157 }
158 
159 //------------------------------------------------------------------------------
161 {
162  const auto& seed_service = art::ServiceHandle<rndm::NuRandomService>();
163  const auto& geom_service = art::ServiceHandle<geo::Geometry const>();
164 
165  // Create a new evgen::ActiveVolumeVertexSampler object based on the current
166  // configuration
167  fVertexSampler = std::make_unique<evgen::ActiveVolumeVertexSampler>(
168  p().vertex_, *seed_service, *geom_service, "MARLEY_Vertex_Sampler");
169 
170  // Create a new marley::Generator object based on the current configuration
171  fhicl::ParameterSet marley_pset = p.get_PSet().get<fhicl::ParameterSet>("marley_parameters");
172  fMarleyHelper = std::make_unique<MARLEYHelper>(marley_pset, *seed_service, "MARLEY");
173 }
174 
virtual void reconfigure(const Parameters &p)
SubRunNumber_t subRun() const
Definition: Event.cc:35
fhicl::Comment Comment
LArSoft interface to the MARLEY (Model of Argon Reaction Low Energy Yields) supernova neutrino event ...
virtual void beginRun(art::Run &run) override
constexpr auto fullRun()
EDProducer(fhicl::ParameterSet const &pset)
Definition: EDProducer.cc:6
PutHandle< PROD > put(std::unique_ptr< PROD > &&edp, std::string const &instance={})
Definition: Run.h:121
std::set< std::string > operator()()
std::unique_ptr< evgen::MARLEYHelper > fMarleyHelper
Definition: Run.h:37
PutHandle< PROD > put(std::unique_ptr< PROD > &&edp, std::string const &instance={})
Definition: Event.h:77
Algorithm that samples vertex locations uniformly within the active volume of a detector. It is fully experiment-agnostic and multi-TPC aware.
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:65
uint_fast32_t fRunNumber
uint_fast32_t fSubRunNumber
std::unique_ptr< evgen::ActiveVolumeVertexSampler > fVertexSampler
Collection of configuration parameters for the module.
EventNumber_t event() const
Definition: Event.cc:41
virtual void produce(art::Event &e) override
auto const & get_PSet() const
Definition: ProducerTable.h:47
An art service to assist in the distribution of guaranteed unique seeds to all engines within an art ...
uint_fast32_t fEventNumber
std::string const & DetectorName() const
Returns a string with the name of the detector, as configured.
Definition: GeometryCore.h:203
Event generator information.
Definition: MCTruth.h:32
Float_t e
Definition: plot.C:35
MarleyGen(const Parameters &p)
RunNumber_t run() const
Definition: Event.cc:29
Namespace collecting geometry-related classes utilities.
Event Generation using GENIE, cosmics or single particles.
std::unique_ptr< marley::Event > fEvent
art framework interface to geometry description