Class Reference for E1039 Core & Analysis Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ChanMapV1495.cc
Go to the documentation of this file.
1 #include <iostream>
2 #include <iomanip>
3 #include <sstream>
4 #include <cstdlib>
5 #include <fstream>
6 #include <TSQLServer.h>
7 #include <TSQLStatement.h>
8 #include <db_svc/DbSvc.h>
9 #include "GeomSvc.h"
10 #include "ChanMapV1495.h"
11 using namespace std;
12 
14  ChanMapBase("v1495", "det\tele\tlvl\troc\tboard\tchan")
15 {
16  ;
17 }
18 
20 {
21  istringstream iss;
22  int nn = 0;
23  for (LineList::iterator it = lines.begin(); it != lines.end(); it++) {
24  iss.clear(); // clear any error flags
25  iss.str(*it);
26  string det;
27  short ele, lvl, roc, board, chan;
28  if (! (iss >> det >> ele >> lvl >> roc >> board >> chan)) continue;
29  Add(roc, board, chan, det, ele, lvl);
30  nn++;
31  }
32  return nn;
33 }
34 
35 int ChanMapV1495::WriteFileCont(std::ostream& os)
36 {
37  int nn = 0;
38  for (List_t::iterator it = m_list.begin(); it != m_list.end(); it++) {
39  os << it->det_name << "\t" << it->ele << "\t" << it->lvl << "\t"
40  << it->roc << "\t" << it->board << "\t" << it->chan << "\n";
41  nn++;
42  }
43  return nn;
44 }
45 
47 {
48  ostringstream oss;
49  oss << "select roc, board, chan, det_name, det, ele, lvl from " << MapTableName();
50  TSQLStatement* stmt = db.Process(oss.str());
51  while (stmt->NextResultRow()) {
52  short roc = stmt->GetInt (0);
53  short board = stmt->GetInt (1);
54  short chan = stmt->GetInt (2);
55  string det_name = stmt->GetString(3);
56  short det = stmt->GetInt (4);
57  short ele = stmt->GetInt (5);
58  short lvl = stmt->GetInt (6);
59  Add(roc, board, chan, det_name, det, ele, lvl);
60  }
61  delete stmt;
62 }
63 
65 {
66  string name_table = MapTableName();
67 
68  const char* list_var [] = { "roc", "board", "chan", "det_name", "det", "ele", "lvl" };
69  const char* list_type[] = { "SMALLINT", "SMALLINT", "SMALLINT", "VARCHAR(32)", "SMALLINT", "SMALLINT", "SMALLINT" };
70  const int n_var = 7;
71  db.CreateTable(name_table, n_var, list_var, list_type);
72 
73  ostringstream oss;
74  oss << "insert into " << name_table << "(roc, board, chan, det_name, det, ele, lvl) values";
75  for (List_t::iterator it = m_list.begin(); it != m_list.end(); it++) {
76  oss << " (" << it->roc << ", " << it->board << ", " << it->chan
77  << ", '" << it->det_name << "', " << it->det << ", " << it->ele << ", " << it->lvl << "),";
78  }
79  string query = oss.str();
80  query.erase(query.length()-1, 1); // Remove the last ',' char.
81  if (! db.Con()->Exec(query.c_str())) {
82  cerr << "!!ERROR!! ChanMapV1495::WriteToDB(): in insert. Abort." << endl;
83  exit(1);
84  }
85 }
86 
91  const short roc, const short board, const short chan,
92  const std::string det, const short ele, const short lvl)
93 {
94  GeomSvc* geom = GeomSvc::instance();
95  string det_new = det;
96  int ele_new = ele;
97  int det_id;
98  if (det == "STOP" ) { det_id = 1000; }
99  else if (det == "L1PXtp") { det_id = 1001; }
100  else if (det == "L1PXtn") { det_id = 1002; }
101  else if (det == "L1PXbp") { det_id = 1003; }
102  else if (det == "L1PXbn") { det_id = 1004; }
103  else {
104  geom->toLocalDetectorName(det_new, ele_new);
105  det_id = geom->getDetectorID(det_new);
106  }
107  Add(roc, board, chan, det, det_id, ele, lvl);
108 
109  if (ele_new != ele) {
110  cout << "!WARNING! ChanMapV1495::Add(): The GeomSvc conversion changed element ID unexpectedly:\n"
111  << " From det = " << det << ", ele = " << ele << "\n"
112  << " To det = " << det_new << "(id = " << det_id << "), ele = " << ele_new << "\n"
113  << " The mapping result will be incorrect!!" << endl;
114  }
115 }
116 
118  const short roc, const short board, const short chan,
119  const std::string det_name, const short det_id, const short ele, const short lvl)
120 {
121  MapItem item;
122  item.roc = roc;
123  item.board = board;
124  item.chan = chan;
125  item.det_name = det_name;
126  item.det = det_id;
127  item.ele = ele;
128  item.lvl = lvl;
129  m_list.push_back(item);
130  m_map[RocBoardChan_t(roc, board, chan)] = DetEleLvl_t(det_id, ele, lvl);
131 }
132 
133 //bool ChanMapV1495::Find(const short roc, const short board, const short chan, std::string& det, short& ele, short& lvl)
134 //{
135 // RocBoardChan_t key(roc, board, chan);
136 // if (m_map.find(key) != m_map.end()) {
137 // DetEleLvl_t val = m_map[key];
138 // det = std::get<0>(val);
139 // ele = std::get<1>(val);
140 // lvl = std::get<2>(val);
141 // return true;
142 // }
143 // det = "";
144 // ele = lvl = 0;
145 // return false;
146 //}
147 
148 bool ChanMapV1495::Find(const short roc, const short board, const short chan, short& det, short& ele, short& lvl)
149 {
150  RocBoardChan_t key(roc, board, chan);
151  if (m_map.find(key) != m_map.end()) {
152  DetEleLvl_t val = m_map[key];
153  det = std::get<0>(val);
154  ele = std::get<1>(val);
155  lvl = std::get<2>(val);
156  return true;
157  }
158 
159  det = ele = lvl = 0;
160  return false;
161 }
162 
163 void ChanMapV1495::Print(std::ostream& os)
164 {
165  int n_ent = 0;
166  for (List_t::iterator it = m_list.begin(); it != m_list.end(); it++) {
167  os << it->det_name << "\t" << it->det << "\t" << it->ele << "\t" << it->lvl << "\t"
168  << it->roc << "\t" << it->board << "\t" << it->chan << "\n";
169  n_ent++;
170  }
171  cout << n_ent << endl;
172 }
std::string MapTableName()
void WriteDbTable(DbSvc &db)
Definition: ChanMapV1495.cc:64
Definition: DbSvc.h:9
void ReadDbTable(DbSvc &db)
Definition: ChanMapV1495.cc:46
std::tuple< short, short, short > RocBoardChan_t
Definition: RunParamBase.h:61
std::vector< std::string > LineList
Definition: RunParamBase.h:51
TSQLStatement * Process(const char *query)
Definition: DbSvc.cc:206
void Add(const short roc, const short board, const short chan, const std::string det, const short ele, const short lvl)
Definition: ChanMapV1495.cc:90
TSQLServer * Con()
Definition: DbSvc.h:38
void Print(std::ostream &os)
void CreateTable(const std::string name, const std::vector< std::string > list_var, const std::vector< std::string > list_type, const std::vector< std::string > list_key)
Definition: DbSvc.cc:132
int ReadFileCont(LineList &lines)
Definition: ChanMapV1495.cc:19
static GeomSvc * instance()
singlton instance
Definition: GeomSvc.cxx:211
void toLocalDetectorName(std::string &detectorName, int &eID)
Convert the official detectorName to local detectorName.
Definition: GeomSvc.cxx:865
int getDetectorID(const std::string &detectorName) const
Get the plane position.
Definition: GeomSvc.h:184
int WriteFileCont(std::ostream &os)
Definition: ChanMapV1495.cc:35
bool Find(const short roc, const short board, const short chan, short &det, short &ele, short &lvl)