CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
EcalCoder.cc
Go to the documentation of this file.
6 //#include "CLHEP/Random/RandGaussQ.h"
7 #include <iostream>
8 
9 //#include "Geometry/CaloGeometry/interface/CaloGenericDetId.h"
10 
11 
12 EcalCoder::EcalCoder( bool addNoise ,
13  bool PreMix1 ,
14  EcalCoder::Noisifier* ebCorrNoise0 ,
15  EcalCoder::Noisifier* eeCorrNoise0 ,
16  EcalCoder::Noisifier* ebCorrNoise1 ,
17  EcalCoder::Noisifier* eeCorrNoise1 ,
18  EcalCoder::Noisifier* ebCorrNoise2 ,
19  EcalCoder::Noisifier* eeCorrNoise2 ) :
20  m_peds ( 0 ) ,
21  m_gainRatios ( 0 ) ,
22  m_intercals ( 0 ) ,
23  m_maxEneEB ( 1668.3 ) , // 4095(MAXADC)*12(gain 2)*0.035(GeVtoADC)*0.97
24  m_maxEneEE ( 2859.9 ) , // 4095(MAXADC)*12(gain 2)*0.060(GeVtoADC)*0.97
25  m_addNoise ( addNoise ) ,
26  m_PreMix1 ( PreMix1 )
27  {
28  m_ebCorrNoise[0] = ebCorrNoise0 ;
29  assert( 0 != m_ebCorrNoise[0] ) ;
30  m_eeCorrNoise[0] = eeCorrNoise0 ;
31  m_ebCorrNoise[1] = ebCorrNoise1 ;
32  m_eeCorrNoise[1] = eeCorrNoise1 ;
33  m_ebCorrNoise[2] = ebCorrNoise2 ;
34  m_eeCorrNoise[2] = eeCorrNoise2 ;
35 }
36 
38 {
39 }
40 
41 void
43  double EEscale )
44 {
45  m_maxEneEB = EBscale ;
46  m_maxEneEE = EEscale ;
47 }
48 
49 
50 void
52 {
53  m_peds = pedestals ;
54 }
55 
56 void
58 {
59  m_gainRatios = gainRatios ;
60 }
61 
62 void
64 {
65  m_intercals = ical ;
66 }
67 
68 double
69 EcalCoder::fullScaleEnergy( const DetId & detId ) const
70 {
71  return detId.subdetId() == EcalBarrel ? m_maxEneEB : m_maxEneEE ;
72 }
73 
74 void
76  EcalDataFrame& df ) const
77 {
78  df.setSize( clf.size() ) ;
79  encode( clf, df );
80 /* std::cout<<" **Id=" ;
81  if( CaloGenericDetId( clf.id() ).isEB() )
82  {
83  std::cout<<EBDetId( clf.id() ) ;
84  }
85  else
86  {
87  std::cout<<EEDetId( clf.id() ) ;
88  }
89  std::cout<<", size="<<df.size();
90  for( int i ( 0 ) ; i != df.size() ; ++i )
91  {
92  std::cout<<", "<<df[i];
93  }
94  std::cout<<std::endl ;*/
95 }
96 
97 void
98 EcalCoder::encode( const EcalSamples& ecalSamples ,
99  EcalDataFrame& df ) const
100 {
101  assert( 0 != m_peds ) ;
102 
103  const unsigned int csize ( ecalSamples.size() ) ;
104 
105 
106  DetId detId = ecalSamples.id();
107  double Emax = fullScaleEnergy(detId);
108 
109  //....initialisation
110  if ( ecalSamples[5] > 0. ) LogDebug("EcalCoder") << "Input caloSample" << "\n" << ecalSamples;
111 
112  double LSB[NGAINS+1];
113  double pedestals[NGAINS+1];
114  double widths[NGAINS+1];
115  double gains[NGAINS+1];
116  int maxADC[NGAINS+1];
117  double trueRMS[NGAINS+1];
118 
119  double icalconst = 1. ;
120  findIntercalibConstant( detId, icalconst );
121 
122  for( unsigned int igain ( 0 ); igain <= NGAINS ; ++igain )
123  {
124  // fill in the pedestal and width
125  findPedestal( detId ,
126  igain ,
127  pedestals[igain] ,
128  widths[igain] ) ;
129 
130  if( 0 < igain )
131  trueRMS[igain] = std::sqrt( widths[igain]*widths[igain] - 1./12. ) ;
132 
133  // set nominal value first
134  findGains( detId ,
135  gains );
136 
137  LSB[igain] = 0.;
138  if ( igain > 0 ) LSB[igain]= Emax/(MAXADC*gains[igain]);
139  maxADC[igain] = ADCGAINSWITCH; // saturation at 4080 for middle and high gains x6 & x12
140  if ( igain == NGAINS ) maxADC[igain] = MAXADC; // saturation at 4095 for low gain x1
141  }
142 
143  CaloSamples noiseframe[] = { CaloSamples( detId , csize ) ,
144  CaloSamples( detId , csize ) ,
145  CaloSamples( detId , csize ) } ;
146 
147  const Noisifier* noisy[3] = { ( 0 == m_eeCorrNoise[0] ||
148  EcalBarrel == detId.subdetId() ?
149  m_ebCorrNoise[0] :
150  m_eeCorrNoise[0] ) ,
151  ( EcalBarrel == detId.subdetId() ?
152  m_ebCorrNoise[1] :
153  m_eeCorrNoise[1] ) ,
154  ( EcalBarrel == detId.subdetId() ?
155  m_ebCorrNoise[2] :
156  m_eeCorrNoise[2] ) } ;
157 
158  if( m_addNoise )
159  {
160  noisy[0]->noisify( noiseframe[0] ) ; // high gain
161  if( 0 == noisy[1] ) noisy[0]->noisify( noiseframe[1] ,
162  &noisy[0]->vecgau() ) ; // med
163  if( 0 == noisy[2] ) noisy[0]->noisify( noiseframe[2] ,
164  &noisy[0]->vecgau() ) ; // low
165  }
166 
167 
168  // std::cout << " intercal, LSBs, gains " << icalconst << " " << LSB[0] << " " << LSB[1] << " " << gains[0] << " " << gains[1] << " " << Emax << std::endl;
169 
170  int wait = 0 ;
171  int gainId = 0 ;
172  bool isSaturated = 0;
173 
174  for( unsigned int i ( 0 ) ; i != csize ; ++i )
175  {
176  bool done ( false ) ;
177  int adc = MAXADC ;
178  if( 0 == wait ) gainId = 1 ;
179 
180  // see which gain bin it fits in
181  int igain ( gainId - 1 ) ;
182  do
183  {
184  ++igain ;
185 
186 // if( igain != gainId ) std::cout <<"$$$$ Gain switch from " << gainId
187 // <<" to "<< igain << std::endl ;
188 
189  if( 1 != igain && // not high gain
190  m_addNoise && // want to add noise
191  0 != noisy[igain-1] && // exists
192  noiseframe[igain-1].isBlank() ) // not already done
193  {
194  noisy[igain-1]->noisify( noiseframe[igain-1] ,
195  &noisy[0]->vecgau() ) ;
196  //std::cout<<"....noisifying gain level = "<<igain<<std::endl ;
197  }
198 
199  double signal;
200 
201  if(!m_PreMix1) {
202 
203  // noiseframe filled with zeros if !m_addNoise
204  const double asignal ( pedestals[igain] +
205  ecalSamples[i] /( LSB[igain]*icalconst ) +
206  trueRMS[igain]*noiseframe[igain-1][i] ) ;
207  signal = asignal;
208  }
209  else {
210 
211  const double asignal ( // no pedestals for pre-mixing
212  ecalSamples[i] /( LSB[igain]*icalconst ) );
213  signal = asignal;
214  }
215 
216  // std::cout << " " << ecalSamples[i] << " " << noiseframe[igain-1][i] << std::endl;
217 
218 
219  const int isignal ( signal ) ;
220  const int tmpadc ( signal - (double)isignal < 0.5 ?
221  isignal : isignal + 1 ) ;
222  // LogDebug("EcalCoder") << "DetId " << detId.rawId() << " gain " << igain << " caloSample "
223  // << ecalSamples[i] << " pededstal " << pedestals[igain]
224  // << " noise " << widths[igain] << " conversion factor " << LSB[igain]
225  // << " result (ped,tmpadc)= " << ped << " " << tmpadc;
226 
227  if( tmpadc <= maxADC[igain] )
228  {
229  adc = tmpadc;
230  done = true ;
231  }
232  }
233  while( !done &&
234  igain < 3 ) ;
235 
236  if (igain == 1 )
237  {
238  wait = 0 ;
239  gainId = igain ;
240  }
241  else
242  {
243  if (igain == gainId) --wait ;
244  else
245  {
246  wait = 5 ;
247  gainId = igain ;
248  }
249  }
250 
251 
252  // change the gain for saturation
253  int storeGainId = gainId;
254  if ( gainId == 3 && adc == MAXADC )
255  {
256  storeGainId = 0;
257  isSaturated = true;
258  }
259  // LogDebug("EcalCoder") << " Writing out frame " << i << " ADC = " << adc << " gainId = " << gainId << " storeGainId = " << storeGainId ;
260 
261  df.setSample( i, EcalMGPASample( adc, storeGainId ) );
262  }
263  // handle saturation properly according to IN-2007/056
264  // N.B. - FIXME
265  // still missing the possibility for a signal to pass the saturation threshold
266  // again within the 5 subsequent samples (higher order effect).
267 
268  if ( isSaturated )
269  {
270  for (unsigned int i = 0 ; i < ecalSamples.size() ; ++i)
271  {
272  if ( df.sample(i).gainId() == 0 )
273  {
274  unsigned int hyst = i+1+2;
275  for ( unsigned int j = i+1; j < hyst && j < ecalSamples.size(); ++j )
276  {
277  df.setSample(j, EcalMGPASample(MAXADC, 0));
278  }
279  }
280  }
281  }
282 }
283 
284 void
286  int gainId ,
287  double& ped ,
288  double& width ) const
289 {
290  /*
291  EcalPedestalsMapIterator mapItr
292  = m_peds->m_pedestals.find(detId.rawId());
293  // should I care if it doesn't get found?
294  if(mapItr == m_peds->m_pedestals.end()) {
295  edm::LogError("EcalCoder") << "Could not find pedestal for " << detId.rawId() << " among the " << m_peds->m_pedestals.size();
296  } else {
297  EcalPedestals::Item item = mapItr->second;
298  */
299 
300  /*
301  EcalPedestals::Item const & item = (*m_peds)(detId);
302  ped = item.mean(gainId);
303  width = item.rms(gainId);
304  */
305 
307  ped = (*itped).mean(gainId);
308  width = (*itped).rms(gainId);
309 
310  if ( (detId.subdetId() != EcalBarrel) && (detId.subdetId() != EcalEndcap) )
311  {
312  edm::LogError("EcalCoder") << "Could not find pedestal for " << detId.rawId() << " among the " << m_peds->getMap().size();
313  }
314 
315  /*
316  switch(gainId) {
317  case 0:
318  ped = 0.;
319  width = 0.;
320  case 1:
321  ped = item.mean_x12;
322  width = item.rms_x12;
323  break;
324  case 2:
325  ped = item.mean_x6;
326  width = item.rms_x6;
327  break;
328  case 3:
329  ped = item.mean_x1;
330  width = item.rms_x1;
331  break;
332  default:
333  edm::LogError("EcalCoder") << "Bad Pedestal " << gainId;
334  break;
335  }
336  */
337 
338  LogDebug("EcalCoder") << "Pedestals for " << detId.rawId() << " gain range " << gainId << " : \n" << "Mean = " << ped << " rms = " << width;
339 }
340 
341 
342 void
343 EcalCoder::findGains( const DetId & detId ,
344  double Gains[] ) const
345 {
346  /*
347  EcalGainRatios::EcalGainRatioMap::const_iterator grit=m_gainRatios->getMap().find(detId.rawId());
348  EcalMGPAGainRatio mgpa;
349  if( grit!=m_gainRatios->getMap().end() ){
350  mgpa = grit->second;
351  Gains[0] = 0.;
352  Gains[3] = 1.;
353  Gains[2] = mgpa.gain6Over1() ;
354  Gains[1] = Gains[2]*(mgpa.gain12Over6()) ;
355  LogDebug("EcalCoder") << "Gains for " << detId.rawId() << "\n" << " 1 = " << Gains[1] << "\n" << " 2 = " << Gains[2] << "\n" << " 3 = " << Gains[3];
356  } else {
357  edm::LogError("EcalCoder") << "Could not find gain ratios for " << detId.rawId() << " among the " << m_gainRatios->getMap().size();
358  }
359  */
360 
362  Gains[0] = 0.;
363  Gains[3] = 1.;
364  Gains[2] = (*grit).gain6Over1();
365  Gains[1] = Gains[2]*( (*grit).gain12Over6() );
366 
367 
368  if ( (detId.subdetId() != EcalBarrel) && (detId.subdetId() != EcalEndcap) )
369  {
370  edm::LogError("EcalCoder") << "Could not find gain ratios for " << detId.rawId() << " among the " << m_gainRatios->getMap().size();
371  }
372 
373 }
374 
375 void
377  double& icalconst ) const
378 {
379  EcalIntercalibConstantMC thisconst = 1.;
380  // find intercalib constant for this xtal
381  const EcalIntercalibConstantMCMap &icalMap = m_intercals->getMap();
382  EcalIntercalibConstantMCMap::const_iterator icalit = icalMap.find(detId);
383  if( icalit!=icalMap.end() )
384  {
385  thisconst = (*icalit);
386  if ( icalconst == 0. ) { thisconst = 1.; }
387  }
388  else
389  {
390  edm::LogError("EcalCoder") << "No intercalib const found for xtal " << detId.rawId() << "! something wrong with EcalIntercalibConstants in your DB? ";
391  }
392  icalconst = thisconst;
393 }
int adc(sample_type sample)
get the ADC sample (12 bits)
#define LogDebug(id)
bool m_addNoise
Definition: EcalCoder.h:99
const EcalGainRatios * m_gainRatios
Definition: EcalCoder.h:92
int i
Definition: DBlmapReader.cc:9
int gainId(sample_type sample)
get the gainId (2 bits)
const self & getMap() const
EcalMGPASample sample(int i) const
Definition: EcalDataFrame.h:29
void setIntercalibConstants(const EcalIntercalibConstantsMC *ical)
Definition: EcalCoder.cc:63
int gainId() const
get the gainId (2 bits)
double fullScaleEnergy(const DetId &did) const
limit on the energy scale due to the electronics range
Definition: EcalCoder.cc:69
const EcalIntercalibConstantsMC * m_intercals
Definition: EcalCoder.h:94
double m_maxEneEE
Definition: EcalCoder.h:97
const Noisifier * m_ebCorrNoise[3]
Definition: EcalCoder.h:102
uint32_t rawId() const
get the raw id
Definition: DetId.h:43
void setSize(int)
Definition: EcalDataFrame.h:41
const Noisifier * m_eeCorrNoise[3]
Definition: EcalCoder.h:103
DetId id() const
T sqrt(T t)
Definition: SSEVec.h:48
void setPedestals(const EcalPedestals *pedestals)
can be fetched every event from the EventSetup
Definition: EcalCoder.cc:51
virtual void analogToDigital(const EcalSamples &clf, EcalDataFrame &df) const
from EcalSamples to EcalDataFrame
Definition: EcalCoder.cc:75
int j
Definition: DBlmapReader.cc:9
void findGains(const DetId &detId, double theGains[]) const
Definition: EcalCoder.cc:343
int subdetId() const
get the contents of the subdetector field (not cast into any detector&#39;s numbering enum) ...
Definition: DetId.h:37
bool m_PreMix1
Definition: EcalCoder.h:100
const EcalPedestals * m_peds
Definition: EcalCoder.h:90
void setGainRatios(const EcalGainRatios *gainRatios)
Definition: EcalCoder.cc:57
Definition: DetId.h:18
void setFullScaleEnergy(double EBscale, double EEscale)
Definition: EcalCoder.cc:42
std::vector< Item >::const_iterator const_iterator
bool isSaturated(const Digi &digi, const int &maxADCvalue, int ifirst, int n)
uint32_t size() const
void setSample(int i, EcalMGPASample sam)
Definition: EcalDataFrame.h:43
virtual ~EcalCoder()
dtor
Definition: EcalCoder.cc:37
double m_maxEneEB
Definition: EcalCoder.h:96
const_iterator find(uint32_t rawId) const
void noisify(T &frame, const VecDou *rangau=0) const
const_iterator end() const
float EcalIntercalibConstantMC
void findIntercalibConstant(const DetId &detId, double &icalconst) const
Definition: EcalCoder.cc:376
void findPedestal(const DetId &detId, int gainId, double &pedestal, double &width) const
not yet implemented
Definition: EcalCoder.cc:285
void encode(const EcalSamples &ecalSamples, EcalDataFrame &df) const
produce the pulse-shape
Definition: EcalCoder.cc:98
EcalCoder(bool addNoise, bool PreMix1, Noisifier *ebCorrNoise0, Noisifier *eeCorrNoise0=0, Noisifier *ebCorrNoise1=0, Noisifier *eeCorrNoise1=0, Noisifier *ebCorrNoise2=0, Noisifier *eeCorrNoise2=0)
ctor
Definition: EcalCoder.cc:12