All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
BaselineMostProbAve_tool.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 /// \file Baseline.cc
3 /// \author T. Usher
4 ////////////////////////////////////////////////////////////////////////
5 
6 #include <cmath>
8 #include "art/Utilities/ToolMacros.h"
9 #include "art/Utilities/make_tool.h"
10 #include "art/Framework/Principal/Handle.h"
11 #include "art_root_io/TFileService.h"
12 #include "messagefacility/MessageLogger/MessageLogger.h"
13 #include "cetlib_except/exception.h"
14 #include "larcore/CoreUtils/ServiceUtil.h" // lar::providerFrom()
16 #include "icarus_signal_processing/WaveformTools.h"
17 
18 #include <fstream>
19 #include <algorithm> // std::minmax_element()
20 
21 namespace icarus_tool
22 {
23 
25 {
26 public:
27  explicit BaselineMostProbAve(const fhicl::ParameterSet& pset);
28 
30 
31  void configure(const fhicl::ParameterSet& pset) override;
32  void outputHistograms(art::TFileDirectory&) const override;
33 
34  float GetBaseline(const std::vector<float>&, raw::ChannelID_t, size_t, size_t) const override;
35 
36 private:
37  std::pair<float,int> GetBaseline(const std::vector<float>&, int, size_t, size_t) const;
38 
39  size_t fMaxROILength; ///< Maximum length for calculating Most Probable Value
40 
41  icarus_signal_processing::WaveformTools<double> fWaveformTool;
42 
43  art::ServiceHandle<icarusutil::SignalShapingICARUSService> fSignalShaping;
44 };
45 
46 //----------------------------------------------------------------------
47 // Constructor.
48 BaselineMostProbAve::BaselineMostProbAve(const fhicl::ParameterSet& pset)
49 {
50  configure(pset);
51 }
52 
54 {
55 }
56 
57 void BaselineMostProbAve::configure(const fhicl::ParameterSet& pset)
58 {
59  // Recover our fhicl variable
60  fMaxROILength = pset.get<size_t>("MaxROILength", 100);
61 
62  // Get signal shaping service.
63  fSignalShaping = art::ServiceHandle<icarusutil::SignalShapingICARUSService>();
64 
65  return;
66 }
67 
68 
69 float BaselineMostProbAve::GetBaseline(const std::vector<float>& holder,
70  raw::ChannelID_t channel,
71  size_t roiStart,
72  size_t roiLen) const
73 {
74  float base(0.);
75 
76  if (roiLen > 1)
77  {
78  // Recover the expected electronics noise on this channel
79  float deconNoise = 1.26491 * fSignalShaping->GetDeconNoise(channel);
80  int binRange = std::max(1, int(std::round(deconNoise)));
81  size_t halfLen = std::min(fMaxROILength,roiLen/2);
82  size_t roiStop = roiStart + roiLen;
83 
84  // This returns back the mean value and the spread from which it was calculated
85  std::pair<float,int> baseFront = GetBaseline(holder, binRange, roiStart, roiStart + halfLen);
86  std::pair<float,int> baseBack = GetBaseline(holder, binRange, roiStop - halfLen, roiStop );
87 
88 // std::cout << "-Baseline size: " << holder.size() << ", front/back: " << baseFront.first << "/" << baseBack.first << ", ranges: " << baseFront.second << "/" << baseBack.second;
89 
90  // Check for a large spread between the two estimates
91  if (std::abs(baseFront.first - baseBack.first) > 1.5 * deconNoise)
92  {
93  // We're going to favor the front, generally, unless the spread on the back is lower
94  if (baseFront.second < 3 * baseBack.second / 2) base = baseFront.first;
95  else base = baseBack.first;
96  }
97  else
98  {
99  // Otherwise we take a weighted average between the two
100  float rangeFront = baseFront.second;
101  float rangeBack = baseBack.second;
102 
103  base = (baseFront.first/rangeFront + baseBack.first/rangeBack)*(rangeFront*rangeBack)/(rangeFront+rangeBack);
104  }
105 
106 // std::cout << ", base: " << base << std::endl;
107  }
108 
109  return base;
110 }
111 
112 std::pair<float,int> BaselineMostProbAve::GetBaseline(const std::vector<float>& holder,
113  int binRange,
114  size_t roiStart,
115  size_t roiStop) const
116 {
117  std::pair<double,int> base(0.,1);
118  int nTrunc;
119 
120  if (roiStop > roiStart)
121  {
122  // Get the truncated mean and rms
123  icarusutil::TimeVec temp(roiStop - roiStart + 1,0.);
124 
125  std::copy(holder.begin() + roiStart,holder.begin() + roiStop,temp.begin());
126 
127  fWaveformTool.getTruncatedMean(temp, base.first, nTrunc, base.second);
128  }
129 
130  return base;
131 }
132 
133 void BaselineMostProbAve::outputHistograms(art::TFileDirectory& histDir) const
134 {
135  // It is assumed that the input TFileDirectory has been set up to group histograms into a common
136  // folder at the calling routine's level. Here we create one more level of indirection to keep
137  // histograms made by this tool separate.
138 /*
139  std::string dirName = "BaselinePlane_" + std::to_string(fPlane);
140 
141  art::TFileDirectory dir = histDir.mkdir(dirName.c_str());
142 
143  auto const* detprop = lar::providerFrom<detinfo::DetectorPropertiesService>();
144  double samplingRate = detprop->SamplingRate();
145  double numBins = fBaselineVec.size();
146  double maxFreq = 500. / samplingRate;
147  std::string histName = "BaselinePlane_" + std::to_string(fPlane);
148 
149  TH1D* hist = dir.make<TH1D>(histName.c_str(), "Baseline;Frequency(MHz)", numBins, 0., maxFreq);
150 
151  for(int bin = 0; bin < numBins; bin++)
152  {
153  double freq = maxFreq * double(bin + 0.5) / double(numBins);
154 
155  hist->Fill(freq, fBaselineVec.at(bin).Re());
156  }
157 */
158 
159  return;
160 }
161 
162 DEFINE_ART_CLASS_TOOL(BaselineMostProbAve)
163 }
float GetBaseline(const std::vector< float > &, raw::ChannelID_t, size_t, size_t) const override
Utilities related to art service access.
void outputHistograms(art::TFileDirectory &) const override
BaselineMostProbAve(const fhicl::ParameterSet &pset)
size_t fMaxROILength
Maximum length for calculating Most Probable Value.
This provides an interface for tools which are tasked with finding the baselines in input waveforms...
T abs(T value)
std::vector< SigProcPrecision > TimeVec
icarus_signal_processing::WaveformTools< double > fWaveformTool
T copy(T const &v)
art::ServiceHandle< icarusutil::SignalShapingICARUSService > fSignalShaping
unsigned int ChannelID_t
Type representing the ID of a readout channel.
Definition: RawTypes.h:28
void configure(const fhicl::ParameterSet &pset) override