FclStickyCodeFlagger_tool.cc
Go to the documentation of this file.
1 // FclStickyCodeFlagger_tool.cc
2 
3 #include "FclStickyCodeFlagger.h"
4 #include <iostream>
5 #include <sstream>
6 #include <set>
7 
8 using std::string;
9 using std::cout;
10 using std::endl;
11 using std::istringstream;
14 using IndexSet = std::set<Index>;
16 using NameVector = std::vector<Name>;
17 using IndexVectorMultiMap = std::multimap<Index, IndexVector>;
20 
21 //**********************************************************************
22 // Local definitions.
23 //**********************************************************************
24 
25 // Read from a fcl map of arbitrary fcl-supported type indexed by unsigned int
26 // encoded in fcl name into a multimap with the same index and value type T.
27 // Leading zeroes are stripped from the fcl index.
28 // Fcl: preIII: TT, ...
29 // to map<string,T> m[III] = TT, ...
30 template<class T>
31 int readFclIndexMap(string pre, string sdesc, int logLevel, const fhicl::ParameterSet& pstab, std::multimap<Index,T>& dat) {
32  using MMap = std::multimap<Index,T>;
33  const string myname = "readFclIndexMap: ";
34  Index lpre = pre.size();
35  Index nerr = 0;
36  NameVector chnams = pstab.get_names();
37  if ( logLevel >= 3 ) cout << myname << sdesc << " fcl entry count: " << chnams.size() << endl;
38  for ( Name chnam : chnams ) {
39  if ( logLevel >= 3 ) cout << myname << sdesc << " fetching for " << chnam << endl;
40  bool badnam = true;
41  string::size_type ipos = lpre;
42  string::size_type jpos = chnam.size() - 1;
43  Name msg = "ERROR: " + sdesc + " invalid index specifier: ";
44  if ( chnam.substr(0,ipos) == pre ) {
45  // Remove leading zeroes to be extra careful. Not needed.
46  while ( chnam[ipos] == '0' && ipos+1 < chnam.size() ) ++ipos;
47  while ( jpos > ipos && chnam[jpos] == 'x' ) --jpos;
48  const Index badIndex = -1;
49  string scha = chnam.substr(ipos, jpos+1-ipos);
50  Index icha = badIndex;
51  istringstream sscha(scha);
52  sscha >> icha;
53  if ( dat.count(icha) ) {
54  cout << myname << "WARNING: " + sdesc + " replacing data for index " << icha << endl;
55  }
56  T val = pstab.get<T>(chnam);
57  dat.insert(typename MMap::value_type(icha, val));
58  badnam = false;
59  } else {
60  ++nerr;
61  }
62  if ( badnam ) cout << myname << msg << chnam << endl;
63  }
64  return nerr;
65 }
66 
67 //**********************************************************************
68 // Class methods.
69 //**********************************************************************
70 
72 : m_LogLevel(ps.get<int>("LogLevel")),
73  m_StickyCode(ps.get<AdcFlag>("StickyCode")) {
74  const string myname = "FclStickyCodeFlagger::ctor: ";
75  // Display configuration.
76  if ( m_LogLevel ) {
77  cout << myname << "Configuration: " << endl;
78  cout << myname << " LogLevel: " << m_LogLevel << endl;
79  cout << myname << " StickyCode: " << m_StickyCode << endl;
80  }
81  // Check the sticky code.
82  if ( m_StickyCode < AdcStuck || m_StickyCode >= AdcMitigated ) {
83  cout << myname << "WARNING: Flag for sticky code has an unexpected value: "
84  << m_StickyCode << endl;
85  }
86  // Build the sticky code map.
87  fhicl::ParameterSet pstabsc = ps.get<fhicl::ParameterSet>("StickyCodes");
88  IndexVectorMultiMap mvmap;
89  int nerr = readFclIndexMap("chan", "Sticky codes", m_LogLevel, pstabsc, mvmap);
90  for ( const IndexVectorMultiMap::value_type& imma : mvmap ) {
91  Index icha = imma.first;
92  const IndexVector& newcodes = imma.second;
93  IndexVector& codes = m_stickyCodes[icha];
94  codes.insert(codes.end(), newcodes.begin(), newcodes.end());
95  }
96  Index ncha = m_stickyCodes.size();
97  Index ncod = 0;
98  for ( IndexVectorMap::value_type& ient : m_stickyCodes ) ncod += ient.second.size();
99  cout << myname << "Found " << ncod << " sticky code" << (ncod == 1 ? "" : "s") << " for "
100  << ncha << " channel" << (ncha == 1 ? "" : "s") << "." << endl;
101  if ( nerr ) {
102  cout << myname << "WARNING: Found " << nerr << " error" << (nerr == 1 ? "" : "s")
103  << " while parsing the sticky code map." << endl;
104  }
105  // Build the sticky code range map.
106  fhicl::ParameterSet pstabsr = ps.get<fhicl::ParameterSet>("StickyRanges");
107  nerr = readFclIndexMap("chan", "Sticky ranges", m_LogLevel, pstabsr, m_stickyRanges);
108  IndexSet chset;
109  Index nran = m_stickyRanges.size();
110  for ( IndexPairMap::value_type& ient : m_stickyRanges ) chset.insert(ient.first);
111  ncha = chset.size();
112  cout << myname << "Found " << nran << " sticky range" << (nran == 1 ? "" : "s") << " for "
113  << ncha << " channel" << (ncha == 1 ? "" : "s") << "." << endl;
114  if ( nerr ) {
115  cout << myname << "WARNING: Found " << nerr << " error" << (nerr == 1 ? "" : "s")
116  << " while parsing the sticky range map." << endl;
117  }
118 }
119 
120 //**********************************************************************
121 
123  const string myname = "FclStickyCodeFlagger::view: ";
124  DataMap ret;
125  // Make sure flags is as long as raw.
126  if ( acd.flags.size() < acd.raw.size() ) {
127  cout << myname << "WARNING: Increasing size of the flags vector for channel " << acd.channel() << endl;
128  acd.flags.resize(acd.raw.size(), AdcGood);
129  }
130  IndexSet samplesToFlag;
131  // Find samples with sticky codes.
133  if ( icod != m_stickyCodes.end() ) {
134  const IndexVector& codes = icod->second;
135  for ( Index isam=0; isam<acd.raw.size(); ++isam ) {
136  if ( std::find(codes.begin(), codes.end(), acd.raw[isam]) != codes.end() ) samplesToFlag.insert(isam);
137  }
138  }
139  // Find samples with in sticky code ranges.
140  using IndexPairRange = std::pair<IndexPairMap::const_iterator, IndexPairMap::const_iterator>;
141  IndexPairRange irans = m_stickyRanges.equal_range(acd.channel());
142  for ( IndexPairMap::const_iterator iran=irans.first; iran!=irans.second; ++iran ) {
143  IndexPair ran = iran->second;
144  for ( Index isam=0; isam<acd.raw.size(); ++isam ) {
145  AdcIndex adc = acd.raw[isam];
146  if ( adc >= ran.first && adc <= ran.second ) samplesToFlag.insert(isam);
147  }
148  }
149  // Set flags.
150  for ( AdcIndex isam : samplesToFlag ) acd.flags[isam] = m_StickyCode;
151  // Fill output data map and return.
152  ret.setInt("stickyChannel", acd.channel());
153  ret.setInt("stickyCodeCount", samplesToFlag.size());
154  return ret;
155 }
156 
157 //**********************************************************************
158 
DataMap update(AdcChannelData &acds) const override
std::multimap< Index, IndexVector > IndexVectorMultiMap
std::vector< Index > IndexVector
Index badIndex
#define DEFINE_ART_CLASS_TOOL(tool)
Definition: ToolMacros.h:42
void msg(const char *fmt,...)
Definition: message.cpp:107
short AdcFlag
Definition: AdcTypes.h:29
unsigned int Index
std::string string
Definition: nybbler.cc:12
FclStickyCodeFlagger(fhicl::ParameterSet const &ps)
std::set< Index > IndexSet
ChannelGroupService::Name Name
int readFclIndexMap(string pre, string sdesc, int logLevel, const fhicl::ParameterSet &pstab, std::multimap< Index, T > &dat)
int16_t adc
Definition: CRTFragment.hh:202
intermediate_table::const_iterator const_iterator
unsigned int Index
const AdcFlag AdcGood
Definition: AdcTypes.h:32
IndexVectorMap m_stickyCodes
T get(std::string const &key) const
Definition: ParameterSet.h:271
void setInt(Name name, int val)
Definition: DataMap.h:131
static constexpr double ps
Definition: Units.h:99
AdcCountVector raw
unsigned int AdcIndex
Definition: AdcTypes.h:15
FclStickyCodeFlagger::IndexPair IndexPair
Channel channel() const
const AdcFlag AdcMitigated
Definition: AdcTypes.h:40
std::vector< string > NameVector
std::vector< std::string > get_names() const
FclStickyCodeFlagger::IndexPairMap IndexPairMultiMap
std::vector< Index > IndexVector
std::pair< Index, Index > IndexPair
auto const & get(AssnsNode< L, R, D > const &r)
Definition: AssnsNode.h:115
std::multimap< Index, IndexPair > IndexPairMap
QTextStream & endl(QTextStream &s)
AdcFlagVector flags