Class Reference for E1039 Core & Analysis Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
RunParamBase.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 <TSystem.h>
7 #include <db_svc/DbSvc.h>
8 #include "RunParamBase.h"
9 using namespace std;
10 
11 RunParamBase::RunParamBase(const std::string type, const std::string label, const std::string header) :
12  m_type(type), m_label(label), m_header(header), m_map_id("")
13 {
14  m_dir_base = gSystem->Getenv("E1039_RESOURCE");
15  if (m_dir_base.length() == 0) {
16  m_dir_base = "/data2/e1039/resource";
17  cout << "RunParamBase: E1039_RESOURCE is empty. Use '" << m_dir_base << "' as the data directory." << endl;
18  }
19 }
20 
21 void RunParamBase::SetMapIDbyFile(const std::string map_id)
22 {
23  m_range.ReadFromFile(RangeFileName().c_str());
24  if (! m_range.Find(map_id)) {
25  cout << " !WARNING! SetMapIDbyFile(): This map ID '" << map_id
26  << "' is not included in the run-range table. OK?" << endl;
27  }
28  m_map_id = map_id;
29 }
30 
31 void RunParamBase::SetMapIDbyDB(const std::string map_id)
32 {
33  m_range.ReadFromDB(RangeFileName().c_str());
34  if (! m_range.Find(map_id)) {
35  cout << " !WARNING! SetMapIDbyDB(): This map ID '" << map_id
36  << "' is not included in the run-range table. OK?" << endl;
37  }
38  m_map_id = map_id;
39 }
40 
42 {
43  m_range.ReadFromFile(RangeFileName().c_str());
44  m_map_id = m_range.Find(run);
45 }
46 
48 {
49  m_range.ReadFromDB(SchemaName());
50  m_map_id = m_range.Find(run);
51 }
52 
54 {
56 }
57 
59 {
61 }
62 
63 void RunParamBase::ReadFromLocalFile(const string fn_tsv)
64 {
65  cout << " RunParamBase::ReadFromFile(): " << fn_tsv << "...";
66  ifstream ifs(fn_tsv.c_str());
67  if (! ifs) {
68  cerr << "\n!!ERROR!! Cannot open the map file '" << fn_tsv << "'." << endl;
69  exit(1);
70  }
71 
72  LineList lines;
73  string buffer;
74  while ( getline(ifs, buffer) ) {
75  if (buffer[0] == '#') continue;
76  lines.push_back(buffer);
77  }
78  ifs.close();
79  int nn = ReadFileCont(lines);
80  cout << " read " << nn << " entries." << endl;
81 }
82 
83 void RunParamBase::WriteToLocalFile(const string fn_tsv)
84 {
85  cout << " RunParamBase::WriteToFile(): " << fn_tsv << "...";
86  ofstream ofs(fn_tsv.c_str());
87  if (! ofs) {
88  cerr << "\n!!ERROR!! Cannot open the map file '" << fn_tsv << "'." << endl;
89  exit(1);
90  }
91  ofs << "#" << m_header << "\n";
92  int nn = WriteFileCont(ofs);
93  ofs.close();
94  cout << " wrote " << nn << " entries." << endl;
95 }
96 
98 {
99  if (m_map_id.length() == 0) {
100  cerr << " ERROR: The map ID is not set. Abort." << endl;
101  exit(1);
102  }
103  string name_schema = SchemaName();
104  string name_table = MapTableName();
105  cout << "Read channel map from "
106  << name_schema << "." << name_table << ".\n";
107  //cout << " Schema = " << name_schema
108  // << "\n Table = " << name_table << "\n";
109 
110  DbSvc db(DbSvc::DB1);
111  db.UseSchema(name_schema);
112  db.HasTable(name_table, true);
113  ReadDbTable(db);
114 }
115 
117 {
118  cout << "RunParamBase::WriteToDB()\n";
119  if (m_map_id.length() == 0) {
120  cerr << " ERROR: The map ID is not set. Abort." << endl;
121  exit(1);
122  }
123  string name_schema = SchemaName();
124  string name_table = MapTableName();
125  cout << " Schema = " << name_schema
126  << "\n Table = " << name_table << "\n";
127 
128  DbSvc db(DbSvc::DB1);
129  db.UseSchema(name_schema, true);
130  db.DropTable(name_table);
131  WriteDbTable(db);
132  cout << " ...done." << endl;
133 }
134 
136 {
137  m_range.WriteToDB(SchemaName());
138 }
139 
140 void RunParamBase::Print(std::ostream& os)
141 {
142  cout << " virtual function called." << endl;
143 }
144 
146 {
147  ostringstream oss;
148  oss << m_dir_base << "/" << m_type << "/" << m_label << "/run_range.tsv";
149  return oss.str();
150 }
151 
153 {
154  ostringstream oss;
155  oss << m_dir_base << "/" << m_type << "/" << m_label << "/" << m_map_id << "/param.tsv";
156  return oss.str();
157 }
158 
160 {
161  ostringstream oss;
162  oss << "user_e1039_" << m_type << "_" << m_label;
163  return oss.str();
164 }
165 
167 {
168  ostringstream oss;
169  oss << "param_" << m_map_id;
170  return oss.str();
171 }
172 
174 {
175  cout << " virtual function called." << endl;
176  return 0;
177 }
178 
179 int RunParamBase::WriteFileCont(std::ostream& os)
180 {
181  cout << " virtual function called." << endl;
182  return 0;
183 }
184 
186 {
187  cout << " virtual function called." << endl;
188 }
189 
191 {
192  cout << " virtual function called." << endl;
193 }
virtual void Print(std::ostream &os)
void WriteToDB()
std::string MapTableName()
Definition: DbSvc.h:9
void UseSchema(const char *name, const bool do_create=false, const bool do_drop=false)
Definition: DbSvc.cc:82
std::string SchemaName()
bool Find(const std::string map_id)
void WriteToDB(const std::string schema)
std::string MapFileName()
std::vector< std::string > LineList
Definition: RunParamBase.h:51
void WriteToLocalFile(const std::string fn_tsv)
Definition: RunParamBase.cc:83
virtual int ReadFileCont(LineList &lines)
void WriteRangeToDB()
int run(const int nEvents=1)
Definition: run.C:10
void SetMapIDbyDB(const std::string map_id)
Definition: RunParamBase.cc:31
void WriteToFile()
Definition: RunParamBase.cc:58
void SetMapIDbyFile(const std::string map_id)
Definition: RunParamBase.cc:21
void ReadFromDB()
Definition: RunParamBase.cc:97
void ReadFromFile(const std::string fn_tsv)
virtual void ReadDbTable(DbSvc &db)
bool HasTable(const char *name, const bool exit_on_false=false)
Definition: DbSvc.cc:122
RunParamBase(const std::string type, const std::string label, const std::string header)
Definition: RunParamBase.cc:11
void DropTable(const char *name)
Definition: DbSvc.cc:112
virtual int WriteFileCont(std::ostream &os)
std::string RangeFileName()
void ReadFromFile()
Definition: RunParamBase.cc:53
void ReadFromLocalFile(const std::string fn_tsv)
Definition: RunParamBase.cc:63
void ReadFromDB(const std::string schema)
virtual void WriteDbTable(DbSvc &db)