Diana Software
QPulseTimeConstants.cc
Go to the documentation of this file.
1 #include "QPulseTimeConstants.hh"
2 #include "QVector.hh"
3 #include "QInterval.hh"
4 
5 using namespace std;
6 using namespace Diana;
7 
8 QPulseTimeConstants::QPulseTimeConstants(const std::vector<QInterval>& riseIntervals,const std::vector<QInterval>& decayIntervals)
9 {
10  if(riseIntervals.empty() && decayIntervals.empty()) {
12  err.SetDescription(__FILE__,__LINE__,"Empty rise and decay intervals");
13  DianaThrow(err);
14  }
15  for(size_t v = 0; v < riseIntervals.size(); v++) {
16  if(riseIntervals[v].GetMin() >= riseIntervals[v].GetMax() || riseIntervals[v].GetMax() > 1) {
18  err.SetDescription(__FILE__,__LINE__,"Rise intervals must be < 1, and Min < Max");
19  DianaThrow(err);
20  }
21  fRise.push_back(make_pair(riseIntervals[v].GetMin(),v));
22  fRise.push_back(make_pair(riseIntervals[v].GetMax(),v));
23  }
24 
25  for(size_t v = 0; v < decayIntervals.size(); v++) {
26  if(decayIntervals[v].GetMin() >= decayIntervals[v].GetMax() || decayIntervals[v].GetMax() > 1) {
28  err.SetDescription(__FILE__,__LINE__,"Decay intervals must be < 1, and Min < Max");
29  DianaThrow(err);
30  }
31  fDecay.push_back(make_pair(decayIntervals[v].GetMin(),v));
32  fDecay.push_back(make_pair(decayIntervals[v].GetMax(),v));
33  }
34  sort(fRise.begin(),fRise.end(),PairComparatorAsc);
35  sort(fDecay.begin(),fDecay.end(),PairComparatorAsc);
36 
37 }
38 
39 
41 {
42 }
43 
44 QError QPulseTimeConstants::Compute(const QVector& input, const int maxpos, const double baseLine,vector<double>& rise, vector<double>& decay)
45 {
46  int auxMaxpos=maxpos;
47  if(auxMaxpos < 1 || auxMaxpos >= (int)input.Size()) {
48 
49  //Compute max pos here
50  size_t window = 10;
51  size_t size = (int)input.Size();
52  size_t from = 0;
53  auxMaxpos=-1;
54  int currMax = -1;
55  double prevMaxValue = -1e15, currMaxValue = -1e15;
56  while(from+window < size)
57  {
58  currMax = input.GetMaxIndex(window,from);
59  currMaxValue = input[currMax];
60  if(currMaxValue > prevMaxValue)
61  {
62  prevMaxValue = currMaxValue;
63  auxMaxpos = currMax;
64  }
65  from += window;
66  }
67  }
68  double base = baseLine;
69  if(base < 0) {
70  base = input[0];
71  if(auxMaxpos > 0) base = input.GetMean(auxMaxpos*3./4);
72  }
73  double max = input[auxMaxpos]-base;
74 
75  if(!fRise.empty()) {
76  rise.resize(fRise.size()/2,Q_DOUBLE_DEFAULT);
77  int riseCounter = fRise.size()-1;
78  for(int i = auxMaxpos; i >= 0; i--) {
79  if(input[i]-base < fRise[riseCounter].first*max) {
80  double time = i + (fRise[riseCounter].first*max - input[i]+base)/(input[i+1] - input[i]);
81  if(rise[fRise[riseCounter].second] == Q_DOUBLE_DEFAULT) rise[fRise[riseCounter].second] = time+input.Size();
82  else rise[fRise[riseCounter].second] -= time+input.Size();
83  riseCounter--;
84  i++;
85  }
86  if(riseCounter < 0) break;
87  }
88  }
89  for(size_t v = 0; v < rise.size();v++) {
90  if(rise[v] >= input.Size()) rise[v] = Q_DOUBLE_DEFAULT;
91  }
92 
93  if(!fDecay.empty()) {
94  decay.resize(fDecay.size()/2,Q_DOUBLE_DEFAULT);
95  int decayCounter = fDecay.size()-1;
96  for(int i = auxMaxpos; i < (int)input.Size()-1; i++) {
97  if(input[i]-base < fDecay[decayCounter].first*max) {
98  double time = i - (fDecay[decayCounter].first*max - input[i]+base)/(input[i-1] - input[i]);
99  if(decay[fDecay[decayCounter].second] == Q_DOUBLE_DEFAULT) decay[fDecay[decayCounter].second] = -time;
100  else decay[fDecay[decayCounter].second] += time;
101  decayCounter--;
102  i--;
103  }
104  if(decayCounter < 0) break;
105  }
106  }
107 
108  for(size_t v = 0; v < decay.size();v++) {
109  if(decay[v] < 0) decay[v] = Q_DOUBLE_DEFAULT;
110  }
111 
112 
113 
114  return QERR_SUCCESS;
115 }
116 
117 bool QPulseTimeConstants::PairComparatorAsc ( pair<double, int> l,pair<double, int> r)
118 {
119  return l.first < r.first;
120 }
121 
122 bool QPulseTimeConstants::PairComparatorDesc ( pair<double, int> l,pair<double, int> r)
123 {
124  return l.first > r.first;
125 }
int maxpos
Definition: CheckOFShape.C:59
double max
Definition: CheckOF.C:53
err
Definition: CheckOF.C:114
#define DianaThrow(obj)
Definition: QDianaDebug.hh:26
#define Q_DOUBLE_DEFAULT
Definition: QDiana.hh:24
@ QERR_OUT_OF_RANGE
Definition: QError.hh:28
@ QERR_SUCCESS
Definition: QError.hh:27
error class with error type and description
Definition: QError.hh:115
QPulseTimeConstants(const std::vector< Diana::QInterval > &riseIntervals, const std::vector< Diana::QInterval > &decayIntervals)
static bool PairComparatorAsc(const std::pair< double, int > l, std::pair< double, int > r)
static bool PairComparatorDesc(const std::pair< double, int > l, std::pair< double, int > r)
QError Compute(const Diana::QVector &input, const int maxpos, const double baseLine, std::vector< double > &rise, std::vector< double > &decay)
the Diana namespace is needed because sometimes we use Qt libraries, that use same class names of our...