00001
00002
00003
00004 #include "Kernel/MuonTileID.h"
00005 #include <numeric>
00006
00007 LHCb::MuonTileID::MuonTileID(const MuonTileID& id,
00008 const IMuonLayout& lay,
00009 const unsigned int x,
00010 const unsigned int y) {
00011
00012 m_muonid = id.m_muonid;
00013 MuonLayout this_layout(lay.grid(id));
00014 setLayout(this_layout);
00015 setX(this_layout.xGrid()/id.layout().xGrid()*id.nX()+x);
00016 setY(this_layout.yGrid()/id.layout().yGrid()*id.nY()+y);
00017 }
00018
00019 LHCb::MuonTileID LHCb::MuonTileID::neighbourID(int dirX, int dirY) const {
00020 LHCb::MuonTileID result(*this);
00021 switch (dirX) {
00022 case MuonBase::RIGHT :
00023 result.setX(nX()+1);
00024 break;
00025 case MuonBase::LEFT :
00026 if(nX() == 0) break;
00027 result.setX(nX()-1);
00028 break;
00029 }
00030 switch (dirY) {
00031 case MuonBase::UP :
00032 result.setY(nY()+1);
00033 break;
00034 case MuonBase::DOWN :
00035 if(nY() == 0) break;
00036 result.setY(nY()-1);
00037 break;
00038 }
00039 return result;
00040 }
00041
00042 bool LHCb::MuonTileID::isDefined() const {
00043 return m_muonid != 0;
00044 }
00045
00046 bool LHCb::MuonTileID::isValid() const {
00047
00048 if ( ! isDefined() ) return false;
00049
00050 MuonLayout ml = layout();
00051
00052 return ml.isValidID(*this);
00053 }
00054
00055 LHCb::MuonTileID
00056 LHCb::MuonTileID::intercept(const LHCb::MuonTileID& otherID) const {
00057
00058
00059
00060 if ( station() != otherID.station() ||
00061 quarter() != otherID.quarter() ) return LHCb::MuonTileID();
00062
00063 int thisGridX = layout().xGrid();
00064 int thisGridY = layout().yGrid();
00065
00066 int otherGridX = otherID.layout().xGrid();
00067 int otherGridY = otherID.layout().yGrid();
00068 if (otherID.region()>region()) {
00069 int rfactor = (1<<otherID.region())/(1<<region());
00070 otherGridX = otherID.layout().xGrid()/rfactor;
00071 otherGridY = otherID.layout().yGrid()/rfactor;
00072 } else if (otherID.region()<region()) {
00073 int rfactor = (1<<region())/(1<<otherID.region());
00074 otherGridX = otherID.layout().xGrid()*rfactor;
00075 otherGridY = otherID.layout().yGrid()*rfactor;
00076 }
00077
00078 if ( thisGridX > otherGridX ) {
00079 unsigned int calcX = nX()*otherGridX/thisGridX;
00080 if (calcX != otherID.nX() ) return LHCb::MuonTileID();
00081 } else {
00082 unsigned int calcX = otherID.nX()*thisGridX/otherGridX;
00083 if (calcX != nX() ) return LHCb::MuonTileID();
00084 }
00085 if ( thisGridY > otherGridY ) {
00086 unsigned int calcY = nY()*otherGridY/thisGridY;
00087 if (calcY != otherID.nY() ) return LHCb::MuonTileID();
00088 } else {
00089 unsigned int calcY = otherID.nY()*thisGridY/otherGridY;
00090 if (calcY != nY() ) return LHCb::MuonTileID();
00091 }
00092
00093
00094
00095 int indX = thisGridX < otherGridX ? otherID.nX() : nX();
00096 int indY = thisGridY < otherGridY ? otherID.nY() : nY();
00097
00098 LHCb::MuonTileID resultID(*this);
00099 resultID.setX(indX);
00100 resultID.setY(indY);
00101 int lx = std::max(thisGridX,otherGridX);
00102 int ly = std::max(thisGridY,otherGridY);
00103 resultID.setLayout(MuonLayout(lx,ly));
00104
00105 return resultID;
00106 }
00107
00108 LHCb::MuonTileID LHCb::MuonTileID::containerID(const IMuonLayout& lay) const {
00109
00110 MuonLayout containerLayout(lay.grid(*this));
00111 LHCb::MuonTileID containerID(*this);
00112 containerID.setX(nX()*containerLayout.xGrid()/layout().xGrid());
00113 containerID.setY(nY()*containerLayout.yGrid()/layout().yGrid());
00114 containerID.setLayout(containerLayout);
00115
00116 return containerID;
00117 }
00118
00119 int LHCb::MuonTileID::localX(const IMuonLayout& lay) const {
00120
00121 MuonLayout padLayout(lay.grid(*this));
00122 return nX() % padLayout.xGrid() ;
00123
00124 }
00125
00126 int LHCb::MuonTileID::localY(const IMuonLayout& lay) const {
00127
00128 MuonLayout padLayout(lay.grid(*this));
00129 return nY() % padLayout.yGrid() ;
00130
00131 }
00132
00133 std::string LHCb::MuonTileID::toString() const {
00134
00135 char bufnm[64];
00136
00137 sprintf(bufnm,"S%d(%d,%d)Q%d,R%d,%d,%d",
00138 station(),
00139 layout().xGrid(),
00140 layout().yGrid(),
00141 quarter(),
00142 region(),
00143 nX(),
00144 nY());
00145 return std::string ( bufnm );
00146
00147 }
00148
00149 std::ostream& LHCb::MuonTileID::fillStream(std::ostream& s) const
00150 {
00151 s << "{ ";
00152 if ( !isValid() ) { s << "WARNING : Unvalid MuonTileID"; }
00153 else if ( !isDefined() ) { s << "WARNING : Undefined MuonTileID"; }
00154 else
00155 {
00156 s << "MuonTileID : " << (int)(*this)
00157 << " : station=" << station()
00158 << " region=" << region()
00159 << " quarter=" << quarter()
00160 << " nX=" << nX()
00161 << " nY=" << nY();
00162 }
00163 return s << " }";
00164 }