Sigmoidfilter_module.cc
Go to the documentation of this file.
1 #ifndef SIGMOIDFILTER_H
2 #define SIGMOIDFILTER_H
3 
4 ////////////////////////////////////////////////////////////////////////
5 //
6 // Sigmoidfilter_module
7 //
8 // k.warburton@sheffield.ac.uk
9 //
10 ////////////////////////////////////////////////////////////////////////
11 
12 // framework
19 #include "art_root_io/TFileService.h"
20 #include "art_root_io/TFileDirectory.h"
22 #include "fhiclcpp/ParameterSet.h"
24 
25 // larsoft
33 
34 // lbne-raw-data
35 #include "lbne-raw-data/Services/ChannelMap/ChannelMapService.h"
36 
37 // C++
38 #include <string>
39 #include <vector>
40 #include <iostream>
41 #include <memory>
42 #include <fstream>
43 #include <sstream>
44 
45 // ROOT
46 #include <TTree.h>
47 #include <TMath.h>
48 #include "TF1.h"
49 #include "TH2.h"
50 #include "TH1.h"
51 #include "TVirtualFFT.h"
52 #include "TStyle.h"
53 
54 namespace lbne {
55  class Sigmoidfilter;
56 }
57 
59 
60 public:
61 
62  explicit Sigmoidfilter(fhicl::ParameterSet const& pset);
63  virtual ~Sigmoidfilter();
64 
65  virtual void produce(art::Event& evt) override;
66  void reconfigure(fhicl::ParameterSet const& pset);
67  void beginRun(art::Run & run) override;
68  void beginJob() override;
69  void endRun(art::Run & run) override;
70 
71 private:
72 
75  bool fMakeTree;
76 
77  TF1* fColFilterFunc; ///< Parameterized collection filter function.
78  TF1* fIndUFilterFunc; ///< Parameterized induction filter function.
79  TF1* fIndVFilterFunc; ///< Parameterized induction filter function.
80 
83 
84  TH2F* RawFFT;
85  TH2F* FixFFT;
86 };
87 
88 //-------------------------------------------------------------------
90  std::string colFilt = pset.get<std::string>("ColFilter");
91  fColFilterFunc = new TF1("colFilter", colFilt.c_str());
92 
93  std::string indUFilt = pset.get<std::string>("IndUFilter");
94  fIndUFilterFunc = new TF1("indUFilter", indUFilt.c_str());
95 
96  std::string indVFilt = pset.get<std::string>("IndVFilter");
97  fIndVFilterFunc = new TF1("indVFilter", indVFilt.c_str());
98 
99 
100  this->reconfigure(pset);
101  produces<std::vector<raw::RawDigit> >();
102 
103 }
104 
105 //-------------------------------------------------------------------
107 }
108 
109 //-------------------------------------------------------------------
111  fDigitModuleLabel = pset.get<std::string>("DigitModuleLabel");
112  fDigitModuleInstance = pset.get<std::string>("DigitModuleInstance");
113  fMakeTree = pset.get<bool>("MakeTree");
114 
115  // Make the filter functions.
116  //std::string colFilt = pset.get<std::string>("ColFilter");
117  std::vector<double> colFiltParams = pset.get<std::vector<double> >("ColFilterParams");
118  //fColFilterFunc = new TF1("colFilter", colFilt.c_str());
119  for(unsigned int i=0; i<colFiltParams.size(); ++i)
120  fColFilterFunc->SetParameter(i, colFiltParams[i]);
121 
122  //std::string indUFilt = pset.get<std::string>("IndUFilter");
123  std::vector<double> indUFiltParams = pset.get<std::vector<double> >("IndUFilterParams");
124  //fIndUFilterFunc = new TF1("indUFilter", indUFilt.c_str());
125  for(unsigned int i=0; i<indUFiltParams.size(); ++i)
126  fIndUFilterFunc->SetParameter(i, indUFiltParams[i]);
127 
128  //std::string indVFilt = pset.get<std::string>("IndVFilter");
129  std::vector<double> indVFiltParams = pset.get<std::vector<double> >("IndVFilterParams");
130  //fIndVFilterFunc = new TF1("indVFilter", indVFilt.c_str());
131  for(unsigned int i=0; i<indVFiltParams.size(); ++i)
132  fIndVFilterFunc->SetParameter(i, indVFiltParams[i]);
133 }
134 
135 //-------------------------------------------------------------------
137  return;
138 }
139 //-------------------------------------------------------------------
142  if (fMakeTree) {
143  RawFFT = tfs->make<TH2F>("RawFFT", "Raw FFT for all channels less than 1000 KHz; Channel Number; Frequency (KHz)" , 2048, 0, 2048, 1000, 0, 1000);
144  FixFFT = tfs->make<TH2F>("FixFFT", "FFT for all channels less than 1000 KHz after filtering; Channel Number; Frequency (KHz)", 2048, 0, 2048, 1000, 0, 1000);
145  }
146 }
147 
148 //-------------------------------------------------------------------
150  return;
151 }
152 
153 //-------------------------------------------------------------------
155 
157 
159  evt.getByLabel(fDigitModuleLabel, fDigitModuleInstance, rawDigitHandle);
160  std::vector<raw::RawDigit> const& rawDigitVector(*rawDigitHandle);
161 
162  std::vector<raw::RawDigit> filterRawDigitVector; // The new Vector of RawDigits
163 
164  // If I want to ignore any frequency ranges....
165  std::vector< std::pair<int,int> > ZeroFreq;
166  //ZeroFreq.push_back( std::make_pair(276 , 285 ) );
167  //ZeroFreq.push_back( std::make_pair(558 , 568 ) );
168  //ZeroFreq.push_back( std::make_pair(837 , 849 ) );
169  //ZeroFreq.push_back( std::make_pair(1116, 1127) );
170  //ZeroFreq.push_back( std::make_pair(4340, 5205) );
171 
172  for (size_t DigLoop=0; DigLoop < rawDigitVector.size(); ++DigLoop) {
173  int Channel = rawDigitVector[DigLoop].Channel();
174  size_t NADC = rawDigitVector[DigLoop].NADC();
175  double Pedestal = rawDigitVector[DigLoop].GetPedestal();
176  const geo::View_t view = geo->View(Channel);
177 
178  //std::cout << "Looking at rawDigitVector["<<DigLoop<<"] it was on channel " << rawDigitVector[DigLoop].Channel() << "("<<Channel<<") it is in View " << view
179  // << ", NADC is " << rawDigitVector[DigLoop].NADC() << " ("<<NADC<<")"
180  // << ", pedestal is " << rawDigitVector[DigLoop].GetPedestal() << " ("<<Pedestal<<")"
181  // << std::endl;
182 
183  // Fill the RawDigit histogram for this histogram.
184  TH1F hRawDigit("hRawDigit","",NADC,0,NADC/2);
185  TH1F hRawFFT("hRawFFT" ,"",NADC,0,NADC);
186  for (size_t ADCs=0; ADCs < NADC; ++ADCs) {
187  hRawDigit.SetBinContent( ADCs+1, rawDigitVector[DigLoop].ADC(ADCs)-Pedestal );
188  }
189  for (size_t ww=NADC; ww<NADC; ++ww)
190  hRawFFT.SetBinContent( ww, 0 );
191  // Make the FFT for this channel.
192  hRawDigit.FFT( (&hRawFFT) ,"MAG");
193  for (size_t bin = 0; bin < NADC; ++bin) {
194  double BinVal = hRawFFT.GetBinContent(bin+1);
195  double freq = 2000. * bin / (double)NADC;
196  if (freq < 1000 && BinVal < 1e5 && fMakeTree) {
197  RawFFT->Fill( (Channel-0.5) , freq, BinVal );
198  }
199  }
200 
201  // I want to do an inverse FFT, so need to convert the tranformed FFT into an array....
202  //double Re[NADC], Im[NADC];
203  std::unique_ptr<double[]> Re( new double[NADC]);
204  std::unique_ptr<double[]> Im( new double[NADC]);
205  TVirtualFFT *fft = TVirtualFFT::GetCurrentTransform();
206  fft->GetPointsComplex(Re.get(),Im.get());
207  delete fft;
208 
209  // Set the noisy frequency range bins to an average value.
210  for (size_t aa=0; aa<ZeroFreq.size(); ++aa) {
211  for (int bb=ZeroFreq[aa].first; bb<ZeroFreq[aa].second; ++bb) {
212  double ReMeanVal=0;
213  double ImMeanVal=0;
214  int Range = 50;
215  for (int cc=0; cc<Range; ++cc) {
216  ReMeanVal += Re[ZeroFreq[aa].first-cc] + Re[ZeroFreq[aa].second+cc];
217  ImMeanVal += Im[ZeroFreq[aa].first-cc] + Im[ZeroFreq[aa].second+cc];
218  }
219  ReMeanVal = ReMeanVal / Range;
220  Re[bb] = Re[1500-bb] = ReMeanVal;
221  ImMeanVal = ImMeanVal / Range;
222  Im[bb] = Im[1500-bb] = ImMeanVal;
223  }
224  }
225 
226  // Apply the filter...
227  for (size_t bin = 0; bin < NADC; ++bin) {
228  double freq = 2000. * bin / NADC;
229  if (view == geo::kU) { // U plane
230  Re[bin] = Re[bin]*fIndUFilterFunc->Eval(freq);
231  Im[bin] = Im[bin]*fIndUFilterFunc->Eval(freq);
232  } else if ( view == geo::kV) { // V plane
233  Re[bin] = Re[bin]*fIndVFilterFunc->Eval(freq);
234  Im[bin] = Im[bin]*fIndVFilterFunc->Eval(freq);
235  } else if ( view == geo::kZ) { // Collection plane
236  Re[bin] = Re[bin]*fColFilterFunc->Eval(freq);
237  Im[bin] = Im[bin]*fColFilterFunc->Eval(freq);
238  }
239 
240  double MagVal = pow ( Re[bin]*Re[bin] + Im[bin]*Im[bin], 0.5);
241  if (TMath::IsNaN(MagVal)) MagVal = 0;
242 
243  // Now do the big histograms...
244  if (freq < 1000 && MagVal < 1e5 && fMakeTree) {
245  FixFFT -> Fill( (Channel-0.5) , freq, MagVal );
246  }
247  }
248 
249  // I have applied the filter so now transform back....
250  int NBins = NADC;
251  TVirtualFFT *fft_back = TVirtualFFT::FFT(1, &NBins, "C2R");
252  fft_back->SetPointsComplex(Re.get(),Im.get());
253  fft_back->Transform();
254  TH1 *hb=0;
255  hb = TH1::TransformHisto(fft_back, hb, "Re");
256  delete fft_back;
257  std::vector<short> NewADC;
258  for (int BinNum=0; BinNum<NBins; ++BinNum) {
259  short Val = rawDigitVector[DigLoop].GetPedestal() + hb->GetBinContent(BinNum+1) / NBins;
260  NewADC.push_back( Val);
261  }
262  delete hb;
263  raw::RawDigit theRawDigit( rawDigitVector[DigLoop].Channel(), NewADC.size(), NewADC );
264  theRawDigit.SetPedestal( rawDigitVector[DigLoop].GetPedestal(), rawDigitVector[DigLoop].GetSigma());
265  filterRawDigitVector.push_back( theRawDigit );
266  } // RawDigit Loop
267 
268  // save filtered waveforms to event
269  evt.put(std::make_unique<decltype(filterRawDigitVector)>(std::move(filterRawDigitVector)));
270  return;
271 }
272 
274 
275 #endif //SIGMOIDFILTER_H
TF1 * fIndVFilterFunc
Parameterized induction filter function.
TF1 * fColFilterFunc
Parameterized collection filter function.
Collection of charge vs time digitized from a single readout channel.
Definition: RawDigit.h:69
enum geo::_plane_proj View_t
Enumerate the possible plane projections.
std::string string
Definition: nybbler.cc:12
Planes which measure V.
Definition: geo_types.h:126
EDProducer(fhicl::ParameterSet const &pset)
Definition: EDProducer.h:20
constexpr T pow(T x)
Definition: pow.h:72
art::ServiceHandle< lbne::ChannelMapService > fChannelMap
Definition of basic raw digits.
Planes which measure Z direction.
Definition: geo_types.h:128
void beginRun(art::Run &run) override
Definition: Run.h:21
void reconfigure(fhicl::ParameterSet const &pset)
Planes which measure U.
Definition: geo_types.h:125
bool getByLabel(std::string const &label, std::string const &instance, Handle< PROD > &result) const
Definition: DataViewImpl.h:446
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:69
def move(depos, offset)
Definition: depos.py:107
T get(std::string const &key) const
Definition: ParameterSet.h:231
art::ServiceHandle< geo::Geometry > fGeom
View_t View(geo::PlaneID const &pid) const
Returns the view (wire orientation) on the channels of specified TPC plane.
void SetPedestal(float ped, float sigma=1.)
Set pedestal and its RMS (the latter is 0 by default)
Definition: RawDigit.cxx:68
ChannelMappingService::Channel Channel
QTextStream & bin(QTextStream &s)
Interface for experiment-specific channel quality info provider.
virtual void produce(art::Event &evt) override
void endRun(art::Run &run) override
Sigmoidfilter(fhicl::ParameterSet const &pset)
TCEvent evt
Definition: DataStructs.cxx:7
Interface for experiment-specific service for channel quality info.
TF1 * fIndUFilterFunc
Parameterized induction filter function.
ProductID put(std::unique_ptr< PROD > &&edp, FullSemantic< Level::Run > const semantic)
Definition: DataViewImpl.h:730
Namespace collecting geometry-related classes utilities.
unsigned int run