TextFileGen_module.cc
Go to the documentation of this file.
1 /**
2  * @file TextFileGen_module.cc
3  * @brief Producer generating Monte Carlo truth record in LArSoft format from a text file
4  * @date Mon Apr 8 09:20:02 2013
5  * @author Brian Rebel
6  */
7 /**
8  * @class evgen::TextFileGen
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/LArSoft) 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 daughter
30  * 6. the entry of the second daughter for this particle in the event,
31  * 0 means no daughter
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 LArSoft/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 LArSoft 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 <fstream>
60 
64 #include "fhiclcpp/ParameterSet.h"
65 #include "cetlib_except/exception.h"
67 
68 #include "TLorentzVector.h"
69 
74 
75 namespace evgen {
76  class TextFileGen;
77 }
78 
80 public:
81  explicit TextFileGen(fhicl::ParameterSet const & p);
82 
83  void produce(art::Event & e) override;
84  void beginJob() override;
85  void beginRun(art::Run & run) override;
86 
87 private:
88 
89  std::ifstream* fInputFile;
90  std::string fInputFileName; ///< Name of text file containing events to simulate
91  double fMoveY; ///< Project particles to a new y plane.
92 };
93 
94 //------------------------------------------------------------------------------
96  : EDProducer{p}
97  , fInputFile(0)
98  , fInputFileName{p.get<std::string>("InputFileName")}
99  , fMoveY{p.get<double>("MoveY", -1e9)}
100 
101 {
102  if (fMoveY>-1e8){
103  mf::LogWarning("TextFileGen")<<"Particles will be moved to a new plane y = "<<fMoveY<<" cm.\n";
104  }
105 
106  produces< std::vector<simb::MCTruth> >();
107  produces< sumdata::RunData, art::InRun >();
108 }
109 
110 //------------------------------------------------------------------------------
112 {
113  fInputFile = new std::ifstream(fInputFileName.c_str());
114 
115  // check that the file is a good one
116  if( !fInputFile->good() )
117  throw cet::exception("TextFileGen") << "input text file "
118  << fInputFileName
119  << " cannot be read.\n";
120 }
121 
122 //------------------------------------------------------------------------------
124 {
126  run.put(std::make_unique<sumdata::RunData>(geo->DetectorName()));
127  }
128 
129 //------------------------------------------------------------------------------
131 {
132  // check that the file is still good
133  if( !fInputFile->good() )
134  throw cet::exception("TextFileGen") << "input text file "
135  << fInputFileName
136  << " cannot be read in produce().\n";
137 
138 
139  std::unique_ptr< std::vector<simb::MCTruth> > truthcol(new std::vector<simb::MCTruth>);
140  simb::MCTruth truth;
141 
142  // declare the variables for reading in the event record
143  int event = 0;
144  unsigned short nParticles = 0;
145  int status = 0;
146  int pdg = 0;
147  int firstMother = 0;
148  int secondMother = 0;
149  int firstDaughter = 0;
150  int secondDaughter = 0;
151  double xMomentum = 0.;
152  double yMomentum = 0.;
153  double zMomentum = 0.;
154  double energy = 0.;
155  double mass = 0.;
156  double xPosition = 0.;
157  double yPosition = 0.;
158  double zPosition = 0.;
159  double time = 0.;
160 
161  // read in line to get event number and number of particles
162  std::string oneLine;
163  std::getline(*fInputFile, oneLine);
164  std::istringstream inputLine;
165  inputLine.str(oneLine);
166 
167  inputLine >> event >> nParticles;
168 
169  // now read in all the lines for the particles
170  // in this interaction. only particles with
171  // status = 1 get tracked in Geant4.
172  for(unsigned short i = 0; i < nParticles; ++i){
173  std::getline(*fInputFile, oneLine);
174  inputLine.clear();
175  inputLine.str(oneLine);
176 
177  inputLine >> status >> pdg
178  >> firstMother >> secondMother >> firstDaughter >> secondDaughter
179  >> xMomentum >> yMomentum >> zMomentum >> energy >> mass
180  >> xPosition >> yPosition >> zPosition >> time;
181 
182  //Project the particle to a new y plane
183  if (fMoveY>-1e8){
184  double totmom = sqrt(pow(xMomentum,2)+pow(yMomentum,2)+pow(zMomentum,2));
185  double kx = xMomentum/totmom;
186  double ky = yMomentum/totmom;
187  double kz = zMomentum/totmom;
188  if (ky){
189  double l = (fMoveY-yPosition)/ky;
190  xPosition += kx*l;
191  yPosition += ky*l;
192  zPosition += kz*l;
193  }
194  }
195 
196  TLorentzVector pos(xPosition, yPosition, zPosition, time);
197  TLorentzVector mom(xMomentum, yMomentum, zMomentum, energy);
198 
199  simb::MCParticle part(i, pdg, "primary", firstMother, mass, status);
200  part.AddTrajectoryPoint(pos, mom);
201 
202  truth.Add(part);
203  }
204 
205  truthcol->push_back(truth);
206 
207  e.put(std::move(truthcol));
208 }
209 
void AddTrajectoryPoint(TLorentzVector const &position, TLorentzVector const &momentum)
Definition: MCParticle.h:257
std::string string
Definition: nybbler.cc:12
EDProducer(fhicl::ParameterSet const &pset)
Definition: EDProducer.h:20
constexpr T pow(T x)
Definition: pow.h:72
void produce(art::Event &e) override
Particle class.
static QStrList * l
Definition: config.cpp:1044
art framework interface to geometry description
Definition: Run.h:17
std::string fInputFileName
Name of text file containing events to simulate.
std::ifstream * fInputFile
const double e
void beginJob() override
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:67
std::string DetectorName() const
Returns a string with the name of the detector, as configured.
def move(depos, offset)
Definition: depos.py:107
p
Definition: test.py:223
void beginRun(art::Run &run) override
ProductID put(std::unique_ptr< PROD > &&edp, std::string const &instance={})
Definition: DataViewImpl.h:686
TextFileGen(fhicl::ParameterSet const &p)
void Add(simb::MCParticle const &part)
Definition: MCTruth.h:80
MaybeLogger_< ELseverityLevel::ELsev_warning, false > LogWarning
Event generator information.
Definition: MCTruth.h:32
LArSoft geometry interface.
Definition: ChannelGeo.h:16
Event Generation using GENIE, cosmics or single particles.
double fMoveY
Project particles to a new y plane.
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33