ClManager.cxx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. // $Id$
  2. // Author: artur 2016/04/07
  3. //_____________________________________________________________________________
  4. //
  5. // ClManager
  6. //_____________________________________________________________________________
  7. #include "ClManager.h"
  8. #include <iostream>
  9. using std::cout;
  10. using std::endl;
  11. ClassImp(ClManager)
  12. //_____________________________________________________________________________
  13. ClManager::ClManager():ElementsN_(0),ClustersN_(0),CollectionsN_(0)
  14. {
  15. //cout << "<ClManager::ClManager> " << endl;
  16. }
  17. //_____________________________________________________________________________
  18. ClManager::~ClManager()
  19. {
  20. DeleteAll();
  21. }
  22. //__________________________________________________________________
  23. void ClManager::DeleteAll()
  24. {
  25. if (!Collections_.empty()) {
  26. CL_SSET::iterator it = Collections_.begin();
  27. for (; it != Collections_.end(); it++) {
  28. if (it->second) delete it->second;
  29. }
  30. Collections_.clear();
  31. }
  32. CollectionsN_ = 0;
  33. if (!Clusters_.empty()) {
  34. CL_CSET::iterator it = Clusters_.begin();
  35. for (; it != Clusters_.end(); it++) {
  36. if (it->second) delete it->second;
  37. }
  38. Clusters_.clear();
  39. }
  40. ClustersN_ = 0;
  41. if (!Elements_.empty()) {
  42. CL_ESET::iterator it = Elements_.begin();
  43. for (; it != Elements_.end(); it++) {
  44. if (it->second) delete it->second;
  45. }
  46. Elements_.clear();
  47. }
  48. ElementsN_ = 0;
  49. }
  50. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  51. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  52. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  53. //_____________________________________________________________________________
  54. ClusterElement* ClManager::FindElement(int id) const
  55. {
  56. if (id < 1) return 0;
  57. CL_ESET::const_iterator it = Elements_.find(id);
  58. return (it != Elements_.end()) ? it->second : 0;
  59. }
  60. //_____________________________________________________________________________
  61. Cluster* ClManager::FindCluster(int id) const
  62. {
  63. if (id < 1) return 0;
  64. CL_CSET::const_iterator it = Clusters_.find(id);
  65. return (it != Clusters_.end()) ? it->second : 0;
  66. }
  67. //_____________________________________________________________________________
  68. ClusterCollection* ClManager::FindCollection(int id) const
  69. {
  70. if (id < 1) return 0;
  71. CL_SSET::const_iterator it = Collections_.find(id);
  72. return (it != Collections_.end()) ? it->second : 0;
  73. }
  74. //_____________________________________________________________________________
  75. ClusterCollection* ClManager::FindCollection(TString name) const
  76. {
  77. if (name.IsWhitespace()) return 0;
  78. CL_SSET::const_iterator it = Collections_.begin();
  79. ClusterCollection* coll = 0;
  80. for ( ; it != Collections_.end(); it++) {
  81. if (name == it->second->GetName()) {
  82. if (!coll) coll = it->second;
  83. else {
  84. cout << "<-W- <ClManager::FindCollection> "
  85. << "The same name collection found: "
  86. << " name = " << name << " id = " << it->first
  87. << endl;
  88. }
  89. }
  90. }
  91. return coll;
  92. }
  93. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  94. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  95. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  96. //_____________________________________________________________________________
  97. ClusterElement* ClManager::CreateNewElement()
  98. {
  99. CL_ESET::iterator it = Elements_.find(ElementsN_+1);
  100. if (it != Elements_.end()) {
  101. cout << "[E]<ClManager::CreateNewElement> Failed" << endl;
  102. return 0;
  103. }
  104. ElementsN_++;
  105. ClusterElement* elem = new ClusterElement(ElementsN_);
  106. Elements_.insert(it,std::pair<Int_t,ClusterElement*>(ElementsN_,elem));
  107. return elem;
  108. }
  109. //_____________________________________________________________________________
  110. Cluster* ClManager::CreateNewCluster()
  111. {
  112. CL_CSET::iterator it = Clusters_.find(ClustersN_+1);
  113. if (it != Clusters_.end()) {
  114. cout << "[E]<ClManager::CreateNewCluster> Failed " << endl;
  115. return 0;
  116. }
  117. ClustersN_++;
  118. Cluster* cluster = new Cluster(ClustersN_);
  119. Clusters_.insert(it,std::pair<Int_t,Cluster*>(ClustersN_,cluster));
  120. return cluster;
  121. }
  122. //_____________________________________________________________________________
  123. ClusterCollection* ClManager::CreateNewCollection()
  124. {
  125. CL_SSET::iterator it = Collections_.find(CollectionsN_+1);
  126. if (it != Collections_.end()) {
  127. cout << "[E]<ClManager::CreateNewCollection> Failed " << endl;
  128. return 0;
  129. }
  130. CollectionsN_++;
  131. ClusterCollection* collection = new ClusterCollection(CollectionsN_);
  132. Collections_.insert(it,std::pair<Int_t,ClusterCollection*>(CollectionsN_,collection));
  133. return collection;
  134. }
  135. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  136. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  137. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  138. //_____________________________________________________________________________
  139. Bool_t ClManager::InitCLPars()
  140. {
  141. return (fCLPars.GetSize() > 0);
  142. }
  143. //_____________________________________________________________________________
  144. Int_t ClManager::GetCLParsN()
  145. {
  146. return fCLPars.GetSize();
  147. }
  148. //_____________________________________________________________________________
  149. Double_t ClManager::GetCLPar(Int_t i)
  150. {
  151. return (i < 0 || i>= fCLPars.GetSize()) ? 0 : fCLPars.At(i);
  152. }
  153. //_____________________________________________________________________________
  154. void ClManager::SetCLPars(unsigned int ncopy, ... )
  155. {
  156. fCLPars.Set(ncopy);
  157. fCLPars.Reset(0);
  158. va_list vars;
  159. va_start(vars,ncopy);
  160. Double_t par = va_arg(vars,Int_t);
  161. fCLPars.SetAt(par,0);
  162. for (unsigned int i = 1; i<ncopy; i++) {
  163. par = va_arg(vars,Double_t);
  164. fCLPars.SetAt(par,i);
  165. //cout << i << " " << par << " " <<endl;
  166. }
  167. va_end(vars);
  168. }
  169. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  170. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  171. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  172. //_____________________________________________________________________________
  173. Bool_t ClManager::RemoveElement(int id)
  174. {
  175. if (id < 1) return kFALSE;
  176. if (Elements_.empty()) return kFALSE;
  177. CL_ESET::iterator ite = Elements_.find(id);
  178. if (ite == Elements_.end()) return kFALSE;
  179. // - remove element from all clusters (without deleting)
  180. // - remove clusters ids from the element
  181. Int_t* ids = ite->second->GetClustersID();
  182. bool del = kFALSE;
  183. if (ids) {
  184. Int_t nc = ite->second->GetNClusters();
  185. Cluster* cl;
  186. for (int i(0); i<nc; i++) {
  187. cl = FindCluster(ids[i]);
  188. if (cl->RemoveElement(id)) del = kTRUE;
  189. }
  190. delete ids;
  191. }
  192. return del;
  193. }
  194. //_____________________________________________________________________________
  195. Bool_t ClManager::RemoveCluster(int id)
  196. {
  197. if (id < 1) return kFALSE;
  198. if (Clusters_.empty()) return kFALSE;
  199. CL_CSET::iterator itc = Clusters_.find(id);
  200. if (itc == Clusters_.end()) return kFALSE;
  201. // - remove cluster from all collections (without deleting)
  202. // - remove collections ids from the cluster
  203. Int_t* ids = itc->second->GetCollectionsID();
  204. bool del = kFALSE;
  205. if (ids) {
  206. Int_t nc = itc->second->GetNCollections();
  207. ClusterCollection* coll;
  208. for (int i(0); i<nc; i++) {
  209. coll = FindCollection(ids[i]);
  210. if (coll && coll->RemoveCluster(id)) del = kTRUE;
  211. }
  212. delete ids;
  213. }
  214. // - remove all elements from the cluster (without deleting)
  215. // - remove cluster's id from the elements */
  216. itc->second->RemoveElements();
  217. return del;
  218. }
  219. //_____________________________________________________________________________
  220. Bool_t ClManager::RemoveCollection(int id)
  221. {
  222. if (id < 1) return kFALSE;
  223. if (Collections_.empty()) return kFALSE;
  224. CL_SSET::iterator its = Collections_.find(id);
  225. if (its == Collections_.end()) return kFALSE;
  226. // - remove all clusters from the collection (without deleting)
  227. // - remove collection's id from clusters
  228. its->second->RemoveClusters();
  229. return kTRUE;
  230. }
  231. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  232. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  233. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  234. //_____________________________________________________________________________
  235. void ClManager::DeleteElement(int id)
  236. {
  237. if (id < 1) return;
  238. if (Elements_.empty()) return;
  239. RemoveElement(id);
  240. CL_ESET::iterator ite = Elements_.find(id);
  241. if (ite == Elements_.end()) return;
  242. ClusterElement* elem = ite->second;
  243. Elements_.erase(ite);
  244. delete elem;
  245. }
  246. //_____________________________________________________________________________
  247. void ClManager::DeleteCluster(int id)
  248. {
  249. if (id < 1) return;
  250. if (Clusters_.empty()) return;
  251. RemoveCluster(id);
  252. CL_CSET::iterator itc = Clusters_.find(id);
  253. if (itc == Clusters_.end()) return;
  254. Cluster* cluster = itc->second;
  255. Clusters_.erase(itc);
  256. delete cluster;
  257. }
  258. //_____________________________________________________________________________
  259. void ClManager::DeleteCollection(int id)
  260. {
  261. if (id < 1) return;
  262. if (Collections_.empty()) return;
  263. RemoveCollection(id);
  264. CL_SSET::iterator it = Collections_.find(id);
  265. if (it == Collections_.end()) return;
  266. ClusterCollection* coll = it->second;
  267. Collections_.erase(it);
  268. delete coll;
  269. }
  270. //__________________________________________________________________
  271. void ClManager::DeleteCollections()
  272. {
  273. CollectionsN_ = 0;
  274. if (Collections_.empty()) return;
  275. CL_SSET::iterator it = Collections_.begin();
  276. for (; it != Collections_.end(); it++) {
  277. it->second->RemoveClusters();
  278. delete it->second;
  279. }
  280. Collections_.clear();
  281. }
  282. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  283. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  284. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  285. //__________________________________________________________________
  286. void ClManager::Print(Int_t opt) const
  287. {
  288. cout << "[I]<ClManager::Print> Elements: " << Elements_.size() << endl;
  289. cout << "[I]<ClManager::Print> Clusters: " << Clusters_.size() << endl;
  290. cout << "[I]<ClManager::Print> Collections: " << Collections_.size() << endl;
  291. }