BaselineDetrend_tool.cc
Go to the documentation of this file.
1 //
2 // BaselineDetrend.cc - Baseline detrend tool
3 #include "BaselineDetrend.h"
4 
5 #include "TGraphSmooth.h"
6 #include "TGraph.h"
7 
8 #include <iostream>
9 #include <string>
10 
11 using std::string;
12 using std::vector;
13 using std::cerr;
14 using std::cout;
15 using std::endl;
16 
17 // ctor
19  m_LogLevel( ps.get<int>("LogLevel") ),
20  m_UseBasicROI( ps.get<bool>("UseBasicROI") ),
21  m_Thresh( ps.get<float>("Thresh") ),
22  m_Pad( ps.get<unsigned>("Pad") ),
23  m_MinFrac( ps.get<float>("MinFrac") ),
24  m_Span( ps.get<float>("Span") )
25 {
26  const string myname = "BaselineDetrend::ctor: ";
27  if( m_LogLevel >= 1 ){
28  cout<<myname<<"Use basic ROI "<<m_UseBasicROI<<endl;
29  cout<<myname<<"Threshold to drop samples from average "<<m_Thresh<<endl;
30  cout<<myname<<"Number of ticks to pad around signals above threshold "<<m_Pad<<endl;
31  cout<<myname<<"Min fraction of pedestal samples for detrending "<<m_MinFrac<<endl;
32  cout<<myname<<"LOWESS span parameter "<<m_Span<<endl;
33  }
34 
35  m_GS = new TGraphSmooth( "smoother" );
36 }
37 
38 // dtor
40  m_GS->Delete();
41 }
42 
43 //
45  DataMap ret;
47 
48  vector<unsigned> pedidx;
49  if( !m_UseBasicROI ){
50  // use info from ROI search already performed
51  auto sig = acd.signal;
52  for( unsigned i=0; i<data.size(); ++i ){
53  if( sig[i] ) continue;
54  pedidx.push_back( i );
55  }
56  }
57 
58  // computed baseline trend
59  auto trend = Smoother( data, pedidx );
60 
61  // detrending
62  for(size_t i=0;i<data.size();++i){
63  data[i] -= trend[i];
64  }
65 
66  // finished
67  return ret;
68 }
69 
70 
71 //
72 //
73 // smoother
75  const vector<unsigned> &pedidx ) const
76 {
77  const string myname = "BaselineDetrend::Smoother: ";
78  AdcSignalVector trend( data.size(), 0 );
79 
80  vector<unsigned> idx;
81  if( !pedidx.empty() ){
82  idx = pedidx;
83  }
84  else { // simple ROI threshold finder
85 
86  for( unsigned i = 0; i < data.size(); ++i ){
87  if( data[i] > m_Thresh ) {
88  unsigned count = 1;
89  // remove last elements from the vector
90  while( !idx.empty() ){
91  if( count >= m_Pad ) break;
92  //
93  if( (int)idx.back() != ((int)i - (int)count) ) break;
94  idx.pop_back();
95  count++;
96  }
97 
98  // move forward until we are below threshold + num of samples to skip after
99  count = 0;
100  for( unsigned j = i+1; j < data.size(); ++j ){
101  if( data[j] > m_Thresh ) continue;
102  if( count >= m_Pad ) {
103  i = j;
104  break;
105  }
106  count++;
107  }
108 
109  continue;
110  }
111 
112  //
113  idx.push_back( i );
114  }
115  } // simple roi finder
116 
117  // do nothing if we have only a limited number of ped samples
118  if( ((float)idx.size())/data.size() < m_MinFrac ){
119  if( m_LogLevel >= 2 ){
120  cout<<myname<<"number of baseline samples is too low\n";
121  }
122  return trend;
123  }
124 
125  //
126  TGraph gin( idx.size() );
127  for( unsigned i=0; i<idx.size(); ++i ){
128  gin.SetPoint(i, idx[i], data[idx[i]] );
129  }
130 
131  // do smoothing
132  auto gout = m_GS->SmoothLowess(&gin,"", m_Span);
133 
134  // our graph data are sorted in ticks
135  gout->SetBit(TGraph::kIsSortedX);
136 
137  // compute detranding function interpolating inside ROIs
138  unsigned nidx = idx.size();
139  for( unsigned i = 0; i<nidx; ++i ){
140  unsigned ii = idx[i];
141  double x, y;
142  gout->GetPoint(i, x, y);
143  trend[ii] = y;
144 
145  //
146  if( i == 0 ){
147  if( ii != 0 ){
148  for(unsigned j=0;j<ii;++j)
149  trend[j] = gout->Eval( j );
150  }
151  }
152  else if( i == nidx - 1 ) {
153  unsigned iii = data.size() - 1;
154  if( ii != iii){
155  for(unsigned j=ii;j<=iii;++j)
156  trend[j] = gout->Eval( j );
157  }
158  }
159  else {
160  unsigned iii = idx[i-1];
161  if( (ii - iii ) > 1 ){
162  for(unsigned j=iii;j<ii;++j)
163  trend[j] = gout->Eval( j );
164  }
165  }
166 
167  }
168 
169  return trend;
170 }
171 
DataMap update(AdcChannelData &acd) const override
#define DEFINE_ART_CLASS_TOOL(tool)
Definition: ToolMacros.h:42
std::string string
Definition: nybbler.cc:12
struct vector vector
AdcSignalVector Smoother(const AdcSignalVector &data, const std::vector< unsigned > &pedidx) const
BaselineDetrend(fhicl::ParameterSet const &ps)
static constexpr double ps
Definition: Units.h:99
TGraphSmooth * m_GS
AdcFilterVector signal
std::vector< AdcSignal > AdcSignalVector
Definition: AdcTypes.h:22
list x
Definition: train.py:276
auto const & get(AssnsNode< L, R, D > const &r)
Definition: AssnsNode.h:115
int bool
Definition: qglobal.h:345
AdcSignalVector samples
QTextStream & endl(QTextStream &s)