Class Reference for E1039 Core & Analysis Software
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
CalibParamInTimeV1495.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 <TGraphErrors.h>
9 #include <db_svc/DbSvc.h>
10 #include "GeomSvc.h"
11 #include "CalibParamInTimeV1495.h"
12 using namespace std;
13 
15  CalibParamBase("intime_v1495", "det\tele\tlvl\tcenter\twidth")
16 {
17  ;
18 }
19 
21 {
22  istringstream iss;
23  int nn = 0;
24  for (LineList::iterator it = lines.begin(); it != lines.end(); it++) {
25  iss.clear(); // clear any error flags
26  iss.str(*it);
27  string det;
28  short ele, lvl;
29  double center, width;
30  if (! (iss >> det >> ele >> lvl >> center >> width)) continue;
31  Add(det, ele, lvl, center, width);
32  nn++;
33  }
34  return nn;
35 }
36 
38 {
39  int nn = 0;
40  for (List_t::iterator it = m_list.begin(); it != m_list.end(); it++) {
41  os << it->det_name << "\t" << it->ele << "\t" << it->lvl << "\t"
42  << it->center << "\t" << it->width << "\n";
43  nn++;
44  }
45  return nn;
46 }
47 
49 {
50  ostringstream oss;
51  oss << "select det_name, det, ele, lvl, center, width from " << MapTableName();
52  TSQLStatement* stmt = db.Process(oss.str());
53  while (stmt->NextResultRow()) {
54  string det_name = stmt->GetString(0);
55  short det = stmt->GetInt (1);
56  short ele = stmt->GetInt (2);
57  short lvl = stmt->GetInt (3);
58  double center = stmt->GetDouble(4);
59  double width = stmt->GetDouble(5);
60  Add(det_name, det, ele, lvl, center, width);
61  }
62  delete stmt;
63 }
64 
66 {
67  string name_table = MapTableName();
68 
69  const char* list_var [] = { "det_name", "det", "ele", "lvl", "center", "width" };
70  const char* list_type[] = { "VARCHAR(32)", "SMALLINT", "SMALLINT", "SMALLINT", "DOUBLE", "DOUBLE" };
71  const int n_var = 6;
72  db.CreateTable(name_table, n_var, list_var, list_type);
73 
74  ostringstream oss;
75  oss << "insert into " << name_table << "(det_name, det, ele, lvl, center, width) values";
76  for (List_t::iterator it = m_list.begin(); it != m_list.end(); it++) {
77  oss << " ('" << it->det_name << "', " << it->det << ", " << it->ele << ", " << it->lvl << ", "
78  << it->center << ", " << it->width << "),";
79  }
80  string query = oss.str();
81  query.erase(query.length()-1, 1); // Remove the last ',' char.
82  if (! db.Con()->Exec(query.c_str())) {
83  cerr << "!!ERROR!! CalibParamInTimeV1495::WriteToDB(): in insert. Abort." << endl;
84  exit(1);
85  }
86 }
87 
89  const std::string det, const short ele, const short lvl,
90  const double center, const double width)
91 {
92  GeomSvc* geom = GeomSvc::instance();
93  string det_new = det;
94  int ele_new = ele;
95  int det_id;
96  if (det == "STOP" ) { det_id = 1000; }
97  else if (det == "L1PXtp") { det_id = 1001; }
98  else if (det == "L1PXtn") { det_id = 1002; }
99  else if (det == "L1PXbp") { det_id = 1003; }
100  else if (det == "L1PXbn") { det_id = 1004; }
101  else {
102  geom->toLocalDetectorName(det_new, ele_new);
103  det_id = geom->getDetectorID(det_new);
104  }
105  Add(det, det_id, ele, lvl, center, width);
106 
107  if (ele_new != ele) {
108  cout << "!WARNING! CalibParamInTimeV1495::Add(): The GeomSvc conversion changed element ID unexpectedly:\n"
109  << " From det = " << det << ", ele = " << ele << "\n"
110  << " To det = " << det_new << "(id = " << det_id << "), ele = " << ele_new << "\n"
111  << " The mapping result will be incorrect!!" << endl;
112  }
113 }
114 
116  const std::string det_name, const short det_id, const short ele, const short lvl,
117  const double center, const double width)
118 {
119  ParamItem item;
120  item.det_name = det_name;
121  item.det = det_id;
122  item.ele = ele;
123  item.lvl = lvl;
124  item.center = center;
125  item.width = width;
126  m_list.push_back(item);
127  m_map[DetEleLvl_t(det_id, ele, lvl)] = CenterWidth_t(center, width);
128 }
129 
130 bool CalibParamInTimeV1495::Find(const short det, const short ele, const short lvl, double& center, double& width)
131 {
132  DetEleLvl_t key(det, ele, lvl);
133  if (m_map.find(key) != m_map.end()) {
134  CenterWidth_t* val = &m_map[key];
135  center = val->first;
136  width = val->second;
137  return true;
138  } else if (ele != 0) { // Try to find an entry common to all elements.
139  return Find(det, 0, lvl, center, width);
140  }
141  center = width = 0;
142  return false;
143 }
144 
145 void CalibParamInTimeV1495::Print(std::ostream& os)
146 {
147  int n_ent = 0;
148  for (List_t::iterator it = m_list.begin(); it != m_list.end(); it++) {
149  os << it->det_name << "\t" << it->det << "\t" << it->ele << "\t" << it->lvl << "\t"
150  << it->center << "\t" << it->width << "\n";
151  n_ent++;
152  }
153  cout << " n = " << n_ent << endl;
154 }
void Print(std::ostream &os)
int ReadFileCont(LineList &lines)
bool Find(const short det, const short ele, const short lvl, double &center, double &width)
void Add(const std::string det, const short ele, const short lvl, const double center, const double width)
int WriteFileCont(std::ostream &os)
Standard interface with SQL database.
Definition: DbSvc.h:15
TSQLStatement * Process(const char *query)
Definition: DbSvc.cc:176
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:102
TSQLServer * Con()
Definition: DbSvc.h:44
User interface class about the geometry of detector planes.
Definition: GeomSvc.h:164
int getDetectorID(const std::string &detectorName) const
Get the plane position.
Definition: GeomSvc.h:219
static GeomSvc * instance()
singlton instance
Definition: GeomSvc.cxx:212
void toLocalDetectorName(std::string &detectorName, int &eID)
Convert the official detectorName to local detectorName.
Definition: GeomSvc.cxx:740
std::vector< std::string > LineList
Definition: RunParamBase.h:51
std::string MapTableName()