AdcChannelTool.h
Go to the documentation of this file.
1 // AdcChannelTool.h
2 
3 // David Adams
4 // April 2018
5 //
6 // Interface for tools that view or update single objects or
7 // maps of ADC channel data.
8 //
9 // All methods return a DataMap object including a status that should be set
10 // to zero to indicate success.
11 //
12 // Modification might read or write LarSoft digits or wires, add or remove
13 // noise or convert raw to/from prepared.
14 //
15 // Viewing might return histograms, graphs or calculated data in its DataMap.
16 // Or it might write png or root files
17 //
18 // All methods have a default implementation so implementers to not have
19 // implement all.
20 //
21 // A typical viewer, i.e. a tool that does not modify the received data, will
22 // override view(acd) with the data handling and override updateWithView() to
23 // return true so the same handling is obtained with a call to moodify(acds).
24 //
25 // A typical updater, i.e. a tool that does modify its received data, will
26 // override update(acd) with that modification. If it overrides viewWithModify()
27 // to return true, then the default view(acd) will call modify(acd) with a
28 // temporary copy of the received data.
29 //
30 // By default, a call to viewMap(acds) loops over objects it its map, passes
31 // each in turn to view(acd) and concatenates the DataMap results to form the
32 // combined result (DataMap). If updateWithView() is true, the the call is
33 // instead forwarded to updateMap(acds).
34 //
35 // The default for updateMap(acds) does the same using update(acd) unless
36 // viewWithModify() is true in which case the call is forwarded to viewMap(acds).
37 //
38 // Implementers which handle multiple channels, e.g. correlated noise removal
39 // or event display, can instead override viewMap(acds) or updateMap(acds).
40 // The single-object methods view(acd) and update(acd) will return status
41 // interfaceNotImplemented().
42 //
43 // Discussion of this interface may be found at
44 // https://cdcvs.fnal.gov/redmine/issues/19661
45 
46 #ifndef AdcChannelTool_H
47 #define AdcChannelTool_H
48 
52 #include <set>
53 
55 
56 public:
57 
58  using Index = unsigned int;
59 
60  virtual ~AdcChannelTool() =default;
61 
62  static int interfaceNotImplemented() { return -999; }
63 
64  // Methods to update or view data. Implementer should override
65  // at least one of these.
66 
67  // Modify data for one channel.
68  // Default returns error.
69  virtual DataMap update(AdcChannelData&) const;
70 
71  // View data for one channel.
72  // Default calls update with a copy of the data and returns the
73  // DataMap from that call.
74  virtual DataMap view(const AdcChannelData& acd) const;
75 
76  // Modify data for multiple channels.
77  // Default calls update for each channel and appends the result for each success.
78  // The status is set to the number of failures.
79  virtual DataMap updateMap(AdcChannelDataMap& acds) const;
80 
81  // View data for multiple channels.
82  // Default call view for each channel and appends the result for each success.
83  // The status is set to the number of failures.
84  virtual DataMap viewMap(const AdcChannelDataMap& acds) const;
85 
86  // Methods indicating if calls to update should be forwarded to view
87  // or vice versa. Implementers should override neither or one to
88  // return true.
89 
90  // If this is true, default calls to update are forwarded to view
91  // and default calls to updateMap are forwarded to viewMap.
92  virtual bool updateWithView() const { return false; }
93 
94  // If this is true, default calls to view are forwarded to view
95  // and default calls to viewMap are forwarded to updateMap.
96  // In both cases, the passed dat is first copied.
97  virtual bool viewWithUpdate() const { return false; }
98 
99  // Optional call at the start of processing an event.
100  virtual DataMap beginEvent(const DuneEventInfo&) const { return DataMap(); }
101 
102  // Optional call at the start of processing an event.
103  virtual DataMap endEvent(const DuneEventInfo&) const { return DataMap(); }
104 
105  // Optional call to notify tool all events have been processed.
106  // Tools which produce an end-of-processing report or plots should put
107  // or call that code from here and call this method from their dtor
108  // in case it is not called directly.
109  // Argument provides means to pass data in.
110  virtual DataMap close(const DataMap* dmin =nullptr) { return DataMap(); }
111 
112 };
113 
114 //**********************************************************************
115 // Definitions for the above declarations.
116 //**********************************************************************
117 
118 inline
120  if ( updateWithView() ) return view(acd);
122 }
123 
124 //**********************************************************************
125 
126 inline
128  if ( viewWithUpdate() ) {
129  AdcChannelData adctmp(acd);
130  return update(adctmp);
131  }
133 }
134 
135 //**********************************************************************
136 
137 inline
139  if ( updateWithView() ) return viewMap(acds);
140  DataMap ret;
141  DataMap::IntVector failedChannels;
142  std::set<int> failedCodeSet;
143  for ( AdcChannelDataMap::value_type& iacd : acds ) {
144  DataMap dm = update(iacd.second);
146  else if ( dm.status() ) {
147  failedChannels.push_back(iacd.first);
148  failedCodeSet.insert(dm.status());
149  if ( ! ret.status() ) ret.setStatus(dm.status());
150  }
151  else ret += dm;
152  }
153  DataMap::IntVector failedCodes(failedCodeSet.begin(), failedCodeSet.end());
154  ret.setIntVector("failedChannels", failedChannels);
155  ret.setIntVector("failedCodes", failedCodes);
156  return ret;
157 }
158 
159 //**********************************************************************
160 
161 inline
163  if ( viewWithUpdate() ) {
164  AdcChannelDataMap adcstmp(acds);
165  return updateMap(adcstmp);
166  }
167  DataMap ret;
168  int nfail = 0;
169  for ( const AdcChannelDataMap::value_type& iacd : acds ) {
170  DataMap dm = view(iacd.second);
172  else if ( dm.status() ) ++nfail;
173  else ret += dm;
174  }
175  ret.setStatus(nfail);
176  return ret;
177 }
178 
179 //**********************************************************************
180 
181 #endif
virtual bool viewWithUpdate() const
virtual ~AdcChannelTool()=default
unsigned int Index
DataMap & setStatus(int stat)
Definition: DataMap.h:130
virtual DataMap viewMap(const AdcChannelDataMap &acds) const
static int interfaceNotImplemented()
virtual DataMap view(const AdcChannelData &acd) const
virtual DataMap update(AdcChannelData &) const
std::vector< int > IntVector
Definition: DataMap.h:50
int status() const
Definition: DataMap.h:202
void setIntVector(Name name, const IntVector &val)
Definition: DataMap.h:132
virtual DataMap beginEvent(const DuneEventInfo &) const
virtual DataMap close(const DataMap *dmin=nullptr)
virtual DataMap updateMap(AdcChannelDataMap &acds) const
virtual bool updateWithView() const
virtual DataMap endEvent(const DuneEventInfo &) const
std::map< AdcChannel, AdcChannelData > AdcChannelDataMap