Class Reference for E1039 Core & Analysis Software
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
DecoError.cc
Go to the documentation of this file.
1 #include <iostream>
2 #include <iomanip>
3 #include <sstream>
4 #include <TSQLServer.h>
5 #include <db_svc/DbSvc.h>
6 #include <UtilAna/UtilOnline.h>
7 #include "DecoError.h"
8 using namespace std;
9 
11  : m_run_id(0)
12  , m_spill_id(0)
13  , m_flush_has_error(false)
14 {
15  InitData();
16 }
17 
18 void DecoError::SetID(const int run_id, const int spill_id)
19 {
20  m_run_id = run_id;
21  m_spill_id = spill_id;
22 }
23 
25 {
26  m_n_evt_all = 0;
27  m_n_evt_ng = 0;
28  for (int roc = 0; roc < N_ROC; roc++) {
29  for (int err = 0; err < N_TDC_ERROR; err++) {
30  m_n_err_tdc[roc][err].clear();
31  }
32  }
33 }
34 
36 {
37  m_n_evt_all++;
38  if (m_flush_has_error) m_n_evt_ng++;
39 }
40 
41 void DecoError::AddTdcError(const int event, const int roc, const TdcError_t type)
42 {
43  m_n_err_tdc[roc][type].push_back(event);
44  cout << "AddTdcError: " << event << " " << roc << " " << type << "." << endl;
45  m_flush_has_error = true;
46 }
47 
49 {
50  DbSvc db(DbSvc::DB1);
52  UpdateDbInfo(&db);
53  UpdateDbTdc (&db);
54 }
55 
56 void DecoError::PrintData(std::ostream& os)
57 {
58  os << "DecoError::PrintData(): " << m_run_id << " " << m_spill_id << "\n";
59  for (int roc = 0; roc < N_ROC; roc++) {
60  for (int err = 0; err < N_TDC_ERROR; err++) {
61  int n_evt = m_n_err_tdc[roc][err].size();
62  if (n_evt == 0) continue;
63  os << " ROC " << setw(2) << roc << ", type " << setw(2) << err << ": n=" << n_evt;
64  if (n_evt > 100) n_evt = 100; // Print 100 event IDs at max.
65  for (int i_evt = 0; i_evt < n_evt; i_evt++) {
66  if (i_evt % 10 == 0) os << "\n ";
67  os << " " << m_n_err_tdc[roc][err].at(i_evt);
68  }
69  os << "\n";
70  }
71  }
72  os << endl;
73 }
74 
75 void DecoError::UpdateDbInfo(DbSvc* db)
76 {
77  const char* table_name = "deco_error_info";
78  //db->DropTable(table_name);
79  if (! db->HasTable(table_name)) {
80  DbSvc::VarList list;
81  list.Add("run_id" , "INT", true);
82  list.Add("spill_id" , "INT");
83  list.Add("utime" , "INT");
84  list.Add("n_evt_all", "INT");
85  list.Add("n_evt_ng" , "INT");
86  db->CreateTable(table_name, list);
87  }
88 
89  ostringstream oss;
90  oss << "delete from " << table_name << " where run_id = " << m_run_id;
91  if (! db->Con()->Exec(oss.str().c_str())) {
92  cerr << "!!ERROR!! DecoError::UpdateDbInfo(): delete." << endl;
93  return;
94  }
95  oss.str("");
96  oss << "insert into " << table_name << " values (" << m_run_id << ", " << m_spill_id << ", " << time(0) << ", " << m_n_evt_all << ", " << m_n_evt_ng << ")";
97  if (! db->Con()->Exec(oss.str().c_str())) {
98  cerr << "!!ERROR!! DecoError::UpdateDbInfo(): insert." << endl;
99  }
100 }
101 
102 void DecoError::UpdateDbTdc(DbSvc* db)
103 {
104  const char* table_name = "deco_error_tdc";
105  //db->DropTable(table_name);
106  if (! db->HasTable(table_name)) {
107  DbSvc::VarList list;
108  list.Add("run_id" , "INT", true);
109  list.Add("roc_id" , "INT", true);
110  list.Add("error_id" , "INT", true);
111  list.Add("error_count" , "INT");
112  list.Add("event_id_min", "INT");
113  list.Add("event_id_max", "INT");
114  db->CreateTable(table_name, list);
115  }
116 
117  ostringstream oss;
118  oss << "delete from " << table_name << " where run_id = " << m_run_id;
119  if (! db->Con()->Exec(oss.str().c_str())) {
120  cerr << "!!ERROR!! DecoError::UpdateDbTdc(): delete." << endl;
121  return;
122  }
123 
124  int n_err = 0;
125  oss.str("");
126  oss << "insert into " << table_name << " values";
127  for (int roc = 0; roc < N_ROC; roc++) {
128  for (int err = 0; err < N_TDC_ERROR; err++) {
129  if (m_n_err_tdc[roc][err].size() == 0) continue;
130  oss << " (" << m_run_id
131  << ", " << roc
132  << ", " << err
133  << ", " << m_n_err_tdc[roc][err].size()
134  << ", " << m_n_err_tdc[roc][err].at(0)
135  << ", " << m_n_err_tdc[roc][err].back()
136  << "),";
137  n_err++;
138  }
139  }
140  if (n_err > 0) {
141  oss.seekp(-1, oss.cur);
142  oss << " "; // Erase the last ',' char.
143  if (! db->Con()->Exec(oss.str().c_str())) {
144  cerr << "!!ERROR!! DecoError::UpdateDbTdc()." << endl;
145  }
146  }
147 }
void Add(const std::string name, const std::string type, const bool is_key=false)
Definition: DbSvc.cc:282
Standard interface with SQL database.
Definition: DbSvc.h:15
@ DB1
Definition: DbSvc.h:17
void UseSchema(const char *name, const bool do_create=false, const bool do_drop=false)
Definition: DbSvc.cc:52
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
bool HasTable(const char *name, const bool exit_on_false=false)
Definition: DbSvc.cc:92
void AggregateData()
Definition: DecoError.cc:48
void PrintData(std::ostream &os=std::cout)
Definition: DecoError.cc:56
void CountFlush()
Definition: DecoError.cc:35
void InitData()
Definition: DecoError.cc:24
void AddTdcError(const int event, const int roc, const TdcError_t type)
Definition: DecoError.cc:41
@ N_TDC_ERROR
Definition: DecoError.h:24
void SetID(const int run_id, const int spill_id)
Definition: DecoError.cc:18
static std::string GetSchemaMainDaq()
Definition: UtilOnline.h:29