GENIERockGen_module.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 ///
3 /// \brief GENIE Rock events generator
4 /// \author rhatcher@fnal.gov
5 /// \date
6 ///
7 ////////////////////////////////////////////////////////////////////////
8 
9 #include <cassert>
10 #include <cstdlib>
11 #include <cmath>
12 #include <string>
13 #include <sstream>
14 #include <vector>
15 #include <map>
16 #include <unistd.h>
17 
18 // ROOT includes
19 #include "TStopwatch.h"
20 
21 // Framework includes
24 #include "fhiclcpp/ParameterSet.h"
30 
31 // NOvA includes
32 #include "nugen/EventGeneratorBase/GENIE/GENIEHelper.h"
33 #include "Geometry/Geometry.h"
34 #include "RawData/DAQHeader.h"
39 #include "SummaryData/POTSum.h"
40 #include "SummaryData/RunData.h"
41 #include "Utilities/AssociationUtil.h"
42 
43 
44 namespace evgen {
45  /// A module to check the results from the Monte Carlo generator
46  class GENIERockGen : public art::EDProducer {
47  public:
48  explicit GENIERockGen(fhicl::ParameterSet const &pset);
49  virtual ~GENIERockGen();
50 
51  void produce(art::Event& evt);
52  void beginJob();
53  void beginRun(art::Run &run);
54  void endSubRun(art::SubRun &sr);
55 
56  private:
57 
58  evgb::GENIEHelper *fGENIEHelp; ///< GENIEHelper object
59  TStopwatch fStopwatch; ///keep track of how long it takes to run the job
61  };
62 }
63 
64 namespace evgen{
65 
66  //___________________________________________________________________________
68  : fSpillCounter(0)
69  {
70  fStopwatch.Start();
71 
72  produces< std::vector<simb::MCTruth> >();
73  produces< std::vector<simb::MCFlux> >();
74  produces< std::vector<simb::GTruth> >();
75  produces< sumdata::POTSum, art::InSubRun >();
76  produces< sumdata::RunData, art::InRun >();
77  // Associate every truth with the flux it came from
78  produces< art::Assns<simb::MCTruth, simb::MCFlux> >();
79  produces< art::Assns<simb::MCTruth, simb::GTruth> >();
80 
82  fGENIEHelp = new evgb::GENIEHelper(pset,
83  geo->ROOTGeoManager(),
84  geo->ROOTFile(),
85  geo->TotalMass(pset.get< std::string>("TopVolume").c_str()));
86  }
87 
88  //___________________________________________________________________________
90  {
91  fStopwatch.Stop();
92  mf::LogInfo("GENIERockGen") << "Real time to produce file: " << fStopwatch.RealTime();
93  }
94 
95  //___________________________________________________________________________
97  {
98  }
99 
100  //___________________________________________________________________________
102  {
103 
104  // grab the geometry object to see what geometry we are using
106 
107  std::unique_ptr<sumdata::RunData> runcol(new sumdata::RunData(geo->DetId(),
108  geo->FileBaseName()));
109 
110  run.put(std::move(runcol));
111 
112  // initialize the GENIEHelper here rather than in beginJob to avoid problems with
113  // the Geometry reloading at a run boundary. If we ever make more than one run
114  // in a single job we will have to re-evaluate
115  fGENIEHelp->Initialize();
116 
117  return;
118  }
119 
120  //___________________________________________________________________________
121 
123  {
124 
125  std::unique_ptr< sumdata::POTSum > p(new sumdata::POTSum);
126 
127  p->totpot = fGENIEHelp->TotalExposure();
128  p->totgoodpot = fGENIEHelp->TotalExposure();
129  p->totspills = fSpillCounter;
130  p->goodspills = fSpillCounter;
131  p->Print(std::cout);
132 
133  sr.put(std::move(p));
134 
135 
136  }
137 
138  //___________________________________________________________________________
140  {
141 
142  ///check if we are going to make events for this spill - could be
143  ///that we are using flux histograms and the poisson number for
144  ///how many interactions to make is 0
145  if(fGENIEHelp->Stop()){
146  throw cet::exception("NoNuInteraction")
147  << "no neutrino interaction made for this spill\n"
148  << __FILE__ << ":" << __LINE__ << "\n";
149  return;
150  }
151 
152  std::unique_ptr< std::vector<simb::MCTruth> > truthcol(new std::vector<simb::MCTruth>);
153  std::unique_ptr< std::vector<simb::MCFlux> > fluxcol (new std::vector<simb::MCFlux >);
154  std::unique_ptr< std::vector<simb::GTruth> > gtruthcol (new std::vector<simb::GTruth >);
155  std::unique_ptr< art::Assns<simb::MCTruth, simb::GTruth> > tgtassn(new art::Assns<simb::MCTruth, simb::GTruth>);
156  std::unique_ptr< art::Assns<simb::MCTruth, simb::MCFlux> > assns(new art::Assns<simb::MCTruth, simb::MCFlux>);
157 
159 
160  while(!fGENIEHelp->Stop()){
161 
162  simb::MCTruth truth;
164  simb::GTruth gTruth;
165 
166  ///GENIEHelper returns a false in the sample method if
167  ///either no neutrino was generated, or the interaction
168  ///occurred beyond the detector's z extent - ie something we
169  ///would never see anyway.
170  if(fGENIEHelp->Sample(truth, flux, gTruth)){
171 
172 
173  const simb::MCParticle nu = truth.GetNeutrino().Nu();
174 
175  if(fabs(nu.Vx())>geo->DetHalfWidth()
176  || fabs(nu.Vy())>geo->DetHalfHeight()
177  || nu.Vz()> geo->DetLength()
178  || nu.Vz()<0 ){
179 
180 
181  truthcol->push_back(truth);
182  gtruthcol->push_back(gTruth);
183  fluxcol->push_back(flux);
184 
185  util::CreateAssn(*this, evt, *truthcol, *fluxcol, *assns,
186  fluxcol->size()-1, fluxcol->size());
187 
188  util::CreateAssn(*this, evt, *truthcol, *gtruthcol, *tgtassn,
189  gtruthcol->size()-1, gtruthcol->size());
190  }
191 
192  }///end if genie was able to make an event
193 
194 
195  }///end event generation loop
196 
197  // put the collections in the event
198  evt.put(std::move(truthcol));
199  evt.put(std::move(fluxcol));
200  evt.put(std::move(gtruthcol));
201  evt.put(std::move(assns));
202  evt.put(std::move(tgtassn));
203 
204  return;
205  }
206 
207 }//namespace
208 
209 namespace evgen { DEFINE_ART_MODULE(GENIERockGen) }
void beginRun(art::Run &run)
static bool CreateAssn(art::EDProducer const &prod, art::Event &evt, std::vector< T > &a, art::Ptr< U > b, art::Assns< T, U > &assn, size_t indx=UINT_MAX, std::string const &instance=std::string())
Create a 1 to 1 association between a new product and one already in the event.
void endSubRun(art::SubRun &sr)
const simb::MCNeutrino & GetNeutrino() const
Definition: MCTruth.h:76
fvar< T > fabs(const fvar< T > &x)
Definition: fabs.hpp:15
MaybeLogger_< ELseverityLevel::ELsev_info, false > LogInfo
int fSpillCounter
keep track of how long it takes to run the job
const char * p
Definition: xmltok.h:285
const simb::MCParticle & Nu() const
Definition: MCNeutrino.h:146
double DetLength() const
::xsd::cxx::tree::exception< char > exception
Definition: Database.h:225
void produce(art::Event &evt)
art::ProductID put(std::unique_ptr< PROD > &&)
Definition: Run.h:149
DEFINE_ART_MODULE(TestTMapFile)
Particle class.
Loaders::FluxType flux
object containing MC flux information
Definition: Run.h:31
ProductID put(std::unique_ptr< PROD > &&product)
Definition: Event.h:102
A module to check the results from the Monte Carlo generator.
T get(std::string const &key) const
Definition: ParameterSet.h:231
novadaq::cnv::DetId DetId() const
Prefer ds::DetectorService::DetId() instead.
Definition: GeometryBase.h:243
int evt
caf::StandardRecord * sr
double DetHalfHeight() const
Definition: run.py:1
OStream cout
Definition: OStream.cxx:6
GENIERockGen(fhicl::ParameterSet const &pset)
evgb::GENIEHelper * fGENIEHelp
GENIEHelper object.
double Vx(const int i=0) const
Definition: MCParticle.h:220
double DetHalfWidth() const
ProductID put(std::unique_ptr< PROD > &&)
double Vz(const int i=0) const
Definition: MCParticle.h:222
Event generator information.
Definition: MCTruth.h:32
Helper for AttenCurve.
Definition: Path.h:10
Module to generate only pions from cosmic rays.
size_t size() const
double Vy(const int i=0) const
Definition: MCParticle.h:221
Encapsulate the geometry of one entire detector (near, far, ndos)
std::string FileBaseName() const
enum BeamMode string