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