otsdaq  v1_01_02
 All Classes Namespaces Functions
ROC.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/ROC.h"
11 
12 using namespace monicelli;
13 
14 ClassImp(ROC)
15 
16 //==========================================================================
17 ROC::ROC(unsigned int position, int chipID, unsigned int degrees) :
18  rocLengthX_(0)
19  ,rocLengthY_(0)
20  ,numberOfRows_(0)
21  ,numberOfCols_(0)
22  ,chipID_(chipID)
23  ,orientation_(0)
24  ,position_(position)
25  ,standardPixelPitch_(std::pair<double,double>(-1,-1))
26  ,calibrationFilePath_("")
27 {
28  this->setOrientation(degrees);
29 }
30 
31 //===============================================================================
32 double ROC::calibrationFitFunction(double *x, double *par)
33 {
34  return par[0]+par[1]*tanh(par[2]*x[0]+par[3]);
35 }
36 
37 //===============================================================================
38 double ROC::calibrationFitFunctionInv(double *x, double *par)
39 {
40  return (atanh((x[0]-par[0])/par[1]) - par[3])/par[2];
41 }
42 
43 //==========================================================================
44 void ROC::setOrientation(unsigned int degrees)
45 {
46  if (degrees==0 || degrees==180)
47  orientation_ = degrees;
48  else if(degrees==90 || degrees==270)
49  {
50  STDLINE("ERROR: Only 0 and 180 degrees rotations are supported for a ROC!", ACRed);
51  exit(EXIT_FAILURE);
52  }
53  else
54  {
55  STDLINE("ERROR: Only 0 and 180 degrees rotations are supported for a ROC!", ACRed);
56  exit(EXIT_FAILURE);
57  }
58 }
59 
60 //==========================================================================
61 bool ROC::goodRow(unsigned int row)
62 {
63  if ( row < numberOfRows_ )
64  return true;
65  else{
66  ss_.str("");
67  ss_ << "WARNING: no pixel found with col number " << row;
68  STDLINE(ss_.str(), ACRed);
69  return false;
70  }
71 }
72 
73 //==========================================================================
74 bool ROC::goodCol(unsigned int col)
75 {
76  if ( col < numberOfCols_ )
77  return true;
78  else
79  {
80  ss_.str("");
81  ss_ << "WARNING: no pixel found with col number " << col;
82  STDLINE(ss_.str(), ACRed);
83  return false;
84  }
85 }
86 
87 //==========================================================================
88 void ROC::setRowPitchVector(void)
89 {
90  if(numberOfRows_ == 0) return;
91  rowPitches_.resize(numberOfRows_, standardPixelPitch_.first);
92  rowLowEdge_.resize(numberOfRows_,0);
93  for(nonStandardPitchMapDef::iterator it=nonStandardRowPitch_.begin(); it != nonStandardRowPitch_.end(); it++)
94  {
95  if(it->first < rowPitches_.size())
96  {
97  rowPitches_[it->first] = it->second;
98  }
99  }
100  rowLowEdge_[0] = 0;
101  for(unsigned int r=1; r<rowPitches_.size(); r++)
102  {
103  rowLowEdge_[r] = rowLowEdge_[r-1] + rowPitches_[r-1];
104  }
105  rocLengthY_ = rowLowEdge_[rowLowEdge_.size()-1] + rowPitches_[rowPitches_.size()-1];
106 }
107 
108 //==========================================================================
109 void ROC::setColPitchVector(void)
110 {
111  if(numberOfCols_ == 0) return;
112  colPitches_.resize(numberOfCols_, standardPixelPitch_.second);
113  colLowEdge_.resize(numberOfCols_,0);
114  for(nonStandardPitchMapDef::iterator it=nonStandardColPitch_.begin(); it != nonStandardColPitch_.end(); it++)
115  {
116  if(it->first < colPitches_.size())
117  {
118  colPitches_[it->first] = it->second;
119  }
120  }
121  colLowEdge_[0] = 0;
122  for(unsigned int c=1; c<colPitches_.size(); c++)
123  {
124  colLowEdge_[c] = colLowEdge_[c-1] + colPitches_[c-1];
125  }
126  rocLengthX_ = colLowEdge_[colLowEdge_.size()-1] + colPitches_[colPitches_.size()-1];
127 }
128 
129 //==========================================================================
130 void ROC::setNumberOfRowsCols(unsigned int numberOfRows, unsigned int numberOfCols)
131 {
132  numberOfRows_=numberOfRows;
133  setRowPitchVector();
134  numberOfCols_=numberOfCols;
135  setColPitchVector();
136 }
137 
138 //==========================================================================
139 void ROC::setNumberOfRows(unsigned int numberOfRows)
140 {
141  numberOfRows_=numberOfRows;
142  setRowPitchVector();
143 }
144 
145 //==========================================================================
146 void ROC::setNumberOfCols(unsigned int numberOfCols)
147 {
148  numberOfCols_=numberOfCols;
149  setColPitchVector();
150 }
151 
152 //==========================================================================
153 void ROC::setStandardPixPitch(double row_cm, double col_cm)
154 {
155  standardPixelPitch_=std::make_pair(row_cm,col_cm);
156  setRowPitchVector();
157  setColPitchVector();
158 }
159 
160 //==========================================================================
161 void ROC::setOneRowPitch(unsigned int row, double row_cm)
162 {
163  nonStandardRowPitch_[row] = row_cm;
164  setRowPitchVector();
165 }
166 
167 //==========================================================================
168 void ROC::setOneColPitch(unsigned int col, double col_cm)
169 {
170  nonStandardColPitch_[col] = col_cm;
171  setColPitchVector();
172 }
173 
174 //==========================================================================
175 double ROC::getPixelCenterLocalX( unsigned int pixelCol )
176 {
177  if ( !goodCol(pixelCol) ) return -1;
178  return (this->getPixelLowEdgeLocalX(pixelCol) + this->getPixelPitchLocalX(pixelCol)/2.);
179 }
180 
181 //==========================================================================
182 double ROC::getPixelCenterLocalY(unsigned int pixelRow )
183 {
184  if ( !goodRow(pixelRow) ) return -1;
185  return (this->getPixelLowEdgeLocalY(pixelRow) + this->getPixelPitchLocalY(pixelRow)/2.);
186 }
187 
188 //==========================================================================
189 double ROC::getPixelLowEdgeLocalX(unsigned int pixelCol )
190 {
191  if ( !goodCol(pixelCol) ) return -1;
192 
193  if( orientation_ == 0)
194  return colLowEdge_[pixelCol];
195  else if(orientation_ == 180)
196  return rocLengthX_ - colLowEdge_[pixelCol] - colPitches_[pixelCol];
197  else
198  {
199  ss_.str("");
200  ss_ << "WARNING: orientation not supported " << orientation_;
201  STDLINE(ss_.str(), ACRed);
202  return -1;
203  }
204 }
205 //==========================================================================
206 double ROC::getPixelLowEdgeLocalY(unsigned int pixelRow )
207 {
208  if ( !goodRow(pixelRow) ) return -1;
209 
210  if( orientation_ == 0)
211  return rowLowEdge_[pixelRow];
212  else if(orientation_ == 180)
213  return rocLengthY_ - rowLowEdge_[pixelRow] - rowPitches_[pixelRow];
214  else
215  {
216  ss_.str("");
217  ss_ << "WARNING: orientation not supported " << orientation_;
218  STDLINE(ss_.str(), ACRed);
219  return -1;
220  }
221 }
222 
223 //==========================================================================
224 double ROC::getPixelHiEdgeLocalX(unsigned int pixelCol )
225 {
226  if ( !goodCol(pixelCol) ) return -1;
227  return (this->getPixelLowEdgeLocalX(pixelCol) + this->getPixelPitchLocalX(pixelCol));
228 }
229 
230 //==========================================================================
231 double ROC::getPixelHiEdgeLocalY(unsigned int pixelRow )
232 {
233  if ( !goodRow(pixelRow) ) return -1;
234  return (this->getPixelLowEdgeLocalY(pixelRow) + this->getPixelPitchLocalY(pixelRow));
235 }
236 
237 //==========================================================================
238 double ROC::getPixelPitchLocalX(unsigned int pixelCol )
239 {
240  if ( !goodCol(pixelCol) ) return -1;
241  return colPitches_[pixelCol];
242 }
243 
244 //==========================================================================
245 double ROC::getPixelPitchLocalY(unsigned int pixelRow )
246 {
247  if ( !goodRow(pixelRow) ) return -1;
248  return rowPitches_[pixelRow];
249 }
250 
251 //==========================================================================
252 double ROC::getLengthLocalX(void )
253 {
254  return rocLengthX_;
255 }
256 
257 //==========================================================================
258 double ROC::getLengthLocalY(void )
259 {
260  return rocLengthY_;
261 }
262 
263 //=============================================================================
264 bool ROC::calibratePixel(int row, int col, int adc, int& charge)
265 {
266  double newAdc[1];
267  double maxVCal[1] = {255*421};
268  newAdc[0] = adc;
269  double *par = this->getCalibrationFunction(row, col);
270  if(par != 0)
271  {
272  if(newAdc[0] > ROC::calibrationFitFunction(maxVCal, par))
273  newAdc[0] = (int)ROC::calibrationFitFunction(maxVCal, par);
274  charge = (int)ROC::calibrationFitFunctionInv(newAdc, par);//, true);
275  return true;
276  }
277  else
278  {
279  //newAdc[0] = abs(adc-400);
280  charge = -999999;
281  return false;
282  }
283 }
284 
285 //=============================================================================
286 bool ROC::isPixelCalibrated(int row, int col)
287 {
288  if( pixelCalibrationFunctionTmp_.count(row) && pixelCalibrationFunctionTmp_[row].count(col))
289  return true;
290  else
291  return false;
292 }
293 
294 //============================================================================
295 void ROC::setCalibrationFunction(int row, int col, double *par, double *cov)//, int nPars )
296 {
297  if( pixelCalibrationFunctionTmp_.count(row) && pixelCalibrationFunctionTmp_[row].count(col))
298  pixelCalibrationFunctionTmp_.clear();
299 
300  if(par != NULL)
301  {
302  for(int i=0; i < 4; i++)
303  {
304  pixelCalibrationFunctionTmp_[row][col].push_back(par[i]);
305  *cov = 0;
306  }
307  }
308 }
309 
310 //============================================================================
311 double* ROC::getCalibrationFunction(int row, int col)
312 {
313  if( pixelCalibrationFunctionTmp_.count(row) && pixelCalibrationFunctionTmp_[row].count(col))
314  for(unsigned int i=0; i < pixelCalibrationFunctionTmp_[row][col].size(); i++)
315  par_[i] = pixelCalibrationFunctionTmp_[row][col][i] ;
316  else
317  return 0;
318 
319  return par_;
320 }
321 
322 //============================================================================
323 double ROC::getCalibrationError(int /*row*/, int /*col*/, int /*adc*/)
324 {
325  /*
326  double err;
327  if( pixelCalibrationFunctionTmp_.count(row) && pixelCalibrationFunctionTmp_[row].count(col) )
328  {
329  double arg = ( adc - pixelCalibrationFunctionTmp_[row][col][0] )/pixelCalibrationFunctionTmp_[row][col][1];
330  double der0 = -1./( pixelCalibrationFunctionTmp_[row][col][2]*pow(cosh(arg),2) );
331  double der1 = -arg/( pow(cosh(arg),2)*pixelCalibrationFunctionTmp_[row][col][2]*pixelCalibrationFunctionTmp_[row][col][1] );
332  double der2 = ( pixelCalibrationFunctionTmp_[row][col][3] - tanh(arg) ) / ( pixelCalibrationFunctionTmp_[row][col][2]*pixelCalibrationFunctionTmp_[row][col][2] );
333  double der3 = -1./pixelCalibrationFunctionTmp_[row][col][2];
334 
335  ss_.str("");
336  ss_ << "par0: " << pixelCalibrationFunctionTmp_[row][col][0]
337  << " par1: " << pixelCalibrationFunctionTmp_[row][col][1]
338  << " par2: " << pixelCalibrationFunctionTmp_[row][col][2]
339  << " par3: " << pixelCalibrationFunctionTmp_[row][col][3];
340  //STDLINE(ss_.str(), ACCyan);
341  ss_.str("");
342  ss_ << "der0: " << der0 << " der1: " << der1 << " der2: " << der2 << " der3: " << der3;
343  //STDLINE(ss_.str(), ACPurple);
344  ss_.str("");
345  ss_ << "eer0: " << pixelCalibrationCovMat_[row][col][0][0] << " eer1: " << pixelCalibrationCovMat_[row][col][1][1]
346  << " eer2: " << pixelCalibrationCovMat_[row][col][2][2] << " eer3: " << pixelCalibrationCovMat_[row][col][3][3];
347  //STDLINE(ss_.str(), ACYellow);
348 
349  err = sqrt(der0*der0*pixelCalibrationCovMat_[row][col][0][0] +
350  der1*der1*pixelCalibrationCovMat_[row][col][1][1] +
351  der2*der2*pixelCalibrationCovMat_[row][col][2][2] +
352  der3*der3*pixelCalibrationCovMat_[row][col][3][3] +
353  2*der0*der1*pixelCalibrationCovMat_[row][col][0][1] +
354  2*der0*der2*pixelCalibrationCovMat_[row][col][0][2] +
355  2*der0*der3*pixelCalibrationCovMat_[row][col][0][3] +
356  2*der1*der2*pixelCalibrationCovMat_[row][col][1][2] +
357  2*der1*der3*pixelCalibrationCovMat_[row][col][1][3] +
358  2*der2*der3*pixelCalibrationCovMat_[row][col][2][3] );
359 
360  ss_.str("");
361  ss_ << "err " << err;
362  //STDLINE(ss_.str(), ACRed);
363 
364  }
365  else return -1;
366 
367  return err;
368  */
369  return -1;
370 }