CMS 3D CMS Logo

CrossingPtBasedLinearizationPointFinder.cc
Go to the documentation of this file.
8 #include <cmath>
9 #include <algorithm>
10 
11 // #define Use_GraphicsHarvester
12 #ifdef Use_GraphicsHarvester
13 #include "RecoVertex/VertexSimpleVis/interface/PrimitivesHarvester.h"
14 #include "Utilities/UI/interface/SimpleConfigurable.h"
18 #endif
19 
20 namespace
21 {
22 inline GlobalPoint operator - ( const GlobalPoint & a, const GlobalPoint & b )
23 {
24  return GlobalPoint ( a.x() - b.x(), a.y() - b.y(), a.z() - b.z() );
25 }
26 
27 inline GlobalPoint operator + ( const GlobalPoint & a, const GlobalPoint & b )
28 {
29  return GlobalPoint ( a.x() + b.x(), a.y() + b.y(), a.z() + b.z() );
30 }
31 
32 inline GlobalPoint operator / ( const GlobalPoint & a, const double b )
33 {
34  return GlobalPoint ( a.x() / b, a.y() / b, a.z() / b );
35 }
36 
37 #ifndef __clang__
38 inline GlobalPoint operator * ( const GlobalPoint & a, const double b )
39 {
40  return GlobalPoint ( a.x() * b, a.y() * b, a.z() * b );
41 }
42 
43 inline GlobalPoint operator * ( const double b , const GlobalPoint & a )
44 {
45  return GlobalPoint ( a.x() * b, a.y() * b, a.z() * b );
46 }
47 #endif
48 
49 inline unsigned int sum ( unsigned int nr )
50 {
51  /*
52  int ret=0;
53  for ( int i=1; i<= nr ; i++ )
54  {
55  ret+=i;
56  }
57  return ret;
58  */
59  return ( nr * ( nr + 1 ) ) / 2;
60 }
61 
62 #ifdef Use_GraphicsHarvester
63 
64 void graphicsDebug ( const std::vector < PointAndDistance > & input )
65 {
66  bool sve = SimpleConfigurable<bool> (false,
67  "CrossingPointStudy:Harvest").value();
68  std::string fname = SimpleConfigurable<string>("crossingpoints.txt",
69  "CrossingPointStudy:FileName").value();
70  if (sve)
71  {
72  for ( std::vector< PointAndDistance >::const_iterator i=input.begin();
73  i!=input.end() ; ++i )
74  {
75  std::map < std::string, MultiType > attrs;
76  attrs["name"]="GlobalPoint";
77  attrs["color"]="yellow";
78  attrs["mag"]=.7;
79  attrs["comment"]="Input for CrossingPtBasedLinearizationPointFinder";
80  PrimitivesHarvester( fname )
81  .save ( i->first, attrs );
82  }
83  std::map < std::string, MultiType > attrs;
84  attrs["name"]="The Mode(*theAlgo)";
85  attrs["color"]="green";
86  attrs["comment"]="Output from CrossingPtBasedLinearizationPointFinder";
87  PrimitivesHarvester( fname )
88  .save ( ret, attrs );
89  GlobalPoint subsethsm = SubsetHsmModeFinder3d()( input );
90  attrs["name"]="The Mode(SubsetHSM)";
91  attrs["color"]="red";
92  PrimitivesHarvester( fname ).save ( subsethsm, attrs );
94  attrs["name"]="The Mode(HSM)";
95  attrs["color"]="blue";
96  PrimitivesHarvester( fname ).save ( hsm, attrs );
98  attrs["name"]="The Mode(LMS)";
99  attrs["color"]="gold";
100  PrimitivesHarvester( fname ).save ( lms, attrs );
101  }
102 }
103 #endif
104 }
105 
107  const ModeFinder3d & algo, const signed int n_pairs ) :
108  useMatrix ( false ) , theNPairs ( n_pairs ), theMatrix ( nullptr ),
109  theAlgo ( algo.clone() )
110 {}
111 
113  const RecTracksDistanceMatrix * m, const ModeFinder3d & algo,
114  const signed int n_pairs ) :
115  useMatrix ( m->hasCrossingPoints() ) ,
116  theNPairs ( n_pairs ), theMatrix ( m ), theAlgo ( algo.clone() )
117 {}
118 
121  useMatrix ( o.useMatrix ), theNPairs ( o.theNPairs ),
122  theMatrix ( o.theMatrix /* nope, we dont clone!! */ ),
123  theAlgo ( o.theAlgo->clone() )
124 {}
125 
127 {
128  delete theAlgo;
129 }
130 
131 std::vector <reco::TransientTrack> CrossingPtBasedLinearizationPointFinder::getBestTracks (
132  const std::vector<reco::TransientTrack> & tracks ) const
133 {
134  unsigned int n_tracks = (2*(unsigned int) (theNPairs)) < tracks.size() ? 2*theNPairs : tracks.size();
135 
136  std::vector <reco::TransientTrack> newtracks = tracks;
137  partial_sort ( newtracks.begin(), newtracks.begin()+n_tracks, newtracks.end(), CompareTwoTracks() );
138  newtracks.erase ( newtracks.begin()+n_tracks, newtracks.end() );
139 
140  /*
141  * old version, when we have a default constructor
142  *
143 
144  std::vector <reco::TransientTrack> newtracks ( n_tracks );
145  partial_sort_copy ( tracks.begin(), tracks.end(), newtracks.begin(),
146  newtracks.begin() + n_tracks , CompareTwoTracks() );
147  */
148 
149  return newtracks;
150 }
151 
152 typedef std::pair < GlobalPoint , float > PointAndDistance;
153 
155  const std::vector<reco::TransientTrack> & tracks ) const
156 {
157  std::vector< PointAndDistance > vgp;
158  // vgp.reserve ( ( tracks.size() * ( tracks.size()-1 ) ) / 2 - 1 );
160  bool status;
161  std::vector<reco::TransientTrack>::const_iterator end=tracks.end();
162  std::vector<reco::TransientTrack>::const_iterator endm1=(end-1);
163  for ( std::vector<reco::TransientTrack>::const_iterator x=tracks.begin();
164  x!=endm1 ; ++x )
165  {
166  for ( std::vector<reco::TransientTrack>::const_iterator y=x+1;
167  y!=end; ++y )
168  {
169  status = ttmd.calculate( (*x).impactPointState(), (*y).impactPointState() );
170  if (status) {
171  std::pair < GlobalPoint, GlobalPoint > pts = ttmd.points();
172  std::pair < GlobalPoint , float > v ( ( pts.second + pts.first ) / 2. ,
173  ( pts.second - pts.first ).mag() );
174  vgp.push_back( v );
175  } // If sth goes wrong, we just dont add. Who cares?
176  }
177  }
178  if (vgp.empty() )
179  {
181  }
182  return find ( vgp );
183 }
184 
186  const std::vector<reco::TransientTrack> & tracks ) const
187 {
188  std::vector< PointAndDistance > vgp;
189  vgp.reserve ( (int) ( tracks.size() * ( tracks.size() -1 ) / 2. - 1 ) );
190  std::vector<reco::TransientTrack>::const_iterator end=tracks.end();
191  std::vector<reco::TransientTrack>::const_iterator endm1=(end-1);
192  for ( std::vector<reco::TransientTrack>::const_iterator x=tracks.begin();
193  x!=endm1 ; ++x )
194  {
195  for ( std::vector<reco::TransientTrack>::const_iterator y=x+1;
196  y!=end; ++y )
197  {
198  PointAndDistance v ( theMatrix->crossingPoint ( *x , *y ),
199  theMatrix->distance ( *x, *y ) );
200  vgp.push_back ( v );
201  }
202  }
203  if (vgp.empty() )
204  {
206  }
207  return find ( vgp );
208 }
209 
211  const std::vector<FreeTrajectoryState> & tracks ) const
212 {
214 }
215 
217  const std::vector < PointAndDistance > & input ) const
218 {
219  /*
220  std::cout << "[CrossingPtBasedLinearizationPointFinder] input size="
221  << input.size() << std::endl;*/
222  GlobalPoint ret = (*theAlgo) (input);
223 #ifdef Use_GraphicsHarvester
224 
225  graphicsDebug ( input );
226 #endif
227 
228  return ret;
229 }
230 
232  const std::vector<reco::TransientTrack> & tracks ) const
233 {
234  if ( tracks.size() < 2 )
235  throw LinPtException
236  ("CrossingPtBasedLinPtFinder: too few tracks given.");
237  std::vector < PointAndDistance > vgp;
238  if ( theNPairs == -1 )
239  {
240  if ( useMatrix )
241  {
242  return useFullMatrix( tracks );
243  }
244  else
245  {
246  return useAllTracks ( tracks );
247  }
248  }
249 
250  if ( sum ( tracks.size() - 1 ) < ((unsigned int) (theNPairs)) )
251  {
252  /*
253  std::cout << "[CrossingPtBasedLinearizationPointFinder] we exploit all track pairs"
254  << std::endl;*/
255  // we have fewer track pair options than is foreseen
256  // in the quota.
257  // so we exploit all tracks pairs.
258  return useAllTracks ( tracks );
259  }
260 
261  std::vector <reco::TransientTrack> goodtracks = getBestTracks ( tracks );
262 
263  // we sort according to momenta.
264  if ( goodtracks.size() < 2 )
265  throw LinPtException (
266  "CrossingPtBasedLinPtFinder: less than two tracks given");
267  // vgp.reserve ( theNPairs - 1 );
268  unsigned int t_first = 0;
269  unsigned int t_interval = goodtracks.size() / 2;
270  unsigned int lim = goodtracks.size() - 1;
271 
272  /*
273  std::cout << "[CrossingPtBasedLinearizationPointFinder] we start: npairs=" << theNPairs
274  << std::endl;
275  std::cout << "[CrossingPtBasedLinearizationPointFinder] t_interval=" << t_interval << std::endl;
276  std::cout << "[CrossingPtBasedLinearizationPointFinder goodtracks.size=" << goodtracks.size()
277  << std::endl;*/
278 
279  // the 'direction' false: intervals will expand
280  // true: intervals will shrink
281  bool dir = false;
282 
283  while ( vgp.size() < ((unsigned int) (theNPairs)) )
284  {
285  reco::TransientTrack rt1 = goodtracks [ t_first ];
286  reco::TransientTrack rt2 = goodtracks [ t_first + t_interval ];
287  // std::cout << "Considering now: " << t_first << ", " << t_first+t_interval << std::endl;
288  if ( useMatrix )
289  {
290  PointAndDistance v ( theMatrix->crossingPoint ( rt1, rt2 ),
291  theMatrix->distance ( rt1, rt2 ) );
292  vgp.push_back ( v );
293  }
294  else
295  { // No DistanceMatrix available
297  bool status = ttmd.calculate( rt1.impactPointState(), rt2.impactPointState() );
298  if (status) {
299  std::pair < GlobalPoint, GlobalPoint > pts = ttmd.points();
300  PointAndDistance v ( ( pts.second + pts.first ) / 2. ,
301  ( pts.second - pts.first ).mag() );
302  vgp.push_back( v );
303  }
304  }
305  if ( ( t_first + t_interval ) < lim )
306  {
307  t_first++;
308  }
309  else if ( dir )
310  {
311  t_first=0;
312  t_interval--;
313  if ( t_interval == 0 )
314  {
315  /* std::cout << "[CrossingPtBasedLinearizationPointFinder] t_interval=0. break."
316  << std::endl;*/
317  break;
318  }
319  }
320  else
321  {
322  t_first=0;
323  t_interval++;
324  if ( t_interval == goodtracks.size() )
325  {
326  /* std::cout << "[CrossingPtBasedLinearizationPointFinder] t_interval="
327  << goodtracks.size() << "(max). start to decrease intervals"
328  << std::endl; */
329  dir=true;
330  t_interval = goodtracks.size() / 2 - 1;
331  }
332  }
333  }
334  if (vgp.empty() )
335  {
336  // no crossing points? Fallback to a crossingpoint-less lin pt finder!
338  }
339  return find ( vgp );
340 
341  return GlobalPoint(0.,0.,0.); // if nothing else, then return 0,0,0
342 }
GlobalPoint useAllTracks(const std::vector< reco::TransientTrack > &) const
GlobalPoint find(const std::vector< std::pair< GlobalPoint, float > > &) const
std::pair< GlobalPoint, GlobalPoint > points() const override
std::pair< GlobalPoint, float > PointAndDistance
MatrixMeschach operator+(const MatrixMeschach &mat1, const MatrixMeschach &mat2)
virtual double distance(const reco::TransientTrack, const reco::TransientTrack) const =0
MatrixMeschach operator-(const MatrixMeschach &mat1, const MatrixMeschach &mat2)
Global3DPoint GlobalPoint
Definition: GlobalPoint.h:10
T y() const
Definition: PV3DBase.h:63
GlobalPoint getLinearizationPoint(const std::vector< reco::TransientTrack > &) const override
std::vector< reco::TransientTrack > getBestTracks(const std::vector< reco::TransientTrack > &) const
#define nullptr
Basic3DVector< long double > operator/(const Basic3DVector< long double > &v, S s)
static std::string const input
Definition: EdmProvDump.cc:44
bool calculate(const TrajectoryStateOnSurface &sta, const TrajectoryStateOnSurface &stb) override
static const double pts[33]
Definition: Constants.h:30
CrossingPtBasedLinearizationPointFinder(const ModeFinder3d &algo, const signed int n_pairs=5)
T z() const
Definition: PV3DBase.h:64
virtual ModeFinder3d * clone() const =0
#define end
Definition: vmac.h:37
CrossingPtBasedLinearizationPointFinder * clone() const override
virtual GlobalPoint getLinearizationPoint(const std::vector< reco::TransientTrack > &) const =0
GlobalPoint getLinearizationPoint(const std::vector< reco::TransientTrack > &) const override
double b
Definition: hdecay.h:120
virtual GlobalPoint crossingPoint(const reco::TransientTrack, const reco::TransientTrack) const =0
TEveGeoShape * clone(const TEveElement *element, TEveElement *parent)
Definition: eve_macros.cc:135
string fname
main script
double a
Definition: hdecay.h:121
TrajectoryStateOnSurface impactPointState() const
MatrixMeschach operator*(const MatrixMeschach &mat1, const MatrixMeschach &mat2)
dbl *** dir
Definition: mlp_gen.cc:35
GlobalPoint useFullMatrix(const std::vector< reco::TransientTrack > &) const
T x() const
Definition: PV3DBase.h:62