All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ChannelMapSQLite_tool.cc
Go to the documentation of this file.
1 /**
2  * @file ChannelMapSQLite_tool.cc
3  *
4  * @brief This tool converts from daq to LArSoft format with noise filtering
5  *
6  */
7 
8 // Framework Includes
9 #include "art/Framework/Core/EDProducer.h"
10 #include "art/Framework/Services/Registry/ServiceHandle.h"
11 #include "art/Persistency/Common/PtrMaker.h"
12 #include "art/Utilities/ToolMacros.h"
13 #include "art/Utilities/make_tool.h"
14 #include "cetlib/cpu_timer.h"
15 #include "fhiclcpp/ParameterSet.h"
16 #include "messagefacility/MessageLogger/MessageLogger.h"
17 #include "wda.h"
18 
19 // LArSoft includes
22 
23 // std includes
24 #include <string>
25 #include <iostream>
26 #include <memory>
27 #include <sqlite3.h>
28 #include <time.h>
29 
30 //------------------------------------------------------------------------------------------------------------------------------------------
31 // implementation follows
32 
33 namespace icarusDB {
34 /**
35  * @brief ChannelMapSQLite class definiton
36  */
37 class ChannelMapSQLite : virtual public IChannelMapping
38 {
39 public:
40  /**
41  * @brief Constructor
42  *
43  * @param pset
44  */
45  explicit ChannelMapSQLite(fhicl::ParameterSet const &pset);
46 
47  /**
48  * @brief Destructor
49  */
51 
52  /**
53  * @brief Define the returned data structures for a mapping between TPC Fragment IDs
54  * and the related crate and readout information.
55  * Then define the function interface to fill these data structures
56  */
58 
59  /**
60  * @brief Define the returned data structures for a mapping between TPC readout boards
61  * and the channel information
62  * Then define the function interface to fill these data structures
63  */
65 
66  /**
67  * @brief Define the returned data structures for a mapping between PMT Fragment IDs
68  * and the related crate and readout information.
69  * Then define the function interface to fill these data structures
70  */
72 
73 
74  /**
75  * @brief Define the returned data structures for a mapping between CRT hardware mac_address
76  * to the simulated mac_address.
77  * Then define the function interface to fill these data structures
78  */
81 
82  virtual int BuildSideCRTCalibrationMap(SideCRTChannelToCalibrationMap&) const override;
83 
84 private:
85  // Recover data from postgres database
86  int GetDataset(const std::string&, int func(void*,int,char**,char**), void*) const;
87 
88 
89  std::string fDBFileName; //< File name of our sqlite database
90  std::string fCalibDBFileName; //< File name of our side crt calibration sqlite database
91  std::string fTag; //< Tag for conditioned database
92 };
93 
94 ChannelMapSQLite::ChannelMapSQLite(fhicl::ParameterSet const &pset)
95 {
96  fDBFileName = pset.get<std::string>("DBFileName");
97  fCalibDBFileName = pset.get<std::string>("CalibDBFileName");
98  fTag = pset.get<std::string>("Tag");
99 
100  return;
101 }
102 
103 //------------------------------------------------------------------------------------------------------------------------------------------
104 
106 {
107 }
108 
109 //------------------------------------------------------------------------------------------------------------------------------------------
110 
111 int callback(void *data, int argc, char **argv, char **azColName)
112 {
113  int i;
114 
115  for(i = 0; i<argc; i++){
116  std::cout << "column: " << azColName[i] << "- value: " << (argv[i] ? argv[i] : "NULL") << " ";
117  }
118  std::cout << std::endl;
119 
120  std::cout << "\n" << std::endl;
121  return 0;
122 }
123 
124 // -----------------------------------------------------
125 // This Function does the basic information retrieval
126 // One passes in the type of data requested and a reference to the data holder
127 // The function connects via the libwda function to recover the
128 // data and checks to insure there were not connection errors.
129 // The function returns the error status, if null then success
130 //-----------------------------------------------------
131  int ChannelMapSQLite::GetDataset(const std::string& table, int func(void*,int,char**,char**), void* data) const
132  {
133  std::string fullFileName;
134  cet::search_path searchPath("FW_SEARCH_PATH");
135 
136  if (!searchPath.find_file(fDBFileName, fullFileName))
137  throw cet::exception("ChannelMapSQLite::GetDataset") << "Can't find input file: '" << fDBFileName << "'\n";
138 
139  // Set up to open the database
140  sqlite3* database;
141 
142  int rc = sqlite3_open(fullFileName.c_str(), &database);
143 
144  if (rc)
145  throw cet::exception("ChannelMapSQLite::GetDataset") << "Can't open the database, return code:" << sqlite3_errmsg(database) << "'\n";
146 
147  // Test reading the database
148  std::string select = "SELECT * FROM " + table;
149 
150  char* zErrMsg = 0;
151 
152  rc = sqlite3_exec(database, select.c_str(), func, data, &zErrMsg);
153 
154  if( rc != SQLITE_OK )
155  {
156  std::cout << "ChannelMapSQLite::GetDataset: SQL error: " << zErrMsg << std::endl;
157  sqlite3_free(zErrMsg);
158  }
159  else
160  {
161  std::cout << "ChannelMapSQLite::GetDataset: Successfully read database" << std::endl;
162  }
163 
164  sqlite3_close(database);
165 
166  return 0;
167  }
168 
169 // -----------------------------------------------------
170 // The aim of this function is to build a map between the
171 // TPC Fragment IDs and the readout board IDs. Here we
172 // expect there will be some number of boards per Fragment
173 //-----------------------------------------------------
174 int buildTPCFragmentIDToReadoutIDMap_callback(void* dataIn, int argc, char**argv, char** azColName)
175 {
176  const unsigned int tpcIdentifier(0x00001000);
177 
179 
180  // Include a by hand mapping of fragement ID to crate
181  // Note that we now know we can get this from the "flanges" table... so an upgrade coming soon...
182  using FlangeIDToCrateMap = std::map<size_t,std::string>;
183  FlangeIDToCrateMap flangeIDToCrateMap;
184  flangeIDToCrateMap[19] = "WW01T";
185  flangeIDToCrateMap[68] = "WW01M";
186  flangeIDToCrateMap[41] = "WW01B";
187  flangeIDToCrateMap[11] = "WW02";
188  flangeIDToCrateMap[17] = "WW03";
189  flangeIDToCrateMap[36] = "WW04";
190  flangeIDToCrateMap[18] = "WW05";
191  flangeIDToCrateMap[58] = "WW06";
192  flangeIDToCrateMap[71] = "WW07";
193  flangeIDToCrateMap[14] = "WW08";
194  flangeIDToCrateMap[25] = "WW09";
195  flangeIDToCrateMap[34] = "WW10";
196  flangeIDToCrateMap[67] = "WW11";
197  flangeIDToCrateMap[33] = "WW12";
198  flangeIDToCrateMap[87] = "WW13";
199  flangeIDToCrateMap[10] = "WW14";
200  flangeIDToCrateMap[59] = "WW15";
201  flangeIDToCrateMap[95] = "WW16";
202  flangeIDToCrateMap[22] = "WW17";
203  flangeIDToCrateMap[91] = "WW18";
204  flangeIDToCrateMap[61] = "WW19";
205  flangeIDToCrateMap[55] = "WW20T";
206  flangeIDToCrateMap[97] = "WW20M";
207  flangeIDToCrateMap[100] = "WW20B";
208  flangeIDToCrateMap[83] = "WE01T";
209  flangeIDToCrateMap[85] = "WE01M";
210  flangeIDToCrateMap[7] = "WE01B";
211  flangeIDToCrateMap[80] = "WE02";
212  flangeIDToCrateMap[52] = "WE03";
213  flangeIDToCrateMap[32] = "WE04";
214  flangeIDToCrateMap[70] = "WE05";
215  flangeIDToCrateMap[74] = "WE06";
216  flangeIDToCrateMap[46] = "WE07";
217  flangeIDToCrateMap[81] = "WE08";
218  flangeIDToCrateMap[63] = "WE09";
219  flangeIDToCrateMap[30] = "WE10";
220  flangeIDToCrateMap[51] = "WE11";
221  flangeIDToCrateMap[90] = "WE12";
222  flangeIDToCrateMap[23] = "WE13";
223  flangeIDToCrateMap[93] = "WE14";
224  flangeIDToCrateMap[92] = "WE15";
225  flangeIDToCrateMap[88] = "WE16";
226  flangeIDToCrateMap[73] = "WE17";
227  flangeIDToCrateMap[1] = "WE18";
228  flangeIDToCrateMap[66] = "WE19";
229  flangeIDToCrateMap[48] = "WE20T";
230  flangeIDToCrateMap[13] = "WE20M";
231  flangeIDToCrateMap[56] = "WE20B";
232  flangeIDToCrateMap[94] = "EW01T";
233  flangeIDToCrateMap[77] = "EW01M";
234  flangeIDToCrateMap[72] = "EW01B";
235  flangeIDToCrateMap[65] = "EW02";
236  flangeIDToCrateMap[4] = "EW03";
237  flangeIDToCrateMap[89] = "EW04";
238  flangeIDToCrateMap[37] = "EW05";
239  flangeIDToCrateMap[76] = "EW06";
240  flangeIDToCrateMap[49] = "EW07";
241  flangeIDToCrateMap[60] = "EW08";
242  flangeIDToCrateMap[21] = "EW09";
243  flangeIDToCrateMap[6] = "EW10";
244  flangeIDToCrateMap[62] = "EW11";
245  flangeIDToCrateMap[2] = "EW12";
246  flangeIDToCrateMap[29] = "EW13";
247  flangeIDToCrateMap[44] = "EW14";
248  flangeIDToCrateMap[9] = "EW15";
249  flangeIDToCrateMap[31] = "EW16";
250  flangeIDToCrateMap[98] = "EW17";
251  flangeIDToCrateMap[38] = "EW18";
252  flangeIDToCrateMap[99] = "EW19";
253  flangeIDToCrateMap[53] = "EW20T";
254  flangeIDToCrateMap[82] = "EW20M";
255  flangeIDToCrateMap[35] = "EW20B";
256  flangeIDToCrateMap[96] = "EE01T";
257  flangeIDToCrateMap[28] = "EE01M";
258  flangeIDToCrateMap[16] = "EE01T";
259  flangeIDToCrateMap[69] = "EE02";
260  flangeIDToCrateMap[20] = "EE02";
261  flangeIDToCrateMap[79] = "EE02";
262  flangeIDToCrateMap[50] = "EE02";
263  flangeIDToCrateMap[45] = "EE02";
264  flangeIDToCrateMap[84] = "EE02";
265  flangeIDToCrateMap[42] = "EE02";
266  flangeIDToCrateMap[39] = "EE02";
267  flangeIDToCrateMap[26] = "EE02";
268  flangeIDToCrateMap[64] = "EE02";
269  flangeIDToCrateMap[43] = "EE02";
270  flangeIDToCrateMap[47] = "EE02";
271  flangeIDToCrateMap[15] = "EE02";
272  flangeIDToCrateMap[3] = "EE02";
273  flangeIDToCrateMap[27] = "EE02";
274  flangeIDToCrateMap[24] = "EE02";
275  flangeIDToCrateMap[40] = "EE02";
276  flangeIDToCrateMap[75] = "EE02";
277  flangeIDToCrateMap[86] = "EE20T";
278  flangeIDToCrateMap[54] = "EE20M";
279  flangeIDToCrateMap[8] = "EE20B";
280 
281  unsigned int fragmentID = std::stol(argv[8],nullptr,16);
282 
283  if (fragmentID & tpcIdentifier)
284  {
285  if (fragmentBoardMap.find(fragmentID) == fragmentBoardMap.end())
286  {
287  unsigned int flangeID = std::stol(argv[1]);
288  fragmentBoardMap[fragmentID].first = flangeIDToCrateMap[flangeID];
289  }
290 
291  unsigned int readoutID = std::stol(argv[0]);
292  fragmentBoardMap[fragmentID].second.emplace_back(readoutID);
293 
294  }
295 
296  return 0;
297 }
298 
299  // -----------------------------------------------------
300  // The aim of this function is to build a map between the
301  // TPC Fragment IDs and the readout board IDs. Here we
302  // expect there will be some number of boards per Fragment
303  //-----------------------------------------------------
305  {
306  const std::string dataType("readout_boards");
307 
308  // Recover the data from the database
309  int error = GetDataset(dataType,buildTPCFragmentIDToReadoutIDMap_callback,&fragmentBoardMap);
310 
311  // If there was an error the function above would have printed a message so bail out
312  if (error)
313  throw cet::exception("ChannelMapSQLite::BuildTPCFragmentIDToReadoutIDMap") << "Encountered error in reading the database: '" << error << "'\n";
314 
315  return error;
316  }
317 
318  // -----------------------------------------------------
319  // The aim of this function is to build a map between the
320  // TPC readout board IDs and the associated channels. So for
321  // each readout board ID we expect a number of channels back
322  // from the data base. So the returned data structure will
323  // be a map of readout ID to a vector of channels
324  //-----------------------------------------------------
325  const unsigned int CHANNELSPERBOARD = 64;
326  int buildTPCReadoutBoardToChannelMap_callback(void* dataIn, int argc, char**argv, char** azColName)
327  {
329 
330  unsigned int readoutBoardID = std::stol(argv[2]);
331 
332  if (rbChanMap.find(readoutBoardID) == rbChanMap.end())
333  {
334  unsigned int readoutBoardSlot = std::stol(argv[4]);
335 
336  rbChanMap[readoutBoardID].first = readoutBoardSlot;
337  rbChanMap[readoutBoardID].second.resize(CHANNELSPERBOARD);
338  }
339 
340  unsigned int channelNum = std::stol(argv[5]);
341  unsigned int channelID = std::stol(argv[0]);
342 
343  std::string fragmentBuffer = argv[10];
344 
345  // Make sure lower case... (sigh...)
346  std::transform(fragmentBuffer.begin(),fragmentBuffer.end(),fragmentBuffer.begin(),[](char c){return std::tolower(c);});
347 
348  unsigned int plane(3);
349 
350  if (fragmentBuffer.find("collection") != std::string::npos) plane = 2;
351  else if (fragmentBuffer.find("induction 2") != std::string::npos) plane = 1;
352  else if (fragmentBuffer.find("induction 1") != std::string::npos) plane = 0;
353 
354  if (plane > 2) std::cout << "YIKES!!! Plane is " << plane << " for channel " << channelID << " with type " << std::string(fragmentBuffer) << std::endl;
355 
356  rbChanMap[readoutBoardID].second[channelNum] = IChannelMapping::ChannelPlanePair(channelID,plane);
357 
358  return 0;
359  }
360 
362  {
363  const std::string dataType("daq_channels");
364 
365  // Recover the data from the database
367 
368  // If there was an error the function above would have printed a message so bail out
369  if (error)
370  throw cet::exception("ChannelMapSQLite::BuildTPCReadoutBoardToChannelMap") << "Encountered error in reading the database: '" << error << "'\n";
371 
372  return error;
373  }
374 
375  //******************* PMT Channel Mapping ***********************
376  int buildFragmentToDigitizerChannelMap_callback(void* dataIn, int argc, char**argv, char** azColName)
377  {
379 
380  // Start extracting info
381  // std::string digitizerBuffer = argv[8];
382  unsigned int fragmentID = std::stol(argv[18]);
383  unsigned int digitizerChannelNo = std::stol(argv[9]);
384  unsigned int channelID = std::stol(argv[17]);
385 
386  // Fill the map
387  fragmentToDigitizerChannelMap[fragmentID].emplace_back(digitizerChannelNo,channelID);
388 
389  return 0;
390  }
391 
393  {
394  // clearing is cleansing
395  fragmentToDigitizerChannelMap.clear();
396  // Recover the information from the database on the mapping
397  const std::string dataType("pmt_placements");
398 
399  // Recover the data from the database
400  int error = GetDataset(dataType,buildFragmentToDigitizerChannelMap_callback,&fragmentToDigitizerChannelMap);
401 
402  // If there was an error the function above would have printed a message so bail out
403  if (error)
404  throw cet::exception("ChannelMapSQLite::BuildFragmentToDigitizerChannelMap") << "Encountered error in reading the database: '" << error << "'\n";
405 
406  return error;
407  }
408 
409 
410  //******************* CRT Channel Mapping ***********************
411 
412  int buildCRTChannelIDToHWtoSimMacAddressPairMap_callback(void* dataIn, int argc, char**argv, char** azColName)
413  {
415 
416  /*
417  // Start extracting info
418  unsigned int channelID = std::stol(argv[10]);
419  unsigned int simmacaddress = std::stol(argv[11]);
420  unsigned int hwmacaddress = std::stol(argv[12]);
421  */
422 
423  unsigned int channelID = strcmp(argv[10],"None")==0 ? 0 : std::stol(argv[10]);
424  unsigned int simmacaddress = strcmp(argv[11],"None")==0 ? 0 : std::stol(argv[11]);
425  unsigned int hwmacaddress = strcmp(argv[12],"None")==0 ? 0 : std::stol(argv[12]);
426 
427  // Fill the map
428  crtChannelIDToHWtoSimMacAddressPairMap[channelID]=std::make_pair(hwmacaddress, simmacaddress);
429 
430  return 0;
431  }
432 
434  {
435  // clearing is cleansing
436  crtChannelIDToHWtoSimMacAddressPairMap.clear();
437  // Recover the information from the database on the mapping
438  const std::string dataType("feb_channels");
439 
440  // Recover the data from the database
441  int error = GetDataset(dataType,buildCRTChannelIDToHWtoSimMacAddressPairMap_callback,&crtChannelIDToHWtoSimMacAddressPairMap);
442 
443  // If there was an error the function above would have printed a message so bail out
444  if (error)
445  throw cet::exception("ChannelMapSQLite::BuildCRTChannelIDToHWtoSimMacAddressPairMap") << "Encountered error in reading the database: '" << error << "'\n";
446 
447  return error;
448  }
449 
450  // Top CRT
451  //----------------------------------
452  int buildTopCRTHWtoSimMacAddressPairMap_callback(void* dataIn, int argc, char**argv, char** azColName)
453  {
455 
456  // Start extracting info
457  unsigned int simmacaddress = strcmp(argv[41],"None")==0 ? 0 : std::stol(argv[41]);
458  unsigned int hwmacaddress = strcmp(argv[3], "None")==0 ? 0 : std::stol(argv[3]);
459 
460  // Fill the map
461  topcrtHWtoSimMacAddressPairMap[hwmacaddress] = simmacaddress;
462 
463  return 0;
464  }
465 
467  {
468  // clearing is cleansing
469  topcrtHWtoSimMacAddressPairMap.clear();
470  // Recover the information from the database on the mapping
471  const std::string dataType("crtfeb");
472 
473  // Recover the data from the database
474  int error = GetDataset(dataType,buildTopCRTHWtoSimMacAddressPairMap_callback,&topcrtHWtoSimMacAddressPairMap);
475 
476  // If there was an error the function above would have printed a message so bail out
477  if (error)
478  throw cet::exception("ChannelMapSQLite::BuildTopCRTHWtoSimMacAddressPairMap") << "Encountered error in reading the database: '" << error << "'\n";
479 
480  return error;
481  }
482 
483 
484  //******************* Accessing Side CRT Calibration Database ***********************
485 
487  {
488  // clearing is cleansing
489  sideCRTChannelToCalibrationMap.clear();
490 
491  std::string fullFileName;
492  cet::search_path searchPath("FW_SEARCH_PATH");
493 
494 
495  if (!searchPath.find_file(fCalibDBFileName+".db", fullFileName)){
496  std::cout << "******* Succesfully found calibration input file: " << fCalibDBFileName << std::endl;
497  throw cet::exception("ChannelMapSQLite::GetDataset") << "Can't find calibration input file: '" << fCalibDBFileName << "'\n";
498  }
499 
500  lariov::DBFolder database(fCalibDBFileName, "", "", fTag, true, false);
501 
502  database.UpdateData(1638918271*1e9);
503 
504  std::vector<unsigned int> channels;
505  database.GetChannelList(channels);
506 
507  for (auto it = channels.begin(); it != channels.end(); ++it) {
508 
509  long mac5, chan;
510  double gain, ped;
511 
512  // Start extracting info
513  database.GetNamedChannelData(*it, "mac5", mac5);
514  database.GetNamedChannelData(*it, "localchannel", chan);
515  database.GetNamedChannelData(*it, "gain", gain);
516  database.GetNamedChannelData(*it, "pedestal", ped);
517 
518  // Fill the map
519  sideCRTChannelToCalibrationMap.insert(std::make_pair(std::make_pair((int)mac5,(int)chan), std::make_pair(gain,ped)));
520  }
521 
522  return 0;
523  }
524 
525 
526  DEFINE_ART_CLASS_TOOL(ChannelMapSQLite)
527 } // namespace lar_cluster3d
int GetNamedChannelData(DBChannelID_t channel, const std::string &name, bool &data)
Definition: DBFolder.cxx:70
static constexpr Sample_t transform(Sample_t sample)
int callback(void *data, int argc, char **argv, char **azColName)
int buildTPCReadoutBoardToChannelMap_callback(void *dataIn, int argc, char **argv, char **azColName)
int buildCRTChannelIDToHWtoSimMacAddressPairMap_callback(void *dataIn, int argc, char **argv, char **azColName)
bool UpdateData(DBTimeStamp_t raw_time)
Definition: DBFolder.cxx:254
This provides an art tool interface definition for tools handle the channel mapping The idea is to be...
std::pair< unsigned int, unsigned int > ChannelPlanePair
Define the returned data structures for a mapping between TPC readout boards and the channel informat...
virtual int BuildTopCRTHWtoSimMacAddressPairMap(TopCRTHWtoSimMacAddressPairMap &) const override
int GetChannelList(std::vector< DBChannelID_t > &channels) const
Definition: DBFolder.cxx:200
virtual int BuildCRTChannelIDToHWtoSimMacAddressPairMap(CRTChannelIDToHWtoSimMacAddressPairMap &) const override
Define the returned data structures for a mapping between CRT hardware mac_address to the simulated m...
int GetDataset(const std::string &, int func(void *, int, char **, char **), void *) const
std::map< unsigned int, CRTHWtoSimMacAddressPair > CRTChannelIDToHWtoSimMacAddressPairMap
int buildTopCRTHWtoSimMacAddressPairMap_callback(void *dataIn, int argc, char **argv, char **azColName)
virtual int BuildTPCFragmentIDToReadoutIDMap(TPCFragmentIDToReadoutIDMap &) const override
Define the returned data structures for a mapping between TPC Fragment IDs and the related crate and ...
std::map< unsigned int, SlotChannelVecPair > TPCReadoutBoardToChannelMap
virtual int BuildSideCRTCalibrationMap(SideCRTChannelToCalibrationMap &) const override
ChannelMapSQLite(fhicl::ParameterSet const &pset)
Constructor.
std::map< SideCRTMac5ToChannelPair, SideCRTGainToPedPair > SideCRTChannelToCalibrationMap
const unsigned int CHANNELSPERBOARD
virtual int BuildTPCReadoutBoardToChannelMap(TPCReadoutBoardToChannelMap &) const override
Define the returned data structures for a mapping between TPC readout boards and the channel informat...
std::map< unsigned int, CrateNameReadoutIDPair > TPCFragmentIDToReadoutIDMap
ChannelMapSQLite class definiton.
int buildFragmentToDigitizerChannelMap_callback(void *dataIn, int argc, char **argv, char **azColName)
std::map< size_t, DigitizerChannelChannelIDPairVec > FragmentToDigitizerChannelMap
virtual int BuildFragmentToDigitizerChannelMap(FragmentToDigitizerChannelMap &) const override
Define the returned data structures for a mapping between PMT Fragment IDs and the related crate and ...
IChannelMapping interface class definiton.
int buildTPCFragmentIDToReadoutIDMap_callback(void *dataIn, int argc, char **argv, char **azColName)
BEGIN_PROLOG could also be cout
std::map< unsigned int, unsigned int > TopCRTHWtoSimMacAddressPairMap