StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
St_tpcChargeEventC.cxx
1 #include "StDetectorDbMaker/St_tpcChargeEventC.h"
2 #include "St_starClockOnlC.h"
3 
4 
5 double St_tpcChargeEventC::timeDifference(unsigned long long bunchCrossingNumber, int idx) {
6  // time difference between the event # idx and bunchCrossingNumber
7  return ((double) (bunchCrossingNumber - eventBunchCrossing(idx))) /
8  (St_starClockOnlC::instance()->Frequency());
9  }
10 
11 int St_tpcChargeEventC::indexBeforeBunchCrossing(unsigned long long bunchCrossingNumber) {
12  // optimized search for typically looking for an index which
13  // is the same or very close to previously found index
14 
15  int lastIndex = nChargeEvents() - 1;
16  if (lastIndex < 0) return -999;
17 
18  if (localSearchUpperIndex < 0) { // no search yet
19  // start from the middle and move outwards
20  localSearchLowerIndex = nChargeEvents() / 2;
21  localSearchUpperIndex = localSearchLowerIndex;
22  }
23 
24  // try the same one as before first
25  int direction = 0;
26  if (bunchCrossingNumber >= eventBunchCrossing(localSearchUpperIndex) &&
27  localSearchLowerIndex != lastIndex) direction = 1;
28  else if (bunchCrossingNumber < eventBunchCrossing(localSearchLowerIndex)) direction = -1;
29  else return localSearchLowerIndex;
30 
31  int delta = 1; // look up or down just one first
32 
33  while (direction > 0) { // look for higher indices
34  localSearchLowerIndex = localSearchUpperIndex;
35  localSearchUpperIndex = std::min(lastIndex,localSearchUpperIndex + delta);
36  delta = localSearchUpperIndex - localSearchLowerIndex;
37  if (bunchCrossingNumber < eventBunchCrossing(localSearchUpperIndex)) direction = 0;
38  else if (localSearchUpperIndex == lastIndex) {
39  localSearchLowerIndex = lastIndex;
40  return lastIndex; // local indices will be lastIndex,lastIndex
41  } else delta *= 2; // expand range and keep looking for higher indices
42  }
43  while (direction < 0) { // look for lower indices
44  localSearchUpperIndex = localSearchLowerIndex;
45  localSearchLowerIndex = std::max(0,localSearchLowerIndex - delta);
46  delta = localSearchUpperIndex - localSearchLowerIndex;
47  if (bunchCrossingNumber >= eventBunchCrossing(localSearchLowerIndex)) direction = 0;
48  else if (localSearchLowerIndex == 0) {
49  localSearchUpperIndex = 0;
50  return -1; // local indices will be 0,0
51  } else delta *= 2; // expand range and keep looking for lower indices
52  }
53 
54  // already know that the result is within range
55  while (delta > 1) {
56  int tempIndex = localSearchLowerIndex + (delta/2);
57  if (bunchCrossingNumber < eventBunchCrossing(tempIndex)) localSearchUpperIndex = tempIndex;
58  else localSearchLowerIndex = tempIndex;
59  delta = localSearchUpperIndex - localSearchLowerIndex;
60  }
61  // found
62  return localSearchLowerIndex;
63 }
64 
65 int St_tpcChargeEventC::findChargeTimes(unsigned long long bunchCrossingNumber, unsigned long long bunchCrossingWindow) {
66  int idx2 = indexBeforeBunchCrossing(bunchCrossingNumber);
67  int idx1 = indexBeforeBunchCrossing(bunchCrossingNumber-bunchCrossingWindow);
68  int n = idx2-idx1;
69  idx1++; // start from after the bunchCrossingWindow starts
70  localStoreCharges.Set(n,&(eventCharges()[idx1]));
71  localStoreTimesSinceCharges.Set(n);
72  for (int i=0; i<n; i++) // must convert to times
73  localStoreTimesSinceCharges.AddAt(timeDifference(bunchCrossingNumber,idx1+i),i);
74  return n;
75 }
76 
77 int St_tpcChargeEventC::findChargeTimes(unsigned long long bunchCrossingNumber, double timeWindow) {
78  return findChargeTimes(bunchCrossingNumber,
79  (unsigned long long) (timeWindow*(St_starClockOnlC::instance()->Frequency())));
80 }