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