@@ -77,18 +77,16 @@ int DAQController::Arm(std::shared_ptr<Options>& options){
77
77
// Seriously. This sleep statement is absolutely vital.
78
78
fLog ->Entry (MongoLog::Local, " That felt great, thanks." );
79
79
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 ());
83
82
std::map<int ,int > rets;
84
83
// Parallel digitizer programming to speed baselining
85
84
for ( auto & link : fDigitizers ) {
86
85
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 ]));
89
88
}
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 ();
92
90
93
91
if (std::any_of (rets.begin (), rets.end (), [](auto & p) {return p.second != 0 ;})) {
94
92
fLog ->Entry (MongoLog::Warning, " Encountered errors during digitizer programming" );
@@ -298,7 +296,8 @@ void DAQController::CloseThreads(){
298
296
}
299
297
300
298
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{};
302
301
std::map<int , int > retmap;
303
302
std::pair<long , long > buf{0 ,0 };
304
303
int rate = fDataRate ;
@@ -312,52 +311,53 @@ void DAQController::StatusUpdate(mongocxx::collection* collection) {
312
311
buf.second += x.second ;
313
312
}
314
313
}
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())<<
317
319
" rate" << rate/1e6 <<
318
320
" status" << fStatus <<
319
321
" 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){
323
326
for ( auto const & pair : retmap)
324
327
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&
327
334
return ;
328
335
}
329
336
330
337
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) {
332
339
std::string BL_MODE = fOptions ->GetString (" baseline_dac_mode" , " fixed" );
333
- std::map<int , std::vector<uint16_t >> dac_values;
334
340
int nominal_baseline = fOptions ->GetInt (" baseline_value" , 16000 );
341
+ int nominal_dac = fOptions ->GetInt (" baseline_fixed_value" , 4000 );
335
342
if (BL_MODE == " fit" ) {
336
- if ((ret = FitBaselines (digis, dac_values, nominal_baseline, cal_values ))) {
343
+ if ((ret = FitBaselines (digis, dac_values, nominal_baseline))) {
337
344
fLog ->Entry (MongoLog::Warning, " Errors during baseline fitting" );
338
345
return ;
339
346
}
340
347
}
341
348
342
- for (auto digi : digis){
349
+ for (auto & digi : digis){
343
350
fLog ->Entry (MongoLog::Local, " Board %i beginning specific init" , digi->bid ());
344
351
345
352
// Multiple options here
346
353
int bid = digi->bid (), success (0 );
347
354
if (BL_MODE == " fit" ) {
348
355
} 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);
353
357
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);
357
358
} 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);
361
361
} else {
362
362
fLog ->Entry (MongoLog::Warning, " Received unknown baseline mode '%s', valid options are \" fit\" , \" cached\" , and \" fixed\" " , BL_MODE.c_str ());
363
363
ret = -1 ;
@@ -381,12 +381,10 @@ void DAQController::InitLink(std::vector<std::shared_ptr<V1724>>& digis,
381
381
}
382
382
success += digi->LoadDAC (dac_values[bid]);
383
383
// 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 ());
386
385
387
386
fLog ->Entry (MongoLog::Local, " Board %i programmed" , digi->bid ());
388
387
if (success!=0 ){
389
- // LOG
390
388
fLog ->Entry (MongoLog::Warning, " Failed to configure digitizers." );
391
389
ret = -1 ;
392
390
return ;
@@ -398,8 +396,7 @@ void DAQController::InitLink(std::vector<std::shared_ptr<V1724>>& digis,
398
396
}
399
397
400
398
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) {
403
400
using std::vector;
404
401
using namespace std ::chrono_literals;
405
402
int max_iter = fOptions ->GetInt (" baseline_max_iterations" , 2 );
@@ -418,6 +415,7 @@ int DAQController::FitBaselines(std::vector<std::shared_ptr<V1724>> &digis,
418
415
std::map<int , int > words_read;
419
416
std::map<int , vector<vector<double >>> bl_per_channel;
420
417
std::map<int , vector<int >> diff;
418
+ std::map<int , std::map<std::string, vector<double >>> cal_values;
421
419
422
420
for (auto digi : digis) { // alloc ALL the things!
423
421
bid = digi->bid ();
@@ -541,7 +539,7 @@ int DAQController::FitBaselines(std::vector<std::shared_ptr<V1724>> &digis,
541
539
auto it = buffers[bid]->buff .begin ();
542
540
while (it < buffers[bid]->buff .end ()) {
543
541
if ((*it)>>28 == 0xA ) {
544
- words = (*it)&0x7FFFFFFF ;
542
+ words = (*it)&0xFFFFFFF ;
545
543
std::u32string_view sv (buffers[bid]->buff .data () + std::distance (buffers[bid]->buff .begin (), it), words);
546
544
std::tie (words_in_event, channel_mask, std::ignore, std::ignore) = d->UnpackEventHeader (sv);
547
545
if (words == 4 ) {
@@ -612,11 +610,9 @@ int DAQController::FitBaselines(std::vector<std::shared_ptr<V1724>> &digis,
612
610
// ****************************
613
611
for (auto d : digis) {
614
612
bid = d->bid ();
615
- fMutex .lock ();
616
613
cal_values[bid] = std::map<std::string, vector<double >>(
617
614
{{" slope" , vector<double >(d->GetNumChannels ())},
618
615
{" yint" , vector<double >(d->GetNumChannels ())}});
619
- fMutex .unlock ();
620
616
for (unsigned ch = 0 ; ch < d->GetNumChannels (); ch++) {
621
617
B = C = D = E = F = 0 ;
622
618
for (unsigned i = 0 ; i < DAC_cal_points.size (); i++) {
0 commit comments