LArSoft  v07_13_02
Liquid Argon Software toolkit - http://larsoft.org/
TextFileGen_module.cc
Go to the documentation of this file.
1 
58 #include <string>
59 #include <iostream>
60 #include <fstream>
61 
65 #include "fhiclcpp/ParameterSet.h"
66 #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  virtual ~TextFileGen();
83 
84  void produce(art::Event & e) override;
85  void beginJob() override;
86  void beginRun(art::Run & run) override;
87  void reconfigure(fhicl::ParameterSet const & p) ;
88 
89 private:
90 
91  std::ifstream* fInputFile;
92  std::string fInputFileName;
93  double fMoveY;
94 };
95 
96 //------------------------------------------------------------------------------
98  : fInputFile(0)
99 {
100  this->reconfigure(p);
101 
102  produces< std::vector<simb::MCTruth> >();
103  produces< sumdata::RunData, art::InRun >();
104 }
105 
106 //------------------------------------------------------------------------------
108 {
109 }
110 
111 //------------------------------------------------------------------------------
113 {
114  fInputFile = new std::ifstream(fInputFileName.c_str());
115 
116  // check that the file is a good one
117  if( !fInputFile->good() )
118  throw cet::exception("TextFileGen") << "input text file "
119  << fInputFileName
120  << " cannot be read.\n";
121 
122  return;
123 }
124 
125 //------------------------------------------------------------------------------
127 {
128 
129  // grab the geometry object to see what geometry we are using
131  std::unique_ptr<sumdata::RunData> runcol(new sumdata::RunData(geo->DetectorName()));
132 
133  run.put(std::move(runcol));
134 
135  return;
136  }
137 
138 //------------------------------------------------------------------------------
140 {
141  // check that the file is still good
142  if( !fInputFile->good() )
143  throw cet::exception("TextFileGen") << "input text file "
144  << fInputFileName
145  << " cannot be read in produce().\n";
146 
147 
148  std::unique_ptr< std::vector<simb::MCTruth> > truthcol(new std::vector<simb::MCTruth>);
149  simb::MCTruth truth;
150 
151  // declare the variables for reading in the event record
152  int event = 0;
153  unsigned short nParticles = 0;
154  int status = 0;
155  int pdg = 0;
156  int firstMother = 0;
157  int secondMother = 0;
158  int firstDaughter = 0;
159  int secondDaughter = 0;
160  double xMomentum = 0.;
161  double yMomentum = 0.;
162  double zMomentum = 0.;
163  double energy = 0.;
164  double mass = 0.;
165  double xPosition = 0.;
166  double yPosition = 0.;
167  double zPosition = 0.;
168  double time = 0.;
169 
170  // read in line to get event number and number of particles
171  std::string oneLine;
172  std::getline(*fInputFile, oneLine);
173  std::istringstream inputLine;
174  inputLine.str(oneLine);
175 
176  inputLine >> event >> nParticles;
177 
178  // now read in all the lines for the particles
179  // in this interaction. only particles with
180  // status = 1 get tracked in Geant4.
181  for(unsigned short i = 0; i < nParticles; ++i){
182  std::getline(*fInputFile, oneLine);
183  inputLine.clear();
184  inputLine.str(oneLine);
185 
186  inputLine >> status >> pdg
187  >> firstMother >> secondMother >> firstDaughter >> secondDaughter
188  >> xMomentum >> yMomentum >> zMomentum >> energy >> mass
189  >> xPosition >> yPosition >> zPosition >> time;
190 
191  //Project the particle to a new y plane
192  if (fMoveY>-1e8){
193  double totmom = sqrt(pow(xMomentum,2)+pow(yMomentum,2)+pow(zMomentum,2));
194  double kx = xMomentum/totmom;
195  double ky = yMomentum/totmom;
196  double kz = zMomentum/totmom;
197  if (ky){
198  double l = (fMoveY-yPosition)/ky;
199  xPosition += kx*l;
200  yPosition += ky*l;
201  zPosition += kz*l;
202  }
203  }
204 
205  TLorentzVector pos(xPosition, yPosition, zPosition, time);
206  TLorentzVector mom(xMomentum, yMomentum, zMomentum, energy);
207 
208  simb::MCParticle part(i, pdg, "primary", firstMother, mass, status);
209  part.AddTrajectoryPoint(pos, mom);
210 
211  truth.Add(part);
212  }
213 
214  truthcol->push_back(truth);
215 
216  e.put(std::move(truthcol));
217 
218  return;
219 }
220 
221 //------------------------------------------------------------------------------
223 {
224  fInputFileName = p.get<std::string>("InputFileName");
225  fMoveY = p.get<double>("MoveY", -1e9);
226  if (fMoveY>-1e8){
227  mf::LogWarning("TextFileGen")<<"Particles will be moved to a new plane y = "<<fMoveY<<" cm.\n";
228  }
229  return;
230 }
231 
void AddTrajectoryPoint(TLorentzVector const &position, TLorentzVector const &momentum)
Definition: MCParticle.h:261
void produce(art::Event &e) override
art::ProductID put(std::unique_ptr< PROD > &&)
Definition: Run.h:148
Particle class.
void Add(simb::MCParticle &part)
Definition: MCTruth.h:77
Definition: Run.h:30
ProductID put(std::unique_ptr< PROD > &&product)
Definition: Event.h:102
std::string fInputFileName
Name of text file containing events to simulate.
std::ifstream * fInputFile
void beginJob() override
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:42
std::string DetectorName() const
Returns a string with the name of the detector, as configured.
T get(std::string const &key) const
Definition: ParameterSet.h:231
TString part[npart]
Definition: Style.C:32
double energy
Definition: plottest35.C:25
void beginRun(art::Run &run) override
TextFileGen(fhicl::ParameterSet const &p)
MaybeLogger_< ELseverityLevel::ELsev_warning, false > LogWarning
Event generator information.
Definition: MCTruth.h:30
Float_t e
Definition: plot.C:34
Namespace collecting geometry-related classes utilities.
void reconfigure(fhicl::ParameterSet const &p)
Event Generation using GENIE, cosmics or single particles.
double fMoveY
Project particles to a new y plane.
art framework interface to geometry description
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33