CnrGroupWeighted_tool.cc
Go to the documentation of this file.
1 // CnrGroupWeighted_tool.cc
2 
3 #include "CnrGroupWeighted.h"
8 #include <iostream>
9 #include <iomanip>
10 
11 using std::string;
12 using std::cout;
13 using std::endl;
14 using std::setw;
15 
16 //**********************************************************************
17 // Class methods.
18 //**********************************************************************
19 
21 : m_LogLevel(ps.get<int>("LogLevel")),
22  m_Weight(ps.get<Name>("Weight")),
23  m_Groups(ps.get<NameVector>("Groups")),
24  m_Options(ps.get<NameVector>("Options")) {
25  const string myname = "CnrGroupWeighted::ctor: ";
26  // Decode the options.
27  for ( Name sopt : m_Options ) {
28  if ( sopt == "mean" ) m_useMedian = false;
29  else if ( sopt == "median" ) m_useMedian = true;
30  else cout << myname << "WARNING: Ignoring invalid option: " << sopt << endl;
31  }
32  // Build the channel map.
33  string crtName = "channelGroups";
35  const IndexRangeGroupTool* pcrt = ptm == nullptr ? nullptr : ptm->getShared<IndexRangeGroupTool>(crtName);
36  for ( Name sgrp : m_Groups ) {
37  IndexRangeGroup grp;
38  if ( pcrt != nullptr ) grp = pcrt->get(sgrp);
39  if ( ! grp.isValid() ) {
40  grp = IndexRangeGroup(sgrp);
41  }
42  if ( ! grp.isValid() ) {
43  cout << myname << "WARNING: Unable to find range group " << sgrp << endl;
44  } else {
45  grp.getIndices(m_chg[grp.name]);
46  }
47  }
48 
49  // Log the configuration.
50  if ( m_LogLevel >= 1 ) {
51  cout << myname << " LogLevel: " << m_LogLevel << endl;
52  cout << myname << " Weight: " << m_Weight << endl;
53  cout << myname << " Groups: [";
54  int count = 0;
55  for ( Name nam : m_Groups ) {
56  if ( count && count%10 == 0 ) cout << "\n ";
57  if ( count++ ) cout << ", ";
58  cout << nam;
59  }
60  cout << "]" << endl;
61  cout << myname << " Options: [";
62  count = 0;
63  for ( Name nam : m_Options ) {
64  if ( count++ ) cout << ", ";
65  cout << nam;
66  }
67  cout << "]" << endl;
68  cout << myname << "Using " << (m_useMedian ? "median" : "mean") << " correction." << endl;
69  cout << myname << "Using " << (m_dropSignal ? "all" : "non-signal") << " samples." << endl;
70  cout << myname << (m_requireGoodChannel ? "R" : "Not r") << "equiring good channel status." << endl;
71  cout << myname << " Group #chan" << endl;
72  for ( const auto& ient : m_chg ) {
73  cout << myname << setw(10) << ient.first << setw(8) << ient.second.size() << endl;
74  }
75  }
76 }
77 
78 //**********************************************************************
79 
81  const string myname = "CnrGroupWeighted::updateMap: ";
82  DataMap ret;
83  if ( acds.size() == 0 ) {
84  std::cout << myname << "WARNING: No channels found." << std::endl;
85  return ret.setStatus(1);
86  }
87  // Loop over groups.
88  for ( const auto& entry : m_chg ) {
89  const IndexVector& channels = entry.second;
90  FloatMap wts;
91  getWeights(channels, acds, wts);
92  std::vector<float> correction = getCorrection(channels, acds, wts);
93  for ( Index ich : channels) {
94  auto iacd = acds.find(ich);
95  if ( iacd == acds.end() ) continue;
96  AdcChannelData& acd = iacd->second;
97  if ( acd.samples.size() == 0 ) continue;
98  if ( acd.samples.size() > correction.size() ) correction.resize(acd.samples.size(), 0.);
99  for ( size_t isam=0; isam<acd.samples.size(); ++isam) {
100  acd.samples[isam] -= wts[ich]*correction[isam];
101  }
102  }
103  }
104  return ret;
105 }
106 
107 //**********************************************************************
108 
111  FloatMap& wts) const {
112  const string myname = "CnrGroupWeighted::getWeights: ";
113  for ( Index ich : channels ) {
114  if ( m_Weight.size() == 0 ) {
115  wts[ich] = 1.0;
116  } else {
117  auto iacd = acds.find(ich);
118  if ( iacd == acds.end() ) continue;
119  const AdcChannelData& acd = iacd->second;
120  if ( acd.hasAttribute(m_Weight) ) {
121  wts[ich] = acd.getAttribute(m_Weight);
122  } else {
123  if ( m_LogLevel >= 1 ) {
124  cout << myname << "WARNING: Channel " << ich << " does not have attribute "
125  << m_Weight << endl;
126  }
127  wts[ich] = 0.0;
128  }
129  }
130  }
131 }
132 
133 //**********************************************************************
134 
137  const FloatMap& wts) const {
138  const string myname = "CnrGroupWeighted::getCorrection: ";
139  Index nsam = 0;
140  std::vector<FloatVector> wsamples;
141  for ( Index ich : channels ) {
142  if ( wts.count(ich) == 0 ) continue;
143  float wt = wts.find(ich)->second;
144  auto iacd = acds.find(ich);
145  if ( iacd == acds.end() ) continue;
146  const AdcChannelData& acd = iacd->second;
147  if ( m_requireGoodChannel && acd.channelStatus() ) continue;
148  if ( acd.samples.size() > nsam ) {
149  nsam = acd.samples.size();
150  wsamples.resize(nsam);
151  }
152  for ( size_t isam=0; isam<acd.samples.size(); ++isam ) {
153  if ( m_dropSignal && acd.signal.size()>isam && acd.signal[isam] ) continue;
154  if ( wt == 0 ) continue;
155  wsamples[isam].push_back(acd.samples[isam]/wt);
156  }
157  }
158  std::vector<float> correction(nsam, 0.0);
159  Index nsamCor = 0;
160  for ( Index isam=0; isam<nsam; ++isam ) {
161  size_t nval = wsamples[isam].size();
162  if ( nval < 2 ) continue;
163  if ( m_useMedian ) {
164  std::sort(wsamples[isam].begin(), wsamples[isam].end());
165  if ( nval%2 == 0 ) correction[isam] = 0.5 * (wsamples[isam][nval/2-1] + wsamples[isam][nval/2]);
166  else correction[isam] = wsamples[isam][nval/2];
167  } else {
168  float sum = 0.0;
169  for ( float val : wsamples[isam] ) sum += val;
170  correction[isam] = sum/float(wsamples[isam].size());
171  }
172  ++nsamCor;
173  }
174  if ( m_LogLevel >= 2 ) cout << myname << "Correcting " << nsamCor << "/" << nsam << " samples." << endl;
175  return correction;
176 }
177 
178 //**********************************************************************
179 
end
while True: pbar.update(maxval-len(onlies[E][S])) #print iS, "/", len(onlies[E][S]) found = False for...
std::vector< Name > NameVector
#define DEFINE_ART_CLASS_TOOL(tool)
Definition: ToolMacros.h:42
QList< Entry > entry
FloatVector getCorrection(const IndexVector &channels, const AdcChannelDataMap &acds, const FloatMap &wts) const
unsigned int Index
DataMap & setStatus(int stat)
Definition: DataMap.h:130
std::string string
Definition: nybbler.cc:12
float getAttribute(Name mname, float def=0.0) const
NameVector m_Options
std::vector< float > FloatVector
decltype(auto) constexpr size(T &&obj)
ADL-aware version of std::size.
Definition: StdUtils.h:92
static constexpr double ps
Definition: Units.h:99
virtual IndexRangeGroup get(Name nam) const =0
ChannelGroups m_chg
bool hasAttribute(Name mname, float def=0.0) const
void getWeights(const IndexVector &channels, const AdcChannelDataMap &acds, FloatMap &wts) const
CnrGroupWeighted(fhicl::ParameterSet const &ps)
Q_EXPORT QTSManip setw(int w)
Definition: qtextstream.h:331
Index channelStatus() const
DataMap updateMap(AdcChannelDataMap &acds) const override
std::map< Index, float > FloatMap
AdcFilterVector signal
std::vector< Index > IndexVector
std::map< AdcChannel, AdcChannelData > AdcChannelDataMap
decltype(auto) constexpr begin(T &&obj)
ADL-aware version of std::begin.
Definition: StdUtils.h:72
auto const & get(AssnsNode< L, R, D > const &r)
Definition: AssnsNode.h:115
bool isValid() const
void getIndices(IndexVector &idxs) const
static DuneToolManager * instance(std::string fclname="", int dbg=1)
AdcSignalVector samples
T * getShared(std::string name)
QTextStream & endl(QTextStream &s)