All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RawDigitFilterAlg_tool.cc
Go to the documentation of this file.
1 #include "fhiclcpp/ParameterSet.h"
2 #include "art/Framework/Services/Registry/ServiceHandle.h"
3 #include "art_root_io/TFileService.h"
4 #include "art/Framework/Core/ModuleMacros.h"
5 #include "art/Utilities/ToolMacros.h"
6 #include "messagefacility/MessageLogger/MessageLogger.h"
7 
10 
14 
15 #include <cmath>
16 #include <algorithm>
17 
18 #include "TH1.h"
19 #include "TH2.h"
20 #include "TProfile.h"
21 #include "TProfile2D.h"
22 
23 namespace caldata
24 {
25 
26 class RawDigitFilterAlg : virtual public IRawDigitFilter
27 {
28 public:
29 
30  // Copnstructors, destructor.
31  explicit RawDigitFilterAlg(const fhicl::ParameterSet& pset);
33 
34  // Overrides.
35  void configure(const fhicl::ParameterSet& pset) override;
36  void initializeHistograms(art::TFileDirectory&) const override;
37  size_t plane() const override {return fPlane;}
38 
39  void FilterWaveform(RawDigitVector&, size_t, size_t, float = 0.) const override;
40 
41 private:
42 
43  void doAdaptiveFilter(RawDigitVector&) const;
44 
45  // Fcl parameters.
46  float fTruncMeanFraction; ///< Fraction for truncated mean
47  size_t fStructuringElement; ///< Structuring element to use with Top Hat filter
48  bool fFillHistograms; ///< if true then will fill diagnostic hists
49 
50  size_t fPlane;
51 
52  // We'll use this algorithm internally here too
54 
55  // Useful services, keep copies for now (we can update during begin run periods)
56  art::ServiceHandle<geo::Geometry> fGeometry; ///< pointer to Geometry service
57 };
58 
59 //----------------------------------------------------------------------------
60 /// Constructor.
61 ///
62 /// Arguments:
63 ///
64 /// pset - Fcl parameters.
65 ///
66 RawDigitFilterAlg::RawDigitFilterAlg(fhicl::ParameterSet const & pset) :
67  fPlane(0),
68  fCharacterizationAlg(pset)
69 {
70  configure(pset);
71 
72  // Report.
73  mf::LogInfo("RawDigitFilterAlg") << "RawDigitFilterAlg configured\n";
74 }
75 
76 //----------------------------------------------------------------------------
77 /// Destructor.
79 {}
80 
81 //----------------------------------------------------------------------------
82 /// Reconfigure method.
83 ///
84 /// Arguments:
85 ///
86 /// pset - Fcl parameter set.
87 ///
88 void RawDigitFilterAlg::configure(fhicl::ParameterSet const & pset)
89 {
90  fTruncMeanFraction = pset.get<float> ("TruncMeanFraction", 0.15);
91  fStructuringElement = pset.get<size_t>("StructuringElement", 30);
92  fFillHistograms = pset.get<bool> ("FillHistograms", false);
93 
95 }
96 
97 void RawDigitFilterAlg::initializeHistograms(art::TFileDirectory& tfs) const
98 {
99 
100  return;
101 }
102 
103 void RawDigitFilterAlg::FilterWaveform(RawDigitVector& dataVec, size_t channel, size_t cnt, float pedestal) const
104 {
105  // We need to start by collecting mean/rms
106  float truncMean;
107  float rmsVal;
108  int numBins;
109 
110  fCharacterizationAlg.getMeanAndRms(dataVec, truncMean, rmsVal, numBins);
111 
112  // Declare erosion vector
113  std::vector<float> meanVec;
114 
115  meanVec.resize(dataVec.size(), 0);
116  std::vector<float>::iterator meanVecItr = meanVec.begin();
117 
118  size_t halfStructure(fStructuringElement/2);
119 
120  float runningSum(0.);
121 
122  for(size_t idx = 0; idx < halfStructure - 1; idx++) runningSum += dataVec[idx];
123 
124  // First pass through to build the erosion vector
125  for(RawDigitVector::iterator dataItr = dataVec.begin(); dataItr != dataVec.end(); dataItr++)
126  {
127  size_t startOffset = std::distance(dataVec.begin(),dataItr);
128  size_t stopOffset = std::distance(dataItr,dataVec.end());
129  size_t count = std::min(2 * halfStructure, std::min(startOffset + halfStructure, halfStructure + stopOffset));
130 
131  if (startOffset > halfStructure) runningSum -= *(dataItr - halfStructure - 1);
132  if (stopOffset >= halfStructure) runningSum += *(dataItr + halfStructure - 1);
133 
134  float adaptVal = runningSum / float(count) - truncMean;
135 
136  *meanVecItr++ = adaptVal;
137  }
138 
139  // Now do baseline correction
140  for(size_t idx = 0; idx < dataVec.size(); idx++)
141  dataVec[idx] = dataVec[idx] - std::round(meanVec[idx]);
142 
143  return;
144 }
145 
146 DEFINE_ART_CLASS_TOOL(RawDigitFilterAlg)
147 
148 }
bool fFillHistograms
if true then will fill diagnostic hists
art::ServiceHandle< geo::Geometry > fGeometry
pointer to Geometry service
void getMeanAndRms(const RawDigitVector &rawWaveform, float &aveVal, float &rmsVal, int &numBins) const
raw::RawDigit::ADCvector_t RawDigitVector
size_t fStructuringElement
Structuring element to use with Top Hat filter.
float fTruncMeanFraction
Fraction for truncated mean.
void doAdaptiveFilter(RawDigitVector &) const
double distance(geo::Point_t const &point, CathodeDesc_t const &cathode)
Returns the distance of a point from the cathode.
RawDigitCharacterizationAlg fCharacterizationAlg
void configure(const fhicl::ParameterSet &pset) override
void reconfigure(fhicl::ParameterSet const &pset)
void FilterWaveform(RawDigitVector &, size_t, size_t, float=0.) const override
void initializeHistograms(art::TFileDirectory &) const override
RawDigitFilterAlg(const fhicl::ParameterSet &pset)
This provides an interface for tools which are tasked with filtering input waveforms.
size_t plane() const override
art::ServiceHandle< art::TFileService > tfs
std::size_t count(Cont const &cont)
process_name can override from command line with o or output caldata
Definition: pid.fcl:40
art framework interface to geometry description