// $Id: IGeometryId.cxx,v 1.12 2012/02/22 03:15:09 mcgrew Exp $ #include #include "ICOMETLog.hxx" #include "IGeometryId.hxx" #include "COMETGeomIdDef.hxx" #include "IOADatabase.hxx" #include "IGeomIdManager.hxx" ClassImp(COMET::IGeometryId); COMET::IGeometryId::IGeometryId() : fGeometryId(COMET::GeomId::Def::kEmptyId) {} COMET::IGeometryId::IGeometryId(int i) : fGeometryId(i) {} COMET::IGeometryId::IGeometryId(const IGeometryId& geom) : fGeometryId(geom.fGeometryId) {} COMET::IGeometryId::~IGeometryId() {} void COMET::IGeometryId::SetFieldSafe(int val, int msb, int lsb) { // Make sure the bit range is valid. if (msb31 || lsb <0) { COMETError("Invalid bit field range -- MSB: " << msb << " LSB: " << lsb); throw EGeomIdMSBLSB(); } // Make sure val is in range. int maxValue = (1 << (msb-lsb+1)); if (val >= maxValue) { COMETError("Value out of range " << val << " Bits in field " << msb-lsb+1 << " Maximum value " << maxValue); throw EGeomIdOutOfRange(); } if (val < 0) { COMETError("Value out of range " << val << " -- Negative values are not allowed"); throw EGeomIdOutOfRange(); } // Build a mask for this field long mask = ((1<<(msb-lsb+1))-1) << lsb; // Clear the bit field. fGeometryId &= ~mask; // Shift the value and set the field. fGeometryId |= ((val << lsb)&mask); if (!IsValid()) { COMETError("Invalid id: " << fGeometryId); throw EGeomIdInvalid(); } } int COMET::IGeometryId::GetFieldSafe(int msb, int lsb) const { if (msb31 || lsb <0) throw EGeomIdMSBLSB(); int field = fGeometryId >> lsb; int mask = (1<<(msb-lsb+1))-1; return (field & mask); } int COMET::IGeometryId::GetSubsystemId() const { return GetFieldSafe(COMET::GeomId::Def::kDetectorIdMSB, COMET::GeomId::Def::kDetectorIdLSB); } const bool COMET::IGeometryId::IsValid() const { if (fGeometryId < 0) return false; int field = fGeometryId >> COMET::GeomId::Def::kDetectorIdLSB; int mask = (1<<(COMET::GeomId::Def::kDetectorIdMSB - COMET::GeomId::Def::kDetectorIdLSB+1))-1; int sys = (field & mask); switch (sys) { case COMET::GeomId::Def::kCDC: case COMET::GeomId::Def::kCTH: case COMET::GeomId::Def::kStrawTrk: case COMET::GeomId::Def::kECAL: case COMET::GeomId::Def::kCosmicVeto: case COMET::GeomId::Def::kMuX: case COMET::GeomId::Def::kProtBeamMon: case COMET::GeomId::Def::kPiCapSol: case COMET::GeomId::Def::kMuTrSol: case COMET::GeomId::Def::kColSol: case COMET::GeomId::Def::kDetSol: case COMET::GeomId::Def::kTOF: case COMET::GeomId::Def::kROOTGeoNodeId: return true; default: return false; } return false; } std::string COMET::IGeometryId::GetName() const { return IOADatabase::Get().GeomId().GetPath(*this); } std::string COMET::IGeometryId::GetSubsystemName() const { int sys = GetFieldSafe(COMET::GeomId::Def::kDetectorIdMSB, COMET::GeomId::Def::kDetectorIdLSB); switch (sys) { case COMET::GeomId::Def::kCDC: return "CDC"; case COMET::GeomId::Def::kCTH: return "CTH"; case COMET::GeomId::Def::kStrawTrk: return "StrawTrk"; case COMET::GeomId::Def::kECAL: return "ECAL"; case COMET::GeomId::Def::kCosmicVeto: return "CosmicVeto"; case COMET::GeomId::Def::kMuX: return "MuX"; case COMET::GeomId::Def::kProtBeamMon: return "ProtbeamMon"; case COMET::GeomId::Def::kPiCapSol: return "PiCapSol"; case COMET::GeomId::Def::kMuTrSol: return "MuTrSol"; case COMET::GeomId::Def::kColSol: return "ColSol"; case COMET::GeomId::Def::kDetSol: return "DetSol"; case COMET::GeomId::Def::kTOF: return "TOF"; case COMET::GeomId::Def::kROOTGeoNodeId: return "node"; } return "unknown"; } TVector3 COMET::IGeometryId::GetPosition() const { TVector3 position(0,0,0); if (!IOADatabase::Get().GeomId().GetPosition(*this,position)) { COMETWarn("Position for invalid geometry id was requested"); throw COMET::EGeomIdInvalid(); } return position; } bool operator==(const COMET::IGeometryId& a, const COMET::IGeometryId& b) { return (a.AsInt() == b.AsInt()); } bool operator!=(const COMET::IGeometryId& a, const COMET::IGeometryId& b) { return (a.AsInt() != b.AsInt()); } std::ostream& operator<<(std::ostream& s, const COMET::IGeometryId& id) { try { std::string name = id.GetName(); int len = name.size(); int maxlen = 50; if (len > maxlen) name = "..." + name.substr(name.size()-maxlen); s << name; } catch (...) { s << id.AsInt(); }; return s; }