CMS 3D CMS Logo

PositionCalc.h
Go to the documentation of this file.
1 #ifndef RecoEcal_EgammaCoreTools_PositionCalc_h
2 #define RecoEcal_EgammaCoreTools_PositionCalc_h
3 
14 #include <vector>
15 #include <map>
16 
28 
30 {
31  public:
32  typedef std::vector< std::pair<DetId,float> > HitsAndFractions;
33  typedef std::vector< std::pair<DetId,double> > HitsAndEnergies;
34  // You must call Initialize before you can calculate positions or
35  // covariances.
36 
37  PositionCalc(const edm::ParameterSet& par);
38  PositionCalc() { };
39 
40  const PositionCalc& operator=(const PositionCalc& rhs);
41 
42  // Calculate_Location calculates an arithmetically or logarithmically
43  // weighted average position of a vector of DetIds, which should be
44  // a subset of the map used to Initialize.
45 
46  template<typename HitType>
47  math::XYZPoint Calculate_Location( const HitsAndFractions& iDetIds ,
48  const edm::SortedCollection<HitType>* iRecHits ,
49  const CaloSubdetectorGeometry* iSubGeom ,
50  const CaloSubdetectorGeometry* iESGeom = nullptr ) ;
51 
52  private:
57  double param_W0_;
58  double param_X0_;
59 
61  bool m_esPlus ;
62  bool m_esMinus ;
63 
64 };
65 
66 template<typename HitType>
69  const edm::SortedCollection<HitType>* iRecHits ,
70  const CaloSubdetectorGeometry* iSubGeom ,
71  const CaloSubdetectorGeometry* iESGeom ) {
72  typedef edm::SortedCollection<HitType> HitTypeCollection;
73  math::XYZPoint returnValue ( 0, 0, 0 ) ;
74 
75  // Throw an error if the cluster was not initialized properly
76 
77  if( nullptr == iRecHits || nullptr == iSubGeom ) {
78  throw cms::Exception("PositionCalc")
79  << "Calculate_Location() called uninitialized or wrong initialization.";
80  }
81 
82  if( !iDetIds.empty() &&
83  !iRecHits->empty() ) {
84 
85  HitsAndEnergies detIds;
86  detIds.reserve( iDetIds.size() ) ;
87 
88  double eTot ( 0 ) ;
89  double eMax ( 0 ) ;
90  DetId maxId ;
91 
92  // Check that DetIds are nonzero
93  typename HitTypeCollection::const_iterator endRecHits ( iRecHits->end() ) ;
94  HitsAndFractions::const_iterator n, endDiDs( iDetIds.end() );
95  for( n = iDetIds.begin(); n != endDiDs ; ++n ) {
96  const DetId dId ( (*n).first ) ;
97  const float frac( (*n).second) ;
98  if( !dId.null() ) {
99  typename HitTypeCollection::const_iterator iHit ( iRecHits->find( dId ) ) ;
100  if( iHit != endRecHits ) {
101  const double energy ( iHit->energy() *frac ) ;
102  detIds.push_back( std::make_pair(dId,energy) );
103  if( 0.0 < energy ) { // only save positive energies
104  if( eMax < energy ) {
105  eMax = energy ;
106  maxId = dId ;
107  }
108  eTot += energy ;
109  }
110  }
111  }
112  }
113 
114  if( 0.0 >= eTot ) {
115  LogDebug("ZeroClusterEnergy") << "cluster with 0 energy: "
116  << eTot << " size: " << detIds.size()
117  << " , returning (0,0,0)";
118  } else {
119  // first time or when es geom changes set flags
120  if( nullptr != iESGeom && m_esGeom != iESGeom ) {
121  m_esGeom = iESGeom ;
122  for( uint32_t ic ( 0 ) ;
123  ( ic != m_esGeom->getValidDetIds().size() ) &&
124  ( (!m_esPlus) || (!m_esMinus) ) ; ++ic ) {
125  const double z ( m_esGeom->getGeometry( m_esGeom->getValidDetIds()[ic] )->getPosition().z() ) ;
126  m_esPlus = m_esPlus || ( 0 < z ) ;
127  m_esMinus = m_esMinus || ( 0 > z ) ;
128  }
129  }
130 
131  //Select the correct value of the T0 parameter depending on subdetector
132  auto center_cell ( iSubGeom->getGeometry( maxId ) ) ;
133  const double ctreta (center_cell->getPosition().eta());
134 
135  // for barrel, use barrel T0;
136  // for endcap: if preshower present && in preshower fiducial,
137  // use preshower T0
138  // else use endcap only T0
139  const double preshowerStartEta = 1.653;
140  const int subdet = maxId.subdetId();
141  const double T0 ( subdet == EcalBarrel ? param_T0_barl_ :
142  ( ( ( preshowerStartEta < fabs( ctreta ) ) &&
143  ( ( ( 0 < ctreta ) &&
144  m_esPlus ) ||
145  ( ( 0 > ctreta ) &&
146  m_esMinus ) ) ) ?
148 
149  // Calculate shower depth
150  const float maxDepth ( param_X0_ * ( T0 + log( eTot ) ) ) ;
151  const float maxToFront ( center_cell->getPosition().mag() ) ; // to front face
152 
153  // Loop over hits and get weights
154  double total_weight = 0;
155  const double eTot_inv = 1.0/eTot;
156  const double logETot_inv = ( param_LogWeighted_ ? log(eTot_inv) : 0 );
157 
158  double xw ( 0 ) ;
159  double yw ( 0 ) ;
160  double zw ( 0 ) ;
161 
162 
163  HitsAndEnergies::const_iterator j, hAndE_end = detIds.end();
164  for( j = detIds.begin() ; j != hAndE_end ; ++j ) {
165  const DetId dId ( (*j).first ) ;
166  const double e_j( (*j).second ) ;
167 
168  double weight = 0;
169  if ( param_LogWeighted_ ) {
170  if ( e_j > 0.0 ) {
171  weight = std::max( 0., param_W0_ + log(e_j) + logETot_inv );
172  } else {
173  weight = 0;
174  }
175  } else {
176  weight = e_j*eTot_inv;
177  }
178 
179  auto cell ( iSubGeom->getGeometry( dId ) ) ;
180  const float depth ( maxDepth + maxToFront - cell->getPosition().mag() ) ;
181 
182  const GlobalPoint pos (cell->getPosition( depth ) );
183 
184  xw += weight*pos.x() ;
185  yw += weight*pos.y() ;
186  zw += weight*pos.z() ;
187 
188  total_weight += weight ;
189  }
190  returnValue = math::XYZPoint( xw/total_weight,
191  yw/total_weight,
192  zw/total_weight ) ;
193  }
194  }
195  return returnValue ;
196 }
197 
198 #endif
#define LogDebug(id)
double param_T0_endc_
Definition: PositionCalc.h:55
double param_W0_
Definition: PositionCalc.h:57
constexpr bool null() const
is this a null id ?
Definition: DetId.h:49
Definition: weight.py:1
double param_X0_
Definition: PositionCalc.h:58
const PositionCalc & operator=(const PositionCalc &rhs)
Definition: PositionCalc.cc:17
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)
bool param_LogWeighted_
Definition: PositionCalc.h:53
double param_T0_endcPresh_
Definition: PositionCalc.h:56
constexpr int subdetId() const
get the contents of the subdetector field (not cast into any detector&#39;s numbering enum) ...
Definition: DetId.h:41
std::vector< std::pair< DetId, double > > HitsAndEnergies
Definition: PositionCalc.h:33
const_iterator end() const
Definition: DetId.h:18
std::vector< std::pair< DetId, float > > HitsAndFractions
Definition: PositionCalc.h:32
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.
XYZPointD XYZPoint
point in space with cartesian internal representation
Definition: Point3D.h:12
double param_T0_barl_
Definition: PositionCalc.h:54
iterator find(key_type k)
math::XYZPoint Calculate_Location(const HitsAndFractions &iDetIds, const edm::SortedCollection< HitType > *iRecHits, const CaloSubdetectorGeometry *iSubGeom, const CaloSubdetectorGeometry *iESGeom=0)
Definition: PositionCalc.h:68
auto zw(V v) -> Vec2< typename std::remove_reference< decltype(v[0])>::type >
Definition: ExtVec.h:75
const CaloSubdetectorGeometry * m_esGeom
Definition: PositionCalc.h:60