All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ICARUSChannelMapProvider.cxx
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 /// \file icaruscode/Decode/ChannelMapping/ICARUSChannelMapProvider.cxx
3 /// \author T. Usher (factorized by Gianluca Petrillo, petrillo@slac.stanford.edu)
4 /// \see icaruscode/Decode/ChannelMapping/ICARUSChannelMapProvider.h
5 ////////////////////////////////////////////////////////////////////////
6 
7 // library header
9 
10 #include "art/Utilities/make_tool.h"
11 #include "messagefacility/MessageLogger/MessageLogger.h"
12 #include "fhiclcpp/ParameterSet.h"
13 #include "cetlib_except/exception.h"
14 #include "cetlib/cpu_timer.h"
15 
17 //#include "icaruscode/Decode/ChannelMapping/TPCChannelmapping.h"
19 
20 #include <string>
21 #include <iostream>
22 #include <cassert>
23 
24 namespace icarusDB
25 {
26 
27 
28 //----------------------------------------------------------------------
29 // Constructor.
30 ICARUSChannelMapProvider::ICARUSChannelMapProvider(const fhicl::ParameterSet& pset) {
31 
32  mf::LogInfo("ICARUSChannelMapProvider") << "Building the channel mapping" ;
33 
34  fDiagnosticOutput = pset.get<bool>("DiagnosticOutput", false);
35 
36  // Recover the vector of fhicl parameters for the ROI tools
37  const fhicl::ParameterSet&channelMappingParams = pset.get<fhicl::ParameterSet>("ChannelMappingTool");
38 
39  // Get instance of the mapping tool (allowing switch between database instances)
40  fChannelMappingTool = art::make_tool<IChannelMapping>(channelMappingParams);
41 
42  cet::cpu_timer theClockFragmentIDs;
43 
44  theClockFragmentIDs.start();
45 
46  if (fChannelMappingTool->BuildTPCFragmentIDToReadoutIDMap(fFragmentToReadoutMap))
47  {
48  throw cet::exception("ICARUSChannelMapProvider") << "Cannot recover the Fragment ID channel map from the database \n";
49  }
50  else if (fDiagnosticOutput)
51  {
52  std::cout << "FragmentID to Readout ID map has " << fFragmentToReadoutMap.size() << " elements";
53  for(const auto& pair : fFragmentToReadoutMap) std::cout << " Frag: " << std::hex << pair.first << ", Crate: "
54  << pair.second.first << ", # boards: " << std::dec << pair.second.second.size() << std::endl;
55 
56  }
57 
58  theClockFragmentIDs.stop();
59 
60  double fragmentIDsTime = theClockFragmentIDs.accumulated_real_time();
61 
62  cet::cpu_timer theClockReadoutIDs;
63 
64  theClockReadoutIDs.start();
65 
66  if (fChannelMappingTool->BuildTPCReadoutBoardToChannelMap(fReadoutBoardToChannelMap))
67  {
68  std::cout << "******* FAILED TO CONFIGURE CHANNEL MAP ********" << std::endl;
69  throw cet::exception("ICARUSChannelMapProvider") << "POS didn't read the F'ing database again \n";
70  }
71 
72  // Do the channel mapping initialization
73  if (fChannelMappingTool->BuildFragmentToDigitizerChannelMap(fFragmentToDigitizerMap))
74  {
75  throw cet::exception("ICARUSChannelMapProvider") << "Cannot recover the Fragment ID channel map from the database \n";
76  }
77  else if (fDiagnosticOutput)
78  {
79  std::cout << "FragmentID to Readout ID map has " << fFragmentToDigitizerMap.size() << " Fragment IDs";
80  for(const auto& pair : fFragmentToDigitizerMap) std::cout << " Frag: " << std::hex << pair.first << ", # pairs: "
81  << std::dec << pair.second.size() << std::endl;
82  }
83 
84  // Do the channel mapping initialization for CRT
85  if (fChannelMappingTool->BuildCRTChannelIDToHWtoSimMacAddressPairMap(fCRTChannelIDToHWtoSimMacAddressPairMap))
86  {
87  throw cet::exception("CRTDecoder") << "Cannot recover the HW MAC Address from the database \n";
88  }
89  else if (fDiagnosticOutput)
90  {
91  std::cout << "ChannelID to MacAddress map has " << fCRTChannelIDToHWtoSimMacAddressPairMap.size() << " Channel IDs";
92  for(const auto& pair : fCRTChannelIDToHWtoSimMacAddressPairMap) std::cout <<" ChannelID: "<< pair.first
93  << ", hw mac address: " << pair.second.first
94  <<", sim mac address: " << pair.second.second << std::endl;
95 
96  }
97 
98 
99  // Do the channel mapping initialization for top CRT
100  if (fChannelMappingTool->BuildTopCRTHWtoSimMacAddressPairMap(fTopCRTHWtoSimMacAddressPairMap))
101  {
102  throw cet::exception("CRTDecoder") << "Cannot recover the Top CRT HW MAC Address from the database \n";
103  }
104  else if (fDiagnosticOutput)
105  {
106  std::cout << "Top CRT MacAddress map has " << fTopCRTHWtoSimMacAddressPairMap.size() << " rows";
107  for(const auto& pair : fTopCRTHWtoSimMacAddressPairMap) std::cout << ", hw mac address: " << pair.first
108  <<", sim mac address: " << pair.second << std::endl;
109  }
110 
111 
112  // Do the CRT Charge Calibration initialization
113  if (fChannelMappingTool->BuildSideCRTCalibrationMap(fSideCRTChannelToCalibrationMap))
114  {
115  std::cout << "******* FAILED TO CONFIGURE CRT Calibration ********" << std::endl;
116  throw cet::exception("ICARUSChannelMapProvider") << "Cannot recover the charge calibration information from the database \n";
117  }
118  else if (fDiagnosticOutput)
119  {
120  std::cout << "side crt calibration map has " << fSideCRTChannelToCalibrationMap.size() << " list of rows \n";
121 
122  for(const auto& pair : fSideCRTChannelToCalibrationMap) std::cout <<" mac5: "<< pair.first.first
123  << ", chan: " << pair.first.second
124  << ", Gain: " << pair.second.first
125  << ", Pedestal: " << pair.second.second << std::endl;
126 
127  }
128 
129  theClockReadoutIDs.stop();
130 
131  double readoutIDsTime = theClockReadoutIDs.accumulated_real_time();
132 
133 
134  mf::LogInfo("ICARUSChannelMapProvider") << "==> FragmentID map time: " << fragmentIDsTime << ", Readout IDs time: " << readoutIDsTime << std::endl;
135 
136  return;
137 }
138 
139 bool ICARUSChannelMapProvider::hasFragmentID(const unsigned int fragmentID) const
140 {
141  return fFragmentToReadoutMap.find(fragmentID) != fFragmentToReadoutMap.end();
142 }
143 
144 
146  return fFragmentToReadoutMap.size();
147 }
148 
149 
150 const std::string& ICARUSChannelMapProvider::getCrateName(const unsigned int fragmentID) const
151 {
152  IChannelMapping::TPCFragmentIDToReadoutIDMap::const_iterator fragToReadoutItr = fFragmentToReadoutMap.find(fragmentID);
153 
154  if (fragToReadoutItr == fFragmentToReadoutMap.end())
155  throw cet::exception("ICARUSChannelMapProvider") << "Fragment ID " << fragmentID << " not found in lookup map when looking up crate name \n";
156 
157  return fragToReadoutItr->second.first;
158 }
159 
160 const ReadoutIDVec& ICARUSChannelMapProvider::getReadoutBoardVec(const unsigned int fragmentID) const
161 {
162  IChannelMapping::TPCFragmentIDToReadoutIDMap::const_iterator fragToReadoutItr = fFragmentToReadoutMap.find(fragmentID);
163 
164  if (fragToReadoutItr == fFragmentToReadoutMap.end())
165  throw cet::exception("ICARUSChannelMapProvider") << "Fragment ID " << fragmentID << " not found in lookup map when looking up board vector \n";
166 
167  return fragToReadoutItr->second.second;
168 
169 }
170 
172 {
174 }
175 
176 
177 bool ICARUSChannelMapProvider::hasBoardID(const unsigned int boardID) const
178 {
179  return fReadoutBoardToChannelMap.find(boardID) != fReadoutBoardToChannelMap.end();
180 }
181 
182 
184  return fReadoutBoardToChannelMap.size();
185 }
186 
187 
188 unsigned int ICARUSChannelMapProvider::getBoardSlot(const unsigned int boardID) const
189 {
190  IChannelMapping::TPCReadoutBoardToChannelMap::const_iterator readoutBoardItr = fReadoutBoardToChannelMap.find(boardID);
191 
192  if (readoutBoardItr == fReadoutBoardToChannelMap.end())
193  throw cet::exception("ICARUSChannelMapProvider") << "Board ID " << boardID << " not found in lookup map when looking up board slot \n";
194 
195  return readoutBoardItr->second.first;
196 }
197 
199 {
200  IChannelMapping::TPCReadoutBoardToChannelMap::const_iterator readoutBoardItr = fReadoutBoardToChannelMap.find(boardID);
201 
202  if (readoutBoardItr == fReadoutBoardToChannelMap.end())
203  throw cet::exception("ICARUSChannelMapProvider") << "Board ID " << boardID << " not found in lookup map when looking up channel/plane pair \n";
204 
205  return readoutBoardItr->second.second;
206 
207 }
208 
209 bool ICARUSChannelMapProvider::hasPMTDigitizerID(const unsigned int fragmentID) const
210 {
211  return findPMTfragmentEntry(fragmentID) != nullptr;
212 }
213 
214 
216  return fFragmentToDigitizerMap.size();
217 }
218 
219 
221 {
222  DigitizerChannelChannelIDPairVec const* digitizerPair = findPMTfragmentEntry(fragmentID);
223 
224  if (digitizerPair) return *digitizerPair;
225  throw cet::exception("ICARUSChannelMapProvider") << "Fragment ID " << fragmentID << " not found in lookup map when looking for PMT channel info \n";
226 
227 }
228 
229  unsigned int ICARUSChannelMapProvider::getSimMacAddress(const unsigned int hwmacaddress) const
230  {
231  unsigned int simmacaddress = 0;
232 
233  for(const auto& pair : fCRTChannelIDToHWtoSimMacAddressPairMap){
234  if (pair.second.first == hwmacaddress)
235  simmacaddress = pair.second.second;
236  }
237 
238  return simmacaddress;
239  }
240 
241  unsigned int ICARUSChannelMapProvider::gettopSimMacAddress(const unsigned int hwmacaddress) const
242  {
243  unsigned int simmacaddress = 0;
244 
245  for(const auto& pair : fTopCRTHWtoSimMacAddressPairMap){
246  if (pair.first == hwmacaddress)
247  simmacaddress = pair.second;
248  }
249 
250  return simmacaddress;
251 
252  /* untested:
253  auto const it = fTopCRTHWtoSimMacAddressPairMap.find(hwmacaddress);
254  return (it == fTopCRTHWtoSimMacAddressPairMap.end())? 0: it->second;
255  */
256  }
257 
258  std::pair<double, double> ICARUSChannelMapProvider::getSideCRTCalibrationMap(int mac5, int chan) const
259  {
260  auto const itGainAndPedestal = fSideCRTChannelToCalibrationMap.find({ mac5, chan });
261  return (itGainAndPedestal == fSideCRTChannelToCalibrationMap.cend())
262  ? std::pair{ -99., -99. }: itGainAndPedestal->second;
263  }
264 
265 auto ICARUSChannelMapProvider::findPMTfragmentEntry(unsigned int fragmentID) const
267 {
268  auto it = fFragmentToDigitizerMap.find(PMTfragmentIDtoDBkey(fragmentID));
269  return (it == fFragmentToDigitizerMap.end())? nullptr: &(it->second);
270 }
271 
272 
274  (unsigned int fragmentID)
275 {
276  /*
277  * PMT channel mapping database stores the board number (0-23) as key.
278  * Fragment ID are currently in the pattern 0x20xx, with xx the board number.
279  */
280 
281  // protest if this is a fragment not from the PMT;
282  // but make an exception for old PMT fragment IDs (legacy)
283  assert(((fragmentID & ~0xFF) == 0x00) || ((fragmentID & ~0xFF) == 0x20));
284 
285  return fragmentID & 0xFF;
286 
287 } // ICARUSChannelMapProvider::PMTfragmentIDtoDBkey()
288 
289 
291  (unsigned int DBkey)
292 {
293  /*
294  * PMT channel mapping database stores the board number (0-23) as key.
295  * Fragment ID are currently in the pattern 0x20xx, with xx the board number.
296  */
297 
298  // protest if this is a fragment not from the PMT;
299  // but make an exception for old PMT fragment IDs (legacy)
300  assert((DBkey & 0xFF) < 24);
301 
302  return (DBkey & 0xFF) | 0x2000;
303 
304 } // ICARUSChannelMapProvider::PMTfragmentIDtoDBkey()
305 
306 
307 } // end namespace
308 
bool hasBoardID(const unsigned int) const override
std::vector< ChannelPlanePair > ChannelPlanePairVec
std::vector< DigitizerChannelChannelIDPair > DigitizerChannelChannelIDPairVec
std::vector< unsigned int > ReadoutIDVec
const ChannelPlanePairVec & getChannelPlanePair(const unsigned int) const override
static constexpr unsigned int DBkeyToPMTfragmentID(unsigned int DBkey)
Returns the PMT fragment ID for the specified channel mapping database key.
This provides an art tool interface definition for tools handle the channel mapping The idea is to be...
unsigned int nTPCboardIDs() const override
Returns the number of TPC board IDs known to the service.
IChannelMapping::SideCRTChannelToCalibrationMap fSideCRTChannelToCalibrationMap
IChannelMapping::TopCRTHWtoSimMacAddressPairMap fTopCRTHWtoSimMacAddressPairMap
DigitizerChannelChannelIDPairVec const * findPMTfragmentEntry(unsigned int fragmentID) const
const ReadoutIDVec & getReadoutBoardVec(const unsigned int) const override
std::pair< double, double > getSideCRTCalibrationMap(int mac5, int chan) const override
Returns the Gain and Pedestal for Side CRT.
const std::string & getCrateName(const unsigned int) const override
IChannelMapping::TPCFragmentIDToReadoutIDMap fFragmentToReadoutMap
unsigned int nPMTfragmentIDs() const override
Returns the number of PMT fragment IDs known to the service.
ICARUSChannelMapProvider(const fhicl::ParameterSet &pset)
unsigned int nTPCfragmentIDs() const override
Returns the number of TPC fragment IDs known to the service.
const TPCReadoutBoardToChannelMap & getReadoutBoardToChannelMap() const override
const DigitizerChannelChannelIDPairVec & getChannelIDPairVec(const unsigned int) const override
bool hasFragmentID(const unsigned int) const override
std::map< unsigned int, SlotChannelVecPair > TPCReadoutBoardToChannelMap
static constexpr unsigned int PMTfragmentIDtoDBkey(unsigned int fragmentID)
Returns the channel mapping database key for the specified PMT fragment ID.
unsigned int getSimMacAddress(const unsigned int) const override
std::unique_ptr< IChannelMapping > fChannelMappingTool
unsigned int getBoardSlot(const unsigned int) const override
IChannelMapping::CRTChannelIDToHWtoSimMacAddressPairMap fCRTChannelIDToHWtoSimMacAddressPairMap
IChannelMapping::FragmentToDigitizerChannelMap fFragmentToDigitizerMap
bool hasPMTDigitizerID(const unsigned int) const override
BEGIN_PROLOG could also be cout
unsigned int gettopSimMacAddress(const unsigned int) const override
IChannelMapping::TPCReadoutBoardToChannelMap fReadoutBoardToChannelMap