StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
StFstScanRadiusClusterAlgo.cxx
1 #include "StEvent.h"
3 #include "StMessMgr.h"
4 #include "StFstScanRadiusClusterAlgo.h"
5 #include "StFstUtil/StFstCollection.h"
6 #include "StFstUtil/StFstRawHitCollection.h"
7 #include "StEvent/StFstRawHit.h"
8 #include "StFstUtil/StFstClusterCollection.h"
9 #include "StFstUtil/StFstCluster.h"
10 #include "StEvent/StFstConsts.h"
11 
12 #include <math.h>
13 #include <iostream>
14 #include <iterator>
15 #include <vector>
16 #include <algorithm>
17 
18 
19 Int_t StFstScanRadiusClusterAlgo::doClustering(const StFstCollection &fstCollection, StFstRawHitCollection &rawHitsOriginal, StFstClusterCollection &clusters )
20 {
21  StFstCluster *newCluster = 0;
22  StFstRawHit *rawHitTemp = 0;
23  StFstRawHit *rawHitMaxAdcTemp = 0;
24  Int_t clusterType = kFstScanRadiusClusterAlgo;
25  Int_t maxTb = -1, usedTb = -1;
26  Int_t disk = 0, wedge = 0, sensor = -1, apv = -1;
27  Float_t meanRStrip = -1., maxRStrip = -1, meanPhiStrip = -1;
28  Float_t totCharge = 0., totChargeErr = 0.;
29  Int_t clusterSize = 0, clusterSizeR = 0, clusterSizePhi = 0;
30  unsigned short idTruth = 0;
31 
32  //get number of time bin used in this event
33  Int_t nTimeBins = fstCollection.getNumTimeBins();
34 
35  //sort raw hits in increasing order by geometry ID
36  rawHitsOriginal.sortByGeoId();
37 
38  //copy raw hit collection to temporary vectors
39  std::vector<StFstRawHit *> rawHitsVec[kFstNumSensorsPerWedge][kFstNumPhiSegPerSensor];
40  std::vector<StFstCluster *> clustersVec[kFstNumSensorsPerWedge][kFstNumPhiSegPerSensor];
41  std::vector<StFstRawHit *> rawHitsToMerge;
42 
43  for (int sensorIdx = 0; sensorIdx < kFstNumSensorsPerWedge; sensorIdx++) {
44  for (int phiIdx = 0; phiIdx < kFstNumPhiSegPerSensor; phiIdx++) {
45  rawHitsVec[sensorIdx][phiIdx].reserve(kFstNumRStripsPerSensor);
46  clustersVec[sensorIdx][phiIdx].reserve(kFstNumRStripsPerSensor);
47  }
48  }
49 
50  rawHitsToMerge.reserve(kFstNumRStripsPerSensor);
51 
52  for (std::vector< StFstRawHit * >::iterator rawHitPtr = rawHitsOriginal.getRawHitVec().begin(); rawHitPtr != rawHitsOriginal.getRawHitVec().end(); ++rawHitPtr) {
53  int sensorIndex = (int)(*rawHitPtr)->getSensor();
54  int phiIndex = (int)(*rawHitPtr)->getPhiStrip();
55  rawHitsVec[sensorIndex][phiIndex].push_back( new StFstRawHit( *(*rawHitPtr)) );
56  }
57 
58  //do clustering
59  int clusterLabel = 0;
60 
61  for (int sensorIdx = 0; sensorIdx < kFstNumSensorsPerWedge; sensorIdx++)
62  {
63  //step 1: do clustering for each phi
64  for (int phiIdx = 0; phiIdx < kFstNumPhiSegPerSensor; phiIdx++)
65  {
66  while ( !rawHitsVec[sensorIdx][phiIdx].empty() )
67  {
68  rawHitTemp = rawHitsVec[sensorIdx][phiIdx].back();
69  delete rawHitsVec[sensorIdx][phiIdx].back();
70  rawHitsVec[sensorIdx][phiIdx].pop_back();
71  rawHitsToMerge.push_back(rawHitTemp);
72  //count number to merge
73  int nToMerge = 1;
74  //find all raw hits that are neighboring
75  std::vector<StFstRawHit *>::iterator rawHitsToMergePtr = rawHitsToMerge.begin();
76  rawHitMaxAdcTemp = *rawHitsToMergePtr;
77 
78  // put all raw hits in one phi strip to rawHitsToMerge
79  while (rawHitsToMergePtr != rawHitsToMerge.end() && !rawHitsVec[sensorIdx][phiIdx].empty()) {
80  rawHitTemp = rawHitsVec[sensorIdx][phiIdx].back();
81  delete rawHitsVec[sensorIdx][phiIdx].back();
82  rawHitsVec[sensorIdx][phiIdx].pop_back();
83 
84  ++nToMerge;
85  rawHitsToMerge.push_back(rawHitTemp);
86 
87  if ( rawHitTemp->getCharge(rawHitTemp->getMaxTimeBin()) > (*rawHitsToMergePtr)->getCharge((*rawHitsToMergePtr)->getMaxTimeBin()) )
88  rawHitMaxAdcTemp = rawHitTemp;
89 
90  ++rawHitsToMergePtr;
91  }
92 
93  //used time bin index (raw hits with maximum ADC holds the time-bin priority)
94  maxTb = rawHitMaxAdcTemp->getMaxTimeBin();
95  idTruth = rawHitMaxAdcTemp->getIdTruth();
96 
97  if (maxTb < 0 || maxTb >= nTimeBins) maxTb = rawHitMaxAdcTemp->getDefaultTimeBin();
98 
99  if (mTimeBin < nTimeBins) usedTb = mTimeBin;
100  else usedTb = maxTb;
101 
102  disk = rawHitMaxAdcTemp->getDisk();
103  wedge = rawHitMaxAdcTemp->getWedge();
104  sensor = rawHitMaxAdcTemp->getSensor(); // = sensorIdx
105  apv = rawHitMaxAdcTemp->getApv();
106  meanPhiStrip = (float)rawHitMaxAdcTemp->getPhiStrip(); // = phiIdx
107  clusterSize = nToMerge;
108  clusterSizeR = nToMerge;
109  clusterSizePhi = 1;
110 
111  float tempChargeErr[nToMerge], tempRStrip[nToMerge];
112 
113  for (int i = 0; i < nToMerge; i++) {
114  tempChargeErr[i] = 0.; tempRStrip[i] = 0.;
115  }
116 
117  float tempSumCharge = 0, tempSumChargeErrSquare = 0.;
118  int mergeIdx = 0;
119  //count number to seed hit
120  int nToSeedhit = 0;
121 
122  for (rawHitsToMergePtr = rawHitsToMerge.begin(); rawHitsToMergePtr != rawHitsToMerge.end() && mergeIdx < nToMerge; ++rawHitsToMergePtr, ++mergeIdx) {
123  tempChargeErr[mergeIdx] = (*rawHitsToMergePtr)->getChargeErr(usedTb);
124  tempRStrip[mergeIdx] = (float)(*rawHitsToMergePtr)->getRStrip();
125  tempSumCharge += (*rawHitsToMergePtr)->getCharge(usedTb);
126  if ( (*rawHitsToMergePtr)->getSeedhitflag() == 1 ) ++nToSeedhit;
127  }
128 
129  meanRStrip = 0;
130  maxRStrip = 0;
131 
132  for (int iRawHit = 0; iRawHit < nToMerge; iRawHit++) {
133  if(tempRStrip[iRawHit]>maxRStrip)
134  maxRStrip = tempRStrip[iRawHit];
135  meanRStrip = maxRStrip;
136  tempSumChargeErrSquare += tempChargeErr[iRawHit] * tempChargeErr[iRawHit];
137  }
138 
139  totCharge = tempSumCharge;
140  totChargeErr = sqrt(tempSumChargeErrSquare / nToMerge);
141 
142  if(nToSeedhit>0) {
143  newCluster = new StFstCluster((int)wedge * 10000 + clusterLabel, disk, wedge, sensor, apv, meanRStrip, meanPhiStrip, totCharge, totChargeErr, clusterType);
144  newCluster->setNRawHits(clusterSize);
145  newCluster->setNRawHitsR(clusterSizeR);
146  newCluster->setNRawHitsPhi(clusterSizePhi);
147  newCluster->setMaxTimeBin(maxTb);
148  newCluster->setIdTruth(idTruth);
149 
150  clustersVec[sensorIdx][phiIdx].push_back(newCluster);
151  clusterLabel++;
152  }
153 
154  rawHitsToMerge.clear();
155  }//end current phi raw hits loop
156  }//end current sensor raw hits loop
157 
158  //step 2: do clustering for neighboring phistrips
159  std::vector<StFstCluster *>::iterator clusterIt1, clusterIt2;
160  for (int phiIdx1 = 0; phiIdx1 < kFstNumPhiSegPerSensor - 1; phiIdx1++) {
161  int phiIdx2 = phiIdx1 + 1;
162 
163  if (clustersVec[sensorIdx][phiIdx1].size() > 0 && clustersVec[sensorIdx][phiIdx2].size() > 0) {
164  for (clusterIt1 = clustersVec[sensorIdx][phiIdx1].begin(); clusterIt1 != clustersVec[sensorIdx][phiIdx1].end() && !clustersVec[sensorIdx][phiIdx1].empty(); clusterIt1++) {
165  for (clusterIt2 = clustersVec[sensorIdx][phiIdx2].begin(); clusterIt2 != clustersVec[sensorIdx][phiIdx2].end() && !clustersVec[sensorIdx][phiIdx1].empty(); clusterIt2++) {
166  float rstripDfstance = (*clusterIt1)->getMeanRStrip() - (*clusterIt2)->getMeanRStrip();
167 
168  if (TMath::Abs(rstripDfstance) < 3.5) { //here 3.5 means the dfstance between two clusters' weighted centers in r direction smaller than 3.5 rStrip
169  maxTb = (*clusterIt1)->getMaxTimeBin();
170  idTruth = (*clusterIt1)->getIdTruth();
171  apv = (*clusterIt1)->getApv();
172  if((*clusterIt1)->getTotCharge() < (*clusterIt2)->getTotCharge()) {
173  maxTb = (*clusterIt2)->getMaxTimeBin();
174  idTruth = (*clusterIt2)->getIdTruth();
175  apv = (*clusterIt2)->getApv();
176  }
177 
178  totCharge = (*clusterIt1)->getTotCharge() + (*clusterIt2)->getTotCharge();
179  totChargeErr = sqrt(((*clusterIt1)->getTotChargeErr() * (*clusterIt1)->getTotChargeErr() * (*clusterIt1)->getNRawHits() + (*clusterIt2)->getTotChargeErr() * (*clusterIt2)->getTotChargeErr() * (*clusterIt2)->getNRawHits()) / ((*clusterIt1)->getNRawHits() + (*clusterIt2)->getNRawHits()));
180  clusterSize = (*clusterIt1)->getNRawHits() + (*clusterIt2)->getNRawHits();
181  int maxClusterR1 = (*clusterIt1)->getMeanRStrip();
182  int minClusterR1 = (*clusterIt1)->getMeanRStrip() - (*clusterIt1)->getNRawHitsR() + 1;
183  int maxClusterR2 = (*clusterIt2)->getMeanRStrip();
184  int minClusterR2 = (*clusterIt2)->getMeanRStrip() - (*clusterIt2)->getNRawHitsR() + 1;
185  int maxClusterR = maxClusterR1 > maxClusterR2 ? maxClusterR1 : maxClusterR2;
186  int minClusterR = minClusterR1 < minClusterR2 ? minClusterR1 : minClusterR2;
187  clusterSizeR = maxClusterR - minClusterR + 1; // max at 4
188  clusterSizePhi = (*clusterIt1)->getNRawHitsPhi() + (*clusterIt2)->getNRawHitsPhi();
189  meanRStrip = (*clusterIt1)->getMeanRStrip();
190  if ((*clusterIt2)->getMeanRStrip() > (*clusterIt1)->getMeanRStrip())
191  meanRStrip = (*clusterIt2)->getMeanRStrip();
192  meanPhiStrip = (*clusterIt1)->getMeanPhiStrip() * (*clusterIt1)->getTotCharge() / totCharge + (*clusterIt2)->getMeanPhiStrip() * (*clusterIt2)->getTotCharge() / totCharge;
193 
194  (*clusterIt2)->setMeanRStrip(meanRStrip);
195  (*clusterIt2)->setMeanPhiStrip(meanPhiStrip);
196  (*clusterIt2)->setTotCharge(totCharge);
197  (*clusterIt2)->setTotChargeErr(totChargeErr);
198  (*clusterIt2)->setNRawHits(clusterSize);
199  (*clusterIt2)->setNRawHitsR(clusterSizeR);
200  (*clusterIt2)->setNRawHitsPhi(clusterSizePhi);
201  (*clusterIt2)->setIdTruth(idTruth);
202  (*clusterIt2)->setApv(apv);
203 
204  int distance1 = std::distance(clustersVec[sensorIdx][phiIdx1].begin(), clusterIt1);
205  delete *clusterIt1;
206  clustersVec[sensorIdx][phiIdx1].erase(clusterIt1);
207 
208  if (distance1 == 0)
209  clusterIt1 = clustersVec[sensorIdx][phiIdx1].begin();
210  else
211  --clusterIt1;
212  }//end merge
213  }//end clusters vector 2 loop
214  }//end clusters vector 1 loop
215  }//end phi cluster number cut
216  }//end current sensor cluster loop
217  }//end all sensor clustering loop
218 
219  //fill output container
220  std::vector<StFstCluster *>::iterator clusterIt;
221 
222  for (int sensorIdx = 0; sensorIdx < kFstNumSensorsPerWedge; sensorIdx++) {
223  for (int phiIdx = 0; phiIdx < kFstNumPhiSegPerSensor; phiIdx++) {
224  if (clustersVec[sensorIdx][phiIdx].size() <= 0) continue;
225 
226  for (clusterIt = clustersVec[sensorIdx][phiIdx].begin(); clusterIt != clustersVec[sensorIdx][phiIdx].end(); ++clusterIt)
227  clusters.getClusterVec().push_back(*clusterIt);
228 
229  rawHitsVec[sensorIdx][phiIdx].clear();
230  clustersVec[sensorIdx][phiIdx].clear();
231  }
232  }
233 
234  return kStOk;
235 }
unsigned char getSensor() const
0-2
Definition: StFstRawHit.cxx:94
unsigned char getDisk() const
1-3
Definition: StFstRawHit.cxx:57
unsigned char getPhiStrip() const
0-127
Definition: StFstRawHit.cxx:67
unsigned char getApv() const
0-15
Definition: StFstRawHit.cxx:89
unsigned short getIdTruth() const
for embedding, 0 as background
Definition: StFstRawHit.cxx:55
Definition: Stypes.h:41
unsigned char getWedge() const
1-36
Definition: StFstRawHit.cxx:62