Cluster.cxx 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. // $Id$
  2. // Author: artur 2016/04/07
  3. //_____________________________________________________________________________
  4. //
  5. // Cluster
  6. //_____________________________________________________________________________
  7. #include "Cluster.h"
  8. #include <iostream>
  9. using std::cout;
  10. using std::endl;
  11. ClassImp(Cluster)
  12. //_____________________________________________________________________________
  13. Cluster::Cluster():TNamed("cluster","")
  14. {
  15. Id_.Set(3);
  16. Value_.Set(3);
  17. }
  18. //_____________________________________________________________________________
  19. Cluster::Cluster(Int_t uid):TNamed("cluster","")
  20. {
  21. Id_.Set(3);
  22. Value_.Set(3);
  23. Id_[0] = uid;
  24. }
  25. //__________________________________________________________________
  26. void Cluster::clear()
  27. {
  28. Value_.Set(3);
  29. Value_.Reset(0);
  30. }
  31. //_____________________________________________________________________________
  32. void Cluster::SetId(Int_t i, Int_t id)
  33. {
  34. if (i < 1) return;
  35. if (i < Id_.fN) { Id_[i] = id; return; }
  36. Id_.Set(i+1);
  37. Id_[i] = id;
  38. }
  39. //_____________________________________________________________________________
  40. void Cluster::SetValue(Int_t i, Double_t v)
  41. {
  42. if (i < 0) return;
  43. if (i < Value_.fN) { Value_[i] = v; return; }
  44. Value_.Set(i+1);
  45. Value_[i] = v;
  46. }
  47. //_____________________________________________________________________________
  48. void Cluster::AddValue(Int_t i, Double_t v)
  49. {
  50. if (i < 0) return;
  51. if (i < Value_.fN) { Value_[i] += v; return; }
  52. Value_.Set(i+1);
  53. Value_[i] = v;
  54. }
  55. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  56. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  57. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  58. //_____________________________________________________________________________
  59. const ClusterElement* Cluster::GetElement(Int_t id) const
  60. {
  61. if (GetId() < 1) return 0;
  62. if (id < 1) return 0;
  63. CL_ESET::const_iterator it = Elements_.find(id);
  64. return (it != Elements_.end()) ? it->second : 0;
  65. }
  66. //_____________________________________________________________________________
  67. Int_t* Cluster::GetElementsID() const
  68. {
  69. Int_t n = Elements_.size();
  70. if (n < 1) return 0;
  71. Int_t* ids = new Int_t[n];
  72. CL_ESET::const_iterator it = Elements_.begin();
  73. for (n = 0; it != Elements_.end(); it++) ids[n++] = it->first;
  74. return ids;
  75. }
  76. //_____________________________________________________________________________
  77. Int_t* Cluster::GetCollectionsID() const
  78. {
  79. Int_t n = CollectionsID_.size();
  80. if (n < 1) return 0;
  81. Int_t* ids = new Int_t[n];
  82. CL_IDS::const_iterator it = CollectionsID_.begin();
  83. for (n = 0; it != CollectionsID_.end(); it++) ids[n++] = *it;
  84. return ids;
  85. }
  86. //_____________________________________________________________________________
  87. Bool_t Cluster::FindCollection(Int_t id) const
  88. {
  89. CL_IDS::const_iterator it = CollectionsID_.find(id);
  90. return (it != CollectionsID_.end());
  91. }
  92. //_____________________________________________________________________________
  93. Bool_t Cluster::FindElement(const ClusterElement* elem) const
  94. {
  95. if (GetId() < 1) return kFALSE;
  96. if (!elem) return kFALSE;
  97. Int_t id = elem->GetId();
  98. CL_ESET::const_iterator it = Elements_.find(id);
  99. return (it != Elements_.end());
  100. }
  101. //_____________________________________________________________________________
  102. Bool_t Cluster::FindElement(Int_t id) const
  103. {
  104. if (GetId() < 1) return kFALSE;
  105. if (id < 1) return kFALSE;
  106. CL_ESET::const_iterator it = Elements_.find(id);
  107. return (it != Elements_.end());
  108. }
  109. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  110. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  111. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  112. //_____________________________________________________________________________
  113. Bool_t Cluster::AddElement(ClusterElement* elem)
  114. {
  115. if (GetId() < 1) {
  116. cout << "[W]<Cluster::AddElement> Cluster has no ID (=0)" << endl;
  117. return kFALSE;
  118. }
  119. if (!elem) {
  120. cout << "[W]<Cluster::AddElement> No element" << endl;
  121. return kFALSE;
  122. }
  123. Int_t id = elem->GetId();
  124. CL_ESET::const_iterator it = Elements_.find(id);
  125. if (it == Elements_.end()) Elements_.insert(std::pair<Int_t,ClusterElement*>(id,elem));
  126. elem->AddCluster(GetId());
  127. return kTRUE;
  128. }
  129. //_____________________________________________________________________________
  130. Bool_t Cluster::RemoveElement(ClusterElement* elem)
  131. {
  132. if (GetId() < 1) return kFALSE;
  133. if (!elem) return kFALSE;
  134. Int_t id = elem->GetId();
  135. CL_ESET::iterator it = Elements_.find(id);
  136. if (it != Elements_.end()) Elements_.erase(it);
  137. elem->RemoveCluster(GetId());
  138. return kTRUE;
  139. }
  140. //_____________________________________________________________________________
  141. Bool_t Cluster::RemoveElement(Int_t id)
  142. {
  143. if (GetId() < 1) return kFALSE;
  144. if (id < 1) return kFALSE;
  145. CL_ESET::iterator it = Elements_.find(id);
  146. if (it != Elements_.end()) {
  147. it->second->RemoveCluster(GetId());
  148. Elements_.erase(it);
  149. }
  150. return kTRUE;
  151. }
  152. //_____________________________________________________________________________
  153. void Cluster::RemoveElements() {
  154. //
  155. CL_ESET::iterator it = Elements_.begin();
  156. Int_t id = GetId();
  157. for ( ; it != Elements_.end(); it++) it->second->RemoveCluster(id);
  158. Elements_.clear();
  159. }
  160. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  161. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  162. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  163. //_____________________________________________________________________________
  164. Bool_t Cluster::AddCollection(Int_t id)
  165. {
  166. if (GetId() < 1) return kFALSE;
  167. if (id < 1) return kFALSE;
  168. std::pair<CL_IDS::iterator,Bool_t> it = CollectionsID_.insert(id);
  169. return it.second;
  170. }
  171. //_____________________________________________________________________________
  172. Bool_t Cluster::RemoveCollection(Int_t id)
  173. {
  174. Int_t n = CollectionsID_.erase(id);
  175. return (n > 0);
  176. }
  177. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  178. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  179. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  180. //_____________________________________________________________________________
  181. void Cluster::print(Int_t opt) const
  182. {
  183. }