CMS 3D CMS Logo

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