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