otsdaq  v1_01_02
 All Classes Namespaces Functions
Detector.cxx
1 /****************************************************************************
2 ** Authors: Dario Menasce, Stefano Terzo
3 **
4 ** I.N.F.N. Milan-Bicocca
5 ** Piazza della Scienza 3, Edificio U2
6 ** Milano, 20126
7 **
8 ****************************************************************************/
9 
10 #include "otsdaq-core/MonicelliInterface/Detector.h"
11 #include <TMath.h>
12 
13 #define PI TMath::Pi()
14 
15 using namespace monicelli;
16 
17 ClassImp(Detector)
18 
19 //===============================================================================
20 Detector::Detector (std::string ID, bool isDUT) :
21  ID_ (ID)
22  , isDUT_ (isDUT)
23  , xPosition_ (0)
24  , xPositionCorrection_ (0)
25  , xPositionError_ (0)
26  , yPosition_ (0)
27  , yPositionCorrection_ (0)
28  , yPositionError_ (0)
29  , zPosition_ (0)
30  , zPositionCorrection_ (0)
31  , zPositionError_ (0)
32  , xNumberOfROCs_ (0)
33  , yNumberOfROCs_ (0)
34  , numberOfROCs_ (0)
35  , numberOfRows_ (0)
36  , numberOfCols_ (0)
37  , xBackFlipped_ (false)
38  , yBackFlipped_ (false)
39  , xRotation_ (0)
40  , xRotationCorrection_ (0)
41  , xRotationCorrectionError_(0)
42  , yRotation_ (0)
43  , yRotationCorrection_ (0)
44  , yRotationCorrectionError_(0)
45  , zRotation_ (0)
46  , zRotationCorrection_ (0)
47  , zRotationCorrectionError_(0)
48 {
49 
50 }
51 
52 //===============================================================================
53 Detector::~Detector(void)
54 {
55 // STDLINE("Detector::~Detector",ACRed);
56  for(ROCsMapDef::iterator it=ROCsChipIDMap_.begin(); it!=ROCsChipIDMap_.end(); it++)
57  {
58  delete it->second;
59  }
60  ROCsChipIDMap_ .clear();
61  ROCsPositionMap_.clear();
62 // STDLINE("Detector::~Detector",ACGreen);
63 }
64 
65 //===============================================================================
66 void Detector::setupVariables(void)
67 {
68  numberOfCols_ = 0;
69  numberOfRows_ = 0;
70  for(ROCsMapDef::iterator it=ROCsChipIDMap_.begin(); it!=ROCsChipIDMap_.end(); it++)
71  {
72  unsigned int ROCPosition = it->second->getPosition();
73  unsigned int yPosition = ROCPosition/xNumberOfROCs_;//division between ints is what i want!
74 
75  it->second->setLocalYPosition(yPosition);
76  if( yPosition%2 != 0 ) it->second->setLocalXPosition((yPosition+1) * xNumberOfROCs_ - (ROCPosition+1));
77  else it->second->setLocalXPosition(ROCPosition - yPosition * xNumberOfROCs_) ;
78  if(it->second->getPositionLocalY() == 0)
79  numberOfCols_ += it->second->getNumberOfCols();
80  if(it->second->getPositionLocalX() == 0)
81  numberOfRows_ += it->second->getNumberOfRows();
82  it->second->getNumberOfCols();
83  }
84 
85  ROCsPositionMap_[0]->setFirstCol(0);
86  ROCsPositionMap_[0]->setLastCol(ROCsPositionMap_[0]->getNumberOfCols()-1);
87  for(unsigned int posX=1; posX < xNumberOfROCs_; posX++)
88  {
89  ROCsPositionMap_[posX]->setFirstCol(ROCsPositionMap_[(posX-1)]->getLastCol()+1);
90  ROCsPositionMap_[posX]->setLastCol (ROCsPositionMap_[posX]->getFirstCol()+ROCsPositionMap_[posX]->getNumberOfCols()-1);
91  }
92 
93  ROCsPositionMap_[0]->setFirstRow(0);
94  ROCsPositionMap_[0]->setLastRow(ROCsPositionMap_[0]->getNumberOfRows()-1);
95  for(unsigned int posY=xNumberOfROCs_; posY<yNumberOfROCs_*xNumberOfROCs_; posY+=xNumberOfROCs_)
96  {
97  ROCsPositionMap_[posY]->setFirstRow(ROCsPositionMap_[(posY-xNumberOfROCs_)]->getLastRow()+1);
98  ROCsPositionMap_[posY]->setLastRow (ROCsPositionMap_[posY]->getFirstRow()+ROCsPositionMap_[posY]->getNumberOfRows()-1);
99  }
100 
101  for(ROCsMapDef::iterator it=ROCsChipIDMap_.begin(); it!=ROCsChipIDMap_.end(); it++)
102  {
103  it->second->setFirstCol(ROCsPositionMap_[it->second->getPositionLocalX()]->getFirstCol());
104  it->second->setLastCol (ROCsPositionMap_[it->second->getPositionLocalX()]->getLastCol());
105  it->second->setFirstRow(ROCsPositionMap_[it->second->getPositionLocalY()*xNumberOfROCs_]->getFirstRow());
106  it->second->setLastRow (ROCsPositionMap_[it->second->getPositionLocalY()*xNumberOfROCs_]->getLastRow());
107  }
108 }
109 
110 //===============================================================================
111 Detector::matrix33Def Detector::rotationMatrix(double alpha, double beta, double gamma)
112 {
113  Detector::matrix33Def R;
114 
115  R(0,0) = cos(beta )*cos(gamma);
116  R(0,1) =-cos(alpha)*sin(gamma)+sin(alpha)*sin(beta )*cos(gamma);
117  R(0,2) = sin(alpha)*sin(gamma)+cos(alpha)*sin(beta )*cos(gamma);
118  R(1,0) = cos(beta)*sin(gamma);
119  R(1,1) = cos(alpha)*cos(gamma)+sin(alpha)*sin(beta)*sin(gamma);
120  R(1,2) =-sin(alpha)*cos(gamma)+cos(alpha)*sin(beta)*sin(gamma);
121  R(2,0) =-sin(beta);
122  R(2,1) = sin(alpha)*cos(beta);
123  R(2,2) = cos(alpha)*cos(beta);
124 
125  return R;
126 }
127 
128 //===============================================================================
129 ROC* Detector::addROC(unsigned int chipPosition, int chipID, unsigned int rotationDegrees)
130 {
131  if( chipPosition > numberOfROCs_ )
132  {
133  std::stringstream ss;
134  ss.str("");
135  ss << chipID;
136  STDLINE("ERROR: there is no ROC position: " + ss.str() + " in current detector!!",ACRed);
137  STDLINE("ERROR: Maybe you have to use setComposition first",ACRed);
138  }
139  if( ROCsChipIDMap_.find( chipID ) == ROCsChipIDMap_.end() && ROCsPositionMap_.find( chipID ) == ROCsPositionMap_.end() )
140  {
141  ROCsChipIDMap_ [ chipID ] = new ROC(chipPosition, chipID, rotationDegrees );
142  ROCsPositionMap_ [ chipPosition ] = ROCsChipIDMap_[ chipID ] ;
143  }
144  else
145  {
146  std::stringstream ss;
147  ss.str("");
148  ss << "WARNING: ROC: " << chipID << " in position " << chipPosition << "was already set!!";
149  STDLINE(ss.str(),ACRed);
150  }
151  return ROCsChipIDMap_[ chipID ];
152 }
153 
154 //===============================================================================
155 void Detector::setNumberOfROCs(unsigned int xNumberOfROCs, unsigned int yNumberOfROCs)
156 {
157  xNumberOfROCs_ = xNumberOfROCs;
158  yNumberOfROCs_ = yNumberOfROCs;
159  this->updateNumberOfROCs();
160 }
161 
162 //===============================================================================
163 unsigned int Detector::getNumberOfCols(bool global)
164 {
165  if( !global || (zRotation_ < 45 && zRotation_ >= 0) || (zRotation_ <= 360 && zRotation_ > 315) || (zRotation_ > 135 && zRotation_ < 225) )
166  return numberOfCols_;
167  else
168  return this->getNumberOfRows();
169 }
170 
171 //===============================================================================
172 unsigned int Detector::getNumberOfRows(bool global)
173 {
174  if( !global || (zRotation_ < 45 && zRotation_ >= 0) || (zRotation_ <= 360 && zRotation_ > 315) || (zRotation_ > 135 && zRotation_ < 225) )
175  return numberOfRows_;
176  else
177  return this->getNumberOfCols();
178 }
179 
180 //===============================================================================
181 ROC* Detector::getROC(int chipID)
182 {
183  if( ROCsChipIDMap_.find( chipID ) != ROCsChipIDMap_.end() )
184  return ROCsChipIDMap_[chipID] ;
185  else
186  {
187  STDLINE("WARNING: No ROC found",ACRed);
188  return 0;
189  }
190 }
191 
192 //===============================================================================
193 ROC* Detector::getROCByPosition(unsigned int chipPosition)
194 {
195  if( ROCsPositionMap_.find( chipPosition ) != ROCsPositionMap_.end() )
196  return ROCsPositionMap_[chipPosition] ;
197  else
198  {
199  STDLINE("WARNING: No ROC found",ACRed);
200  return 0;
201  }
202 }
203 
204 //===============================================================================
205 int Detector::getPixelColFromLocalX(double xLocal)
206 {
207  if(xLocal < 0 || xLocal >= this->getDetectorLengthX())
208  return -1;
209 
210  double l = 0;
211  ROC* roc = 0;
212  unsigned int col = 0;
213 
214  for(unsigned int posX=0; posX<xNumberOfROCs_; posX++)
215  {
216  roc = getROCByPosition(posX);
217  if( xLocal < l+roc->getLengthLocalX())
218  break;
219  else
220  {
221  l += roc->getLengthLocalX();
222  if(posX+1 == xNumberOfROCs_)
223  return -1;
224  col += roc->getNumberOfCols();
225  }
226 
227  }
228  for(unsigned int rocCol=0; rocCol<roc->getNumberOfCols(); rocCol++)
229  {
230  l += roc->getPixelPitchLocalX(rocCol);
231  if(xLocal < l)
232  return col+rocCol;
233  }
234  return -1;
235 }
236 
237 //==============================================================================
238 int Detector::getPixelRowFromLocalY(double yLocal)
239 {
240  if(yLocal < 0 || yLocal >= this->getDetectorLengthY())
241  return -1;
242 
243  double l=0 ;
244  for(unsigned int row=0; row<this->getNumberOfRows(); row++)
245  {
246  l+=this->getPixelPitchLocalY(row);
247  if(yLocal < l) return row;
248  }
249  return -1;
250 }
251 
252 //===============================================================================
253 Detector::rowColPair Detector::getPixelCellFromLocal(double x, double y)
254 {
255  Detector::rowColPair rowCol;
256  rowCol.first = this->getPixelRowFromLocalY(y);
257  rowCol.second = this->getPixelColFromLocalX(x);
258  return rowCol;
259 }
260 
261 //===============================================================================
262 Detector::rowColPair Detector::getPixelCellFromGlobal(double x, double y, double z)
263 {
264  fromGlobalToLocal(&x,&y,&z);
265  Detector::rowColPair rowCol;
266  rowCol.first = this->getPixelRowFromLocalY(y);
267  rowCol.second = this->getPixelColFromLocalX(x);
268  return rowCol;
269 }
270 
271 //===============================================================================
272 double Detector::getPixelCenterLocalX(unsigned int col)
273 {
274  return this->getPixelLowEdgeLocalX(col) + this->getPixelPitchLocalX(col)/2.;
275 }
276 
277 //===============================================================================
278 double Detector::getPixelCenterLocalY(unsigned int row)
279 {
280  return this->getPixelLowEdgeLocalY(row) + this->getPixelPitchLocalY(row)/2.;
281 }
282 
283 //===============================================================================
284 double Detector::getPixelLowEdgeLocalX(unsigned int col)
285 {
286  double lowEdge=0;
287  for (unsigned int pos=0; pos < xNumberOfROCs_; pos++)
288  {
289  ROC *roc = getROCByPosition(pos);
290 
291  if(col > roc->getLastCol())
292  lowEdge += roc->getLengthLocalX();
293  else
294  {
295  unsigned int rocRow = 0;
296  roc = this->convertPixelToROC(&rocRow,&col);
297  lowEdge += roc->getPixelLowEdgeLocalX(col) ;
298  return lowEdge;
299  }
300  }
301  STDLINE("WARNING: pixel column is out of range, returning detector's length X", ACRed);
302  return lowEdge;
303 }
304 
305 //===============================================================================
306 double Detector::getPixelLowEdgeLocalY(unsigned int row)
307 {
308  double lowEdge=0;
309  for (unsigned int pos=0; pos < numberOfROCs_ ; pos+=xNumberOfROCs_)
310  {
311  ROC *roc = getROCByPosition(pos);
312  if(row > roc->getLastRow())
313  lowEdge += roc->getLengthLocalY();
314  else
315  {
316  unsigned int rocCol = 0;
317  roc = this->convertPixelToROC(&row,&rocCol);
318  lowEdge += roc->getPixelLowEdgeLocalY(row) ;
319  return lowEdge;
320  }
321  }
322  STDLINE("WARNING: pixel row is out of range, returning detector's length Y", ACRed);
323  return lowEdge;
324 }
325 
326 //===============================================================================
327 double Detector::getPixelPitchLocalX(unsigned int col)
328 {
329  unsigned int rocRow = 0;
330  ROC* roc = this->convertPixelToROC(&rocRow,&col);
331  return roc->getPixelPitchLocalX(col);
332 }
333 
334 //===============================================================================
335 double Detector::getPixelPitchLocalY(unsigned int row)
336 {
337  unsigned int rocCol = 0;
338  ROC* roc = this->convertPixelToROC(&row,&rocCol);
339  return roc->getPixelPitchLocalY(row);
340 }
341 
342 //===============================================================================
343 double Detector::getDetectorLengthX(bool global)
344 {
345  if( !global || (zRotation_ < 45 && zRotation_ >= 0) || (zRotation_ <= 360 && zRotation_ > 315) || (zRotation_ > 135 && zRotation_ < 225) )
346  {
347  double length=0;
348  for (unsigned int pos=0; pos < xNumberOfROCs_; pos++)
349  length += getROCByPosition(pos)->getLengthLocalX();
350  return length;
351  }
352  else
353  return this->getDetectorLengthY(false);
354 }
355 
356 //===============================================================================
357 double Detector::getDetectorLengthY(bool global)
358 {
359  if( !global || (zRotation_ < 45 && zRotation_ >= 0) || (zRotation_ <= 360 && zRotation_ > 315) || (zRotation_ > 135 && zRotation_ < 225) )
360  {
361  double length=0;
362  for (unsigned int pos=0; pos < numberOfROCs_ ; pos+=xNumberOfROCs_)
363  length += getROCByPosition(pos)->getLengthLocalY();
364  return length;
365  }
366  else
367  return this->getDetectorLengthX(false);
368 
369 }
370 
371 //===============================================================================
372 unsigned int Detector::getROCPositionLocalX(int chipID)
373 {
374  return getROC(chipID)->getPositionLocalX();
375 }
376 
377 //===============================================================================
378 unsigned int Detector::getROCPositionLocalY(int chipID)
379 {
380  return getROC(chipID)->getPositionLocalY();
381 }
382 
383 //=================================================================
384 //From row and col of the ROC they belong to the row and col of the detector
385 void Detector::convertPixelFromROC(ROC* roc, unsigned int *row, unsigned int *col)
386 {
387  if(roc->getOrientation() == 0)
388  {
389  *row += roc->getFirstRow();
390  *col += roc->getFirstCol();
391  }
392  else if(roc->getOrientation() == 180)
393  {
394  *row = roc->getLastRow() - *row;
395  *col = roc->getLastCol() - *col;
396  }
397  else
398  {
399  STDLINE("ERROR: Only 0 and 180 degrees rotations are supported for a ROC!", ACRed);
400  exit(EXIT_FAILURE);
401  }
402 }
403 
404 //=================================================================
405 //From row and col of the detector to row and col of the ROC they belong to
406 ROC* Detector::convertPixelToROC(unsigned int *row, unsigned int *col)
407 {
408  ROC *roc = this->findROC(*row, *col);
409  if(roc->getOrientation() == 0)
410  {
411  *row -= roc->getFirstRow();
412  *col -= roc->getFirstCol();
413  }
414  else if(roc->getOrientation() == 180)
415  {
416  *row = roc->getLastRow() - *row;
417  *col = roc->getLastCol() - *col;
418  }
419  else
420  {
421  STDLINE("ERROR: Only 0 and 180 degrees rotations are supported for a ROC!", ACRed);
422  exit(EXIT_FAILURE);
423  }
424  return roc;
425 }
426 
427 //=================================================================
428 ROC* Detector::findROC(unsigned int row, unsigned int col)
429 {
430  unsigned int posX = getROCPositionLocalXFromCol(col);
431  unsigned int posY = getROCPositionLocalYFromRow(row);
432  if(posX != xNumberOfROCs_ && posY != yNumberOfROCs_)
433  {
434  if(posY%2 == 0)
435  return getROCByPosition(posX+posY*xNumberOfROCs_);
436  else
437  return getROCByPosition(xNumberOfROCs_-1-posX+posY*xNumberOfROCs_);
438  }
439  STDLINE("WARNING: pixel column or pixel row is out of detector's range, return NULL", ACRed);
440  return 0;
441 }
442 
443 //=================================================================
444 unsigned int Detector::getROCPositionLocalXFromCol(unsigned int col)
445 {
446  unsigned int posX = 0;
447  for (posX=0; posX < xNumberOfROCs_; posX++)
448  if ( col <= getROCByPosition(posX)->getLastCol() )
449  break;
450  return posX;
451 }
452 
453 //=================================================================
454 unsigned int Detector::getROCPositionLocalYFromRow(unsigned int row)
455 {
456  unsigned int posY = 0;
457  for (posY=0; posY < numberOfROCs_; posY+=xNumberOfROCs_)
458  if ( row <= getROCByPosition(posY)->getLastRow() )
459  break;
460  return posY/xNumberOfROCs_;
461 }
462 
463 //=================================================================
464 void Detector::flipPixel(unsigned int *row, unsigned int *col)
465 {
466  unsigned int lastRow = this->getLastRow();
467  unsigned int lastCol = this->getLastCol();
468 
469  //reflection through orizonatal center of symmetry
470  if( this->isXBackFlipped() )
471  *col = lastCol - *col;
472  //reflection through vertical center of symmetry
473  if( this->isYBackFlipped() )
474  *row = lastRow - *row ;
475 
476  //rotation around z axis
477  int tmp;
478  //vertical clockwise
479  if(zRotation_ > 225 && zRotation_ < 315)
480  {
481  tmp = *col ;
482  *col = *row ;
483  *row = lastCol - tmp ;
484  }
485  //vertical anti-clockwise
486  else if(zRotation_ > 45 && zRotation_ < 135)
487  {
488  tmp = *col ;
489  *col = lastRow - *row ;
490  *row = tmp ;
491  }
492  //orizontal anti-clockwise
493  else if(zRotation_ >= 135 && zRotation_ <= 225)
494  {
495  *row = lastRow - *row ;
496  *col = lastCol - *col ;
497  }
498 }
499 
500 //=================================================================
501 void Detector::flipBackPixel(unsigned int *row, unsigned int *col )
502 {
503  unsigned int lastRow = this->getLastRow();
504  unsigned int lastCol = this->getLastCol();
505  //rotation around z axis
506  int tmp;
507  //vertical clockwise
508  if(zRotation_ > 225 && zRotation_ < 315)
509  {
510  tmp = *row ;
511  *row = *col ;
512  *col = lastCol - tmp ;
513  }
514  //vertical anti-clockwise
515  else if(zRotation_ > 45 && zRotation_ < 135)
516  {
517  tmp = *row ;
518  *row = lastRow - *col ;
519  *col = tmp ;
520  }
521  //orizontal anti-clockwise
522  else if(zRotation_ >= 135 && zRotation_ <= 225)
523  {
524  *col = lastCol - *col ;
525  *row = lastRow - *row ;
526  }
527 
528  //reflection through orizonatal center of symmetry
529  if( this->isXBackFlipped() )
530  *col = lastCol - *col;
531  //reflection through vertical center of symmetry
532  if( this->isYBackFlipped() )
533  *row = lastRow - *row ;
534 }
535 
536 //=================================================================
537 void Detector::flipPositionLocal(double *x, double *y, double *xErr, double *yErr)
538 {
539  double yLength = this->getDetectorLengthY();
540  double xLength = this->getDetectorLengthX();
541 
542  //ss_.str("");
543  //ss_ << "yLength: " << yLength << " xLength: " << xLength;
544  //STDLINE(ss_.str(),ACPurple);
545 
546 
547  //reflection through vertical center of symmetry
548  if( this->isXBackFlipped() )
549  *x = xLength - *x ;
550  //reflection through orizontal center of symmetry
551  if( this->isYBackFlipped() )
552  *y = yLength - *y ;
553  //rotation around z axis (only orizontal or vertical)
554  double tmp;
555  //vertical clockwise
556  if(zRotation_ > 225 && zRotation_ < 315)
557  {
558  tmp = *x ;
559  *x = *y ;
560  *y = xLength - tmp ;
561  if(xErr != 0 && yErr != 0)
562  {
563  tmp = *xErr ;
564  *xErr = *yErr ;
565  *yErr = tmp ;
566  }
567  }
568  //vertical anti-clockwise
569  else if(zRotation_ > 45 && zRotation_ < 135)
570  {
571  tmp = *x ;
572  *x = yLength - *y ;
573  *y = tmp ;
574  if(xErr != 0 && yErr != 0)
575  {
576  tmp = *xErr ;
577  *xErr = *yErr ;
578  *yErr = tmp ;
579  }
580  }
581  //orizontal anti-clockwise
582  else if(zRotation_ >= 135 && zRotation_ <= 225)
583  {
584  *y = yLength - *y ;
585  *x = xLength - *x ;
586  }
587 }
588 
589 //=================================================================
590 void Detector::flipBackPositionLocal(double *x, double *y, double *xErr, double *yErr)
591 {
592  double yLength = this->getDetectorLengthY();
593  double xLength = this->getDetectorLengthX();
594 
595  //rotation around z axis (only orizontal or vertical)
596  double tmp;
597  //vertical clockwise
598  if(zRotation_ > 225 && zRotation_ < 315)
599  {
600  tmp = *y ;
601  *y = *x ;
602  *x = xLength - tmp ;
603  if(xErr != 0 && yErr != 0)
604  {
605  tmp = *yErr ;
606  *yErr = *xErr ;
607  *xErr = tmp ;
608  }
609  }
610  //vertical anti-clockwise
611  else if(zRotation_ > 45 && zRotation_ < 135)
612  {
613  tmp = *y ;
614  *y = yLength - *x ;
615  *x = tmp ;
616  if(xErr != 0 && yErr != 0)
617  {
618  tmp = *yErr ;
619  *yErr = *xErr ;
620  *xErr = tmp ;
621  }
622  }
623  //orizontal anti-clockwise
624  else if(zRotation_ >= 135 && zRotation_ <= 225)
625  {
626  *x = xLength - *x ;
627  *y = yLength - *y ;
628  }
629 
630  //reflection through vertical center of symmetry
631  if( this->isXBackFlipped() )
632  *x = xLength - *x ;
633  //reflection through orizontal center of symmetry
634  if( this->isYBackFlipped() )
635  *y = yLength - *y ;
636 }
637 
638 //=================================================================
639 void Detector::flipDistance(double* deltaX, double* deltaY)
640 {
641  //reflection through vertical center of symmetry
642  if( this->isXBackFlipped() )
643  *deltaX = -*deltaX ;
644  //reflection through orizontal center of symmetry
645  if( this->isYBackFlipped() )
646  *deltaY = -*deltaY ;
647 
648  //rotation around z axis (only orizontal or vertical)
649  double tmp;
650  //vertical clockwise
651  if(zRotation_ > 225 && zRotation_ < 315)
652  {
653  tmp = *deltaX;
654  *deltaX = *deltaY;
655  *deltaY = tmp;
656  }
657  //vertical anti-clockwise
658  else if(zRotation_ > 45 && zRotation_ < 135)
659  {
660  tmp = *deltaX;
661  *deltaX = -*deltaY;
662  *deltaY = tmp;
663  }
664  //orizontal anti-clockwise
665  else if(zRotation_ >= 135 && zRotation_ <= 225)
666  {
667  *deltaY = -*deltaY;
668  *deltaX = -*deltaX;
669  }
670 }
671 
672 //=================================================================
673 void Detector::flipBackDistance(double* deltaX, double* deltaY)
674 {
675  //rotation around z axis (only orizontal or vertical)
676  double tmp;
677  //vertical clockwise
678  if(zRotation_ > 225 && zRotation_ < 315)
679  {
680  tmp = *deltaY;
681  *deltaY = *deltaX;
682  *deltaX = -tmp ;
683  }
684  //vertical anti-clockwise
685  else if(zRotation_ > 45 && zRotation_ < 135)
686  {
687  tmp = *deltaY ;
688  *deltaY = -*deltaX;
689  *deltaX = tmp ;
690  }
691  //orizontal anti-clockwise
692  else if(zRotation_ >= 135 && zRotation_ <= 225)
693  {
694  *deltaX = -*deltaX;
695  *deltaY = -*deltaY;
696  }
697 
698  //reflection through orizonatal center of symmetry
699  if( this->isXBackFlipped() )
700  *deltaX = -*deltaX;
701  //reflection through vertical center of symmetry
702  if( this->isYBackFlipped() )
703  *deltaY = -*deltaY;
704 }
705 
706 //=================================================================
707 bool Detector::switchXYFromLocaToGlobal(void)
708 {
709  //vertical clockwise
710  if( (zRotation_ > 225 && zRotation_ < 315) || (zRotation_ > 45 && zRotation_ < 135) ) return true;
711  return false;
712 }
713 //=================================================================
714 double Detector::getXRotation(bool global)
715 {
716  if(global) return xRotation_;
717  else
718  {
719  //vertical clockwise
720  if (zRotation_ > 225 && zRotation_ < 315 ) return -yRotation_;
721  //vertical anti-clockwise
722  else if(zRotation_ > 45 && zRotation_ < 135 ) return yRotation_;
723  //orizontal anti-clockwise
724  else if(zRotation_ >= 135 && zRotation_ <= 225) return -xRotation_;
725  //orizontal clockwise
726  else return xRotation_;
727  }
728 }
729 
730 //=================================================================
731 double Detector::getYRotation(bool global)
732 {
733  if (global) return yRotation_;
734  else
735  {
736  //vertical clockwise
737  if (zRotation_ > 225 && zRotation_ < 315 ) return -xRotation_;
738  //vertical anti-clockwise
739  else if(zRotation_ > 45 && zRotation_ < 135 ) return xRotation_;
740  //orizontal anti-clockwise
741  else if(zRotation_ >= 135 && zRotation_ <= 225) return -yRotation_;
742  //orizontal clockwise
743  else return yRotation_;
744  }
745 }
746 
747 //=======================================================================================
748 double Detector::fromLocalToGlobal(double* x, double* y, double* z, double* xErr, double* yErr, double* zErr)
749 {
750 
751  this->flipPositionLocal(x,y,xErr,yErr);
752  this->translateXY(x,y);
753  this->translateCorrection(x,y);
754  Detector::matrix33Def R = this->getRotationMatrix();
755 
756  double sigxy = R(0,0)*R(1,0)*pow(*xErr,2)+R(0,1)*R(1,1)*pow(*yErr,2);
757 
758  //this->XRotation(y,z,yErr,zErr);
759  //this->YRotation(x,z,xErr,zErr);
760  //this->ZRotation(x,y,xErr,yErr);
761  this->XYZRotation(x,y,z,xErr,yErr,zErr);
762  *z += this->getZPositionTotal();
763  return sigxy;
764 }
765 
766 //=======================================================================================
767 void Detector::fromLocalToGlobal(double* x, double* y, double* z)
768 {
769 
770  this->flipPositionLocal(x,y);
771  this->translateXY(x,y);
772  this->translateCorrection(x,y);
773 
774  //this->XRotation(y,z);
775  //this->YRotation(x,z);
776  //this->ZRotation(x,y);
777  this->XYZRotation(x,y,z);
778  *z += this->getZPositionTotal();
779 }
780 
781 //=======================================================================================
782 void Detector::fromLocalToGlobalNoRotation(double* x, double* y,double* xErr, double* yErr)
783 {
784  this->flipPositionLocal(x,y,xErr,yErr);
785  this->translateXY(x,y);
786  this->translateCorrection(x,y);
787 }
788 
789 //=======================================================================================
790 void Detector::fromGlobalToLocal(double* x, double* y, double* z, double* xErr, double *yErr, double* zErr)
791 {
792  *z -= this->getZPositionTotal();
793 
794  //this->ZRotation(x,y,xErr,yErr,true);
795  //this->YRotation(x,z,xErr,zErr,true);
796  //this->XRotation(y,z,yErr,zErr,true);
797  this->XYZRotation(x,y,z,xErr,yErr,zErr,true);
798 
799  this->translateXY(x,y,true);
800  this->translateCorrection(x,y,true);
801  this->flipBackPositionLocal(x,y,xErr,yErr);
802  *z = 0;
803  *zErr = 0;
804 }
805 
806 //=======================================================================================
807 void Detector::fromGlobalToLocal(double* x, double* y, double* z)
808 {
809  *z -= this->getZPositionTotal();
810 
811  //this->ZRotation(x,y,xErr,yErr,true);
812  //this->YRotation(x,z,xErr,zErr,true);
813  //this->XRotation(y,z,yErr,zErr,true);
814  this->XYZRotation(x,y,z,true);
815 
816  this->translateXY(x,y,true);
817  this->translateCorrection(x,y,true);
818  this->flipBackPositionLocal(x,y);
819  *z = 0;
820 }
821 
822 //=======================================================================================
823 void Detector::XYZRotation(double* x, double* y, double* z, double* xErr, double* yErr, double* zErr, bool backward)
824 {
825  XYZRotation(x,y,z,backward);
826  XYZRotation(xErr,yErr,zErr,backward);
827 }
828 
829 //=======================================================================================
830 void Detector::XYZRotation(double* x, double* y, double* z, bool backward)
831 {
832  Detector::matrix33Def R = this->getRotationMatrix();
833 
834  double xv[3] = {*x,*y,*z};
835  double* xPrimev[3] = {x,y,z};
836 
837  for(int i=0; i<3; i++)
838  {
839  *(xPrimev[i]) = 0;
840  for(int j=0; j<3; j++)
841  if(backward)
842  *(xPrimev[i]) += R(j,i)*xv[j];
843  else
844  *(xPrimev[i]) += R(i,j)*xv[j];
845  }
846 }
847 
848 //=======================================================================================
849 void Detector::XRotation(double* y, double* z, double* yErr, double* zErr, bool backward)
850 {
851  XRotation(y,z,backward);
852  XRotation(yErr,zErr,backward);
853  //*yErr = fabs(*yErr);
854  //*zErr = fabs(*zErr);
855  /*
856  double yTmp = *y ;
857  double zTmp = *z ;
858  double yErrTmp = *yErr ;
859  double zErrTmp = *zErr ;
860  double alpha = (xRotation_ + xRotationCorrection_)*PI/180. ;
861 
862  if(backward)
863  alpha *= -1;
864 
865  *y = (yTmp)*cos(alpha) - (zTmp)*sin(alpha);
866  *z = (yTmp)*sin(alpha) + (zTmp)*cos(alpha);
867 
868  *yErr = sqrt( pow(yErrTmp*cos(alpha),2) + pow(zErrTmp*sin(alpha),2) );
869  *zErr = sqrt( pow(yErrTmp*sin(alpha),2) + pow(zErrTmp*cos(alpha),2) );
870  */
871 }
872 
873 //=======================================================================================
874 void Detector::XRotation(double* y, double* z, bool backward)
875 {
876  double yTmp = *y;
877  double zTmp = *z;
878  double alpha = (xRotation_ + xRotationCorrection_)*PI/180. ;
879 
880  if(backward)
881  alpha *= -1;
882 
883  *y = (yTmp)*cos(alpha) - (zTmp)*sin(alpha);
884  *z = (yTmp)*sin(alpha) + (zTmp)*cos(alpha);
885 }
886 
887 //=======================================================================================
888 void Detector::YRotation(double *x, double *z, double *xErr, double *zErr, bool backward)
889 {
890  YRotation(x,z,backward);
891  YRotation(xErr,zErr,backward);
892  //*xErr = fabs(*xErr);
893  //*zErr = fabs(*zErr);
894  /*
895  double xTmp = *x;
896  double zTmp = *z;
897  double xErrTmp = *xErr;
898  double zErrTmp = *zErr;
899  double beta = (yRotation_+ yRotationCorrection_)*PI/180. ;
900 
901  if(backward)
902  beta *= -1;
903 
904  *x = (xTmp)*cos(beta) + (zTmp)*sin(beta);
905  *z = -(xTmp)*sin(beta) + (zTmp)*cos(beta);
906 
907  *xErr = sqrt( pow(xErrTmp*cos(beta),2) + pow(zErrTmp*sin(beta),2) );
908  *zErr = sqrt( pow(xErrTmp*sin(beta),2) + pow(zErrTmp*cos(beta),2) );
909  */
910 }
911 
912 //=======================================================================================
913 void Detector::YRotation(double *x, double *z, bool backward)
914 {
915  double xTmp = *x;
916  double zTmp = *z;
917  double beta = (yRotation_+ yRotationCorrection_)*PI/180. ;
918 
919  if(backward)
920  beta *= -1;
921 
922  *x = (xTmp)*cos(beta) + (zTmp)*sin(beta);
923  *z = -(xTmp)*sin(beta) + (zTmp)*cos(beta);
924 }
925 
926 //=======================================================================================
927 void Detector::ZRotation(double* x, double* y, double* xErr, double* yErr, bool backward)
928 {
929  ZRotation(x,y,backward);
930  ZRotation(xErr,yErr,backward);
931  //*xErr = fabs(*xErr);
932  //*yErr = fabs(*yErr);
933  /*
934  double xTmp = *x;
935  double yTmp = *y;
936  double xErrTmp = *xErr;
937  double yErrTmp = *yErr;
938  double gamma = (zRotationCorrection_)*PI/180.;
939 
940  if(backward)
941  gamma *= -1;
942 
943  *x = (xTmp)*cos(gamma) - (yTmp)*sin(gamma);
944  *y = (xTmp)*sin(gamma) + (yTmp)*cos(gamma);
945 
946  *xErr = sqrt( pow(xErrTmp*cos(gamma),2) + pow(yErrTmp*sin(gamma),2) );
947  *yErr = sqrt( pow(xErrTmp*sin(gamma),2) + pow(yErrTmp*cos(gamma),2) );
948  */
949 }
950 
951 //=======================================================================================
952 void Detector::ZRotation(double* x, double* y, bool backward)
953 {
954  double xTmp = *x;
955  double yTmp = *y;
956  double gamma = (zRotationCorrection_)*PI/180.;
957 
958  if(backward)
959  gamma *= -1;
960 
961  *x = (xTmp)*cos(gamma) - (yTmp)*sin(gamma);
962  *y = (xTmp)*sin(gamma) + (yTmp)*cos(gamma);
963 }
964 
965 //=======================================================================================
966 void Detector::translateXY(double* x, double* y, bool backward)
967 {
968  if(backward)
969  {
970  *x += xPosition_ ;
971  *y += yPosition_ ;
972  }
973  else
974  {
975  *x -= xPosition_ ;
976  *y -= yPosition_ ;
977  }
978 }
979 
980 //=======================================================================================
981 void Detector::translateCorrection(double* x, double* y, bool backward)
982 {
983  if(backward)
984  {
985  *x += xPositionCorrection_ ;
986  *y += yPositionCorrection_ ;
987  }
988  else
989  {
990  *x -= xPositionCorrection_ ;
991  *y -= yPositionCorrection_ ;
992  }
993 }
994 
995 //================================================================
996 Detector::matrix33Def Detector::getRotationMatrix()
997 {
998  double alpha,beta,gamma;
999  alpha = (xRotation_ + xRotationCorrection_)*PI/180. ;
1000  beta = (yRotation_ + yRotationCorrection_)*PI/180. ;
1001  gamma = (zRotationCorrection_)*PI/180. ;
1002 
1003  return Detector::rotationMatrix(alpha, beta, gamma);
1004 }
1005 
1006 //================================================================
1007 double Detector::getAlignmentPredictedGlobal(ROOT::Math::SVector<double,4>& trackPars, matrix33Def& RInv, double z, double& predX, double& predY)
1008 {
1009  double numX,numY,den;
1010  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]);
1011  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]);
1012  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]);
1013  predX = numX / den;
1014  predY = numY / den;
1015  return den;
1016 }
1017 
1018 //================================================================
1019 void Detector::getPredictedGlobal(ROOT::Math::SVector<double,4>& trackPars, double& predX, double& predY, double& predZ)
1020 {
1021  double x0 = 0;
1022  double y0 = 0;
1023  double z0 = 0;
1024  fromLocalToGlobal(&x0,&y0,&z0);
1025 
1026  Detector::matrix33Def R = this->getRotationMatrix();
1027  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));
1028 
1029  predX = trackPars[0]*predZ + trackPars[1];
1030  predY = trackPars[2]*predZ + trackPars[3];
1031  /*
1032  STDLINE("------------------------------------------------------",ACWhite);
1033  std::stringstream ss;
1034  ss.str("");
1035  ss << "X: " << predX << " Y: " << predY
1036  << " local2Global: " << z0
1037  << " predZ: " << predZ
1038  << " zPos: " << this->getZPosition();
1039  STDLINE(ss.str(),ACGreen);
1040 
1041  //x0 = 0;
1042  //y0 = 0;
1043  //z0 = 0;
1044  //fromLocalToGlobalNoRotation(&x0,&y0,&z0);
1045  //z0 = predZ;
1046 
1047  double N_X0 = (trackPars[0]*z0 + trackPars[1]) * (R(1,1) - trackPars[2]*R(2,1)) - (trackPars[2]*z0 + trackPars[3]) * (R(0,1) - trackPars[0]*R(2,1));
1048  double N_Y0 = (trackPars[2]*z0 + trackPars[3]) * (R(0,0) - trackPars[0]*R(2,0)) - (trackPars[0]*z0 + trackPars[1]) * (R(1,0) - trackPars[2]*R(2,0));
1049  double D_0 = (R(0,0) - trackPars[0]*R(2,0)) * (R(1,1) - trackPars[2]*R(2,1)) - (R(1,0) - trackPars[2]*R(2,0)) * (R(0,1) - trackPars[0]*R(2,1));
1050  double predXX = N_X0 / D_0;
1051  double predYY = N_Y0 / D_0;
1052 
1053  ss.str("");
1054  ss << "X: " << predXX << " Y: " << predYY << " local2Global: " << z0;
1055  STDLINE(ss.str(),ACRed);
1056 
1057  z0 = predZ;
1058 
1059  N_X0 = (trackPars[0]*z0 + trackPars[1]) * (R(1,1) - trackPars[2]*R(2,1)) - (trackPars[2]*z0 + trackPars[3]) * (R(0,1) - trackPars[0]*R(2,1));
1060  N_Y0 = (trackPars[2]*z0 + trackPars[3]) * (R(0,0) - trackPars[0]*R(2,0)) - (trackPars[0]*z0 + trackPars[1]) * (R(1,0) - trackPars[2]*R(2,0));
1061  D_0 = (R(0,0) - trackPars[0]*R(2,0)) * (R(1,1) - trackPars[2]*R(2,1)) - (R(1,0) - trackPars[2]*R(2,0)) * (R(0,1) - trackPars[0]*R(2,1));
1062  predXX = N_X0 / D_0;
1063  predYY = N_Y0 / D_0;
1064 
1065  ss.str("");
1066  ss << "X: " << predXX << " Y: " << predYY << " predicted z : " << z0;
1067  STDLINE(ss.str(),ACRed);
1068 
1069  z0 = this->getZPosition();
1070 
1071  N_X0 = (trackPars[0]*z0 + trackPars[1]) * (R(1,1) - trackPars[2]*R(2,1)) - (trackPars[2]*z0 + trackPars[3]) * (R(0,1) - trackPars[0]*R(2,1));
1072  N_Y0 = (trackPars[2]*z0 + trackPars[3]) * (R(0,0) - trackPars[0]*R(2,0)) - (trackPars[0]*z0 + trackPars[1]) * (R(1,0) - trackPars[2]*R(2,0));
1073  D_0 = (R(0,0) - trackPars[0]*R(2,0)) * (R(1,1) - trackPars[2]*R(2,1)) - (R(1,0) - trackPars[2]*R(2,0)) * (R(0,1) - trackPars[0]*R(2,1));
1074  predXX = N_X0 / D_0;
1075  predYY = N_Y0 / D_0;
1076 
1077  ss.str("");
1078  ss << "X: " << predXX << " Y: " << predYY << " get z positi: " << z0;
1079  STDLINE(ss.str(),ACRed);
1080 
1081  //this->translateXY(&predXX,&predYY,true);
1082  //this->translateCorrection(&predXX,&predYY,true);
1083 
1084  //ss.str("");
1085  //ss << "X: " << predXX << " Y: " << predYY << " Z0: " << z0;
1086  //STDLINE(ss.str(),ACPurple);
1087  STDLINE("------------------------------------------------------",ACWhite);
1088 */
1089 }
1090 
1091 //================================================================
1092 void Detector::getPredictedLocal(ROOT::Math::SVector<double,4>& trackPars, double& predX, double& predY)
1093 {
1094  double predZ = 0;
1095  getPredictedGlobal(trackPars,predX,predY,predZ);
1096  fromGlobalToLocal(&predX,&predY,&predZ);
1097 }
1098 
1099 //======================================================================================================
1100 Detector::xyPair Detector::getTrackErrorsOnPlane(ROOT::Math::SVector<double,4>& trackPars, matrix44Def& AtVAInv)
1101 {
1102  Detector::matrix33Def RInv = this->getRotationMatrix();
1103  return propagateTrackErrors(trackPars, AtVAInv, RInv, this->getZPositionTotal());
1104 }
1105 
1106 //======================================================================================================
1107 Detector::xyPair Detector::propagateTrackErrors(ROOT::Math::SVector<double,4>& trackPars,
1108  matrix44Def& AtVAInv,
1109  matrix33Def& RInv,
1110  double z)
1111 {
1112  double predX,predY;
1113  double predSigmaXX,predSigmaYY;
1114 
1115  double den = getAlignmentPredictedGlobal(trackPars,RInv,z,predX,predY);
1116 
1117  double dfNX_dslx = z*(RInv[1][1]-trackPars[2]*RInv[2][1])-(trackPars[2]*z+trackPars[3])*(-RInv[2][1]);
1118  double dfNX_dqx = (RInv[1][1]-trackPars[2]*RInv[2][1]);
1119  double dfNX_dsly = (trackPars[0]*z+trackPars[1])*(-RInv[2][1])-(z)*(RInv[0][1]-trackPars[0]*RInv[2][1]);
1120  double dfNX_dqy =-(RInv[0][1]-trackPars[0]*RInv[2][1]);
1121  double dfNY_dslx = (trackPars[2]*z+trackPars[3])*(-RInv[2][0])-(z+trackPars[1])*(RInv[1][0]-trackPars[2]*RInv[2][0]);
1122  double dfNY_dqx =-(RInv[1][0]-trackPars[2]*RInv[2][0]);
1123  double dfNY_dsly = (z)*(RInv[0][0]-trackPars[0]*RInv[2][0])-(trackPars[0]*z+trackPars[1])*(-RInv[2][0]);
1124  double dfNY_dqy = (RInv[0][0]-trackPars[0]*RInv[2][0]);
1125  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]);
1126  //double dfD_dqx = 0;
1127  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]);
1128  //double dfD_dqy = 0;
1129  double dpredx_dslx= dfNX_dslx/den-predX*dfD_dslx/den;
1130  double dpredx_dqx = dfNX_dqx/den;
1131  double dpredx_dsly= dfNX_dsly/den-predX*dfD_dsly/den;
1132  double dpredx_dqy = dfNX_dqy/den;
1133  double dpredy_dslx= dfNY_dslx/den-predY*dfD_dslx/den;
1134  double dpredy_dqx = dfNY_dqx/den;
1135  double dpredy_dsly= dfNY_dsly/den-predY*dfD_dsly/den;
1136  double dpredy_dqy = dfNY_dqy/den;
1137 
1138  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)+
1139  2*dpredx_dslx*dpredx_dqx*AtVAInv(0,1)+2*dpredx_dslx*dpredx_dsly*AtVAInv(0,2)+2*dpredx_dslx*dpredx_dqy*AtVAInv(0,3)+
1140  2*dpredx_dqx*dpredx_dsly*AtVAInv(1,2)+2*dpredx_dqx*dpredx_dqy*AtVAInv(1,3)+
1141  2*dpredx_dsly*dpredx_dqy*AtVAInv(2,3);
1142  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)+
1143  2*dpredy_dslx*dpredy_dqx*AtVAInv(0,1)+2*dpredy_dslx*dpredy_dsly*AtVAInv(0,2)+2*dpredy_dslx*dpredy_dqy*AtVAInv(0,3)+
1144  2*dpredy_dqx*dpredy_dsly*AtVAInv(1,2)+2*dpredy_dqx*dpredy_dqy*AtVAInv(1,3)+
1145  2*dpredy_dsly*dpredy_dqy*AtVAInv(2,3);
1146 
1147  return std::make_pair(predSigmaXX,predSigmaYY);
1148 }
1149 
1150 //=======================================================================================
1151 void Detector::test(double* x, double* y, double* z, double* xErr, double* yErr, double* zErr)
1152 {
1153  std::stringstream ss;
1154  /*
1155  double xc1 = 0;
1156  double yc1 = 0;
1157  double zc1 = 0;
1158  double xErrc1 = 100;
1159  double yErrc1 = 100;
1160  double zErrc1 = 0;
1161  double xc2 = 100;
1162  double yc2 = 100;
1163  double zc2 = 0;
1164  double xErrc2 = 100;
1165  double yErrc2 = 100;
1166  double zErrc2 = 0;
1167  fromDetectorToGlobal(&xc1,&yc1,&zc1,&xErrc1,&yErrc1,&zErrc1);
1168  ss.str("");
1169  ss << "************************ " << ID_ << " **************************************";
1170  STDLINE(ss.str(),ACYellow);
1171  ss.str(""); ss << __PRETTY_FUNCTION__ << "After fromLocalToGlobal1->"
1172  << " x: " << xc1 << " y: " << yc1 << " z:" << zc1
1173  << " xErr: " << xErrc1 << " yErr: " << yErrc1 << " zErr:" << zErrc1;
1174  STDLINE(ss.str(),ACGreen);
1175 
1176  fromDetectorToGlobal(&xc2,&yc2,&zc2,&xErrc2,&yErrc2,&zErrc2);
1177  ss.str("");
1178  ss << "************************ " << ID_ << " **************************************";
1179  STDLINE(ss.str(),ACYellow);
1180  ss.str(""); ss << __PRETTY_FUNCTION__ << "After fromLocalToGlobal2->"
1181  << " x: " << xc2 << " y: " << yc2 << " z:" << zc2
1182  << " xErr: " << xErrc2 << " yErr: " << yErrc2 << " zErr:" << zErrc2;
1183  STDLINE(ss.str(),ACRed);
1184  ss.str("");
1185  ss << "************************ " << ID_ << " **************************************";
1186  STDLINE(ss.str(),ACYellow);
1187  ss.str(""); ss << __PRETTY_FUNCTION__ << "After DeltaGlobal ->"
1188  << " x: " << xc1-xc2 << " y: " << yc1-yc2 << " z:" << zc1-zc2
1189  << " xErr: " << xErrc1-xErrc2 << " yErr: " << yErrc1-yErrc2 << " zErr:" << zErrc1-zErrc2;
1190  STDLINE(ss.str(),ACRed);
1191  ss.str(""); ss << __PRETTY_FUNCTION__ << "After DeltaGlobal square->"
1192  << " x: " << pow(xc1-xc2,2) << " y: " << pow(yc1-yc2,2) << " z:" << pow(zc1-zc2,2)
1193  << " Tot: " << sqrt(pow(xc1-xc2,2)+pow(yc1-yc2,2)+pow(zc1-zc2,2)) << " = " << sqrt(2*pow(100,2));
1194  STDLINE(ss.str(),ACRed);
1195 
1196  fromGlobalToDetector(&xc1,&yc1,&zc1,&xErrc1,&yErrc1,&zErrc1);
1197  ss.str("");
1198  ss << "************************ " << ID_ << " **************************************";
1199  STDLINE(ss.str(),ACYellow);
1200  ss.str(""); ss << __PRETTY_FUNCTION__ << "After fromGlobalToLocal1->"
1201  << " x: " << xc1 << " y: " << yc1 << " z:" << zc1
1202  << " xErr: " << xErrc1 << " yErr: " << yErrc1 << " zErr:" << zErrc1;
1203  STDLINE(ss.str(),ACGreen);
1204 
1205 */
1206  double xc = *x;
1207  double yc = *y;
1208  double zc = *z;
1209  double xErrc = *xErr;
1210  double yErrc = *yErr;
1211  double zErrc = *zErr;
1212 
1213 
1214  Detector::matrix33Def R = this->getRotationMatrix();
1215 
1216 
1217  ss.str("");
1218  ss << "************************ " << ID_ << " **************************************";
1219  STDLINE(ss.str(),ACYellow);
1220  ss.str(""); ss << __PRETTY_FUNCTION__ << "Original Values->"
1221  << " x: " << *x << " y: " << *y << " z:" << *z
1222  << " xErr: " << *xErr << " yErr: " << *yErr << " zErr:" << *zErr;
1223  STDLINE(ss.str(),ACGreen);
1224 
1225  this->flipPositionLocal(x,y,xErr,yErr);
1226  ss.str(""); ss << __PRETTY_FUNCTION__ << "After flipping ->"
1227  << " x: " << *x << " y: " << *y << " z:" << *z
1228  << " xErr: " << *xErr << " yErr: " << *yErr << " zErr:" << *zErr;
1229  // STDLINE(ss.str(),ACRed);
1230 
1231  this->translateXY(x,y);
1232  ss.str(""); ss << __PRETTY_FUNCTION__ << "After translate->"
1233  << " x: " << *x << " y: " << *y << " z:" << *z
1234  << " xErr: " << *xErr << " yErr: " << *yErr << " zErr:" << *zErr;
1235  // STDLINE(ss.str(),ACRed);
1236 
1237  this->translateCorrection(x,y);
1238  ss.str(""); ss << __PRETTY_FUNCTION__ << "After tran corr->"
1239  << " x: " << *x << " y: " << *y << " z:" << *z
1240  << " xErr: " << *xErr << " yErr: " << *yErr << " zErr:" << *zErr;
1241  // STDLINE(ss.str(),ACGreen);
1242 
1243  xc = *x;
1244  yc = *y;
1245  zc = *z;
1246  xErrc = *xErr;
1247  yErrc = *yErr;
1248  zErrc = *zErr;
1249  this->XRotation(y,z,yErr,zErr);
1250  ss.str(""); ss << __PRETTY_FUNCTION__ << "After XRotation->"
1251  << " x: " << *x << " y: " << *y << " z:" << *z
1252  << " xErr: " << *xErr << " yErr: " << *yErr << " zErr:" << *zErr;
1253  // STDLINE(ss.str(),ACRed);
1254 
1255  this->YRotation(x,z,xErr,zErr);
1256  ss.str(""); ss << __PRETTY_FUNCTION__ << "After YRotation->"
1257  << " x: " << *x << " y: " << *y << " z:" << *z
1258  << " xErr: " << *xErr << " yErr: " << *yErr << " zErr:" << *zErr;
1259  STDLINE(ss.str(),ACRed);
1260 
1261  this->ZRotation(x,y,xErr,yErr);
1262  ss.str(""); ss << __PRETTY_FUNCTION__ << "After ZRotation->"
1263  << " x: " << *x << " y: " << *y << " z:" << *z
1264  << " xErr: " << *xErr << " yErr: " << *yErr << " zErr:" << *zErr;
1265  // STDLINE(ss.str(),ACRed);
1266 
1267  this->XYZRotation(&xc,&yc,&zc,&xErrc,&yErrc,&zErrc);
1268  ss.str(""); ss << __PRETTY_FUNCTION__ << "After XYZRot ->"
1269  << " x: " << xc << " y: " << yc << " z:" << zc
1270  << " xErr: " << xErrc << " yErr: " << yErrc << " zErr:" << zErrc;
1271  // STDLINE(ss.str(),ACRed);
1272 
1273  *z += this->getZPositionTotal();
1274 
1275  ss.str(""); ss << __PRETTY_FUNCTION__ << "Final ->"
1276  << " x: " << *x << " y: " << *y << " z:" << *z
1277  << " xErr: " << *xErr << " yErr: " << *yErr << " zErr:" << *zErr;
1278  STDLINE(ss.str(),ACPurple);
1279 
1281  ss.str(""); ss << __PRETTY_FUNCTION__ << "Original Values->"
1282  << " x: " << *x << " y: " << *y << " z:" << *z
1283  << " xErr: " << *xErr << " yErr: " << *yErr << " zErr:" << *zErr;
1284  // STDLINE(ss.str(),ACPurple);
1285  *z -= this->getZPositionTotal();
1286 
1287  this->ZRotation(x,y,xErr,yErr,true);
1288  ss.str(""); ss << __PRETTY_FUNCTION__ << "After ZRotation->"
1289  << " x: " << *x << " y: " << *y << " z:" << *z
1290  << " xErr: " << *xErr << " yErr: " << *yErr << " zErr:" << *zErr;
1291  // STDLINE(ss.str(),ACRed);
1292 
1293  this->YRotation(x,z,xErr,zErr,true);
1294  ss.str(""); ss << __PRETTY_FUNCTION__ << "After YRotation->"
1295  << " x: " << *x << " y: " << *y << " z:" << *z
1296  << " xErr: " << *xErr << " yErr: " << *yErr << " zErr:" << *zErr;
1297  // STDLINE(ss.str(),ACRed);
1298 
1299  this->XRotation(y,z,yErr,zErr,true);
1300  ss.str(""); ss << __PRETTY_FUNCTION__ << "After XRotation->"
1301  << " x: " << *x << " y: " << *y << " z:" << *z
1302  << " xErr: " << *xErr << " yErr: " << *yErr << " zErr:" << *zErr;
1303  STDLINE(ss.str(),ACRed);
1304 
1305 
1306  this->XYZRotation(&xc,&yc,&zc,&xErrc,&yErrc,&zErrc,true);
1307  ss.str(""); ss << __PRETTY_FUNCTION__ << "After XYZRot ->"
1308  << " x: " << xc << " y: " << yc << " z:" << zc
1309  << " xErr: " << xErrc << " yErr: " << yErrc << " zErr:" << zErrc;
1310  // STDLINE(ss.str(),ACRed);
1311 
1312  this->translateXY(x,y,true);
1313  ss.str(""); ss << __PRETTY_FUNCTION__ << "After translate->"
1314  << " x: " << *x << " y: " << *y << " z:" << *z
1315  << " xErr: " << *xErr << " yErr: " << *yErr << " zErr:" << *zErr;
1316  // STDLINE(ss.str(),ACRed);
1317 
1318  this->translateCorrection(x,y,true);
1319  ss.str(""); ss << __PRETTY_FUNCTION__ << "After tran corr->"
1320  << " x: " << *x << " y: " << *y << " z:" << *z
1321  << " xErr: " << *xErr << " yErr: " << *yErr << " zErr:" << *zErr;
1322  // STDLINE(ss.str(),ACRed);
1323 
1324  *z = 0;
1325  this->flipBackPositionLocal(x,y,xErr,yErr);
1326  ss.str(""); ss << __PRETTY_FUNCTION__ << "Final AfterFlip->"
1327  << " x: " << *x << " y: " << *y << " z:" << *z
1328  << " xErr: " << *xErr << " yErr: " << *yErr << " zErr:" << *zErr;
1329  STDLINE(ss.str(),ACGreen);
1330 
1331  this->fromLocalToGlobal(x,y,z,xErr,yErr,zErr);
1332  ss.str(""); ss << __PRETTY_FUNCTION__ << "After DetToGlob->"
1333  << " x: " << *x << " y: " << *y << " z:" << *z
1334  << " xErr: " << *xErr << " yErr: " << *yErr << " zErr:" << *zErr;
1335  STDLINE(ss.str(),ACPurple);
1336 
1337  this->fromGlobalToLocal(x,y,z,xErr,yErr,zErr);
1338  ss.str(""); ss << __PRETTY_FUNCTION__ << "After GlobToDet->"
1339  << " x: " << *x << " y: " << *y << " z:" << *z
1340  << " xErr: " << *xErr << " yErr: " << *yErr << " zErr:" << *zErr;
1341  STDLINE(ss.str(),ACGreen);
1342 
1343 }
1344 
1345 //=======================================================================================
1346 void Detector::dump(void)
1347 {
1348  char buffer[128];
1349  sprintf (buffer, "Alpha -> Total = %10.2f (%10.2f + %7.2f) deg", getXRotation() + getXRotationCorrection(), getXRotation(), getXRotationCorrection());STDLINE(buffer,ACCyan);
1350  sprintf (buffer, "Beta -> Total = %10.2f (%10.2f + %7.2f) deg", getYRotation() + getYRotationCorrection(), getYRotation(), getYRotationCorrection());STDLINE(buffer,ACCyan);
1351  sprintf (buffer, "Gamma -> Total = %10.2f (%10.2f + %7.2f) deg", getZRotation() + getZRotationCorrection(), getZRotation(), getZRotationCorrection());STDLINE(buffer,ACCyan);
1352  sprintf (buffer, "X -> Total = %10.2f (%10.2f + %7.2f) um", 10*(getXPositionTotal()), 10*getXPosition(), 10*getXPositionCorrection());STDLINE(buffer,ACCyan);
1353  sprintf (buffer, "Y -> Total = %10.2f (%10.2f + %7.2f) um", 10*(getYPositionTotal()), 10*getYPosition(), 10*getYPositionCorrection());STDLINE(buffer,ACCyan);
1354  sprintf (buffer, "Z -> Total = %10.2f (%10.2f + %7.2f) um", 10*(getZPositionTotal()), 10*getZPosition(), 10*getZPositionCorrection());STDLINE(buffer,ACCyan);
1355 }