CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_2_9/src/Alignment/CocoaModel/src/DeviationsFromFileSensor2D.cc

Go to the documentation of this file.
00001 //   COCOA class implementation file
00002 //Id: DeviationsFromFileSensor2D.cc
00003 //CAT: Model
00004 //
00005 //   History: v1.0 
00006 //   Pedro Arce
00007 
00008 #include "Alignment/CocoaModel/interface/DeviationsFromFileSensor2D.h"
00009 #include "Alignment/CocoaUtilities/interface/ALIFileIn.h"
00010 #include "Alignment/CocoaUtilities/interface/ALIUtils.h"
00011 #include <cstdlib>
00012 
00013 enum directions{ xdir = 0, ydir = 1};
00014 
00015 ALIbool DeviationsFromFileSensor2D::theApply = true;
00016  
00017 
00018 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00019 void DeviationsFromFileSensor2D::readFile( ALIFileIn& ifdevi )
00020 {
00021   verbose = ALIUtils::debug;
00022   //  verbose = 4;
00023   if( verbose >= 3) std::cout << "DeviationsFromFileSensor2D::readFile " << this << ifdevi.name() << std::endl;
00024 
00025   theScanSenseX = 0;
00026   theScanSenseY = 0;
00027 
00028   ALIuint nl = 1;
00029 
00030   ALIdouble oldposX=0, oldposY=0;
00031   vd vcol;
00032   std::vector<ALIstring> wl;
00033   /*  //------ first line with dimension factors //now with global option
00034   ifdevi.getWordsInLine( wl );
00035   if( wl[0] != "DIMFACTOR:" || wl.size() != 3) {
00036     std::cerr << "Error reading sensor2D deviation file " << ifdevi.name() << std::endl 
00037          << " first line has to be DIMFACTOR: 'posDimFactor' 'angDimFactor' " << std::endl;
00038     ALIUtils::dumpVS( wl, " ");
00039     exit(1);
00040   }
00041   ALIdouble posDimFactor = atof(wl[1].c_str());
00042   ALIdouble angDimFactor = atof(wl[2].c_str());
00043   */
00044 
00045   for(;;) {
00046     ifdevi.getWordsInLine( wl );
00047     if(ifdevi.eof() ) break;
00048      
00049     DeviationSensor2D* dev = new DeviationSensor2D();
00050     dev->fillData( wl );
00051     
00052     if(verbose >= 5) {
00053       ALIUtils::dumpVS( wl, "deviation sensor2D", std::cout );
00054     }
00055     
00056     //--- line 2 of data
00057     if(nl == 2) {
00058       //--------- get if scan is first in Y or X
00059       firstScanDir = ydir;
00060       if(verbose >= 3) std::cout << "firstScanDir " << firstScanDir << " " <<  dev->posX() <<  " " << oldposX << " " <<  dev->posY() <<  " " << oldposY << std::endl; 
00061       if( fabs( dev->posX() - oldposX ) >  fabs( dev->posY() - oldposY ) ) {
00062         std::cerr << "!!!EXITING first scan direction has to be Y for the moment " << ifdevi.name() << std::endl;
00063         firstScanDir = xdir;
00064         exit(1);
00065       }
00066       //-------- get sense of first scan direction 
00067       if(firstScanDir == ydir ){
00068         if( dev->posY() > oldposY ) {
00069           theScanSenseY = +1;
00070         } else {
00071           theScanSenseY = -1;
00072         }
00073         if( verbose >= 3 ) std::cout << " theScanSenseY " << theScanSenseY << std::endl;
00074       }else {
00075         if( dev->posX() > oldposX ) {
00076           theScanSenseX = +1;
00077         } else {
00078           theScanSenseX = -1;
00079         }
00080         if( verbose >= 3 ) std::cout << " theScanSenseX " << theScanSenseX << std::endl;
00081       }
00082     }
00083     
00084     //-      std::cout << "firstScanDir " << firstScanDir << " " <<  dev->posX() <<  " " << oldposX << " " <<  dev->posY() <<  " " << oldposY << std::endl; 
00085 
00086     //------- check if change of row: clear current std::vector of column values
00087     if( ( firstScanDir == ydir && ( dev->posY() - oldposY)*theScanSenseY < 0 )
00088         || ( firstScanDir == xdir && ( dev->posX() - oldposX)*theScanSenseX < 0 )) {
00089       theDeviations.push_back( vcol );
00090       vcol.clear(); 
00091       
00092       //-      std::cout << " theDevi size " << theDeviations.size() << " " << ifdevi.name()  << std::endl;
00093       //-------- get sense of second scan direction 
00094       if( theScanSenseY == 0 ) {
00095         if( dev->posY() > oldposY ) {
00096           theScanSenseY = +1;
00097         } else {
00098           theScanSenseY = -1;
00099         }
00100       }
00101       if( theScanSenseX == 0 ) {
00102         if( dev->posX() > oldposX ) {
00103           theScanSenseX = +1;
00104         } else {
00105           theScanSenseX = -1;
00106         }
00107       }
00108       if( verbose >= 3 ) std::cout << " theScanSenseX " << theScanSenseX << " theScanSenseY " << theScanSenseY << std::endl;
00109     }
00110     
00111     oldposX = dev->posX();
00112     oldposY = dev->posY();
00113     
00114     //--- fill deviation std::vectors
00115     vcol.push_back( dev );
00116     nl++;
00117   }
00118   theDeviations.push_back( vcol );
00119 
00120   //----- Calculate std::vector size
00121   ALIdouble dvsiz = ( sqrt( ALIdouble(nl-1) ) );
00122   //----- Check that it is a square of points (<=> vsiz is integer)
00123   ALIuint vsiz = ALIuint(dvsiz);
00124   if( vsiz != dvsiz ) {
00125     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; 
00126     //    exit(1);
00127   }
00128   theNPoints = vsiz;
00129   
00130   if(verbose >= 4 ) {
00131     std::cout << " Filling deviation from file: " << ifdevi.name() << " theNPoints " <<  theNPoints << std::endl;
00132   }
00133     
00134 }
00135 
00136 
00137 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00138 std::pair< ALIdouble, ALIdouble > DeviationsFromFileSensor2D::getDevis( ALIdouble intersX, ALIdouble intersY )
00139 { 
00140   //intersX += 10000;
00141   //intersY += 10000;
00142   if(verbose >= 4) std::cout << " entering getdevis " << intersX << " " <<intersY << " " << this << std::endl;
00143   vvd::iterator vvdite;
00144   vd::iterator vdite; 
00145   
00146   // assume scan is in Y first, else revers intersection coordinates
00147 /*  if( !firstScanDirY ) {
00148     ALIdouble tt = intersY;
00149     intersY = intersX;
00150     intersX = tt;
00151   }
00152 */
00153 
00154   //---------- look which point in the deviation matrices correspond to intersX,Y
00155   //----- Look for each column, between which rows intersY is
00156   //assume first dir is Y
00157   unsigned int* yrows = new unsigned int[ theNPoints ];
00158 
00159   unsigned int ii = 0;
00160   ALIbool insideMatrix = 0;
00161   for( vvdite = theDeviations.begin(); vvdite != (theDeviations.end()-1); vvdite++ ){
00162     for( vdite = (*vvdite).begin(); vdite != ((*vvdite).end()-1); vdite++ ){
00163      if( verbose >= 5 ) std::cout << " check posy " << (*(vdite))->posY()  << " " <<  (*(vdite+1))->posY()  << " " <<  (*(vdite)) << std::endl;
00164       // if posy is between this point and previous one
00165 
00166      //-     std::cout << "intersy" << intersY << " " <<  (*(vdite))->posY() << std::endl;
00167 
00168       if( (intersY - (*(vdite))->posY() )*theScanSenseY > 0 
00169           && (intersY - (*(vdite+1))->posY() )*theScanSenseY < 0 ) {
00170         //-std::cout << " ii " << ii << std::endl;
00171         yrows[ii] = vdite - (*vvdite).begin();  
00172         if( verbose >= 3 ) std::cout << intersY << " DeviationsFromFileSensor2D yrows " << ii << " " << yrows[ii] << " : " << (*(vdite))->posY() << std::endl;
00173         insideMatrix = 1;
00174         break;
00175       } 
00176     }
00177     ii++;
00178   }
00179   if(insideMatrix == 0) {
00180     std::cerr << "!!EXITING intersection in Y outside matrix of deviations from file " << intersY << std::endl;
00181     exit(1);
00182   }
00183   insideMatrix = 0;
00184 
00185   vd thePoints;
00186   thePoints.clear();
00187   //----- For each row in 'yrows' look between which columns intersX is
00188   unsigned int rn;
00189   DeviationSensor2D *dev1,*dev2;
00190   for( ii = 0; ii < theNPoints-1; ii++) {
00191     rn = yrows[ii];
00192     //-    std::cout << ii << " rn " << rn << std::endl;
00193     dev1 = (*(theDeviations.begin()+ii))[ rn ]; // column ii, row yrows[ii]
00194     rn = yrows[ii+1];
00195     dev2 = (*(theDeviations.begin()+ii+1))[ rn ]; // column ii+1, row yrows[ii+1]
00196     if( (intersX - dev1->posX() )*theScanSenseX > 0 
00197         && (intersX - dev2->posX() )*theScanSenseX < 0) {
00198       thePoints.push_back( dev1 ); 
00199       thePoints.push_back( dev2 ); 
00200       insideMatrix = 1;
00201       if( verbose >= 3 ) std::cout << " column up " << ii << " " << dev1->posX()  << " " << dev2->posX() << " : " << intersX << std::endl;
00202     }
00203     
00204     rn = yrows[ii] + 1;
00205     if(rn == theNPoints) rn = theNPoints-1;
00206     dev1 = (*(theDeviations.begin()+ii))[ rn ]; // column ii, row yrows[ii]+1
00207     rn = yrows[ii+1] + 1;
00208     if(rn == theNPoints) rn = theNPoints-1;
00209     dev2 = (*(theDeviations.begin()+ii+1))[ rn ]; // column ii+1, row yrows[ii+1]+1
00210     if( (intersX - dev1->posX() )*theScanSenseX > 0 
00211         && (intersX - dev2->posX() )*theScanSenseX < 0) {
00212       thePoints.push_back( dev1 ); 
00213       thePoints.push_back( dev2 ); 
00214       if( verbose >= 3 ) std::cout << " column down " << ii << " " <<  dev1->posX()  << " " << dev2->posX() << " : " << intersX << std::endl;
00215     }
00216     
00217     if( thePoints.size() == 4 ) break;
00218     
00219   }
00220  
00221   if(insideMatrix == 0) {
00222     std::cerr << "!!EXITING intersection in X outside matrix of deviations from file " << intersX << std::endl;
00223     exit(1);
00224   }
00225  
00226   //----------- If loop finished and not 4 points, point is outside scan bounds
00227  
00228   //----- calculate deviation in x and y interpolating between four points
00229   ALIdouble dist, disttot=0, deviX=0, deviY=0;
00230   
00231    if( verbose >= 4) std::cout << " thepoints size " << thePoints.size() << std::endl;
00232 
00233   for( ii = 0; ii < 4; ii++) {
00234     dist = sqrt( pow(thePoints[ii]->posX() - intersX, 2 ) +  pow(thePoints[ii]->posY() - intersY, 2 ) ); 
00235     disttot += 1./dist;
00236     deviX += thePoints[ii]->devX()/dist;
00237     deviY += thePoints[ii]->devY()/dist;
00238     if( verbose >= 4 ) { 
00239       //t      std::cout << ii << " point " << *thePoints[ii] << std::endl;
00240       std::cout << ii << " distances: " << dist << " " << deviX << " " << deviY << " devX " << thePoints[ii]->devX() << " devY " << thePoints[ii]->devY() << std::endl;
00241     }
00242   }
00243   deviX /= disttot;
00244   deviY /= disttot;
00245 
00246   // add offset
00247   deviX += theOffsetX; 
00248   deviY += theOffsetY; 
00249   if( verbose >= 4 ) { 
00250     std::cout << " devisX/Y: " << deviX << " " << deviY 
00251          << " intersX/Y " << intersX  << " " << intersY << std::endl;
00252   }
00253 
00254   //change sign!!!!!?!?!?!?!?!
00255    deviX *= -1;
00256    deviY *= -1;
00257   return std::pair< ALIdouble, ALIdouble>( deviX*1.e-6, deviY*1e-6 );  // matrix is in microrad
00258 
00259 }
00260