All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
NeutronOsc_module.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 // Class: NeutronOsc
3 // Module Type: producer
4 // GENIE neutron-antineutron oscillation generator
5 //
6 // Adapted from NucleonDecay_module.cc (tjyang@fnal.gov)
7 // by jhewes15@fnal.gov
8 //
9 // Neutron-antineutron oscillation mode ID:
10 // ---------------------------------------------------------
11 // ID | Decay Mode
12 // |
13 // ---------------------------------------------------------
14 // 0 | Random oscillation mode
15 // 1 | p + nbar --> \pi^{+} + \pi^{0}
16 // 2 | p + nbar --> \pi^{+} + 2\pi^{0}
17 // 3 | p + nbar --> \pi^{+} + 3\pi^{0}
18 // 4 | p + nbar --> 2\pi^{+} + \pi^{-} + \pi^{0}
19 // 5 | p + nbar --> 2\pi^{+} + \pi^{-} + 2\pi^{0}
20 // 6 | p + nbar --> 2\pi^{+} + \pi^{-} + 2\omega^{0}
21 // 7 | p + nbar --> 3\pi^{+} + 2\pi^{-} + \pi^{0}
22 // 8 | n + nbar --> \pi^{+} + \pi^{-}
23 // 9 | n + nbar --> 2\pi^{0}
24 // 10 | n + nbar --> \pi^{+} + \pi^{-} + \pi^{0}
25 // 11 | n + nbar --> \pi^{+} + \pi^{-} + 2\pi^{0}
26 // 12 | n + nbar --> \pi^{+} + \pi^{-} + 3\pi^{0}
27 // 13 | n + nbar --> 2\pi^{+} + 2\pi^{-}
28 // 14 | n + nbar --> 2\pi^{+} + 2\pi^{-} + \pi^{0}
29 // 15 | n + nbar --> \pi^{+} + \pi^{-} + \omega^{0}
30 // 16 | n + nbar --> 2\pi^{+} + 2\pi^{-} + 2\pi^{0}
31 // ---------------------------------------------------------
32 //
33 ////////////////////////////////////////////////////////////////////////
34 
35 #include "art/Framework/Core/EDProducer.h"
36 #include "art/Framework/Core/ModuleMacros.h"
37 #include "art/Framework/Principal/Event.h"
38 #include "art/Framework/Principal/Run.h"
39 #include "art/Framework/Services/Registry/ServiceHandle.h"
40 #include "fhiclcpp/ParameterSet.h"
41 #include "messagefacility/MessageLogger/MessageLogger.h"
42 
43 // GENIE includes
44 #include "Framework/Algorithm/AlgFactory.h"
45 #include "Framework/EventGen/EventRecordVisitorI.h"
46 #include "Framework/EventGen/EventRecord.h"
47 #include "Physics/NNBarOscillation/NNBarOscMode.h"
48 #include "Framework/ParticleData/PDGLibrary.h"
49 #include "Framework/GHEP/GHepParticle.h"
50 #include "Framework/Utils/AppInit.h"
51 
52 // larsoft includes
53 #include "nusimdata/SimulationBase/MCTruth.h"
54 #include "nusimdata/SimulationBase/MCParticle.h"
55 
56 #include "nugen/EventGeneratorBase/GENIE/GENIE2ART.h"
57 
60 #include "nurandom/RandomUtils/NuRandomService.h"
61 
62 // c++ includes
63 #include <memory>
64 #include <string>
65 
66 #include "CLHEP/Random/RandFlat.h"
67 
68 namespace evgen {
69  class NeutronOsc;
70 }
71 
72 class evgen::NeutronOsc : public art::EDProducer {
73 public:
74  explicit NeutronOsc(fhicl::ParameterSet const & p);
75  // The destructor generated by the compiler is fine for classes
76  // without bare pointers or other resource use.
77 
78  // Plugins should not be copied or assigned.
79  NeutronOsc(NeutronOsc const &) = delete;
80  NeutronOsc(NeutronOsc &&) = delete;
81  NeutronOsc & operator = (NeutronOsc const &) = delete;
82  NeutronOsc & operator = (NeutronOsc &&) = delete;
83 
84  // Required functions.
85  void produce(art::Event & e) override;
86 
87  // Selected optional functions.
88  void beginRun(art::Run& run) override;
89 
90 private:
91 
92  // Additional functions
93  int SelectAnnihilationMode(int pdg_code);
94 
95  // Declare member data here.
96  const genie::EventRecordVisitorI * mcgen;
97  genie::NNBarOscMode_t gOptDecayMode = genie::kNONull; // neutron-antineutron oscillation mode
98  CLHEP::RandFlat flatDist;
99 
100 };
101 
102 
103 evgen::NeutronOsc::NeutronOsc(fhicl::ParameterSet const & p)
104  : art::EDProducer{p}
105  // create a default random engine; obtain the random seed from NuRandomService,
106  // unless overridden in configuration with key "Seed"
107  , flatDist{art::ServiceHandle<rndm::NuRandomService>{}->createEngine(*this, p, "Seed")}
108 {
109  genie::PDGLibrary::Instance(); //Ensure Messenger is started first in GENIE.
110 
111  string sname = "genie::EventGenerator";
112  // GENIE v2 // string sconfig = "NeutronOsc";
113  string sconfig = "NNBarOsc";
114  // GENIE v3 needs a tune (even if irrelevant)
115  evgb::SetEventGeneratorListAndTune("Default","Default");
116 
117  genie::AlgFactory * algf = genie::AlgFactory::Instance();
119  dynamic_cast<const genie::EventRecordVisitorI *> (algf->GetAlgorithm(sname,sconfig));
120  if(!mcgen) {
121  throw cet::exception("NeutronOsc") << "Couldn't instantiate the neutron-antineutron oscillation generator";
122  }
123  int fDecayMode = p.get<int>("DecayMode");
124  gOptDecayMode = (genie::NNBarOscMode_t) fDecayMode;
125 
126  produces< std::vector<simb::MCTruth> >();
127  produces< sumdata::RunData, art::InRun >();
128 
129  unsigned int seed = art::ServiceHandle<rndm::NuRandomService>()->getSeed();
130  genie::utils::app_init::RandGen(seed);
131 }
132 
133 void evgen::NeutronOsc::produce(art::Event & e)
134 {
135  // Implementation of required member function here.
136  genie::EventRecord * event = new genie::EventRecord;
137  int target = 1000180400; //Only use argon target
138  int decay = SelectAnnihilationMode(target);
139  genie::Interaction * interaction = genie::Interaction::NOsc(target,decay);
140  event->AttachSummary(interaction);
141 
142  // Simulate decay
143  mcgen->ProcessEventRecord(event);
144 
145 // genie::Interaction *inter = event->Summary();
146 // const genie::InitialState &initState = inter->InitState();
147 // std::cout<<"initState = "<<initState.AsString()<<std::endl;
148 // const genie::ProcessInfo &procInfo = inter->ProcInfo();
149 // std::cout<<"procInfo = "<<procInfo.AsString()<<std::endl;
150  MF_LOG_DEBUG("NeutronOsc")
151  << "Generated event: " << *event;
152 
153  std::unique_ptr< std::vector<simb::MCTruth> > truthcol(new std::vector<simb::MCTruth>);
154  simb::MCTruth truth;
155 
156  art::ServiceHandle<geo::Geometry const> geo;
157 
158  // Find boundary of active volume
159  double minx = 1e9;
160  double maxx = -1e9;
161  double miny = 1e9;
162  double maxy = -1e9;
163  double minz = 1e9;
164  double maxz = -1e9;
165  for (size_t i = 0; i<geo->NTPC(); ++i){
166  const geo::TPCGeo &tpc = geo->TPC(i);
167  if (minx>tpc.MinX()) minx = tpc.MinX();
168  if (maxx<tpc.MaxX()) maxx = tpc.MaxX();
169  if (miny>tpc.MinY()) miny = tpc.MinY();
170  if (maxy<tpc.MaxY()) maxy = tpc.MaxY();
171  if (minz>tpc.MinZ()) minz = tpc.MinZ();
172  if (maxz<tpc.MaxZ()) maxz = tpc.MaxZ();
173  }
174 
175  // Assign vertice position
176  double X0 = flatDist.fire( minx, maxx );
177  double Y0 = flatDist.fire( miny, maxy );
178  double Z0 = flatDist.fire( minz, maxz );
179 
180  TIter partitr(event);
181  genie::GHepParticle *part = 0;
182  // GHepParticles return units of GeV/c for p. the V_i are all in fermis
183  // and are relative to the center of the struck nucleus.
184  // add the vertex X/Y/Z to the V_i for status codes 0 and 1
185  int trackid = 0;
186  std::string primary("primary");
187 
188  while( (part = dynamic_cast<genie::GHepParticle *>(partitr.Next())) ){
189 
190  simb::MCParticle tpart(trackid,
191  part->Pdg(),
192  primary,
193  part->FirstMother(),
194  part->Mass(),
195  part->Status());
196 
197  TLorentzVector pos(X0, Y0, Z0, 0);
198  TLorentzVector mom(part->Px(), part->Py(), part->Pz(), part->E());
199  tpart.AddTrajectoryPoint(pos,mom);
200  if(part->PolzIsSet()) {
201  TVector3 polz;
202  part->GetPolarization(polz);
203  tpart.SetPolarization(polz);
204  }
205  truth.Add(tpart);
206 
207  ++trackid;
208  }// end loop to convert GHepParticles to MCParticles
209  truth.SetOrigin(simb::kUnknown);
210  truthcol->push_back(truth);
211  //FillHistograms(truth);
212  e.put(std::move(truthcol));
213 
214  delete event;
215 }
216 
217 void evgen::NeutronOsc::beginRun(art::Run& run)
218 {
219  art::ServiceHandle<geo::Geometry const> geo;
220  run.put(std::make_unique<sumdata::RunData>(geo->DetectorName()));
221 }
222 
223 
225 {
226  // if the mode is set to 'random' (the default), pick one at random!
227  if ((int)gOptDecayMode == 0) {
228  int mode;
229 
230  std::string pdg_string = std::to_string(static_cast<long long>(pdg_code));
231  if (pdg_string.size() != 10) {
232  std::cout << "Expecting PDG code to be a 10-digit integer; instead, it's the following: " << pdg_string << std::endl;
233  exit(1);
234  }
235 
236  // count number of protons & neutrons
237  int n_nucleons = std::stoi(pdg_string.substr(6,3)) - 1;
238  int n_protons = std::stoi(pdg_string.substr(3,3));
239 
240  // factor proton / neutron ratio into branching ratios
241  double proton_frac = ((double)n_protons) / ((double)n_nucleons);
242  double neutron_frac = 1 - proton_frac;
243 
244  // set branching ratios, taken from bubble chamber data
245  const int n_modes = 16;
246  double br [n_modes] = { 0.010, 0.080, 0.100, 0.220,
247  0.360, 0.160, 0.070, 0.020,
248  0.015, 0.065, 0.110, 0.280,
249  0.070, 0.240, 0.100, 0.100 };
250 
251  for (int i = 0; i < n_modes; i++) {
252  if (i < 7)
253  br[i] *= proton_frac;
254  else
255  br[i] *= neutron_frac;
256  }
257 
258  // randomly generate a number between 1 and 0
259  double p = flatDist.fire();
260 
261  // loop through all modes, figure out which one our random number corresponds to
262  double threshold = 0;
263  for (int i = 0; i < n_modes; i++) {
264  threshold += br[i];
265  if (p < threshold) {
266  // once we've found our mode, return it!
267  mode = i + 1;
268  return mode;
269  }
270  }
271 
272  // error message, in case the random number selection fails
273  std::cout << "Random selection of final state failed!" << std::endl;
274  exit(1);
275  }
276 
277  // if specific annihilation mode specified, just use that
278  else {
279  int mode = (int) gOptDecayMode;
280  return mode;
281  }
282 }
283 
284 DEFINE_ART_MODULE(evgen::NeutronOsc)
NeutronOsc(fhicl::ParameterSet const &p)
void beginRun(art::Run &run) override
gOptDecayMode
process_name opdaq physics producers generator physics producers generator physics producers generator Z0
Definition: gen_protons.fcl:45
pdgs p
Definition: selectors.fcl:22
double MinX() const
Returns the world x coordinate of the start of the box.
Definition: BoxBoundedGeo.h:88
string sconfig
Geometry information for a single TPC.
Definition: TPCGeo.h:38
double MaxX() const
Returns the world x coordinate of the end of the box.
Definition: BoxBoundedGeo.h:91
process_name opdaq physics producers generator physics producers generator Y0
Definition: gen_protons.fcl:45
int fDecayMode
int SelectAnnihilationMode(int pdg_code)
standard_singlep gaussian distribution X0
Definition: multigen.fcl:8
const char mode
Definition: noise_ana.cxx:20
double MinZ() const
Returns the world z coordinate of the start of the box.
unsigned int seed
string sname
void produce(art::Event &e) override
double MaxY() const
Returns the world y coordinate of the end of the box.
const genie::EventRecordVisitorI * mcgen
std::string to_string(WindowPattern const &pattern)
genie::NNBarOscMode_t gOptDecayMode
double MaxZ() const
Returns the world z coordinate of the end of the box.
genie::AlgFactory * algf
do i e
double MinY() const
Returns the world y coordinate of the start of the box.
art framework interface to geometry description
BEGIN_PROLOG could also be cout
NeutronOsc & operator=(NeutronOsc const &)=delete
CLHEP::RandFlat flatDist