CMS 3D CMS Logo

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