LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
NeutronOsc_module.cc
Go to the documentation of this file.
1 // 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 //
34 
40 #include "fhiclcpp/ParameterSet.h"
42 
43 // GENIE includes
44 #include "Framework/Algorithm/AlgFactory.h"
45 #include "Framework/EventGen/EventRecord.h"
46 #include "Framework/EventGen/EventRecordVisitorI.h"
47 #include "Framework/GHEP/GHepParticle.h"
48 #include "Framework/ParticleData/PDGLibrary.h"
49 #include "Framework/Utils/AppInit.h"
50 #include "Physics/NNBarOscillation/NNBarOscMode.h"
51 
52 // larsoft includes
55 
57 
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 
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  // Additional functions
92  int SelectAnnihilationMode(int pdg_code);
93 
94  // Declare member data here.
95  const genie::EventRecordVisitorI* mcgen;
96  genie::NNBarOscMode_t gOptDecayMode = genie::kNONull; // neutron-antineutron oscillation mode
97  CLHEP::RandFlat flatDist;
98 };
99 
101  : art::EDProducer{p}
102  // create a default random engine; obtain the random seed from NuRandomService,
103  // unless overridden in configuration with key "Seed"
105  -> registerAndSeedEngine(createEngine(0), p, "Seed")}
106 {
107  string sname = "genie::EventGenerator";
108  // GENIE v2 // string sconfig = "NeutronOsc";
109  string sconfig = "NNBarOsc";
110  // GENIE v3 needs a tune (even if irrelevant)
111  evgb::SetEventGeneratorListAndTune("Default", "Default");
112 
113  genie::PDGLibrary::Instance(); //Ensure Messenger is started first in GENIE.
114 
115  genie::AlgFactory* algf = genie::AlgFactory::Instance();
116  mcgen = dynamic_cast<const genie::EventRecordVisitorI*>(algf->GetAlgorithm(sname, sconfig));
117  if (!mcgen) {
118  throw cet::exception("NeutronOsc")
119  << "Couldn't instantiate the neutron-antineutron oscillation generator";
120  }
121  int fDecayMode = p.get<int>("DecayMode");
122  gOptDecayMode = (genie::NNBarOscMode_t)fDecayMode;
123 
124  produces<std::vector<simb::MCTruth>>();
125  produces<sumdata::RunData, art::InRun>();
126 
127  unsigned int seed = art::ServiceHandle<rndm::NuRandomService>()->getSeed();
128  genie::utils::app_init::RandGen(seed);
129 }
130 
132 {
133  // Implementation of required member function here.
134  genie::EventRecord* event = new genie::EventRecord;
135  int target = 1000180400; //Only use argon target
136  int decay = SelectAnnihilationMode(target);
137  genie::Interaction* interaction = genie::Interaction::NOsc(target, decay);
138  event->AttachSummary(interaction);
139 
140  // Simulate decay
141  mcgen->ProcessEventRecord(event);
142 
143  // genie::Interaction *inter = event->Summary();
144  // const genie::InitialState &initState = inter->InitState();
145  // std::cout<<"initState = "<<initState.AsString()<<std::endl;
146  // const genie::ProcessInfo &procInfo = inter->ProcInfo();
147  // std::cout<<"procInfo = "<<procInfo.AsString()<<std::endl;
148  MF_LOG_DEBUG("NeutronOsc") << "Generated event: " << *event;
149 
150  std::unique_ptr<std::vector<simb::MCTruth>> truthcol(new std::vector<simb::MCTruth>);
151  simb::MCTruth truth;
152 
154 
155  // Find boundary of active volume
156  double minx = 1e9;
157  double maxx = -1e9;
158  double miny = 1e9;
159  double maxy = -1e9;
160  double minz = 1e9;
161  double maxz = -1e9;
162  for (auto const& tpc : geo->Iterate<geo::TPCGeo>(geo::CryostatID{0})) {
163  if (minx > tpc.MinX()) minx = tpc.MinX();
164  if (maxx < tpc.MaxX()) maxx = tpc.MaxX();
165  if (miny > tpc.MinY()) miny = tpc.MinY();
166  if (maxy < tpc.MaxY()) maxy = tpc.MaxY();
167  if (minz > tpc.MinZ()) minz = tpc.MinZ();
168  if (maxz < tpc.MaxZ()) maxz = tpc.MaxZ();
169  }
170 
171  // Assign vertice position
172  double X0 = flatDist.fire(minx, maxx);
173  double Y0 = flatDist.fire(miny, maxy);
174  double Z0 = flatDist.fire(minz, maxz);
175 
176  TIter partitr(event);
177  genie::GHepParticle* part = 0;
178  // GHepParticles return units of GeV/c for p. the V_i are all in fermis
179  // and are relative to the center of the struck nucleus.
180  // add the vertex X/Y/Z to the V_i for status codes 0 and 1
181  int trackid = 0;
182  std::string primary("primary");
183 
184  while ((part = dynamic_cast<genie::GHepParticle*>(partitr.Next()))) {
185 
186  simb::MCParticle tpart(
187  trackid, part->Pdg(), primary, part->FirstMother(), part->Mass(), part->Status());
188 
189  TLorentzVector pos(X0, Y0, Z0, 0);
190  TLorentzVector mom(part->Px(), part->Py(), part->Pz(), part->E());
191  tpart.AddTrajectoryPoint(pos, mom);
192  if (part->PolzIsSet()) {
193  TVector3 polz;
194  part->GetPolarization(polz);
195  tpart.SetPolarization(polz);
196  }
197  truth.Add(tpart);
198 
199  ++trackid;
200  } // end loop to convert GHepParticles to MCParticles
201  truth.SetOrigin(simb::kUnknown);
202  truthcol->push_back(truth);
203  //FillHistograms(truth);
204  e.put(std::move(truthcol));
205 
206  delete event;
207 }
208 
210 {
212  run.put(std::make_unique<sumdata::RunData>(geo->DetectorName()), art::fullRun());
213 }
214 
216 {
217  // if the mode is set to 'random' (the default), pick one at random!
218  if ((int)gOptDecayMode == 0) {
219  int mode;
220 
221  std::string pdg_string = std::to_string(static_cast<long long>(pdg_code));
222  if (pdg_string.size() != 10) {
223  std::cout << "Expecting PDG code to be a 10-digit integer; instead, it's the following: "
224  << pdg_string << std::endl;
225  exit(1);
226  }
227 
228  // count number of protons & neutrons
229  int n_nucleons = std::stoi(pdg_string.substr(6, 3)) - 1;
230  int n_protons = std::stoi(pdg_string.substr(3, 3));
231 
232  // factor proton / neutron ratio into branching ratios
233  double proton_frac = ((double)n_protons) / ((double)n_nucleons);
234  double neutron_frac = 1 - proton_frac;
235 
236  // set branching ratios, taken from bubble chamber data
237  const int n_modes = 16;
238  double br[n_modes] = {0.010,
239  0.080,
240  0.100,
241  0.220,
242  0.360,
243  0.160,
244  0.070,
245  0.020,
246  0.015,
247  0.065,
248  0.110,
249  0.280,
250  0.070,
251  0.240,
252  0.100,
253  0.100};
254 
255  for (int i = 0; i < n_modes; i++) {
256  if (i < 7)
257  br[i] *= proton_frac;
258  else
259  br[i] *= neutron_frac;
260  }
261 
262  // randomly generate a number between 1 and 0
263  double p = flatDist.fire();
264 
265  // loop through all modes, figure out which one our random number corresponds to
266  double threshold = 0;
267  for (int i = 0; i < n_modes; i++) {
268  threshold += br[i];
269  if (p < threshold) {
270  // once we've found our mode, return it!
271  mode = i + 1;
272  return mode;
273  }
274  }
275 
276  // error message, in case the random number selection fails
277  std::cout << "Random selection of final state failed!" << std::endl;
278  exit(1);
279  }
280 
281  // if specific annihilation mode specified, just use that
282  else {
283  int mode = (int)gOptDecayMode;
284  return mode;
285  }
286 }
287 
details::range_type< T > Iterate() const
Initializes the specified ID with the ID of the first cryostat.
Definition: GeometryCore.h:541
base_engine_t & createEngine(seed_t seed)
NeutronOsc(fhicl::ParameterSet const &p)
void beginRun(art::Run &run) override
constexpr auto fullRun()
void SetOrigin(simb::Origin_t origin)
Definition: MCTruth.h:82
EDProducer(fhicl::ParameterSet const &pset)
Definition: EDProducer.cc:6
Geometry information for a single TPC.
Definition: TPCGeo.h:36
PutHandle< PROD > put(std::unique_ptr< PROD > &&edp, std::string const &instance={})
Definition: Run.h:121
Functions for transforming GENIE objects into ART objects (and back)
Particle class.
int SelectAnnihilationMode(int pdg_code)
Definition: Run.h:37
PutHandle< PROD > put(std::unique_ptr< PROD > &&edp, std::string const &instance={})
Definition: Event.h:77
TString part[npart]
Definition: Style.C:32
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:65
long seed
Definition: chem4.cc:67
decltype(auto) constexpr to_string(T &&obj)
ADL-aware version of std::to_string.
An art service to assist in the distribution of guaranteed unique seeds to all engines within an art ...
void produce(art::Event &e) override
const genie::EventRecordVisitorI * mcgen
void Add(simb::MCParticle const &part)
Definition: MCTruth.h:80
void SetEventGeneratorListAndTune(const std::string &evtlistname="", const std::string &tunename="${GENIE_XSEC_TUNE}")
Definition: GENIE2ART.cxx:128
genie::NNBarOscMode_t gOptDecayMode
#define MF_LOG_DEBUG(id)
cout<< "-> Edep in the target
Definition: analysis.C:53
Definition: MVAAlg.h:12
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
Namespace collecting geometry-related classes utilities.
Event Generation using GENIE, cosmics or single particles.
art framework interface to geometry description
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
Event finding and building.
The data type to uniquely identify a cryostat.
Definition: geo_types.h:192
NeutronOsc & operator=(NeutronOsc const &)=delete
CLHEP::RandFlat flatDist