Skip to content

Commit e6d2efc

Browse files
committed
Work
1 parent fda9624 commit e6d2efc

8 files changed

+143
-161
lines changed

CControl_Handler.cc

+25-17
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ int CControl_Handler::Arm(std::shared_ptr<Options>& opts){
3131

3232
fOptions = opts;
3333
try{
34-
fCurrentRun = opts->GetInt("run_identifier", -1);
34+
fCurrentRun = opts->GetInt("number", -1);
3535
}catch(std::exception& e) {
3636
fLog->Entry(MongoLog::Warning, "No run number specified in config?? %s", e.what());
3737
return -1;
@@ -142,26 +142,34 @@ int CControl_Handler::Stop(){
142142

143143
// Reporting back on the status of V2718, V1495, DDC10 etc...
144144
void CControl_Handler::StatusUpdate(mongocxx::collection* collection){
145-
bsoncxx::builder::stream::document builder{};
146-
builder << "host" << fHostname << "status" << fStatus <<
147-
"time" << bsoncxx::types::b_date(std::chrono::system_clock::now()) <<
148-
"mode" << (fOptions ? fOptions->GetString("name", "none") : "none");
149-
auto in_array = builder << "active" << bsoncxx::builder::stream::open_array;
145+
using namespace bsoncxx::builder::stream;
146+
document builder{};
147+
builder << "$currentDate" << open_document << "time" << true << close_document <<
148+
"$set" << open_document <<
149+
"host" << fHostname <<
150+
"status" << fStatus <<
151+
//"time" << bsoncxx::types::b_date(std::chrono::system_clock::now()) <<
152+
"mode" << (fOptions ? fOptions->GetString("name", "none") : "none") <<
153+
"number" << (fOptions ? fOptions->GetInt("number", -1) : -1);
154+
auto in_array = builder << "active" << open_array;
150155

151156
if(fV2718){
152157
auto crate_options = fV2718->GetCrateOptions();
153-
in_array << bsoncxx::builder::stream::open_document
154-
<< "run_number" << fCurrentRun
155-
<< "type" << "V2718"
156-
<< "s_in" << crate_options.s_in
157-
<< "neutron_veto" << crate_options.neutron_veto
158-
<< "muon_veto" << crate_options.muon_veto
159-
<< "led_trigger" << crate_options.led_trigger
160-
<< "pulser_freq" << crate_options.pulser_freq
161-
<< bsoncxx::builder::stream::close_document;
158+
in_array << open_document <<
159+
"type" << "V2718" <<
160+
"s_in" << crate_options.s_in <<
161+
"neutron_veto" << crate_options.neutron_veto <<
162+
"muon_veto" << crate_options.muon_veto <<
163+
"led_trigger" << crate_options.led_trigger <<
164+
"pulser_freq" << crate_options.pulser_freq <<
165+
close_document;
162166
}
163-
auto after_array = in_array << bsoncxx::builder::stream::close_array;
164-
collection->insert_one(after_array << bsoncxx::builder::stream::finalize);
167+
auto after_array = in_array << close_array;
168+
auto doc = after_array << close_document << finalize;
169+
auto q = document{} << finalize;
170+
mongocxx::options::update opts;
171+
opts.upsert(true);
172+
collection->update_one(std::move(q), std::move(doc), opts);
165173
return;
166174
/*
167175
// DDC10 parameters might change for future updates of the XENONnT HEV

DAQController.cc

+33-37
Original file line numberDiff line numberDiff line change
@@ -77,18 +77,16 @@ int DAQController::Arm(std::shared_ptr<Options>& options){
7777
// Seriously. This sleep statement is absolutely vital.
7878
fLog->Entry(MongoLog::Local, "That felt great, thanks.");
7979
std::map<int, std::map<std::string, std::vector<double>>> dac_values;
80-
if (fOptions->GetString("baseline_dac_mode") == "cached")
81-
fOptions->GetDAC(dac_values, BIDs);
82-
std::vector<std::thread*> init_threads;
80+
std::vector<std::thread> init_threads;
81+
init_threads.reserve(fDigitizers.size());
8382
std::map<int,int> rets;
8483
// Parallel digitizer programming to speed baselining
8584
for( auto& link : fDigitizers ) {
8685
rets[link.first] = 1;
87-
init_threads.push_back(new std::thread(&DAQController::InitLink, this,
88-
std::ref(link.second), std::ref(dac_values), std::ref(rets[link.first])));
86+
init_threads.emplace_back(&DAQController::InitLink, this,
87+
std::ref(link.second), std::ref(dac_values), std::ref(rets[link.first]));
8988
}
90-
std::for_each(init_threads.begin(), init_threads.end(),
91-
[](std::thread* t) {t->join(); delete t;});
89+
for (auto& t : init_threads) if (t.joinable()) t.join();
9290

9391
if (std::any_of(rets.begin(), rets.end(), [](auto& p) {return p.second != 0;})) {
9492
fLog->Entry(MongoLog::Warning, "Encountered errors during digitizer programming");
@@ -298,7 +296,8 @@ void DAQController::CloseThreads(){
298296
}
299297

300298
void DAQController::StatusUpdate(mongocxx::collection* collection) {
301-
auto insert_doc = bsoncxx::builder::stream::document{};
299+
using namespace bsoncxx::builder::stream;
300+
auto insert_doc = document{};
302301
std::map<int, int> retmap;
303302
std::pair<long, long> buf{0,0};
304303
int rate = fDataRate;
@@ -312,52 +311,53 @@ void DAQController::StatusUpdate(mongocxx::collection* collection) {
312311
buf.second += x.second;
313312
}
314313
}
315-
insert_doc << "host" << fHostname <<
316-
"time" << bsoncxx::types::b_date(std::chrono::system_clock::now())<<
314+
auto doc = document{} <<
315+
"$currentDate" << open_document << "time" << true << close_document <<
316+
"$set" << open_document <<
317+
"host" << fHostname <<
318+
//"time" << bsoncxx::types::b_date(std::chrono::system_clock::now())<<
317319
"rate" << rate/1e6 <<
318320
"status" << fStatus <<
319321
"buffer_size" << (buf.first + buf.second)/1e6 <<
320-
"run_mode" << (fOptions ? fOptions->GetString("name", "none") : "none") <<
321-
"channels" << bsoncxx::builder::stream::open_document <<
322-
[&](bsoncxx::builder::stream::key_context<> doc){
322+
"mode" << (fOptions ? fOptions->GetString("name", "none") : "none") <<
323+
"number" << (fOptions ? fOptions->GetInt("number", -1) : -1) <<
324+
"channels" << open_document <<
325+
[&](key_context<> doc){
323326
for( auto const& pair : retmap)
324327
doc << std::to_string(pair.first) << short(pair.second>>10); // KB not MB
325-
} << bsoncxx::builder::stream::close_document;
326-
collection->insert_one(insert_doc << bsoncxx::builder::stream::finalize);
328+
} << close_document <<
329+
finalize;
330+
mongocxx::options::update opts;
331+
opts.upsert(true);
332+
auto query = document{} << finalize;
333+
collection->update_one(std::move(query), std::move(doc), opts); // opts is const&
327334
return;
328335
}
329336

330337
void DAQController::InitLink(std::vector<std::shared_ptr<V1724>>& digis,
331-
std::map<int, std::map<std::string, std::vector<double>>>& cal_values, int& ret) {
338+
std::map<int, std::vector<uint16_t>>& dac_values, int& ret) {
332339
std::string BL_MODE = fOptions->GetString("baseline_dac_mode", "fixed");
333-
std::map<int, std::vector<uint16_t>> dac_values;
334340
int nominal_baseline = fOptions->GetInt("baseline_value", 16000);
341+
int nominal_dac = fOptions->GetInt("baseline_fixed_value", 4000);
335342
if (BL_MODE == "fit") {
336-
if ((ret = FitBaselines(digis, dac_values, nominal_baseline, cal_values))) {
343+
if ((ret = FitBaselines(digis, dac_values, nominal_baseline))) {
337344
fLog->Entry(MongoLog::Warning, "Errors during baseline fitting");
338345
return;
339346
}
340347
}
341348

342-
for(auto digi : digis){
349+
for(auto& digi : digis){
343350
fLog->Entry(MongoLog::Local, "Board %i beginning specific init", digi->bid());
344351

345352
// Multiple options here
346353
int bid = digi->bid(), success(0);
347354
if (BL_MODE == "fit") {
348355
} else if(BL_MODE == "cached") {
349-
fMutex.lock();
350-
auto board_dac_cal = cal_values.count(bid) ? cal_values[bid] : cal_values[-1];
351-
fMutex.unlock();
352-
dac_values[bid] = std::vector<uint16_t>(digi->GetNumChannels());
356+
dac_values[bid] = fOptions->GetDAC(bid, digi->GetNumChannels(), nominal_dac);
353357
fLog->Entry(MongoLog::Local, "Board %i using cached baselines", bid);
354-
for (unsigned ch = 0; ch < digi->GetNumChannels(); ch++)
355-
dac_values[bid][ch] = nominal_baseline*board_dac_cal["slope"][ch] + board_dac_cal["yint"][ch];
356-
digi->ClampDACValues(dac_values[bid], board_dac_cal);
357358
} else if(BL_MODE == "fixed"){
358-
int BLVal = fOptions->GetInt("baseline_fixed_value", 4000);
359-
fLog->Entry(MongoLog::Local, "Loading fixed baselines with value 0x%04x", BLVal);
360-
dac_values[bid] = std::vector<uint16_t>(digi->GetNumChannels(), BLVal);
359+
fLog->Entry(MongoLog::Local, "Loading fixed baselines with value 0x%04x", nominal_dac);
360+
dac_values[bid] = std::vector<uint16_t>(digi->GetNumChannels(), nominal_dac);
361361
} else {
362362
fLog->Entry(MongoLog::Warning, "Received unknown baseline mode '%s', valid options are \"fit\", \"cached\", and \"fixed\"", BL_MODE.c_str());
363363
ret = -1;
@@ -381,12 +381,10 @@ void DAQController::InitLink(std::vector<std::shared_ptr<V1724>>& digis,
381381
}
382382
success += digi->LoadDAC(dac_values[bid]);
383383
// Load all the other fancy stuff
384-
success += digi->SetThresholds(fOptions->GetThresholds(bid));
385-
digi->ResetClocks();
384+
success += digi->SetThresholds(fOptions->GetThresholds(bid), digi->GetNumChannels());
386385

387386
fLog->Entry(MongoLog::Local, "Board %i programmed", digi->bid());
388387
if(success!=0){
389-
//LOG
390388
fLog->Entry(MongoLog::Warning, "Failed to configure digitizers.");
391389
ret = -1;
392390
return;
@@ -398,8 +396,7 @@ void DAQController::InitLink(std::vector<std::shared_ptr<V1724>>& digis,
398396
}
399397

400398
int DAQController::FitBaselines(std::vector<std::shared_ptr<V1724>> &digis,
401-
std::map<int, std::vector<u_int16_t>> &dac_values, int target_baseline,
402-
std::map<int, std::map<std::string, std::vector<double>>> &cal_values) {
399+
std::map<int, std::vector<u_int16_t>> &dac_values, int target_baseline) {
403400
using std::vector;
404401
using namespace std::chrono_literals;
405402
int max_iter = fOptions->GetInt("baseline_max_iterations", 2);
@@ -418,6 +415,7 @@ int DAQController::FitBaselines(std::vector<std::shared_ptr<V1724>> &digis,
418415
std::map<int, int> words_read;
419416
std::map<int, vector<vector<double>>> bl_per_channel;
420417
std::map<int, vector<int>> diff;
418+
std::map<int, std::map<std::string, vector<double>>> cal_values;
421419

422420
for (auto digi : digis) { // alloc ALL the things!
423421
bid = digi->bid();
@@ -541,7 +539,7 @@ int DAQController::FitBaselines(std::vector<std::shared_ptr<V1724>> &digis,
541539
auto it = buffers[bid]->buff.begin();
542540
while (it < buffers[bid]->buff.end()) {
543541
if ((*it)>>28 == 0xA) {
544-
words = (*it)&0x7FFFFFFF;
542+
words = (*it)&0xFFFFFFF;
545543
std::u32string_view sv(buffers[bid]->buff.data() + std::distance(buffers[bid]->buff.begin(), it), words);
546544
std::tie(words_in_event, channel_mask, std::ignore, std::ignore) = d->UnpackEventHeader(sv);
547545
if (words == 4) {
@@ -612,11 +610,9 @@ int DAQController::FitBaselines(std::vector<std::shared_ptr<V1724>> &digis,
612610
// ****************************
613611
for (auto d : digis) {
614612
bid = d->bid();
615-
fMutex.lock();
616613
cal_values[bid] = std::map<std::string, vector<double>>(
617614
{{"slope", vector<double>(d->GetNumChannels())},
618615
{"yint", vector<double>(d->GetNumChannels())}});
619-
fMutex.unlock();
620616
for (unsigned ch = 0; ch < d->GetNumChannels(); ch++) {
621617
B = C = D = E = F = 0;
622618
for (unsigned i = 0; i < DAC_cal_points.size(); i++) {

DAQController.hh

+2-3
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,8 @@ private:
4242
void ReadData(int link);
4343
int OpenThreads();
4444
void CloseThreads();
45-
void InitLink(std::vector<std::shared_ptr<V1724>>&, std::map<int, std::map<std::string, std::vector<double>>>&, int&);
46-
int FitBaselines(std::vector<std::shared_ptr<V1724>>&, std::map<int, std::vector<uint16_t>>&, int,
47-
std::map<int, std::map<std::string, std::vector<double>>>&);
45+
void InitLink(std::vector<std::shared_ptr<V1724>>&, std::map<int, std::vector<uint16_t>>&, int&);
46+
int FitBaselines(std::vector<std::shared_ptr<V1724>>&, std::map<int, std::vector<uint16_t>>&, int);
4847

4948
std::vector<std::unique_ptr<StraxFormatter>> fFormatters;
5049
std::vector<std::thread> fProcessingThreads;

MongoLog.cc

+8-8
Original file line numberDiff line numberDiff line change
@@ -111,23 +111,23 @@ int MongoLog::Entry(int priority, std::string message, ...){
111111
va_end (args);
112112
message = &vec[0];
113113

114-
std::unique_lock<std::mutex> lg(fMutex);
115-
116114
auto t = std::time(nullptr);
117115
auto tm = *std::gmtime(&t);
118116
std::stringstream msg;
119117
msg<<FormatTime(&tm)<<" ["<<fPriorities[priority+1] <<"]: "<<message<<std::endl;
118+
std::unique_lock<std::mutex> lg(fMutex);
120119
std::cout << msg.str();
121120
if (Today(&tm) != fToday) RotateLogFile();
122121
fOutfile<<msg.str();
123122
if(priority >= fLogLevel){
124123
try{
125-
fMongoCollection.insert_one(bsoncxx::builder::stream::document{} <<
126-
"user" << fHostname <<
127-
"message" << message <<
128-
"priority" << priority <<
129-
"runid" << fRunId <<
130-
bsoncxx::builder::stream::finalize);
124+
auto d = bsoncxx::builder::stream::document{} <<
125+
"user" << fHostname <<
126+
"message" << message <<
127+
"priority" << priority <<
128+
"runid" << runid <<
129+
bxoncxx::builder::stream::finalize;
130+
fMongoCollection.insert_one(std::move(d));
131131
}
132132
catch(const std::exception &e){
133133
std::cout<<"Failed to insert log message "<<message<<" ("<<

Options.cc

+20-46
Original file line numberDiff line numberDiff line change
@@ -333,74 +333,48 @@ int Options::GetFaxOptions(fax_options_t& opts) {
333333
return 0;
334334
}
335335

336-
int Options::GetDAC(std::map<int, std::map<std::string, std::vector<double>>>& board_dacs,
337-
std::vector<int>& bids) {
338-
board_dacs.clear();
339-
std::map<std::string, std::vector<double>> defaults {
340-
{"slope", std::vector<double>(16, -0.2695)},
341-
{"yint", std::vector<double>(16, 17169)}};
342-
// let's provide a default
343-
board_dacs[-1] = defaults;
344-
std::map<std::string, std::vector<double>> this_board_dac{
345-
{"slope", std::vector<double>(16)},
346-
{"yint", std::vector<double>(16)}};
347-
int ret(0);
348-
auto sort_order = bsoncxx::builder::stream::document{} <<
349-
"_id" << -1 << bsoncxx::builder::stream::finalize;
336+
std::vector<uint16_t> Options::GetDAC(int bid, int num_chan, uint16_t default_value) {
337+
using namespace bsoncxx::builder::stream;
338+
std::vector<uint16_t> ret(num_chan, default_value);
339+
auto sort_order = document{} << "_id" << -1 << finalize;
340+
auto q = document{} << bid << open_document << "$exists" << 1 << close_document << finalize;
350341
auto opts = mongocxx::options::find{};
351342
opts.sort(sort_order.view());
352-
auto cursor = fDAC_collection.find({}, opts);
343+
auto cursor = fDAC_collection.find(q, opts);
353344
auto doc = cursor.begin();
354-
if (doc == cursor.end()) {
345+
if (doc == cursor.end() || doc->find(std::to_string(bid)) == doc->end()) {
355346
fLog->Entry(MongoLog::Local, "No baseline calibrations? You must be new");
356-
return -1;
347+
return ret;
357348
}
358349
/* doc should look like this:
359-
*{ run : 000042,
360-
* bid : {
361-
* slope : [ch0, ch1, ch2, ...],
362-
* yint : [ch0, ch1, ch2, ...],
363-
* },
350+
*{ run : 42,
351+
* bid : [ val, val, val, ...],
364352
* ...
365353
* }
366354
*/
367-
for (auto bid : bids) {
368-
if ((*doc).find(std::to_string(bid)) == (*doc).end()) {
369-
board_dacs[bid] = defaults;
370-
continue;
371-
}
372-
for (auto& kv : this_board_dac) { // (string, vector<double>)
373-
kv.second.clear();
374-
for(auto& val : (*doc)[std::to_string(bid)][kv.first].get_array().value)
375-
kv.second.push_back(val.get_double());
376-
}
377-
board_dacs[bid] = this_board_dac;
378-
}
355+
for (int i = 0; i < num_chan; i++)
356+
ret[i] = (*doc)[std::to_string(bid)][i];
379357
return ret;
380358
}
381359

382-
void Options::UpdateDAC(std::map<int, std::map<std::string, std::vector<double>>>& all_dacs){
360+
void Options::UpdateDAC(std::map<int, std::vector<uint16_t>>& all_dacs){
383361
using namespace bsoncxx::builder::stream;
384-
std::string run_id = GetString("run_identifier", "default");
362+
int run_id = GetInt("number", -1);
385363
fLog->Entry(MongoLog::Local, "Saving DAC calibration");
386364
auto search_doc = document{} << "run" << run_id << finalize;
387365
auto update_doc = document{};
388366
update_doc<< "$set" << open_document << "run" << run_id;
389-
for (auto& bid_map : all_dacs) { // (bid, map<string, vector>)
390-
update_doc << std::to_string(bid_map.first) << open_document;
391-
for(auto& str_vec : bid_map.second){ // (string, vector)
392-
update_doc << str_vec.first << open_array <<
393-
[&](array_context<> arr){
394-
for (auto& val : str_vec.second) arr << val;
395-
} << close_array;
396-
}
397-
update_doc << close_document;
367+
for (auto& bid_map : all_dacs) { // (bid, vector)
368+
update_doc << std::to_string(bid_map.first) << open_array <<
369+
[&](array_context<> arr){
370+
for (auto& val : bid_map.second) arr << val;
371+
} << close_array;
398372
}
399373
update_doc << close_document;
400374
auto write_doc = update_doc<<finalize;
401375
mongocxx::options::update options;
402376
options.upsert(true);
403-
fDAC_collection.update_one(search_doc.view(), write_doc.view(), options);
377+
fDAC_collection.update_one(std::move(search_doc), std::move(write_doc), options);
404378
return;
405379
}
406380

dispatcher/DAQController.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ def CheckTimeouts(self, detector, command = None):
333333
if command == 'stop':
334334
if self.error_stop_count[detector] >= self.stop_retries:
335335
# failed too many times, issue error
336-
self.mongo.LogError("dispatcher",
336+
self.mongo.LogError(
337337
("Dispatcher control loop detects a timeout that STOP " +
338338
"can't solve"),
339339
'ERROR',
@@ -344,7 +344,7 @@ def CheckTimeouts(self, detector, command = None):
344344
self.log.debug('Working on a stop counter for %s' % detector)
345345
self.error_stop_count[detector] += 1
346346
else:
347-
self.mongo.LogError("dispatcher",
347+
self.mongo.LogError(
348348
('%s took more than %i seconds to %s, indicating a possible timeout or error' %
349349
(detector, self.timeouts[command], command)),
350350
'ERROR',
@@ -358,7 +358,7 @@ def ThrowError(self):
358358
'''
359359
Throw a general error that the DAQ is stuck
360360
'''
361-
self.mongo.LogError("dispatcher",
361+
self.mongo.LogError(
362362
"Dispatcher control loop can't get DAQ out of stuck state",
363363
'ERROR',
364364
"GENERAL_ERROR")

0 commit comments

Comments
 (0)