TpcToolBasedRawDigitPrepService_service.cc
Go to the documentation of this file.
1 // TpcToolBasedRawDigitPrepService_service.cc
2 
9 #include <iostream>
10 #include <iomanip>
11 
12 #include "valgrind/callgrind.h"
13 
14 using std::string;
15 using std::cout;
16 using std::endl;
17 using std::vector;
18 using std::ostringstream;
19 using std::setw;
20 using std::setprecision;
21 
22 using raw::RawDigit;
23 
24 using Index = unsigned int;
25 
26 //**********************************************************************
27 
30 : m_LogLevel(pset.get<int>("LogLevel")),
31  m_DoWires(pset.get<bool>("DoWires")),
32  m_ToolNames(pset.get<vector<string>>("ToolNames")),
33  m_CallgrindToolNames(pset.get<vector<string>>("CallgrindToolNames")),
34  m_pWireBuildingService(nullptr),
35  m_cgset(m_CallgrindToolNames.begin(), m_CallgrindToolNames.end()),
36  m_pstate(new State) {
37  const string myname = "TpcToolBasedRawDigitPrepService::ctor: ";
38  pset.get_if_present<int>("LogLevel", m_LogLevel);
39  // Fetch the tools.
40  if ( m_ToolNames.size() ) {
42  if ( ptm == nullptr ) {
43  cout << myname << "ERROR: Unable to retrieve tool manaager." << endl;
44  } else {
45  for ( string tname : m_ToolNames ) {
46  if ( m_LogLevel ) cout << myname << " Fetching " << tname << endl;
47  TpcDataToolPtr ptool = ptm->getPrivate<TpcDataTool>(tname);
48  NamedTool nt(tname, ptool.get());
49  if ( nt.tool ) {
50  if ( m_LogLevel ) cout << myname << " Found tool " << tname << " @ " << nt.tool << endl;
51  m_TpcDataTools.push_back(std::move(ptool));
52  m_TpcDataNamedTools.push_back(nt);
53  } else {
54  cout << myname << "ERROR: Unable to retrieve display tool " << tname << endl;
55  }
56  }
57  }
58  }
59  state().toolTimes.resize(m_ToolNames.size());
60  if ( m_DoWires ) {
61  if ( m_LogLevel ) cout << myname << "Fetching wire building service." << endl;
63  if ( m_LogLevel ) cout << myname << " Wire building service: @" << m_pWireBuildingService << endl;
64  }
65  if ( m_LogLevel >=1 ) print(cout, myname);
66 }
67 
68 //**********************************************************************
69 
71  const string myname = "TpcToolBasedRawDigitPrepService:dtor: ";
72  if ( state().nevtBegin != state().nevtEnd ) {
73  cout << myname << "WARNING: Event counts are inconsistent: " << state().nevtBegin
74  << " != " << state().nevtEnd << endl;
75  }
76  Index ntoo = m_ToolNames.size();
77  Index nevt = state().nevtEnd;
78  if ( m_LogLevel >= 1 ) {
79  cout << myname << "Event count: " << nevt << endl;
80  cout << myname << " Call count: " << state().ncall << endl;
81  cout << myname << "Time report for " << ntoo << " tools." << endl;
82  string sunit = "sec/event";
83  float xnevt = float(nevt);
84  if ( nevt == 0 ) {
85  xnevt = 1.0;
86  sunit = "sec";
87  }
88  for ( Index itoo=0; itoo<ntoo; ++itoo ) {
89  string name = m_ToolNames[itoo];
90  double time = state().toolTimes[itoo].count();
91  cout << myname << setw(30) << name << ":"
92  << setw(7) << std::fixed << setprecision(2)
93  << time/xnevt << " " << sunit << endl;
94  }
95  }
96 }
97 
98 //**********************************************************************
99 
101  const string myname = "TpcToolBasedRawDigitPrepService:beginEvent: ";
102  if ( m_LogLevel >= 2 ) {
103  cout << myname << "Begin processing run " << devt.runString();
104  cout << " event " << devt.event;
105  cout << " with " << m_TpcDataNamedTools.size() << " tools." << endl;
106  }
107  if ( state().nevtBegin != state().nevtEnd ) {
108  cout << myname << "WARNING: Event counts are inconsistent: " << state().nevtBegin
109  << " != " << state().nevtEnd << endl;
110  }
111  ++state().nevtBegin;
112  Index nfail = 0;
113  if ( m_TpcDataNamedTools.size() ) {
114  for ( NamedTool nt : m_TpcDataNamedTools ) {
115  DataMap ret = nt.tool->beginEvent(devt);
116  if ( ret.status() ) {
117  ++nfail;
118  cout << myname << "WARNING: Iniitalization for tool " << nt.name
119  << " failed for event " << devt.event << " with status code " << ret.status() << endl;
120  }
121  }
122  }
123  return nfail;
124 }
125 
126 //**********************************************************************
127 
129  const string myname = "TpcToolBasedRawDigitPrepService:endEvent: ";
130  if ( m_LogLevel >= 2 ) {
131  cout << myname << "End processing run " << devt.runString();
132  cout << " event " << devt.event;
133  cout << " with " << m_TpcDataNamedTools.size() << " tools." << endl;
134  }
135  Index nfail = 0;
136  ++state().nevtEnd;
137  if ( state().nevtBegin != state().nevtEnd ) {
138  cout << myname << "WARNING: Event counts are inconsistent: " << state().nevtBegin
139  << " != " << state().nevtEnd << endl;
140  }
141  if ( m_TpcDataNamedTools.size() ) {
142  for ( NamedTool nt : m_TpcDataNamedTools ) {
143  DataMap ret = nt.tool->endEvent(devt);
144  if ( ret.status() ) {
145  ++nfail;
146  cout << myname << "WARNING: Event finalization for tool " << nt.name
147  << " failed for event " << devt.event << " with status code " << ret.status() << endl;
148  }
149  }
150  }
151  return nfail;
152 }
153 
154 //**********************************************************************
155 
158  AdcChannelDataMap& datamap,
159  std::vector<recob::Wire>* pwires, WiredAdcChannelDataMap* pintStates) const {
160  const string myname = "TpcToolBasedRawDigitPrepService:prepare: ";
161  // Loop over tools.
162  ++state().ncall;
163  if ( m_LogLevel >= 2 ) cout << myname << "Processing " << datamap.size() << " channels with "
164  << m_TpcDataNamedTools.size() << " tools." << endl;
165  if ( m_TpcDataNamedTools.size() ) {
166  Index itoo = 0;
167  for ( NamedTool nt : m_TpcDataNamedTools ) {
168  if ( m_LogLevel >= 3 ) cout << myname << " Running tool " << nt.name << endl;
169  bool useCallgrind = m_cgset.count(nt.name);
170  if ( useCallgrind ) {
171  CALLGRIND_START_INSTRUMENTATION;
172  CALLGRIND_TOGGLE_COLLECT;
173  }
174  auto start = Clock::now();
175  DataMap ret = nt.tool->updateMap(datamap);
176  auto stop = Clock::now();
177  if ( useCallgrind ) {
178  CALLGRIND_TOGGLE_COLLECT;
179  CALLGRIND_STOP_INSTRUMENTATION;
180  }
181  Duration dtim =stop - start;
182  state().toolTimes[itoo] += dtim;
183  if ( ret ) {
184  cout << myname << "WARNING: Tool " << nt.name << " failed";
185  if ( ret.haveIntVector("failedChannels") ) {
186  Index ncha = ret.getIntVector("failedChannels").size();
187  cout << " for " << ncha << " channel" << (ncha == 1 ? "" : "s");
188  }
189  cout << " with error " << ret.status();
190  if ( ret.haveIntVector("failedCodes") ) {
191  Index ncod = ret.getIntVector("failedCodes").size();
192  if ( ncod > 1 ) cout << " and " << ncod - 1 << " other errors";
193  }
194  cout << "." << endl;
195  }
196  if ( m_LogLevel >= 4 ) {
197  cout << myname << "----------------------------" << endl;
198  ret.print();
199  cout << myname << "----------------------------" << endl;
200  }
201  ++itoo;
202  }
203  }
204  if ( m_DoWires ) {
205  for ( auto& chdata : datamap ) {
206  AdcChannelData& acd = chdata.second;
207  m_pWireBuildingService->build(acd, pwires);
208  }
209  }
210  return 0;
211 }
212 
213 //**********************************************************************
214 
216 print(std::ostream& out, std::string prefix) const {
217  out << prefix << "TpcToolBasedRawDigitPrepService:" << endl;
218  out << prefix << " LogLevel: " << m_LogLevel << endl;
219  out << prefix << " DoWires: " << m_DoWires << endl;
220  if ( m_TpcDataNamedTools.size() ) {
221  cout << prefix << " ADC channel tools:";
222  for ( const NamedTool& nm : m_TpcDataNamedTools ) {
223  out << "\n" << prefix << " " << nm.name;
224  if ( m_cgset.count(nm.name) ) cout << " (callgrind enabled)";
225  }
226  cout << endl;
227  } else {
228  out << prefix << " No ADC channel tools." << endl;
229  }
230  return out;
231 }
232 
233 //**********************************************************************
234 
236 
237 //**********************************************************************
static QCString name
Definition: declinfo.cpp:673
end
while True: pbar.update(maxval-len(onlies[E][S])) #print iS, "/", len(onlies[E][S]) found = False for...
std::ostream & print(std::ostream &out=std::cout, std::string prefix="") const override
Collection of charge vs time digitized from a single readout channel.
Definition: RawDigit.h:69
std::string string
Definition: nybbler.cc:12
std::chrono::duration< double > Duration
int beginEvent(const DuneEventInfo &devt) const override
struct vector vector
std::unique_ptr< TpcDataTool > TpcDataToolPtr
const IntVector & getIntVector(Name name) const
Definition: DataMap.h:219
unsigned int Index
void print(std::ostream *pout) const
Definition: DataMap.h:245
int prepare(detinfo::DetectorClocksData const &clockData, AdcChannelDataMap &prepdigs, std::vector< recob::Wire > *pwires, WiredAdcChannelDataMap *pintStates) const override
Q_EXPORT QTSManip setprecision(int p)
Definition: qtextstream.h:343
int status() const
Definition: DataMap.h:202
virtual int build(AdcChannelData &data, WireVector *wires) const =0
def move(depos, offset)
Definition: depos.py:107
std::string runString(Index opt=2) const
Q_EXPORT QTSManip setw(int w)
Definition: qtextstream.h:331
std::unique_ptr< T > getPrivate(std::string name)
Contains all timing reference information for the detector.
std::optional< T > get_if_present(std::string const &key) const
Definition: ParameterSet.h:224
const AdcWireBuildingService * m_pWireBuildingService
int endEvent(const DuneEventInfo &devt) const override
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
TpcToolBasedRawDigitPrepService(fhicl::ParameterSet const &pset, art::ActivityRegistry &)
int bool
Definition: qglobal.h:345
bool haveIntVector(Name name) const
Definition: DataMap.h:208
static DuneToolManager * instance(std::string fclname="", int dbg=1)
QTextStream & endl(QTextStream &s)
#define DEFINE_ART_SERVICE_INTERFACE_IMPL(svc, iface)