LArSoft  v06_85_00
Liquid Argon Software toolkit - http://larsoft.org/
ToyOneShowerGen_module.cc
Go to the documentation of this file.
1 // Class: ToyOneShowerGen
3 // Module Type: producer
4 // File: ToyOneShowerGen_module.cc
5 //
6 // Generated at Mon Aug 11 14:14:59 2014 by Kazuhiro Terao using artmod
7 // from cetpkgsupport v1_05_04.
9 
17 #include "fhiclcpp/ParameterSet.h"
18 
19 #include <memory>
20 
21 #include "CLHEP/Random/RandFlat.h"
22 
23 #include "TDatabasePDG.h"
24 #include "TRandom.h"
25 #include "TLorentzVector.h"
26 #include "TF1.h"
27 
28 // art extensions
30 
36 
37 class ToyOneShowerGen;
38 
40 public:
41  explicit ToyOneShowerGen(fhicl::ParameterSet const & p);
42  virtual ~ToyOneShowerGen();
43 
44  void produce(art::Event & e) override;
45  void beginRun(art::Run & run) override;
46  std::vector<double> GetXYZDirection(double uz=0);
47  std::vector<double> GetXYZPosition();
48 
49 private:
50 
53  CLHEP::RandFlat *fFlatRandom;
54  //size_t fNEvents; // unused
55  double fMass;
56  double fTime;
57  int fPDGCode;
58 };
59 
60 
62  : fShapeMomentum(nullptr), fShapeTheta(nullptr), fFlatRandom(nullptr)
63 {
64  produces< std::vector<simb::MCTruth> >();
65  produces< sumdata::RunData, art::InRun >();
66 
67  // Time
68  fTime = p.get<double>("Time");
69 
70  // PDGCode
71  fPDGCode = p.get<int>("PDGCode");
72 
73  // Mass
74  fMass = TDatabasePDG().GetParticle(fPDGCode)->Mass();
75  if(fPDGCode != 22 && !fMass) throw std::exception();
76 
77  //
78  // Momentum distribution
79  //
80 
81  // Read-in formula
82  fShapeMomentum = new TF1("fShapeMomentum",
83  (p.get<std::string>("MomentumShapeFormula")).c_str(),
84  p.get<double>("MomentumLowerBound"),
85  p.get<double>("MomentumUpperBound"));
86  // Check formula
87  if(!fShapeMomentum)
88 
89  throw cet::exception(__PRETTY_FUNCTION__)
90  << "Failed making momentum spectrum shape from provided formula!" << std::endl;
91 
92  // Read-in parameter values
93  std::vector<double> shape_par_values = p.get<std::vector<double> >("MomentumShapeParameters");
94 
95  // Check parameter values
96  if((int)(shape_par_values.size()) != fShapeMomentum->GetNpar())
97 
98  throw cet::exception(__PRETTY_FUNCTION__)
99  << "Number of parameters provided to MomentumShapeFormula does not match with the formula!" << std::endl;
100 
101  // Set parameter values
102  for(size_t i=0; i<shape_par_values.size(); ++i)
103 
104  fShapeMomentum->SetParameter(i,shape_par_values.at(i));
105 
106  //
107  // Angular distribution
108  //
109 
110  // Read-in formula
111  fShapeTheta = new TF1("fShapeTheta",
112  (p.get<std::string>("ThetaShapeFormula")).c_str(),
113  p.get<double>("ThetaLowerBound"),
114  p.get<double>("ThetaUpperBound"));
115  // Check formula
116  if(!fShapeTheta)
117 
118  throw cet::exception(__PRETTY_FUNCTION__)
119  << "Failed making momentum spectrum shape from provided formula!" << std::endl;
120 
121  // Read-in parameter values
122  shape_par_values = p.get<std::vector<double> >("ThetaShapeParameters");
123 
124  // Check parameter values
125  if((int)(shape_par_values.size()) != fShapeTheta->GetNpar())
126 
127  throw cet::exception(__PRETTY_FUNCTION__)
128  << "Number of parameters provided to ThetaShapeFormula does not match with the formula!" << std::endl;
129 
130  // Set parameter values
131  for(size_t i=0; i<shape_par_values.size(); ++i)
132 
133  fShapeTheta->SetParameter(i,shape_par_values.at(i));
134 
135  //
136  // Random engine initialization
137  //
138  // create a default random engine; obtain the random seed from NuRandomService,
139  // unless overridden in configuration with key "Seed"
142  CLHEP::HepRandomEngine &engine = rng->getEngine();
143  fFlatRandom = new CLHEP::RandFlat(engine);
144 }
145 
146 //------------------------------------------------------------------------------
148 {
149  // grab the geometry object to see what geometry we are using
151 
152  std::unique_ptr<sumdata::RunData> runData(new sumdata::RunData(geo->DetectorName()));
153 
154  run.put(std::move(runData));
155 
156  return;
157 }
158 
160 {
161  delete fShapeTheta;
162  delete fShapeMomentum;
163  delete fFlatRandom;
164 }
165 
166 std::vector<double> ToyOneShowerGen::GetXYZPosition() {
167 
168  std::vector<double> pos(3,0);
169 
170  //pos.at(0) = fFlatRandom->fire(170.,2390.);
171  //pos.at(1) = fFlatRandom->fire(-995.,995.);
172  //pos.at(2) = fFlatRandom->fire(170.,10190.);
173 
174  //pos.at(0) = fFlatRandom->fire(17.,239.);
175  //pos.at(1) = fFlatRandom->fire(-99.5,99.5);
176  //pos.at(2) = fFlatRandom->fire(17.,1019.);
177 
179 
180  pos.at(0) = fFlatRandom->fire(0.,2.*(geo->DetHalfWidth()));
181  pos.at(1) = fFlatRandom->fire(-1.*(geo->DetHalfHeight()), geo->DetHalfHeight());
182  pos.at(2) = fFlatRandom->fire(0.,geo->DetLength());
183 
184  return pos;
185 }
186 
187 std::vector<double> ToyOneShowerGen::GetXYZDirection(double uz) {
188 
189  std::vector<double> udir(3,0);
190 
191  double phi = fFlatRandom->fire(0, 2 * 3.141592653589793238);
192 
193  udir[0] = TMath::Sin(TMath::ACos(uz)) * TMath::Cos(phi);
194  udir[1] = TMath::Sin(TMath::ACos(uz)) * TMath::Sin(phi);
195  udir[2] = uz;
196 
197  return udir;
198 }
199 
201 {
202  std::unique_ptr< std::vector<simb::MCTruth> > mctArray(new std::vector<simb::MCTruth>);
203  double mom = fShapeMomentum->GetRandom();
204  double Uz = TMath::Cos(fShapeTheta->GetRandom());
205 
206  std::vector<double> pos = GetXYZPosition();
207 
208  std::vector<double> dir = GetXYZDirection(Uz);
209 
210  simb::MCTruth truth;
211 
212  TLorentzVector pos_lorentz(pos.at(0), pos.at(1), pos.at(2), fTime);
213  TLorentzVector mom_lorentz( dir.at(0) * mom,
214  dir.at(1) * mom,
215  dir.at(2) * mom,
216  sqrt(pow(mom,2)+pow(fMass,2)));
217 
218  simb::MCParticle part(0, fPDGCode, "primary", 0, fMass, 1);
219 
220  part.AddTrajectoryPoint(pos_lorentz, mom_lorentz);
221 
222  truth.Add(part);
223 
224  mctArray->push_back(truth);
225 
226  e.put(std::move(mctArray));
227 
228 }
229 
geo::Length_t DetHalfWidth(geo::TPCID const &tpcid) const
Returns the half width of the active volume of the specified TPC.
ToyOneShowerGen(fhicl::ParameterSet const &p)
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
base_engine_t & getEngine() const
base_engine_t & createEngine(seed_t seed)
geo::Length_t DetHalfHeight(geo::TPCID const &tpcid) const
Returns the half height of the active volume of the specified TPC.
#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
geo::Length_t DetLength(geo::TPCID const &tpcid) const
Returns the length of the active volume of the specified TPC.
An art service to assist in the distribution of guaranteed unique seeds to all engines within an art ...
std::vector< double > GetXYZDirection(double uz=0)
CLHEP::RandFlat * fFlatRandom
static art::ServiceHandle< art::RandomNumberGenerator > & rng()
void produce(art::Event &e) override
std::vector< double > GetXYZPosition()
TDirectory * dir
Definition: macro.C:5
void beginRun(art::Run &run) override
Event generator information.
Definition: MCTruth.h:30
Float_t e
Definition: plot.C:34
Namespace collecting geometry-related classes utilities.
Tools and modules for checking out the basics of the Monte Carlo.
art framework interface to geometry description
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33