Class Reference for E1039 Core & Analysis Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
CalibParamXT.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 "CalibParamXT.h"
12 using namespace std;
13 
15  CalibParamBase("xt_curve", "det\tt\tx\tdx")
16 {
17  ;
18 }
19 
21 {
22  for (Map_t::iterator it = m_map_t2x .begin(); it != m_map_t2x .end(); it++) delete it->second;
23  for (Map_t::iterator it = m_map_t2dx.begin(); it != m_map_t2dx.end(); it++) delete it->second;
24 }
25 
27 {
28  istringstream iss;
29  int nn = 0;
30  for (LineList::iterator it = lines.begin(); it != lines.end(); it++) {
31  iss.clear(); // clear any error flags
32  iss.str(*it);
33  string det;
34  double t, x, dx;
35  if (! (iss >> det >> t >> x >> dx)) continue;
36  Add(det, t, x, dx);
37  nn++;
38  }
39  return nn;
40 }
41 
42 int CalibParamXT::WriteFileCont(std::ostream& os)
43 {
44  int nn = 0;
45  for (List_t::iterator it = m_list.begin(); it != m_list.end(); it++) {
46  os << it->det_name << "\t"
47  << it->t << "\t" << it->x << "\t" << it->dx << "\n";
48  nn++;
49  }
50  return nn;
51 }
52 
54 {
55  ostringstream oss;
56  oss << "select det_name, det, t, x, dx from " << MapTableName();
57  TSQLStatement* stmt = db.Process(oss.str());
58  while (stmt->NextResultRow()) {
59  string det_name = stmt->GetString(0);
60  short det = stmt->GetInt (1);
61  double t = stmt->GetDouble(2);
62  double x = stmt->GetDouble(3);
63  double dx = stmt->GetDouble(4);
64  Add(det_name, det, t, x, dx);
65  }
66  delete stmt;
67 }
68 
70 {
71  string name_table = MapTableName();
72 
73  const char* list_var [] = { "det_name", "det", "t", "x", "dx" };
74  const char* list_type[] = { "VARCHAR(32)", "SMALLINT", "DOUBLE", "DOUBLE", "DOUBLE" };
75  const int n_var = 5;
76  db.CreateTable(name_table, n_var, list_var, list_type);
77 
78  ostringstream oss;
79  oss << "insert into " << name_table << "(det_name, det, t, x, dx) values";
80  for (List_t::iterator it = m_list.begin(); it != m_list.end(); it++) {
81  oss << " ('" << it->det_name << "', " << it->det << ", "
82  << it->t << ", " << it->x << ", " << it->dx << "),";
83  }
84  string query = oss.str();
85  query.erase(query.length()-1, 1); // Remove the last ',' char.
86  if (! db.Con()->Exec(query.c_str())) {
87  cerr << "!!ERROR!! CalibParamXT::WriteToDB(): in insert. Abort." << endl;
88  exit(1);
89  }
90 }
91 
93  const std::string det,
94  const double t, const double x, const double dx)
95 {
100  if (det[0] == 'P' && det.length() == 3) {
101  Add(det + "1f", t, x, dx);
102  Add(det + "1b", t, x, dx);
103  return;
104  }
105 
106  const int ele = 1; // Any element is OK since the X-T curve is single per plane.
107  GeomSvc* geom = GeomSvc::instance();
108  string det_new = det;
109  int ele_new = ele;
110  geom->toLocalDetectorName(det_new, ele_new);
111  int det_id = geom->getDetectorID(det_new);
112  Add(det, det_id, t, x, dx);
113 
114  //if (ele_new != ele) {
115  // cout << "!WARNING! CalibParamXT::Add(): The GeomSvc conversion changed element ID unexpectedly:\n"
116  // << " From det = " << det << ", ele = " << ele << "\n"
117  // << " To det = " << det_new << " (id=" << det_id << "), ele = " << ele_new << "\n"
118  // << " The mapping result will be incorrect!!" << endl;
119  //}
120 }
121 
123  const std::string det_name, const short det_id,
124  const double t, const double x, const double dx)
125 {
126  ParamItem item;
127  item.det_name = det_name;
128  item.det = det_id;
129  item.t = t;
130  item.x = x;
131  item.dx = dx;
132  m_list.push_back(item);
133  TGraphErrors* gr_t2x;
134  TGraphErrors* gr_t2dx;
135  if (m_map_t2x.find(det_id) == m_map_t2x.end()) {
136  m_map_t2x [det_id] = gr_t2x = new TGraphErrors();
137  m_map_t2dx[det_id] = gr_t2dx = new TGraphErrors();
138  } else {
139  gr_t2x = m_map_t2x [det_id];
140  gr_t2dx = m_map_t2dx[det_id];
141  }
142  int n_pt = gr_t2x->GetN();
143  gr_t2x ->SetPoint(n_pt, t, x);
144  gr_t2dx->SetPoint(n_pt, t, dx);
145  gr_t2x ->SetPointError(n_pt, 0, dx);
146 }
147 
148 bool CalibParamXT::Find(const short det, TGraphErrors*& gr_t2x, TGraphErrors*& gr_t2dx)
149 {
150  if (m_map_t2x.find(det) != m_map_t2x.end()) {
151  gr_t2x = m_map_t2x [det];
152  gr_t2dx = m_map_t2dx[det];
153  return true;
154  }
155  gr_t2x = gr_t2dx = 0;
156  return false;
157 }
158 
159 void CalibParamXT::Print(std::ostream& os)
160 {
161  //int n_ent = 0;
162  //for (List_t::iterator it = m_list.begin(); it != m_list.end(); it++) {
163  // os << it->det_name << "\t" << it->det << "\t" << it->ele << "\t"
164  // << it->roc << "\t" << it->board << "\t" << it->chan << "\n";
165  // n_ent++;
166  //}
167  //cout << " n = " << n_ent << endl;
168 }
std::string MapTableName()
void Print(std::ostream &os)
Definition: DbSvc.h:9
virtual ~CalibParamXT()
Definition: CalibParamXT.cc:20
void WriteDbTable(DbSvc &db)
Definition: CalibParamXT.cc:69
std::vector< std::string > LineList
Definition: RunParamBase.h:51
TSQLStatement * Process(const char *query)
Definition: DbSvc.cc:206
TSQLServer * Con()
Definition: DbSvc.h:38
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: CalibParamXT.cc:26
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
bool Find(const short det, TGraphErrors *&gr_t2x, TGraphErrors *&gr_t2dx)
int getDetectorID(const std::string &detectorName) const
Get the plane position.
Definition: GeomSvc.h:184
int WriteFileCont(std::ostream &os)
Definition: CalibParamXT.cc:42
void Add(const std::string det, const double t, const double x, const double dx)
Definition: CalibParamXT.cc:92
void ReadDbTable(DbSvc &db)
Definition: CalibParamXT.cc:53