StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
StSstPackage.cc
1 //$Id: StSstPackage.cc,v 1.3 2016/05/30 21:43:07 bouchet Exp $
2 //
3 //$Log: StSstPackage.cc,v $
4 //Revision 1.3 2016/05/30 21:43:07 bouchet
5 //coverity : RESOURCE_LEAK fixed in exchange method
6 //
7 //Revision 1.2 2015/06/24 17:37:21 smirnovd
8 //StSstUtil: Prepend included headers with path to submodule
9 //
10 //Revision 1.1 2015/06/23 16:26:20 jeromel
11 //First version created from the SSD code and reshaped
12 //
13 //Revision 1.1 2015/04/19 17:30:32 bouchet
14 //initial commit ; SST codes
15 //
16 
17 //fork from the SSD code, move along - see history therein
18 
19 #include "StSstUtil/StSstPackage.hh"
20 #include "St_base/Stiostream.h"
21 #include "StSstUtil/StSstCluster.hh"
22 #include "StSstUtil/StSstClusterControl.h"
23 #include <string.h>
24 #include "StMessMgr.h"
25 
26 StSstPackage::StSstPackage(Int_t rNPackage, StSstClusterControl *control)
27 {
28  Int_t maxMatcheds = control->getClusterTreat();
29  mNPackage = rNPackage;
30  mListLengthM = 0;
31  mFirstMatched = 0;
32  mLastMatched = 0;
33  mMatcheds = new StSstCluster*[maxMatcheds];
34  for(Int_t i=0;i<maxMatcheds;i++) mMatcheds[i]=0;
35  mKind = new char[2*maxMatcheds+1];
36  mKind[0] = '\0';
37  mPrevPackage = 0;
38  mNextPackage = 0;
39 }
40 
41 StSstPackage::StSstPackage(Int_t rNPackage, Int_t rNMatched)
42 {
43  mNPackage = rNPackage;
44  mListLengthM = rNMatched;
45  mFirstMatched = 0;
46  mLastMatched = 0;
47  mMatcheds = new StSstCluster*[rNMatched];
48  for(Int_t i=0;i<rNMatched;i++) mMatcheds[i]=0;
49  mKind = new char[2*rNMatched+1];
50  mKind[0] = '\0';
51  mPrevPackage = 0;
52  mNextPackage = 0;
53 }
54 
55 StSstPackage::StSstPackage(const StSstPackage & originalPackage)
56 {
57  Int_t i = 0;
58  mNPackage = originalPackage.mNPackage;
59  mListLengthM = originalPackage.mListLengthM;
60  mFirstMatched = originalPackage.mFirstMatched;
61  mLastMatched = originalPackage.mLastMatched;
62  mMatcheds = new StSstCluster*[mListLengthM];
63  for(i=0;i<mListLengthM;i++) mMatcheds[i] = originalPackage.mMatcheds[i];
64  mKind = new char[2*mListLengthM+1];
65  for(i=0;i<2*mListLengthM+1;i++) mKind[i] = originalPackage.mKind[i];
66  mPrevPackage = originalPackage.mPrevPackage;
67  mNextPackage = originalPackage.mNextPackage;
68 }
69 
70 StSstPackage::~StSstPackage()
71 {
72  delete [ ] mMatcheds;
73  delete [ ] mKind;
74 }
75 
76 StSstPackage& StSstPackage::operator=(const StSstPackage originalPackage)
77 {
78  Int_t i = 0;
79  mNPackage = originalPackage.mNPackage;
80  mListLengthM = originalPackage.mListLengthM;
81  mFirstMatched = originalPackage.mFirstMatched;
82  mLastMatched = originalPackage.mLastMatched;
83  for(i=0;i<mListLengthM;i++) mMatcheds[i] = originalPackage.mMatcheds[i];
84  for(i=0;i<2*mListLengthM+1;i++) mKind[i] = originalPackage.mKind[i];
85  mPrevPackage = originalPackage.mPrevPackage;
86  mNextPackage = originalPackage.mNextPackage;
87 
88  return *this;
89 }
90 
91 StSstCluster* StSstPackage::next(StSstCluster *ptr)
92 { return ptr->getNextCluster(); }
93 
94 StSstCluster* StSstPackage::prev(StSstCluster *ptr)
95 { return ptr->getPrevCluster(); }
96 
97 StSstCluster* StSstPackage::first()
98 { return mFirstMatched; }
99 
100 StSstCluster* StSstPackage::last()
101 { return mLastMatched; }
102 
103 void StSstPackage::purgePackage()
104 {
105  mKind[0] = '\0';
106  mListLengthM = 0;
107  mPrevPackage=0;
108  mNextPackage=0;
109 }
110 
111 void StSstPackage::takeMatcheds(StSstPackage *ptr)
112 {
113  if (this->mListLengthM == ptr->mListLengthM)
114  {
115  this->mFirstMatched = ptr->mFirstMatched;
116  this->mLastMatched = ptr->mLastMatched;
117  strcpy(this->mKind, ptr->mKind);
118  for(Int_t i = 0;i < this->mListLengthM;i++)
119  { this->mMatcheds[i] = ptr->mMatcheds[i]; }
120  }
121  else
122  LOG_INFO<<"Problem to transfer matched clusters (not enough room) !! "<< endm;
123 }
124 
125 void StSstPackage::setNPackage(Int_t rNPackage)
126 { this->mNPackage = rNPackage; }
127 
128 void StSstPackage::setKind(char *rKind)
129 { this->mKind = rKind ; }
130 
131 void StSstPackage::setPrevPackage(StSstPackage *rPrevPackage)
132 { this->mPrevPackage = rPrevPackage ; }
133 
134 void StSstPackage::setNextPackage(StSstPackage *rNextPackage)
135 { this->mNextPackage = rNextPackage ; }
136 
137 Int_t StSstPackage::getNPackage()
138 { return this->mNPackage; }
139 
140 StSstCluster* StSstPackage::getMatched(Int_t numMatched)
141 {
142  if (numMatched>mListLengthM) return 0;
143  return this->mMatcheds[numMatched];
144 }
145 
146 char* StSstPackage::getKind()
147 { return this->mKind; }
148 
149 StSstPackage* StSstPackage::getPrevPackage()
150 { return this->mPrevPackage; }
151 
152 StSstPackage* StSstPackage::getNextPackage()
153 { return this->mNextPackage; }
154 
155 
156 Int_t StSstPackage::addNewMatched(StSstCluster *ptr, Int_t maxMatcheds)
157 {
158  if ( (!ptr) || (mListLengthM == maxMatcheds) ) return 0;
159  if (mListLengthM == 0)
160  {
161  mFirstMatched = ptr;
162  mLastMatched = ptr;
163  }
164  else
165  {
166  mLastMatched = ptr;
167  }
168  mMatcheds[mListLengthM] = ptr;
169  mListLengthM++;
170  return 1;
171 }
172 
173 Int_t StSstPackage::addKindPackage(Int_t numMatched, Int_t rSide, Int_t maxMatcheds)
174 {
175  if ( (!this->getSize()) || (mListLengthM == maxMatcheds)) return 0;
176  mKind[2*(this->mListLengthM-1)]=char(48+numMatched);
177  if (rSide==0)
178  {
179  mKind[2*(this->mListLengthM)-1]='p';
180  mKind[2*(this->mListLengthM)]='\0';
181  }
182  else
183  {
184  mKind[2*(this->mListLengthM)-1]='n';
185  mKind[2*(this->mListLengthM)]='\0';
186  }
187  return 1;
188 }
189 
190 Int_t StSstPackage::removeMatched(StSstCluster *ptr)
191 {
192  if (!this->getSize()) return 0;
193  StSstCluster *ptBefore = ptr->getPrevCluster();
194  StSstCluster *ptAfter = ptr->getNextCluster();
195 
196  if (ptBefore == 0)
197  {
198  if (ptAfter== 0)
199  {
200  // taille = 1
201  this->mFirstMatched = 0;
202  this->mLastMatched = 0;
203  this->mListLengthM = 0;
204  delete ptr;
205  return 1;
206  }
207  else
208  {
209  this->mFirstMatched = ptAfter;
210  ptAfter->setPrevCluster(0);
211  this->mListLengthM--;
212  delete ptr;
213  return 1;
214  }
215  }
216  else
217  {
218  if (ptAfter== 0)
219  {
220  this->mLastMatched = ptBefore;
221  ptBefore->setNextCluster(0);
222  this->mListLengthM--;
223  delete ptr;
224  return 1;
225  }
226  else
227  {
228  ptBefore->setNextCluster(ptAfter);
229  ptAfter->setPrevCluster(ptBefore);
230  this->mListLengthM--;
231  delete ptr;
232  return 1;
233  }
234  }
235 }
236 
237 void StSstPackage::exchangeTwoMatcheds(StSstCluster *ptr1, StSstCluster *ptr2)
238 {
239  StSstCluster *ptrTemp = new StSstCluster(ptr1->getNCluster());
240  ptr1->copyTo(ptrTemp);
241  ptr2->copyTo(ptr1);
242  ptrTemp->copyTo(ptr2);
243  delete ptrTemp;
244 }
245 
246 void StSstPackage::sortMatched()
247 {
248  Int_t localSize=this->getSize();
249  if (localSize<2) return;
250 
251  StSstCluster *ptCurr = this->first();
252  ptCurr = this->next(ptCurr);
253  for ( ; ptCurr!=0 ; )
254 
255  {
256  StSstCluster *ptB1 = ptCurr;
257  StSstCluster *ptB2;
258  Int_t isCont = 1;
259  while ((ptB1 != this->first())&&(isCont))
260  {
261  ptB2 = this->prev(ptB1);
262  if (ptB2->getFirstStrip() > ptB1->getFirstStrip())
263  {
264  this->exchangeTwoMatcheds(ptB1,ptB2);
265  ptB1 = ptB2;
266  }
267  else
268  {
269  isCont = 0;
270  }
271  }
272  ptCurr = this->next(ptCurr);
273 
274  }
275  return;
276 }
277 
278 void StSstPackage::renumMatched()
279 {
280  Int_t CurrentListSize = this->getSize();
281  if (!CurrentListSize) return;
282  StSstCluster *CurrCluster = this->first();
283  for (Int_t i = 0; i < CurrentListSize; i++)
284  {
285  CurrCluster->setNCluster(i);
286  CurrCluster = this->next(CurrCluster);
287  }
288  return;
289 }
290 
291 Int_t StSstPackage::getSize()
292 { return mListLengthM; }
293 
294 Int_t StSstPackage::isSorted()
295 {
296  StSstCluster *ptr1 = this->first();
297  StSstCluster *ptr2 = 0;
298 
299  while(ptr1 != this->last())
300  {
301  ptr2 = this->next(ptr1);
302  if (ptr1->getFirstStrip()>ptr2->getFirstStrip()) return 0;
303  ptr1 = this->next(ptr1);
304  }
305  return 1;
306 }