GapStrategy.cc
Go to the documentation of this file.
1 #include "GapStrategy.h"
2 //
3 #include <mevt/ChannelRecData.h>
4 //
5 #include <utl/Trace.h>
6 //
7 #include <deque>
8 
9 namespace MdMuonCounterAG {
10 
11  GapStrategy::GapStrategy(unsigned int windowSize, unsigned int nOnes, unsigned int nGaps, const std::string opt ) :
12  fWindowSize(windowSize) ,
13  fNumOnes(nOnes),
14  fNumGaps(nGaps),
15  fOpt( opt )
16  {
17  }
18 
19  unsigned int
21  const
22  {
23  unsigned int nPatternMatchs = 0;
24  /*
25  * The sample has to contain at least fNumOnes samples.
26  */
27  if (samples.GetSize() >= fNumOnes+fNumGaps) {
28  // Window to accumulate the positive samples
29  // and the zeros following them (til next positive sample).
30  typedef std::deque<std::pair<unsigned int, unsigned int> > Window;
31  typedef Window::const_iterator WinItor;
32  Window win;
33  // Global count of zeros within the current window.
34  unsigned int nZeros = 0;
35  for (utl::TraceB::SizeType sNumber = 0; sNumber < samples.GetSize();) {
36  /*
37  * Window updating.
38  */
39  if (samples[ sNumber ]) {
40  // Register the bin and an own zero count.
41  win.push_back(std::make_pair(sNumber, 0));
42  } else if (! win.empty()){
43  // If there's an open window, increase the zero count
44  // (of the trailing element of the window),
45  win.back().second++;
46  // ... and increase the global count.
47  ++nZeros;
48  }
49  // Advance to the next (or the samples are exhausted).
50  ++sNumber;
51  /*
52  * Window checking.
53  */
54  // If not empty, then a window is being considered.
55  if (! win.empty() ) {
56  // Check if we ran out of samples, or the advanced-to sample lies outside the window.
57  // In both cases we have to analyze what's in the window.
58  if ( sNumber == samples.GetSize() || !(sNumber-win.front().first < fWindowSize) ) {
59  bool detected=false;
60  if (win.size() >= fNumOnes) {
61  double sumOnes=0;
62  double sumZeros=0;
63  WinItor wIt = win.begin();
64  for(; wIt!=win.end(); ++wIt){
65  sumOnes+=1;
66  if(sumOnes+sumZeros>=fNumOnes+fNumGaps)
67  break;
68  sumZeros+=(*wIt).second;
69  }
70  if ( sumOnes>=fNumOnes ) {
71  /*
72  In the "strict" option the number of Zeros (or Gaps) in the signal is fixed by fNumGaps and by
73  definition the pattern has to be fulfilled at the beggining of a trace.
74  This means that only fNumGaps number of Zeros are allow in the signal.
75  Thus the minimum patterns to be matched to fulfill the 1X1 condition is 111 or 101
76  at a beginning of a muon trace within a fWindowSize window.
77  Either 1001 or 11001 are not matched for the strict strategy. To relax this condition and
78  to allow at most fNumGaps number of Zeros, use
79  "relax" option which implies that at most fNumGaps should be present in between the fNumOnes number of Ones.
80  */
81  if ( fOpt=="strict" ) {
82 
83  if ( (sumZeros<=fNumGaps) && (sumOnes+sumZeros==fNumOnes+fNumGaps) )
84  detected=true;
85 
86  } else {//relax
87  //Continue with the sum of Zeros
88  for (bool first=true; wIt!=win.end(); ++wIt, first=false) {
89  if( !first) sumOnes+=1;//fisrt 1 already added in previous loop
90  }
91  double DeltaOnes = (win.back().first-win.front().first);
92 
93  if ( (sumOnes+sumZeros <= fWindowSize) ){ //Sanity check: total amount of samples must not exceed the window
94  if( sumOnes > fNumOnes )
95  detected=true;
96  else if ( (1<DeltaOnes)&&(DeltaOnes<(fNumGaps+1)) )
97  detected=true;
98  }//end sanity if
99  }//end relax condition
100  }//end if sumOnes>=fNumOnes
101  }//end if win.size() >= fNumOnes
102  /*
103  * On-detection actions.
104  */
105  if (detected) {
106  // The bin of detection is the start bin (note that with the strategy
107  // here implemented the first bin is guaranteed to be correspond
108  // to a one, but not the last).
109  nZeros = 0;
110  recData.AddPatternMatchBin(win.front().first);
111  ++nPatternMatchs;
112  // get rid of everything because we have already counted a muon.
113  win.clear();
114  } else {
115  // Erase only the first element (reducing the quantity of ones):
116  // it has been found that a window starting at this one is not good.
117  // Before poping, substract the zeros from the to-be-eliminated one from
118  // the grand total.
119  nZeros -= win.front().second;
120  win.pop_front();
121  }
122  }
123  }
124  }
125  }
126  return nPatternMatchs;
127  }
128 }
129 
130 
std::vector< T >::size_type SizeType
Definition: Trace.h:58
unsigned int operator()(const utl::TraceB &samples, mevt::ChannelRecData &recData) const
Performs the counting.
Definition: GapStrategy.cc:20
SizeType GetSize() const
Definition: Trace.h:156
void AddPatternMatchBin(unsigned int bin)
Adds the time of a pattern match detection.
Template class for a FADC data or calibrated data container. Use the typedefs (TraceD, TraceI, etc.) defined in Trace-fwd.h.
Definition: Trace-fwd.h:19
GapStrategy(unsigned int windowSize, unsigned int nOnes, unsigned int nGaps, const std::string opt="strict")
Creates a functor.
Definition: GapStrategy.cc:11
Channel level reconstruction data.

, generated on Tue Sep 26 2023.