CMS 3D CMS Logo

ComputeClusterTime.h
Go to the documentation of this file.
1 #ifndef RecoParticleFlow_PFClusterProducer_ComputeClusterTime_h
2 #define RecoParticleFlow_PFClusterProducer_ComputeClusterTime_h
3 
4 // user include files
5 #include <algorithm>
6 #include <cmath>
7 #include <vector>
8 
9 
10 // functions to select the hits to compute the time of a given cluster
11 // start with the only hits with timing information
12 // average among the hits contained in the chosen time interval
13 
14 // N.B. time is corrected wrt vtx-calorimeter distance
15 // with straight line and light speed hypothesis
16 // for charged tracks or heavy particles (longer track length or beta < 1)
17 // need to correct the offset at analysis level
18 
19 
20 
22 
23  //time-interval based on that ~210ps wide and with the highest number of hits
24  float fixSizeHighestDensity(std::vector<float>& t, float deltaT=0.210 /*time window in ns*/, float timeWidthBy=0.5){
25 
26  float tolerance = 0.05f;
27  std::sort(t.begin(), t.end());
28 
29  int max_elements = 0;
30  int start_el = 0;
31  int end_el = 0;
32  float timeW = 0.f;
33 
34  for(auto start = t.begin(); start != t.end(); ++start) {
35  const auto startRef = *start;
36  int c = count_if(start, t.end(), [&](float el) {
37  return el - startRef <= deltaT + tolerance;
38  });
39  if (c > max_elements) {
40  max_elements = c;
41  auto last_el = find_if_not(start, t.end(), [&](float el) {
42  return el - startRef <= deltaT + tolerance;
43  });
44  auto val = *(--last_el);
45  if (std::abs(deltaT - (val - startRef)) < tolerance) {
46  tolerance = std::abs(deltaT - (val - startRef));
47  }
48  start_el = distance(t.begin(), start);
49  end_el = distance(t.begin(), last_el);
50  timeW = val - startRef;
51  }
52  }
53 
54  // further adjust time width around the chosen one based on the hits density
55  // proved to improve the resolution: get as many hits as possible provided they are close in time
56 
57  float HalfTimeDiff = timeW * timeWidthBy;
58  float sum = 0.;
59  int num = 0;
60  int totSize = t.size();
61 
62  for(int ij=0; ij<=start_el; ++ij){
63  if(t[ij] > (t[start_el] - HalfTimeDiff) ){
64  for(int kl=ij; kl<totSize; ++kl){
65  if(t[kl] < (t[end_el] + HalfTimeDiff) ){
66  sum += t[kl];
67  ++num;
68  }
69  else break;
70  }
71  break;
72  }
73  }
74 
75  if(num == 0) return -99.;
76  return sum/num;
77  }
78 
79 
80  //useful for future developments - baseline for 0PU
81  /*
82  //time-interval based on the smallest one containing a minimum fraction of hits
83  // vector with time values of the hit; fraction between 0 and 1; how much furher enlarge the selected time window
84  float highestDensityFraction(std::vector<float>& hitTimes, float fractionToKeep=0.68, float timeWidthBy=0.5){
85 
86  std::sort(hitTimes.begin(), hitTimes.end());
87  int totSize = hitTimes.size();
88  int num = 0.;
89  float sum = 0.;
90  float minTimeDiff = 999.;
91  int startTimeBin = 0;
92 
93  int totToKeep = int(totSize*fractionToKeep);
94  int maxStart = totSize - totToKeep;
95 
96  for(int ij=0; ij<maxStart; ++ij){
97  float localDiff = fabs(hitTimes[ij] - hitTimes[int(ij+totToKeep)]);
98  if(localDiff < minTimeDiff){
99  minTimeDiff = localDiff;
100  startTimeBin = ij;
101  }
102  }
103 
104  // further adjust time width around the chosen one based on the hits density
105  // proved to improve the resolution: get as many hits as possible provided they are close in time
106 
107  int startBin = startTimeBin;
108  int endBin = startBin+totToKeep;
109  float HalfTimeDiff = std::abs(hitTimes[startBin] - hitTimes[endBin]) * timeWidthBy;
110 
111  for(int ij=0; ij<startBin; ++ij){
112  if(hitTimes[ij] > (hitTimes[startBin] - HalfTimeDiff) ){
113  for(int kl=ij; kl<totSize; ++kl){
114  if(hitTimes[kl] < (hitTimes[endBin] + HalfTimeDiff) ){
115  sum += hitTimes[kl];
116  ++num;
117  }
118  else break;
119  }
120  break;
121  }
122  }
123 
124  if(num == 0) return -99.;
125  return sum/num;
126  }
127  */
128 }
129 
130 #endif
Definition: start.py:1
float fixSizeHighestDensity(std::vector< float > &t, float deltaT=0.210, float timeWidthBy=0.5)
const double tolerance
Abs< T >::type abs(const T &t)
Definition: Abs.h:22