EcalTiming
 All Classes Functions Variables Pages
EcalCrystalTimingCalibration.h
1 #include "EcalTiming/EcalTiming/interface/EcalTimingEvent.h"
2 #include <vector>
3 #include <TTree.h>
4 
5 /** \class EcalCrystalTimingCalibration EcalCrystalTimingCalibration.h EcalTiming/EcalTiming/interface/EcalCrystalTimingCalibration.h
6  *
7  * Description: add a description here
8  * This class contains all the timing information for a single crystal
9  */
10 
11 //Defines for Dump Status (reason for dumping each crystal)
12 #define DS_NONE 0x00
13 #define DS_HIGH_SKEW 0x01
14 #define DS_UNSTABLE_EN 0x02
15 #define DS_CCU_OOT 0x04
16 #define DS_RING 0x08
17 #define DS_CRYS 0x10
18 
20 {
21 public:
22 // DetId _detId; ///< detId of the channel
23 
24 
25 private:
26 
27  float _sum; ///< scalar sum of the time of each timingEvent
28  float _sum2; ///< scalar sum of the square of the time of each timingEvent
29  unsigned long int _num; ///< number of timingEvents;
30 
31  float _sumE; ///< scalar sum of the energy of each timingEvent: needed for average energy
32  bool _storingEvents;
33 
34  mutable std::map<float, float> _sumWithinNSigma, _sum2WithinNSigma, _sum3WithinNSigma, _sumEWithinNSigma; ///< variables for calculation of mean, stdDev within n-times the origina stdDev (to remove tails)
35  mutable std::map<float, unsigned int> _numWithinNSigma; ///< variables for calculation of mean, stdDev within n-times the origina stdDev (to remove tails)
36 
37 
38  std::vector<EcalTimingEvent> timingEvents; ///< vector containing all the events for this crystal
39  std::vector<EcalTimingEvent>::iterator maxChi2Itr;
40 
41 
42 public:
43  /// default constructor
44  EcalCrystalTimingCalibration(bool weightMean = true) :
45  //_detId(),
46  _sum(0), _sum2(0), _num(0), _sumE(0), _storingEvents(true)
47  //totalChi2(-1),
48  //useWeightedMean(weightMean)
49  {
50  }
51 
52  inline unsigned int num() const
53  {
54  return _num;
55  };
56  inline float mean() const
57  {
58  if(!_num) return -999.f;
59  return _sum / _num;
60  }; ///< average time (mean of the time distribution)
61  inline float stdDev() const ///< standard deviation of the time distribution
62  {
63  float mean_ = mean();
64  return sqrt(_sum2 / _num - mean_ * mean_);
65  };
66  inline float meanError() const
67  {
68  return stdDev() / sqrt(_num);
69  };
70  inline float meanE() const
71  {
72  return _sumE / _num;
73  }; ///< average Energy (mean of the Energy distribution)
74  /* bool operator<( EcalCrystalTimingCalibration& rhs) */
75  /* { */
76  /* if(_detId < rhs._detId) return true; */
77  /* else return false; */
78  /* } */
79  //float totalChi2;
80 
81  float getMeanWithinNSigma(float sigma, float maxRange) const; ///< returns the mean time within abs(mean+ n * stdDev) to reject tails
82  float getStdDevWithinNSigma(float sigma, float maxRange) const; ///< returns the stdDev calculated within abs(mean+ n * stdDev) to reject tails
83  float getNumWithinNSigma(float sigma, float maxRange) const; ///< returns the num calculated within abs(mean+ n * stdDev) to reject tails
84  float getMeanErrorWithinNSigma(float sigma, float maxRange) const; ///< returns the error on the mean calculated within abs(mean+ n * stdDev) to reject tails
85  float getSkewnessWithinNSigma(float sigma, float maxRange) const; ///< returns the skewness calculated within abs(mean+ n * stdDev) to reject tails
86 
87  friend std::ostream& operator<< (std::ostream& os, const EcalCrystalTimingCalibration& s)
88  {
89  os << s.mean() << "\t" << s.stdDev() << "\t" << s.num();
90  return os;
91  }
92 
93  /// add new event for this crystal
94  inline bool add(EcalTimingEvent te_, bool storeEvent = true)
95  {
96  return insertEvent(te_,storeEvent);
97  }
98  inline void clear()
99  {
100  _sum = 0.0f;
101  _sum2 = 0.0f;
102  _num = 0;
103  _sumE = 0.0f;
104  _sumWithinNSigma.clear();
105  _sum2WithinNSigma.clear();
106  _sum3WithinNSigma.clear();
107  _numWithinNSigma.clear();
108 
109  timingEvents.clear();
110  }
111 
112  void dumpToTree(TTree *tree, int ix_, int iy_, int iz_, unsigned int status_, unsigned int elecID_, int iRing_); ///< dump the full set of events in a TTree: need an empty tree
113  void dumpCalibToTree(TTree * tree, int rawid_, int ix_, int iy_, int iz_, unsigned int elecID_, int iRing_) const; ///< dump the callibratoin to the tree
114  /// checks if the time measurement is stable changing the min energy threshold
115  bool isStableInEnergy(float min, float max, float step, std::vector< std::pair<float, EcalCrystalTimingCalibration*> > &ret);
116 
117 private:
118  void calcAllWithinNSigma(float n_sigma, float maxRange = 10) const; ///< calculate sum, sum2, sum3, n for time if time within n x stdDev and store the result
119  // since the values are stored, the calculation is done only once with only one loop over the events
120 
121  /// \todo weighted average by timeError
122  bool insertEvent(EcalTimingEvent te_, bool storeEvent)
123  {
124  if(!storeEvent) _storingEvents = false;
125  //exclude values with wrong timeError estimation
126  //if(!(te_.timeError() > 0 && te_.timeError() < 1000 && te_.timeError() < 3))
127  // return false;
128 
129  _sum += te_.time();
130  _sum2 += te_.time() * te_.time();
131  _sumE += te_.energy();
132  _num++;
133  if(_storingEvents)
134  timingEvents.push_back(te_);
135  return true;
136  }
137 
138  /* int filterOutliers(float threshold = 0.5) */
139  /* { */
140  /* int numPointsErased = 0; */
141 
142  /* while(timingEvents.size() > 4) { */
143  /* updateChi2(); */
144  /* float oldMean = mean; */
145  /* // Erase largest chi2 event */
146  /* EcalTimingEvent toRemove = *maxChi2Itr; */
147  /* timingEvents.erase(maxChi2Itr); */
148  /* //Calculate new mean/error */
149  /* updateChi2(); */
150 
151  /* //Compare to old mean and break if |(newMean-oldMean)| < newSigma */
152  /* //TODO: study acceptance threshold */
153  /* if(fabs(mean - oldMean) < threshold * meanE) { */
154  /* insertEvent(toRemove); */
155  /* break; */
156  /* } else { */
157  /* numPointsErased++; */
158  /* } */
159  /* } */
160 
161  /* return numPointsErased; */
162  /* } */
163 
164 private:
165  /* bool useWeightedMean; */
166 
167  /* //calculate chi2: assume a gaussian distribution */
168  /* void updateChi2() // update individual, total, maxChi2s */
169  /* { */
170  /* if(useWeightedMean) { */
171  /* updateChi2Weighted(); */
172  /* } else { */
173  /* updateChi2Unweighted(); */
174  /* } */
175  /* } */
176 
177  /* void updateChi2Weighted() */
178  /* { */
179  /* updateMeanWeighted(); */
180  /* float chi2 = 0; */
181  /* maxChi2Itr = timingEvents.begin(); */
182 
183  /* for(std::vector<EcalTimingEvent>::iterator itr = timingEvents.begin(); */
184  /* itr != timingEvents.end(); ++itr) { */
185  /* float singleChi = (itr->time - mean) / itr->sigmaTime; */
186  /* itr->chi2 = singleChi * singleChi; */
187  /* chi2 += singleChi * singleChi; */
188 
189  /* if(itr->chi2 > maxChi2Itr->chi2) { */
190  /* maxChi2Itr = itr; */
191  /* } */
192  /* } */
193 
194  /* totalChi2 = chi2; */
195  /* } */
196 
197  /* void updateChi2Unweighted() */
198  /* { */
199  /* updateMeanUnweighted(); */
200  /* float chi2 = 0; */
201  /* maxChi2Itr = timingEvents.begin(); */
202 
203  /* for(std::vector<EcalTimingEvent>::iterator itr = timingEvents.begin(); */
204  /* itr != timingEvents.end(); ++itr) { */
205  /* float singleChi = (itr->time - mean); */
206  /* itr->chi2 = singleChi * singleChi; */
207  /* chi2 += singleChi * singleChi; */
208 
209  /* if(itr->chi2 > maxChi2Itr->chi2) { */
210  /* maxChi2Itr = itr; */
211  /* } */
212  /* } */
213 
214  /* totalChi2 = chi2; */
215  /* } */
216 
217  /* void updateMeanWeighted() */
218  /* { */
219  /* float meanTmp = 0; */
220  /* float mean2Tmp = 0; */
221  /* float sigmaTmp = 0; */
222 
223  /* for(std::vector<EcalTimingEvent>::const_iterator itr = timingEvents.begin(); */
224  /* itr != timingEvents.end(); ++itr) { */
225  /* float sigmaT2 = itr->sigmaTime; */
226  /* sigmaT2 *= sigmaT2; */
227  /* sigmaTmp += 1 / (sigmaT2); */
228  /* meanTmp += (itr->time) / (sigmaT2); */
229  /* mean2Tmp += ((itr->time) * (itr->time)) / (sigmaT2); */
230  /* } */
231 
232  /* meanE = sqrt(1 / sigmaTmp); */
233  /* mean = meanTmp / sigmaTmp; */
234  /* rms = sqrt(mean2Tmp / sigmaTmp); */
235  /* stdDev = sqrt(rms * rms - mean * mean); */
236  /* } */
237 
238  /* void updateMeanUnweighted() */
239  /* { */
240  /* float meanTmp = 0; */
241  /* float mean2Tmp = 0; */
242 
243  /* for(std::vector<EcalTimingEvent>::const_iterator itr = timingEvents.begin(); */
244  /* itr != timingEvents.end(); ++itr) { */
245  /* meanTmp += itr->time; */
246  /* mean2Tmp += (itr->time) * (itr->time); */
247  /* } */
248 
249  /* mean = meanTmp / timingEvents.size(); */
250  /* rms = sqrt(mean2Tmp / timingEvents.size()); */
251  /* stdDev = sqrt(rms * rms - mean * mean); */
252  /* meanE = stdDev / sqrt(timingEvents.size()); // stdDev/sqrt(n) */
253  /* } */
254 
255 };
void calcAllWithinNSigma(float n_sigma, float maxRange=10) const
calculate sum, sum2, sum3, n for time if time within n x stdDev and store the result ...
Definition: EcalCrystalTimingCalibration.cc:43
float getMeanWithinNSigma(float sigma, float maxRange) const
average Energy (mean of the Energy distribution)
Definition: EcalCrystalTimingCalibration.cc:11
bool isStableInEnergy(float min, float max, float step, std::vector< std::pair< float, EcalCrystalTimingCalibration * > > &ret)
checks if the time measurement is stable changing the min energy threshold
Definition: EcalCrystalTimingCalibration.cc:76
float getNumWithinNSigma(float sigma, float maxRange) const
returns the num calculated within abs(mean+ n * stdDev) to reject tails
Definition: EcalCrystalTimingCalibration.cc:4
float getStdDevWithinNSigma(float sigma, float maxRange) const
returns the stdDev calculated within abs(mean+ n * stdDev) to reject tails
Definition: EcalCrystalTimingCalibration.cc:18
Definition: EcalTimingEvent.h:16
EcalCrystalTimingCalibration(bool weightMean=true)
default constructor
Definition: EcalCrystalTimingCalibration.h:44
float _sumE
scalar sum of the energy of each timingEvent: needed for average energy
Definition: EcalCrystalTimingCalibration.h:31
void dumpCalibToTree(TTree *tree, int rawid_, int ix_, int iy_, int iz_, unsigned int elecID_, int iRing_) const
Definition: EcalCrystalTimingCalibration.cc:116
float _sum2
scalar sum of the square of the time of each timingEvent
Definition: EcalCrystalTimingCalibration.h:28
float getSkewnessWithinNSigma(float sigma, float maxRange) const
returns the skewness calculated within abs(mean+ n * stdDev) to reject tails
Definition: EcalCrystalTimingCalibration.cc:32
std::vector< EcalTimingEvent > timingEvents
vector containing all the events for this crystal
Definition: EcalCrystalTimingCalibration.h:38
std::map< float, float > _sumEWithinNSigma
variables for calculation of mean, stdDev within n-times the origina stdDev (to remove tails) ...
Definition: EcalCrystalTimingCalibration.h:34
float time() const
Time is stored in a int16_t in ps. time() returns a float in ns.
Definition: EcalTimingEvent.h:37
bool add(EcalTimingEvent te_, bool storeEvent=true)
add new event for this crystal
Definition: EcalCrystalTimingCalibration.h:94
float stdDev() const
average time (mean of the time distribution)
Definition: EcalCrystalTimingCalibration.h:61
Definition: EcalCrystalTimingCalibration.h:19
float getMeanErrorWithinNSigma(float sigma, float maxRange) const
returns the error on the mean calculated within abs(mean+ n * stdDev) to reject tails ...
Definition: EcalCrystalTimingCalibration.cc:25
void dumpToTree(TTree *tree, int ix_, int iy_, int iz_, unsigned int status_, unsigned int elecID_, int iRing_)
dump the full set of events in a TTree: need an empty tree
Definition: EcalCrystalTimingCalibration.cc:175
unsigned long int _num
number of timingEvents;
Definition: EcalCrystalTimingCalibration.h:29
float _sum
scalar sum of the time of each timingEvent
Definition: EcalCrystalTimingCalibration.h:27
std::map< float, unsigned int > _numWithinNSigma
variables for calculation of mean, stdDev within n-times the origina stdDev (to remove tails) ...
Definition: EcalCrystalTimingCalibration.h:35
float energy() const
Energy is stored in a uint16_t in 10&#39;s of MeV. energy() returns a float in GeV.
Definition: EcalTimingEvent.h:39
bool insertEvent(EcalTimingEvent te_, bool storeEvent)
Definition: EcalCrystalTimingCalibration.h:122