Skip to content

Commit 9210485

Browse files
committed
Added override to set collection option
1 parent 4ba9ada commit 9210485

5 files changed

Lines changed: 108 additions & 37 deletions

File tree

DAQController.cc

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,17 @@ std::string DAQController::run_mode(){
3939
}
4040
}
4141

42-
int DAQController::InitializeElectronics(std::string opts){
42+
int DAQController::InitializeElectronics(std::string opts, std::string override){
4343

44+
// Load options including override if any
4445
if(fOptions != NULL)
4546
delete fOptions;
4647
fOptions = new Options(opts);
48+
if(override!=""){
49+
fOptions->Override(bsoncxx::from_json(override).view());
50+
}
51+
52+
// Initialize digitizers
4753
fStatus = 1;
4854
for(auto d : fOptions->GetBoards("V1724")){
4955

@@ -63,6 +69,7 @@ int DAQController::InitializeElectronics(std::string opts){
6369
}
6470
}
6571

72+
// Load registers into digitizers
6673
for(auto digi : fDigitizers){
6774
int success=0;
6875
for(auto regi : fOptions->GetRegisters(digi->bid())){
@@ -77,9 +84,14 @@ int DAQController::InitializeElectronics(std::string opts){
7784
return -1;
7885
}
7986
}
87+
88+
// Look at this later! This initializes all boards to SW controlled
89+
// and inactive. Will need option for HW control.
8090
for(unsigned int x=0;x<fDigitizers.size();x++)
8191
fDigitizers[x]->WriteRegister(0x8100, 0x0);
8292
fStatus = 2;
93+
94+
std::cout<<fOptions->ExportToString()<<std::endl;
8395
return 0;
8496
}
8597

DAQController.hh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public:
2525
DAQController(MongoLog *log=NULL);
2626
~DAQController();
2727

28-
int InitializeElectronics(string opts);
28+
int InitializeElectronics(std::string opts, std::string override = "");
2929

3030
// Get data (return new buffer and size)
3131
double data_rate(){

Options.cc

Lines changed: 56 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,7 @@ std::string Options::ExportToString(){
4646

4747
int Options::Load(std::string opts){
4848
try{
49-
// Holy cow. So the whole pointer thing is some lengths I went to to
50-
// keep the stupid value in scope. You can't have a member variable with
51-
// a non-pointer value since there is no way to initialize a default 'value'.
52-
// So needs to be a pointer. Needs to stay in scope for as long as you might
49+
// This needs to be a pointer. Needs to stay in scope for as long as you might
5350
// want to see the view.
5451
bson_value = new bsoncxx::document::value(bsoncxx::from_json(opts).view());
5552
bson_options = bson_value->view();
@@ -65,27 +62,73 @@ int Options::Load(std::string opts){
6562
return 0;
6663
}
6764

65+
int Options::Override(bsoncxx::document::view override_opts){
66+
67+
// Here's the best way I can find to do this. We create a new doc, which
68+
// is a concatenation of the two old docs (original first). Then we
69+
// use the concatenation object to initialize a 'new' value for the
70+
// combined doc and delete the orginal. A new view will point to the new value.
71+
72+
using bsoncxx::builder::stream::document;
73+
using bsoncxx::builder::stream::finalize;
74+
75+
//using bsoncxx::builder::concatenate;
76+
77+
auto doc = document{};
78+
bsoncxx::document::value *new_value = new bsoncxx::document::value
79+
( document{}<<bsoncxx::builder::concatenate_doc{bson_options}<<
80+
"override_doc" << bsoncxx::builder::stream::open_document<<
81+
bsoncxx::builder::concatenate_doc{override_opts}<<
82+
bsoncxx::builder::stream::close_document<<
83+
finalize);
84+
//builder<<concatenate(bson_options);
85+
//builder<<concatenate(override_opts);
86+
//doc<<finalize;
87+
88+
// Create a new doc
89+
//bsoncxx::document::value *new_value = new bsoncxx::document::value(doc.extract());
90+
91+
// Delete the original
92+
delete bson_value;
93+
94+
// Set to new
95+
bson_value = new_value;
96+
bson_options = bson_value->view();
97+
98+
return 0;
99+
}
100+
68101
int Options::GetInt(std::string path, int default_value){
69102

70103
try{
71-
return bson_options[path.c_str()].get_int32();
104+
return bson_options["override_doc"][path.c_str()].get_int32();
72105
}
73106
catch (const std::exception &e){
74-
//LOG
75-
std::cout<<e.what()<<std::endl;
76-
return default_value;
107+
try{
108+
return bson_options[path.c_str()].get_int32();
109+
}
110+
catch (const std::exception &e){
111+
//LOG
112+
std::cout<<e.what()<<std::endl;
113+
return default_value;
114+
}
77115
}
78116
return -1;
79117
}
80118

81119
std::string Options::GetString(std::string path, std::string default_value){
82120
try{
83-
return bson_options[path.c_str()].get_utf8().value.to_string();
121+
return bson_options["override_doc"][path.c_str()].get_utf8().value.to_string();
84122
}
85-
catch (const std::exception &e){
86-
//LOG
87-
std::cout<<e.what()<<std::endl;
88-
return default_value;
123+
catch(const std::exception &e){
124+
try{
125+
return bson_options[path.c_str()].get_utf8().value.to_string();
126+
}
127+
catch (const std::exception &e){
128+
//LOG
129+
std::cout<<e.what()<<std::endl;
130+
return default_value;
131+
}
89132
}
90133
return "";
91134
}

Options.hh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <bsoncxx/json.hpp>
1313
#include <bsoncxx/document/view.hpp>
1414
#include <bsoncxx/document/value.hpp>
15+
#include <bsoncxx/builder/stream/document.hpp>
1516
#include <bsoncxx/exception/exception.hpp>
1617
#include "DAXHelpers.hh"
1718

@@ -46,6 +47,8 @@ public:
4647
std::vector<RegisterType> GetRegisters(int board=-1);
4748

4849
std::string ExportToString();
50+
int Override(bsoncxx::document::view override_opts);
51+
4952
private:
5053
std::string defaultPath = "defaults/daxOptions.json";
5154
DAXHelpers *fHelper;

main.cc

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -112,39 +112,52 @@ int main(int argc, char** argv){
112112
// Can only arm if we're in the idle, arming, or armed state
113113
if(controller->status() == 0 || controller->status() == 1 || controller->status() == 2){
114114
controller->End();
115+
116+
// Try to pull options from database and store in an 'optional' object
115117
string options = "";
118+
bsoncxx::stdx::optional<bsoncxx::document::value> trydoc;
116119
try{
117120
string option_name = doc["mode"].get_utf8().value.to_string();
118121
logger->Entry("Loading options " + option_name, MongoLog::Debug);
119122

120-
bsoncxx::stdx::optional<bsoncxx::document::value> trydoc =
121-
options_collection.find_one(bsoncxx::builder::stream::document{}<<
122-
"name" << option_name.c_str() <<
123-
bsoncxx::builder::stream::finalize);
123+
trydoc = options_collection.find_one(bsoncxx::builder::stream::document{}<<
124+
"name" << option_name.c_str() <<
125+
bsoncxx::builder::stream::finalize);
124126
logger->Entry("Received arm command from user "+
125127
doc["user"].get_utf8().value.to_string() +
126128
" for mode " + option_name, MongoLog::Message);
127-
if(trydoc){
128-
options = bsoncxx::to_json(*trydoc);
129-
if(controller->InitializeElectronics(options)!=0){
130-
logger->Entry("Failed to initialized electronics", MongoLog::Error);
131-
}
132-
else{
133-
logger->Entry("Initialized electronics", MongoLog::Debug);
134-
}
135-
136-
if(readoutThread!=NULL){
137-
logger->Entry("Cannot start DAQ while readout thread from previous run active. Please perform a reset", MongoLog::Message);
138-
}
139-
else
140-
readoutThread = new std::thread(DAQController::ReadThreadWrapper,
141-
(static_cast<void*>(controller)));
142-
}
143129
}
144-
catch( const std::exception &e){
130+
catch(const std::exception &e){
145131
logger->Entry("Want to arm boards but no valid mode provided", MongoLog::Warning);
146-
options = "";
132+
options = "";
133+
}
134+
135+
// Get an override doc from the 'options_override' field if it exists
136+
std::string override_json = "";
137+
try{
138+
bsoncxx::document::view oopts = doc["options_override"].get_document().view();
139+
override_json = bsoncxx::to_json(oopts);
140+
}
141+
catch(const std::exception &e){
142+
logger->Entry("No override options provided, continue without.", MongoLog::Debug);
147143
}
144+
145+
if(trydoc){
146+
options = bsoncxx::to_json(*trydoc);
147+
if(controller->InitializeElectronics(options, override_json)!=0){
148+
logger->Entry("Failed to initialized electronics", MongoLog::Error);
149+
}
150+
else{
151+
logger->Entry("Initialized electronics", MongoLog::Debug);
152+
}
153+
154+
if(readoutThread!=NULL){
155+
logger->Entry("Cannot start DAQ while readout thread from previous run active. Please perform a reset", MongoLog::Message);
156+
}
157+
else
158+
readoutThread = new std::thread(DAQController::ReadThreadWrapper,
159+
(static_cast<void*>(controller)));
160+
}
148161
}
149162
else
150163
logger->Entry("Cannot arm DAQ while not 'Idle'", MongoLog::Warning);

0 commit comments

Comments
 (0)