CMS 3D CMS Logo

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