CnrByGroup_tool.cc
Go to the documentation of this file.
1 // CnrByGroup_tool.cc
2 
3 #include "CnrByGroup.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_Groups(ps.get<NameVector>("Groups")),
23  m_Options(ps.get<NameVector>("Options")) {
24  const string myname = "CnrByGroup::ctor: ";
25  // Decode the options.
26  for ( Name sopt : m_Options ) {
27  if ( sopt == "mean" ) m_useMedian = false;
28  else if ( sopt == "median" ) m_useMedian = true;
29  else cout << myname << "WARNING: Ignoring invalid option: " << sopt << endl;
30  }
31  // Build the channel map.
32  string crtName = "channelGroups";
34  const IndexRangeGroupTool* pcrt = ptm == nullptr ? nullptr : ptm->getShared<IndexRangeGroupTool>(crtName);
35  for ( Name sgrp : m_Groups ) {
36  IndexRangeGroup grp;
37  if ( pcrt != nullptr ) grp = pcrt->get(sgrp);
38  if ( ! grp.isValid() ) {
39  grp = IndexRangeGroup(sgrp);
40  }
41  if ( ! grp.isValid() ) {
42  cout << myname << "WARNING: Unable to find range group " << sgrp << endl;
43  } else {
44  grp.getIndices(m_chg[grp.name]);
45  }
46  }
47 
48  // Log the configuration.
49  if ( m_LogLevel >= 1 ) {
50  cout << myname << " LogLevel: " << m_LogLevel << endl;
51  cout << myname << " Groups: [";
52  int count = 0;
53  for ( Name nam : m_Groups ) {
54  if ( count && count%10 == 0 ) cout << "\n ";
55  if ( count++ ) cout << ", ";
56  cout << nam;
57  }
58  cout << "]" << endl;
59  cout << myname << " Options: [";
60  count = 0;
61  for ( Name nam : m_Options ) {
62  if ( count++ ) cout << ", ";
63  cout << nam;
64  }
65  cout << "]" << endl;
66  cout << myname << "Using " << (m_useMedian ? "median" : "mean") << " correction." << endl;
67  cout << myname << "Using " << (m_dropSignal ? "all" : "non-signal") << " samples." << endl;
68  cout << myname << (m_requireGoodChannel ? "R" : "Not r") << "equiring good channel status." << endl;
69  cout << myname << " Group #chan" << endl;
70  for ( const auto& ient : m_chg ) {
71  cout << myname << setw(10) << ient.first << setw(8) << ient.second.size() << endl;
72  }
73  }
74 }
75 
76 //**********************************************************************
77 
79  const string myname = "CnrByGroup::updateMap: ";
80  DataMap ret;
81  if ( acds.size() == 0 ) {
82  std::cout << myname << "WARNING: No channels found." << std::endl;
83  return ret.setStatus(1);
84  }
85  for ( const auto& entry : m_chg ) {
86  const IndexVector& channels = entry.second;
87  std::vector<float> correction = getCorrection(channels, acds);
88  for ( Index ich : channels) {
89  auto iacd = acds.find(ich);
90  if ( iacd == acds.end() ) continue;
91  AdcChannelData& acd = iacd->second;
92  if ( acd.samples.size() == 0 ) continue;
93  if ( acd.samples.size() > correction.size() ) correction.resize(acd.samples.size(), 0.);
94  for ( size_t isam=0; isam<acd.samples.size(); ++isam) {
95  acd.samples[isam] -= correction[isam];
96  }
97  }
98  }
99  return ret;
100 }
101 
102 //**********************************************************************
103 
106  const string myname = "CnrByGroup::getCorrection: ";
107  Index nsam = 0;
108  std::vector<FloatVector> samples;
109  for ( Index ich : channels ) {
110  auto iacd = acds.find(ich);
111  if ( iacd == acds.end() ) continue;
112  const AdcChannelData& acd = iacd->second;
113  if ( m_requireGoodChannel && acd.channelStatus() ) continue;
114  if ( acd.samples.size() > nsam ) {
115  nsam = acd.samples.size();
116  samples.resize(nsam);
117  }
118  for ( size_t isam=0; isam<acd.samples.size(); ++isam ) {
119  if ( m_dropSignal && acd.signal.size()>isam && acd.signal[isam] ) continue;
120  samples[isam].push_back(acd.samples[isam]);
121  }
122  }
123  std::vector<float> correction(nsam, 0.0);
124  Index nsamCor = 0;
125  for ( Index isam=0; isam<nsam; ++isam ) {
126  size_t nval = samples[isam].size();
127  if ( nval < 2 ) continue;
128  if ( m_useMedian ) {
129  std::sort(samples[isam].begin(), samples[isam].end());
130  if ( nval%2 == 0 ) correction[isam] = 0.5 * (samples[isam][nval/2-1] + samples[isam][nval/2]);
131  else correction[isam] = samples[isam][nval/2];
132  } else {
133  float sum = 0.0;
134  for ( float val : samples[isam] ) sum += val;
135  correction[isam] = sum/float(samples[isam].size());
136  }
137  ++nsamCor;
138  }
139  if ( m_LogLevel >= 2 ) cout << myname << "Correcting " << nsamCor << "/" << nsam << " samples." << endl;
140  return correction;
141 }
142 
143 //**********************************************************************
144 
end
while True: pbar.update(maxval-len(onlies[E][S])) #print iS, "/", len(onlies[E][S]) found = False for...
#define DEFINE_ART_CLASS_TOOL(tool)
Definition: ToolMacros.h:42
QList< Entry > entry
NameVector m_Groups
Definition: CnrByGroup.h:52
unsigned int Index
DataMap & setStatus(int stat)
Definition: DataMap.h:130
std::string string
Definition: nybbler.cc:12
CnrByGroup(fhicl::ParameterSet const &ps)
NameVector m_Options
Definition: CnrByGroup.h:53
FloatVector getCorrection(const IndexVector &channels, const AdcChannelDataMap &acds) const
int m_LogLevel
Definition: CnrByGroup.h:51
std::vector< Index > IndexVector
Definition: CnrByGroup.h:46
std::vector< float > FloatVector
Definition: CnrByGroup.h:48
DataMap updateMap(AdcChannelDataMap &acds) const override
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
Q_EXPORT QTSManip setw(int w)
Definition: qtextstream.h:331
Index channelStatus() const
bool m_dropSignal
Definition: CnrByGroup.h:58
AdcFilterVector signal
bool m_requireGoodChannel
Definition: CnrByGroup.h:59
bool m_useMedian
Definition: CnrByGroup.h:57
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
std::vector< Name > NameVector
Definition: CnrByGroup.h:44
T * getShared(std::string name)
QTextStream & endl(QTextStream &s)
ChannelGroups m_chg
Definition: CnrByGroup.h:56
std::string Name
Definition: CnrByGroup.h:43