StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
StSsdClusterList.cc
1 // $Id: StSsdClusterList.cc,v 1.2 2007/01/08 18:14:06 bouchet Exp $
2 //
3 // $Log: StSsdClusterList.cc,v $
4 // Revision 1.2 2007/01/08 18:14:06 bouchet
5 // correction in the clusters splitting : the size (number of strips) of the clusters found after splitting the original cluster in several are not successively
6 //
7 // Revision 1.1 2006/10/16 16:43:29 bouchet
8 // StSsdUtil regroups now methods for the classes StSsdStrip, StSsdCluster and StSsdPoint
9 //
10 // Revision 1.2 2005/03/18 14:23:46 lmartin
11 // missing CVS header added
12 //
13 
14 #include "StSsdUtil/StSsdClusterList.hh"
15 #include "StSsdUtil/StSsdClusterControl.h"
16 #include "StSsdUtil/StSsdCluster.hh"
17 #include "StSsdStripList.hh"
18 #include <Stiostream.h>
19 #include <math.h>
20 
21 StSsdClusterList::StSsdClusterList()
22 {
23  mListLength = 0;
24  mFirstCluster = 0;
25  mLastCluster = 0;
26 }
27 
28 StSsdClusterList::~StSsdClusterList()
29 {
30  if (mListLength)
31  {
32  StSsdCluster *ptr = mLastCluster;
33  StSsdCluster *toDele;
34  while (mListLength)
35  {
36  toDele = ptr;
37  ptr = prev(ptr);
38  delete toDele;
39  mListLength--;
40  }
41  }
42 }
43 
44 StSsdCluster* StSsdClusterList::next(StSsdCluster *ptr)
45 { return ptr->getNextCluster(); }
46 
47 StSsdCluster* StSsdClusterList::prev(StSsdCluster *ptr)
48 { return ptr->getPrevCluster(); }
49 
50 StSsdCluster* StSsdClusterList::first()
51 { return mFirstCluster; }
52 
53 StSsdCluster* StSsdClusterList::last()
54 { return mLastCluster; }
55 
56 Int_t StSsdClusterList::addNewCluster(StSsdCluster *ptr)
57 {
58  if (!ptr) return 0;
59  if (mListLength == 0)
60  {
61  ptr->setPrevCluster(0);
62  ptr->setNextCluster(0);
63  mFirstCluster = ptr;
64  mLastCluster = ptr;
65  }
66  else
67  {
68  ptr->setPrevCluster(mLastCluster);
69  ptr->setNextCluster(0);
70  mLastCluster->setNextCluster(ptr);
71  mLastCluster = ptr;
72  }
73  mListLength++;
74  return 1;
75 }
76 
77 void StSsdClusterList::exchangeTwoClusters(StSsdCluster *ptr1,StSsdCluster *ptr2)
78 {
79  StSsdCluster *ptrTemp = new StSsdCluster(ptr1->getNCluster());
80  ptr1->copyTo(ptrTemp);
81  ptr2->copyTo(ptr1);
82  ptrTemp->copyTo(ptr2);
83  delete ptrTemp;
84 }
85 
86 void StSsdClusterList::sortCluster()
87 {
88  Int_t localSize=this->getSize();
89  if (localSize<2) return;
90 
91  StSsdCluster *ptCurr = this->first();
92  ptCurr = this->next(ptCurr);
93  for ( ; ptCurr!=0 ; )
94  {
95  StSsdCluster *ptB1 = ptCurr;
96  StSsdCluster *ptB2;
97  Int_t isCont = 1;
98  while ((ptB1 != this->first())&&(isCont))
99  {
100  ptB2 = this->prev(ptB1);
101  if (ptB2->getFirstStrip() > ptB1->getFirstStrip())
102  {
103  this->exchangeTwoClusters(ptB1,ptB2);
104  ptB1 = ptB2;
105  }
106  else
107  {
108  isCont = 0;
109  }
110  }
111  ptCurr = this->next(ptCurr);
112 
113  }
114  return;
115 }
116 
117 void StSsdClusterList::renumCluster()
118 {
119  Int_t CurrentListSize = this->getSize();
120  if (!CurrentListSize) return;
121  StSsdCluster *CurrCluster = this->first();
122  for (Int_t i = 0; i < CurrentListSize; i++)
123  {
124  CurrCluster->setNCluster(i);
125  CurrCluster = this->next(CurrCluster);
126  }
127  return;
128 }
129 
130 Int_t StSsdClusterList::removeCluster(StSsdCluster *ptr)
131 {
132  if (!this->getSize()) return 0;
133  StSsdCluster *ptBefore = ptr->getPrevCluster();
134  StSsdCluster *ptAfter = ptr->getNextCluster();
135 
136  if (ptBefore == 0)
137  {
138  if (ptAfter== 0)
139  {
140  // taille = 1
141  this->mFirstCluster = 0;
142  this->mLastCluster = 0;
143  this->mListLength = 0;
144  delete ptr;
145  return 1;
146  }
147  else
148  {
149  this->mFirstCluster = ptAfter;
150  ptAfter->setPrevCluster(0);
151  this->mListLength--;
152  delete ptr;
153  return 1;
154  }
155  }
156  else
157  {
158  if (ptAfter== 0)
159  {
160  this->mLastCluster = ptBefore;
161  ptBefore->setNextCluster(0);
162  this->mListLength--;
163  delete ptr;
164  return 1;
165  }
166  else
167  {
168  ptBefore->setNextCluster(ptAfter);
169  ptAfter->setPrevCluster(ptBefore);
170  this->mListLength--;
171  delete ptr;
172  return 1;
173  }
174  }
175 }
176 
177 Int_t StSsdClusterList::getSize()
178 { return mListLength; }
179 
180 Int_t StSsdClusterList::splitCluster(StSsdClusterControl *clusterControl, StSsdCluster *CurrentCluster, Int_t *ListAdc, StSsdStripList *currentStripList)
181 {
182  Int_t CurrentClusterSize = CurrentCluster->getClusterSize();
183  if (CurrentClusterSize<3) return 0;
184  Int_t nSubCluster = 0;
185  Int_t isClimbOrFall = 0;
186 
187  Float_t testTolerance = clusterControl->getTestTolerance();//20%
188  Int_t *minima = new int[CurrentClusterSize];
189  Int_t *maxima = new int[CurrentClusterSize];
190  Int_t *keyToIdStrip = new int[CurrentClusterSize];
191  Int_t nMinima = 0;
192  Int_t nMaxima = 0;
193  Int_t iStrip = 0;
194 
195  keyToIdStrip[0] = CurrentCluster->getFirstStrip();
196 
197  for(iStrip = 1; iStrip<CurrentClusterSize; iStrip++)
198  {
199  keyToIdStrip[iStrip] = keyToIdStrip[iStrip-1]+1;
200  }
201 
202  for(iStrip = 0; iStrip<CurrentClusterSize; iStrip++)
203  {
204  minima[iStrip] = 0;
205  maxima[iStrip] = 0;
206  }
207  iStrip = 0;
208 
209  for (iStrip = 0; iStrip<CurrentClusterSize-1; iStrip++) // we check both iStrip and iStrip+1
210  {
211  if (ListAdc[iStrip]<ListAdc[iStrip+1])
212  {
213  if ((isClimbOrFall == -1) || (isClimbOrFall == 0))
214  {
215  isClimbOrFall = 1;
216  minima[nMinima] = iStrip;
217  nMinima++;
218  }
219  if ((isClimbOrFall == 1) && (iStrip+2 == CurrentClusterSize))
220  {
221  maxima[nMaxima] = iStrip+1;
222  nMaxima++;
223  }
224  }
225  if (ListAdc[iStrip]>ListAdc[iStrip+1])
226  {
227  if ((isClimbOrFall == 1) || (isClimbOrFall == 0))
228  {
229  isClimbOrFall = -1;
230  maxima[nMaxima] = iStrip;
231  nMaxima++;
232  }
233  if ((isClimbOrFall == -1) && (iStrip+2 == CurrentClusterSize))
234  {
235  minima[nMinima] = iStrip+1;
236  nMinima++;
237  }
238  }
239  }
240 
241  if (nMaxima<2)
242  {
243  delete [] maxima;
244  delete [] minima;
245  delete [] keyToIdStrip;
246  return 0;
247  }
248  Int_t iMaxima = 0;
249  Int_t maxLeft = 0;
250  Int_t maxRight =0;
251  Int_t localFirstStrip = CurrentCluster->getFirstStrip();
252  Float_t weight = 1.;
253  Int_t strip_offset = 0;//JB : 01/08/2007 : update the current strip when the splitting of clusters is done
254  Int_t strip_diff = 0;
255  for (iMaxima = 0; iMaxima<nMaxima-1 ; iMaxima++)
256  {
257  maxLeft = maxima[iMaxima];
258  maxRight = maxima[iMaxima+1];
259  Int_t iMinima = 0;
260  while (!(minima[iMinima]>maxLeft && minima[iMinima]<maxRight) && iMinima<nMinima) iMinima++;
261 
262  if (iMinima == nMinima)
263  {
264  cout<<"Big Bug in StSsdClusterList -> I do not find any minima between the two maxima !"<<endl;
265  return 0;
266  }
267 
268  // process strip in the Left direction
269  Int_t theEnd = 0;
270  Int_t iStripLeft = minima[iMinima]-1;
271  Int_t iStripRight = minima[iMinima]+1;
272  Int_t iSuccess = 1;
273  Int_t addLeft = 0;
274  Int_t addRight = 0;
275  Int_t nStripInTheGroup = 1;
276  Float_t meanAdc = (float)ListAdc[minima[iMinima]];
277  localFirstStrip = CurrentCluster->getFirstStrip()+strip_offset;
278  weight = 1.;
279 
280  while(!theEnd)
281  {
282  if (fabs(((float)ListAdc[iStripLeft]-meanAdc)/meanAdc)<testTolerance)
283  {
284  if (iStripLeft > maxLeft)
285  {
286  addLeft = 1;
287  }
288  else
289  {
290  theEnd = 1;
291  iSuccess = 0;
292  }
293  }
294  if (fabs(((float)ListAdc[iStripRight]-meanAdc)/meanAdc)<testTolerance)
295  {
296  if (iStripRight < maxRight)
297  {
298  addRight = 1;
299  }
300  else
301  {
302  theEnd = 1;
303  iSuccess = 0;
304  }
305  }
306 
307  if ((!addLeft)&&(!addRight)) theEnd =1;
308  if (addLeft)
309  {
310  meanAdc = (((float)nStripInTheGroup)*meanAdc + (float)ListAdc[iStripLeft])/((float)(nStripInTheGroup+1));
311  nStripInTheGroup++;
312  iStripLeft--;
313  addLeft = 0;
314  }
315  if (addRight)
316  {
317  meanAdc = (((float)nStripInTheGroup)*meanAdc + (float)ListAdc[iStripRight])/((float)(nStripInTheGroup+1));
318  nStripInTheGroup++;
319  iStripRight++;
320  addRight = 0;
321  }
322 
323  }
324 
325  if (iSuccess)
326  {
327 
328  if (nStripInTheGroup%2==0)
329  {
330  StSsdCluster *newCluster = new StSsdCluster(this->getSize());
331  newCluster->setFlag(1);
332  Int_t currentStrip = localFirstStrip;
333  for (currentStrip = localFirstStrip; currentStrip<=(keyToIdStrip[iStripLeft]+(nStripInTheGroup/2)); currentStrip++)
334  {
335  newCluster->update(currentStripList->getStrip(currentStrip),weight);
336  weight=1.;
337  localFirstStrip=currentStrip+1;
338  strip_diff++;
339  }
340  this->addNewCluster(newCluster);
341  }
342  else
343  {
344  StSsdCluster *newCluster = new StSsdCluster(this->getSize());
345  newCluster->setFlag(1);
346  Int_t currentStrip = localFirstStrip;
347  for (currentStrip = localFirstStrip; currentStrip<=(keyToIdStrip[iStripLeft]+(int)(nStripInTheGroup/2)); currentStrip++)
348  {
349  newCluster->update(currentStripList->getStrip(currentStrip),weight);
350  weight=1.;
351  localFirstStrip=currentStrip+1;
352  strip_diff++;
353  }
354  weight=0.5;
355  newCluster->update(currentStripList->getStrip(localFirstStrip),weight);//last strip
356  this->addNewCluster(newCluster);
357  }
358  nSubCluster++;
359  }
360  strip_offset = strip_diff;
361  }
362 
363  if(nSubCluster)
364  {
365  StSsdCluster *newCluster = new StSsdCluster(this->getSize());
366  newCluster->setFlag(1);
367  Int_t currentStrip = localFirstStrip;
368  for (currentStrip = localFirstStrip; currentStrip<(CurrentCluster->getFirstStrip()+CurrentCluster->getClusterSize()); currentStrip++)
369  {
370  newCluster->update(currentStripList->getStrip(currentStrip),weight);
371  weight=1.;
372  }
373  this->addNewCluster(newCluster);
374  nSubCluster++;
375  }
376  delete [] maxima;
377  delete [] minima;
378  delete [] keyToIdStrip;
379  return nSubCluster;
380 }
381 
382 Int_t StSsdClusterList::isSorted()
383 {
384  StSsdCluster *ptr1 = this->first();
385  StSsdCluster *ptr2 = 0;
386 
387  while(ptr1 != this->last())
388  {
389  ptr2 = this->next(ptr1);
390  if (ptr1->getFirstStrip()>ptr2->getFirstStrip()) return 0;
391  ptr1 = this->next(ptr1);
392  }
393  return 1;
394 }
395 
396 StSsdClusterList::StSsdClusterList(const StSsdClusterList & originalClusterList)
397 {
398  mListLength = originalClusterList.mListLength;
399  mFirstCluster = originalClusterList.mFirstCluster;
400  mLastCluster = originalClusterList.mLastCluster;
401 }
402 
403 StSsdClusterList& StSsdClusterList::operator=(const StSsdClusterList originalClusterList)
404 {
405  mListLength = originalClusterList.mListLength;
406  mFirstCluster = originalClusterList.mFirstCluster;
407  mLastCluster = originalClusterList.mLastCluster;
408 
409  return *this;
410 }