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  for(unsigned int i = 0; i < m_denseArraySize; ++i) { m_deriv.push_back(0.0); m_shape.push_back(0.0); }
67 
68  const double maxel ( *max_element( shapeArray.begin(), shapeArray.end() ) ) ;
69 
70  const double maxelt ( 1.e-5 < maxel ? maxel : 1 ) ;
71 
72  for( unsigned int i ( 0 ) ; i != shapeArray.size(); ++i )
73  {
74  shapeArray[i] = shapeArray[i]/maxelt ;
75  }
76 
77  const double thresh ( threshold()/maxelt ) ;
78 
79 
80 
81  const double delta ( m_qNSecPerBin/2. ) ;
82 
83  for( unsigned int denseIndex ( 0 ) ; denseIndex != m_denseArraySize ; ++denseIndex )
84  {
85  const double xb ( ( denseIndex + 0.5 )*m_qNSecPerBin ) ;
86 
87  const unsigned int ibin ( denseIndex/10 ) ;
88 
89  double value = 0.0 ;
90  double deriv = 0.0 ;
91 
92  if( 0 == ibin ||
93  shapeArray.size() == 1 + ibin ) // cannot do quadratic interpolation at ends
94  {
95  value = shapeArray[ibin];
96  deriv = 0.0 ;
97  }
98  else
99  {
100  const double x ( xb - ( ibin + 0.5 )*time_interval ) ;
101  const double f1 ( shapeArray[ ibin - 1 ] ) ;
102  const double f2 ( shapeArray[ ibin ] ) ;
103  const double f3 ( shapeArray[ ibin + 1 ] ) ;
104  const double a ( f2 ) ;
105  const double b ( ( f3 - f1 )/(2.*time_interval) ) ;
106  const double c ( (( f1 + f3 )/2. - f2 )/(time_interval*time_interval) );
107  value = a + b*x + c*x*x;
108  deriv = ( b + 2*c*x )/delta ;
109  }
110 
111  m_shape[ denseIndex ] = value;
112  m_deriv[ denseIndex ] = deriv;
113 
114  if( 0 < denseIndex &&
115  thresh < value &&
117  {
118  m_firstIndexOverThreshold = denseIndex - 1 ;
120  }
121 
122  if( m_shape[ m_indexOfMax ] < value )
123  {
124  m_indexOfMax = denseIndex ;
125  }
126 
127  }
129 
130 }
131 
132 unsigned int
133 EcalShapeBase::timeIndex( double aTime ) const
134 {
135  const int index ( m_firstIndexOverThreshold +
136  (unsigned int) ( aTime*m_kNBinsPerNSec + 0.5 ) ) ;
137 
138  const bool bad ( (int) m_firstIndexOverThreshold > index ||
139  (int) m_denseArraySize <= index ) ;
140 
141  if( (int) m_denseArraySize <= index )
142  {
143  LogDebug("EcalShapeBase") << " ECAL MGPA shape requested for out of range time " << aTime ;
144  }
145  return ( bad ? m_denseArraySize : (unsigned int) index ) ;
146 }
147 
148 double
149 EcalShapeBase::operator() ( double aTime ) const
150 {
151  // return pulse amplitude for request time in ns
152 
153  const unsigned int index ( timeIndex( aTime ) ) ;
154  return ( m_denseArraySize == index ? 0 : m_shape[ index ] ) ;
155 }
156 
157 double
158 EcalShapeBase::derivative( double aTime ) const
159 {
160  const unsigned int index ( timeIndex( aTime ) ) ;
161  return ( m_denseArraySize == index ? 0 : m_deriv[ index ] ) ;
162 }
163 
164 void
165 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