All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Filter_tool.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 /// \file Filter.cc
3 /// \author T. Usher
4 ////////////////////////////////////////////////////////////////////////
5 
6 #include <cmath>
7 #include "IFilter.h"
8 #include "art/Utilities/ToolMacros.h"
9 #include "art_root_io/TFileService.h"
10 #include "messagefacility/MessageLogger/MessageLogger.h"
11 #include "cetlib_except/exception.h"
14 
15 #include "TF1.h"
16 #include "TProfile.h"
17 
18 #include <fstream>
19 
20 namespace icarus_tool
21 {
22 
23 class Filter : IFilter
24 {
25 public:
26  explicit Filter(const fhicl::ParameterSet& pset);
27 
28  ~Filter();
29 
30  void configure(const fhicl::ParameterSet& pset) override;
31  void setResponse(size_t numBins, double correct3D, double timeScaleFctr) override;
32  void outputHistograms(art::TFileDirectory&) const override;
33 
34  size_t getPlane() const override {return fPlane;}
35  const icarusutil::FrequencyVec& getResponseVec() const override {return fFilterVec;}
36 
37 private:
38  // Member variables from the fhicl file
39  size_t fPlane;
40  std::vector<double> fParameters;
41  std::string fFunctionString;
43 
44  // Container for the field response "function"
46 };
47 
48 //----------------------------------------------------------------------
49 // Constructor.
50 Filter::Filter(const fhicl::ParameterSet& pset)
51 {
52  configure(pset);
53 }
54 
56 {
57  return;
58 }
59 
60 void Filter::configure(const fhicl::ParameterSet& pset)
61 {
62  // Start by recovering the parameters
63  fPlane = pset.get<size_t>("Plane");
64  fParameters = pset.get<std::vector<double>>("FilterParametersVec");
65  fFunctionString = pset.get<std::string>("FilterFunction");
66  fFilterWidthCorrectionFactor = pset.get<double>("FilterWidthCorrectionFactor");
67 
68  return;
69 }
70 
71 void Filter::setResponse(size_t numBins, double correct3D, double timeScaleFctr)
72 {
73  // Note that here we are working in frequency space, not in the time domain...
74  // FIXME: Assumes that the job-level clock data is sufficient.
75  auto const clockData = art::ServiceHandle<detinfo::DetectorClocksService const>()->DataForJob();
76  double samplingRate = 1.e-3 * sampling_rate(clockData); // Note sampling rate is in ns, convert to us
77  double maxFreq = 1.e3 / (2. * samplingRate); // highest frequency in cycles/us (MHz)
78  double freqRes = maxFreq / double(numBins/2); // frequency resolution in cycles/us
79 
80  std::string funcName = "tempFilter";
81  TF1 function(funcName.c_str(),fFunctionString.c_str());
82 
83  // Set the range on the function, probably not really necessary
84  function.SetRange(0, maxFreq);
85 
86  // now to scale the filter function!
87  // only scale params 1,2 &3
88  double timeFactor = 1. / (timeScaleFctr * correct3D * fFilterWidthCorrectionFactor);
89  size_t paramIdx(0);
90 
91  for(const auto& parameter : fParameters) function.SetParameter(paramIdx++, timeFactor * parameter);
92 
93  // Don't assume that the filter vec has not already been initialized...
94  // Note we are setting up the FFT to be the "full" so add a bin for folding over
95  fFilterVec.resize(numBins,icarusutil::ComplexVal(0.,0.));
96 
97  // Keep track of the peak value
98  double peakVal(std::numeric_limits<double>::min());
99 
100  size_t nyquistBin = numBins/2 + 1;
101 
102  // Now ready to set the response vector
103  for(size_t bin = 0; bin < nyquistBin; bin++)
104  {
105  // This takes a sampling rate in ns -> gives a frequency in cycles/us
106  double freq = bin * freqRes;
107  double f = function.Eval(freq);
108 
109  peakVal = std::max(peakVal, f);
110 
112  }
113 
114  // Since we are returning the "full" FFT... folder over the first half
115  for(size_t bin = nyquistBin; bin < numBins; bin++)
116  fFilterVec[bin] = std::conj(fFilterVec[numBins - bin]);
117 
118  // "Normalize" to peak value
119  for(auto& filterValue : fFilterVec) filterValue = filterValue / peakVal;
120 
121  return;
122 }
123 
124 void Filter::outputHistograms(art::TFileDirectory& histDir) const
125 {
126  // It is assumed that the input TFileDirectory has been set up to group histograms into a common
127  // folder at the calling routine's level. Here we create one more level of indirection to keep
128  // histograms made by this tool separate.
129  std::string dirName = "FilterPlane_" + std::to_string(fPlane);
130 
131  art::TFileDirectory dir = histDir.mkdir(dirName.c_str());
132 
133  auto const clockData = art::ServiceHandle<detinfo::DetectorClocksService const>()->DataForJob();
134  double samplingRate = 1.e-3 * sampling_rate(clockData); // Note sampling rate is in ns, convert to us
135  double numBins = fFilterVec.size();
136  double maxFreq = 1.e3 / samplingRate; // Max frequency in MHz
137  double minFreq = maxFreq / numBins;
138  std::string histName = "FilterPlane_" + std::to_string(fPlane);
139  TProfile* hist = dir.make<TProfile>(histName.c_str(), "Filter;Frequency(kHz)", numBins/2, minFreq, 0.5*maxFreq);
140 
141  for(int bin = 0; bin < numBins/2; bin++)
142  {
143  double freq = bin * minFreq;
144 
145  hist->Fill(freq, std::abs(fFilterVec.at(bin)), 1.);
146  }
147 
148 
149  return;
150 }
151 
152 DEFINE_ART_CLASS_TOOL(Filter)
153 }
Filter(const fhicl::ParameterSet &pset)
Definition: Filter_tool.cc:50
Utilities related to art service access.
std::complex< SigProcPrecision > ComplexVal
std::vector< ComplexVal > FrequencyVec
constexpr details::BinObj< T > bin(T value)
Returns a wrapper to print the specified data in binary format.
std::string fFunctionString
Definition: Filter_tool.cc:41
T abs(T value)
std::vector< double > fParameters
Definition: Filter_tool.cc:40
const icarusutil::FrequencyVec & getResponseVec() const override
Definition: Filter_tool.cc:35
void configure(const fhicl::ParameterSet &pset) override
Definition: Filter_tool.cc:60
This is the interface class for a tool to handle a filter for the overall response.
tuple dir
Definition: dropbox.py:28
double fFilterWidthCorrectionFactor
Definition: Filter_tool.cc:42
std::string to_string(WindowPattern const &pattern)
icarusutil::FrequencyVec fFilterVec
Definition: Filter_tool.cc:45
size_t getPlane() const override
Definition: Filter_tool.cc:34
void setResponse(size_t numBins, double correct3D, double timeScaleFctr) override
Definition: Filter_tool.cc:71
void outputHistograms(art::TFileDirectory &) const override
Definition: Filter_tool.cc:124
double sampling_rate(DetectorClocksData const &data)
Returns the period of the TPC readout electronics clock.