TimingRawDecoderOffsetTool_tool.cc
Go to the documentation of this file.
1 // TimingRawDecoderOffsetTool_tool.cc
2 
8 #include <iostream>
9 #include <fstream>
10 
11 using std::cout;
12 using std::endl;
13 using std::string;
14 using std::ifstream;
15 
17 
18 //**********************************************************************
19 
21 : m_LogLevel(ps.get<Index>("LogLevel")),
22  m_TpcTickPhase(ps.get<Index>("TpcTickPhase")),
23  m_Unit(ps.get<Name>("Unit")),
24  m_FembScaleIds(ps.get<IndexVector>("FembScaleIds")),
25  m_FembScaleValues(ps.get<DoubleVector>("FembScaleValues")),
26  m_RunDataTool(ps.get<Name>("RunDataTool")) {
27  const Name myname = "TimingRawDecoderOffsetTool::ctor: ";
28  if ( m_RunDataTool.size() ) {
31  if ( m_pRunDataTool == nullptr ) {
32  cout << myname << "WARNING: RunDataTool not found: " << m_RunDataTool << endl;
33  }
34  }
35  Index nfmb = std::min(m_FembScaleIds.size(), m_FembScaleValues.size());
36  for ( Index kfmb=0; kfmb<nfmb; ++kfmb ) {
38  }
39  // If needed, pad FEMB scale values.
40  if ( m_FembScaleValues.size() < m_FembScaleIds.size() ) {
41  cout << myname << "WARNING: Missing scale values have been set to one." << endl;
42  }
43  if ( m_FembScaleValues.size() > m_FembScaleIds.size() ) {
44  cout << myname << "WARNING: Extra scale values will be ignored." << endl;
45  }
46  if ( m_LogLevel ) {
47  cout << myname << "Configuration:" << endl;
48  cout << myname << " LogLevel: " << m_LogLevel << endl;
49  cout << myname << " TpcTickPhase: " << m_TpcTickPhase << endl;
50  cout << myname << " Unit: " << m_Unit << endl;
51  cout << myname << " FembScales: [";
52  Index nfmb = 0;
53  for ( ScaleMap::value_type isca : m_fembScales ) {
54  if ( nfmb ) cout << ", ";
55  cout << isca.first << ":" << isca.second;
56  ++nfmb;
57  }
58  cout << "]" << endl;
59  cout << myname << " RunDataTool: " << m_RunDataTool << " @ " << m_pRunDataTool << endl;
60  }
61 }
62 
63 //**********************************************************************
64 
66  const Name myname = "TimingRawDecoderOffsetTool::offset: ";
67  Name ifname = "artdaqTimestamp-Run" + std::to_string(dat.run) +
68  "-Event" + std::to_string(dat.event) + ".dat";
69  Offset res;
70  unsigned long daqVal = dat.triggerClock;
71  static Index checkCount = 0;
73  bool haveScale = isca != m_fembScales.end();
74  double scale = haveScale ? isca->second : 1.0;
75  if ( checkCount ) {
76  ifstream fin(ifname.c_str());
77  if ( ! fin ) {
78  cout << myname << "Unable to find time offset file: " << ifname << endl;
79  //return res.setStatus(1);
80  }
81  unsigned long daqValFile = 0;
82  fin >> daqValFile;
83  if ( daqVal == daqValFile ) {
84  cout << myname << "Input clock matches file clock." << endl;
85  } else {
86  cout << myname << "Input clock does not match file: " << daqVal << " != " << daqValFile << endl;
87  }
88  --checkCount;
89  }
90  if ( m_Unit == "daq" ) {
91  res.value = scale*daqVal;
92  res.rem = 0.0;
93  } else if ( m_Unit == "ns" ) {
94  res.value = scale*20*daqVal;
95  res.rem = 0.0;
96  } else if ( m_Unit == "tick" ) {
97  Index runPhase = 0;
98  if ( m_pRunDataTool != nullptr ) {
99  Index icha = dat.channel;
100  RunData rdat = m_pRunDataTool->runData(dat.run, dat.subrun);
101  if ( rdat.havePhaseGroup() ) {
102  bool haveGroup = true;
103  Index igrp = 0;
105  bool useWib = false;
106  bool useFemb = false;
107  if ( rdat.phaseGroup() == "channel" ) igrp = icha;
108  else if ( rdat.phaseGroup() == "all" ) igrp = 0;
109  else if ( rdat.phaseGroup() == "wib" ) useWib = true;
110  else if ( rdat.phaseGroup() == "femb" ) useFemb = true;
111  else {
112  haveGroup = false;
113  cout << myname << "WARNING: Invalid phase group: " << rdat.phaseGroup() << endl;
114  }
115  if ( haveGroup ) {
117  if ( pchanMap.get() != nullptr ) {
118  Index kapa = pchanMap->APAFromOfflineChannel(icha);
119  Index jwib = pchanMap->WIBFromOfflineChannel(icha);
120  Index jcon = pchanMap->FEMBFromOfflineChannel(icha);
121  Index jfmb = 5*jcon + jwib;
122  Index kfmb = 20*kapa + jfmb;
123  Index kwib = 5*kapa + jfmb%5;
124  if ( useWib ) igrp = kwib;
125  if ( useFemb ) igrp = kfmb;
126  } else {
127  haveGroup = false;
128  cout << myname << "WARNING: Channel map service not found." << endl;
129  }
130  }
131  if ( haveGroup ) {
132  const RunData::IndexVector& phases = rdat.phases();
133  if ( igrp + 1 > phases.size() ) {
134  cout << myname << "WARNING: Phases is too short: " << igrp << "/" << phases.size() << endl;
135  } else {
136  runPhase = phases[igrp];
137  }
138  }
139  }
140  if ( m_LogLevel >= 3 ) cout << myname << "Run phase is " << runPhase << endl;
141  } else {
142  if ( m_LogLevel >= 3 ) cout << myname << "Run data tool not found." << endl;
143  }
144  long daqoff = daqVal + m_TpcTickPhase + runPhase;
145  if ( haveScale ) {
146  res.value = scale*daqoff/25;
147  } else {
148  res.value = daqoff/25;
149  }
150  res.rem = (daqoff % 25)/25.0;
151  } else {
152  cout << myname << "Invalid unit: " << m_Unit << ifname << endl;
153  return res.setStatus(2);
154  }
155  res.unit = m_Unit;
156  if ( m_LogLevel >= 2 ) cout << myname << "Offset is " << res.value
157  << " " << res.unit << endl;
158  return res;
159 }
160 
161 //**********************************************************************
162 
Offset offset(const Data &dat) const override
#define DEFINE_ART_CLASS_TOOL(tool)
Definition: ToolMacros.h:42
const IndexVector & phases() const
Definition: RunData.h:45
T * get() const
Definition: ServiceHandle.h:63
Name phaseGroup() const
Definition: RunData.h:44
std::string string
Definition: nybbler.cc:12
const Offset & setStatus(int val)
intermediate_table::const_iterator const_iterator
TimingRawDecoderOffsetTool::Offset Offset
unsigned int APAFromOfflineChannel(unsigned int offlineChannel) const
Returns APA/crate.
unsigned int Index
TimingRawDecoderOffsetTool(fhicl::ParameterSet const &ps)
bool havePhaseGroup() const
Definition: RunData.h:60
void checkCount(const ToolPtr &ptool, double vin, Count countExpected)
std::vector< Index > IndexVector
Definition: RunData.h:27
virtual RunData runData(Index run, Index subRun=0) const =0
static constexpr double ps
Definition: Units.h:99
unsigned int WIBFromOfflineChannel(unsigned int offlineChannel) const
Returns WIB/slot.
T min(sqlite3 *const db, std::string const &table_name, std::string const &column_name)
Definition: statistics.h:55
unsigned int FEMBFromOfflineChannel(unsigned int offlineChannel) const
Returns FEMB/fiber.
auto const & get(AssnsNode< L, R, D > const &r)
Definition: AssnsNode.h:115
std::string to_string(ModuleType const mt)
Definition: ModuleType.h:34
static DuneToolManager * instance(std::string fclname="", int dbg=1)
T * getShared(std::string name)
QTextStream & endl(QTextStream &s)