CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
TIDLayer.cc
Go to the documentation of this file.
1 #include "TIDLayer.h"
2 
4 
6 
9 
10 #include<array>
11 #include "DetGroupMerger.h"
12 
13 
14 //#include "CommonDet/DetLayout/src/DetLessR.h"
15 
16 
17 using namespace std;
18 
20 
21 namespace {
22 
23 // 2 0 1
24 /*
25 class TIDringLess {
26  // z-position ordering of TID rings indices
27 public:
28  bool operator()( const pair<vector<DetGroup> const *,int> & a,const pair<vector<DetGroup>const *,int> & b) const {
29  if(a.second==2) {return true;}
30  else if(a.second==0) {
31  if(b.second==2) return false;
32  if(b.second==1) return true;
33  }
34  return false;
35  };
36 };
37 */
38 
39  // groups in correct order: one may be empty
40  inline
41  void mergeOutward(std::array<vector<DetGroup>,3> & groups,
42  std::vector<DetGroup> & result ) {
43  typedef DetGroupMerger Merger;
44  Merger::orderAndMergeTwoLevels(std::move(groups[0]),
45  std::move(groups[1]),result,1,1);
46  if(!groups[2].empty()) {
47  std::vector<DetGroup> tmp;
48  tmp.swap(result);
49  Merger::orderAndMergeTwoLevels(std::move(tmp),std::move(groups[2]),result,1,1);
50  }
51 
52  }
53 
54  inline
55  void mergeInward(std::array<vector<DetGroup>,3> & groups,
56  std::vector<DetGroup> & result ) {
57  typedef DetGroupMerger Merger;
58  Merger::orderAndMergeTwoLevels(std::move(groups[2]),
59  std::move(groups[1]),result,1,1);
60  if(!groups[0].empty()) {
61  std::vector<DetGroup> tmp;
62  tmp.swap(result);
63  Merger::orderAndMergeTwoLevels(std::move(tmp),std::move(groups[0]),result,1,1);
64  }
65 
66  }
67 
68 
69  void
70  orderAndMergeLevels(const TrajectoryStateOnSurface& tsos,
71  const Propagator& prop,
72  std::array<vector<DetGroup>,3> & groups,
73  std::vector<DetGroup> & result ) {
74 
75  float zpos = tsos.globalPosition().z();
76  if(tsos.globalMomentum().z()*zpos>0){ // momentum points outwards
78  mergeOutward(groups,result);
79  else
80  mergeInward(groups,result);
81  }
82  else{ // momentum points inwards
84  mergeOutward(groups,result);
85  else
86  mergeInward(groups,result);
87  }
88 
89  }
90 
91 }
92 
93 //hopefully is never called!
94 const std::vector<const GeometricSearchDet*>& TIDLayer::components() const{
95  if( not theComponents) {
96  std::unique_ptr<std::vector<const GeometricSearchDet*>> temp( new std::vector<const GeometricSearchDet*>() );
97  temp->reserve(3);
98  for ( auto c: theComps) temp->push_back(c);
99  std::vector<const GeometricSearchDet*>* expected = nullptr;
100  if(theComponents.compare_exchange_strong(expected,temp.get())) {
101  //this thread set the value
102  temp.release();
103  }
104  }
105 
106  return *theComponents;
107  }
108 
109 
110 void
112  const BoundDisk& ringDisk = static_cast<const BoundDisk&>(theComps[i]->surface());
113  float ringMinZ = std::abs( ringDisk.position().z()) - ringDisk.bounds().thickness()/2.;
114  float ringMaxZ = std::abs( ringDisk.position().z()) + ringDisk.bounds().thickness()/2.;
115  ringPars[i].thetaRingMin = ringDisk.innerRadius()/ ringMaxZ;
116  ringPars[i].thetaRingMax = ringDisk.outerRadius()/ ringMinZ;
117  ringPars[i].theRingR=( ringDisk.innerRadius() +
118  ringDisk.outerRadius())/2.;
119 
120 }
121 
122 
123 TIDLayer::TIDLayer(vector<const TIDRing*>& rings) :
125  theComponents{nullptr}
126 {
127  //They should be already R-ordered. TO BE CHECKED!!
128  //sort( theRings.begin(), theRings.end(), DetLessR());
129 
130  if ( rings.size() != 3) throw DetLayerException("Number of rings in TID layer is not equal to 3 !!");
131  setSurface( computeDisk( rings ) );
132 
133  for(int i=0; i!=3; ++i) {
134  theComps[i]=rings[i];
135  fillRingPars(i);
136  theBasicComps.insert(theBasicComps.end(),
137  (*rings[i]).basicComponents().begin(),
138  (*rings[i]).basicComponents().end());
139  }
140 
141 
142  LogDebug("TkDetLayers") << "==== DEBUG TIDLayer =====" ;
143  LogDebug("TkDetLayers") << "r,zed pos , thickness, innerR, outerR: "
144  << this->position().perp() << " , "
145  << this->position().z() << " , "
146  << this->specificSurface().bounds().thickness() << " , "
147  << this->specificSurface().innerRadius() << " , "
148  << this->specificSurface().outerRadius() ;
149 }
150 
151 
152 BoundDisk*
153 TIDLayer::computeDisk( const vector<const TIDRing*>& rings) const
154 {
155  float theRmin = rings.front()->specificSurface().innerRadius();
156  float theRmax = rings.front()->specificSurface().outerRadius();
157  float theZmin = rings.front()->position().z() -
158  rings.front()->surface().bounds().thickness()/2;
159  float theZmax = rings.front()->position().z() +
160  rings.front()->surface().bounds().thickness()/2;
161 
162  for (vector<const TIDRing*>::const_iterator i = rings.begin(); i != rings.end(); i++) {
163  float rmin = (**i).specificSurface().innerRadius();
164  float rmax = (**i).specificSurface().outerRadius();
165  float zmin = (**i).position().z() - (**i).surface().bounds().thickness()/2.;
166  float zmax = (**i).position().z() + (**i).surface().bounds().thickness()/2.;
167  theRmin = min( theRmin, rmin);
168  theRmax = max( theRmax, rmax);
169  theZmin = min( theZmin, zmin);
170  theZmax = max( theZmax, zmax);
171  }
172 
173  float zPos = (theZmax+theZmin)/2.;
174  PositionType pos(0.,0.,zPos);
176 
177  return new BoundDisk( pos, rot, new SimpleDiskBounds(theRmin, theRmax,
178  theZmin-zPos, theZmax-zPos));
179 
180 }
181 
182 
184  for (auto c : theComps) delete c;
185 
186  delete theComponents.load();
187 }
188 
189 
190 
191 void
193  const Propagator& prop,
194  const MeasurementEstimator& est,
195  std::vector<DetGroup> & result) const
196 {
197  std::array<int,3> const & ringIndices = ringIndicesByCrossingProximity(startingState,prop);
198  if ( ringIndices[0]==-1 ) {
199  edm::LogError("TkDetLayers") << "TkRingedForwardLayer::groupedCompatibleDets : error in CrossingProximity";
200  return;
201  }
202 
203  std::array<vector<DetGroup>,3> groupsAtRingLevel;
204  //order is ring3,ring1,ring2 i.e. 2 0 1
205  // 0 1 2
206 #ifdef __INTEL_COMPILER
207  const int ringOrder[3]{1,2,0};
208 #else
209  constexpr int ringOrder[3]{1,2,0};
210 #endif
211  auto index = [&ringIndices,& ringOrder](int i) { return ringOrder[ringIndices[i]];};
212 
213  auto & closestResult = groupsAtRingLevel[index(0)];
214  theComps[ringIndices[0]]->groupedCompatibleDetsV( startingState, prop, est, closestResult);
215  if ( closestResult.empty() ){
216  theComps[ringIndices[1]]->groupedCompatibleDetsV( startingState, prop, est, result);
217  return;
218  }
219 
220  DetGroupElement closestGel( closestResult.front().front());
221  float rWindow = computeWindowSize( closestGel.det(), closestGel.trajectoryState(), est);
222 
223  if(!overlapInR(closestGel.trajectoryState(),ringIndices[1],rWindow)) {
224  result.swap(closestResult);
225  return;
226  }
227 
228 
229  auto & nextResult = groupsAtRingLevel[index(1)];
230  theComps[ringIndices[1]]->groupedCompatibleDetsV( startingState, prop, est, nextResult);
231  if(nextResult.empty()) {
232  result.swap(closestResult);
233  return;
234  }
235 
236  if(!overlapInR(closestGel.trajectoryState(),ringIndices[2],rWindow) ) {
237  //then merge 2 levels & return
238  orderAndMergeLevels(closestGel.trajectoryState(),prop,groupsAtRingLevel,result);
239  return;
240  }
241 
242  auto & nextNextResult = groupsAtRingLevel[index(2)];
243  theComps[ringIndices[2]]->groupedCompatibleDetsV( startingState, prop, est, nextNextResult);
244  if(nextNextResult.empty()) {
245  // then merge 2 levels and return
246  orderAndMergeLevels(closestGel.trajectoryState(),prop,groupsAtRingLevel,result); //
247  return;
248  }
249 
250  // merge 3 level and return merged
251  orderAndMergeLevels(closestGel.trajectoryState(),prop,groupsAtRingLevel, result);
252 }
253 
254 
255 std::array<int,3>
257  const Propagator& prop ) const
258 {
259  typedef HelixForwardPlaneCrossing Crossing;
261 
262  HelixPlaneCrossing::PositionType startPos( startingState.globalPosition());
263  HelixPlaneCrossing::DirectionType startDir( startingState.globalMomentum());
265  float rho( startingState.transverseCurvature());
266 
267  // calculate the crossings with the ring surfaces
268  // rings are assumed to be sorted in R !
269 
270  Crossing myXing( startPos, startDir, rho, propDir );
271 
272  GlobalPoint ringCrossings[3];
273  // vector<GlobalVector> ringXDirections;
274 
275  for (int i = 0; i < 3 ; i++ ) {
276  const BoundDisk & theRing = static_cast<const BoundDisk &>(theComps[i]->surface());
277  pair<bool,double> pathlen = myXing.pathLength( theRing);
278  if ( pathlen.first ) {
279  ringCrossings[i] = GlobalPoint( myXing.position(pathlen.second ));
280  // ringXDirections.push_back( GlobalVector( myXing.direction(pathlen.second )));
281  } else {
282  // TO FIX.... perhaps there is something smarter to do
283  //throw DetLayerException("trajectory doesn't cross TID rings");
284  ringCrossings[i] = GlobalPoint( 0.,0.,0.);
285  // ringXDirections.push_back( GlobalVector( 0.,0.,0.));
286  }
287  }
288 
289  int closestIndex = findClosest(ringCrossings);
290  int nextIndex = findNextIndex(ringCrossings,closestIndex);
291  if ( closestIndex<0 || nextIndex<0 ) return std::array<int,3>{{-1,-1,-1}};
292  int nextNextIndex = -1;
293  for(int i=0; i<3 ; i++){
294  if(i!= closestIndex && i!=nextIndex) {
295  nextNextIndex = i;
296  break;
297  }
298  }
299 
300  std::array<int,3> indices{{closestIndex,nextIndex,nextNextIndex}};
301  return indices;
302 }
303 
304 
305 
306 
307 float
309  const TrajectoryStateOnSurface& tsos,
310  const MeasurementEstimator& est) const
311 {
312  const Plane& startPlane = det->surface();
314  est.maximalLocalDisplacement( tsos, startPlane);
315  return maxDistance.y();
316 }
317 
318 
319 int
320 TIDLayer::findClosest(const GlobalPoint ringCrossing[3] ) const
321 {
322  int theBin = 0;
323  float initialR = ringPars[0].theRingR;
324  float rDiff = std::abs( ringCrossing[0].perp() - initialR);
325  for (int i = 1; i < 3 ; i++){
326  float ringR = ringPars[i].theRingR;
327  float testDiff = std::abs( ringCrossing[i].perp() - ringR);
328  if ( testDiff<rDiff ) {
329  rDiff = testDiff;
330  theBin = i;
331  }
332  }
333  return theBin;
334 }
335 
336 int
337 TIDLayer::findNextIndex(const GlobalPoint ringCrossing[3], int closest ) const
338 {
339 
340  int firstIndexToCheck = (closest != 0)? 0 : 1;
341  float initialR = ringPars[firstIndexToCheck].theRingR;
342  float rDiff = std::abs( ringCrossing[firstIndexToCheck].perp() - initialR);
343  int theBin = firstIndexToCheck;
344  for (int i = firstIndexToCheck+1; i < 3 ; i++){
345  if ( i != closest) {
346  float ringR = ringPars[i].theRingR;
347  float testDiff = std::abs( ringCrossing[i].perp() - ringR);
348  if ( testDiff<rDiff ) {
349  rDiff = testDiff;
350  theBin = i;
351  }
352  }
353  }
354  return theBin;
355 }
356 
357 
358 
359 bool
360 TIDLayer::overlapInR( const TrajectoryStateOnSurface& tsos, int index, double ymax ) const
361 {
362  // assume "fixed theta window", i.e. margin in local y = r is changing linearly with z
363  float tsRadius = tsos.globalPosition().perp();
364  float thetamin = ( max(0.,tsRadius-ymax))/(std::abs(tsos.globalPosition().z())+10.f); // add 10 cm contingency
365  float thetamax = ( tsRadius + ymax)/(std::abs(tsos.globalPosition().z())-10.f);
366 
367  // do the theta regions overlap ?
368 
369  return !( thetamin > ringPars[index].thetaRingMax || ringPars[index].thetaRingMin > thetamax);
370 }
371 
372 
#define LogDebug(id)
TkRotation< Scalar > RotationType
Definition: Definitions.h:29
Common base class.
int i
Definition: DBlmapReader.cc:9
tuple array
Definition: mps_check.py:181
T y() const
Definition: PV2DBase.h:46
virtual const std::vector< const GeometricSearchDet * > & components() const __attribute__((cold))
Definition: TIDLayer.cc:94
T perp() const
Definition: PV3DBase.h:72
virtual Local2DVector maximalLocalDisplacement(const TrajectoryStateOnSurface &ts, const Plane &plane) const =0
Global3DPoint GlobalPoint
Definition: GlobalPoint.h:10
std::array< int, 3 > ringIndicesByCrossingProximity(const TrajectoryStateOnSurface &startingState, const Propagator &prop) const
Definition: TIDLayer.cc:256
GlobalPoint globalPosition() const
Vector2DBase< float, LocalTag > Local2DVector
virtual PropagationDirection propagationDirection() const final
Definition: Propagator.h:151
PropagationDirection
const Plane & surface() const
The nominal surface of the GeomDet.
Definition: GeomDet.h:40
Definition: Plane.h:17
#define constexpr
float thetaRingMin
Definition: TIDLayer.h:66
tuple result
Definition: mps_fire.py:95
Point3DBase< Scalar, GlobalTag > PositionType
Definition: Definitions.h:30
float thetaRingMax
Definition: TIDLayer.h:66
RingPar ringPars[3]
Definition: TIDLayer.h:67
T z() const
Definition: PV3DBase.h:64
TIDLayer(std::vector< const TIDRing * > &rings) __attribute__((cold))
Definition: TIDLayer.cc:123
def move
Definition: eostools.py:510
~TIDLayer() __attribute__((cold))
Definition: TIDLayer.cc:183
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
double f[11][100]
T min(T a, T b)
Definition: MathUtil.h:58
std::pair< const GeomDet *, TrajectoryStateOnSurface > DetWithState
virtual const BoundSurface & surface() const
The surface of the GeometricSearchDet.
Definition: TIDRing.h:21
std::atomic< std::vector< const GeometricSearchDet * > * > theComponents
Definition: TIDLayer.h:64
void fillRingPars(int i) __attribute__((cold))
Definition: TIDLayer.cc:111
bool overlapInR(const TrajectoryStateOnSurface &tsos, int i, double ymax) const __attribute__((hot))
Definition: TIDLayer.cc:360
Disk BoundDisk
Definition: BoundDisk.h:62
std::vector< std::vector< double > > tmp
Definition: MVATrainer.cc:100
T perp() const
Magnitude of transverse component.
GlobalVector globalMomentum() const
static int position[264][3]
Definition: ReadPGInfo.cc:509
float computeWindowSize(const GeomDet *det, const TrajectoryStateOnSurface &tsos, const MeasurementEstimator &est) const __attribute__((hot))
Definition: TIDLayer.cc:308
BoundDisk * computeDisk(const std::vector< const TIDRing * > &rings) const __attribute__((cold))
Definition: TIDLayer.cc:153
std::pair< const GeomDet *, TrajectoryStateOnSurface > DetWithState
const TIDRing * theComps[3]
Definition: TIDLayer.h:65
int findClosest(const GlobalPoint[3]) const __attribute__((hot))
Definition: TIDLayer.cc:320
void groupedCompatibleDetsV(const TrajectoryStateOnSurface &tsos, const Propagator &prop, const MeasurementEstimator &est, std::vector< DetGroup > &result) const __attribute__((hot))
Definition: TIDRing.cc:92
int findNextIndex(const GlobalPoint[3], int) const __attribute__((hot))
Definition: TIDLayer.cc:337
void groupedCompatibleDetsV(const TrajectoryStateOnSurface &tsos, const Propagator &prop, const MeasurementEstimator &est, std::vector< DetGroup > &result) const __attribute__((hot))
Definition: TIDLayer.cc:192