Class Reference for E1039 Core & Analysis Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
DbUpRun.cc
Go to the documentation of this file.
1 #include <iomanip>
3 #include <TClass.h>
4 #include <interface_main/SQRun.h>
8 #include <phool/PHNodeIterator.h>
9 #include <phool/PHIODataNode.h>
10 #include <phool/getClass.h>
11 #include <TSQLServer.h>
12 #include <db_svc/DbSvc.h>
13 #include <UtilAna/UtilOnline.h>
14 #include "DecoStatusDb.h"
15 #include "DbUpRun.h"
16 using namespace std;
17 
18 DbUpRun::DbUpRun(const std::string& name) : SubsysReco(name)
19 {
20  ;
21 }
22 
24 {
26 }
27 
29 {
30  SQRun* run = findNode::getClass<SQRun >(topNode, "SQRun");
31  SQParamDeco* par_deco = findNode::getClass<SQParamDeco>(topNode, "SQParamDeco");
32  if (!run || !par_deco) return Fun4AllReturnCodes::ABORTEVENT;
33  UploadRun(run);
34  UploadParam(run->get_run_id(), par_deco);
36 }
37 
39 {
40  static int utime_pre = 0;
41  int utime_now = time(0);
42  if (utime_now - utime_pre >= 10) { // Suppress the number of updates
43  utime_pre = utime_now;
44  SQRun* run_header = findNode::getClass<SQRun>(topNode, "SQRun");
45  if (!run_header) return Fun4AllReturnCodes::ABORTEVENT;
46  UploadRun(run_header);
47  DecoStatusDb deco_stat;
48  deco_stat.RunUpdated(run_header->get_run_id());
49  }
51 }
52 
54 {
55  SQRun* run = findNode::getClass<SQRun >(topNode, "SQRun");
56  SQParamDeco* par_deco = findNode::getClass<SQParamDeco>(topNode, "SQParamDeco");
57  if (!run || !par_deco) return Fun4AllReturnCodes::ABORTEVENT;
58  UploadRun(run);
59  UploadParam(run->get_run_id(), par_deco);
61 }
62 
67 void DbUpRun::UploadRun(SQRun* sq)
68 {
69  const char* table_name = "run";
70  static DbSvc* db = 0;
71  if (db == 0) {
72  db = new DbSvc(DbSvc::DB1);
74  //db->DropTable(table_name); // Use this when you want to refresh
75  if (! db->HasTable(table_name)) {
76  DbSvc::VarList list;
77  list.Add("run_id" , "INT", true);
78  list.Add("utime_b" , "INT");
79  list.Add("utime_e" , "INT");
80  list.Add("fpga1_enabled" , "BOOL");
81  list.Add("fpga2_enabled" , "BOOL");
82  list.Add("fpga3_enabled" , "BOOL");
83  list.Add("fpga4_enabled" , "BOOL");
84  list.Add("fpga5_enabled" , "BOOL");
85  list.Add( "nim1_enabled" , "BOOL");
86  list.Add( "nim2_enabled" , "BOOL");
87  list.Add( "nim3_enabled" , "BOOL");
88  list.Add( "nim4_enabled" , "BOOL");
89  list.Add( "nim5_enabled" , "BOOL");
90  list.Add("fpga1_prescale", "INT");
91  list.Add("fpga2_prescale", "INT");
92  list.Add("fpga3_prescale", "INT");
93  list.Add("fpga4_prescale", "INT");
94  list.Add("fpga5_prescale", "INT");
95  list.Add( "nim1_prescale", "INT");
96  list.Add( "nim2_prescale", "INT");
97  list.Add( "nim3_prescale", "INT");
98  list.Add("n_spill" , "INT");
99  list.Add("n_evt_all" , "INT");
100  list.Add("n_evt_dec" , "INT");
101  db->CreateTable(table_name, list);
102  }
103  }
104 
105  ostringstream oss;
106  oss << "delete from " << table_name << " where run_id = " << sq->get_run_id();
107  if (! db->Con()->Exec(oss.str().c_str())) {
108  cerr << "!!ERROR!! DbUpRun::UploadRun()." << endl;
109  return;
110  }
111  oss.str("");
112  oss << "insert into " << table_name << " values"
113  << " (" << sq->get_run_id()
114  << ", " << sq->get_unix_time_begin()
115  << ", " << sq->get_unix_time_end();
116  for (int ii=0; ii<5; ii++) oss << ", " << sq->get_fpga_enabled(ii);
117  for (int ii=0; ii<5; ii++) oss << ", " << sq->get_nim_enabled(ii);
118  for (int ii=0; ii<5; ii++) oss << ", " << sq->get_fpga_prescale(ii);
119  for (int ii=0; ii<3; ii++) oss << ", " << sq->get_nim_prescale(ii);
120  oss << ", " << sq->get_n_spill()
121  << ", " << sq->get_n_evt_all()
122  << ", " << sq->get_n_evt_dec()
123  << ")";
124  if (! db->Con()->Exec(oss.str().c_str())) {
125  cerr << "!!ERROR!! DbUpRun::UploadRun()." << endl;
126  return;
127  }
128 }
129 
131 void DbUpRun::UploadParam(const int run, const SQParamDeco* sq)
132 {
133  const char* table_name = "param_deco";
134  DbSvc db(DbSvc::DB1);
136  //db.DropTable(table_name); // Use this when you want to refresh
137  if (! db.HasTable(table_name)) {
138  DbSvc::VarList list;
139  list.Add("run_id", "INT", true);
140  list.Add("name" , "VARCHAR(64)", true);
141  list.Add("value" , "VARCHAR(64)");
142  db.CreateTable(table_name, list);
143  }
144 
145  if (sq->size() == 0) return;
146 
147  ostringstream oss;
148  oss << "delete from " << table_name << " where run_id = " << run;
149  if (! db.Con()->Exec(oss.str().c_str())) {
150  cerr << "!!ERROR!! DbUpRun::UploadParam()." << endl;
151  return;
152  }
153  oss.str("");
154  oss << "insert into " << table_name << " values";
155  for (SQParamDeco::ParamConstIter it = sq->begin(); it != sq->end(); it++) {
156  oss << " (" << run << ", '" << it->first << "', '" << it->second << "'),";
157  }
158  string query = oss.str();
159  query.erase(query.length()-1, 1); // Remove the last ',' char.
160  if (! db.Con()->Exec(query.c_str())) {
161  cerr << "!!ERROR!! DbUpRun::UploadParam()." << endl;
162  return;
163  }
164 }
void RunUpdated(const int run, int utime=0)
Definition: DecoStatusDb.cc:68
virtual ParamConstIter begin() const =0
Return the begin iterator.
virtual int get_n_evt_dec() const
Return the number of decoded events. The online decoding usually skips a part of events in order to f...
Definition: SQRun.h:71
Definition: DbSvc.h:9
virtual int get_unix_time_end() const
Return the Unix time when this run ended.
Definition: SQRun.h:38
void UseSchema(const char *name, const bool do_create=false, const bool do_drop=false)
Definition: DbSvc.cc:82
virtual int get_nim_prescale(const int chan) const
Return the prescale factor of the given channel (&#39;chan&#39;) of the NIM trigger in this run...
Definition: SQRun.h:50
int End(PHCompositeNode *topNode)
Called at the end of all processing.
Definition: DbUpRun.cc:53
virtual int get_unix_time_begin() const
Return the Unix time when this run began.
Definition: SQRun.h:35
int run(const int nEvents=1)
Definition: run.C:10
virtual int get_n_spill() const
Return the number of spill events.
Definition: SQRun.h:65
virtual int get_fpga_prescale(const int chan) const
Return the prescale factor of the given channel (&#39;chan&#39;) of the FPGA trigger in this run...
Definition: SQRun.h:47
int InitRun(PHCompositeNode *topNode)
Definition: DbUpRun.cc:28
virtual unsigned int size() const =0
Return the number of variables held.
void Add(const std::string name, const std::string type, const bool is_key=false)
Definition: DbSvc.cc:245
static std::string GetSchemaMainDaq()
Definition: UtilOnline.h:28
TSQLServer * Con()
Definition: DbSvc.h:38
int process_event(PHCompositeNode *topNode)
Definition: DbUpRun.cc: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
DbUpRun(const std::string &name="DbUpRun")
Definition: DbUpRun.cc:18
bool HasTable(const char *name, const bool exit_on_false=false)
Definition: DbSvc.cc:122
virtual int get_n_evt_all() const
Return the number of all recorded events.
Definition: SQRun.h:68
virtual int get_run_id() const
Return the run ID.
Definition: SQRun.h:32
int Init(PHCompositeNode *topNode)
Definition: DbUpRun.cc:23
virtual ParamConstIter end() const =0
Return the end iterator.
An SQ interface class to hold the run-level info.
Definition: SQRun.h:18
virtual int get_nim_enabled(const int chan) const
Return &#39;true&#39; if the given channel (&#39;chan&#39;) of the NIM trigger was enabled in this run...
Definition: SQRun.h:44
SQParamDeco.h.
Definition: SQParamDeco.h:16
virtual int get_fpga_enabled(const int chan) const
Return &#39;true&#39; if the given channel (&#39;chan&#39;) of the FPGA trigger was enabled in this run...
Definition: SQRun.h:41
ParamMap::const_iterator ParamConstIter
Definition: SQParamDeco.h:19