ATLAS Offline Software
RoiUtil.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3 */
4 //
5 // @file RoiUtil.cxx
6 //
7 // non-member, non friend RoiDescriptor utility functions
8 // to improve encapsulation
9 //
10 // @author M.Sutton
11 //
12 //
13 
14 
17 
18 #include <cmath>
19 
20 namespace {
21  constexpr double M_2PI = 2*M_PI;
22  constexpr float M_PIF = M_PI;
23 
24  constexpr double MAX_R = 1100; // maximum radius of RoI - outer TRT radius ~1070 mm - should be configurable?
25 }
26 
27 
28 namespace RoiUtil {
29 
30 class range_error : public std::exception {
31 public:
32  range_error( const char* s ) : std::exception(), m_str(s) { }
33  virtual const char* what() const throw() { return m_str.c_str(); }
34 private:
35  std::string m_str;
36 };
37 
38 }
39 
40 
42 bool RoiUtil::contains( const IRoiDescriptor& roi, double z0, double dzdr ) {
43  const double zouter = dzdr*MAX_R + z0;
44 
45  auto contains_internal = [zouter, z0]( const IRoiDescriptor& rd ) {
46  return ( z0<=rd.zedPlus() && z0>=rd.zedMinus() &&
47  zouter<=rd.zedOuterPlus() && zouter>=rd.zedOuterMinus() );
48  };
49 
50  if ( roi.composite() ) {
51  for ( const IRoiDescriptor* rd : roi ) {
52  if ( contains_internal( *rd ) ) return true;
53  }
54  return false;
55  }
56  else return contains_internal( roi );
57 }
58 
59 
60 bool RoiUtil::contains_zrange( const IRoiDescriptor& roi, double z0, double dzdr, double zmin, double zmax ) {
61  const double zouter = dzdr*MAX_R + z0;
62 
63  auto contains_internal = [zouter, z0, zmin, zmax]( const IRoiDescriptor& rd ) {
64  return ( z0<=zmax && z0>=zmin &&
65  zouter<=rd.zedOuterPlus() && zouter>=rd.zedOuterMinus() );
66  };
67 
68  if ( roi.composite() ) {
69  for ( const IRoiDescriptor* rd : roi ) {
70  if ( contains_internal( *rd ) ) return true;
71  }
72  return false;
73  }
74  else return contains_internal( roi );
75 }
76 
77 
79 bool RoiUtil::containsPhi( const IRoiDescriptor& roi, double phi ) {
80  if ( roi.composite() ) {
81  for ( const IRoiDescriptor* rd : roi ) {
82  if ( RoiUtil::containsPhi( *rd, phi ) ) return true;
83  }
84  return false;
85  }
86  else {
87  if ( roi.isFullscan() ) return true;
88  if ( roi.phiPlus()>roi.phiMinus() ) return ( phi<roi.phiPlus() && phi>roi.phiMinus() );
89  else return ( phi<roi.phiPlus() || phi>roi.phiMinus() );
90  }
91 }
92 
93 
94 bool RoiUtil::containsZed( const IRoiDescriptor& roi, double z, double r ) {
95  if ( roi.composite() ) {
96  for ( const IRoiDescriptor* rd : roi ) {
97  if ( RoiUtil::containsZed( *rd, z, r ) ) return true;
98  }
99  return false;
100  }
101  else {
102  if ( roi.isFullscan() ) return true;
103  double zminus = r*roi.dzdrMinus() + roi.zedMinus();
104  double zplus = r*roi.dzdrPlus() + roi.zedPlus();
105  return ( z>=zminus && z<=zplus );
106  }
107 }
108 
109 
110 bool RoiUtil::contains( const IRoiDescriptor& roi, double z, double r, double phi ) {
111  if ( roi.composite() ) {
112  for ( const IRoiDescriptor* rd : roi ) {
113  if ( RoiUtil::contains( *rd, z, r, phi ) ) return true;
114  }
115  return false;
116  }
117  else {
118  return ( RoiUtil::containsZed( roi, z, r ) && RoiUtil::containsPhi( roi, phi ) );
119  }
120 }
121 
122 
123 double RoiUtil::phicheck(double phi) {
124  while ( phi> M_PIF ) phi-=M_2PI;
125  while ( phi<-M_PIF ) phi+=M_2PI;
126  if ( !(phi >= -M_PIF && phi <= M_PIF) ) { // use ! of range rather than range to also catch nan etc
127  throw range_error( (std::string("phi out of range: ")+std::to_string(phi)).c_str() );
128  }
129  return phi;
130 }
131 
132 
133 double RoiUtil::etacheck(double eta) {
134  if ( !(eta>-100 && eta<100) ) { // check also for nan
135  throw range_error( (std::string("eta out of range: ")+std::to_string(eta)).c_str() );
136  }
137  return eta;
138 }
139 
140 
141 double RoiUtil::zedcheck(double zed ) {
142  if ( !(zed>-100000 && zed<100000 ) ) { // check also for nan
143  throw range_error( (std::string("zed out of range: ")+std::to_string(zed)).c_str() );
144  }
145  return zed;
146 }
147 
148 
149 bool operator==( const IRoiDescriptor& roi0, const IRoiDescriptor& roi1 ) {
150 
152  if ( &roi0 == &roi1 ) return true;
153 
155  if ( roi0.composite() != roi1.composite() ) return false;
156 
157  if ( !roi0.composite() ) {
159 
161  if ( roi0.isFullscan() != roi1.isFullscan() ) return false;
162  if ( roi0.isFullscan() ) return true;
163 
165  if ( std::fabs(roi0.zed() -roi1.zed() )>1e-7 ) return false;
166  if ( std::fabs(roi0.zedPlus() -roi1.zedPlus() )>1e-7 ) return false;
167  if ( std::fabs(roi0.zedMinus()-roi1.zedMinus())>1e-7 ) return false;
168 
169  if ( std::fabs(roi0.eta() -roi1.eta() )>1e-7 ) return false;
170  if ( std::fabs(roi0.etaPlus() -roi1.etaPlus() )>1e-7 ) return false;
171  if ( std::fabs(roi0.etaMinus()-roi1.etaMinus())>1e-7 ) return false;
172 
175  if ( std::fabs(roi0.phi() -roi1.phi() ) >1e-7 ) return false;
176  if ( std::fabs(roi0.phiPlus() -roi1.phiPlus()) >1e-7 ) return false;
177  if ( std::fabs(roi0.phiMinus()-roi1.phiMinus())>1e-7 ) return false;
178  }
179  else {
181  if ( roi0.size() != roi1.size() ) return false;
182  for ( unsigned i=roi0.size() ; i-- ; ) if ( !( *roi0.at(i) == *roi1.at(i) ) ) return false;
183  }
184 
185  return true;
186 }
187 
188 
189 
190 bool operator!=( const IRoiDescriptor& roi0, const IRoiDescriptor& roi1 ) { return !(roi0==roi1); }
RoiUtil::range_error::m_str
std::string m_str
Definition: RoiUtil.cxx:35
IRoiDescriptor::phi
virtual double phi() const =0
Methods to retrieve data members.
beamspotman.r
def r
Definition: beamspotman.py:676
RoiUtil::phicheck
double phicheck(double phi)
basic range checkers
Definition: RoiUtil.cxx:123
python.SystemOfUnits.s
int s
Definition: SystemOfUnits.py:131
phi
Scalar phi() const
phi method
Definition: AmgMatrixBasePlugin.h:64
eta
Scalar eta() const
pseudorapidity method
Definition: AmgMatrixBasePlugin.h:79
PixelAthClusterMonAlgCfg.zmin
zmin
Definition: PixelAthClusterMonAlgCfg.py:176
IRoiDescriptor::etaMinus
virtual double etaMinus() const =0
RoiUtil::containsPhi
bool containsPhi(const IRoiDescriptor &roi, double phi)
test whether a stub is contained within the roi
Definition: RoiUtil.cxx:79
RoiUtil::containsZed
bool containsZed(const IRoiDescriptor &roi, double z, double r)
Definition: RoiUtil.cxx:94
IRoiDescriptor::size
virtual unsigned size() const =0
number of constituents
M_PI
#define M_PI
Definition: ActiveFraction.h:11
IRoiDescriptor::etaPlus
virtual double etaPlus() const =0
IRoiDescriptor::dzdrMinus
virtual double dzdrMinus() const =0
return the gradients
IRoiDescriptor::dzdrPlus
virtual double dzdrPlus() const =0
IRoiDescriptor::eta
virtual double eta() const =0
lumiFormat.i
int i
Definition: lumiFormat.py:92
z
#define z
IRoiDescriptor
Describes the API of the Region of Ineterest geometry.
Definition: IRoiDescriptor.h:23
RoiUtil::zedcheck
double zedcheck(double zed)
Definition: RoiUtil.cxx:141
calibdata.exception
exception
Definition: calibdata.py:496
PixelAthClusterMonAlgCfg.zmax
zmax
Definition: PixelAthClusterMonAlgCfg.py:176
IRoiDescriptor::phiMinus
virtual double phiMinus() const =0
TRT::Track::z0
@ z0
Definition: InnerDetector/InDetCalibEvent/TRT_CalibData/TRT_CalibData/TrackInfo.h:63
IRoiDescriptor::phiPlus
virtual double phiPlus() const =0
extreme phi values
RoiUtil
Definition: RoiUtil.h:20
operator==
bool operator==(const IRoiDescriptor &roi0, const IRoiDescriptor &roi1)
Definition: RoiUtil.cxx:149
ActsTrk::to_string
std::string to_string(const DetectorType &type)
Definition: GeometryDefs.h:34
RoiUtil.h
RoiUtil::contains_zrange
bool contains_zrange(const IRoiDescriptor &roi, double z0, double dzdr, double zmin, double zmax)
Definition: RoiUtil.cxx:60
RoiUtil::range_error::what
virtual const char * what() const
Definition: RoiUtil.cxx:33
DiTauMassTools::MaxHistStrategyV2::e
e
Definition: PhysicsAnalysis/TauID/DiTauMassTools/DiTauMassTools/HelperFunctions.h:26
IRoiDescriptor::zedPlus
virtual double zedPlus() const =0
the zed and eta values at the most forward and most rear ends of the RoI
IRoiDescriptor::isFullscan
virtual bool isFullscan() const =0
is this a full detector RoI?
IRoiDescriptor::zed
virtual double zed() const =0
IRoiDescriptor::zedMinus
virtual double zedMinus() const =0
IRoiDescriptor.h
RoiUtil::contains
bool contains(const IRoiDescriptor &roi, double z0, double dzdr)
see whether a segment is contained within the roi in r-z
Definition: RoiUtil.cxx:42
RoiUtil::range_error::range_error
range_error(const char *s)
Definition: RoiUtil.cxx:32
M_2PI
#define M_2PI
Definition: CaloGpuGeneral_fnc.cxx:8
IRoiDescriptor::at
virtual const IRoiDescriptor * at(int i) const =0
find an RoiDescriptor constituent
RoiUtil::etacheck
double etacheck(double eta)
Definition: RoiUtil.cxx:133
RoiUtil::range_error
Definition: RoiUtil.cxx:30
IRoiDescriptor::composite
virtual bool composite() const =0
Super RoI access methods.
operator!=
bool operator!=(const IRoiDescriptor &roi0, const IRoiDescriptor &roi1)
Definition: RoiUtil.cxx:190