directions

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
//   COCOA class implementation file
//Id: DeviationsFromFileSensor2D.cc
//CAT: Model
//
//   History: v1.0
//   Pedro Arce

#include "Alignment/CocoaModel/interface/DeviationsFromFileSensor2D.h"
#include "Alignment/CocoaUtilities/interface/ALIFileIn.h"
#include "Alignment/CocoaUtilities/interface/ALIUtils.h"
#include <cassert>
#include <cstdlib>
#include <cmath>  // include floating-point std::abs functions
#include <memory>

enum directions { xdir = 0, ydir = 1 };

ALIbool DeviationsFromFileSensor2D::theApply = true;

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
void DeviationsFromFileSensor2D::readFile(ALIFileIn& ifdevi) {
  verbose = ALIUtils::debug;
  //  verbose = 4;
  if (verbose >= 3)
    std::cout << "DeviationsFromFileSensor2D::readFile " << this << ifdevi.name() << std::endl;

  theScanSenseX = 0;
  theScanSenseY = 0;

  ALIuint nl = 1;

  ALIdouble oldposX = 0, oldposY = 0;
  vd vcol;
  std::vector<ALIstring> wl;
  /*  //------ first line with dimension factors //now with global option
  ifdevi.getWordsInLine( wl );
  if( wl[0] != "DIMFACTOR:" || wl.size() != 3) {
    std::cerr << "Error reading sensor2D deviation file " << ifdevi.name() << std::endl
	 << " first line has to be DIMFACTOR: 'posDimFactor' 'angDimFactor' " << std::endl;
    ALIUtils::dumpVS( wl, " ");
    exit(1);
  }
  ALIdouble posDimFactor = atof(wl[1].c_str());
  ALIdouble angDimFactor = atof(wl[2].c_str());
  */

  for (;;) {
    ifdevi.getWordsInLine(wl);
    if (ifdevi.eof())
      break;

    DeviationSensor2D* dev = new DeviationSensor2D();
    dev->fillData(wl);

    if (verbose >= 5) {
      ALIUtils::dumpVS(wl, "deviation sensor2D", std::cout);
    }

    //--- line 2 of data
    if (nl == 2) {
      //--------- get if scan is first in Y or X
      firstScanDir = ydir;
      if (verbose >= 3)
        std::cout << "firstScanDir " << firstScanDir << " " << dev->posX() << " " << oldposX << " " << dev->posY()
                  << " " << oldposY << std::endl;
      if (std::abs(dev->posX() - oldposX) > std::abs(dev->posY() - oldposY)) {
        std::cerr << "!!!EXITING first scan direction has to be Y for the moment " << ifdevi.name() << std::endl;
        firstScanDir = xdir;
        exit(1);
      }
      //-------- get sense of first scan direction
      if (firstScanDir == ydir) {
        if (dev->posY() > oldposY) {
          theScanSenseY = +1;
        } else {
          theScanSenseY = -1;
        }
        if (verbose >= 3)
          std::cout << " theScanSenseY " << theScanSenseY << std::endl;
      } else {
        if (dev->posX() > oldposX) {
          theScanSenseX = +1;
        } else {
          theScanSenseX = -1;
        }
        if (verbose >= 3)
          std::cout << " theScanSenseX " << theScanSenseX << std::endl;
      }
    }

    //-      std::cout << "firstScanDir " << firstScanDir << " " <<  dev->posX() <<  " " << oldposX << " " <<  dev->posY() <<  " " << oldposY << std::endl;

    //------- check if change of row: clear current std::vector of column values
    if ((firstScanDir == ydir && (dev->posY() - oldposY) * theScanSenseY < 0) ||
        (firstScanDir == xdir && (dev->posX() - oldposX) * theScanSenseX < 0)) {
      theDeviations.push_back(vcol);
      vcol.clear();

      //-      std::cout << " theDevi size " << theDeviations.size() << " " << ifdevi.name()  << std::endl;
      //-------- get sense of second scan direction
      if (theScanSenseY == 0) {
        if (dev->posY() > oldposY) {
          theScanSenseY = +1;
        } else {
          theScanSenseY = -1;
        }
      }
      if (theScanSenseX == 0) {
        if (dev->posX() > oldposX) {
          theScanSenseX = +1;
        } else {
          theScanSenseX = -1;
        }
      }
      if (verbose >= 3)
        std::cout << " theScanSenseX " << theScanSenseX << " theScanSenseY " << theScanSenseY << std::endl;
    }

    oldposX = dev->posX();
    oldposY = dev->posY();

    //--- fill deviation std::vectors
    vcol.push_back(dev);
    nl++;
  }
  theDeviations.push_back(vcol);

  //----- Calculate std::vector size
  ALIdouble dvsiz = (sqrt(ALIdouble(nl - 1)));
  //----- Check that it is a square of points (<=> vsiz is integer)
  ALIuint vsiz = ALIuint(dvsiz);
  if (vsiz != dvsiz) {
    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;
    //    exit(1);
  }
  theNPoints = vsiz;

  if (verbose >= 4) {
    std::cout << " Filling deviation from file: " << ifdevi.name() << " theNPoints " << theNPoints << std::endl;
  }
}

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
std::pair<ALIdouble, ALIdouble> DeviationsFromFileSensor2D::getDevis(ALIdouble intersX, ALIdouble intersY) {
  //intersX += 10000;
  //intersY += 10000;
  if (verbose >= 4)
    std::cout << " entering getdevis " << intersX << " " << intersY << " " << this << std::endl;
  vvd::iterator vvdite;
  vd::iterator vdite;

  // assume scan is in Y first, else revers intersection coordinates
  /*  if( !firstScanDirY ) {
    ALIdouble tt = intersY;
    intersY = intersX;
    intersX = tt;
  }
*/

  //---------- look which point in the deviation matrices correspond to intersX,Y
  //----- Look for each column, between which rows intersY is
  //assume first dir is Y
  auto yrows = std::make_unique<unsigned int[]>(theNPoints);

  unsigned int ii = 0;
  ALIbool insideMatrix = false;
  for (vvdite = theDeviations.begin(); vvdite != (theDeviations.end() - 1); ++vvdite) {
    for (vdite = (*vvdite).begin(); vdite != ((*vvdite).end() - 1); ++vdite) {
      if (verbose >= 5)
        std::cout << " check posy " << (*(vdite))->posY() << " " << (*(vdite + 1))->posY() << " " << (*(vdite))
                  << std::endl;
      // if posy is between this point and previous one

      //-     std::cout << "intersy" << intersY << " " <<  (*(vdite))->posY() << std::endl;

      if ((intersY - (*(vdite))->posY()) * theScanSenseY > 0 &&
          (intersY - (*(vdite + 1))->posY()) * theScanSenseY < 0) {
        //-std::cout << " ii " << ii << std::endl;
        yrows[ii] = vdite - (*vvdite).begin();
        if (verbose >= 3)
          std::cout << intersY << " DeviationsFromFileSensor2D yrows " << ii << " " << yrows[ii] << " : "
                    << (*(vdite))->posY() << std::endl;
        insideMatrix = true;
        break;
      }
    }
    ii++;
  }
  assert(ii == theNPoints);
  if (insideMatrix == 0) {
    std::cerr << "!!EXITING intersection in Y outside matrix of deviations from file " << intersY << std::endl;
    exit(1);
  }
  insideMatrix = false;

  vd thePoints;
  thePoints.clear();
  //----- For each row in 'yrows' look between which columns intersX is
  unsigned int rn;
  DeviationSensor2D *dev1, *dev2;
  for (ii = 0; ii < theNPoints - 1; ii++) {
    rn = yrows[ii];
    //-    std::cout << ii << " rn " << rn << std::endl;
    dev1 = (*(theDeviations.begin() + ii))[rn];  // column ii, row yrows[ii]
    rn = yrows[ii + 1];
    dev2 = (*(theDeviations.begin() + ii + 1))[rn];  // column ii+1, row yrows[ii+1]
    if ((intersX - dev1->posX()) * theScanSenseX > 0 && (intersX - dev2->posX()) * theScanSenseX < 0) {
      thePoints.push_back(dev1);
      thePoints.push_back(dev2);
      insideMatrix = true;
      if (verbose >= 3)
        std::cout << " column up " << ii << " " << dev1->posX() << " " << dev2->posX() << " : " << intersX << std::endl;
    }

    rn = yrows[ii] + 1;
    if (rn == theNPoints)
      rn = theNPoints - 1;
    dev1 = (*(theDeviations.begin() + ii))[rn];  // column ii, row yrows[ii]+1
    rn = yrows[ii + 1] + 1;
    if (rn == theNPoints)
      rn = theNPoints - 1;
    dev2 = (*(theDeviations.begin() + ii + 1))[rn];  // column ii+1, row yrows[ii+1]+1
    if ((intersX - dev1->posX()) * theScanSenseX > 0 && (intersX - dev2->posX()) * theScanSenseX < 0) {
      thePoints.push_back(dev1);
      thePoints.push_back(dev2);
      if (verbose >= 3)
        std::cout << " column down " << ii << " " << dev1->posX() << " " << dev2->posX() << " : " << intersX
                  << std::endl;
    }

    if (thePoints.size() == 4)
      break;
  }

  if (insideMatrix == 0) {
    std::cerr << "!!EXITING intersection in X outside matrix of deviations from file " << intersX << std::endl;
    exit(1);
  }

  //----------- If loop finished and not 4 points, point is outside scan bounds

  //----- calculate deviation in x and y interpolating between four points
  ALIdouble dist, disttot = 0, deviX = 0, deviY = 0;

  if (verbose >= 4)
    std::cout << " thepoints size " << thePoints.size() << std::endl;

  for (ii = 0; ii < 4; ii++) {
    dist = sqrt(pow(thePoints[ii]->posX() - intersX, 2) + pow(thePoints[ii]->posY() - intersY, 2));
    disttot += 1. / dist;
    deviX += thePoints[ii]->devX() / dist;
    deviY += thePoints[ii]->devY() / dist;
    if (verbose >= 4) {
      //t      std::cout << ii << " point " << *thePoints[ii] << std::endl;
      std::cout << ii << " distances: " << dist << " " << deviX << " " << deviY << " devX " << thePoints[ii]->devX()
                << " devY " << thePoints[ii]->devY() << std::endl;
    }
  }
  deviX /= disttot;
  deviY /= disttot;

  // add offset
  deviX += theOffsetX;
  deviY += theOffsetY;
  if (verbose >= 4) {
    std::cout << " devisX/Y: " << deviX << " " << deviY << " intersX/Y " << intersX << " " << intersY << std::endl;
  }

  //change sign!!!!!?!?!?!?!?!
  deviX *= -1;
  deviY *= -1;
  return std::pair<ALIdouble, ALIdouble>(deviX * 1.e-6, deviY * 1e-6);  // matrix is in microrad
}