ATLAS Offline Software
TRT_TrackSegmentsUtils_xk.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 // Header file for class TRT_TrackSegmentsUtils_xk
8 // (c) ATLAS Detector software
10 // Class for straight line finder
12 // Version 1.0 3/10/2004 I.Gavrilenko
14 
15 #ifndef TRT_TrackSegmentsUtils_xk_H
16 #define TRT_TrackSegmentsUtils_xk_H
17 
18 #include <new>
19 #include <cmath>
20 #include <algorithm>
21 
22 namespace InDet{
23 
25  {
27  // Public methods:
29 
30  public:
31 
34 
36  // Main methods
38 
39  const int& size () const {return m_size;}
40  void initiate(int);
41  template<class T> void findStraightLine(int,T*,const T*,const T*);
42  template<class T> void sort(T*,int*,int n);
43 
44  protected:
45 
47  // Protected Data
49 
50  int m_size;
51  int* m_NA ;
52  int* m_MA ;
53  double* m_F ;
54 
56  // Methods
58 
59  };
60 
62  // Inline methods
64 
66  {
67  m_size = 0;
68  m_NA = 0;
69  m_MA = 0;
70  m_F = 0;
71  }
72 
74  {
75  if(n<=0) return;
76 
77  if(m_size) {
78 
79  delete[] m_NA;
80  delete[] m_MA;
81  delete[] m_F ;
82  }
83  m_size = n;
84  m_NA = new int [n];
85  m_MA = new int [n];
86  m_F = new double[n];
87  }
88 
90  {
91  if(!m_size) return;
92  delete[] m_NA;
93  delete[] m_MA;
94  delete[] m_F ;
95  }
96 
98  // Search a stright line Y = A*X+B crossed max number segments
99  // Input parameters: X - X-coordinates of the segment boundaries
100  // Y - Y-coordinates ------
101  // par[0] - initial A
102  // par[1] - initial B
103  // par[2] - boundary of A abs(A)<DA
104  //
105  // Output parameters : A,B - stright line parameters Y = A*X+B
106  // Program try to find a stright line which cross a segments a such a way that
107  // total number of all crossed segments to get max value
109 
111  (int Np, Tem* par, const Tem* X, const Tem* Y)
112  {
113  if(m_size<2 || Np<2 ) return;
114  if(Np>m_size) Np = m_size;
115 
116  Tem A = par[0];
117  Tem B = par[1];
118  Tem Amax = fabs(par[2]);
119 
120  int i=0;
121  while(i!=Np) {
122  m_NA[i]=i; m_F[i]=Y[i]; ++i;
123  m_NA[i]=i; m_F[i]=Y[i]; ++i;
124  }
125 
126  sort(m_F,m_NA,Np);
127 
128  // Search B with A = par[0];
129  //
130  i=0; int sm=0, s=0, l=0;
131  while(i!=Np) {
132  if((m_NA[i ]&1)==(m_MA[i ]=0)) {if(++s>sm) {sm=s; l= i;}} else --s;
133  if((m_NA[i+1]&1)==(m_MA[i+1]=0)) {if(++s>sm) {sm=s; l=1+i;}} else --s; i+=2;
134  }
135  B = .5*(Y[m_NA[l]]+Y[m_NA[l+1]]);
136 
137  // Search A and B
138  //
139  while(1) {
140 
141  i=(l=m_NA[l]); Tem u0=X[l], v0=Y[l];
142 
143  // Vector slopes preparation
144  //
145 
146  while(++i!=Np) {if(X[i]!=u0) break;} Tem U1=-1000., d=0.; int m=0;
147  while(i<Np-1) {
148 
149  if (X[i]==U1) {
150  if(fabs(m_F[m]=(Y[i ]-v0)*d)<Amax) m_NA[m++]=i;
151  if(fabs(m_F[m]=(Y[i+1]-v0)*d)<Amax) m_NA[m++]=i+1;
152  }
153  else {
154  d=1./((U1=X[i])-u0);
155  if(fabs(m_F[m]=(Y[i ]-v0)*d)<Amax) m_NA[m++]=i;
156  if(fabs(m_F[m]=(Y[i+1]-v0)*d)<Amax) m_NA[m++]=i+1;
157  }
158  i+=2;
159  }
160  (i=l); while(--i>0) {if(X[i]!=u0) break;} U1=-1000.;
161  while(i>0) {
162  if (X[i]==U1) {
163  if(fabs(m_F[m]=(Y[i ]-v0)*d)<Amax) m_NA[m++]=i;
164  if(fabs(m_F[m]=(Y[i-1]-v0)*d)<Amax) m_NA[m++]=i-1;
165 
166  }
167  else {
168  d=1./((U1=X[i])-u0);
169  if(fabs(m_F[m]=(Y[i ]-v0)*d)<Amax) m_NA[m++]=i;
170  if(fabs(m_F[m]=(Y[i-1]-v0)*d)<Amax) m_NA[m++]=i-1;
171  }
172  i-=2;
173  }
174 
175  if(m<=4) break;
176  sort(m_F,m_NA,m);
177 
178  int nm = 0; s = 0; sm=-1000;
179  for(int i=0; i!=m-1; ++i) {
180  int na = m_NA[i];
181  if((na&1)==0) {if(na>l) {if(++s>sm) {sm=s; nm=i;}} else --s;}
182  else {if(na<l) {if(++s>sm) {sm=s; nm=i;}} else --s;}
183  }
184  if(nm==0) break;
185  B = v0-(A=.5*(m_F[nm]+m_F[nm+1]))*u0; m_MA[l]=1;
186  if (m_MA[m_NA[nm ]]==0) l = nm;
187  else if(m_MA[m_NA[nm+1]]==0) l = nm+1;
188  else break;
189  }
190  par[0] = A;
191  par[1] = B;
192  }
193 
195  // The procedure sorts the elements of the array A(1:N)
196  // into ascending order. Algorithm 271, Comm., ACM(1965) p.669
197  //
198  // Input parameters : a - one-dimension array of elements
199  // to be sorted
200  // b - one-dimension array of elements
201  // to be sorted together with a
202  // n - number of words to be sorted
204 
205  template<class Tem> void TRT_TrackSegmentsUtils_xk::sort(Tem* a,int* b,int n) {
206 
207  if(n<=1) return;
208  int mt[50],lt[50], j=n-1, i=0, m=0;
209  while (1) {
210  if(j-i>1) {
211  int ip=(j+i)>>1, q=j; Tem ta=a[ip]; a[ip]=a[i]; int tb=b[ip]; b[ip]=b[i];
212  for(int k =i+1; k<=q; ++k) {
213  if(a[k]<=ta) continue;
214  int l=q;
215  while(1) {
216  if(a[l]<ta) {
217  Tem x=a[k]; a[k]=a[l]; a[l]=x;
218  int y=b[k]; b[k]=b[l]; b[l]=y; q=l-1; break;
219  }
220  if(--l< k) {q=k-1; break;}
221  }
222  }
223  a[i]=a[q]; a[q]=ta; b[i]=b[q]; b[q]=tb;
224  if((q<<1)>i+j) {lt[m]=i; mt[m++]=q-1; i=q+1;}
225  else {lt[m]=q+1; mt[m++]=j; j=q-1;}
226  }
227  else {
228  if(i<j && a[i]>a[j]) {
229  Tem x=a[i]; a[i]=a[j]; a[j]=x;
230  int y=b[i]; b[i]=b[j]; b[j]=y;
231  }
232  if(--m<0) return;
233  i=lt[m]; j=mt[m];
234  }
235  }
236  }
237 } // end of name space
238 
239 #endif // TRT_TrackSegmentsUtils_xk
InDet::TRT_TrackSegmentsUtils_xk::size
const int & size() const
Definition: TRT_TrackSegmentsUtils_xk.h:39
InDet::TRT_TrackSegmentsUtils_xk
Definition: TRT_TrackSegmentsUtils_xk.h:25
python.SystemOfUnits.s
int s
Definition: SystemOfUnits.py:131
python.SystemOfUnits.m
int m
Definition: SystemOfUnits.py:91
InDet::TRT_TrackSegmentsUtils_xk::m_MA
int * m_MA
Definition: TRT_TrackSegmentsUtils_xk.h:52
hist_file_dump.d
d
Definition: hist_file_dump.py:137
InDet
Primary Vertex Finder.
Definition: VP1ErrorUtils.h:36
UploadAMITag.l
list l
Definition: UploadAMITag.larcaf.py:158
x
#define x
InDet::TRT_TrackSegmentsUtils_xk::TRT_TrackSegmentsUtils_xk
TRT_TrackSegmentsUtils_xk()
Definition: TRT_TrackSegmentsUtils_xk.h:65
InDet::TRT_TrackSegmentsUtils_xk::findStraightLine
void findStraightLine(int, T *, const T *, const T *)
Monitored::X
@ X
Definition: HistogramFillerUtils.h:24
Execution.tb
tb
Definition: Execution.py:15
A
InDet::TRT_TrackSegmentsUtils_xk::~TRT_TrackSegmentsUtils_xk
~TRT_TrackSegmentsUtils_xk()
Definition: TRT_TrackSegmentsUtils_xk.h:89
parseMapping.v0
def v0
Definition: parseMapping.py:149
lumiFormat.i
int i
Definition: lumiFormat.py:85
beamspotman.n
n
Definition: beamspotman.py:731
find_tgc_unfilled_channelids.ip
ip
Definition: find_tgc_unfilled_channelids.py:3
InDet::TRT_TrackSegmentsUtils_xk::initiate
void initiate(int)
Definition: TRT_TrackSegmentsUtils_xk.h:73
Monitored::Y
@ Y
Definition: HistogramFillerUtils.h:24
createCoolChannelIdFile.par
par
Definition: createCoolChannelIdFile.py:29
plotBeamSpotMon.b
b
Definition: plotBeamSpotMon.py:77
InDet::TRT_TrackSegmentsUtils_xk::m_F
double * m_F
Definition: TRT_TrackSegmentsUtils_xk.h:53
a
TList * a
Definition: liststreamerinfos.cxx:10
CalibCoolCompareRT.nm
nm
Definition: CalibCoolCompareRT.py:110
y
#define y
extractSporadic.q
list q
Definition: extractSporadic.py:98
InDet::TRT_TrackSegmentsUtils_xk::sort
void sort(T *, int *, int n)
InDet::TRT_TrackSegmentsUtils_xk::m_size
int m_size
Definition: TRT_TrackSegmentsUtils_xk.h:50
fitman.k
k
Definition: fitman.py:528
InDet::TRT_TrackSegmentsUtils_xk::m_NA
int * m_NA
Definition: TRT_TrackSegmentsUtils_xk.h:51