test
CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
DeviationsFromFileSensor2D.cc
Go to the documentation of this file.
1 // COCOA class implementation file
2 //Id: DeviationsFromFileSensor2D.cc
3 //CAT: Model
4 //
5 // History: v1.0
6 // Pedro Arce
7 
11 #include <cstdlib>
12 #include <cmath> // include floating-point std::abs functions
13 
14 enum directions{ xdir = 0, ydir = 1};
15 
17 
18 
19 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
21 {
23  // verbose = 4;
24  if( verbose >= 3) std::cout << "DeviationsFromFileSensor2D::readFile " << this << ifdevi.name() << std::endl;
25 
26  theScanSenseX = 0;
27  theScanSenseY = 0;
28 
29  ALIuint nl = 1;
30 
31  ALIdouble oldposX=0, oldposY=0;
32  vd vcol;
33  std::vector<ALIstring> wl;
34  /* //------ first line with dimension factors //now with global option
35  ifdevi.getWordsInLine( wl );
36  if( wl[0] != "DIMFACTOR:" || wl.size() != 3) {
37  std::cerr << "Error reading sensor2D deviation file " << ifdevi.name() << std::endl
38  << " first line has to be DIMFACTOR: 'posDimFactor' 'angDimFactor' " << std::endl;
39  ALIUtils::dumpVS( wl, " ");
40  exit(1);
41  }
42  ALIdouble posDimFactor = atof(wl[1].c_str());
43  ALIdouble angDimFactor = atof(wl[2].c_str());
44  */
45 
46  for(;;) {
47  ifdevi.getWordsInLine( wl );
48  if(ifdevi.eof() ) break;
49 
51  dev->fillData( wl );
52 
53  if(verbose >= 5) {
54  ALIUtils::dumpVS( wl, "deviation sensor2D", std::cout );
55  }
56 
57  //--- line 2 of data
58  if(nl == 2) {
59  //--------- get if scan is first in Y or X
61  if(verbose >= 3) std::cout << "firstScanDir " << firstScanDir << " " << dev->posX() << " " << oldposX << " " << dev->posY() << " " << oldposY << std::endl;
62  if( std::abs( dev->posX() - oldposX ) > std::abs( dev->posY() - oldposY ) ) {
63  std::cerr << "!!!EXITING first scan direction has to be Y for the moment " << ifdevi.name() << std::endl;
65  exit(1);
66  }
67  //-------- get sense of first scan direction
68  if(firstScanDir == ydir ){
69  if( dev->posY() > oldposY ) {
70  theScanSenseY = +1;
71  } else {
72  theScanSenseY = -1;
73  }
74  if( verbose >= 3 ) std::cout << " theScanSenseY " << theScanSenseY << std::endl;
75  }else {
76  if( dev->posX() > oldposX ) {
77  theScanSenseX = +1;
78  } else {
79  theScanSenseX = -1;
80  }
81  if( verbose >= 3 ) std::cout << " theScanSenseX " << theScanSenseX << std::endl;
82  }
83  }
84 
85  //- std::cout << "firstScanDir " << firstScanDir << " " << dev->posX() << " " << oldposX << " " << dev->posY() << " " << oldposY << std::endl;
86 
87  //------- check if change of row: clear current std::vector of column values
88  if( ( firstScanDir == ydir && ( dev->posY() - oldposY)*theScanSenseY < 0 )
89  || ( firstScanDir == xdir && ( dev->posX() - oldposX)*theScanSenseX < 0 )) {
90  theDeviations.push_back( vcol );
91  vcol.clear();
92 
93  //- std::cout << " theDevi size " << theDeviations.size() << " " << ifdevi.name() << std::endl;
94  //-------- get sense of second scan direction
95  if( theScanSenseY == 0 ) {
96  if( dev->posY() > oldposY ) {
97  theScanSenseY = +1;
98  } else {
99  theScanSenseY = -1;
100  }
101  }
102  if( theScanSenseX == 0 ) {
103  if( dev->posX() > oldposX ) {
104  theScanSenseX = +1;
105  } else {
106  theScanSenseX = -1;
107  }
108  }
109  if( verbose >= 3 ) std::cout << " theScanSenseX " << theScanSenseX << " theScanSenseY " << theScanSenseY << std::endl;
110  }
111 
112  oldposX = dev->posX();
113  oldposY = dev->posY();
114 
115  //--- fill deviation std::vectors
116  vcol.push_back( dev );
117  nl++;
118  }
119  theDeviations.push_back( vcol );
120 
121  //----- Calculate std::vector size
122  ALIdouble dvsiz = ( sqrt( ALIdouble(nl-1) ) );
123  //----- Check that it is a square of points (<=> vsiz is integer)
124  ALIuint vsiz = ALIuint(dvsiz);
125  if( vsiz != dvsiz ) {
126  if( ALIUtils::debug >= 0) std::cerr << "!!WARNING: error reading deviation file: number of X points <> number of Y points : Number of points in X " << dvsiz << " nl " << nl-1 << " file " << ifdevi.name() << std::endl;
127  // exit(1);
128  }
129  theNPoints = vsiz;
130 
131  if(verbose >= 4 ) {
132  std::cout << " Filling deviation from file: " << ifdevi.name() << " theNPoints " << theNPoints << std::endl;
133  }
134 
135 }
136 
137 
138 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
139 std::pair< ALIdouble, ALIdouble > DeviationsFromFileSensor2D::getDevis( ALIdouble intersX, ALIdouble intersY )
140 {
141  //intersX += 10000;
142  //intersY += 10000;
143  if(verbose >= 4) std::cout << " entering getdevis " << intersX << " " <<intersY << " " << this << std::endl;
144  vvd::iterator vvdite;
145  vd::iterator vdite;
146 
147  // assume scan is in Y first, else revers intersection coordinates
148 /* if( !firstScanDirY ) {
149  ALIdouble tt = intersY;
150  intersY = intersX;
151  intersX = tt;
152  }
153 */
154 
155  //---------- look which point in the deviation matrices correspond to intersX,Y
156  //----- Look for each column, between which rows intersY is
157  //assume first dir is Y
158  unsigned int* yrows = new unsigned int[ theNPoints ];
159 
160  unsigned int ii = 0;
161  ALIbool insideMatrix = 0;
162  for( vvdite = theDeviations.begin(); vvdite != (theDeviations.end()-1); ++vvdite ){
163  for( vdite = (*vvdite).begin(); vdite != ((*vvdite).end()-1); ++vdite ){
164  if( verbose >= 5 ) std::cout << " check posy " << (*(vdite))->posY() << " " << (*(vdite+1))->posY() << " " << (*(vdite)) << std::endl;
165  // if posy is between this point and previous one
166 
167  //- std::cout << "intersy" << intersY << " " << (*(vdite))->posY() << std::endl;
168 
169  if( (intersY - (*(vdite))->posY() )*theScanSenseY > 0
170  && (intersY - (*(vdite+1))->posY() )*theScanSenseY < 0 ) {
171  //-std::cout << " ii " << ii << std::endl;
172  yrows[ii] = vdite - (*vvdite).begin();
173  if( verbose >= 3 ) std::cout << intersY << " DeviationsFromFileSensor2D yrows " << ii << " " << yrows[ii] << " : " << (*(vdite))->posY() << std::endl;
174  insideMatrix = 1;
175  break;
176  }
177  }
178  ii++;
179  }
180  if(insideMatrix == 0) {
181  std::cerr << "!!EXITING intersection in Y outside matrix of deviations from file " << intersY << std::endl;
182  exit(1);
183  }
184  insideMatrix = 0;
185 
186  vd thePoints;
187  thePoints.clear();
188  //----- For each row in 'yrows' look between which columns intersX is
189  unsigned int rn;
190  DeviationSensor2D *dev1,*dev2;
191  for( ii = 0; ii < theNPoints-1; ii++) {
192  rn = yrows[ii];
193  //- std::cout << ii << " rn " << rn << std::endl;
194  dev1 = (*(theDeviations.begin()+ii))[ rn ]; // column ii, row yrows[ii]
195  rn = yrows[ii+1];
196  dev2 = (*(theDeviations.begin()+ii+1))[ rn ]; // column ii+1, row yrows[ii+1]
197  if( (intersX - dev1->posX() )*theScanSenseX > 0
198  && (intersX - dev2->posX() )*theScanSenseX < 0) {
199  thePoints.push_back( dev1 );
200  thePoints.push_back( dev2 );
201  insideMatrix = 1;
202  if( verbose >= 3 ) std::cout << " column up " << ii << " " << dev1->posX() << " " << dev2->posX() << " : " << intersX << std::endl;
203  }
204 
205  rn = yrows[ii] + 1;
206  if(rn == theNPoints) rn = theNPoints-1;
207  dev1 = (*(theDeviations.begin()+ii))[ rn ]; // column ii, row yrows[ii]+1
208  rn = yrows[ii+1] + 1;
209  if(rn == theNPoints) rn = theNPoints-1;
210  dev2 = (*(theDeviations.begin()+ii+1))[ rn ]; // column ii+1, row yrows[ii+1]+1
211  if( (intersX - dev1->posX() )*theScanSenseX > 0
212  && (intersX - dev2->posX() )*theScanSenseX < 0) {
213  thePoints.push_back( dev1 );
214  thePoints.push_back( dev2 );
215  if( verbose >= 3 ) std::cout << " column down " << ii << " " << dev1->posX() << " " << dev2->posX() << " : " << intersX << std::endl;
216  }
217 
218  if( thePoints.size() == 4 ) break;
219 
220  }
221 
222  if(insideMatrix == 0) {
223  std::cerr << "!!EXITING intersection in X outside matrix of deviations from file " << intersX << std::endl;
224  exit(1);
225  }
226 
227  //----------- If loop finished and not 4 points, point is outside scan bounds
228 
229  //----- calculate deviation in x and y interpolating between four points
230  ALIdouble dist, disttot=0, deviX=0, deviY=0;
231 
232  if( verbose >= 4) std::cout << " thepoints size " << thePoints.size() << std::endl;
233 
234  for( ii = 0; ii < 4; ii++) {
235  dist = sqrt( pow(thePoints[ii]->posX() - intersX, 2 ) + pow(thePoints[ii]->posY() - intersY, 2 ) );
236  disttot += 1./dist;
237  deviX += thePoints[ii]->devX()/dist;
238  deviY += thePoints[ii]->devY()/dist;
239  if( verbose >= 4 ) {
240  //t std::cout << ii << " point " << *thePoints[ii] << std::endl;
241  std::cout << ii << " distances: " << dist << " " << deviX << " " << deviY << " devX " << thePoints[ii]->devX() << " devY " << thePoints[ii]->devY() << std::endl;
242  }
243  }
244  deviX /= disttot;
245  deviY /= disttot;
246 
247  // add offset
248  deviX += theOffsetX;
249  deviY += theOffsetY;
250  if( verbose >= 4 ) {
251  std::cout << " devisX/Y: " << deviX << " " << deviY
252  << " intersX/Y " << intersX << " " << intersY << std::endl;
253  }
254 
255  //change sign!!!!!?!?!?!?!?!
256  deviX *= -1;
257  deviY *= -1;
258  return std::pair< ALIdouble, ALIdouble>( deviX*1.e-6, deviY*1e-6 ); // matrix is in microrad
259 
260 }
261 
long double ALIdouble
Definition: CocoaGlobals.h:11
ALIbool eof()
Definition: ALIFileIn.cc:211
const ALIstring & name()
Definition: ALIFileIn.h:44
std::pair< ALIdouble, ALIdouble > getDevis(ALIdouble intersX, ALIdouble intersY)
static ALIint debug
Definition: ALIUtils.h:35
int ii
Definition: cuy.py:588
bool ALIbool
Definition: CocoaGlobals.h:19
std::vector< DeviationSensor2D * > vd
T sqrt(T t)
Definition: SSEVec.h:18
const ALIdouble & posY()
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
ALIint getWordsInLine(std::vector< ALIstring > &wl)
Definition: ALIFileIn.cc:83
const ALIdouble & posX()
void fillData(const std::vector< ALIstring > &wl)
static void dumpVS(const std::vector< ALIstring > &wl, const std::string &msg, std::ostream &outs=std::cout)
dumps a vector of strings with a message to outs
Definition: ALIUtils.cc:501
tuple cout
Definition: gather_cfg.py:145
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:40
unsigned int ALIuint
Definition: CocoaGlobals.h:17