63 #include "art/Framework/Core/EDProducer.h"
64 #include "art/Framework/Core/ModuleMacros.h"
65 #include "art/Framework/Principal/Event.h"
66 #include "art/Framework/Principal/Run.h"
67 #include "art/Framework/Services/Registry/ServiceHandle.h"
68 #include "fhiclcpp/ParameterSet.h"
69 #include "cetlib_except/exception.h"
70 #include "messagefacility/MessageLogger/MessageLogger.h"
72 #include "TLorentzVector.h"
76 #include "nusimdata/SimulationBase/MCTruth.h"
77 #include "nusimdata/SimulationBase/MCParticle.h"
87 void produce(art::Event &
e)
override;
89 void beginRun(art::Run & run)
override;
93 std::pair<unsigned, unsigned>
readEventInfo(std::istream& is);
104 , fOffset{
p.get<
unsigned long int>(
"Offset")}
106 , fInputFileName{
p.get<std::string>(
"InputFileName")}
107 , fMoveY{
p.get<
double>(
"MoveY", -1e9)}
110 mf::LogWarning(
"TextFileGen")<<
"Particles will be moved to a new plane y = "<<fMoveY<<
" cm.\n";
113 produces< std::vector<simb::MCTruth> >();
114 produces< sumdata::RunData, art::InRun >();
122 fInputFile =
new std::ifstream(fInputFileName.c_str());
125 if( !fInputFile->good() )
126 throw cet::exception(
"TextFileGen") <<
"input text file "
128 <<
" cannot be read.\n";
131 for (
unsigned i = 0; i != fOffset; ++i) {
132 auto const [eventNo, nparticles] = readEventInfo(*fInputFile);
133 for (
unsigned p = 0;
p != nparticles; ++
p) {
134 constexpr
auto all_chars_until = std::numeric_limits<unsigned>::max();
135 fInputFile->ignore(all_chars_until,
'\n');
145 art::ServiceHandle<geo::Geometry const> geo;
146 run.put(std::make_unique<sumdata::RunData>(geo->DetectorName()));
153 if( !fInputFile->good() )
154 throw cet::exception(
"TextFileGen") <<
"input text file "
156 <<
" cannot be read in produce().\n";
166 if( !fInputFile->good() )
167 throw cet::exception(
"TextFileGen") <<
"input text file "
169 <<
" cannot be read in produce().\n";
170 auto truthcol = std::make_unique<std::vector<simb::MCTruth>>();
171 truthcol->push_back(readNextHepEvt());
174 e.put(std::move(truthcol));
187 int secondMother = 0;
188 int firstDaughter = 0;
189 int secondDaughter = 0;
190 double xMomentum = 0.;
191 double yMomentum = 0.;
192 double zMomentum = 0.;
195 double xPosition = 0.;
196 double yPosition = 0.;
197 double zPosition = 0.;
204 std::istringstream inputLine;
205 simb::MCTruth nextEvent;
206 auto const [eventNo, nParticles] = readEventInfo(*fInputFile);
213 for(
unsigned short i = 0; i < nParticles; ++i){
214 std::getline(*fInputFile, oneLine);
216 inputLine.str(oneLine);
218 inputLine >> status >> pdg
219 >> firstMother >> secondMother >> firstDaughter >> secondDaughter
220 >> xMomentum >> yMomentum >> zMomentum >> energy >> mass
221 >> xPosition >> yPosition >> zPosition >> time;
227 double totmom = sqrt(pow(xMomentum,2)+pow(yMomentum,2)+pow(zMomentum,2));
228 double kx = xMomentum/totmom;
229 double ky = yMomentum/totmom;
230 double kz = zMomentum/totmom;
232 double l = (fMoveY-yPosition)/ky;
239 TLorentzVector pos(xPosition, yPosition, zPosition, time);
240 TLorentzVector mom(xMomentum, yMomentum, zMomentum, energy);
242 simb::MCParticle part(i, pdg,
"primary", firstMother, mass, status);
243 part.AddTrajectoryPoint(pos, mom);
258 std::istringstream buffer{line};
261 unsigned event, nparticles;
262 buffer >>
event >> nparticles;
263 return {event, nparticles};
std::pair< unsigned, unsigned > readEventInfo(std::istream &is)
unsigned long int fOffset
void produce(art::Event &e) override
std::string fInputFileName
Name of text file containing events to simulate.
std::ifstream * fInputFile
void beginRun(art::Run &run) override
TextFileGen(fhicl::ParameterSet const &p)
simb::MCTruth readNextHepEvt()
double fMoveY
Project particles to a new y plane.
art framework interface to geometry description