All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MARLEYGen_module.cc
Go to the documentation of this file.
1 //////////////////////////////////////////////////////////////////////////////
2 /// \file MARLEYGen_module.cc
3 /// \brief LArSoft interface to the MARLEY (Model of Argon Reaction Low Energy
4 /// Yields) supernova neutrino event generator
5 ///
6 /// \author Steven Gardiner <sjgardiner@ucdavis.edu>
7 //////////////////////////////////////////////////////////////////////////////
8 
9 // standard library includes
10 #include <memory>
11 #include <string>
12 
13 // framework includes
14 #include "art/Framework/Core/EDProducer.h"
15 #include "art/Framework/Core/ModuleMacros.h"
16 #include "art/Framework/Principal/Event.h"
17 #include "art/Framework/Principal/Run.h"
18 #include "art/Framework/Services/Registry/ServiceHandle.h"
19 #include "art_root_io/TFileService.h"
20 #include "fhiclcpp/types/Table.h"
21 
22 // art extensions
23 #include "nurandom/RandomUtils/NuRandomService.h"
24 
25 // LArSoft includes
28 #include "nusimdata/SimulationBase/MCTruth.h"
29 
32 
33 // ROOT includes
34 #include "TTree.h"
35 
36 namespace evgen {
37  class MarleyGen;
38 }
39 
40 class evgen::MarleyGen : public art::EDProducer {
41 
42  public:
43 
44  using Name = fhicl::Name;
45  using Comment = fhicl::Comment;
46 
47  /// Ignore the marley_parameters FHiCL table during the validation
48  /// step (MARLEY will take care of that by itself)
49  struct KeysToIgnore {
50  std::set< std::string > operator()()
51  {
52  return { "marley_parameters" };
53  }
54  };
55 
56  /// Collection of configuration parameters for the module
57  struct Config {
58 
59  fhicl::Table<evgen::ActiveVolumeVertexSampler::Config> vertex_ {
60  Name("vertex"),
61  Comment("Configuration for selecting the vertex location(s)")
62  };
63 
64  fhicl::Atom<std::string> module_type_ {
65  Name("module_type"),
66  Comment(""),
67  "MARLEYGen" // default value
68  };
69 
70  }; // struct Config
71 
72  // Type to enable FHiCL parameter validation by art
73  using Parameters = art::EDProducer::Table<Config, KeysToIgnore>;
74 
75  // Configuration-checking constructors
76  explicit MarleyGen(const Parameters& p);
77 
78  virtual void produce(art::Event& e) override;
79  virtual void beginRun(art::Run& run) override;
80 
81  virtual void reconfigure(const Parameters& p);
82 
83  private:
84 
85  // Object that provides an interface to the MARLEY event generator
86  std::unique_ptr<evgen::MARLEYHelper> fMarleyHelper;
87 
88  // Algorithm that allows us to sample vertex locations within the active
89  // volume(s) of the detector
90  std::unique_ptr<evgen::ActiveVolumeVertexSampler> fVertexSampler;
91 
92  // unique_ptr to the current event created by MARLEY
93  std::unique_ptr<marley::Event> fEvent;
94 
95  // the MARLEY event TTree
96  TTree* fEventTree;
97 
98  // Run, subrun, and event numbers from the art::Event being processed
99  uint_fast32_t fRunNumber;
100  uint_fast32_t fSubRunNumber;
101  uint_fast32_t fEventNumber;
102 };
103 
104 //------------------------------------------------------------------------------
106  : EDProducer{ p.get_PSet() },
107  fEvent(new marley::Event), fRunNumber(0), fSubRunNumber(0), fEventNumber(0)
108 {
109  // Configure the module (including MARLEY itself) using the FHiCL parameters
110  this->reconfigure( p );
111 
112  // Create a ROOT TTree using the TFileService that will store the MARLEY
113  // event objects (useful for debugging purposes)
114  art::ServiceHandle<art::TFileService const> tfs;
115  fEventTree = tfs->make<TTree>("MARLEY_event_tree",
116  "Neutrino events generated by MARLEY");
117  fEventTree->Branch("event", "marley::Event", fEvent.get());
118 
119  // Add branches that give the art::Event run, subrun, and event numbers for
120  // easy match-ups between the MARLEY and art TTrees. All three are recorded
121  // as 32-bit unsigned integers.
122  fEventTree->Branch("run_number", &fRunNumber, "run_number/i");
123  fEventTree->Branch("subrun_number", &fSubRunNumber, "subrun_number/i");
124  fEventTree->Branch("event_number", &fEventNumber, "event_number/i");
125 
126  produces< std::vector<simb::MCTruth> >();
128 }
129 
130 //------------------------------------------------------------------------------
131 void evgen::MarleyGen::beginRun(art::Run& run)
132 {
133  art::ServiceHandle<geo::Geometry const> geo;
134  run.put(std::make_unique<sumdata::RunData>(geo->DetectorName()));
135 }
136 
137 //------------------------------------------------------------------------------
138 void evgen::MarleyGen::produce(art::Event& e)
139 {
140  // Get the run, subrun, and event numbers from the current art::Event
141  fRunNumber = e.run();
142  fSubRunNumber = e.subRun();
143  fEventNumber = e.event();
144 
145  std::unique_ptr< std::vector<simb::MCTruth> >
146  truthcol(new std::vector<simb::MCTruth>);
147 
148  // Get the primary vertex location for this event
149  art::ServiceHandle<geo::Geometry const> geo;
150  TLorentzVector vertex_pos = fVertexSampler->sample_vertex_pos(*geo);
151 
152  // Create the MCTruth object, and retrieve the marley::Event object
153  // that was generated as it was created
154  simb::MCTruth truth = fMarleyHelper->create_MCTruth(vertex_pos,
155  fEvent.get());
156 
157  // Write the marley::Event object to the event tree
158  fEventTree->Fill();
159 
160  truthcol->push_back(truth);
161 
162  e.put(std::move(truthcol));
163 }
164 
165 //------------------------------------------------------------------------------
167 {
168  const auto& seed_service = art::ServiceHandle<rndm::NuRandomService>();
169  const auto& geom_service = art::ServiceHandle<geo::Geometry const>();
170 
171  // Create a new evgen::ActiveVolumeVertexSampler object based on the current
172  // configuration
173  fVertexSampler = std::make_unique<evgen::ActiveVolumeVertexSampler>(
174  p().vertex_, *seed_service, *geom_service, "MARLEY_Vertex_Sampler");
175 
176  // Create a new marley::Generator object based on the current configuration
177  fhicl::ParameterSet marley_pset = p.get_PSet().get< fhicl::ParameterSet >(
178  "marley_parameters" );
179  fMarleyHelper = std::make_unique<MARLEYHelper>( marley_pset,
180  *seed_service, "MARLEY" );
181 }
182 
183 DEFINE_ART_MODULE(evgen::MarleyGen)
virtual void reconfigure(const Parameters &p)
fhicl::Atom< std::string > module_type_
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
pdgs p
Definition: selectors.fcl:22
produces< sumdata::RunData, art::InRun >()
for pfile in ack l reconfigure(.*) override"` do echo "checking $
std::unique_ptr< evgen::MARLEYHelper > fMarleyHelper
fhicl::Table< evgen::ActiveVolumeVertexSampler::Config > vertex_
Algorithm that samples vertex locations uniformly within the active volume of a detector. It is fully experiment-agnostic and multi-TPC aware.
uint_fast32_t fRunNumber
uint_fast32_t fSubRunNumber
std::unique_ptr< evgen::ActiveVolumeVertexSampler > fVertexSampler
Collection of configuration parameters for the module.
BEGIN_PROLOG vertical distance to the surface Name
virtual void produce(art::Event &e) override
do i e
uint_fast32_t fEventNumber
art::ServiceHandle< art::TFileService > tfs
MarleyGen(const Parameters &p)
std::unique_ptr< marley::Event > fEvent
art framework interface to geometry description
std::set< std::string > operator()()
art::EDProducer::Table< Config, KeysToIgnore > Parameters