StandardAdcChannelStringTool_tool.cc
Go to the documentation of this file.
1 // StandardAdcChannelStringTool_tool.cc
2 
5 #include <iostream>
6 #include <iomanip>
7 #include "TTimeStamp.h"
8 
9 using std::string;
10 using std::cout;
11 using std::endl;
12 using std::istringstream;
13 using std::ostringstream;
14 using std::setw;
15 
16 //**********************************************************************
17 // Class methods.
18 //**********************************************************************
19 
22 : m_LogLevel(ps.get<int>("LogLevel")),
23  m_RunWidth(ps.get<Index>("RunWidth")),
24  m_SubRunWidth(ps.get<Index>("SubRunWidth")),
25  m_EventWidth(ps.get<Index>("EventWidth")),
26  m_ChannelWidth(ps.get<Index>("ChannelWidth")),
27  m_CountWidth(ps.get<Index>("CountWidth")),
28  m_FembWidth(ps.get<Index>("FembWidth")),
29  m_TriggerWidth(ps.get<Index>("TriggerWidth")),
30  m_TrigNames(ps.get<NameVector>("TrigNames")) {
31  const string myname = "StandardAdcChannelStringTool::ctor: ";
32  m_reps[0] = "RUN";
33  m_reps[1] = "SUBRUN";
34  m_reps[2] = "EVENT";
35  m_reps[3] = "CHAN";
36  m_reps[4] = "COUNT";
37  m_reps[5] = "CHAN1";
38  m_reps[6] = "CHAN2";
39  m_reps[7] = "FEMB";
40  m_reps[8] = "TRIG";
41  m_wids[0] = m_RunWidth;
42  m_wids[1] = m_SubRunWidth;
43  m_wids[2] = m_EventWidth;
45  m_wids[4] = m_CountWidth;
48  m_wids[7] = m_FembWidth;
50  m_bads[0] = "RunNotFound";
51  m_bads[1] = "SubRunNotFound";
52  m_bads[2] = "EventNotFound";
53  m_bads[3] = "ChannelNotFound";
54  m_bads[4] = "CountNotFound";
55  m_bads[5] = "Channel1NotFound";
56  m_bads[6] = "Channel2NotFound";
57  m_bads[7] = "FembNotFound";
58  m_bads[8] = "TriggerNotFound";
59  if ( m_LogLevel >= 1 ) {
60  cout << myname << "Configuration parameters:" << endl;
61  cout << myname << " LogLevel: " << m_LogLevel << endl;
62  cout << myname << " RunWidth: " << m_RunWidth << endl;
63  cout << myname << " SubRunWidth: " << m_SubRunWidth << endl;
64  cout << myname << " EventWidth: " << m_EventWidth << endl;
65  cout << myname << " ChannelWidth: " << m_ChannelWidth << endl;
66  cout << myname << " CountWidth: " << m_CountWidth << endl;
67  cout << myname << " FembWidth: " << m_CountWidth << endl;
68  cout << myname << " TriggerWidth: " << m_TriggerWidth << endl;
69  cout << myname << " TrigNames: [";
70  Index icnt = 0;
71  for ( Name tnam : m_TrigNames ) {
72  if ( icnt ) {
73  cout << ", ";
74  if ( (icnt/10)*10 == icnt ) cout << "\n" << myname << " ";
75  }
76  cout << tnam;
77  ++icnt;
78  }
79  cout << "]" << endl;
80  }
81 }
82 
83 //**********************************************************************
84 
86 build(const AdcChannelData& acd, const DataMap& dm, string spat) const {
87  const string myname = "StandardAdcChannelStringTool::build: ";
88  // First replace the indices (run, event, ....)
89  Index itrig = acd.trigger();
90  Index vals[m_nrep] = {acd.run(), acd.subRun(), acd.event(), acd.channel(),
91  Index(dm.getInt("count")),
92  Index(dm.getInt("chan1")),
93  Index(dm.getInt("chan2")),
94  acd.fembID(),
95  itrig};
96  bool isBad[m_nrep] = {
97  acd.run() == AdcChannelData::badIndex(),
101  !dm.haveInt("count"),
102  !dm.haveInt("chan1"),
103  !dm.haveInt("chan2"),
104  acd.fembID() == AdcChannelData::badIndex(),
106  };
107  string sout = spat;
108  for ( Index irep=0; irep<m_nrep; ++irep ) {
109  string smat = m_reps[irep] + "%";
110  string::size_type ipos = 1; // Current index in string
111  Index wNew = 0; // Width of the replacement field.
112  while ( (ipos = sout.find(smat, ipos)) != string::npos ) {
113  // Check for replacement with natural width.
114  string::size_type iposRep = ipos; // The position where we make the replacement.
115  string::size_type wOld = 0; // Width of the field to be replaced
116  if ( sout[--iposRep] == '%' ) {
117  wOld = smat.size() + 1;
118  // Check for replacement with padded width.
119  } else if ( iposRep > 0 && sout[--iposRep] == '%' ) {
120  istringstream sswid(sout.substr(iposRep+1, 1));
121  wNew = 999;
122  sswid >> wNew;
123  if ( wNew == 0 ) wNew = m_wids[irep];
124  if ( wNew == 999 ) wNew = 0;
125  wOld = smat.size() + 2;
126  } else {
127  // If we get here we found "XXX%" but not the preceding "%'.
128  // Continue to the later part of the string.
129  ++ipos;
130  }
131  if ( wOld > 0 ) {
132  string sval; // replacement field
133  if ( isBad[irep] ) {
134  sval = m_bads[irep];
135  } else {
136  Index ival = vals[irep];
137  ostringstream ssval;
138  ssval << ival;
139  sval = ssval.str();
140  while ( wNew > sval.size() ) sval = "0" + sval;
141  }
142  sout.replace(iposRep, wOld, sval);
143  ipos = iposRep + sval.size();
144  }
145  }
146  }
147  // Next replace the signal unit strings.
148  StringManipulator sman(sout, false);
149  string sunit = acd.sampleUnit;
150  string sunitSpaced = sunit.size() ? " " + sunit : "";
151  string sunitWrapped = sunit.size() ? "(" + sunit + ")" : "";
152  string sunitSpWrapped = sunit.size() ? " " + sunitWrapped : "";
153  string sunitOptWrapped = sunit.find(" ") != string::npos ? sunitWrapped : "";
154  string sunitSpOptWrapped = sunitOptWrapped.size() ? " " + sunitOptWrapped : "";
155  string sunitBarred = sunit.size() ? "[" + sunit + "]" : "";
156  string sunitSpBarred = sunit.size() ? " " + sunitBarred : "";
157  sman.replace("%SUNIT%", sunit);
158  sman.replace("% SUNIT%", sunitSpaced);
159  sman.replace("%(SUNIT)%", sunitWrapped);
160  sman.replace("% (SUNIT)%", sunitSpWrapped);
161  sman.replace("%((SUNIT))%", sunitOptWrapped);
162  sman.replace("% ((SUNIT))%", sunitSpOptWrapped);
163  sman.replace("%[SUNIT]%", sunitBarred);
164  sman.replace("% [SUNIT]%", sunitSpBarred);
165  // Next replace the signal area unit strings.
166  string asunit = sunit + "-tick";
167  string::size_type ipos = sunit.find("/tick");
168  if ( ipos != string::npos ) {
169  asunit = sunit.substr(0, ipos) + sunit.substr(ipos + 5);
170  }
171  string asunitSpaced = asunit.size() ? " " + asunit : "";
172  string asunitWrapped = asunit.size() ? "(" + asunit + ")" : "";
173  string asunitSpWrapped = asunit.size() ? " " + asunitWrapped : "";
174  string asunitOptWrapped = asunit.find(" ") != string::npos ? asunitWrapped : "";
175  string asunitSpOptWrapped = asunitOptWrapped.size() ? " " + asunitOptWrapped : "";
176  string asunitBarred = asunit.size() ? "[" + asunit + "]" : "";
177  string asunitSpBarred = asunit.size() ? " " + asunitBarred : "";
178  sman.replace("%ASUNIT%", asunit);
179  sman.replace("% ASUNIT%", asunitSpaced);
180  sman.replace("%(ASUNIT)%", asunitWrapped);
181  sman.replace("% (ASUNIT)%", asunitSpWrapped);
182  sman.replace("%((ASUNIT))%", asunitOptWrapped);
183  sman.replace("% ((ASUNIT))%", asunitSpOptWrapped);
184  sman.replace("%[ASUNIT]%", asunitBarred);
185  sman.replace("% [ASUNIT]%", asunitSpBarred);
186  // Next replace trigger name.
187  if ( sout.find("%TRIGNAME") != string::npos ) {
188  Index ntrn = m_TrigNames.size();
189  Name strig = "undefined";
190  if ( ntrn ) {
191  Index itrn = itrig < ntrn ? itrig : ntrn - 1;
192  strig = m_TrigNames[itrn];
193  }
194  sman.replace("%TRIGNAME%", strig);
195  if ( strig.size() ) strig[0] = std::toupper(strig[0]);
196  sman.replace("%TRIGNAMECAP%", strig);
197  }
198  // Next replace time name.
199  string spatpre = "%UTCTIME";
200  ipos = sout.find(spatpre);
201  if ( ipos != string::npos ) {
202  time_t tim = acd.time();
203  int rem = acd.timerem();
204  int remMax = 1000000000;
205  if ( std::abs(rem) >= remMax ) rem = 0;
206  // Hndle rem < 0. Never happens?
207  if ( rem < 0 ) {
208  tim -= 1;
209  rem = remMax - rem;
210  }
211  TTimeStamp ts(tim, rem);
212  string stimpre = ts.AsString("s");
213  sman.replace(spatpre + "%", stimpre);
214  string srem = std::to_string(rem);
215  while ( srem.size() < 9 ) srem = "0" + srem;
216  for ( Index ndig=0; ndig<10; ++ndig ) {
217  string sdig = std::to_string(ndig);
218  string spat = spatpre + std::to_string(ndig) + "%";
219  string stim = stimpre;
220  if ( ndig ) stim += "." + srem.substr(0, ndig);
221  sman.replace(spat, stim);
222  }
223  }
224  if ( m_LogLevel >= 2 ) cout << myname << spat << " --> " << sout << endl;
225  return sout;
226 }
227 
228 //**********************************************************************
229 
#define DEFINE_ART_CLASS_TOOL(tool)
Definition: ToolMacros.h:42
AdcIndex subRun() const
std::string string
Definition: nybbler.cc:12
StandardAdcChannelStringTool(fhicl::ParameterSet const &ps)
int replace(std::string substr, const T &xsub)
bool haveInt(Name name) const
Definition: DataMap.h:207
AdcIndex trigger() const
time_t time() const
Index fembID() const
T abs(T value)
AdcIndex run() const
AdcIndex event() const
static constexpr double ps
Definition: Units.h:99
Q_EXPORT QTSManip setw(int w)
Definition: qtextstream.h:331
Channel channel() const
int timerem() const
int getInt(Name name, int def=0) const
Definition: DataMap.h:218
static Index badChannel()
std::string build(const AdcChannelData &acd, const DataMap &dm, std::string spat) const override
auto const & get(AssnsNode< L, R, D > const &r)
Definition: AssnsNode.h:115
static Index badIndex()
std::string to_string(ModuleType const mt)
Definition: ModuleType.h:34
QTextStream & endl(QTextStream &s)