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  inline GlobalPoint operator-(const GlobalPoint& a, const GlobalPoint& b) {
22  return GlobalPoint(a.x() - b.x(), a.y() - b.y(), a.z() - b.z());
23  }
24 
25  inline GlobalPoint operator+(const GlobalPoint& a, const GlobalPoint& b) {
26  return GlobalPoint(a.x() + b.x(), a.y() + b.y(), a.z() + b.z());
27  }
28 
29  inline GlobalPoint operator/(const GlobalPoint& a, const double b) {
30  return GlobalPoint(a.x() / b, a.y() / b, a.z() / b);
31  }
32 
33 #ifndef __clang__
34  inline GlobalPoint operator*(const GlobalPoint& a, const double b) {
35  return GlobalPoint(a.x() * b, a.y() * b, a.z() * b);
36  }
37 
38  inline GlobalPoint operator*(const double b, const GlobalPoint& a) {
39  return GlobalPoint(a.x() * b, a.y() * b, a.z() * b);
40  }
41 #endif
42 
43  inline unsigned int sum(unsigned int nr) {
44  /*
45  int ret=0;
46  for ( int i=1; i<= nr ; i++ )
47  {
48  ret+=i;
49  }
50  return ret;
51  */
52  return (nr * (nr + 1)) / 2;
53  }
54 
55 #ifdef Use_GraphicsHarvester
56 
57  void graphicsDebug(const std::vector<PointAndDistance>& input) {
58  bool sve = SimpleConfigurable<bool>(false, "CrossingPointStudy:Harvest").value();
59  std::string fname = SimpleConfigurable<string>("crossingpoints.txt", "CrossingPointStudy:FileName").value();
60  if (sve) {
61  for (std::vector<PointAndDistance>::const_iterator i = input.begin(); i != input.end(); ++i) {
62  std::map<std::string, MultiType> attrs;
63  attrs["name"] = "GlobalPoint";
64  attrs["color"] = "yellow";
65  attrs["mag"] = .7;
66  attrs["comment"] = "Input for CrossingPtBasedLinearizationPointFinder";
67  PrimitivesHarvester(fname).save(i->first, attrs);
68  }
69  std::map<std::string, MultiType> attrs;
70  attrs["name"] = "The Mode(*theAlgo)";
71  attrs["color"] = "green";
72  attrs["comment"] = "Output from CrossingPtBasedLinearizationPointFinder";
73  PrimitivesHarvester(fname).save(ret, attrs);
75  attrs["name"] = "The Mode(SubsetHSM)";
76  attrs["color"] = "red";
77  PrimitivesHarvester(fname).save(subsethsm, attrs);
79  attrs["name"] = "The Mode(HSM)";
80  attrs["color"] = "blue";
81  PrimitivesHarvester(fname).save(hsm, attrs);
83  attrs["name"] = "The Mode(LMS)";
84  attrs["color"] = "gold";
85  PrimitivesHarvester(fname).save(lms, attrs);
86  }
87  }
88 #endif
89 } // namespace
90 
92  const signed int n_pairs)
93  : useMatrix(false), theNPairs(n_pairs), theMatrix(nullptr), theAlgo(algo.clone()) {}
94 
96  const ModeFinder3d& algo,
97  const signed int n_pairs)
98  : useMatrix(m->hasCrossingPoints()), theNPairs(n_pairs), theMatrix(m), theAlgo(algo.clone()) {}
99 
102  : useMatrix(o.useMatrix),
103  theNPairs(o.theNPairs),
104  theMatrix(o.theMatrix /* nope, we dont clone!! */),
105  theAlgo(o.theAlgo->clone()) {}
106 
108 
110  const std::vector<reco::TransientTrack>& tracks) const {
111  unsigned int n_tracks = (2 * (unsigned int)(theNPairs)) < tracks.size() ? 2 * theNPairs : tracks.size();
112 
113  std::vector<reco::TransientTrack> newtracks = tracks;
114  partial_sort(newtracks.begin(), newtracks.begin() + n_tracks, newtracks.end(), CompareTwoTracks());
115  newtracks.erase(newtracks.begin() + n_tracks, newtracks.end());
116 
117  /*
118  * old version, when we have a default constructor
119  *
120 
121  std::vector <reco::TransientTrack> newtracks ( n_tracks );
122  partial_sort_copy ( tracks.begin(), tracks.end(), newtracks.begin(),
123  newtracks.begin() + n_tracks , CompareTwoTracks() );
124  */
125 
126  return newtracks;
127 }
128 
129 typedef std::pair<GlobalPoint, float> PointAndDistance;
130 
132  const std::vector<reco::TransientTrack>& tracks) const {
133  std::vector<PointAndDistance> vgp;
134  // vgp.reserve ( ( tracks.size() * ( tracks.size()-1 ) ) / 2 - 1 );
136  bool status;
137  std::vector<reco::TransientTrack>::const_iterator end = tracks.end();
138  std::vector<reco::TransientTrack>::const_iterator endm1 = (end - 1);
139  for (std::vector<reco::TransientTrack>::const_iterator x = tracks.begin(); x != endm1; ++x) {
140  for (std::vector<reco::TransientTrack>::const_iterator y = x + 1; y != end; ++y) {
141  status = ttmd.calculate((*x).impactPointState(), (*y).impactPointState());
142  if (status) {
143  std::pair<GlobalPoint, GlobalPoint> pts = ttmd.points();
144  std::pair<GlobalPoint, float> v((pts.second + pts.first) / 2., (pts.second - pts.first).mag());
145  vgp.push_back(v);
146  } // If sth goes wrong, we just dont add. Who cares?
147  }
148  }
149  if (vgp.empty()) {
151  }
152  return find(vgp);
153 }
154 
156  const std::vector<reco::TransientTrack>& tracks) const {
157  std::vector<PointAndDistance> vgp;
158  vgp.reserve((int)(tracks.size() * (tracks.size() - 1) / 2. - 1));
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(); x != endm1; ++x) {
162  for (std::vector<reco::TransientTrack>::const_iterator y = x + 1; y != end; ++y) {
164  vgp.push_back(v);
165  }
166  }
167  if (vgp.empty()) {
169  }
170  return find(vgp);
171 }
172 
174  const std::vector<FreeTrajectoryState>& tracks) const {
176 }
177 
178 GlobalPoint CrossingPtBasedLinearizationPointFinder::find(const std::vector<PointAndDistance>& input) const {
179  /*
180  std::cout << "[CrossingPtBasedLinearizationPointFinder] input size="
181  << input.size() << std::endl;*/
182  GlobalPoint ret = (*theAlgo)(input);
183 #ifdef Use_GraphicsHarvester
184 
185  graphicsDebug(input);
186 #endif
187 
188  return ret;
189 }
190 
192  const std::vector<reco::TransientTrack>& tracks) const {
193  if (tracks.size() < 2)
194  throw LinPtException("CrossingPtBasedLinPtFinder: too few tracks given.");
195  std::vector<PointAndDistance> vgp;
196  if (theNPairs == -1) {
197  if (useMatrix) {
198  return useFullMatrix(tracks);
199  } else {
200  return useAllTracks(tracks);
201  }
202  }
203 
204  if (sum(tracks.size() - 1) < ((unsigned int)(theNPairs))) {
205  /*
206  std::cout << "[CrossingPtBasedLinearizationPointFinder] we exploit all track pairs"
207  << std::endl;*/
208  // we have fewer track pair options than is foreseen
209  // in the quota.
210  // so we exploit all tracks pairs.
211  return useAllTracks(tracks);
212  }
213 
214  std::vector<reco::TransientTrack> goodtracks = getBestTracks(tracks);
215 
216  // we sort according to momenta.
217  if (goodtracks.size() < 2)
218  throw LinPtException("CrossingPtBasedLinPtFinder: less than two tracks given");
219  // vgp.reserve ( theNPairs - 1 );
220  unsigned int t_first = 0;
221  unsigned int t_interval = goodtracks.size() / 2;
222  unsigned int lim = goodtracks.size() - 1;
223 
224  /*
225  std::cout << "[CrossingPtBasedLinearizationPointFinder] we start: npairs=" << theNPairs
226  << std::endl;
227  std::cout << "[CrossingPtBasedLinearizationPointFinder] t_interval=" << t_interval << std::endl;
228  std::cout << "[CrossingPtBasedLinearizationPointFinder goodtracks.size=" << goodtracks.size()
229  << std::endl;*/
230 
231  // the 'direction' false: intervals will expand
232  // true: intervals will shrink
233  bool dir = false;
234 
235  while (vgp.size() < ((unsigned int)(theNPairs))) {
236  reco::TransientTrack rt1 = goodtracks[t_first];
237  reco::TransientTrack rt2 = goodtracks[t_first + t_interval];
238  // std::cout << "Considering now: " << t_first << ", " << t_first+t_interval << std::endl;
239  if (useMatrix) {
241  vgp.push_back(v);
242  } else { // No DistanceMatrix available
244  bool status = ttmd.calculate(rt1.impactPointState(), rt2.impactPointState());
245  if (status) {
246  std::pair<GlobalPoint, GlobalPoint> pts = ttmd.points();
247  PointAndDistance v((pts.second + pts.first) / 2., (pts.second - pts.first).mag());
248  vgp.push_back(v);
249  }
250  }
251  if ((t_first + t_interval) < lim) {
252  t_first++;
253  } else if (dir) {
254  t_first = 0;
255  t_interval--;
256  if (t_interval == 0) {
257  /* std::cout << "[CrossingPtBasedLinearizationPointFinder] t_interval=0. break."
258  << std::endl;*/
259  break;
260  }
261  } else {
262  t_first = 0;
263  t_interval++;
264  if (t_interval == goodtracks.size()) {
265  /* std::cout << "[CrossingPtBasedLinearizationPointFinder] t_interval="
266  << goodtracks.size() << "(max). start to decrease intervals"
267  << std::endl; */
268  dir = true;
269  t_interval = goodtracks.size() / 2 - 1;
270  }
271  }
272  }
273  if (vgp.empty()) {
274  // no crossing points? Fallback to a crossingpoint-less lin pt finder!
276  }
277  return find(vgp);
278 
279  return GlobalPoint(0., 0., 0.); // if nothing else, then return 0,0,0
280 }
GlobalPoint useFullMatrix(const std::vector< reco::TransientTrack > &) const
std::pair< GlobalPoint, float > PointAndDistance
ret
prodAgent to be discontinued
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
static std::string const input
Definition: EdmProvDump.cc:50
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)
std::pair< GlobalPoint, GlobalPoint > points() const override
T1 operator/(const Phi< T1, Range > &a, const Phi< T1, Range > &b)
Division.
Definition: Phi.h:176
virtual GlobalPoint crossingPoint(const reco::TransientTrack, const reco::TransientTrack) const =0
auto const & tracks
cannot be loose
GlobalPoint find(const std::vector< std::pair< GlobalPoint, float > > &) const
double b
Definition: hdecay.h:118
TEveGeoShape * clone(const TEveElement *element, TEveElement *parent)
Definition: eve_macros.cc:135
virtual GlobalPoint getLinearizationPoint(const std::vector< reco::TransientTrack > &) const =0
string fname
main script
GlobalPoint getLinearizationPoint(const std::vector< reco::TransientTrack > &) const override
double a
Definition: hdecay.h:119
GlobalPoint getLinearizationPoint(const std::vector< reco::TransientTrack > &) const override
MatrixMeschach operator*(const MatrixMeschach &mat1, const MatrixMeschach &mat2)
std::vector< reco::TransientTrack > getBestTracks(const std::vector< reco::TransientTrack > &) const
TrajectoryStateOnSurface impactPointState() const
GlobalPoint useAllTracks(const std::vector< reco::TransientTrack > &) const