-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathget_trigger_params.cpp
More file actions
138 lines (106 loc) · 4.61 KB
/
Copy pathget_trigger_params.cpp
File metadata and controls
138 lines (106 loc) · 4.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/** This example shows how to get several values from TRIGGER section of the run configuration file.
*
* This document illustrates:
* a. How to parse config file and get values from config parser
* b. How to get config file from the database
* c. How to get config file name from CODA 'rtvs'
* d. How to deal with maps and arrays saved as JSon
*
* The values from trigger section (and how they are defined there):
*
* BLOCKLEVEL 1 # so it is name-value
*
* TS_TRIG_HOLD 10 1 # that is name-array
*
* TRIG_TYPE PS 440 ... # array lines that occurs met many times
* TRIG_TYPE BFCAL 440 ...
* ...
*
* TRIG_EQ PS 35 ... # Same as TRIG_TYPE - array which occurs many times
* TRIG_EQ BCAL_E 20 ...
*
* To do this, in terma of RCDB C++ API we have to perform the following steps:
*
* 1. Connect to DB
* 2. Read 'rvts' condition, that holds run config file name (we need the name for the next step)
* 3. Get run config file from the DB
* 4. Parse file contents (that will five us ConfigFileParseResult)
* 5. Get required data out of ConfigFileParseResult
*
*/
#include <string>
#include <vector>
#include <iostream>
#include "RCDB/Connection.h"
#include "RCDB/ConfigParser.h"
#include <tao/json.hpp>
using namespace std;
vector<string> SectionNames = {"TRIGGER", "GLOBAL", "FCAL", "BCAL", "TOF", "ST", "TAGH",
"TAGM", "PS", "PSC", "TPOL", "CDC", "FDC"};
int main ( int argc, char *argv[] )
{
using namespace std;
// ==> 0. PREPARE
if ( argc != 3 ) {
cout<<"This example gets some trigger parameters for a specified run"<<endl;
cout<<"usage: "<< argv[0] <<" <connection_string> <run_number>"<<endl;
cout<<"exmpl: "<< argv[0] <<" mysql://rcdb@hallddb.jlab.org/rcdb 10200"<<endl;
return 1;
}
string connection_str(argv[1]); // Get a connection string from arguments
int runNumber = atoi(argv[2]); // The run number, we work with
// ==> 1. Create DB connection
rcdb::Connection connection(connection_str);
// ==> 2. Get rtvs condition, that contains config file name for the given run
auto rtvsCondition = connection.GetCondition(runNumber, "rtvs"); // Get condition by run and name
if(!rtvsCondition) {
cout<<"'rtvs' condition is not set for run "<<runNumber<<endl;
return 2;
}
auto json = tao::json::from_string(rtvsCondition->ToString()); // The CODA rtvs is serialized as JSon dictionary.
string fileName = json.at("%(config)").get_string(); // We need item with name '%(config)'
// That is our file name
// ==> 3. Get file out of RCDB (by run number and name)
auto file = connection.GetFile(runNumber, fileName);
if(!file) { // If there is no such file, null is returned
cout<<"File with name: "<< fileName
<<" doesn't exist (not associated) with run: "<< runNumber << endl;
return 3;
}
// ==> 4. Parse run config file content
string fileContent = file->GetContent(); // Get file content
auto result = rcdb::ConfigParser::Parse(fileContent, SectionNames); // Parse it!
// NameValues member of Sections contains things like 'BLOCKLEVEL 1'
cout<<"BLOCKLEVEL = "<<result.Sections["TRIGGER"].NameValues["BLOCKLEVEL"]<< endl;
// NameVectors contains all lines with name-multiple values, like 'TS_TRIG_HOLD 10 1'
vector<string> trig_holds = result.Sections["TRIGGER"].NameVectors["TS_TRIG_HOLD"];
cout<<"TS_TRIG_HOLD = ";
for (auto val: trig_holds){
cout<<val<<", ";
}
cout<<endl;
// All lines of section go to Rows. To get TRIG_TYPE and TRIG_EQ we grep through lines:
vector<vector<string>> triggerTypes;
vector<vector<string>> triggerEqs;
// Get all TRIG_TYPE from TRIGGER section
for(auto row : result.Sections["TRIGGER"].Rows) {
if(row[0] == "TRIG_TYPE") {
triggerTypes.push_back(row); // The line starts with TRIG_TYPE
}
if(row[0] == "TRIG_EQ") {
triggerEqs.push_back(row); // The line starts with TRIG_EQ
}
}
// Print the results
cout<<"TRIG_TYPEs:"<<endl;
for(auto line: triggerTypes) {
for(auto cell: line) cout<<cell<<" ";
cout<<endl<<endl;
}
cout<<"TRIG_TYPEs:"<<endl;
for(auto line: triggerTypes) {
for(auto cell: line) cout<<cell<<" ";
cout<<endl<<endl;
}
return 0;
}