TextFileGen_module.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 // Class: TextFileGen
3 // Module Type: producer
4 // File: TextFileGen_module.cc
5 //
6 // Generated at Mon Apr 8 09:20:02 2013 by Brian Rebel using artmod
7 // from art v1_02_06.
8 //
9 //
10 // This module assumes that the input file has the hepevt format for
11 // each event to be simulated. See
12 //
13 // http://cepa.fnal.gov/psm/simulation/mcgen/lund/pythia_manual/pythia6.3/pythia6301/node39.html
14 //
15 // for details on the format. In brief each event contains at least two
16 // lines. The first line contains two entries, the event number (which is
17 // ignored in ART/GArSoft) and the number of particles in the event. Each
18 // following line containes 15 entries to describe each particle. The entries
19 // are:
20 //
21 // 1) status code (should be set to 1 for any particle to be tracked, others
22 // won't be tracked)
23 // 2) the pdg code for the particle
24 // 3) the entry of the first mother for this particle in the event,
25 // 0 means no mother
26 // 4) the entry of the second mother for this particle in the event,
27 // 0 means no mother
28 // 5) the entry of the first daughter for this particle in the event,
29 // 0 means no mother
30 // 6) the entry of the second daughter for this particle in the event,
31 // 0 means no mother
32 // 7) x component of the particle momentum
33 // 8) y component of the particle momentum
34 // 9) z component of the particle momentum
35 // 10) energy of the particle
36 // 11) mass of the particle
37 // 12) x position of the particle initial position
38 // 13) y position of the particle initial position
39 // 14) z position of the particle initial position
40 // 15) time of the particle production
41 //
42 // For example, if you want to simulate a single muon with a 5 GeV energy
43 // moving only in the z direction, the entry would be
44 //
45 // 0 1
46 // 1 13 0 0 0 0 0. 0. 1.0 5.0011 0.105 1.0 1.0 1.0 0.0
47 //
48 // There are some assumptions that go into using this format that may not
49 // be obvious. The first is that only particles with status code = 1
50 // are tracked in the GArSoft/Geant4 combination making the mother daughter
51 // relations somewhat irrelevant. That also means that you should let
52 // Geant4 handle any decays.
53 //
54 // The units in GArSoft are cm for distances and ns for time
55 // The use of TLorentzVector below does not imply space and time have the same units
56 // (do not use TLorentzVector::Boost())
57 ////////////////////////////////////////////////////////////////////////
58 #include <string>
59 #include <iostream>
60 #include <fstream>
61 
65 #include "fhiclcpp/ParameterSet.h"
66 #include "cetlib_except/exception.h"
68 
69 #include "TLorentzVector.h"
70 
73 
74 #include "Geometry/GeometryGAr.h"
76 #include "CoreUtils/ServiceUtil.h"
77 
78 namespace gar {
79  namespace evgen {
80  class TextFileGen;
81  }
82 
84  public:
85  explicit TextFileGen(fhicl::ParameterSet const & p);
86  virtual ~TextFileGen();
87 
88  void produce(::art::Event & e) override;
89  void beginJob() override;
90  void beginRun(::art::Run & run) override;
91  void reconfigure(fhicl::ParameterSet const & p);
92 
93  private:
94 
95  std::ifstream* fInputFile; ///< pointer to input text file
96  std::string fInputFileName; ///< Name of text file containing events to simulate
97  double fMoveY; ///< Project particles to a new y plane.
98  };
99 
100  //------------------------------------------------------------------------------
102  : EDProducer{p}, fInputFile(0)
103  {
104  this->reconfigure(p);
105 
106  produces< std::vector<simb::MCTruth> >();
107  produces< sumdata::RunData, ::art::InRun >();
108  }
109 
110  //------------------------------------------------------------------------------
112  {
113  }
114 
115  //------------------------------------------------------------------------------
117  {
118  fInputFile = new std::ifstream(fInputFileName.c_str());
119 
120  // check that the file is a good one
121  if( !fInputFile->good() )
122  throw cet::exception("TextFileGen")
123  << "input text file "
124  << fInputFileName
125  << " cannot be read.";
126 
127  return;
128  }
129 
130  //------------------------------------------------------------------------------
132  {
133 
134  // grab the geometry object to see what geometry we are using
135  auto geo = gar::providerFrom<geo::GeometryGAr>();
136  std::unique_ptr<gar::sumdata::RunData> runcol(new gar::sumdata::RunData(geo->DetectorName()));
137 
138  run.put(std::move(runcol));
139 
140  return;
141  }
142 
143  //------------------------------------------------------------------------------
145  {
146  // check that the file is still good
147  if( !fInputFile->good() )
148  throw cet::exception("TextFileGen")
149  << "input text file "
150  << fInputFileName
151  << " cannot be read in produce().";
152 
153 
154  std::unique_ptr< std::vector<simb::MCTruth> > truthcol(new std::vector<simb::MCTruth>);
155  simb::MCTruth truth;
156 
157  // declare the variables for reading in the event record
158  int event = 0;
159  unsigned short nParticles = 0;
160  int status = 0;
161  int pdg = 0;
162  int firstMother = 0;
163  int secondMother = 0;
164  int firstDaughter = 0;
165  int secondDaughter = 0;
166  double xMomentum = 0.;
167  double yMomentum = 0.;
168  double zMomentum = 0.;
169  double energy = 0.;
170  double mass = 0.;
171  double xPosition = 0.;
172  double yPosition = 0.;
173  double zPosition = 0.;
174  double time = 0.;
175 
176  // read in line to get event number and number of particles
177  std::string oneLine;
178  std::getline(*fInputFile, oneLine);
179  std::istringstream inputLine;
180  inputLine.str(oneLine);
181 
182  inputLine >> event >> nParticles;
183 
184  // now read in all the lines for the particles
185  // in this interaction. only particles with
186  // status = 1 get tracked in Geant4.
187  for(unsigned short i = 0; i < nParticles; ++i){
188  std::getline(*fInputFile, oneLine);
189  inputLine.clear();
190  inputLine.str(oneLine);
191 
192  inputLine >> status >> pdg
193  >> firstMother >> secondMother >> firstDaughter >> secondDaughter
194  >> xMomentum >> yMomentum >> zMomentum >> energy >> mass
195  >> xPosition >> yPosition >> zPosition >> time;
196 
197  //Project the particle to a new y plane
198  if (fMoveY>-1e8){
199  double totmom = sqrt(pow(xMomentum,2)+pow(yMomentum,2)+pow(zMomentum,2));
200  double kx = xMomentum/totmom;
201  double ky = yMomentum/totmom;
202  double kz = zMomentum/totmom;
203  if (ky){
204  double l = (fMoveY-yPosition)/ky;
205  xPosition += kx*l;
206  yPosition += ky*l;
207  zPosition += kz*l;
208  }
209  }
210 
211  TLorentzVector pos(xPosition, yPosition, zPosition, time);
212  TLorentzVector mom(xMomentum, yMomentum, zMomentum, energy);
213 
214  simb::MCParticle part(i, pdg, "primary", firstMother, mass, status);
215  part.AddTrajectoryPoint(pos, mom);
216 
217  truth.Add(part);
218  }
219 
220  truthcol->push_back(truth);
221 
222  e.put(std::move(truthcol));
223 
224  return;
225  }
226 
227  //------------------------------------------------------------------------------
229  {
230  fInputFileName = p.get<std::string>("InputFileName");
231  fMoveY = p.get<double>("MoveY", -1e9);
232  if (fMoveY>-1e8){
233  MF_LOG_WARNING("TextFileGen")
234  << "Particles will be moved to a new plane y = "
235  << fMoveY
236  << " cm.";
237  }
238  return;
239  }
240 
242 
243 }// namespace gar
void produce(::art::Event &e) override
void AddTrajectoryPoint(TLorentzVector const &position, TLorentzVector const &momentum)
Definition: MCParticle.h:257
std::string string
Definition: nybbler.cc:12
double fMoveY
Project particles to a new y plane.
constexpr T pow(T x)
Definition: pow.h:72
void reconfigure(fhicl::ParameterSet const &p)
std::string fInputFileName
Name of text file containing events to simulate.
Particle class.
static QStrList * l
Definition: config.cpp:1044
Definition: Run.h:17
const double e
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:67
void beginJob()
Definition: Breakpoints.cc:14
virtual void reconfigure(fhicl::ParameterSet const &pset)
std::ifstream * fInputFile
pointer to input text file
def move(depos, offset)
Definition: depos.py:107
T get(std::string const &key) const
Definition: ParameterSet.h:271
void beginRun(::art::Run &run) override
p
Definition: test.py:223
ProductID put(std::unique_ptr< PROD > &&edp, std::string const &instance={})
Definition: DataViewImpl.h:686
General GArSoft Utilities.
void Add(simb::MCParticle const &part)
Definition: MCTruth.h:80
Event generator information.
Definition: MCTruth.h:32
#define MF_LOG_WARNING(category)
LArSoft geometry interface.
Definition: ChannelGeo.h:16
art framework interface to geometry description
Event Generation using GENIE, cosmics or single particles.
TextFileGen(fhicl::ParameterSet const &p)
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33