Class Reference for E1039 Core & Analysis Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
DbUpSpill.cc
Go to the documentation of this file.
1 #include <iomanip>
3 #include <TClass.h>
4 #include <interface_main/SQRun.h>
13 #include <phool/PHNodeIterator.h>
14 #include <phool/PHIODataNode.h>
15 #include <phool/getClass.h>
16 #include <TSQLServer.h>
17 #include <db_svc/DbSvc.h>
18 #include <UtilAna/UtilOnline.h>
19 #include "DbUpSpill.h"
20 using namespace std;
21 
22 DbUpSpill::DbUpSpill(const std::string& name) : SubsysReco(name)
23 {
24  ;
25 }
26 
28 {
30 }
31 
33 {
34  SQRun* run_header = findNode::getClass<SQRun>(topNode, "SQRun");
35  if (!run_header) return Fun4AllReturnCodes::ABORTEVENT;
36  int run_id = run_header->get_run_id();
37  ClearTable("spill" , run_id);
38  ClearTable("scaler_bos" , run_id);
39  ClearTable("scaler_eos" , run_id);
40  ClearTable("slow_cont_DAQ" , run_id);
41  ClearTable("slow_cont_beam" , run_id);
42  ClearTable("slow_cont_environment", run_id);
43  ClearTable("slow_cont_target ", run_id);
45 }
46 
48 {
49  SQSpillMap* spill_map = findNode::getClass<SQSpillMap >(topNode, "SQSpillMap");
50  SQEvent* event_header = findNode::getClass<SQEvent >(topNode, "SQEvent");
51  if (!spill_map || !event_header) return Fun4AllReturnCodes::ABORTEVENT;
52 
53  static int spill_id_pre = -1;
54  if (event_header->get_spill_id() != spill_id_pre) {
55  spill_id_pre = event_header->get_spill_id();
56  SQSpill* spi = spill_map->get(spill_id_pre);
57  //PrintSpill(spi);
58  UploadToSpillTable(spi);
59  UploadToScalerTable(spi, "bos");
60  UploadToScalerTable(spi, "eos");
61  UploadToSlowContTable(spi);
62  }
63 
65 }
66 
68 {
70 }
71 
72 void DbUpSpill::ClearTable(const char* table_name, const int run_id)
73 {
74  DbSvc db(DbSvc::DB1);
75  db.UseSchema(UtilOnline::GetSchemaMainDaq(), true);
76  if (! db.HasTable(table_name)) return;
77  ostringstream oss;
78  oss << "delete from " << table_name << " where run_id = " << run_id;
79  if (! db.Con()->Exec(oss.str().c_str())) {
80  cerr << "!!ERROR!! DbUpSpill::ClearTables(): table = " << table_name << ", run_id = " << run_id << "." << endl;
81  return;
82  }
83 }
84 
85 void DbUpSpill::UploadToSpillTable(SQSpill* spi)
86 {
87  const char* table_name = "spill";
88  DbSvc db(DbSvc::DB1);
89  db.UseSchema(UtilOnline::GetSchemaMainDaq(), true);
90  //db.DropTable(table_name); // Use this when you want to refresh
91  if (! db.HasTable(table_name)) {
92  DbSvc::VarList list;
93  list.Add("run_id" , "INT", true);
94  list.Add("spill_id" , "INT", true);
95  list.Add("target_pos" , "INT");
96  list.Add("bos_coda_id" , "INT");
97  list.Add("bos_vme_time", "INT");
98  list.Add("eos_coda_id" , "INT");
99  list.Add("eos_vme_time", "INT");
100  db.CreateTable(table_name, list);
101  }
102 
103  ostringstream oss;
104  oss << "delete from " << table_name << " where run_id = " << spi->get_run_id() << " and spill_id = " << spi->get_spill_id();
105  if (! db.Con()->Exec(oss.str().c_str())) {
106  cerr << "!!ERROR!! DbUpSpill::UploadToSpillTable()." << endl;
107  return;
108  }
109  oss.str("");
110  oss << "insert into " << table_name << " values"
111  << " (" << spi->get_run_id ()
112  << ", " << spi->get_spill_id ()
113  << ", " << spi->get_target_pos ()
114  << ", " << spi->get_bos_coda_id ()
115  << ", " << spi->get_bos_vme_time()
116  << ", " << spi->get_eos_coda_id ()
117  << ", " << spi->get_eos_vme_time()
118  << ")";
119  if (! db.Con()->Exec(oss.str().c_str())) {
120  cerr << "!!ERROR!! DbUpSpill::UploadToSpillTable()." << endl;
121  return;
122  }
123 }
124 
125 void DbUpSpill::UploadToScalerTable(SQSpill* spi, const std::string boseos)
126 {
127  int run_id = spi->get_run_id();
128  int spill_id = spi->get_spill_id();
129  SQStringMap* map_sca;
130  if (boseos == "bos") map_sca = spi->get_bos_scaler_list();
131  else if (boseos == "eos") map_sca = spi->get_eos_scaler_list();
132  else {
133  cerr << "!!ERROR!! DbUpSpill::UploadToScalerTable(): " << boseos << "?" << endl;
134  return;
135  }
136 
137  ostringstream oss;
138  oss << "scaler_" << boseos;
139  string table_name = oss.str();
140 
141  DbSvc db(DbSvc::DB1);
142  db.UseSchema(UtilOnline::GetSchemaMainDaq(), true);
143  //db.DropTable(table_name); // Use this when you want to refresh
144  if (! db.HasTable(table_name)) {
145  DbSvc::VarList list;
146  list.Add("run_id" , "INT", true);
147  list.Add("spill_id", "INT", true);
148  list.Add("name" , "VARCHAR(32)", true);
149  list.Add("count" , "INT");
150  db.CreateTable(table_name, list);
151  }
152 
153  oss.str("");
154  oss << "delete from " << table_name << " where run_id = " << run_id << " and spill_id = " << spill_id;
155  if (! db.Con()->Exec(oss.str().c_str())) {
156  cerr << "!!ERROR!! DbUpSpill::UploadToScalerTable()." << endl;
157  return;
158  }
159  oss.str("");
160  oss << "insert into " << table_name << " values";
161  for (SQStringMap::ConstIter it = map_sca->begin(); it != map_sca->end(); it++) {
162  string name = it->first;
163  SQScaler* sca = dynamic_cast<SQScaler*>(it->second);
164  oss << " (" << run_id << ", " << spill_id << ", '" << name << "', " << sca->get_count() << "),";
165  }
166  oss.seekp(-1, oss.cur);
167  oss << " "; // Erase the last ',' char.
168  if (! db.Con()->Exec(oss.str().c_str())) {
169  cerr << "!!ERROR!! DbUpSpill::UploadToScalerTable()." << endl;
170  return;
171  }
172 }
173 
174 void DbUpSpill::UploadToSlowContTable(SQSpill* spi)
175 {
176  int run_id = spi->get_run_id();
177  int spill_id = spi->get_spill_id();
178  SQStringMap* map_slo = spi->get_slow_cont_list();
179 
180  typedef map<string, ostringstream> OssMap_t;
181  OssMap_t map_oss;
182  for (SQStringMap::ConstIter it = map_slo->begin(); it != map_slo->end(); it++) {
183  string name = it->first;
184  if (name == "U:TODB25") continue; // Known that the value for this name is badly formatted
185  SQSlowCont* slo = dynamic_cast<SQSlowCont*>(it->second);
186  string type = slo->get_type();
187  if (name == "SLOWCONTROL_IS_GOOD") type = "DAQ"; // Known that the type for this name contains extra (invisible) characters.
188  map_oss[type] << " (" << run_id << ", " << spill_id << ", '" << name << "', '" << slo->get_time_stamp() << "', '" << slo->get_value() << "'),";
189  }
190 
191  DbSvc db(DbSvc::DB1);
192  db.UseSchema(UtilOnline::GetSchemaMainDaq(), true);
193 
194  ostringstream oss;
195  for (OssMap_t::iterator it = map_oss.begin(); it != map_oss.end(); it++) {
196  string type = it->first;
197  string values = it->second.str();
198  if (values.size() == 0) continue;
199 
200  string table_name = "slow_cont_";
201  table_name += type;
202  //db.DropTable(table_name); // Use this when you want to refresh
203  if (! db.HasTable(table_name)) {
204  DbSvc::VarList list;
205  list.Add("run_id" , "INT", true);
206  list.Add("spill_id" , "INT", true);
207  list.Add("name" , "VARCHAR(64)", true);
208  list.Add("time_stamp", "CHAR(14)");
209  list.Add("value" , "TEXT");
210  db.CreateTable(table_name, list);
211  }
212  oss.str("");
213  oss << "delete from " << table_name << " where run_id = " << run_id << " and spill_id = " << spill_id;
214  if (! db.Con()->Exec(oss.str().c_str())) {
215  cerr << "!!ERROR!! DbUpSpill::UploadToSlowContTable()." << endl;
216  continue;
217  }
218  oss.str("");
219  oss << "insert into " << table_name << " values" << values;
220  oss.seekp(-1, oss.cur);
221  oss << " "; // Erase the last ',' char.
222  if (! db.Con()->Exec(oss.str().c_str())) {
223  cerr << "!!ERROR!! DbUpSpill::UploadToSlowContTable()." << endl;
224  continue;
225  }
226  }
227 }
228 
229 void DbUpSpill::PrintSpill(SQSpill* spi)
230 {
231  cout << "SQSpill: "
232  << " " << spi->get_spill_id ()
233  << " " << spi->get_run_id ()
234  << " " << spi->get_target_pos ()
235  << " " << spi->get_bos_coda_id ()
236  << " " << spi->get_bos_vme_time()
237  << " " << spi->get_eos_coda_id ()
238  << " " << spi->get_eos_vme_time()
239  << " \nBOS Scaler: " << spi->get_bos_scaler_list()->size() << "\n";
240  for (SQStringMap::ConstIter it = spi->get_bos_scaler_list()->begin(); it != spi->get_bos_scaler_list()->end(); it++) {
241  string name = it->first;
242  SQScaler* sca = dynamic_cast<SQScaler*>(it->second);
243  cout << " " << setw(20) << name << " " << setw(10) << sca->get_count() << "\n";
244  }
245  cout << " EOS Scaler: " << spi->get_eos_scaler_list()->size() << "\n";
246  for (SQStringMap::ConstIter it = spi->get_eos_scaler_list()->begin(); it != spi->get_eos_scaler_list()->end(); it++) {
247  string name = it->first;
248  SQScaler* sca = dynamic_cast<SQScaler*>(it->second);
249  cout << " " << setw(20) << name << " " << setw(10) << sca->get_count() << "\n";
250  }
251  cout << " Slow Control " << spi->get_slow_cont_list()->size() << ":\n";
252  for (SQStringMap::ConstIter it = spi->get_slow_cont_list()->begin(); it != spi->get_slow_cont_list()->end(); it++) {
253  string name = it->first;
254  SQSlowCont* slo = dynamic_cast<SQSlowCont*>(it->second);
255  cout << " " << setw(20) << name << " " << slo->get_time_stamp() << " " << setw(20) << slo->get_value() << " " << slo->get_type() << "\n";
256  }
257 }
int process_event(PHCompositeNode *topNode)
Definition: DbUpSpill.cc:47
An SQ interface class to hold the data of one scaler channel.
Definition: SQScaler.h:15
Definition: DbSvc.h:9
virtual std::string get_type() const
Return the type (i.e. caterogy name) of this channel.
Definition: SQSlowCont.h:38
virtual int get_run_id() const
Return the run ID when this spill was taken.
Definition: SQSpill.h:32
virtual ConstIter begin() const
Definition: SQStringMap.h:39
virtual int get_count() const
Return the count of this scaler channel.
Definition: SQScaler.h:39
virtual std::string get_value() const
Return the value of this channel.
Definition: SQSlowCont.h:35
virtual size_t size() const
Definition: SQStringMap.h:30
virtual int get_bos_vme_time() const
Return the VME time at BOS of this spill.
Definition: SQSpill.h:44
An SQ interface class to hold the data of one slow-control channel.
Definition: SQSlowCont.h:15
int Init(PHCompositeNode *topNode)
Definition: DbUpSpill.cc:27
virtual short get_target_pos() const
Return the target position in this spill.
Definition: SQSpill.h:38
virtual SQStringMap * get_bos_scaler_list()
Return the list of scaler variables read out at BOS.
Definition: SQSpill.h:53
virtual int get_spill_id() const
Return the spill ID.
Definition: SQSpill.h:35
void Add(const std::string name, const std::string type, const bool is_key=false)
Definition: DbSvc.cc:245
virtual SQStringMap * get_eos_scaler_list()
Return the list of scaler variables read out at EOS.
Definition: SQSpill.h:54
static std::string GetSchemaMainDaq()
Definition: UtilOnline.h:28
virtual int get_bos_coda_id() const
Return the Coda ID at BOS of this spill.
Definition: SQSpill.h:41
An SQ interface class to hold the data of one spill.
Definition: SQSpill.h:19
virtual ConstIter end() const
Definition: SQStringMap.h:41
A general-purpose SQ interface class that holds a list of PHObjects with key = string.
Definition: SQStringMap.h:14
int End(PHCompositeNode *topNode)
Called at the end of all processing.
Definition: DbUpSpill.cc:67
virtual int get_eos_vme_time() const
Return the VME time at EOS of this spill.
Definition: SQSpill.h:50
int InitRun(PHCompositeNode *topNode)
Definition: DbUpSpill.cc:32
An SQ interface class to hold one event header.
Definition: SQEvent.h:17
virtual int get_run_id() const
Return the run ID.
Definition: SQRun.h:32
virtual std::string get_time_stamp() const
Return the time when this channel was read out.
Definition: SQSlowCont.h:29
An SQ interface class to hold the run-level info.
Definition: SQRun.h:18
virtual SQStringMap * get_slow_cont_list()
Return the list of slow control variables.
Definition: SQSpill.h:56
DbUpSpill(const std::string &name="DbUpSpill")
Definition: DbUpSpill.cc:22
An SQ interface class to hold a list of SQSpill objects.
Definition: SQSpillMap.h:19
virtual int get_eos_coda_id() const
Return the Coda ID at EOS of this spill.
Definition: SQSpill.h:47
ObjectMap::const_iterator ConstIter
Definition: SQStringMap.h:17
virtual const SQSpill * get(unsigned int idkey) const
Return the SQSpill entry having spill ID = &#39;idkey&#39;. Return &#39;0&#39; if no entry exists.
Definition: SQSpillMap.h:41