All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ReThrowRayTraceBox_tool.cc
Go to the documentation of this file.
1 /**
2  *
3  */
4 
5 // Framework Includes
6 #include "art/Framework/Principal/Event.h"
7 #include "art/Framework/Principal/Handle.h"
8 #include "art/Framework/Services/Registry/ServiceHandle.h"
9 #include "art/Persistency/Common/PtrMaker.h"
10 #include "art/Utilities/ToolMacros.h"
11 #include "cetlib/cpu_timer.h"
12 #include "fhiclcpp/ParameterSet.h"
13 #include "messagefacility/MessageLogger/MessageLogger.h"
14 
15 // local includes
16 #include "IRayTrace.h"
19 
20 // LArSoft includes
25 
26 // std includes
27 #include <string>
28 #include <iostream>
29 #include <memory>
30 
31 //------------------------------------------------------------------------------------------------------------------------------------------
32 // implementation follows
33 
34 namespace evgen {
35 namespace ldm {
36 /**
37  * @brief ReThrowRayTraceBox class definiton
38  */
40 {
41 public:
42  /**
43  * @brief Constructor
44  */
45  ReThrowRayTraceBox(fhicl::ParameterSet const &pset);
46 
47  /**
48  * @brief Destructor
49  */
51 
52  void configure(const fhicl::ParameterSet&) override;
53 
54  bool IntersectDetector(MeVPrtlFlux &flux, std::array<TVector3, 2> &intersection, double &weight) override;
55 
56  TLorentzVector ThrowMeVPrtlMomentum(const MeVPrtlFlux &flux);
57 
58  // always thrown at least once
59  double MaxWeight() override {
60  return fMaxWeight;
61  }
62 
63 private:
65  unsigned fNThrows;
66 
71 
72  void CalculateMaxWeight();
73 
74  double fMaxWeight;
75 };
76 
77 ReThrowRayTraceBox::ReThrowRayTraceBox(fhicl::ParameterSet const &pset):
78  IMeVPrtlStage("ReThrowRayTraceBox")
79 {
80  this->configure(pset);
81 }
82 
83 //------------------------------------------------------------------------------------------------------------------------------------------
84 
86 {
87 }
88 
89 //------------------------------------------------------------------------------------------------------------------------------------------
90 void ReThrowRayTraceBox::configure(fhicl::ParameterSet const &pset)
91 {
92  if (pset.has_key("Box")) {
93  std::array<double, 6> box_config = pset.get<std::array<double, 6>>("Box");
94  // xmin, xmax, ymin, ymax, zmin, zmax
95  fBox = geo::BoxBoundedGeo(box_config[0], box_config[1], box_config[2], box_config[3], box_config[4], box_config[5]);
96  }
97  else {
98  const geo::GeometryCore *geometry = lar::providerFrom<geo::Geometry>();
99  fBox = geometry->DetectorEnclosureBox(pset.get<std::string>("Volume"));
100  }
101 
102  fNThrows = pset.get<unsigned>("NThrows", 10000);
103 
104  std::cout << "Detector Box." << std::endl;
105  std::cout << "X " << fBox.MinX() << " " << fBox.MaxX() << std::endl;
106  std::cout << "Y " << fBox.MinY() << " " << fBox.MaxY() << std::endl;
107  std::cout << "Z " << fBox.MinZ() << " " << fBox.MaxZ() << std::endl;
108 
109  fReferenceLabSolidAngle = pset.get<double>("ReferenceLabSolidAngle");
110  fReferencePrtlMass = pset.get<double>("ReferencePrtlMass");
111  fReferenceScndPDG = pset.get<int>("ReferenceScndPDG");
112  fReferenceKaonEnergy = pset.get<double>("ReferenceKaonEnergy");
113 
115 
116  // TEST: compare results of this weight to calcENuWeight
117 
118 }
119 
121  double secondary_mass = 0.;
122  switch (fReferenceScndPDG) {
123  case 11:
124  case -11:
125  secondary_mass = Constants::Instance().elec_mass;
126  break;
127  case 13:
128  case -13:
129  secondary_mass = Constants::Instance().muon_mass;
130  break;
131  case 211:
132  case -211:
133  secondary_mass = Constants::Instance().piplus_mass;
134  break;
135  default:
136  std::cerr << "RETHROWRAYTACE -- bad secondary pdg: " << fReferenceScndPDG << std::endl;
137  std::cout << "RETHROWRAYTACE -- bad secondary pdg: " << fReferenceScndPDG << std::endl;
138  break;
139  }
140 
141  double p = twobody_momentum(Constants::Instance().kplus_mass, secondary_mass, fReferencePrtlMass);
142  double E = sqrt(p*p + fReferencePrtlMass*fReferencePrtlMass);
143 
145  double kbeta = sqrt(1 - 1. / (kgamma * kgamma));
146 
147  double scale = kgamma * kgamma * (p + kbeta*E) * (p + kbeta*E) / (p*p);
148 
150 }
151 
153  // pick a direction for the rest-frame
154  TVector3 dir = RandomUnitVector();
155 
156  // make the mevprtl momentum this
157  //
158  // Move the mevprtl momentum back to the kaon rest frame
159  TLorentzVector mevprtl_mom = flux.mom;
160  mevprtl_mom.Boost(-flux.kmom.BoostVector());
161 
162  mevprtl_mom = TLorentzVector(mevprtl_mom.P() * dir, mevprtl_mom.E());
163 
164  // boost back
165  mevprtl_mom.Boost(flux.kmom.BoostVector());
166 
167  return mevprtl_mom;
168 
169 }
170 
171 bool ReThrowRayTraceBox::IntersectDetector(MeVPrtlFlux &flux, std::array<TVector3, 2> &intersection, double &weight) {
172  // try out the mevprtl direction a bunch of times
173  std::vector<std::vector<TVector3>> allIntersections;
174  std::vector<TLorentzVector> allHMom;
175 
176  for (unsigned i = 0; i <= fNThrows; i++) {
177  TLorentzVector mevprtl_mom = ThrowMeVPrtlMomentum(flux);
178  std::vector<TVector3> box_intersections = fBox.GetIntersections(flux.pos.Vect(), mevprtl_mom.Vect().Unit());
179 
180  // Does this ray intersect the box?
181  if (box_intersections.size() != 2) {
182  continue;
183  }
184 
185  TVector3 A = box_intersections[0];
186 
187  // if the ray points the wrong way, it doesn't intersect
188  if (mevprtl_mom.Vect().Unit().Dot((A - flux.pos.Vect()).Unit()) < 0.) {
189  std::cout << "RAYTRACE: MeVPrtl points wrong way" << std::endl;
190  std::cout << "Pos: " << flux.pos.X() << " " << flux.pos.Y() << " " << flux.pos.Z() << std::endl;
191  std::cout << "A: " << A.X() << " " << A.Y() << " " << A.Z() << std::endl;
192  std::cout << "P: " << mevprtl_mom.Vect().Unit().X() << " " << mevprtl_mom.Vect().Unit().Y() << " " << mevprtl_mom.Vect().Unit().Z() << std::endl;
193  continue;
194  }
195 
196  // if we're here, we have a valid ray
197  allIntersections.push_back(box_intersections);
198  allHMom.push_back(mevprtl_mom);
199  }
200 
201  std::cout << "Prtl intersected (" << allIntersections.size() << " / " << fNThrows << ") times.\n";
202 
203  // did we get a hit?
204  if (allIntersections.size() == 0) {
205  return false;
206  }
207 
208  unsigned ind = CLHEP::RandFlat::shootInt(fEngine, 0, allIntersections.size()-1); // inclusive?
209 
210  TLorentzVector mevprtl_mom = allHMom[ind];
211  std::vector<TVector3> box_intersections = allIntersections[ind];
212 
213  TVector3 A = box_intersections[0];
214  TVector3 B = box_intersections[1];
215 
216  // make sure that the flux start lies outside the detector
217  if ((flux.pos.Vect() - A).Mag() < (A-B).Mag() && (flux.pos.Vect() - B).Mag() < (A-B).Mag()) {
218  throw cet::exception("ReThrowRayTraceBox Exception", "Input mevprtl flux starts inside detector volume: "
219  "MeVPrtl start At: (" + std::to_string(flux.pos.X()) + ", " + std::to_string(flux.pos.Y()) + ", " + std::to_string(flux.pos.Z()) + "). "
220  "Intersection A At: " + std::to_string(A.X()) + ", " + std::to_string(A.Y()) + ", " + std::to_string(A.Z()) + "). "
221  "Intersection B At: " + std::to_string(B.X()) + ", " + std::to_string(B.Y()) + ", " + std::to_string(B.Z()) + ").\n"
222  );
223  }
224 
225  // set things
226  weight = (double)allIntersections.size() / fNThrows;
227  flux.mom = mevprtl_mom;
228  // transform to beam-coord frame
229  flux.mom_beamcoord = mevprtl_mom;
230  flux.mom_beamcoord.Boost(-flux.kmom.BoostVector()); // Boost to kaon rest frame
231  flux.mom_beamcoord.Boost(flux.kmom_beamcoord.BoostVector()); // And to beam coordinate frame
232  // Two boosts make a rotation!
233 
234  // Set the secondary 4-momentum
235  flux.sec = flux.kmom - flux.mom;
236  flux.sec_beamcoord = flux.kmom_beamcoord - flux.mom_beamcoord;
237 
238  if ((flux.pos.Vect() - A).Mag() < (flux.pos.Vect() - B).Mag()) {
239  intersection = {A, B}; // A is entry, B is exit
240  }
241  else {
242  intersection = {B, A}; // reversed
243  }
244 
245  std::cout << "Kaon 4P: " << flux.kmom.E() << " " << flux.kmom.Px() << " " << flux.kmom.Py() << " " << flux.kmom.Pz() << std::endl;
246  std::cout << "Selected Prtl 4P: " << flux.mom.E() << " " << flux.mom.Px() << " " << flux.mom.Py() << " " << flux.mom.Pz() << std::endl;
247  std::cout << "Selected Scdy 4P: " << flux.sec.E() << " " << flux.sec.Px() << " " << flux.sec.Py() << " " << flux.sec.Pz() << std::endl;
248 
249  return true;
250 }
251 
252 DEFINE_ART_CLASS_TOOL(ReThrowRayTraceBox)
253 
254 } // namespace ldm
255 } // namespace evgen
ReThrowRayTraceBox class definiton.
TLorentzVector sec_beamcoord
Definition: MeVPrtlFlux.h:17
void configure(const fhicl::ParameterSet &) override
Interface for configuring the particular algorithm tool.
Utilities related to art service access.
TLorentzVector mom
Definition: MeVPrtlFlux.h:14
const geo::GeometryCore * geometry
BEGIN_PROLOG could also be cerr
TLorentzVector mom_beamcoord
Definition: MeVPrtlFlux.h:15
pdgs p
Definition: selectors.fcl:22
double MinX() const
Returns the world x coordinate of the start of the box.
Definition: BoxBoundedGeo.h:88
geo::BoxBoundedGeo DetectorEnclosureBox(std::string const &name="volDetEnclosure") const
bool IntersectDetector(MeVPrtlFlux &flux, std::array< TVector3, 2 > &intersection, double &weight) override
double twobody_momentum(double parent_mass, double childA_mass, double childB_mass)
Definition: Constants.cpp:73
double MaxX() const
Returns the world x coordinate of the end of the box.
Definition: BoxBoundedGeo.h:91
process_name E
CLHEP::HepRandomEngine * fEngine
Definition: IMeVPrtlStage.h:79
Access the description of detector geometry.
TLorentzVector kmom
Definition: MeVPrtlFlux.h:13
double MinZ() const
Returns the world z coordinate of the start of the box.
IMeVPrtlStage interface class definiton. General interface behind each stage. Provides random number ...
Definition: IMeVPrtlStage.h:36
Description of geometry of one entire detector.
IRayTrace interface class definiton.
Definition: IRayTrace.h:39
Provides a base class aware of world box coordinates.
double MaxY() const
Returns the world y coordinate of the end of the box.
TLorentzVector ThrowMeVPrtlMomentum(const MeVPrtlFlux &flux)
tuple dir
Definition: dropbox.py:28
static const Constants & Instance()
Definition: Constants.h:68
ReThrowRayTraceBox(fhicl::ParameterSet const &pset)
Constructor.
A base class aware of world box coordinatesAn object describing a simple shape can inherit from this ...
Definition: BoxBoundedGeo.h:33
std::string to_string(WindowPattern const &pattern)
double MaxZ() const
Returns the world z coordinate of the end of the box.
TLorentzVector pos
Definition: MeVPrtlFlux.h:11
std::vector< TVector3 > GetIntersections(TVector3 const &TrajectoryStart, TVector3 const &TrajectoryDirect) const
Calculates the entry and exit points of a trajectory on the box surface.
float A
Definition: dedx.py:137
This provides an interface for an art tool which ray traces &quot;Prtl&quot; (massive) particles from their pro...
double MinY() const
Returns the world y coordinate of the start of the box.
TLorentzVector kmom_beamcoord
Definition: MeVPrtlFlux.h:12
art framework interface to geometry description
BEGIN_PROLOG could also be cout
TLorentzVector sec
Definition: MeVPrtlFlux.h:16