00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include "otsdaq-core/MonicelliInterface/Detector.h"
00011 #include <TMath.h>
00012
00013 #define PI TMath::Pi()
00014
00015 using namespace monicelli;
00016
00017 ClassImp(Detector)
00018
00019
00020 Detector::Detector (std::string ID, bool isDUT) :
00021 ID_ (ID)
00022 , isDUT_ (isDUT)
00023 , xPosition_ (0)
00024 , xPositionCorrection_ (0)
00025 , xPositionError_ (0)
00026 , yPosition_ (0)
00027 , yPositionCorrection_ (0)
00028 , yPositionError_ (0)
00029 , zPosition_ (0)
00030 , zPositionCorrection_ (0)
00031 , zPositionError_ (0)
00032 , xNumberOfROCs_ (0)
00033 , yNumberOfROCs_ (0)
00034 , numberOfROCs_ (0)
00035 , numberOfRows_ (0)
00036 , numberOfCols_ (0)
00037 , xBackFlipped_ (false)
00038 , yBackFlipped_ (false)
00039 , xRotation_ (0)
00040 , xRotationCorrection_ (0)
00041 , xRotationCorrectionError_(0)
00042 , yRotation_ (0)
00043 , yRotationCorrection_ (0)
00044 , yRotationCorrectionError_(0)
00045 , zRotation_ (0)
00046 , zRotationCorrection_ (0)
00047 , zRotationCorrectionError_(0)
00048 {
00049
00050 }
00051
00052
00053 Detector::~Detector(void)
00054 {
00055
00056 for(ROCsMapDef::iterator it=ROCsChipIDMap_.begin(); it!=ROCsChipIDMap_.end(); it++)
00057 {
00058 delete it->second;
00059 }
00060 ROCsChipIDMap_ .clear();
00061 ROCsPositionMap_.clear();
00062
00063 }
00064
00065
00066 void Detector::setupVariables(void)
00067 {
00068 numberOfCols_ = 0;
00069 numberOfRows_ = 0;
00070 for(ROCsMapDef::iterator it=ROCsChipIDMap_.begin(); it!=ROCsChipIDMap_.end(); it++)
00071 {
00072 unsigned int ROCPosition = it->second->getPosition();
00073 unsigned int yPosition = ROCPosition/xNumberOfROCs_;
00074
00075 it->second->setLocalYPosition(yPosition);
00076 if( yPosition%2 != 0 ) it->second->setLocalXPosition((yPosition+1) * xNumberOfROCs_ - (ROCPosition+1));
00077 else it->second->setLocalXPosition(ROCPosition - yPosition * xNumberOfROCs_) ;
00078 if(it->second->getPositionLocalY() == 0)
00079 numberOfCols_ += it->second->getNumberOfCols();
00080 if(it->second->getPositionLocalX() == 0)
00081 numberOfRows_ += it->second->getNumberOfRows();
00082 it->second->getNumberOfCols();
00083 }
00084
00085 ROCsPositionMap_[0]->setFirstCol(0);
00086 ROCsPositionMap_[0]->setLastCol(ROCsPositionMap_[0]->getNumberOfCols()-1);
00087 for(unsigned int posX=1; posX < xNumberOfROCs_; posX++)
00088 {
00089 ROCsPositionMap_[posX]->setFirstCol(ROCsPositionMap_[(posX-1)]->getLastCol()+1);
00090 ROCsPositionMap_[posX]->setLastCol (ROCsPositionMap_[posX]->getFirstCol()+ROCsPositionMap_[posX]->getNumberOfCols()-1);
00091 }
00092
00093 ROCsPositionMap_[0]->setFirstRow(0);
00094 ROCsPositionMap_[0]->setLastRow(ROCsPositionMap_[0]->getNumberOfRows()-1);
00095 for(unsigned int posY=xNumberOfROCs_; posY<yNumberOfROCs_*xNumberOfROCs_; posY+=xNumberOfROCs_)
00096 {
00097 ROCsPositionMap_[posY]->setFirstRow(ROCsPositionMap_[(posY-xNumberOfROCs_)]->getLastRow()+1);
00098 ROCsPositionMap_[posY]->setLastRow (ROCsPositionMap_[posY]->getFirstRow()+ROCsPositionMap_[posY]->getNumberOfRows()-1);
00099 }
00100
00101 for(ROCsMapDef::iterator it=ROCsChipIDMap_.begin(); it!=ROCsChipIDMap_.end(); it++)
00102 {
00103 it->second->setFirstCol(ROCsPositionMap_[it->second->getPositionLocalX()]->getFirstCol());
00104 it->second->setLastCol (ROCsPositionMap_[it->second->getPositionLocalX()]->getLastCol());
00105 it->second->setFirstRow(ROCsPositionMap_[it->second->getPositionLocalY()*xNumberOfROCs_]->getFirstRow());
00106 it->second->setLastRow (ROCsPositionMap_[it->second->getPositionLocalY()*xNumberOfROCs_]->getLastRow());
00107 }
00108 }
00109
00110
00111 Detector::matrix33Def Detector::rotationMatrix(double alpha, double beta, double gamma)
00112 {
00113 Detector::matrix33Def R;
00114
00115 R(0,0) = cos(beta )*cos(gamma);
00116 R(0,1) =-cos(alpha)*sin(gamma)+sin(alpha)*sin(beta )*cos(gamma);
00117 R(0,2) = sin(alpha)*sin(gamma)+cos(alpha)*sin(beta )*cos(gamma);
00118 R(1,0) = cos(beta)*sin(gamma);
00119 R(1,1) = cos(alpha)*cos(gamma)+sin(alpha)*sin(beta)*sin(gamma);
00120 R(1,2) =-sin(alpha)*cos(gamma)+cos(alpha)*sin(beta)*sin(gamma);
00121 R(2,0) =-sin(beta);
00122 R(2,1) = sin(alpha)*cos(beta);
00123 R(2,2) = cos(alpha)*cos(beta);
00124
00125 return R;
00126 }
00127
00128
00129 ROC* Detector::addROC(unsigned int chipPosition, int chipID, unsigned int rotationDegrees)
00130 {
00131 if( chipPosition > numberOfROCs_ )
00132 {
00133 std::stringstream ss;
00134 ss.str("");
00135 ss << chipID;
00136 STDLINE("ERROR: there is no ROC position: " + ss.str() + " in current detector!!",ACRed);
00137 STDLINE("ERROR: Maybe you have to use setComposition first",ACRed);
00138 }
00139 if( ROCsChipIDMap_.find( chipID ) == ROCsChipIDMap_.end() && ROCsPositionMap_.find( chipID ) == ROCsPositionMap_.end() )
00140 {
00141 ROCsChipIDMap_ [ chipID ] = new ROC(chipPosition, chipID, rotationDegrees );
00142 ROCsPositionMap_ [ chipPosition ] = ROCsChipIDMap_[ chipID ] ;
00143 }
00144 else
00145 {
00146 std::stringstream ss;
00147 ss.str("");
00148 ss << "WARNING: ROC: " << chipID << " in position " << chipPosition << "was already set!!";
00149 STDLINE(ss.str(),ACRed);
00150 }
00151 return ROCsChipIDMap_[ chipID ];
00152 }
00153
00154
00155 void Detector::setNumberOfROCs(unsigned int xNumberOfROCs, unsigned int yNumberOfROCs)
00156 {
00157 xNumberOfROCs_ = xNumberOfROCs;
00158 yNumberOfROCs_ = yNumberOfROCs;
00159 this->updateNumberOfROCs();
00160 }
00161
00162
00163 unsigned int Detector::getNumberOfCols(bool global)
00164 {
00165 if( !global || (zRotation_ < 45 && zRotation_ >= 0) || (zRotation_ <= 360 && zRotation_ > 315) || (zRotation_ > 135 && zRotation_ < 225) )
00166 return numberOfCols_;
00167 else
00168 return this->getNumberOfRows();
00169 }
00170
00171
00172 unsigned int Detector::getNumberOfRows(bool global)
00173 {
00174 if( !global || (zRotation_ < 45 && zRotation_ >= 0) || (zRotation_ <= 360 && zRotation_ > 315) || (zRotation_ > 135 && zRotation_ < 225) )
00175 return numberOfRows_;
00176 else
00177 return this->getNumberOfCols();
00178 }
00179
00180
00181 ROC* Detector::getROC(int chipID)
00182 {
00183 if( ROCsChipIDMap_.find( chipID ) != ROCsChipIDMap_.end() )
00184 return ROCsChipIDMap_[chipID] ;
00185 else
00186 {
00187 STDLINE("WARNING: No ROC found",ACRed);
00188 return 0;
00189 }
00190 }
00191
00192
00193 ROC* Detector::getROCByPosition(unsigned int chipPosition)
00194 {
00195 if( ROCsPositionMap_.find( chipPosition ) != ROCsPositionMap_.end() )
00196 return ROCsPositionMap_[chipPosition] ;
00197 else
00198 {
00199 STDLINE("WARNING: No ROC found",ACRed);
00200 return 0;
00201 }
00202 }
00203
00204
00205 int Detector::getPixelColFromLocalX(double xLocal)
00206 {
00207 if(xLocal < 0 || xLocal >= this->getDetectorLengthX())
00208 return -1;
00209
00210 double l = 0;
00211 ROC* roc = 0;
00212 unsigned int col = 0;
00213
00214 for(unsigned int posX=0; posX<xNumberOfROCs_; posX++)
00215 {
00216 roc = getROCByPosition(posX);
00217 if( xLocal < l+roc->getLengthLocalX())
00218 break;
00219 else
00220 {
00221 l += roc->getLengthLocalX();
00222 if(posX+1 == xNumberOfROCs_)
00223 return -1;
00224 col += roc->getNumberOfCols();
00225 }
00226
00227 }
00228 for(unsigned int rocCol=0; rocCol<roc->getNumberOfCols(); rocCol++)
00229 {
00230 l += roc->getPixelPitchLocalX(rocCol);
00231 if(xLocal < l)
00232 return col+rocCol;
00233 }
00234 return -1;
00235 }
00236
00237
00238 int Detector::getPixelRowFromLocalY(double yLocal)
00239 {
00240 if(yLocal < 0 || yLocal >= this->getDetectorLengthY())
00241 return -1;
00242
00243 double l=0 ;
00244 for(unsigned int row=0; row<this->getNumberOfRows(); row++)
00245 {
00246 l+=this->getPixelPitchLocalY(row);
00247 if(yLocal < l) return row;
00248 }
00249 return -1;
00250 }
00251
00252
00253 Detector::rowColPair Detector::getPixelCellFromLocal(double x, double y)
00254 {
00255 Detector::rowColPair rowCol;
00256 rowCol.first = this->getPixelRowFromLocalY(y);
00257 rowCol.second = this->getPixelColFromLocalX(x);
00258 return rowCol;
00259 }
00260
00261
00262 Detector::rowColPair Detector::getPixelCellFromGlobal(double x, double y, double z)
00263 {
00264 fromGlobalToLocal(&x,&y,&z);
00265 Detector::rowColPair rowCol;
00266 rowCol.first = this->getPixelRowFromLocalY(y);
00267 rowCol.second = this->getPixelColFromLocalX(x);
00268 return rowCol;
00269 }
00270
00271
00272 double Detector::getPixelCenterLocalX(unsigned int col)
00273 {
00274 return this->getPixelLowEdgeLocalX(col) + this->getPixelPitchLocalX(col)/2.;
00275 }
00276
00277
00278 double Detector::getPixelCenterLocalY(unsigned int row)
00279 {
00280 return this->getPixelLowEdgeLocalY(row) + this->getPixelPitchLocalY(row)/2.;
00281 }
00282
00283
00284 double Detector::getPixelLowEdgeLocalX(unsigned int col)
00285 {
00286 double lowEdge=0;
00287 for (unsigned int pos=0; pos < xNumberOfROCs_; pos++)
00288 {
00289 ROC *roc = getROCByPosition(pos);
00290
00291 if(col > roc->getLastCol())
00292 lowEdge += roc->getLengthLocalX();
00293 else
00294 {
00295 unsigned int rocRow = 0;
00296 roc = this->convertPixelToROC(&rocRow,&col);
00297 lowEdge += roc->getPixelLowEdgeLocalX(col) ;
00298 return lowEdge;
00299 }
00300 }
00301 STDLINE("WARNING: pixel column is out of range, returning detector's length X", ACRed);
00302 return lowEdge;
00303 }
00304
00305
00306 double Detector::getPixelLowEdgeLocalY(unsigned int row)
00307 {
00308 double lowEdge=0;
00309 for (unsigned int pos=0; pos < numberOfROCs_ ; pos+=xNumberOfROCs_)
00310 {
00311 ROC *roc = getROCByPosition(pos);
00312 if(row > roc->getLastRow())
00313 lowEdge += roc->getLengthLocalY();
00314 else
00315 {
00316 unsigned int rocCol = 0;
00317 roc = this->convertPixelToROC(&row,&rocCol);
00318 lowEdge += roc->getPixelLowEdgeLocalY(row) ;
00319 return lowEdge;
00320 }
00321 }
00322 STDLINE("WARNING: pixel row is out of range, returning detector's length Y", ACRed);
00323 return lowEdge;
00324 }
00325
00326
00327 double Detector::getPixelPitchLocalX(unsigned int col)
00328 {
00329 unsigned int rocRow = 0;
00330 ROC* roc = this->convertPixelToROC(&rocRow,&col);
00331 return roc->getPixelPitchLocalX(col);
00332 }
00333
00334
00335 double Detector::getPixelPitchLocalY(unsigned int row)
00336 {
00337 unsigned int rocCol = 0;
00338 ROC* roc = this->convertPixelToROC(&row,&rocCol);
00339 return roc->getPixelPitchLocalY(row);
00340 }
00341
00342
00343 double Detector::getDetectorLengthX(bool global)
00344 {
00345 if( !global || (zRotation_ < 45 && zRotation_ >= 0) || (zRotation_ <= 360 && zRotation_ > 315) || (zRotation_ > 135 && zRotation_ < 225) )
00346 {
00347 double length=0;
00348 for (unsigned int pos=0; pos < xNumberOfROCs_; pos++)
00349 length += getROCByPosition(pos)->getLengthLocalX();
00350 return length;
00351 }
00352 else
00353 return this->getDetectorLengthY(false);
00354 }
00355
00356
00357 double Detector::getDetectorLengthY(bool global)
00358 {
00359 if( !global || (zRotation_ < 45 && zRotation_ >= 0) || (zRotation_ <= 360 && zRotation_ > 315) || (zRotation_ > 135 && zRotation_ < 225) )
00360 {
00361 double length=0;
00362 for (unsigned int pos=0; pos < numberOfROCs_ ; pos+=xNumberOfROCs_)
00363 length += getROCByPosition(pos)->getLengthLocalY();
00364 return length;
00365 }
00366 else
00367 return this->getDetectorLengthX(false);
00368
00369 }
00370
00371
00372 unsigned int Detector::getROCPositionLocalX(int chipID)
00373 {
00374 return getROC(chipID)->getPositionLocalX();
00375 }
00376
00377
00378 unsigned int Detector::getROCPositionLocalY(int chipID)
00379 {
00380 return getROC(chipID)->getPositionLocalY();
00381 }
00382
00383
00384
00385 void Detector::convertPixelFromROC(ROC* roc, unsigned int *row, unsigned int *col)
00386 {
00387 if(roc->getOrientation() == 0)
00388 {
00389 *row += roc->getFirstRow();
00390 *col += roc->getFirstCol();
00391 }
00392 else if(roc->getOrientation() == 180)
00393 {
00394 *row = roc->getLastRow() - *row;
00395 *col = roc->getLastCol() - *col;
00396 }
00397 else
00398 {
00399 STDLINE("ERROR: Only 0 and 180 degrees rotations are supported for a ROC!", ACRed);
00400 exit(EXIT_FAILURE);
00401 }
00402 }
00403
00404
00405
00406 ROC* Detector::convertPixelToROC(unsigned int *row, unsigned int *col)
00407 {
00408 ROC *roc = this->findROC(*row, *col);
00409 if(roc->getOrientation() == 0)
00410 {
00411 *row -= roc->getFirstRow();
00412 *col -= roc->getFirstCol();
00413 }
00414 else if(roc->getOrientation() == 180)
00415 {
00416 *row = roc->getLastRow() - *row;
00417 *col = roc->getLastCol() - *col;
00418 }
00419 else
00420 {
00421 STDLINE("ERROR: Only 0 and 180 degrees rotations are supported for a ROC!", ACRed);
00422 exit(EXIT_FAILURE);
00423 }
00424 return roc;
00425 }
00426
00427
00428 ROC* Detector::findROC(unsigned int row, unsigned int col)
00429 {
00430 unsigned int posX = getROCPositionLocalXFromCol(col);
00431 unsigned int posY = getROCPositionLocalYFromRow(row);
00432 if(posX != xNumberOfROCs_ && posY != yNumberOfROCs_)
00433 {
00434 if(posY%2 == 0)
00435 return getROCByPosition(posX+posY*xNumberOfROCs_);
00436 else
00437 return getROCByPosition(xNumberOfROCs_-1-posX+posY*xNumberOfROCs_);
00438 }
00439 STDLINE("WARNING: pixel column or pixel row is out of detector's range, return NULL", ACRed);
00440 return 0;
00441 }
00442
00443
00444 unsigned int Detector::getROCPositionLocalXFromCol(unsigned int col)
00445 {
00446 unsigned int posX = 0;
00447 for (posX=0; posX < xNumberOfROCs_; posX++)
00448 if ( col <= getROCByPosition(posX)->getLastCol() )
00449 break;
00450 return posX;
00451 }
00452
00453
00454 unsigned int Detector::getROCPositionLocalYFromRow(unsigned int row)
00455 {
00456 unsigned int posY = 0;
00457 for (posY=0; posY < numberOfROCs_; posY+=xNumberOfROCs_)
00458 if ( row <= getROCByPosition(posY)->getLastRow() )
00459 break;
00460 return posY/xNumberOfROCs_;
00461 }
00462
00463
00464 void Detector::flipPixel(unsigned int *row, unsigned int *col)
00465 {
00466 unsigned int lastRow = this->getLastRow();
00467 unsigned int lastCol = this->getLastCol();
00468
00469
00470 if( this->isXBackFlipped() )
00471 *col = lastCol - *col;
00472
00473 if( this->isYBackFlipped() )
00474 *row = lastRow - *row ;
00475
00476
00477 int tmp;
00478
00479 if(zRotation_ > 225 && zRotation_ < 315)
00480 {
00481 tmp = *col ;
00482 *col = *row ;
00483 *row = lastCol - tmp ;
00484 }
00485
00486 else if(zRotation_ > 45 && zRotation_ < 135)
00487 {
00488 tmp = *col ;
00489 *col = lastRow - *row ;
00490 *row = tmp ;
00491 }
00492
00493 else if(zRotation_ >= 135 && zRotation_ <= 225)
00494 {
00495 *row = lastRow - *row ;
00496 *col = lastCol - *col ;
00497 }
00498 }
00499
00500
00501 void Detector::flipBackPixel(unsigned int *row, unsigned int *col )
00502 {
00503 unsigned int lastRow = this->getLastRow();
00504 unsigned int lastCol = this->getLastCol();
00505
00506 int tmp;
00507
00508 if(zRotation_ > 225 && zRotation_ < 315)
00509 {
00510 tmp = *row ;
00511 *row = *col ;
00512 *col = lastCol - tmp ;
00513 }
00514
00515 else if(zRotation_ > 45 && zRotation_ < 135)
00516 {
00517 tmp = *row ;
00518 *row = lastRow - *col ;
00519 *col = tmp ;
00520 }
00521
00522 else if(zRotation_ >= 135 && zRotation_ <= 225)
00523 {
00524 *col = lastCol - *col ;
00525 *row = lastRow - *row ;
00526 }
00527
00528
00529 if( this->isXBackFlipped() )
00530 *col = lastCol - *col;
00531
00532 if( this->isYBackFlipped() )
00533 *row = lastRow - *row ;
00534 }
00535
00536
00537 void Detector::flipPositionLocal(double *x, double *y, double *xErr, double *yErr)
00538 {
00539 double yLength = this->getDetectorLengthY();
00540 double xLength = this->getDetectorLengthX();
00541
00542
00543
00544
00545
00546
00547
00548 if( this->isXBackFlipped() )
00549 *x = xLength - *x ;
00550
00551 if( this->isYBackFlipped() )
00552 *y = yLength - *y ;
00553
00554 double tmp;
00555
00556 if(zRotation_ > 225 && zRotation_ < 315)
00557 {
00558 tmp = *x ;
00559 *x = *y ;
00560 *y = xLength - tmp ;
00561 if(xErr != 0 && yErr != 0)
00562 {
00563 tmp = *xErr ;
00564 *xErr = *yErr ;
00565 *yErr = tmp ;
00566 }
00567 }
00568
00569 else if(zRotation_ > 45 && zRotation_ < 135)
00570 {
00571 tmp = *x ;
00572 *x = yLength - *y ;
00573 *y = tmp ;
00574 if(xErr != 0 && yErr != 0)
00575 {
00576 tmp = *xErr ;
00577 *xErr = *yErr ;
00578 *yErr = tmp ;
00579 }
00580 }
00581
00582 else if(zRotation_ >= 135 && zRotation_ <= 225)
00583 {
00584 *y = yLength - *y ;
00585 *x = xLength - *x ;
00586 }
00587 }
00588
00589
00590 void Detector::flipBackPositionLocal(double *x, double *y, double *xErr, double *yErr)
00591 {
00592 double yLength = this->getDetectorLengthY();
00593 double xLength = this->getDetectorLengthX();
00594
00595
00596 double tmp;
00597
00598 if(zRotation_ > 225 && zRotation_ < 315)
00599 {
00600 tmp = *y ;
00601 *y = *x ;
00602 *x = xLength - tmp ;
00603 if(xErr != 0 && yErr != 0)
00604 {
00605 tmp = *yErr ;
00606 *yErr = *xErr ;
00607 *xErr = tmp ;
00608 }
00609 }
00610
00611 else if(zRotation_ > 45 && zRotation_ < 135)
00612 {
00613 tmp = *y ;
00614 *y = yLength - *x ;
00615 *x = tmp ;
00616 if(xErr != 0 && yErr != 0)
00617 {
00618 tmp = *yErr ;
00619 *yErr = *xErr ;
00620 *xErr = tmp ;
00621 }
00622 }
00623
00624 else if(zRotation_ >= 135 && zRotation_ <= 225)
00625 {
00626 *x = xLength - *x ;
00627 *y = yLength - *y ;
00628 }
00629
00630
00631 if( this->isXBackFlipped() )
00632 *x = xLength - *x ;
00633
00634 if( this->isYBackFlipped() )
00635 *y = yLength - *y ;
00636 }
00637
00638
00639 void Detector::flipDistance(double* deltaX, double* deltaY)
00640 {
00641
00642 if( this->isXBackFlipped() )
00643 *deltaX = -*deltaX ;
00644
00645 if( this->isYBackFlipped() )
00646 *deltaY = -*deltaY ;
00647
00648
00649 double tmp;
00650
00651 if(zRotation_ > 225 && zRotation_ < 315)
00652 {
00653 tmp = *deltaX;
00654 *deltaX = *deltaY;
00655 *deltaY = tmp;
00656 }
00657
00658 else if(zRotation_ > 45 && zRotation_ < 135)
00659 {
00660 tmp = *deltaX;
00661 *deltaX = -*deltaY;
00662 *deltaY = tmp;
00663 }
00664
00665 else if(zRotation_ >= 135 && zRotation_ <= 225)
00666 {
00667 *deltaY = -*deltaY;
00668 *deltaX = -*deltaX;
00669 }
00670 }
00671
00672
00673 void Detector::flipBackDistance(double* deltaX, double* deltaY)
00674 {
00675
00676 double tmp;
00677
00678 if(zRotation_ > 225 && zRotation_ < 315)
00679 {
00680 tmp = *deltaY;
00681 *deltaY = *deltaX;
00682 *deltaX = -tmp ;
00683 }
00684
00685 else if(zRotation_ > 45 && zRotation_ < 135)
00686 {
00687 tmp = *deltaY ;
00688 *deltaY = -*deltaX;
00689 *deltaX = tmp ;
00690 }
00691
00692 else if(zRotation_ >= 135 && zRotation_ <= 225)
00693 {
00694 *deltaX = -*deltaX;
00695 *deltaY = -*deltaY;
00696 }
00697
00698
00699 if( this->isXBackFlipped() )
00700 *deltaX = -*deltaX;
00701
00702 if( this->isYBackFlipped() )
00703 *deltaY = -*deltaY;
00704 }
00705
00706
00707 bool Detector::switchXYFromLocaToGlobal(void)
00708 {
00709
00710 if( (zRotation_ > 225 && zRotation_ < 315) || (zRotation_ > 45 && zRotation_ < 135) ) return true;
00711 return false;
00712 }
00713
00714 double Detector::getXRotation(bool global)
00715 {
00716 if(global) return xRotation_;
00717 else
00718 {
00719
00720 if (zRotation_ > 225 && zRotation_ < 315 ) return -yRotation_;
00721
00722 else if(zRotation_ > 45 && zRotation_ < 135 ) return yRotation_;
00723
00724 else if(zRotation_ >= 135 && zRotation_ <= 225) return -xRotation_;
00725
00726 else return xRotation_;
00727 }
00728 }
00729
00730
00731 double Detector::getYRotation(bool global)
00732 {
00733 if (global) return yRotation_;
00734 else
00735 {
00736
00737 if (zRotation_ > 225 && zRotation_ < 315 ) return -xRotation_;
00738
00739 else if(zRotation_ > 45 && zRotation_ < 135 ) return xRotation_;
00740
00741 else if(zRotation_ >= 135 && zRotation_ <= 225) return -yRotation_;
00742
00743 else return yRotation_;
00744 }
00745 }
00746
00747
00748 double Detector::fromLocalToGlobal(double* x, double* y, double* z, double* xErr, double* yErr, double* zErr)
00749 {
00750
00751 this->flipPositionLocal(x,y,xErr,yErr);
00752 this->translateXY(x,y);
00753 this->translateCorrection(x,y);
00754 Detector::matrix33Def R = this->getRotationMatrix();
00755
00756 double sigxy = R(0,0)*R(1,0)*pow(*xErr,2)+R(0,1)*R(1,1)*pow(*yErr,2);
00757
00758
00759
00760
00761 this->XYZRotation(x,y,z,xErr,yErr,zErr);
00762 *z += this->getZPositionTotal();
00763 return sigxy;
00764 }
00765
00766
00767 void Detector::fromLocalToGlobal(double* x, double* y, double* z)
00768 {
00769
00770 this->flipPositionLocal(x,y);
00771 this->translateXY(x,y);
00772 this->translateCorrection(x,y);
00773
00774
00775
00776
00777 this->XYZRotation(x,y,z);
00778 *z += this->getZPositionTotal();
00779 }
00780
00781
00782 void Detector::fromLocalToGlobalNoRotation(double* x, double* y,double* xErr, double* yErr)
00783 {
00784 this->flipPositionLocal(x,y,xErr,yErr);
00785 this->translateXY(x,y);
00786 this->translateCorrection(x,y);
00787 }
00788
00789
00790 void Detector::fromGlobalToLocal(double* x, double* y, double* z, double* xErr, double *yErr, double* zErr)
00791 {
00792 *z -= this->getZPositionTotal();
00793
00794
00795
00796
00797 this->XYZRotation(x,y,z,xErr,yErr,zErr,true);
00798
00799 this->translateXY(x,y,true);
00800 this->translateCorrection(x,y,true);
00801 this->flipBackPositionLocal(x,y,xErr,yErr);
00802 *z = 0;
00803 *zErr = 0;
00804 }
00805
00806
00807 void Detector::fromGlobalToLocal(double* x, double* y, double* z)
00808 {
00809 *z -= this->getZPositionTotal();
00810
00811
00812
00813
00814 this->XYZRotation(x,y,z,true);
00815
00816 this->translateXY(x,y,true);
00817 this->translateCorrection(x,y,true);
00818 this->flipBackPositionLocal(x,y);
00819 *z = 0;
00820 }
00821
00822
00823 void Detector::XYZRotation(double* x, double* y, double* z, double* xErr, double* yErr, double* zErr, bool backward)
00824 {
00825 XYZRotation(x,y,z,backward);
00826 XYZRotation(xErr,yErr,zErr,backward);
00827 }
00828
00829
00830 void Detector::XYZRotation(double* x, double* y, double* z, bool backward)
00831 {
00832 Detector::matrix33Def R = this->getRotationMatrix();
00833
00834 double xv[3] = {*x,*y,*z};
00835 double* xPrimev[3] = {x,y,z};
00836
00837 for(int i=0; i<3; i++)
00838 {
00839 *(xPrimev[i]) = 0;
00840 for(int j=0; j<3; j++)
00841 if(backward)
00842 *(xPrimev[i]) += R(j,i)*xv[j];
00843 else
00844 *(xPrimev[i]) += R(i,j)*xv[j];
00845 }
00846 }
00847
00848
00849 void Detector::XRotation(double* y, double* z, double* yErr, double* zErr, bool backward)
00850 {
00851 XRotation(y,z,backward);
00852 XRotation(yErr,zErr,backward);
00853
00854
00855
00856
00857
00858
00859
00860
00861
00862
00863
00864
00865
00866
00867
00868
00869
00870
00871 }
00872
00873
00874 void Detector::XRotation(double* y, double* z, bool backward)
00875 {
00876 double yTmp = *y;
00877 double zTmp = *z;
00878 double alpha = (xRotation_ + xRotationCorrection_)*PI/180. ;
00879
00880 if(backward)
00881 alpha *= -1;
00882
00883 *y = (yTmp)*cos(alpha) - (zTmp)*sin(alpha);
00884 *z = (yTmp)*sin(alpha) + (zTmp)*cos(alpha);
00885 }
00886
00887
00888 void Detector::YRotation(double *x, double *z, double *xErr, double *zErr, bool backward)
00889 {
00890 YRotation(x,z,backward);
00891 YRotation(xErr,zErr,backward);
00892
00893
00894
00895
00896
00897
00898
00899
00900
00901
00902
00903
00904
00905
00906
00907
00908
00909
00910 }
00911
00912
00913 void Detector::YRotation(double *x, double *z, bool backward)
00914 {
00915 double xTmp = *x;
00916 double zTmp = *z;
00917 double beta = (yRotation_+ yRotationCorrection_)*PI/180. ;
00918
00919 if(backward)
00920 beta *= -1;
00921
00922 *x = (xTmp)*cos(beta) + (zTmp)*sin(beta);
00923 *z = -(xTmp)*sin(beta) + (zTmp)*cos(beta);
00924 }
00925
00926
00927 void Detector::ZRotation(double* x, double* y, double* xErr, double* yErr, bool backward)
00928 {
00929 ZRotation(x,y,backward);
00930 ZRotation(xErr,yErr,backward);
00931
00932
00933
00934
00935
00936
00937
00938
00939
00940
00941
00942
00943
00944
00945
00946
00947
00948
00949 }
00950
00951
00952 void Detector::ZRotation(double* x, double* y, bool backward)
00953 {
00954 double xTmp = *x;
00955 double yTmp = *y;
00956 double gamma = (zRotationCorrection_)*PI/180.;
00957
00958 if(backward)
00959 gamma *= -1;
00960
00961 *x = (xTmp)*cos(gamma) - (yTmp)*sin(gamma);
00962 *y = (xTmp)*sin(gamma) + (yTmp)*cos(gamma);
00963 }
00964
00965
00966 void Detector::translateXY(double* x, double* y, bool backward)
00967 {
00968 if(backward)
00969 {
00970 *x += xPosition_ ;
00971 *y += yPosition_ ;
00972 }
00973 else
00974 {
00975 *x -= xPosition_ ;
00976 *y -= yPosition_ ;
00977 }
00978 }
00979
00980
00981 void Detector::translateCorrection(double* x, double* y, bool backward)
00982 {
00983 if(backward)
00984 {
00985 *x += xPositionCorrection_ ;
00986 *y += yPositionCorrection_ ;
00987 }
00988 else
00989 {
00990 *x -= xPositionCorrection_ ;
00991 *y -= yPositionCorrection_ ;
00992 }
00993 }
00994
00995
00996 Detector::matrix33Def Detector::getRotationMatrix()
00997 {
00998 double alpha,beta,gamma;
00999 alpha = (xRotation_ + xRotationCorrection_)*PI/180. ;
01000 beta = (yRotation_ + yRotationCorrection_)*PI/180. ;
01001 gamma = (zRotationCorrection_)*PI/180. ;
01002
01003 return Detector::rotationMatrix(alpha, beta, gamma);
01004 }
01005
01006
01007 double Detector::getAlignmentPredictedGlobal(ROOT::Math::SVector<double,4>& trackPars, matrix33Def& RInv, double z, double& predX, double& predY)
01008 {
01009 double numX,numY,den;
01010 numX=(trackPars[0]*z+trackPars[1])*(RInv[1][1]-trackPars[2]*RInv[2][1])-(trackPars[2]*z+trackPars[3])*(RInv[0][1]-trackPars[0]*RInv[2][1]);
01011 numY=(trackPars[2]*z+trackPars[3])*(RInv[0][0]-trackPars[0]*RInv[2][0])-(trackPars[0]*z+trackPars[1])*(RInv[1][0]-trackPars[2]*RInv[2][0]);
01012 den =(RInv[0][0]-trackPars[0]*RInv[2][0])*(RInv[1][1]-trackPars[2]*RInv[2][1])-(RInv[1][0]-trackPars[2]*RInv[2][0])*(RInv[0][1]-trackPars[0]*RInv[2][1]);
01013 predX = numX / den;
01014 predY = numY / den;
01015 return den;
01016 }
01017
01018
01019 void Detector::getPredictedGlobal(ROOT::Math::SVector<double,4>& trackPars, double& predX, double& predY, double& predZ)
01020 {
01021 double x0 = 0;
01022 double y0 = 0;
01023 double z0 = 0;
01024 fromLocalToGlobal(&x0,&y0,&z0);
01025
01026 Detector::matrix33Def R = this->getRotationMatrix();
01027 predZ = (R(0,2)*(x0-trackPars[1])+R(1,2)*(y0-trackPars[3])+R(2,2)*z0)/(R(0,2)*trackPars[0]+R(1,2)*trackPars[2]+R(2,2));
01028
01029 predX = trackPars[0]*predZ + trackPars[1];
01030 predY = trackPars[2]*predZ + trackPars[3];
01031
01032
01033
01034
01035
01036
01037
01038
01039
01040
01041
01042
01043
01044
01045
01046
01047
01048
01049
01050
01051
01052
01053
01054
01055
01056
01057
01058
01059
01060
01061
01062
01063
01064
01065
01066
01067
01068
01069
01070
01071
01072
01073
01074
01075
01076
01077
01078
01079
01080
01081
01082
01083
01084
01085
01086
01087
01088
01089 }
01090
01091
01092 void Detector::getPredictedLocal(ROOT::Math::SVector<double,4>& trackPars, double& predX, double& predY)
01093 {
01094 double predZ = 0;
01095 getPredictedGlobal(trackPars,predX,predY,predZ);
01096 fromGlobalToLocal(&predX,&predY,&predZ);
01097 }
01098
01099
01100 Detector::xyPair Detector::getTrackErrorsOnPlane(ROOT::Math::SVector<double,4>& trackPars, matrix44Def& AtVAInv)
01101 {
01102 Detector::matrix33Def RInv = this->getRotationMatrix();
01103 return propagateTrackErrors(trackPars, AtVAInv, RInv, this->getZPositionTotal());
01104 }
01105
01106
01107 Detector::xyPair Detector::propagateTrackErrors(ROOT::Math::SVector<double,4>& trackPars,
01108 matrix44Def& AtVAInv,
01109 matrix33Def& RInv,
01110 double z)
01111 {
01112 double predX,predY;
01113 double predSigmaXX,predSigmaYY;
01114
01115 double den = getAlignmentPredictedGlobal(trackPars,RInv,z,predX,predY);
01116
01117 double dfNX_dslx = z*(RInv[1][1]-trackPars[2]*RInv[2][1])-(trackPars[2]*z+trackPars[3])*(-RInv[2][1]);
01118 double dfNX_dqx = (RInv[1][1]-trackPars[2]*RInv[2][1]);
01119 double dfNX_dsly = (trackPars[0]*z+trackPars[1])*(-RInv[2][1])-(z)*(RInv[0][1]-trackPars[0]*RInv[2][1]);
01120 double dfNX_dqy =-(RInv[0][1]-trackPars[0]*RInv[2][1]);
01121 double dfNY_dslx = (trackPars[2]*z+trackPars[3])*(-RInv[2][0])-(z+trackPars[1])*(RInv[1][0]-trackPars[2]*RInv[2][0]);
01122 double dfNY_dqx =-(RInv[1][0]-trackPars[2]*RInv[2][0]);
01123 double dfNY_dsly = (z)*(RInv[0][0]-trackPars[0]*RInv[2][0])-(trackPars[0]*z+trackPars[1])*(-RInv[2][0]);
01124 double dfNY_dqy = (RInv[0][0]-trackPars[0]*RInv[2][0]);
01125 double dfD_dslx = (-RInv[2][0])*(RInv[1][1]-trackPars[2]*RInv[2][1])-(RInv[1][0]-trackPars[2]*RInv[2][0])*(-RInv[2][1]);
01126
01127 double dfD_dsly = (RInv[0][0]-trackPars[0]*RInv[2][0])*(-RInv[2][1])-(-RInv[2][0])*(RInv[0][1]-trackPars[0]*RInv[2][1]);
01128
01129 double dpredx_dslx= dfNX_dslx/den-predX*dfD_dslx/den;
01130 double dpredx_dqx = dfNX_dqx/den;
01131 double dpredx_dsly= dfNX_dsly/den-predX*dfD_dsly/den;
01132 double dpredx_dqy = dfNX_dqy/den;
01133 double dpredy_dslx= dfNY_dslx/den-predY*dfD_dslx/den;
01134 double dpredy_dqx = dfNY_dqx/den;
01135 double dpredy_dsly= dfNY_dsly/den-predY*dfD_dsly/den;
01136 double dpredy_dqy = dfNY_dqy/den;
01137
01138 predSigmaXX = dpredx_dslx*dpredx_dslx*AtVAInv(0,0)+dpredx_dqx*dpredx_dqx*AtVAInv(1,1)+dpredx_dsly*dpredx_dsly*AtVAInv(2,2)+dpredx_dqy*dpredx_dqy*AtVAInv(3,3)+
01139 2*dpredx_dslx*dpredx_dqx*AtVAInv(0,1)+2*dpredx_dslx*dpredx_dsly*AtVAInv(0,2)+2*dpredx_dslx*dpredx_dqy*AtVAInv(0,3)+
01140 2*dpredx_dqx*dpredx_dsly*AtVAInv(1,2)+2*dpredx_dqx*dpredx_dqy*AtVAInv(1,3)+
01141 2*dpredx_dsly*dpredx_dqy*AtVAInv(2,3);
01142 predSigmaYY = dpredy_dslx*dpredy_dslx*AtVAInv(0,0)+dpredy_dqx*dpredy_dqx*AtVAInv(1,1)+dpredy_dsly*dpredy_dsly*AtVAInv(2,2)+dpredy_dqy*dpredy_dqy*AtVAInv(3,3)+
01143 2*dpredy_dslx*dpredy_dqx*AtVAInv(0,1)+2*dpredy_dslx*dpredy_dsly*AtVAInv(0,2)+2*dpredy_dslx*dpredy_dqy*AtVAInv(0,3)+
01144 2*dpredy_dqx*dpredy_dsly*AtVAInv(1,2)+2*dpredy_dqx*dpredy_dqy*AtVAInv(1,3)+
01145 2*dpredy_dsly*dpredy_dqy*AtVAInv(2,3);
01146
01147 return std::make_pair(predSigmaXX,predSigmaYY);
01148 }
01149
01150
01151 void Detector::test(double* x, double* y, double* z, double* xErr, double* yErr, double* zErr)
01152 {
01153 std::stringstream ss;
01154
01155
01156
01157
01158
01159
01160
01161
01162
01163
01164
01165
01166
01167
01168
01169
01170
01171
01172
01173
01174
01175
01176
01177
01178
01179
01180
01181
01182
01183
01184
01185
01186
01187
01188
01189
01190
01191
01192
01193
01194
01195
01196
01197
01198
01199
01200
01201
01202
01203
01204
01205
01206 double xc = *x;
01207 double yc = *y;
01208 double zc = *z;
01209 double xErrc = *xErr;
01210 double yErrc = *yErr;
01211 double zErrc = *zErr;
01212
01213
01214 Detector::matrix33Def R = this->getRotationMatrix();
01215
01216
01217 ss.str("");
01218 ss << "************************ " << ID_ << " **************************************";
01219 STDLINE(ss.str(),ACYellow);
01220 ss.str(""); ss << __PRETTY_FUNCTION__ << "Original Values->"
01221 << " x: " << *x << " y: " << *y << " z:" << *z
01222 << " xErr: " << *xErr << " yErr: " << *yErr << " zErr:" << *zErr;
01223 STDLINE(ss.str(),ACGreen);
01224
01225 this->flipPositionLocal(x,y,xErr,yErr);
01226 ss.str(""); ss << __PRETTY_FUNCTION__ << "After flipping ->"
01227 << " x: " << *x << " y: " << *y << " z:" << *z
01228 << " xErr: " << *xErr << " yErr: " << *yErr << " zErr:" << *zErr;
01229
01230
01231 this->translateXY(x,y);
01232 ss.str(""); ss << __PRETTY_FUNCTION__ << "After translate->"
01233 << " x: " << *x << " y: " << *y << " z:" << *z
01234 << " xErr: " << *xErr << " yErr: " << *yErr << " zErr:" << *zErr;
01235
01236
01237 this->translateCorrection(x,y);
01238 ss.str(""); ss << __PRETTY_FUNCTION__ << "After tran corr->"
01239 << " x: " << *x << " y: " << *y << " z:" << *z
01240 << " xErr: " << *xErr << " yErr: " << *yErr << " zErr:" << *zErr;
01241
01242
01243 xc = *x;
01244 yc = *y;
01245 zc = *z;
01246 xErrc = *xErr;
01247 yErrc = *yErr;
01248 zErrc = *zErr;
01249 this->XRotation(y,z,yErr,zErr);
01250 ss.str(""); ss << __PRETTY_FUNCTION__ << "After XRotation->"
01251 << " x: " << *x << " y: " << *y << " z:" << *z
01252 << " xErr: " << *xErr << " yErr: " << *yErr << " zErr:" << *zErr;
01253
01254
01255 this->YRotation(x,z,xErr,zErr);
01256 ss.str(""); ss << __PRETTY_FUNCTION__ << "After YRotation->"
01257 << " x: " << *x << " y: " << *y << " z:" << *z
01258 << " xErr: " << *xErr << " yErr: " << *yErr << " zErr:" << *zErr;
01259 STDLINE(ss.str(),ACRed);
01260
01261 this->ZRotation(x,y,xErr,yErr);
01262 ss.str(""); ss << __PRETTY_FUNCTION__ << "After ZRotation->"
01263 << " x: " << *x << " y: " << *y << " z:" << *z
01264 << " xErr: " << *xErr << " yErr: " << *yErr << " zErr:" << *zErr;
01265
01266
01267 this->XYZRotation(&xc,&yc,&zc,&xErrc,&yErrc,&zErrc);
01268 ss.str(""); ss << __PRETTY_FUNCTION__ << "After XYZRot ->"
01269 << " x: " << xc << " y: " << yc << " z:" << zc
01270 << " xErr: " << xErrc << " yErr: " << yErrc << " zErr:" << zErrc;
01271
01272
01273 *z += this->getZPositionTotal();
01274
01275 ss.str(""); ss << __PRETTY_FUNCTION__ << "Final ->"
01276 << " x: " << *x << " y: " << *y << " z:" << *z
01277 << " xErr: " << *xErr << " yErr: " << *yErr << " zErr:" << *zErr;
01278 STDLINE(ss.str(),ACPurple);
01279
01281 ss.str(""); ss << __PRETTY_FUNCTION__ << "Original Values->"
01282 << " x: " << *x << " y: " << *y << " z:" << *z
01283 << " xErr: " << *xErr << " yErr: " << *yErr << " zErr:" << *zErr;
01284
01285 *z -= this->getZPositionTotal();
01286
01287 this->ZRotation(x,y,xErr,yErr,true);
01288 ss.str(""); ss << __PRETTY_FUNCTION__ << "After ZRotation->"
01289 << " x: " << *x << " y: " << *y << " z:" << *z
01290 << " xErr: " << *xErr << " yErr: " << *yErr << " zErr:" << *zErr;
01291
01292
01293 this->YRotation(x,z,xErr,zErr,true);
01294 ss.str(""); ss << __PRETTY_FUNCTION__ << "After YRotation->"
01295 << " x: " << *x << " y: " << *y << " z:" << *z
01296 << " xErr: " << *xErr << " yErr: " << *yErr << " zErr:" << *zErr;
01297
01298
01299 this->XRotation(y,z,yErr,zErr,true);
01300 ss.str(""); ss << __PRETTY_FUNCTION__ << "After XRotation->"
01301 << " x: " << *x << " y: " << *y << " z:" << *z
01302 << " xErr: " << *xErr << " yErr: " << *yErr << " zErr:" << *zErr;
01303 STDLINE(ss.str(),ACRed);
01304
01305
01306 this->XYZRotation(&xc,&yc,&zc,&xErrc,&yErrc,&zErrc,true);
01307 ss.str(""); ss << __PRETTY_FUNCTION__ << "After XYZRot ->"
01308 << " x: " << xc << " y: " << yc << " z:" << zc
01309 << " xErr: " << xErrc << " yErr: " << yErrc << " zErr:" << zErrc;
01310
01311
01312 this->translateXY(x,y,true);
01313 ss.str(""); ss << __PRETTY_FUNCTION__ << "After translate->"
01314 << " x: " << *x << " y: " << *y << " z:" << *z
01315 << " xErr: " << *xErr << " yErr: " << *yErr << " zErr:" << *zErr;
01316
01317
01318 this->translateCorrection(x,y,true);
01319 ss.str(""); ss << __PRETTY_FUNCTION__ << "After tran corr->"
01320 << " x: " << *x << " y: " << *y << " z:" << *z
01321 << " xErr: " << *xErr << " yErr: " << *yErr << " zErr:" << *zErr;
01322
01323
01324 *z = 0;
01325 this->flipBackPositionLocal(x,y,xErr,yErr);
01326 ss.str(""); ss << __PRETTY_FUNCTION__ << "Final AfterFlip->"
01327 << " x: " << *x << " y: " << *y << " z:" << *z
01328 << " xErr: " << *xErr << " yErr: " << *yErr << " zErr:" << *zErr;
01329 STDLINE(ss.str(),ACGreen);
01330
01331 this->fromLocalToGlobal(x,y,z,xErr,yErr,zErr);
01332 ss.str(""); ss << __PRETTY_FUNCTION__ << "After DetToGlob->"
01333 << " x: " << *x << " y: " << *y << " z:" << *z
01334 << " xErr: " << *xErr << " yErr: " << *yErr << " zErr:" << *zErr;
01335 STDLINE(ss.str(),ACPurple);
01336
01337 this->fromGlobalToLocal(x,y,z,xErr,yErr,zErr);
01338 ss.str(""); ss << __PRETTY_FUNCTION__ << "After GlobToDet->"
01339 << " x: " << *x << " y: " << *y << " z:" << *z
01340 << " xErr: " << *xErr << " yErr: " << *yErr << " zErr:" << *zErr;
01341 STDLINE(ss.str(),ACGreen);
01342
01343 }
01344
01345
01346 void Detector::dump(void)
01347 {
01348 char buffer[128];
01349 sprintf (buffer, "Alpha -> Total = %10.2f (%10.2f + %7.2f) deg", getXRotation() + getXRotationCorrection(), getXRotation(), getXRotationCorrection());STDLINE(buffer,ACCyan);
01350 sprintf (buffer, "Beta -> Total = %10.2f (%10.2f + %7.2f) deg", getYRotation() + getYRotationCorrection(), getYRotation(), getYRotationCorrection());STDLINE(buffer,ACCyan);
01351 sprintf (buffer, "Gamma -> Total = %10.2f (%10.2f + %7.2f) deg", getZRotation() + getZRotationCorrection(), getZRotation(), getZRotationCorrection());STDLINE(buffer,ACCyan);
01352 sprintf (buffer, "X -> Total = %10.2f (%10.2f + %7.2f) um", 10*(getXPositionTotal()), 10*getXPosition(), 10*getXPositionCorrection());STDLINE(buffer,ACCyan);
01353 sprintf (buffer, "Y -> Total = %10.2f (%10.2f + %7.2f) um", 10*(getYPositionTotal()), 10*getYPosition(), 10*getYPositionCorrection());STDLINE(buffer,ACCyan);
01354 sprintf (buffer, "Z -> Total = %10.2f (%10.2f + %7.2f) um", 10*(getZPositionTotal()), 10*getZPosition(), 10*getZPositionCorrection());STDLINE(buffer,ACCyan);
01355 }