CMS 3D CMS Logo

CaloSubdetectorGeometry.cc
Go to the documentation of this file.
3 
4 #include <Math/Transform3D.h>
5 #include <Math/EulerAngles.h>
6 
7 #include <algorithm>
8 
12 
14 
16  m_parMgr ( nullptr ) ,
17  m_cmgr ( nullptr ) ,
18  m_deltaPhi (nullptr) ,
19  m_deltaEta (nullptr)
20 {}
21 
22 
24 {
25  delete m_cmgr ;
26  delete m_parMgr ;
27  if (m_deltaPhi) delete m_deltaPhi.load() ;
28  if (m_deltaEta) delete m_deltaEta.load() ;
29 }
30 
31 void
33 {
34  auto pos = std::lower_bound(m_validIds.begin(), m_validIds.end(), id);
35  m_validIds.insert(pos, id);
36 }
37 
38 const std::vector<DetId>&
40  int /*subdet*/ ) const
41 {
42  return m_validIds ;
43 }
44 
45 std::shared_ptr<const CaloCellGeometry>
47 {
48  return cellGeomPtr(CaloGenericDetId(id).denseIndex());
49 }
50 
51 bool
53  return std::find(m_validIds.begin(),m_validIds.end(),id)!=m_validIds.end();
54 }
55 
56 DetId
58  const CCGFloat eta ( r.eta() ) ;
59  const CCGFloat phi ( r.phi() ) ;
60  uint32_t index ( ~0 ) ;
61  CCGFloat closest ( 1e9 ) ;
62 
63  for( uint32_t i ( 0 ); i != m_validIds.size() ; ++i ) {
64  std::shared_ptr<const CaloCellGeometry> cell ( getGeometry( m_validIds[ i ] ) ) ;
65  if( nullptr != cell ) {
66  const GlobalPoint& p ( cell->getPosition() ) ;
67  const CCGFloat eta0 ( p.eta() ) ;
68  const CCGFloat phi0 ( p.phi() ) ;
69  const CCGFloat dR2 ( reco::deltaR2( eta0, phi0, eta, phi ) ) ;
70  if( dR2 < closest ) {
71  closest = dR2 ;
72  index = i ;
73  }
74  }
75  }
76  return ( closest > 0.9e9 ||
77  (uint32_t)(~0) == index ? DetId(0) :
78  m_validIds[index] ) ;
79 }
80 
83  const double dR2 ( dR*dR ) ;
84  const double eta ( r.eta() ) ;
85  const double phi ( r.phi() ) ;
86 
87  DetIdSet dss;
88 
89  if( 0.000001 < dR )
90  {
91  for( uint32_t i ( 0 ); i != m_validIds.size() ; ++i )
92  {
93  std::shared_ptr<const CaloCellGeometry> cell ( getGeometry( m_validIds[i] ) ) ;
94  if( nullptr != cell )
95  {
96  const GlobalPoint& p ( cell->getPosition() ) ;
97  const CCGFloat eta0 ( p.eta() ) ;
98  if( fabs( eta - eta0 ) < dR )
99  {
100  const CCGFloat phi0 ( p.phi() ) ;
101  CCGFloat delp ( fabs( phi - phi0 ) ) ;
102  if( delp > M_PI ) delp = 2*M_PI - delp ;
103  if( delp < dR )
104  {
105  const CCGFloat dist2 ( reco::deltaR2( eta0, phi0, eta, phi ) ) ;
106  if( dist2 < dR2 ) dss.insert( m_validIds[i] ) ;
107  }
108  }
109  }
110  }
111  }
112  return dss;
113 }
114 
117  // stupid implementation not to be really used...
118  DetIdSet ids = getCells(r, dR);
119  CellSet cells; cells.reserve(ids.size());
120  for ( auto id : ids) cells.emplace_back(getGeometry(id));
121  return cells;
122 }
123 
124 void
126 {
127  assert( nullptr == m_cmgr ) ;
130 
131  m_validIds.reserve( n ) ;
132 }
133 
134 void
136  unsigned int m )
137 {
138  assert( nullptr == m_parMgr ) ;
139  m_parMgr = new ParMgr( n*m, m ) ;
140 }
141 
142 void
146  CaloSubdetectorGeometry::IVec& /*dins*/) const {
147  tVec.reserve( m_validIds.size()*numberOfTransformParms() ) ;
148  iVec.reserve( numberOfShapes()==1 ? 1 : m_validIds.size() ) ;
149  dVec.reserve( numberOfShapes()*numberOfParametersPerShape() ) ;
150 
151  for(const auto & pv : parVecVec()) {
152  for(float iv : pv) {
153  dVec.emplace_back( iv ) ;
154  }
155  }
156 
157  for( uint32_t i ( 0 ) ; i != m_validIds.size() ; ++i ) {
158  Tr3D tr ;
159  std::shared_ptr<const CaloCellGeometry> ptr ( cellGeomPtr( i ) ) ;
160  assert( nullptr != ptr ) ;
161  ptr->getTransform( tr, ( Pt3DVec* ) nullptr ) ;
162 
163  if( Tr3D() == tr ) {// for preshower there is no rotation
164  const GlobalPoint& gp ( ptr->getPosition() ) ;
165  tr = HepGeom::Translate3D( gp.x(), gp.y(), gp.z() ) ;
166  }
167 
168  const CLHEP::Hep3Vector tt ( tr.getTranslation() ) ;
169  tVec.emplace_back( tt.x() ) ;
170  tVec.emplace_back( tt.y() ) ;
171  tVec.emplace_back( tt.z() ) ;
172  if( 6 == numberOfTransformParms() ) {
173  const CLHEP::HepRotation rr ( tr.getRotation() ) ;
174  const ROOT::Math::Transform3D rtr ( rr.xx(), rr.xy(), rr.xz(), tt.x(),
175  rr.yx(), rr.yy(), rr.yz(), tt.y(),
176  rr.zx(), rr.zy(), rr.zz(), tt.z() ) ;
178  rtr.GetRotation( ea ) ;
179  tVec.emplace_back( ea.Phi() ) ;
180  tVec.emplace_back( ea.Theta() ) ;
181  tVec.emplace_back( ea.Psi() ) ;
182  }
183 
184  const CCGFloat* par ( ptr->param() ) ;
185 
186  unsigned int ishape ( 9999 ) ;
187  for( unsigned int ivv ( 0 ) ; ivv != parVecVec().size() ; ++ivv ) {
188  bool ok ( true ) ;
189  const CCGFloat* pv ( &(*parVecVec()[ivv].begin() ) ) ;
190  for( unsigned int k ( 0 ) ; k != numberOfParametersPerShape() ; ++k ) {
191  ok = ok && ( fabs( par[k] - pv[k] ) < 1.e-6 ) ;
192  }
193  if( ok ) {
194  ishape = ivv ;
195  break ;
196  }
197  }
198  assert( 9999 != ishape ) ;
199 
200  const unsigned int nn (( numberOfShapes()==1) ? (unsigned int)1 : m_validIds.size() ) ;
201  if( iVec.size() < nn ) iVec.emplace_back( ishape ) ;
202  }
203 }
204 
206  const CaloGenericDetId cgId ( detId ) ;
207 
208  if(!m_deltaPhi.load(std::memory_order_acquire)) {
209  const uint32_t kSize ( sizeForDenseIndex(detId));
210  auto ptr = new std::vector<CCGFloat>(kSize);
211  for( uint32_t i ( 0 ) ; i != kSize ; ++i ) {
212  std::shared_ptr<const CaloCellGeometry> cellPtr ( cellGeomPtr( i ) ) ;
213  if( nullptr != cellPtr ) {
214  CCGFloat dPhi1 (fabs(
215  GlobalPoint((cellPtr->getCorners()[0].x() +
216  cellPtr->getCorners()[1].x() )/2.,
217  (cellPtr->getCorners()[0].y() +
218  cellPtr->getCorners()[1].y() )/2.,
219  (cellPtr->getCorners()[0].z() +
220  cellPtr->getCorners()[1].z() )/2.).phi() -
221  GlobalPoint((cellPtr->getCorners()[2].x() +
222  cellPtr->getCorners()[3].x() )/2.,
223  (cellPtr->getCorners()[2].y() +
224  cellPtr->getCorners()[3].y() )/2.,
225  (cellPtr->getCorners()[2].z() +
226  cellPtr->getCorners()[3].z() )/2.).phi() ) ) ;
227  CCGFloat dPhi2 (fabs(
228  GlobalPoint((cellPtr->getCorners()[0].x() +
229  cellPtr->getCorners()[3].x() )/2.,
230  (cellPtr->getCorners()[0].y() +
231  cellPtr->getCorners()[3].y() )/2.,
232  (cellPtr->getCorners()[0].z() +
233  cellPtr->getCorners()[3].z() )/2. ).phi() -
234  GlobalPoint((cellPtr->getCorners()[2].x() +
235  cellPtr->getCorners()[1].x() )/2.,
236  (cellPtr->getCorners()[2].y() +
237  cellPtr->getCorners()[1].y() )/2.,
238  (cellPtr->getCorners()[2].z() +
239  cellPtr->getCorners()[1].z() )/2.).phi() ) ) ;
240  if( M_PI < dPhi1 ) dPhi1 = fabs( dPhi1 - 2.*M_PI ) ;
241  if( M_PI < dPhi2 ) dPhi2 = fabs( dPhi2 - 2.*M_PI ) ;
242  (*ptr)[i] = dPhi1>dPhi2 ? dPhi1 : dPhi2 ;
243  }
244  }
245  std::vector<CCGFloat>* expect = nullptr;
246  bool exchanged = m_deltaPhi.compare_exchange_strong(expect, ptr, std::memory_order_acq_rel);
247  if (!exchanged) delete ptr;
248  }
249  return (*m_deltaPhi.load(std::memory_order_acquire))[ indexFor(detId) ] ;
250 }
251 
252 CCGFloat
254 
255  if(!m_deltaEta.load(std::memory_order_acquire)) {
256  const uint32_t kSize ( sizeForDenseIndex(detId));
257  auto ptr = new std::vector<CCGFloat> ( kSize ) ;
258  for( uint32_t i ( 0 ) ; i != kSize ; ++i ) {
259  std::shared_ptr<const CaloCellGeometry> cellPtr ( cellGeomPtr( i ) ) ;
260  if( nullptr != cellPtr ) {
261  const CCGFloat dEta1(fabs(
262  GlobalPoint((cellPtr->getCorners()[0].x() +
263  cellPtr->getCorners()[1].x())/2.,
264  (cellPtr->getCorners()[0].y() +
265  cellPtr->getCorners()[1].y())/2.,
266  (cellPtr->getCorners()[0].z() +
267  cellPtr->getCorners()[1].z())/2.).eta() -
268  GlobalPoint((cellPtr->getCorners()[2].x() +
269  cellPtr->getCorners()[3].x())/2.,
270  (cellPtr->getCorners()[2].y() +
271  cellPtr->getCorners()[3].y())/2. ,
272  (cellPtr->getCorners()[2].z() +
273  cellPtr->getCorners()[3].z())/2.).eta() ) ) ;
274  const CCGFloat dEta2(fabs(
275  GlobalPoint((cellPtr->getCorners()[0].x() +
276  cellPtr->getCorners()[3].x())/2.,
277  (cellPtr->getCorners()[0].y() +
278  cellPtr->getCorners()[3].y())/2.,
279  (cellPtr->getCorners()[0].z() +
280  cellPtr->getCorners()[3].z())/2.).eta() -
281  GlobalPoint((cellPtr->getCorners()[2].x() +
282  cellPtr->getCorners()[1].x())/2.,
283  (cellPtr->getCorners()[2].y() +
284  cellPtr->getCorners()[1].y())/2.,
285  (cellPtr->getCorners()[2].z() +
286  cellPtr->getCorners()[1].z())/2.).eta() ) ) ;
287  (*ptr)[i] = dEta1>dEta2 ? dEta1 : dEta2 ;
288  }
289  }
290  std::vector<CCGFloat>* expect = nullptr;
291  bool exchanged = m_deltaEta.compare_exchange_strong(expect, ptr, std::memory_order_acq_rel);
292  if (!exchanged) delete ptr;
293  }
294  return (*m_deltaEta.load(std::memory_order_acquire))[ indexFor(detId)];
295 }
296 
297 
298 unsigned int CaloSubdetectorGeometry::indexFor(const DetId& id) const { return CaloGenericDetId(id).denseIndex(); }
299 
301 
302 std::shared_ptr<const CaloCellGeometry>
304  // Default version
305  auto ptr = getGeometryRawPtr(index);
306  static const auto do_not_delete = [](const void*){};
307  return ptr == nullptr ? nullptr : std::shared_ptr<const CaloCellGeometry>(ptr, do_not_delete);
308 }
virtual const CaloCellGeometry * getGeometryRawPtr(uint32_t index) const =0
CaloCellGeometry::CornersMgr * m_cmgr
std::vector< CCGFloat > DimVec
virtual unsigned int numberOfParametersPerShape() const
CCGFloat deltaPhi(const DetId &detId) const
Geom::Phi< T > phi() const
Definition: PV3DBase.h:69
MgrType::size_type size_type
Definition: EZArrayFL.h:30
Global3DPoint GlobalPoint
Definition: GlobalPoint.h:10
std::vector< unsigned int > IVec
std::vector< CCGFloat > TrVec
CaloCellGeometry::Tr3D Tr3D
CaloCellGeometry::Pt3DVec Pt3DVec
HepGeom::Transform3D Tr3D
uint32_t sizeForDenseIndexing() const
std::vector< Pt3D > Pt3DVec
#define nullptr
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:20
CCGFloat deltaEta(const DetId &detId) const
CaloCellGeometry::CCGFloat CCGFloat
EZMgrFL< GlobalPoint > CornersMgr
void allocatePar(ParVec::size_type n, unsigned int m)
virtual CellSet getCellSet(const GlobalPoint &r, double dR) const
virtual const std::vector< DetId > & getValidDetIds(DetId::Detector det=DetId::Detector(0), int subdet=0) const
Get a list of valid detector ids (for the given subdetector)
std::vector< std::shared_ptr< const CaloCellGeometry > > CellSet
static constexpr unsigned int k_cornerSize
virtual void getSummary(TrVec &trVector, IVec &iVector, DimVec &dimVector, IVec &dinsVector) const
virtual bool present(const DetId &id) const
is this detid present in the geometry?
virtual std::shared_ptr< const CaloCellGeometry > cellGeomPtr(uint32_t index) const
virtual DetIdSet getCells(const GlobalPoint &r, double dR) const
Get a list of all cells within a dR of the given cell.
virtual unsigned int numberOfShapes() const
std::vector< DetId > m_validIds
std::atomic< std::vector< CCGFloat > * > m_deltaPhi
def pv(vc)
Definition: MetAnalyzer.py:6
CaloSubdetectorGeometry::CCGFloat CCGFloat
uint32_t denseIndex() const
#define M_PI
int k[5][pyjets_maxn]
virtual DetId getClosestCell(const GlobalPoint &r) const
Definition: DetId.h:18
AlgebraicVector EulerAngles
Definition: Definitions.h:36
void addValidID(const DetId &id)
virtual std::shared_ptr< const CaloCellGeometry > getGeometry(const DetId &id) const
Get the cell geometry of a given detector id. Should return false if not found.
Detector
Definition: DetId.h:24
CaloCellGeometry::Pt3DVec Pt3DVec
HepGeom::Point3D< CCGFloat > Pt3D
CaloCellGeometry::Pt3D Pt3D
CaloCellGeometry::ParMgr ParMgr
T1 deltaR2(T1 eta1, T2 phi1, T3 eta2, T4 phi2)
Definition: deltaR.h:36
CaloCellGeometry::Tr3D Tr3D
T eta() const
Definition: PV3DBase.h:76
virtual unsigned int indexFor(const DetId &id) const
virtual ~CaloSubdetectorGeometry()
The base class DOES assume that it owns the CaloCellGeometry objects.
#define begin
Definition: vmac.h:32
void allocateCorners(CaloCellGeometry::CornersVec::size_type n)
std::atomic< std::vector< CCGFloat > * > m_deltaEta
virtual unsigned int numberOfTransformParms() const
virtual unsigned int sizeForDenseIndex(const DetId &id) const