Jpp  master_rocky-40-g5f0272dcd
the software that should make you happy
JPhotonPath.hh
Go to the documentation of this file.
1 #ifndef H_J_PHOTON_PATH
2 #define H_J_PHOTON_PATH
3 
4 /**
5  * \author mjongen
6  */
7 
8 // c++ standard library includes
9 #include<iostream>
10 #include<sstream>
11 #include<iomanip>
12 #include<cmath>
13 #include<cstdlib>
14 #include<vector>
15 
16 // root includes
17 #include "TObject.h"
18 #include "TH1F.h"
19 #include "TRandom3.h"
20 
21 // JPP includes
23 #include "JGeometry3D/JVersor3D.hh"
24 #include "JIO/JSerialisable.hh"
25 
26 namespace JMARKOV {}
27 namespace JPP { using namespace JMARKOV; }
28 
29 namespace JMARKOV {
30  using namespace std ;
31  using namespace JGEOMETRY3D ;
32  using namespace JGEOMETRY2D ;
33 
34  /**
35  A photon path.
36  This is basically a JPolyline3D with at least two vertices and a weight.
37  **/
38  class JPhotonPath : public JPolyline3D, public JIO::JSerialisable {
39  public:
40 
41  /// default constructor
42  JPhotonPath() : n(2), weight(1) { resize(n) ; }
43 
44  /// constructor to actually be used
45  JPhotonPath( int _nscat ) : n(_nscat+2), weight(1) { resize(n) ; }
46 
47  /// get a string with the path vertex positions
48  string getString() ;
49 
50  /// get the total path length
51  double getLength() ;
52 
53  void addVertex() {
54  JPosition3D pos(0,0,0) ;
55  addVertex(pos) ;
56  }
57  void addVertex( JPosition3D& pos ) ;
58 
59  /// Returns whether the photon path intersects a sphere of radius r at position pos
60  bool hitsSphere( const JPosition3D& pos, double r ) ;
61 
62  /// Returns the position where the photon hits a sphere of radius r at position pos. If the photon does not hit, it returns (0,0,0).
63  JPosition3D getSphereHitPosition( const JPosition3D& pos, double r ) ;
64 
65 
66  /// return all coordinates where the photon path has the given x
67  std::vector<JPosition3D> getPointsWithX( double x ) ;
68 
69  /// return all coordinates where the photon path has the given y
70  std::vector<JPosition3D> getPointsWithY( double y ) ;
71 
72  /// return all coordinates where the photon path has the given z
73  std::vector<JPosition3D> getPointsWithZ( double z ) ;
74 
76  // read number of vertices and adjust size accordingly
77  in >> n ;
78  if( n>0 ) resize(n) ;
79  // read vertices
80  for( iterator it=begin() ; it!=end() ; ++it )
81  in >> *it ;
82  // read weight
83  in >> weight ;
84 
85  return in ;
86  }
87 
89  // write size
90  out << (int)size() ;
91  // write vertices
92  for( const_iterator it=begin() ; it!=end() ; ++it )
93  out << *it ; //it->getX() << it->getY() << it->getZ() ;
94  // write weight
95  out << weight ;
96 
97  return out ;
98  }
99 
100  int n ; // the number of vertices in the path
101  double weight ;
102  } ;
103 
105  iterator v1 = begin() ;
106  iterator v2(v1) ;
107  ++v2 ;
108  double L = 0 ;
109  for( ; v2!=end() ; ++v1,++v2 ) {
110  // length of path segment
111  L += (*v2-*v1).getLength() ;
112  }
113  return L ;
114  }
115 
117  ++n ;
118  push_back(pos) ;
119  }
120 
123 
124  // loop over the path segments
125  for( unsigned int i=1 ; i<size() ; ++i ) {
126  JPosition3D v1( at(i-1) ) ;
127  JPosition3D v2( at(i) ) ;
128  if( (v1.getX()>x) != (v2.getX()>x) ) {
129  double frac = (x-v1.getX())/(v2.getX()-v1.getX()) ;
130  res.push_back( v1+frac*(v2-v1) ) ;
131  }
132  }
133  return res ;
134  }
135 
137  ostringstream oss ;
138  for( unsigned int i=0 ; i<size() ; ++i ) {
139  oss << at(i) ;
140  if( i+1!=size() ) oss << ", " ;
141  }
142  return oss.str() ;
143  }
144 
147 
148  // loop over the path segments
149  for( unsigned int i=1 ; i<size() ; ++i ) {
150  JPosition3D v1( at(i-1) ) ;
151  JPosition3D v2( at(i) ) ;
152  if( (v1.getY()>y) != (v2.getY()>y) ) {
153  double frac = (y-v1.getY())/(v2.getY()-v1.getY()) ;
154  res.push_back( v1+frac*(v2-v1) ) ;
155  }
156  }
157  return res ;
158  }
159 
162 
163  // loop over the path segments
164  for( unsigned int i=1 ; i<size() ; ++i ) {
165  JPosition3D v1( at(i-1) ) ;
166  JPosition3D v2( at(i) ) ;
167  if( (v1.getZ()>z) != (v2.getZ()>z) ) {
168  double frac = (z-v1.getZ())/(v2.getZ()-v1.getZ()) ;
169  res.push_back( v1+frac*(v2-v1) ) ;
170  }
171  }
172  return res ;
173  }
174 
175 
176  bool JPhotonPath::hitsSphere( const JPosition3D& pos, double r ) {
177  // loop over the path segments
178  for( unsigned int i=1 ; i<size() ; ++i ) {
179  // the current path segment is from x1 to x2
180 
181  // vector pointing from x1 to x2
182  JPosition3D seg = at(i)-at(i-1) ;
183  JVersor3D dir_seg(seg) ; // normalized version of seg
184  double Lseg = seg.getLength() ;
185 
186  // vector pointing from the center of the sphere to x1
187  JPosition3D to_center(at(i-1)-pos) ;
188  JVersor3D dir_to_center(to_center) ; // normalized version of to_center
189  double r_to_center = to_center.getLength() ;
190 
191  // cosine of angle between the two directions
192  double ct = dir_seg.getDot(dir_to_center) ;
193 
194  // if sin^2(theta) > (r_to_center/radius)^2
195  if( 1-ct*ct > r*r/(r_to_center*r_to_center) ) {
196  // definitely no hit in this segment
197  // continue with the next one
198  continue ;
199  }
200 
201  // the segment can be parametrized as x1 + xi * dir_seg
202  // where xi is in the range [0,Lseg]
203  // there are two possible solutions for xi
204  double D = sqrt( r_to_center*r_to_center*(ct*ct-1) + r*r ) ;
205  double xi1 = -r_to_center*ct + D ;
206  if( xi1>0 && xi1<Lseg ) return true ;
207  double xi2 = -r_to_center*ct - D ;
208  if( xi2>0 && xi2<Lseg ) return true ;
209  }
210  return false ;
211  }
212 
214  // loop over the path segments
215  for( unsigned int i=1 ; i<size() ; ++i ) {
216  // the current path segment is from x1 to x2
217 
218  // vector pointing from x1 to x2
219  JPosition3D seg = at(i)-at(i-1) ;
220  JVersor3D dir_seg(seg) ; // normalized version of seg
221  double Lseg = seg.getLength() ;
222 
223  // vector pointing from the center of the sphere to x1
224  JPosition3D to_center(at(i-1)-pos) ;
225  JVersor3D dir_to_center(to_center) ; // normalized version of to_center
226  double r_to_center = to_center.getLength() ;
227 
228  // cosine of angle between the two directions
229  double ct = dir_seg.getDot(dir_to_center) ;
230 
231  // if sin^2(theta) > (r_to_center/radius)^2
232  if( 1-ct*ct > r*r/(r_to_center*r_to_center) ) {
233  // definitely no hit in this segment
234  // continue with the next one
235  continue ;
236  }
237 
238  // the segment can be parametrized as x1 + xi * dir_seg
239  // where xi is in the range [0,Lseg]
240  // there are two possible solutions for xi
241  double D = sqrt( r_to_center*r_to_center*(ct*ct-1) + r*r ) ;
242 
243  // first xi
244  double xi1 = -r_to_center*ct + D ;
245  if( xi1<0 || xi1>Lseg ) {
246  xi1 = 1.1*Lseg ;
247  }
248 
249  // second xi
250  double xi2 = -r_to_center*ct - D ;
251  // if this is a xi within the segment
252  if( xi2>0 && xi2<Lseg ) {
253  // and it is before xi1, we replace xi1
254  if( xi2<xi1 ) xi1 = xi2 ;
255  }
256 
257  if( xi1>0 && xi1<Lseg ) {
258  return at(i-1) + JPosition3D(dir_seg)*xi1 ;
259  }
260  }
261  return JPosition3D(0,0,0) ;
262  }
263 
264 
265 }
266 
267 #endif
Data structure for polyline in three dimensions.
Definition: JPolyline3D.hh:25
Data structure for position in three dimensions.
Definition: JPosition3D.hh:38
double getY() const
Get y position.
Definition: JVector3D.hh:104
double getLength() const
Get length.
Definition: JVector3D.hh:246
double getZ() const
Get z position.
Definition: JVector3D.hh:115
double getX() const
Get x position.
Definition: JVector3D.hh:94
Data structure for normalised vector in three dimensions.
Definition: JVersor3D.hh:28
double getDot(const JVersor3D &versor) const
Get dot product.
Definition: JVersor3D.hh:156
Interface for binary input.
Forward declaration of binary output.
Interface for binary output.
A photon path.
Definition: JPhotonPath.hh:38
std::vector< JPosition3D > getPointsWithY(double y)
return all coordinates where the photon path has the given y
Definition: JPhotonPath.hh:145
JIO::JReader & read(JIO::JReader &in)
Read from input.
Definition: JPhotonPath.hh:75
std::vector< JPosition3D > getPointsWithZ(double z)
return all coordinates where the photon path has the given z
Definition: JPhotonPath.hh:160
bool hitsSphere(const JPosition3D &pos, double r)
Returns whether the photon path intersects a sphere of radius r at position pos.
Definition: JPhotonPath.hh:176
JIO::JWriter & write(JIO::JWriter &out) const
Write to output.
Definition: JPhotonPath.hh:88
JPosition3D getSphereHitPosition(const JPosition3D &pos, double r)
Returns the position where the photon hits a sphere of radius r at position pos. If the photon does n...
Definition: JPhotonPath.hh:213
string getString()
get a string with the path vertex positions
Definition: JPhotonPath.hh:136
JPhotonPath()
default constructor
Definition: JPhotonPath.hh:42
std::vector< JPosition3D > getPointsWithX(double x)
return all coordinates where the photon path has the given x
Definition: JPhotonPath.hh:121
double getLength()
get the total path length
Definition: JPhotonPath.hh:104
JPhotonPath(int _nscat)
constructor to actually be used
Definition: JPhotonPath.hh:45
JMODEL::JString getString(const JFit &fit)
Get model parameters of string.
Auxiliary classes and methods for 2D geometrical objects and operations.
Definition: JAngle2D.hh:19
Auxiliary classes and methods for 3D geometrical objects and operations.
Definition: JAngle3D.hh:19
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
const int n
Definition: JPolint.hh:786
data_type r[M+1]
Definition: JPolint.hh:868
Definition: JSTDTypes.hh:14