forked from coderdj/redax
-
Notifications
You must be signed in to change notification settings - Fork 7
/
CControl_Handler.cc
197 lines (178 loc) · 5.79 KB
/
CControl_Handler.cc
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#include "CControl_Handler.hh"
#include "DAXHelpers.hh"
#include "MongoLog.hh"
#include "V2718.hh"
#include "f2718.hh"
#ifdef HASDDC10
#include "DDC10.hh"
#endif
#include "V1495.hh"
#include "V1495_tpc.hh"
#include <bsoncxx/builder/stream/document.hpp>
CControl_Handler::CControl_Handler(std::shared_ptr<MongoLog>& log, std::string procname) : DAQController(log, procname){
fCurrentRun = -1;
fV2718 = nullptr;
fV1495 = nullptr;
#ifdef HASDDC10
fDDC10 = nullptr;
#endif
fStatus = DAXHelpers::Idle;
}
CControl_Handler::~CControl_Handler(){
Stop();
}
// Initialising various devices namely; V2718 crate controller, V1495, DDC10...
int CControl_Handler::Arm(std::shared_ptr<Options>& opts){
int ret = 0, cc_handle = -1;
fStatus = DAXHelpers::Arming;
// Just in case clear out any remaining objects from previous runs
Stop();
fOptions = opts;
try{
fCurrentRun = opts->GetInt("number", -1);
}catch(std::exception& e) {
fLog->Entry(MongoLog::Warning, "No run number specified in config?? %s", e.what());
return -1;
}
// Pull options for V2718
CrateOptions copts;
if(fOptions->GetCrateOpt(copts) != 0){
fLog->Entry(MongoLog::Error,
"Failed to pull crate options from file. Required fields: s_in, pulser_freq, muon_veto, neutron_veto, led_trigger");
fStatus = DAXHelpers::Idle;
return -1;
}
// Getting the link and crate for V2718
std::vector<BoardType> bv = fOptions->GetBoards("V27XX");
if(bv.size() != 1){
fLog->Entry(MongoLog::Message, "Require one V2718 to be defined");
fStatus = DAXHelpers::Idle;
return -1;
}
BoardType cc = bv[0];
try{
if (cc.type == "f2718")
fV2718 = std::make_unique<f2718>(fLog, copts);
else
fV2718 = std::make_unique<V2718>(fLog, copts);
if((ret = fV2718->Init(cc.link, cc.crate)) != 0){
fLog->Entry(MongoLog::Error, "Failed to init V2718 with CAEN error: %i", ret);
throw std::runtime_error("Could not init CC");
}
}catch(std::exception& e){
fLog->Entry(MongoLog::Error, "Failed to initialize V2718 crate controller: %s", e.what());
fStatus = DAXHelpers::Idle;
return -1;
}
cc_handle = fV2718->GetHandle();
fLog->Entry(MongoLog::Local, "V2718 Initialized");
#ifdef HASDDC10
// Getting options for DDC10 HEV module
std::vector<BoardType> dv = fOptions->GetBoards("DDC10");
if (dv.size() == 1){
HEVOptions hopts;
if(fOptions->GetHEVOpt(hopts) == 0){
fDDC10 = std::make_unique<DDC10>();
if(fDDC10->Initialize(hopts) != 0){
fLog->Entry(MongoLog::Error, "Failed to initialise DDC10 HEV");
fStatus = DAXHelpers::Idle;
return -1;
}else{
fLog->Entry(MongoLog::Local, "DDC10 Initialised");
}
}else{
fLog->Entry(MongoLog::Error, "Failed to pull DDC10 options from file");
}
} else {
}
#endif // HASDDC10
std::vector<BoardType> boards = fOptions->GetBoards("V1495");
if (boards.size() == 1){
BoardType v1495 = boards[0];
if (v1495.type == "V1495_TPC")
fV1495 = std::make_unique<V1495_TPC>(fLog, fOptions, v1495.board, cc_handle, v1495.vme_address);
else
fV1495 = std::make_unique<V1495>(fLog, fOptions, v1495.board, cc_handle, v1495.vme_address);
std::map<std::string, int> opts;
if (fOptions->GetV1495Opts(opts) < 0) {
fLog->Entry(MongoLog::Warning, "Error getting V1495 options");
} else if (fV1495->Arm(opts)) {
fLog->Entry(MongoLog::Warning, "Could not initialize V1495");
}
}else{
// no V1495
}
fStatus = DAXHelpers::Armed;
return 0;
}
int CControl_Handler::Start(){
if(fStatus != DAXHelpers::Armed){
fLog->Entry(MongoLog::Warning, "V2718 attempt to start without arming. Maybe unclean shutdown");
return 0;
}
if(fV1495 && fV1495->BeforeSINStart()) {
fLog->Entry(MongoLog::Error, "Could not start V1495");
fStatus = DAXHelpers::Error;
return -1;
}
if(!fV2718 || fV2718->SendStartSignal()!=0){
fLog->Entry(MongoLog::Error, "V2718 either failed to start");
fStatus = DAXHelpers::Error;
return -1;
}
if(fV1495 && fV1495->AfterSINStart()) {
fLog->Entry(MongoLog::Error, "Could not start V1495");
fStatus = DAXHelpers::Error;
return -1;
}
fStatus = DAXHelpers::Running;
return 0;
}
// Stopping the previously started devices; V2718, V1495, DDC10...
int CControl_Handler::Stop(){
if(fV2718){
if (fV1495 && fV1495->BeforeSINStop()) {
fLog->Entry(MongoLog::Warning, "Could not stop V1495");
}
if(fV2718->SendStopSignal() != 0){
fLog->Entry(MongoLog::Warning, "Failed to stop V2718");
}
if (fV1495 && fV1495->AfterSINStop()) {
fLog->Entry(MongoLog::Warning, "Could not stop V1495");
}
}
fV1495.reset();
fV2718.reset();
#ifdef HASDDC10
// HEV cleans up on reset
fDDC10.reset();
#endif
fOptions.reset();
fStatus = DAXHelpers::Idle;
return 0;
}
// Reporting back on the status of V2718, V1495, DDC10 etc...
void CControl_Handler::StatusUpdate(mongocxx::collection* collection){
using namespace bsoncxx::builder::stream;
document builder{};
builder << "host" << fHostname << "status" << fStatus <<
"time" << bsoncxx::types::b_date(std::chrono::system_clock::now()) <<
"mode" << (fOptions ? fOptions->GetString("name", "none") : "none") <<
"number" << (fOptions ? fOptions->GetInt("number", -1) : -1);
auto in_array = builder << "active" << open_array;
if(fV2718){
auto crate_options = fV2718->GetCrateOptions();
in_array << open_document
<< "type" << "V2718"
<< "s_in" << crate_options.s_in
<< "neutron_veto" << crate_options.neutron_veto
<< "muon_veto" << crate_options.muon_veto
<< "led_trigger" << crate_options.led_trigger
<< "pulser_freq" << crate_options.pulser_freq
<< close_document;
}
auto after_array = in_array << close_array;
auto doc = after_array << finalize;
collection->insert_one(std::move(doc));
return;
}