ATLAS Offline Software
TileOFC.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #include <cstdio>
6 #include <fstream>
7 #include <iostream>
8 #include <sstream>
9 #include <cmath>
10 #include <vector>
11 #include <string>
12 #include <stdint.h>
13 
14 #include "TileByteStream/TileOFC.h"
15 
16 using namespace std;
17 
18 void Double2Int_calib( double calib, unsigned int &scale, unsigned int &offset, bool verbose )
19 {
20  // Number of bits of the integer word (signed -1 )
21  int NumberBits = 16;
22  NumberBits = NumberBits - 1;
23 
24  // Get Scale
25  scale = 0;
26  double max = fabs(calib);
27  if (max!=0) scale = (unsigned int) truncf(log((pow(2.,NumberBits)-1.)/max)*(1./log(2.)));
28 
29  // Convert to integer
30  offset = (unsigned int) roundf(calib*pow(2.,(int)scale));
31 
32  if (verbose) cout << "Calib = " << hex << calib
33  << " ==> ( scale: " << scale
34  << " , offset: " << offset
35  << ")" << dec << endl;
36 
37 }
38 
39 
40 
41 void Double2Int_ofc( int w_off_size, const vector<double>& w_off, vector<int> &w_dsp, int &w_sum_dsp, int &scale, bool verbose )
42 {
43  // Number of bits of the integer word (signed -1 )
44  int NumberBits = 16;
45  NumberBits = NumberBits - 1;
46 
47  // Get Absolute Maximum
48  double max = -10000.;
49  double sum = 0.;
50  for (int i=0; i<w_off_size; i++)
51  {
52  sum += w_off[i];
53  if ( fabs(w_off[i]) > max ) max = fabs(w_off[i]);
54  }
55  if (fabs(sum) > max) max = fabs(sum);
56 
57  // Get Scale at Maximum
58  scale = 0;
59  if (max!=0) scale = (int) truncf(log((pow(2.,NumberBits)-1.)/max)*(1./log(2.)));
60 
61  // Convert to Integer the weights and the sum
62  for (int i=0; i<w_off_size; i++) w_dsp.push_back( (int) roundf(w_off[i]*pow(2.,scale)) );
63  w_sum_dsp = (int) roundf(sum*pow(2.,scale));
64 
65 
66  if (verbose) {
67  printf("\nAbsolute Max value = %15.8f -> Scale = %3d\n",max,scale);
68 
69  for (int i=0; i<w_off_size; i++)
70  {
71  if ( i == 0){
72  printf("\n Offline Off*Scale Dsp/scale Dsp Scale \n");
73  printf("----------------------------------------------------------------------------------\n");
74  printf(" %17.10f %17.10f %17.10f 0x%04X %3d \n",
75  w_off[i],w_off[i]*pow(2.,scale), w_dsp[i]/pow(2.,scale),(unsigned int) w_dsp[i] ,scale);
76  } else {
77  printf(" %17.10f %17.10f %17.10f 0x%04X \n",
78  w_off[i],w_off[i]*pow(2.,scale),w_dsp[i]/pow(2.,scale),(unsigned int)w_dsp[i]);
79  }
80  }
81  printf(" %17.10f %17.10f %17.10f 0x%04X <- SUM\n",
82  sum,sum*pow(2.,scale),w_sum_dsp/pow(2.,scale),(unsigned int)w_sum_dsp);
83  }
84 
85  return ;
86 }
87 
88 bool ConvertOFC(const vector<vector<vector<vector<double> > > >& w_off,
89  const vector<vector<double> >& calibration, int calibtype, int runtype,
90  vector<unsigned int> &OFC, bool verbose) {
91  bool flag;
92 
93  // Select number of phases and step inside DSP
94  //int format = 5;
95  //format = format;
96  float dsp_step = 1. ;
97  float dsp_min_phase = -100.;
98  float dsp_max_phase = -dsp_min_phase;
99  int dsp_phases = (int) roundf( (dsp_max_phase - dsp_min_phase)*(1./dsp_step) +1. );
100 
101  if (1)
102  {
103  cout<<"----- DSP -----"<<endl;
104  cout<<"Step: "<<dsp_step<<" ns"<<endl;
105  cout<<"Min Phase: "<<dsp_min_phase<<" ns"<<endl;
106  cout<<"Max Phase: "<<dsp_max_phase<<" ns"<<endl;
107  cout<<"Nb phases: "<<dsp_phases<<endl;
108  }
109 
110  // Number of phases and step of the offline Opt. filte constants
111  const float off_step = 0.1;
112  float off_min_phase = -100.;
113  float off_max_phase = -off_min_phase;
114  int off_phases = (int) roundf( (off_max_phase - off_min_phase)*(1./off_step) + 1. );
115  if (verbose)
116  {
117  cout<<"----- OFFLINE -----"<<endl;
118  cout<<"Step: "<<off_step<<" ns"<<endl;
119  cout<<"Min Phase: "<<off_min_phase<<" ns"<<endl;
120  cout<<"Max Phase: "<<off_max_phase<<" ns"<<endl;
121  cout<<"Nb phases: "<<off_phases<<endl;
122  }
123 
124  int ngains = (int) w_off.size();
125  int nphases = (int) w_off[0].size();
126  int nparams = (int) w_off[0][0].size();
127  int nsamples = (int) w_off[0][0][0].size();
128 
129  if ( dsp_step < off_step ){
130  cout<<"ERROR: DSP phase step smaller than Offline phase step"<<endl;
131  return false;
132  }
133  if (ngains != 2){
134  cout<<"ERROR: Incorrect number of gains"<<endl;
135  return false;
136  }
137  if (off_phases != nphases){
138  cout<<"ERROR: Incorrect number of phases"<<endl;
139  return false;
140  }
141  if (dsp_min_phase < off_min_phase){
142  cout<<"ERROR: Incorrect minimum phase"<<endl;
143  return false;
144  }
145  if (dsp_max_phase > off_max_phase){
146  cout<<"ERROR: Incorrecto maximum phase"<<endl;
147  return false;
148  }
149 
150  int algorithm;
151  if (nparams == 3){
152  algorithm = 0; // OF1
153  }else if (nparams == 4){
154  algorithm = 1; // OF2
155  }else{
156  cout<<"ERROR: Incorrect algorithm type"<<endl;
157  return false;
158  }
159 
160  // Get Calibration
161  int ig;
162  int calib_gain = (int) calibration.size();
163  int calib_size = (int) calibration[0].size();
164  if (calib_gain!=ngains){
165  cout<<"ERROR: Number of gain in calibration vector incorrect"<<endl;
166  return false;
167  }
168  vector<double> calibration_lo;
169  vector<double> calibration_hi;
170  for (int i=0; i<calib_size; i++)
171  {
172  ig = 0; // low gain
173  calibration_lo.push_back( calibration[ig][i] );
174  ig = 1; // high gain
175  calibration_hi.push_back( calibration[ig][i] );
176  }
177  if (calibtype != 0) calibration_hi[1] = calibration_hi[1]*64.;
178 
179  if (verbose)
180  {
181  cout<<"--- calibration ---"<<endl;
182  cout<<"[low gain] -> offset: "<<calibration_lo[0]<<" slope: "<<calibration_lo[1]<<endl;
183  cout<<"[high gain] -> offset: "<<calibration_hi[0]<<" slope: "<<calibration_hi[1]<<endl;
184  cout<<"If calibtype != ADC then slope is multiplied by a factor 64"<<endl;
185  }
186 
187  // Get Optimal filtering constants
188  vector<double> a_lo(nsamples,0);
189  vector<double> b_lo(nsamples,0);
190  vector<double> c_lo(nsamples,0);
191  vector<double> g_lo(nsamples,0);
192  vector<double> h_lo(nsamples,0);
193  vector<double> a_hi(nsamples,0);
194  vector<double> b_hi(nsamples,0);
195  vector<double> c_hi(nsamples,0);
196  vector<double> g_hi(nsamples,0);
197  vector<double> h_hi(nsamples,0);
198 
199  // Select phases for the DSP
200  int index;
201  float current_phase;
202 
203  // bln
204  const float inv_off_step = 1. / off_step;
205  for (int i=0; i<dsp_phases; i++){
206 
207  current_phase = dsp_min_phase + i*dsp_step;
208  index = (int) roundf((current_phase - off_min_phase)*inv_off_step);
209  if (verbose) cout<<"["<<index<<"]: Phase "<<current_phase<<" ns"<<endl;
210  for (int is=0; is<nsamples; is++)
211  {
212  ig = 0; // low gain
213  a_lo[is] = w_off[ig][index][0][is];
214  b_lo[is] = w_off[ig][index][1][is];
215  g_lo[is] = w_off[ig][index][nparams-1][is];
216  // missing c_lo, h_lo
217 
218  ig = 1; // high gain
219  a_hi[is] = w_off[ig][index][0][is];
220  b_hi[is] = w_off[ig][index][1][is];
221  g_hi[is] = w_off[ig][index][nparams-1][is];
222  // missing c_hi, h_hi
223 
224  }
225 
226 
227  // convert Format5
228  if (verbose) cout<<"---- LOW GAIN ----"<<endl;
229  flag = Format5( a_lo, b_lo, c_lo, g_lo, h_lo, OFC, verbose);
230  if (!flag) return flag;
231  if (verbose) cout<<"---- HIGH GAIN ----"<<endl;
232  flag = Format5( a_hi, b_hi, c_hi, g_hi, h_hi, OFC, verbose);
233  if (!flag) return flag;
234 
235  }
236 
237  cout << "ERROR!!! Format 5 in this call is incomplete (OFC::ConvertOFC)" << endl;
238 
239  flag = FormatInfo( nsamples, calibtype, algorithm, runtype, OFC, verbose);
240  if (!flag) return flag;
241 
242  a_lo.clear();
243  b_lo.clear();
244  g_lo.clear();
245  a_hi.clear();
246  b_hi.clear();
247  g_hi.clear();
248  calibration_lo.clear();
249  calibration_hi.clear();
250 
251  return true;
252 }
253 
270 bool Format6( const std::vector<double>& a,
271  const std::vector<double>& b,
272  const std::vector<double>& c,
273  const std::vector<double>& g,
274  const std::vector<double>& h,
275  unsigned int channel_index,
276  int phase,
277  double calibration,
278  std::vector<unsigned int> &OFC,
279  bool verbose)
280 {
281  vector<int> adsp;
282  vector<int> bdsp;
283  vector<int> cdsp;
284  vector<int> gdsp;
285  vector<int> hdsp;
286 
287  int dump;
288 
289  int ascale;
290  int bscale;
291  int cscale;
292  int gscale;
293  int hscale;
294 
295  Double2Int_ofc( (int) a.size(), a, adsp, dump, ascale, verbose );
296  Double2Int_ofc( (int) b.size(), b, bdsp, dump, bscale, verbose );
297  Double2Int_ofc( (int) c.size(), c, cdsp, dump, cscale, verbose );
298  Double2Int_ofc( (int) g.size(), g, gdsp, dump, gscale, verbose );
299  Double2Int_ofc( (int) h.size(), h, hdsp, dump, hscale, verbose );
300 
301  unsigned int slope_scale;
302  unsigned int slope;
303 
304  Double2Int_calib( calibration, slope_scale, slope, verbose );
305 
306  // Start formating
307  int n_2 = (int) trunc(a.size()/2.);
308 
309  // OFC for energy
310  OFC.push_back( ( (adsp[0] << 16) & 0xFFFF0000) | ( (ascale-1) & 0xFFFF) );
311  for (int i=1; i<=n_2; i++)
312  OFC.push_back( ( (adsp[2*i] << 16) & 0xFFFF0000) | ( adsp[2*i-1] & 0xFFFF) );
313 
314  // Calibration
315  OFC.push_back( ( (slope << 16) & 0xFFFF0000) | ( (ascale + slope_scale ) & 0xFFFF ) );
316 
317  // OFC for phase
318  OFC.push_back( ( (bdsp[0] << 16) & 0xFFFF0000) | ( (bscale-4) & 0xFFFF) );
319  for (int i=1; i<=n_2; i++)
320  OFC.push_back( ( (bdsp[2*i] << 16) & 0xFFFF0000) | ( bdsp[2*i-1] & 0xFFFF) );
321 
322  // OFC for pedestal
323  OFC.push_back( ( (cdsp[0] << 16) & 0xFFFF0000) | ( (cscale) & 0xFFFF) );
324  for (int i=1; i<=n_2; i++)
325  OFC.push_back( ( (cdsp[2*i] << 16) & 0xFFFF0000) | ( cdsp[2*i-1] & 0xFFFF) );
326 
327  // OFC for quality factor
328  OFC.push_back( ( (hscale << 16) & 0xFFFF0000) | ( (gscale+1) & 0xFFFF) );
329  for (size_t i=0; i<g.size(); i++)
330  OFC.push_back( ( (hdsp[i] << 16) & 0xFFFF0000) | ( gdsp[i] & 0xFFFF) );
331 
332  // codificar index channel y fase
333  OFC.push_back( ( (channel_index << 16) & 0xFFFF0000) | (phase & 0xFFFF) ) ;
334 
335  if (verbose) cout<<"size of OFC: "<<OFC.size()<<endl;
336 
337  adsp.clear();
338  bdsp.clear();
339  cdsp.clear();
340  gdsp.clear();
341  hdsp.clear();
342 
343  return true;
344 }
345 
359 bool Format5( const vector<double>& a,
360  const vector<double>& b ,
361  const vector<double>& c,
362  const vector<double>& g,
363  const vector<double>& h,
364  vector<unsigned int> &OFC ,
365  bool verbose)
366 {
367  vector<int> adsp;
368  vector<int> bdsp;
369  vector<int> cdsp;
370  vector<int> gdsp;
371  vector<int> hdsp;
372 
373  int asum;
374  int bsum;
375  int dump;
376 
377  int ascale;
378  int bscale;
379  int cscale;
380  int gscale;
381  int hscale;
382 
383  Double2Int_ofc( (int) a.size(), a, adsp, asum, ascale, verbose );
384  Double2Int_ofc( (int) b.size(), b, bdsp, bsum, bscale, verbose );
385  Double2Int_ofc( (int) c.size(), c, cdsp, dump, cscale, verbose );
386  Double2Int_ofc( (int) g.size(), g, gdsp, dump, gscale, verbose );
387  Double2Int_ofc( (int) h.size(), h, hdsp, dump, hscale, verbose );
388 
389  // Start formating
390  int n_2 = (int) trunc(a.size()/2.);
391 
392  for (int i=0; i<n_2; i++)
393  OFC.push_back( ( (adsp[2*i] << 16 )& 0xFFFF0000) | ( adsp[2*i+1] & 0x0000FFFF) ) ;
394  OFC.push_back( ( (adsp[2*n_2] << 16 )& 0xFFFF0000) | ( (-asum) & 0x0000FFFF) );
395 
396  for (int i=0; i<n_2; i++)
397  OFC.push_back( ( (bdsp[2*i] << 16 )& 0xFFFF0000) | ( bdsp[2*i+1] & 0x0000FFFF) ) ;
398  OFC.push_back( ( (bdsp[2*n_2] << 16 )& 0xFFFF0000) | ( (-bsum) & 0x0000FFFF) );
399 
400  OFC.push_back( ( (bscale << 16 )& 0xFFFF0000) | ( ascale & 0x0000FFFF) );
401 
402  OFC.push_back( ( (cdsp[0] << 16 )& 0xFFFF0000) | ( cscale & 0x0000FFFF) );
403  for (int i=1; i<=n_2; i++)
404  OFC.push_back( ( (cdsp[2*i] << 16 )& 0xFFFF0000) | ( cdsp[2*i-1] & 0x0000FFFF) ) ;
405 
406  OFC.push_back( ( (hscale << 16 )& 0xFFFF0000) | ( gscale & 0x0000FFFF) );
407  for (size_t i=0; i<g.size();i++)
408  OFC.push_back( ( (hdsp[i] << 16 )& 0xFFFF0000) | ( gdsp[i] & 0x0000FFFF) );
409 
410  adsp.clear();
411  bdsp.clear();
412  cdsp.clear();
413  gdsp.clear();
414  hdsp.clear();
415 
416  if (verbose) cout<<"size of OFC: "<<OFC.size()<<endl;
417  return true;
418 
419 }
420 
421 bool Format5calib( double calib, vector<unsigned int> &OFC , bool verbose){
422 
423  unsigned int offset_scale = 0;
424  unsigned int offset = 0;
425 
426  unsigned int slope_scale;
427  unsigned int slope;
428 
429  Double2Int_calib( calib, slope_scale, slope, verbose );
430 
431  OFC.push_back( ( (offset_scale << 16) & 0xFFFF0000) | ( slope_scale & 0x0000FFFF) );
432  OFC.push_back( ( (offset << 16) & 0xFFFF0000) | ( slope & 0x0000FFFF) );
433 
434  return true;
435 }
436 
437 bool FormatInfo( int nsamples, int calibrationtype, int algorithm, int runtype, vector<unsigned int> &OFC, bool verbose)
438 {
439  int in = 1;
440  if (nsamples == 9){
441  in = 1;
442  }else if(nsamples == 7){
443  in = 0;
444  }else {
445  cout<<"ERROR: [FormatInfo] Incorrect number of samples"<<endl;
446  return false;
447  }
448 
449  if (calibrationtype > 3 || calibrationtype < 0 ) {
450  cout<<"ERROR: [FormatInfo] Unknown calibration type"<<endl;
451  return false;
452  }
453 
454  // poner lo mismo con runtype
455 
456  if (algorithm != 0 && algorithm!=1){
457  cout<<"ERROR: [FormatInfo] Unknown algorithm type"<<endl;
458  return false;
459  }
460 
461  OFC.push_back( ((calibrationtype & 0x3)<<6) | ((runtype & 0x3)<<4) | ((in & 0x1)<<3) | ((algorithm & 0x1) << 2) );
462 
463  if (verbose) printf("OFC info: 0x%08x\n", ((calibrationtype & 0x3)<<6) | ((runtype & 0x3)<<4) | ((in & 0x1)<<3) | ((algorithm & 0x1) << 2) );
464 
465  return true;
466 }
467 
468 bool ReadOFfile(vector<vector<vector<vector<double> > > > &w_off, char * OFCfile, bool verbose)
469 {
470  // OPEN FILE
471  ifstream fin;
472  fin.open(OFCfile, ios::in);
473  if (!fin.is_open())
474  {
475  cout<<"Error opening file "<<(OFCfile)<<endl;
476  return false;
477  }
478 
479  // input file settings
480  float f_samples = 9; // 9 samples
481  float f_phi = -100; // ns (-100,100) ns
482  float f_step = 0.1;
483  int nphases = (int) roundf(-f_phi*2./f_step + 1.);
484  int ngains = 2;
485  int nsamples = (int) roundf(f_samples);
486 
487  // get number of parameters
488  string s;
489  double tmp1;
490  int nparams=0;
491  getline(fin,s);
492  istringstream ss(s);
493  while (ss >> tmp1) {
494  nparams++;
495  }
496  cout<<"Number of parameters: "<<nparams<<endl;
497 
498  // Resize w_off vector
499  w_off.resize(ngains);
500  for (int ig=0; ig<ngains; ig++){
501  w_off[ig].resize(nphases);
502  for (int it=0; it<nphases; it++){
503  w_off[ig][it].resize(nparams);
504  for (int ip=0; ip<nparams; ip++){
505  w_off[ig][it][ip].resize(nsamples,0);
506  }
507  }
508  }
509  fin.close();
510 
511  // read file
512  fin.open(OFCfile, ios::in);
513  for (int it=0; it<nphases; it++)
514  {
515  for (int is=0; is<nsamples; is++)
516  {
517  for (int ig=0; ig<ngains; ig++)
518  {
519  int ip = 0;
520  getline(fin,s);
521  istringstream ss(s);
522  while (ss >> tmp1) {
523  w_off[ig][it][ip][is] = tmp1;
524  ip++;
525  }
526  }
527  }
528  }
529 
530  verbose = true;
531  if (verbose)
532  {
533  cout<<"Optimal Filtering file: "<<(OFCfile)<<endl;
534  cout<<"Nb phases: "<<w_off[0].size()<<endl;
535  cout<<"Nb samples: "<<w_off[0][0][0].size()<<endl;
536  cout<<"Nb gains: "<<w_off.size()<<endl;
537  if ((int) w_off[0][0].size() == 3 ) cout<<"Algorithm: OF1"<<endl;
538  else if ((int) w_off[0][0].size() == 4 ) cout<<"Algorithm: OF2"<<endl;
539  }
540 
541  // CLOSE FILE
542  fin.close();
543 
544  return true;
545 }
546 
547 
548 
549 /*
550 bool ConvertOFC_iter( TileCalibDrawerOfc ofcDrawer,
551  std::vector<unsigned int> &OFC,
552  vector<vector<float> > calib,
553  int calibtype,
554  int runtype,
555  int algorithm,
556  bool verbose)
557 {
558 
559  bool flag;
560 
561  const uint32_t nphases = labs(ofcDrawer.getNPhases());
562  if (nphases != 2001) {
563  cout<<"ERROR: [ConvertOFC_iter] Incorrect number of phases"<<endl;
564  return false;
565  }
566 
567  const uint32_t nsamples = ofcDrawer.getNSamples();
568 
569  float dsp_step;
570  float dsp_min_phase;
571  float dsp_max_phase;
572  int dsp_phases;
573 
574  // Select number of phases and step inside DSP
575 
576  switch(nsamples){
577  case 7:
578  dsp_step = 1.;
579  dsp_min_phase = -75.;
580  dsp_max_phase = +75.;
581  dsp_phases = 151 ;
582  break;
583  case 9:
584  dsp_step = 1.;
585  dsp_min_phase = -100.;
586  dsp_max_phase = +100.;
587  dsp_phases = 201 ;
588  break;
589  default:
590  cout<<"ERROR: [ConverOFC_iter] Incorrect number of samples"<<endl;
591  return false;
592  }
593 
594  if (0)
595  {
596  cout << "----- DSP -----" << endl
597  << "Step: " << dsp_step << " ns" << endl
598  << "Min Phase: " << dsp_min_phase << " ns" << endl
599  << "Max Phase: " << dsp_max_phase << " ns" << endl
600  << "Nb phases: " << dsp_phases << endl;
601  }
602 
603  const uint16_t ngains = ofcDrawer.getNGains();
604  if (ngains !=2) {
605  cout<<"ERROR: [ConvertOFC_iter] Incorrect number of phases"<<endl;
606  return false;
607  }
608 
609  // Get Optimal Filtering constants
610  // A, B, C, G, H (G')
611  vector<double> a_lo(nsamples,0);
612  vector<double> b_lo(nsamples,0);
613  vector<double> c_lo(nsamples,0);
614  vector<double> g_lo(nsamples,0);
615  vector<double> h_lo(nsamples,0);
616  vector<double> a_hi(nsamples,0);
617  vector<double> b_hi(nsamples,0);
618  vector<double> c_hi(nsamples,0);
619  vector<double> g_hi(nsamples,0);
620  vector<double> h_hi(nsamples,0);
621 
622  int current_phase;
623  unsigned int channel = 0;
624  unsigned int adc;
625  int lo = TileDspGain::Low;
626  int hi = TileDspGain::High;
627 
628  for (int i=0; i<dsp_phases; i++)
629  {
630  current_phase = (int) roundf( (dsp_min_phase + i*dsp_step)*10.); // in units of ns*10.
631  if (verbose) cout<<"Phase "<<current_phase/10.<<" ns"<<endl;
632 
633  for (unsigned int is=0; is<nsamples; is++)
634  {
635  adc = 0; // low gain
636  a_lo[is] = ofcDrawer.getOfc(TileCalibDrawerOfc::FieldA, channel, adc, current_phase, is);
637  b_lo[is] = ofcDrawer.getOfc(TileCalibDrawerOfc::FieldB, channel, adc, current_phase, is);
638  c_lo[is] = ofcDrawer.getOfc(TileCalibDrawerOfc::FieldC, channel, adc, current_phase, is);
639  g_lo[is] = ofcDrawer.getOfc(TileCalibDrawerOfc::FieldG, channel, adc, current_phase, is);
640  h_lo[is] = ofcDrawer.getOfc(TileCalibDrawerOfc::FieldDG, channel, adc, current_phase, is);
641 
642  adc = 1; // high gain
643  a_hi[is] = ofcDrawer.getOfc(TileCalibDrawerOfc::FieldA, channel, adc, current_phase, is);
644  b_hi[is] = ofcDrawer.getOfc(TileCalibDrawerOfc::FieldB, channel, adc, current_phase, is);
645  c_hi[is] = ofcDrawer.getOfc(TileCalibDrawerOfc::FieldC, channel, adc, current_phase, is);
646  g_hi[is] = ofcDrawer.getOfc(TileCalibDrawerOfc::FieldG, channel, adc, current_phase, is);
647  h_hi[is] = ofcDrawer.getOfc(TileCalibDrawerOfc::FieldDG, channel, adc, current_phase, is);
648  }
649 
650  // convert Format5
651  if (verbose) cout<<"---- LOW GAIN ----"<<endl;
652  flag = Format5( a_lo, b_lo, c_lo, g_lo, h_lo, OFC, verbose);
653  if (!flag) return flag;
654  if (verbose) cout<<"---- HIGH GAIN ----"<<endl;
655  flag = Format5( a_hi, b_hi, c_hi, g_hi, h_hi, OFC, verbose);
656  if (!flag) return flag;
657 
658  }
659 
660  double calibration_hi;
661  double calibration_lo;
662 
663  for(size_t ich=0;ich<calib.size();ich++){
664 
665  calibration_lo=calib[ich][lo];
666  calibration_hi=calib[ich][hi];
667 
668  // calibration_hi should be scaled for DSP. Ask Alberto
669  if(calibtype!=0)
670  calibration_hi = calibration_hi*64.;
671 
672  Format5calib(calibration_lo, OFC, verbose);
673  Format5calib(calibration_hi, OFC, verbose);
674  }
675 
676  flag = FormatInfo( nsamples, calibtype, algorithm, runtype, OFC, verbose);
677  if (!flag) return flag;
678 
679  a_lo.clear();
680  b_lo.clear();
681  c_lo.clear();
682  g_lo.clear();
683  h_lo.clear();
684  a_hi.clear();
685  b_hi.clear();
686  c_hi.clear();
687  g_hi.clear();
688  h_hi.clear();
689 
690  return true;
691 }
692 
693 bool ConvertOFC_HR( TileCalibDrawerOfc ofcDrawer,
694  TileCalibDrawerFlt phaseDrawer,
695  std::vector<unsigned int> &OFC,
696  std::vector< std::vector<float> > calib,
697  int calibtype,
698  int runtype,
699  int algorithm,
700  bool verbose)
701 {
702  // Converter for High Rate (no iterations)
703  // one phase per adc
704  // The initial phases (phaseDrawer) are an input of the function
705  // Update to read selected phase from database
706 
707  bool flag;
708 
709  // This should be temporary !
710  const uint32_t nphases = labs(ofcDrawer.getNPhases());
711  if (nphases != 2001) {
712  cout<<"ERROR: [ConvertOFC_HR] Incorrect number of phases"<<endl;
713  return false;
714  } else {
715  cout<<"Reading OFCs for 2001 phases from the database"<<endl;
716  }
717 
718  // in principle we can use 7 or 9 samples!!
719  const uint32_t nsamples = ofcDrawer.getNSamples();
720  if (nsamples != 7 && nsamples != 9) {
721  cout<<"ERROR: [ConverOFC_HR] Incorrect number of samples"<<endl;
722  return false;
723  }
724 
725  const uint16_t ngains = ofcDrawer.getNGains();
726  if (ngains !=2) {
727  cout<<"ERROR: [ConvertOFC_HR] Incorrect number of phases"<<endl;
728  return false;
729  }
730 
731  // Calibration for each channel (only slope)
732  const uint16_t nchannel = calib.size();
733  if (nchannel != 48){
734  cout<<"ERROR: [ConvertOFC_HR] Incorrect number of channels"<<endl;
735  return false;
736  }
737 
738  // Get Optimal Filtering constants
739  // A, B, C, G, H (G')
740  vector<double> a_lo(nsamples,0);
741  vector<double> b_lo(nsamples,0);
742  vector<double> c_lo(nsamples,0);
743  vector<double> g_lo(nsamples,0);
744  vector<double> h_lo(nsamples,0);
745  vector<double> a_hi(nsamples,0);
746  vector<double> b_hi(nsamples,0);
747  vector<double> c_hi(nsamples,0);
748  vector<double> g_hi(nsamples,0);
749  vector<double> h_hi(nsamples,0);
750 
751  double calibration_lo;
752  double calibration_hi;
753  int phase_lo;
754  int phase_hi;
755  int lo = TileDspGain::Low;
756  int hi = TileDspGain::High;
757 
758  for (unsigned int ich=0; ich<nchannel; ich++){
759 
760  // Get phases (in units of ns/10)
761 
762  if(true){
763 
764  //const double phase2int = 10.;
765  //It has been identified by the Reco Validation group that the best phase in cool
766  //has inverted sign. For the sake of DB operation, we switch the sign here.
767  const double phase2int = -10.;
768 
769  phase_lo = static_cast<int>( round(phase2int * phaseDrawer.getData(ich, lo, 0)) );
770  if(verbose) cout << "Idx phase_lo(" << ich << ") =" << phase_lo << endl;
771 
772  phase_hi = static_cast<int>( round(phase2int * phaseDrawer.getData(ich, hi, 0)) );
773  if(verbose) cout << "Idx phase_hi(" << ich << ") =" << phase_hi << endl;
774 
775  }else{
776 
777  phase_lo = 0;
778  phase_hi = 0;
779 
780  }
781 
782  // Get calibration constants for current
783 
784  calibration_lo = (double) calib[ich][lo];
785  calibration_hi = (double) calib[ich][hi];
786 
787  // calibration_hi should be scaled for DSP. Ask Alberto
788 
789  if(calibtype!=0)
790  calibration_hi = calibration_hi*64.;
791 
792  for (unsigned int is=0; is<nsamples; is++){
793 
794  a_lo[is] = ofcDrawer.getOfc(TileCalibDrawerOfc::FieldA, ich, lo, phase_lo, is);
795  b_lo[is] = ofcDrawer.getOfc(TileCalibDrawerOfc::FieldB, ich, lo, phase_lo, is);
796  c_lo[is] = ofcDrawer.getOfc(TileCalibDrawerOfc::FieldC, ich, lo, phase_lo, is);
797  g_lo[is] = ofcDrawer.getOfc(TileCalibDrawerOfc::FieldG, ich, lo, phase_lo, is);
798  h_lo[is] = ofcDrawer.getOfc(TileCalibDrawerOfc::FieldDG, ich, lo, phase_lo, is);
799 
800  a_hi[is] = ofcDrawer.getOfc(TileCalibDrawerOfc::FieldA, ich, hi, phase_hi, is);
801  b_hi[is] = ofcDrawer.getOfc(TileCalibDrawerOfc::FieldB, ich, hi, phase_hi, is);
802  c_hi[is] = ofcDrawer.getOfc(TileCalibDrawerOfc::FieldC, ich, hi, phase_hi, is);
803  g_hi[is] = ofcDrawer.getOfc(TileCalibDrawerOfc::FieldG, ich, hi, phase_hi, is);
804  h_hi[is] = ofcDrawer.getOfc(TileCalibDrawerOfc::FieldDG, ich, hi, phase_hi, is);
805 
806  }
807 
808  flag = Format6( a_lo, b_lo, c_lo, g_lo, h_lo, ich, phase_lo, calibration_lo, OFC, verbose);
809  if (!flag) return flag;
810  flag = Format6( a_hi, b_hi, c_hi, g_hi, h_hi, ich, phase_hi, calibration_hi, OFC, verbose);
811  if (!flag) return flag;
812 
813  }
814 
815  flag = FormatInfo( nsamples, calibtype, algorithm, runtype, OFC, verbose);
816  if (!flag) return flag;
817 
818  a_lo.clear();
819  b_lo.clear();
820  c_lo.clear();
821  g_lo.clear();
822  h_lo.clear();
823  a_hi.clear();
824  b_hi.clear();
825  c_hi.clear();
826  g_hi.clear();
827  h_hi.clear();
828 
829  return true;
830 }
831 */
plotBeamSpotCompare.x1
x1
Definition: plotBeamSpotCompare.py:216
ReadOfcFromCool.phase
phase
Definition: ReadOfcFromCool.py:127
python.SystemOfUnits.s
int s
Definition: SystemOfUnits.py:131
algorithm
std::string algorithm
Definition: hcg.cxx:82
PowhegControl_ttHplus_NLO.ss
ss
Definition: PowhegControl_ttHplus_NLO.py:83
CaloCellPos2Ntuple.int
int
Definition: CaloCellPos2Ntuple.py:24
index
Definition: index.py:1
max
constexpr double max()
Definition: ap_fixedTest.cxx:33
ConvertOFC
bool ConvertOFC(const vector< vector< vector< vector< double > > > > &w_off, const vector< vector< double > > &calibration, int calibtype, int runtype, vector< unsigned int > &OFC, bool verbose)
Definition: TileOFC.cxx:88
skel.it
it
Definition: skel.GENtoEVGEN.py:396
FormatInfo
bool FormatInfo(int nsamples, int calibrationtype, int algorithm, int runtype, vector< unsigned int > &OFC, bool verbose)
Definition: TileOFC.cxx:437
Double2Int_calib
void Double2Int_calib(double calib, unsigned int &scale, unsigned int &offset, bool verbose)
Definition: TileOFC.cxx:18
yodamerge_tmp.scale
scale
Definition: yodamerge_tmp.py:138
CalibDbCompareRT.calibtype
string calibtype
Definition: CalibDbCompareRT.py:9
run_Egamma1_LArStrip_Fex.dump
dump
Definition: run_Egamma1_LArStrip_Fex.py:88
python.setupRTTAlg.size
int size
Definition: setupRTTAlg.py:39
Format5
bool Format5(const vector< double > &a, const vector< double > &b, const vector< double > &c, const vector< double > &g, const vector< double > &h, vector< unsigned int > &OFC, bool verbose)
Format5.
Definition: TileOFC.cxx:359
ReadOfcFromCool.nphases
nphases
Definition: ReadOfcFromCool.py:116
convertTimingResiduals.sum
sum
Definition: convertTimingResiduals.py:55
ascale
void ascale(TH1F *h, double s_)
Definition: comparitor.cxx:411
Format5calib
bool Format5calib(double calib, vector< unsigned int > &OFC, bool verbose)
Definition: TileOFC.cxx:421
lumiFormat.i
int i
Definition: lumiFormat.py:85
TileOFC.h
h
Format6
bool Format6(const std::vector< double > &a, const std::vector< double > &b, const std::vector< double > &c, const std::vector< double > &g, const std::vector< double > &h, unsigned int channel_index, int phase, double calibration, std::vector< unsigned int > &OFC, bool verbose)
Format6.
Definition: TileOFC.cxx:270
Double2Int_ofc
void Double2Int_ofc(int w_off_size, const vector< double > &w_off, vector< int > &w_dsp, int &w_sum_dsp, int &scale, bool verbose)
Definition: TileOFC.cxx:41
python.CaloCondTools.g
g
Definition: CaloCondTools.py:15
master.flag
bool flag
Definition: master.py:29
find_tgc_unfilled_channelids.ip
ip
Definition: find_tgc_unfilled_channelids.py:3
ReadOFfile
bool ReadOFfile(vector< vector< vector< vector< double > > > > &w_off, char *OFCfile, bool verbose)
Definition: TileOFC.cxx:468
plotBeamSpotMon.b
b
Definition: plotBeamSpotMon.py:77
PlotSFuncertainty.calib
calib
Definition: PlotSFuncertainty.py:110
ReadOfcFromCool.nsamples
nsamples
Definition: ReadOfcFromCool.py:115
DeMoScan.index
string index
Definition: DeMoScan.py:364
a
TList * a
Definition: liststreamerinfos.cxx:10
python.TriggerHandler.verbose
verbose
Definition: TriggerHandler.py:297
python.CaloCondTools.log
log
Definition: CaloCondTools.py:20
convertTimingResiduals.offset
offset
Definition: convertTimingResiduals.py:71
compute_lumi.fin
fin
Definition: compute_lumi.py:19
pow
constexpr int pow(int base, int exp) noexcept
Definition: ap_fixedTest.cxx:15
python.compressB64.c
def c
Definition: compressB64.py:93