#include #include "TVector2.h" #include "TVector3.h" #include "TMath.h" #include "IECALCrystalMap.hxx" #include "IECALCrystalManager.hxx" #include "IGeomInfo.hxx" #include "IECALGeom.hxx" //================================================================= // Constructor/Destructor //================================================================= COMET::IECALCrystalInfoMap::IECALCrystalInfoMap() { } COMET::IECALCrystalInfoMap::~IECALCrystalInfoMap() { for(IIndexChanIdMap::iterator rit = fIndexChanIdMap.begin(); rit != fIndexChanIdMap.end(); rit++){ for(std::vector::iterator cit = rit->begin(); cit != rit->end(); cit++){ if(*cit) delete (*cit); } } for(IIndexChanIdMap::iterator rit = fBlockIndexChanIdMap.begin(); rit != fBlockIndexChanIdMap.end(); rit++){ for(std::vector::iterator cit = rit->begin(); cit != rit->end(); cit++){ if(*cit) delete (*cit); } } } COMET::IECALWritableCrystalInfoMap:: IECALWritableCrystalInfoMap(COMET::IECALCrystalManager::ICrystalInfoContainer& container) { BuildMap(container); } COMET::IECALWritableCrystalInfoMap::~IECALWritableCrystalInfoMap() { } //================================================================= // BuildMap for crystal map //================================================================= bool COMET::IECALWritableCrystalInfoMap:: BuildMap(COMET::IECALCrystalManager::ICrystalInfoContainer& container) { if(container.size() == 0){ COMETError("IECALCrystalInfoMap::BuildMap(): No CrystalInfo stored."); return false; } double blockWidth = COMET::IGeomInfo::Get().ECAL().GetBlockWidth(); //------------------------------------------------------ // (1) Get the positions of the ends //------------------------------------------------------ TVector2 minPos(1e9, 1e9), maxPos(-1e9, -1e9); for(IECALCrystalManager::ICrystalInfoContainer:: const_iterator info = container.begin(); info != container.end(); info++){ minPos.Set( std::min(minPos.X(), info->Get2dPosition().X() ), std::min(minPos.Y(), info->Get2dPosition().Y() )); maxPos.Set( std::max(maxPos.X(), info->Get2dPosition().X() ), std::max(maxPos.Y(), info->Get2dPosition().Y() )); } //------------------------------------------------------ // (2) Get the maximum numbers of vertically/horizontally aligned crystals //------------------------------------------------------ fSizeX = 2*Int_t((maxPos.X() - minPos.X())/blockWidth + 1); fSizeY = 2*Int_t((maxPos.Y() - minPos.Y())/blockWidth + 1); fIndexChanIdMap = std::vector< std::vector > (fSizeX, std::vector(fSizeY, NULL)); fBlockIndexChanIdMap = std::vector< std::vector > (fSizeX/2, std::vector(fSizeY/2, NULL)); COMETNamedDebug("IECALCrystalInfoMap", "BuildMap(): MaxNum = (" << fSizeX << "," << fSizeY << ")"); // Averaged cystal width double crystalWidth = (maxPos.X()-minPos.X())/(fSizeX-1); minPos -= 0.5*TVector2(crystalWidth, crystalWidth); //------------------------------------------------------ // (3) Create Map and fill NULL //------------------------------------------------------ fMap = IMap(fSizeX, ITVector(fSizeY, NULL)); //------------------------------------------------------ // (4) Mapping info to map and assign index to each info //------------------------------------------------------ for(IECALCrystalManager::ICrystalInfoContainer:: iterator info = container.begin(); info != container.end(); info++){ TVector2 pos = info->Get2dPosition() - minPos; int idxX = pos.X()/crystalWidth; int idxY = pos.Y()/crystalWidth; // Outside of the map if(not IsValidIndex(idxX, idxY)){ COMETError("BuildMap(): Invalid index of the map"); throw EoaGeomInfo(); } // Duplicated mapping if(Get(idxX, idxY)){ COMETError("BuildMap(): Mapping duplication at (" << idxX << "," << idxY << ")."); throw EoaGeomInfo(); } Set(idxX, idxY, &(*info)); info->SetIdx(idxX, idxY); COMETNamedDebug("IECALCrystalInfoMap", "BuildMap(): Mapped info(" << info->GetSequenceId() << ":" << info->GetBlockId() << "," << info->GetCrystalId() << ") at (" << info->GetIdxX() << "," << info->GetIdxY() << ")."); // Set map (ChannelId -> index) COMET::IChannelId chanId = COMET::IECALChannelId(COMET::IChannelId::kECAL, info->GetBlockId(), info->GetCrystalId()); fChanIdIndexMap[chanId] = IIndex(idxX, idxY); fIndexChanIdMap[idxX][idxY] = new IChannelId(chanId); // Set block map (IECALTrigChannelId -> index) if(0 == idxX%2 && 0 == idxY%2){ COMET::IChannelId bChanId = ((IECALChannelId)chanId).GetTrigChannelId(); fBlockChanIdIndexMap[bChanId] = IIndex(idxX/2, idxY/2); fBlockIndexChanIdMap[idxX/2][idxY/2] = new COMET::IChannelId(bChanId); } } //------------------------------------------------------ // (5) Link neighbors to each info //------------------------------------------------------ for(int x=0; xSetNeighbor(idx, Get(xx,yy)); } } COMETNamedDebug("IECALCrystalInfoMap", "Crystal (" << x << "," << y << ")" << " has " << info->GetNumberOfNeighbors() << " neighbors."); } } return true; }