CMS 3D CMS Logo

EcalShapeBase.cc
Go to the documentation of this file.
3 
4 #include <cmath>
5 #include <algorithm>
6 
7 #include <iostream>
8 
10 {
11 }
12 
13 EcalShapeBase::EcalShapeBase(bool useDBShape) :
14  m_useDBShape (useDBShape),
16  m_firstTimeOverThreshold ( 0.0 ) ,
17  m_indexOfMax ( 0 ) ,
18  m_timeOfMax ( 0.0 ) ,
19  m_thresh ( 0.0 ) ,
20  m_es ( nullptr )
21 {
22 }
23 
24 void EcalShapeBase::setEventSetup( const edm::EventSetup & evtSetup ){ m_es = &evtSetup; buildMe();}
25 
26 
27 double
29 {
31  }
32 
33 double
35 {
36  return m_timeOfMax ;
37 }
38 
39 double
41 {
42  return timeOfMax() - timeOfThr() ;
43 }
44 
45 
46 double
48 {
49  return m_thresh;
50 }
51 
52 
53 void
55 {
56  DVec shapeArray;
57 
58  float time_interval = 0;
59  fillShape(time_interval, m_thresh, shapeArray, m_es) ; // pure virtual function, implementation may vary for EB/EE/APD ...
60  m_arraySize = shapeArray.size(); // original data
61 
62  m_denseArraySize = 10*m_arraySize; // dense array with interpolation between data
63  m_kNBinsPerNSec = (unsigned int) (10/time_interval); // used to be an unsigned int = 10 in < CMSSW10X, should work for time intervals ~0.1, 0.2, 0.5, 1
64  m_qNSecPerBin = time_interval/10.;
65 
66  m_deriv.resize(m_denseArraySize);
67  m_shape.resize(m_denseArraySize);
68 
69  const double maxel ( *max_element( shapeArray.begin(), shapeArray.end() ) ) ;
70 
71  const double maxelt ( 1.e-5 < maxel ? maxel : 1 ) ;
72 
73  for( unsigned int i ( 0 ) ; i != shapeArray.size(); ++i )
74  {
75  shapeArray[i] = shapeArray[i]/maxelt ;
76  }
77 
78  const double thresh ( threshold()/maxelt ) ;
79 
80 
81 
82  const double delta ( m_qNSecPerBin/2. ) ;
83 
84  for( unsigned int denseIndex ( 0 ) ; denseIndex != m_denseArraySize ; ++denseIndex )
85  {
86  const double xb ( ( denseIndex + 0.5 )*m_qNSecPerBin ) ;
87 
88  const unsigned int ibin ( denseIndex/10 ) ;
89 
90  double value = 0.0 ;
91  double deriv = 0.0 ;
92 
93  if( 0 == ibin ||
94  shapeArray.size() == 1 + ibin ) // cannot do quadratic interpolation at ends
95  {
96  value = shapeArray[ibin];
97  deriv = 0.0 ;
98  }
99  else
100  {
101  const double x ( xb - ( ibin + 0.5 )*time_interval ) ;
102  const double f1 ( shapeArray[ ibin - 1 ] ) ;
103  const double f2 ( shapeArray[ ibin ] ) ;
104  const double f3 ( shapeArray[ ibin + 1 ] ) ;
105  const double a ( f2 ) ;
106  const double b ( ( f3 - f1 )/(2.*time_interval) ) ;
107  const double c ( (( f1 + f3 )/2. - f2 )/(time_interval*time_interval) );
108  value = a + b*x + c*x*x;
109  deriv = ( b + 2*c*x )/delta ;
110  }
111 
112  m_shape[ denseIndex ] = value;
113  m_deriv[ denseIndex ] = deriv;
114 
115  if( 0 < denseIndex &&
116  thresh < value &&
118  {
119  m_firstIndexOverThreshold = denseIndex - 1 ;
121  }
122 
123  if( m_shape[ m_indexOfMax ] < value )
124  {
125  m_indexOfMax = denseIndex ;
126  }
127 
128  }
130 
131 }
132 
133 unsigned int
134 EcalShapeBase::timeIndex( double aTime ) const
135 {
136  const int index ( m_firstIndexOverThreshold +
137  (unsigned int) ( aTime*m_kNBinsPerNSec + 0.5 ) ) ;
138 
139  const bool bad ( (int) m_firstIndexOverThreshold > index ||
140  (int) m_denseArraySize <= index ) ;
141 
142  if( (int) m_denseArraySize <= index )
143  {
144  LogDebug("EcalShapeBase") << " ECAL MGPA shape requested for out of range time " << aTime ;
145  }
146  return ( bad ? m_denseArraySize : (unsigned int) index ) ;
147 }
148 
149 double
150 EcalShapeBase::operator() ( double aTime ) const
151 {
152  // return pulse amplitude for request time in ns
153 
154  const unsigned int index ( timeIndex( aTime ) ) ;
155  return ( m_denseArraySize == index ? 0 : m_shape[ index ] ) ;
156 }
157 
158 double
159 EcalShapeBase::derivative( double aTime ) const
160 {
161  const unsigned int index ( timeIndex( aTime ) ) ;
162  return ( m_denseArraySize == index ? 0 : m_deriv[ index ] ) ;
163 }
164 
165 void
166 EcalShapeBase::m_shape_print(const char *fileName){std::ofstream fs; fs.open(fileName); for (auto i:m_shape) fs << "vec.push_back(" << i << ");\n"; fs.close();}
#define LogDebug(id)
dbl * delta
Definition: mlp_gen.cc:36
virtual void fillShape(float &time_interval, double &m_thresh, EcalShapeBase::DVec &aVec, const edm::EventSetup *es) const =0
double derivative(double time) const
double threshold() const
#define nullptr
unsigned int m_denseArraySize
Definition: EcalShapeBase.h:68
~EcalShapeBase() override
Definition: EcalShapeBase.cc:9
std::vector< double > DVec
Definition: EcalShapeBase.h:28
unsigned int timeIndex(double aTime) const
Definition: value.py:1
double m_firstTimeOverThreshold
Definition: EcalShapeBase.h:59
double m_qNSecPerBin
Definition: EcalShapeBase.h:69
void m_shape_print(const char *fileName)
double operator()(double aTime) const override
unsigned int m_arraySize
Definition: EcalShapeBase.h:67
double b
Definition: hdecay.h:120
unsigned int m_indexOfMax
Definition: EcalShapeBase.h:60
double timeOfThr() const
double m_timeOfMax
Definition: EcalShapeBase.h:61
double a
Definition: hdecay.h:121
unsigned int m_firstIndexOverThreshold
Definition: EcalShapeBase.h:58
void setEventSetup(const edm::EventSetup &evtSetup)
const edm::EventSetup * m_es
Definition: EcalShapeBase.h:70
unsigned int m_kNBinsPerNSec
Definition: EcalShapeBase.h:63
double timeToRise() const override
double timeOfMax() const