AdcToRoi2d_tool.cc
Go to the documentation of this file.
1 // AdcToRoi2d_tool.cc
2 
3 #include "AdcToRoi2d.h"
4 
5 using std::string;
6 using std::cout;
7 using std::endl;
8 using std::vector;
10 
12 using LongIndex = unsigned long int;
13 
14 namespace {
15 
16 string optionString(Index iopt) {
17  if ( iopt == 0 ) return "No copy";
18  if ( iopt == 1 ) return "One ROI/map";
19  if ( iopt == 2 ) return "One ROI for all maps";
20  return "Invalid option";
21 }
22 
23 } // end unnamed namespace
24 
25 //**********************************************************************
26 // Class methods.
27 //**********************************************************************
28 
30 : m_LogLevel(ps.get<int>("LogLevel")),
31  m_Option(ps.get<Index>("Option")),
32  m_InputAdcMaps(ps.get<IndexVector>("InputAdcMaps")),
33  m_OutputNames(ps.get<NameVector>("OutputNames")) {
34  const string myname = "AdcToRoi2d::ctor: ";
35  if ( m_Option > 2 ) {
36  cout << myname << "WARNING: Invalid option: " << m_Option
37  << " will be treated as 0." << endl;
38  }
39  // Display the configuration.
40  if ( m_LogLevel>= 1 ) {
41  cout << myname << " LogLevel: " << m_LogLevel << endl;
42  cout << myname << " Option: " << m_Option
43  << " (" << optionString(m_Option) << ")" << endl;
44  cout << myname << " OutputNames: [";
45  bool first = true;
46  for ( Name nam : m_OutputNames ) {
47  if ( first ) first = false;
48  else cout << ", ";
49  cout << nam;
50  }
51  cout << "]" << endl;
52  }
53 }
54 
55 //**********************************************************************
56 
58 
59 //**********************************************************************
60 
62  const string myname = "AdcToRoi2d::updateTpcData: ";
63  DataMap ret;
64  Index imax = -1;
65  LongIndex lmax = -1;
66  // Find the channel and tick ranges for each ADC map.
67  Index nacm = tpd.getAdcData().size();
68  vector<LongIndex> itck1s(nacm, lmax);
69  vector<LongIndex> itck2s(nacm, 0);
70  vector<Index> icha1s(nacm, imax);
71  vector<Index> icha2s(nacm, 0);
72  vector<Index> ndats(nacm, 0);
73  Index nerr = 0;
74  if ( m_LogLevel >= 3 ) cout << myname << "Channel map count: " << nacm << endl;
75  IndexVector mapIndices = m_InputAdcMaps;
76  if ( mapIndices.size() == 0 ) {
77  for ( Index iacm=0; iacm<nacm; ++iacm ) mapIndices.push_back(iacm);
78  }
79  for ( Index iacm : mapIndices ) {
80  if ( iacm >= tpd.getAdcData().size() ) {
81  cout << myname << "WARNING: Skipping invalid ADC map index " << iacm << endl;
82  continue;
83  }
84  LongIndex ndat = 0;
85  const TpcData::AdcDataPtr pacm = tpd.getAdcData().at(iacm);
86  if ( ! pacm ) {
87  if ( m_LogLevel >=2 ) cout << myname << "Skipping missing ADC channel map." << endl;
88  ++nerr;
89  continue;
90  }
91  if ( pacm->size() == 0 ) {
92  if ( m_LogLevel >=2 ) cout << myname << "Skipping empty ADC channel map." << endl;
93  ++nerr;
94  continue;
95  }
96  if ( m_LogLevel >= 3 ) cout << myname << " Channel count: " << pacm->size() << endl;
97  for ( const auto& iacd : *pacm ) {
98  const AdcChannelData& acd = iacd.second;
99  Index nsam = acd.samples.size();
100  Index icha1 = acd.channel();
101  Index icha2 = icha1 + 1;
102  Index itck1 = acd.tickOffset();
103  Index itck2 = itck1 + nsam;
104  if ( icha1 < icha1s[iacm] ) icha1s[iacm] = icha1;
105  if ( icha2 > icha2s[iacm] ) icha2s[iacm] = icha2;
106  if ( itck1 < itck1s[iacm] ) itck1s[iacm] = itck1;
107  if ( itck2 > itck2s[iacm] ) itck2s[iacm] = itck2;
108  if ( m_LogLevel >= 3 ) cout << myname << " Channel " << acd.channel()
109  << " sample count: " << nsam << endl;
110  ndat += nsam;
111  }
112  if ( icha1s[iacm] >= icha2s[iacm] ) {
113  cout << myname << "ERROR: Channels out of range. Skipping ADC map " << iacm << "." << endl;
114  ++nerr;
115  continue;
116  }
117  if ( ndat == 0 ) {
118  cout << myname << "ERROR: No samples found. Skipping ADC map " << iacm << "." << endl;
119  ++nerr;
120  continue;
121  }
122  if ( itck1s[iacm] >= itck2s[iacm] ) {
123  cout << myname << "ERROR: Ticks out of range. Skipping ADC map " << iacm << "." << endl;
124  ++nerr;
125  continue;
126  }
127  ndats[iacm] = ndat;
128  LongIndex ncha = icha2s[iacm] - icha1s[iacm];
129  LongIndex ntck = itck2s[iacm] - itck1s[iacm];
130  LongIndex ncht = ncha*ntck;
131  double frac = double(ndat)/double(ncht);
132  if ( m_LogLevel >= 2 ) {
133  cout << myname << " ADC map is " << ncha << " x " << ntck
134  << " with fill fraction " << frac << endl;
135  cout << myname << " Channel range: [ " << icha1s[iacm] << ", " << icha2s[iacm] << ")" << endl;
136  cout << myname << " Tick range: [ " << itck1s[iacm] << ", " << itck2s[iacm] << ")" << endl;
137  }
138  }
139  Index nroi = 0;
140  vector<int> roiNdats;
141  if ( m_Option == 1 ) {
142  for ( Index iacm=0; iacm<nacm; ++iacm ) {
143  Index icha1 = icha1s[iacm];
144  Index icha2 = icha2s[iacm];
145  LongIndex itck1 = itck1s[iacm];
146  LongIndex itck2 = itck2s[iacm];
147  if ( icha1 >= icha2 ) continue;
148  if ( itck1 >= itck2 ) continue;
149  Index ncha = icha2 - icha1;
150  Index ntck = Index(itck2 - itck1);
151  // Create and fill ROI.
152  tpd.get2dRois().emplace_back(ncha, ntck, icha1, itck1);
153  ++nroi;
154  TpcData* ptpdo = &tpd;
155  if ( m_OutputNames.size() ) {
156  if ( iacm >= m_OutputNames.size() ) {
157  cout << myname << "WARNING: No outpu name supplied for ADC map " << iacm << endl;
158  } else {
159  Name nam = m_OutputNames[iacm];
160  ptpdo = tpd.getTpcData(nam);
161  if ( ptpdo == nullptr ) {
162  ptpdo = tpd.addTpcData(nam);
163  if ( ptpdo == nullptr ) {
164  cout << myname << "ERROR: Unable to add TpcData directory " << nam << endl;
165  continue;
166  } else if ( m_LogLevel >= 2 ) {
167  cout << myname << "Added TpcData directory " << nam << endl;
168  }
169  } else if ( m_LogLevel >= 2 ) {
170  cout << myname << "Using existing TpcData directory " << nam << endl;
171  }
172  }
173  }
174  Tpc2dRoi& roi = ptpdo->get2dRois().back();
175  const TpcData::AdcDataPtr pacm = tpd.getAdcData().at(iacm);
177  Index ndat = 0;
178  for ( const auto& iacd : *pacm ) {
179  const AdcChannelData& acd = iacd.second;
180  idxs[0] = acd.channel() - icha1;
181  idxs[1] = acd.tickOffset() - itck1;
182  for ( Index isam=0; isam<acd.samples.size(); ++isam, ++idxs[1], ++ndat ) {
183  roi.data().setValue(idxs, acd.samples[isam]);
184  }
185  }
186  roiNdats.push_back(ndat);
187  if ( ndat != ndats[iacm] ) {
188  cout << myname << "ERROR: Unexpected fill count: " << ndat << " != " << ndats[iacm] << endl;
189  ++nerr;
190  }
191  }
192  } else if ( m_Option == 2 ) {
193  cout << myname << "ERROR: Merging of ADC data is not yet supported." << endl;
194  }
195  ret.setStatus(nerr);
196  ret.setInt("a2r_nroi", nroi);
197  ret.setIntVector("a2r_nsams", roiNdats);
198  return ret;
199 }
200 
201 //**********************************************************************
202 
unsigned long int LongIndex
#define DEFINE_ART_CLASS_TOOL(tool)
Definition: ToolMacros.h:42
int m_LogLevel
Definition: AdcToRoi2d.h:53
DataMap updateTpcData(TpcData &tpd) const override
unsigned int Index
DataMap & setStatus(int stat)
Definition: DataMap.h:130
std::string string
Definition: nybbler.cc:12
struct vector vector
std::shared_ptr< AdcChannelDataMap > AdcDataPtr
Definition: TpcData.h:35
Index m_Option
Definition: AdcToRoi2d.h:54
TpcData * addTpcData(Name nam, bool copyAdcData=true)
Definition: TpcData.cxx:24
NameVector m_OutputNames
Definition: AdcToRoi2d.h:56
unsigned int Index
void setIntVector(Name name, const IntVector &val)
Definition: DataMap.h:132
AdcDataVector & getAdcData()
Definition: TpcData.h:55
std::vector< Name > NameVector
Definition: AdcToRoi2d.h:41
std::array< Index, 2 > IndexArray
Definition: Real2dData.h:34
std::string Name
Definition: AdcToRoi2d.h:40
~AdcToRoi2d() override
AdcLongIndex tickOffset() const
void setInt(Name name, int val)
Definition: DataMap.h:131
static constexpr double ps
Definition: Units.h:99
AdcToRoi2d(fhicl::ParameterSet const &ps)
unsigned int Index
Definition: AdcToRoi2d.h:38
Channel channel() const
TpcData * getTpcData(Name nam)
Definition: TpcData.cxx:42
int imax
Definition: tracks.py:195
const DataArray & data() const
Definition: Tpc2dRoi.h:50
std::vector< Index > IndexVector
Definition: AdcToRoi2d.h:39
auto const & get(AssnsNode< L, R, D > const &r)
Definition: AssnsNode.h:115
IndexVector m_InputAdcMaps
Definition: AdcToRoi2d.h:55
Index setValue(const IndexArray &isams, Float val, Index *pchk=nullptr)
Definition: Real2dData.h:197
Tpc2dRoiVector & get2dRois()
Definition: TpcData.h:57
AdcSignalVector samples
QTextStream & endl(QTextStream &s)