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  static std::vector<const GeometricSearchDet*> crap;
96  for ( auto c: theComps) crap.push_back(c);
97  return crap;
98  }
99 
100 
101 void
102 TIDLayer::fillRingPars(int i) {
103  const BoundDisk& ringDisk = static_cast<const BoundDisk&>(theComps[i]->surface());
104  float ringMinZ = fabs( ringDisk.position().z()) - ringDisk.bounds().thickness()/2.;
105  float ringMaxZ = fabs( ringDisk.position().z()) + ringDisk.bounds().thickness()/2.;
106  ringPars[i].thetaRingMin = ringDisk.innerRadius()/ ringMaxZ;
107  ringPars[i].thetaRingMax = ringDisk.outerRadius()/ ringMinZ;
108  ringPars[i].theRingR=( ringDisk.innerRadius() +
109  ringDisk.outerRadius())/2.;
110 
111 }
112 
113 
114 TIDLayer::TIDLayer(vector<const TIDRing*>& rings) {
115  //They should be already R-ordered. TO BE CHECKED!!
116  //sort( theRings.begin(), theRings.end(), DetLessR());
117 
118  if ( rings.size() != 3) throw DetLayerException("Number of rings in TID layer is not equal to 3 !!");
119  setSurface( computeDisk( rings ) );
120 
121  for(int i=0; i!=3; ++i) {
122  theComps[i]=rings[i];
123  fillRingPars(i);
124  theBasicComps.insert(theBasicComps.end(),
125  (*rings[i]).basicComponents().begin(),
126  (*rings[i]).basicComponents().end());
127  }
128 
129 
130  LogDebug("TkDetLayers") << "==== DEBUG TIDLayer =====" ;
131  LogDebug("TkDetLayers") << "r,zed pos , thickness, innerR, outerR: "
132  << this->position().perp() << " , "
133  << this->position().z() << " , "
134  << this->specificSurface().bounds().thickness() << " , "
135  << this->specificSurface().innerRadius() << " , "
136  << this->specificSurface().outerRadius() ;
137 }
138 
139 
140 BoundDisk*
141 TIDLayer::computeDisk( const vector<const TIDRing*>& rings) const
142 {
143  float theRmin = rings.front()->specificSurface().innerRadius();
144  float theRmax = rings.front()->specificSurface().outerRadius();
145  float theZmin = rings.front()->position().z() -
146  rings.front()->surface().bounds().thickness()/2;
147  float theZmax = rings.front()->position().z() +
148  rings.front()->surface().bounds().thickness()/2;
149 
150  for (vector<const TIDRing*>::const_iterator i = rings.begin(); i != rings.end(); i++) {
151  float rmin = (**i).specificSurface().innerRadius();
152  float rmax = (**i).specificSurface().outerRadius();
153  float zmin = (**i).position().z() - (**i).surface().bounds().thickness()/2.;
154  float zmax = (**i).position().z() + (**i).surface().bounds().thickness()/2.;
155  theRmin = min( theRmin, rmin);
156  theRmax = max( theRmax, rmax);
157  theZmin = min( theZmin, zmin);
158  theZmax = max( theZmax, zmax);
159  }
160 
161  float zPos = (theZmax+theZmin)/2.;
162  PositionType pos(0.,0.,zPos);
164 
165  return new BoundDisk( pos, rot, new SimpleDiskBounds(theRmin, theRmax,
166  theZmin-zPos, theZmax-zPos));
167 
168 }
169 
170 
171 TIDLayer::~TIDLayer(){
172  for (auto c : theComps) delete c;
173 }
174 
175 
176 
177 void
178 TIDLayer::groupedCompatibleDetsV( const TrajectoryStateOnSurface& startingState,
179  const Propagator& prop,
180  const MeasurementEstimator& est,
181  std::vector<DetGroup> & result) const
182 {
183  std::array<int,3> const & ringIndices = ringIndicesByCrossingProximity(startingState,prop);
184  if ( ringIndices[0]==-1 ) {
185  edm::LogError("TkDetLayers") << "TkRingedForwardLayer::groupedCompatibleDets : error in CrossingProximity";
186  return;
187  }
188 
189  std::array<vector<DetGroup>,3> groupsAtRingLevel;
190  //order is ring3,ring1,ring2 i.e. 2 0 1
191  // 0 1 2
192  constexpr int ringOrder[3]{1,2,0};
193  auto index = [&ringIndices,& ringOrder](int i) { return ringOrder[ringIndices[i]];};
194 
195  auto & closestResult = groupsAtRingLevel[index(0)];
196  theComps[ringIndices[0]]->groupedCompatibleDetsV( startingState, prop, est, closestResult);
197  if ( closestResult.empty() ){
198  theComps[ringIndices[1]]->groupedCompatibleDetsV( startingState, prop, est, result);
199  return;
200  }
201 
202  DetGroupElement closestGel( closestResult.front().front());
203  float rWindow = computeWindowSize( closestGel.det(), closestGel.trajectoryState(), est);
204 
205  if(!overlapInR(closestGel.trajectoryState(),ringIndices[1],rWindow)) {
206  result.swap(closestResult);
207  return;
208  }
209 
210 
211  auto & nextResult = groupsAtRingLevel[index(1)];
212  theComps[ringIndices[1]]->groupedCompatibleDetsV( startingState, prop, est, nextResult);
213  if(nextResult.empty()) {
214  result.swap(closestResult);
215  return;
216  }
217 
218  if(!overlapInR(closestGel.trajectoryState(),ringIndices[2],rWindow) ) {
219  //then merge 2 levels & return
220  orderAndMergeLevels(closestGel.trajectoryState(),prop,groupsAtRingLevel,result);
221  return;
222  }
223 
224  auto & nextNextResult = groupsAtRingLevel[index(2)];
225  theComps[ringIndices[2]]->groupedCompatibleDetsV( startingState, prop, est, nextNextResult);
226  if(nextNextResult.empty()) {
227  // then merge 2 levels and return
228  orderAndMergeLevels(closestGel.trajectoryState(),prop,groupsAtRingLevel,result); //
229  return;
230  }
231 
232  // merge 3 level and return merged
233  orderAndMergeLevels(closestGel.trajectoryState(),prop,groupsAtRingLevel, result);
234 }
235 
236 
237 std::array<int,3>
238 TIDLayer::ringIndicesByCrossingProximity(const TrajectoryStateOnSurface& startingState,
239  const Propagator& prop ) const
240 {
241  typedef HelixForwardPlaneCrossing Crossing;
243 
244  HelixPlaneCrossing::PositionType startPos( startingState.globalPosition());
245  HelixPlaneCrossing::DirectionType startDir( startingState.globalMomentum());
247  float rho( startingState.transverseCurvature());
248 
249  // calculate the crossings with the ring surfaces
250  // rings are assumed to be sorted in R !
251 
252  Crossing myXing( startPos, startDir, rho, propDir );
253 
254  GlobalPoint ringCrossings[3];
255  // vector<GlobalVector> ringXDirections;
256 
257  for (int i = 0; i < 3 ; i++ ) {
258  const BoundDisk & theRing = static_cast<const BoundDisk &>(theComps[i]->surface());
259  pair<bool,double> pathlen = myXing.pathLength( theRing);
260  if ( pathlen.first ) {
261  ringCrossings[i] = GlobalPoint( myXing.position(pathlen.second ));
262  // ringXDirections.push_back( GlobalVector( myXing.direction(pathlen.second )));
263  } else {
264  // TO FIX.... perhaps there is something smarter to do
265  //throw DetLayerException("trajectory doesn't cross TID rings");
266  ringCrossings[i] = GlobalPoint( 0.,0.,0.);
267  // ringXDirections.push_back( GlobalVector( 0.,0.,0.));
268  }
269  }
270 
271  int closestIndex = findClosest(ringCrossings);
272  int nextIndex = findNextIndex(ringCrossings,closestIndex);
273  if ( closestIndex<0 || nextIndex<0 ) return std::array<int,3>{{-1,-1,-1}};
274  int nextNextIndex = -1;
275  for(int i=0; i<3 ; i++){
276  if(i!= closestIndex && i!=nextIndex) {
277  nextNextIndex = i;
278  break;
279  }
280  }
281 
282  std::array<int,3> indices{{closestIndex,nextIndex,nextNextIndex}};
283  return indices;
284 }
285 
286 
287 
288 
289 float
291  const TrajectoryStateOnSurface& tsos,
292  const MeasurementEstimator& est) const
293 {
294  const Plane& startPlane = det->surface();
296  est.maximalLocalDisplacement( tsos, startPlane);
297  return maxDistance.y();
298 }
299 
300 
301 int
302 TIDLayer::findClosest(const GlobalPoint ringCrossing[3] ) const
303 {
304  int theBin = 0;
305  float initialR = ringPars[0].theRingR;
306  float rDiff = fabs( ringCrossing[0].perp() - initialR);
307  for (int i = 1; i < 3 ; i++){
308  float ringR = ringPars[i].theRingR;
309  float testDiff = fabs( ringCrossing[i].perp() - ringR);
310  if ( testDiff<rDiff ) {
311  rDiff = testDiff;
312  theBin = i;
313  }
314  }
315  return theBin;
316 }
317 
318 int
319 TIDLayer::findNextIndex(const GlobalPoint ringCrossing[3], int closest ) const
320 {
321 
322  int firstIndexToCheck = (closest != 0)? 0 : 1;
323  float initialR = ringPars[firstIndexToCheck].theRingR;
324  float rDiff = fabs( ringCrossing[0].perp() - initialR);
325  int theBin = firstIndexToCheck;
326  for (int i = firstIndexToCheck+1; i < 3 ; i++){
327  if ( i != closest) {
328  float ringR = ringPars[i].theRingR;
329  float testDiff = fabs( ringCrossing[i].perp() - ringR);
330  if ( testDiff<rDiff ) {
331  rDiff = testDiff;
332  theBin = i;
333  }
334  }
335  }
336  return theBin;
337 }
338 
339 
340 
341 bool
342 TIDLayer::overlapInR( const TrajectoryStateOnSurface& tsos, int index, double ymax ) const
343 {
344  // assume "fixed theta window", i.e. margin in local y = r is changing linearly with z
345  float tsRadius = tsos.globalPosition().perp();
346  float thetamin = ( max(0.,tsRadius-ymax))/(fabs(tsos.globalPosition().z())+10.f); // add 10 cm contingency
347  float thetamax = ( tsRadius + ymax)/(fabs(tsos.globalPosition().z())-10.f);
348 
349  // do the theta regions overlap ?
350 
351  return !( thetamin > ringPars[index].thetaRingMax || ringPars[index].thetaRingMin > thetamax);
352 }
353 
354 
#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
T perp() const
Definition: PV3DBase.h:72
double zPos
Definition: DDAxes.h:10
Global3DPoint GlobalPoint
Definition: GlobalPoint.h:10
GlobalPoint globalPosition() const
Vector2DBase< float, LocalTag > Local2DVector
PropagationDirection
const Plane & surface() const
The nominal surface of the GeomDet.
Definition: GeomDet.h:35
Definition: Plane.h:17
static int position[TOTALCHAMBERS][3]
Definition: ReadPGInfo.cc:509
virtual PropagationDirection propagationDirection() const GCC11_FINAL
Definition: Propagator.h:145
float computeWindowSize(const GeomDet *det, const TrajectoryStateOnSurface &tsos, const MeasurementEstimator &est)
Definition: TkDetUtil.cc:31
Point3DBase< Scalar, GlobalTag > PositionType
Definition: Definitions.h:30
const T & max(const T &a, const T &b)
T z() const
Definition: PV3DBase.h:64
tuple result
Definition: query.py:137
double f[11][100]
Definition: Merger.h:31
std::pair< const GeomDet *, TrajectoryStateOnSurface > DetWithState
virtual Local2DVector maximalLocalDisplacement(const TrajectoryStateOnSurface &ts, const Plane &plane) const
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
std::pair< const GeomDet *, TrajectoryStateOnSurface > DetWithState
#define constexpr