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  constexpr int ringOrder[3]{1,2,0};
207  auto index = [&ringIndices,& ringOrder](int i) { return ringOrder[ringIndices[i]];};
208 
209  auto & closestResult = groupsAtRingLevel[index(0)];
210  theComps[ringIndices[0]]->groupedCompatibleDetsV( startingState, prop, est, closestResult);
211  if ( closestResult.empty() ){
212  theComps[ringIndices[1]]->groupedCompatibleDetsV( startingState, prop, est, result);
213  return;
214  }
215 
216  DetGroupElement closestGel( closestResult.front().front());
217  float rWindow = computeWindowSize( closestGel.det(), closestGel.trajectoryState(), est);
218 
219  if(!overlapInR(closestGel.trajectoryState(),ringIndices[1],rWindow)) {
220  result.swap(closestResult);
221  return;
222  }
223 
224 
225  auto & nextResult = groupsAtRingLevel[index(1)];
226  theComps[ringIndices[1]]->groupedCompatibleDetsV( startingState, prop, est, nextResult);
227  if(nextResult.empty()) {
228  result.swap(closestResult);
229  return;
230  }
231 
232  if(!overlapInR(closestGel.trajectoryState(),ringIndices[2],rWindow) ) {
233  //then merge 2 levels & return
234  orderAndMergeLevels(closestGel.trajectoryState(),prop,groupsAtRingLevel,result);
235  return;
236  }
237 
238  auto & nextNextResult = groupsAtRingLevel[index(2)];
239  theComps[ringIndices[2]]->groupedCompatibleDetsV( startingState, prop, est, nextNextResult);
240  if(nextNextResult.empty()) {
241  // then merge 2 levels and return
242  orderAndMergeLevels(closestGel.trajectoryState(),prop,groupsAtRingLevel,result); //
243  return;
244  }
245 
246  // merge 3 level and return merged
247  orderAndMergeLevels(closestGel.trajectoryState(),prop,groupsAtRingLevel, result);
248 }
249 
250 
251 std::array<int,3>
253  const Propagator& prop ) const
254 {
255  typedef HelixForwardPlaneCrossing Crossing;
257 
258  HelixPlaneCrossing::PositionType startPos( startingState.globalPosition());
259  HelixPlaneCrossing::DirectionType startDir( startingState.globalMomentum());
261  float rho( startingState.transverseCurvature());
262 
263  // calculate the crossings with the ring surfaces
264  // rings are assumed to be sorted in R !
265 
266  Crossing myXing( startPos, startDir, rho, propDir );
267 
268  GlobalPoint ringCrossings[3];
269  // vector<GlobalVector> ringXDirections;
270 
271  for (int i = 0; i < 3 ; i++ ) {
272  const BoundDisk & theRing = static_cast<const BoundDisk &>(theComps[i]->surface());
273  pair<bool,double> pathlen = myXing.pathLength( theRing);
274  if ( pathlen.first ) {
275  ringCrossings[i] = GlobalPoint( myXing.position(pathlen.second ));
276  // ringXDirections.push_back( GlobalVector( myXing.direction(pathlen.second )));
277  } else {
278  // TO FIX.... perhaps there is something smarter to do
279  //throw DetLayerException("trajectory doesn't cross TID rings");
280  ringCrossings[i] = GlobalPoint( 0.,0.,0.);
281  // ringXDirections.push_back( GlobalVector( 0.,0.,0.));
282  }
283  }
284 
285  int closestIndex = findClosest(ringCrossings);
286  int nextIndex = findNextIndex(ringCrossings,closestIndex);
287  if ( closestIndex<0 || nextIndex<0 ) return std::array<int,3>{{-1,-1,-1}};
288  int nextNextIndex = -1;
289  for(int i=0; i<3 ; i++){
290  if(i!= closestIndex && i!=nextIndex) {
291  nextNextIndex = i;
292  break;
293  }
294  }
295 
296  std::array<int,3> indices{{closestIndex,nextIndex,nextNextIndex}};
297  return indices;
298 }
299 
300 
301 
302 
303 float
305  const TrajectoryStateOnSurface& tsos,
306  const MeasurementEstimator& est) const
307 {
308  const Plane& startPlane = det->surface();
310  est.maximalLocalDisplacement( tsos, startPlane);
311  return maxDistance.y();
312 }
313 
314 
315 int
316 TIDLayer::findClosest(const GlobalPoint ringCrossing[3] ) const
317 {
318  int theBin = 0;
319  float initialR = ringPars[0].theRingR;
320  float rDiff = std::abs( ringCrossing[0].perp() - initialR);
321  for (int i = 1; i < 3 ; i++){
322  float ringR = ringPars[i].theRingR;
323  float testDiff = std::abs( ringCrossing[i].perp() - ringR);
324  if ( testDiff<rDiff ) {
325  rDiff = testDiff;
326  theBin = i;
327  }
328  }
329  return theBin;
330 }
331 
332 int
333 TIDLayer::findNextIndex(const GlobalPoint ringCrossing[3], int closest ) const
334 {
335 
336  int firstIndexToCheck = (closest != 0)? 0 : 1;
337  float initialR = ringPars[firstIndexToCheck].theRingR;
338  float rDiff = std::abs( ringCrossing[firstIndexToCheck].perp() - initialR);
339  int theBin = firstIndexToCheck;
340  for (int i = firstIndexToCheck+1; i < 3 ; i++){
341  if ( i != closest) {
342  float ringR = ringPars[i].theRingR;
343  float testDiff = std::abs( ringCrossing[i].perp() - ringR);
344  if ( testDiff<rDiff ) {
345  rDiff = testDiff;
346  theBin = i;
347  }
348  }
349  }
350  return theBin;
351 }
352 
353 
354 
355 bool
356 TIDLayer::overlapInR( const TrajectoryStateOnSurface& tsos, int index, double ymax ) const
357 {
358  // assume "fixed theta window", i.e. margin in local y = r is changing linearly with z
359  float tsRadius = tsos.globalPosition().perp();
360  float thetamin = ( max(0.,tsRadius-ymax))/(std::abs(tsos.globalPosition().z())+10.f); // add 10 cm contingency
361  float thetamax = ( tsRadius + ymax)/(std::abs(tsos.globalPosition().z())-10.f);
362 
363  // do the theta regions overlap ?
364 
365  return !( thetamin > ringPars[index].thetaRingMax || ringPars[index].thetaRingMin > thetamax);
366 }
367 
368 
#define LogDebug(id)
TkRotation< Scalar > RotationType
Definition: Definitions.h:29
Common base class.
int i
Definition: DBlmapReader.cc:9
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 PropagationDirection propagationDirection() const
Definition: Propagator.h:155
double zPos
Definition: DDAxes.h:10
Global3DPoint GlobalPoint
Definition: GlobalPoint.h:10
std::array< int, 3 > ringIndicesByCrossingProximity(const TrajectoryStateOnSurface &startingState, const Propagator &prop) const
Definition: TIDLayer.cc:252
GlobalPoint globalPosition() const
Vector2DBase< float, LocalTag > Local2DVector
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
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
tuple result
Definition: query.py:137
TIDLayer(std::vector< const TIDRing * > &rings) __attribute__((cold))
Definition: TIDLayer.cc:123
def move
Definition: eostools.py:508
~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
virtual Local2DVector maximalLocalDisplacement(const TrajectoryStateOnSurface &ts, const Plane &plane) const
bool overlapInR(const TrajectoryStateOnSurface &tsos, int i, double ymax) const __attribute__((hot))
Definition: TIDLayer.cc:356
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:304
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:316
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:333
void groupedCompatibleDetsV(const TrajectoryStateOnSurface &tsos, const Propagator &prop, const MeasurementEstimator &est, std::vector< DetGroup > &result) const __attribute__((hot))
Definition: TIDLayer.cc:192