Newer
Older
TB_Chris / TbUT / src / .svn / text-base / TbUTClusterCreator.cpp.svn-base
  1. /*
  2. * TbUTClusterCreator.cpp
  3. *
  4. * Created on: Jan 5, 2015
  5. * Author: ADendek
  6. */
  7.  
  8. #include "TbUTClusterCreator.h"
  9. #include <iostream>
  10. #include <cmath>
  11.  
  12. using namespace TbUT;
  13. using namespace std;
  14.  
  15. ClusterCreator::ClusterCreator(const std::string& p_sensorType,ITresholdProvider& p_thresholds):
  16. m_clusterMaxSize(4),
  17. m_thresholds(p_thresholds),
  18. m_culsterVector()
  19. {
  20. convertStringToSensorType(p_sensorType);
  21. }
  22.  
  23. ClusterContainer::ClusterVector ClusterCreator::createClusters(RawData<double> *p_inputData)
  24. {
  25. m_culsterVector.clear();
  26. findCulsterSeeds(p_inputData);
  27. removeDuplicatedSeeds(p_inputData);
  28. extendClusterSeeds(p_inputData);
  29. fillOuterStrips(p_inputData);
  30. return m_culsterVector;
  31. }
  32.  
  33. void ClusterCreator::convertStringToSensorType(const std::string& p_sensorType)
  34. {
  35. if(p_sensorType == "PType")
  36. m_sensorType= ClusterCreator::SensorType::P_TYPE;
  37. else if (p_sensorType == "NType")
  38. m_sensorType= ClusterCreator::SensorType::N_TYPE;
  39. else
  40. m_sensorType= ClusterCreator::SensorType::CUSTOM;
  41. }
  42.  
  43. void ClusterCreator::findCulsterSeeds(RawData<double> *p_inputData)
  44. {
  45. int l_channelNumber=RawData<>::getnChannelNumber();
  46. for(auto channel=0;channel<l_channelNumber;channel++){
  47. auto l_channelSignal=p_inputData->getSignal(channel);
  48. if(isBiggerThanSeedThreshold(l_channelSignal,channel ))
  49. m_culsterVector.push_back(createCluster(p_inputData,channel));
  50. }
  51.  
  52. }
  53.  
  54. bool ClusterCreator::isBiggerThanSeedThreshold(RawData<double>::DataType p_channelSignal, int p_channel) const
  55. {
  56. double l_channelThreshold=m_thresholds.getHighClusterThreshold(p_channel);
  57.  
  58. switch(m_sensorType)
  59. {
  60. case ClusterCreator::SensorType::P_TYPE:
  61. return p_channelSignal<(-1)*l_channelThreshold;
  62. case ClusterCreator::SensorType::N_TYPE:
  63. return p_channelSignal>l_channelThreshold;
  64. default:
  65. return abs(p_channelSignal) >l_channelThreshold ;
  66. }
  67. }
  68.  
  69. Cluster ClusterCreator::createCluster(RawData<double> *p_inputData, int l_channel) const
  70. {
  71. int l_seedSize=1;
  72. Cluster l_cluster;
  73. l_cluster.m_charge=p_inputData->getSignal(l_channel);
  74. l_cluster.m_chargeSeed=p_inputData->getSignal(l_channel);
  75. l_cluster.m_seedPosition=l_channel;
  76. l_cluster.m_size=l_seedSize;
  77. l_cluster.m_position=l_channel*abs(l_cluster.m_charge); //position is weighted average, weight charge
  78.  
  79. return l_cluster;
  80. }
  81.  
  82. void ClusterCreator::removeDuplicatedSeeds(RawData<double> *p_inputData)
  83. {
  84. if(0==m_culsterVector.size()) return;
  85. for(auto firstClusterIt = m_culsterVector.begin();firstClusterIt!=m_culsterVector.end();firstClusterIt++)
  86. {
  87. for(auto secondClusterIt=firstClusterIt+1;secondClusterIt!=m_culsterVector.end();)
  88. {
  89. if(arePartOfTheSameCluster(p_inputData, firstClusterIt,secondClusterIt))
  90. removeClusterSeedWithSmallerCharge(firstClusterIt,secondClusterIt);
  91. else
  92. secondClusterIt++;
  93. if(secondClusterIt==m_culsterVector.end()) break;
  94. }
  95. if(firstClusterIt==m_culsterVector.end()) break;
  96. }
  97. }
  98.  
  99. bool ClusterCreator::arePartOfTheSameCluster(RawData<double>* p_inputData, ClusterIterator& p_firstIt, ClusterIterator& p_secondIt) const
  100. {
  101. if(!canBePartOfTheSameCluster(p_firstIt, p_secondIt)) return false;
  102.  
  103. auto channelRIterator=p_secondIt->m_seedPosition-1; // take previous strip position than pointed by secondIt
  104. auto firstSeedPosition=p_firstIt->m_seedPosition;
  105. for(;channelRIterator>firstSeedPosition;channelRIterator--){
  106. auto channelCharge=p_inputData->getSignal(channelRIterator);
  107. if(! isBiggerThanLowThreshold(channelCharge,channelRIterator) ) return false;
  108. }
  109. return true;
  110. }
  111.  
  112.  
  113. bool ClusterCreator::canBePartOfTheSameCluster(ClusterIterator& p_firstIt, ClusterIterator& p_secondIt) const
  114. {
  115. int seedDistance=abs(p_firstIt->m_seedPosition-p_secondIt->m_seedPosition);
  116. return seedDistance<m_clusterMaxSize;
  117. }
  118.  
  119. void ClusterCreator::removeClusterSeedWithSmallerCharge(ClusterIterator& p_firstIt, ClusterIterator& p_secondIt)
  120. {
  121. if(abs(p_firstIt->m_charge)>abs(p_secondIt->m_charge))
  122. p_secondIt=m_culsterVector.erase(p_secondIt);
  123. else{
  124. p_firstIt =m_culsterVector.erase(p_firstIt);
  125. p_secondIt=p_firstIt+1;
  126. }
  127. }
  128.  
  129. void ClusterCreator::extendClusterSeeds(RawData<double> *p_inputData)
  130. {
  131. for(auto clusterIt = m_culsterVector.begin();clusterIt!=m_culsterVector.end();clusterIt++)
  132. {
  133. extendCluster(clusterIt, p_inputData);
  134. }
  135. }
  136.  
  137. void ClusterCreator::extendCluster(ClusterIterator& p_clusterIt,RawData<double>* p_inputData)
  138. {
  139. int l_actualShift=1;
  140. bool l_checkLeft=true;
  141. bool l_checkRight=true;
  142. bool l_isCheckingLeft=true;
  143.  
  144.  
  145. while(hasNotMaximumSize(p_clusterIt))
  146. {
  147. l_isCheckingLeft=true;
  148. if(l_checkLeft && (l_checkLeft=isStripNeedToBeAddedToCluster(p_clusterIt,p_inputData, l_actualShift, l_isCheckingLeft ) ) ){
  149. updateCluster(p_clusterIt,p_inputData, l_actualShift, l_isCheckingLeft);
  150. }
  151. if(! hasNotMaximumSize(p_clusterIt) ) break;
  152.  
  153. l_isCheckingLeft=false;
  154. if(l_checkRight && (l_checkRight=isStripNeedToBeAddedToCluster(p_clusterIt,p_inputData, l_actualShift, l_isCheckingLeft ) ) ) updateCluster(p_clusterIt,p_inputData, l_actualShift, l_isCheckingLeft);
  155. l_actualShift++;
  156. if(!l_checkLeft && !l_checkRight ) break;
  157. }
  158. }
  159.  
  160. bool ClusterCreator::hasNotMaximumSize(ClusterIterator& p_firstIt) const
  161. {
  162. return p_firstIt->m_size<m_clusterMaxSize;
  163. }
  164.  
  165. bool ClusterCreator::isStripNeedToBeAddedToCluster(ClusterIterator& p_clusterIt, RawData<double> *p_inputData, int p_stripShift,bool p_isCheckingLeft ) const
  166. {
  167. int l_sign =(p_isCheckingLeft)? -1:1;
  168. int l_channelNumber=p_clusterIt->m_seedPosition+l_sign*p_stripShift;
  169.  
  170. if(isInvalidChannelNumber(l_channelNumber ) ) return false;
  171.  
  172. double l_channelSignal=p_inputData->getSignal(l_channelNumber);
  173. return isBiggerThanLowThreshold(l_channelSignal, l_channelNumber);
  174. }
  175.  
  176. void ClusterCreator::updateCluster(ClusterIterator& p_clusterIt,RawData<double> *p_inputData ,int p_stripShift, bool p_isCheckingLeft)
  177. {
  178. int l_sign =(p_isCheckingLeft)? -1:1;
  179. int l_channelNumber=p_clusterIt->m_seedPosition+l_sign*p_stripShift;
  180. auto l_channelSignal=p_inputData->getSignal(l_channelNumber);
  181. p_clusterIt->m_charge+=l_channelSignal;
  182. p_clusterIt->m_size+=1;
  183. p_clusterIt->m_position+=l_channelNumber*abs(l_channelSignal);
  184. }
  185.  
  186. bool ClusterCreator::isInvalidChannelNumber(int p_stripNumber) const
  187. {
  188. if(p_stripNumber<RawData<>::getMinChannel()) return true;
  189. if(p_stripNumber>RawData<>::getMaxChannel()) return true;
  190. return false;
  191. }
  192.  
  193. bool ClusterCreator::isBiggerThanLowThreshold(RawData<double>::DataType p_channelSignal, int p_chnnel) const
  194. {
  195. double l_threshold=m_thresholds.getLowClusterThreshold(p_chnnel);
  196. switch(m_sensorType)
  197. {
  198. case SensorType::P_TYPE:
  199. return p_channelSignal<(-1)*l_threshold;
  200. case SensorType::N_TYPE:
  201. return p_channelSignal>l_threshold;
  202. default:
  203. return abs(p_channelSignal) >l_threshold ;
  204. }
  205. }
  206.  
  207.  
  208. void ClusterCreator::fillOuterStrips(RawData<double> *p_inputData)
  209. {
  210. for(auto& cluster : m_culsterVector){
  211. int position=cluster.m_seedPosition;
  212. int oneLeft=position-1;
  213. if(isInvalidChannelNumber(oneLeft)) cluster.m_charge1StripLeft = 0 ;
  214. else cluster.m_charge1StripLeft = p_inputData->getSignal(oneLeft) ;
  215.  
  216. int twoLeft=oneLeft-1;
  217. if(isInvalidChannelNumber(twoLeft)) cluster.m_charge2StripLeft = 0;
  218. else cluster.m_charge2StripLeft = p_inputData->getSignal(twoLeft) ;
  219.  
  220. int oneRight=position+1;
  221. if(isInvalidChannelNumber(oneRight)) cluster.m_charge1StripRight = 0 ;
  222. else cluster.m_charge1StripRight = p_inputData->getSignal(oneRight) ;
  223.  
  224. int twoRight=oneRight+1;
  225. if(isInvalidChannelNumber(twoRight)) cluster.m_charge2StripRight = 0;
  226. else cluster.m_charge2StripRight = p_inputData->getSignal(twoRight) ;
  227.  
  228. /// normalize position
  229. cluster.m_position/=abs(cluster.m_charge);
  230. }
  231. }
  232.  
  233.  
  234.