00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #include <stdio.h>
00014 #include <stdlib.h>
00015
00016 #include "TrkReco/THistogram.h"
00017 #include "TrkReco/TCircle.h"
00018
00019 THistogram::THistogram(unsigned nBins) : _nBins(nBins) {
00020 _binSize = 2. * M_PI / (float) _nBins;
00021 _bins = (unsigned *) malloc(_nBins * sizeof(unsigned));
00022 _masks = (bool *) malloc(_nBins * sizeof(bool));
00023 _links = (AList<TMLink> **) malloc(_nBins * sizeof(AList<TMLink> *));
00024 for (unsigned i = 0; i < _nBins; i++) {
00025 _bins[i] = 0;
00026 _masks[i] = false;
00027 _links[i] = new AList<TMLink>;
00028 }
00029 }
00030
00031 THistogram::~THistogram() {
00032 free(_bins);
00033 free(_masks);
00034 for (unsigned i = 0; i < _nBins; i++)
00035 delete _links[i];
00036 free(_links);
00037 }
00038
00039 void
00040 THistogram::dump(const std::string & msg, const std::string & pre) const {
00041 std::cout << pre;
00042 std::cout << "THistogram dump:#bins=" << _nBins << std::endl;
00043 unsigned nLoops = _nBins / 15 + 1;
00044 unsigned n0 = 0;
00045 unsigned n1 = 0;
00046 for (unsigned i = 0; i < nLoops; i++) {
00047 for (unsigned j = 0; j < 15; j++) {
00048 if (n0 == _nBins) break;
00049 printf("%4d", n0);
00050 ++n0;
00051 }
00052 std::cout << std::endl;
00053 for (unsigned j = 0; j < 15; j++) {
00054 if (n1 == _nBins) break;
00055 if (! _masks[n1]) printf("%4d", _bins[n1]);
00056 else printf("-%3d", _bins[n1]);
00057 ++n1;
00058 }
00059 std::cout << std::endl;
00060 }
00061
00062 if (msg.find("detail") != std::string::npos) {
00063 for (unsigned i = 0; i < _nBins; i++) {
00064 std::cout << "bin " << i << " : ";
00065 for (unsigned j = 0; j < _links[i]->length(); j++) {
00066 std::cout << (* _links[i])[j]->wire()->name() << ",";
00067 }
00068 std::cout << std::endl;
00069 }
00070 }
00071
00072 return;
00073 }
00074
00075 void
00076 THistogram::fillX(const AList<TMLink> & links) {
00077 _all = (AList<TMLink> &) links;
00078 unsigned nLinks = links.length();
00079 double offset = _binSize / 4.;
00080 for (unsigned i = 0; i < nLinks; i++) {
00081 TMLink * l = links[i];
00082 const HepPoint3D & p = l->position();
00083 unsigned pos = (unsigned) floor((p.x() + offset) / _binSize);
00084
00085
00086 pos %= _nBins;
00087
00088 ++_bins[pos];
00089 _links[pos]->append(l);
00090 }
00091 }
00092
00093 void
00094 THistogram::fillY(const AList<TMLink> & links) {
00095 _all = (AList<TMLink> &) links;
00096 unsigned nLinks = links.length();
00097 for (unsigned i = 0; i < nLinks; i++) {
00098 TMLink * l = links[i];
00099 const HepPoint3D & p = l->position();
00100 unsigned pos = (unsigned) floor(p.y() / _binSize);
00101
00102
00103 pos %= _nBins;
00104
00105 ++_bins[pos];
00106 _links[pos]->append(l);
00107 }
00108 }
00109
00110 void
00111 THistogram::fillPhi(const AList<TMLink> & links) {
00112 _all = (AList<TMLink> &) links;
00113 unsigned nLinks = links.length();
00114 double offset = _binSize / 4.;
00115 for (unsigned i = 0; i < nLinks; i++) {
00116 TMLink * l = links[i];
00117 const HepPoint3D & p = l->position();
00118 float phi = atan2(p.y(), p.x()) + M_PI;
00119
00120 unsigned pos = (unsigned) floor((phi + offset) / _binSize);
00121
00122
00123
00124
00125
00126 pos %= _nBins;
00127
00128 ++_bins[pos];
00129 _links[pos]->append(l);
00130 }
00131 }
00132
00133 void
00134 THistogram::remove(const AList<TMLink> & links) {
00135 for (unsigned i = 0; i < _nBins; i++) {
00136 _links[i]->remove(links);
00137 _bins[i] = _links[i]->length();
00138 }
00139 _all.remove(links);
00140 }
00141
00142 AList<TMLink>
00143 THistogram::contents(unsigned center, unsigned width) const {
00144 AList<TMLink> links;
00145 for (int i = - (int) width;
00146 i <= (int) width;
00147 i++) {
00148 links.append(* bin((int) center + i));
00149 }
00150 return links;
00151 }
00152
00153 AList<TMLink>
00154 THistogram::contents(int start, int end) const {
00155 AList<TMLink> links;
00156 for (int i = start; i <= end; i++)
00157 links.append(* bin(i));
00158 return links;
00159 }
00160
00161 AList<TSegment0>
00162 THistogram::clusters0(void) const {
00163 AList<TSegment0> list;
00164
00165
00166 unsigned begin = 0;
00167 while (_bins[begin] > 0) begin++;
00168 if (begin == _nBins) return list;
00169
00170
00171 unsigned loop = 0;
00172 while (loop < _nBins) {
00173 ++loop;
00174 unsigned id = (begin + loop) % _nBins;
00175 if (_bins[id]) {
00176 unsigned size = 0;
00177 TSegment0 * c = new TSegment0();
00178 while (_bins[id]) {
00179 if (_bins[id]) ++size;
00180 c->append(* _links[id]);
00181 ++loop;
00182 id = (begin + loop) % _nBins;
00183 if (loop == _nBins) break;
00184 }
00185 list.append(c);
00186 }
00187 }
00188 return list;
00189 }
00190
00191 AList<TSegment0>
00192 THistogram::segments0(void) const {
00193
00194
00195 AList<TSegment0> list = clusters0();
00196 unsigned n = list.length();
00197 if (n == 0) return list;
00198
00199
00200 AList<TSegment0> splitted;
00201 for (unsigned i = 0; i < n; i++) {
00202 TSegment0 * c = list[i];
00203
00204 AList<TSegment0> newClusters = c->split();
00205 if (newClusters.length() == 0) {
00206 c->solveDualHits();
00207 continue;
00208 }
00209
00210 list.append(newClusters);
00211 splitted.append(c);
00212 #ifdef TRKRECO_DEBUG_DETAIL
00213 c->dump("hits", " ");
00214 std::cout << " ... splitted as" << std::endl;
00215 for (unsigned j = 0; j < newClusters.length(); j++) {
00216 std::cout << " " << j << " : ";
00217 newClusters[j]->dump("hits");
00218 }
00219 #endif
00220 }
00221 list.remove(splitted);
00222 HepAListDeleteAll(splitted);
00223
00224 return list;
00225
00226 }
00227
00228 AList<TSegment>
00229 THistogram::clusters(void) const {
00230 AList<TSegment> list;
00231
00232
00233 unsigned begin = 0;
00234 while (_bins[begin] > 0)begin++;
00235 if (begin == _nBins) return list;
00236
00237
00238 unsigned loop = 0;
00239 while (loop < _nBins) {
00240 ++loop;
00241 unsigned id = (begin + loop) % _nBins;
00242 if (_bins[id]) {
00243 unsigned size = 0;
00244 TSegment * c = new TSegment();
00245 while (_bins[id]) {
00246 if (_bins[id]) ++size;
00247 c->append(* _links[id]);
00248 ++loop;
00249 id = (begin + loop) % _nBins;
00250 if (loop == _nBins) break;
00251 }
00252 list.append(c);
00253 }
00254 }
00255 return list;
00256 }
00257
00258 AList<TSegment>
00259 THistogram::segments(void) const {
00260
00261
00262
00263
00264
00265
00266
00267 AList<TSegment> list = clusters();
00268 unsigned n = list.length();
00269 if (n == 0) return list;
00270
00271
00272 AList<TSegment> splitted;
00273 for (unsigned i = 0; i < n; i++) {
00274 TSegment * c = list[i];
00275
00276 #ifdef TRKRECO_DEBUG_DETAIL
00277 std::cout << " base segment : ";
00278 c->dump("hits");
00279 #endif
00280
00281 AList<TSegment> newClusters = c->split();
00282 if (newClusters.length() == 0) {
00283 #ifdef TRKRECO_DEBUG_DETAIL
00284 std::cout << " ... Solving dual hits" << std::endl;
00285 #endif
00286 c->solveDualHits();
00287 continue;
00288 }
00289
00290 list.append(newClusters);
00291 splitted.append(c);
00292 #ifdef TRKRECO_DEBUG_DETAIL
00293 c->dump("hits", " ");
00294 std::cout << " ... splitted as" << std::endl;
00295 for (unsigned j = 0; j < newClusters.length(); j++) {
00296 std::cout << " " << j << " : ";
00297 newClusters[j]->dump("hits");
00298 }
00299 #endif
00300 }
00301 list.remove(splitted);
00302 HepAListDeleteAll(splitted);
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312 return list;
00313 }