1414#include " TGraph.h"
1515#include " TAxis.h"
1616#include " TH2F.h"
17+ #include " TFile.h"
1718
1819#define ADC_TIME_STEP 4 // ns
1920
@@ -33,7 +34,7 @@ void Oscilloscope::UpdateGraph(int size_){
3334 }
3435
3536 std::stringstream stream;
36- stream << " mod = " << mod << " , chan = " << chan ;
37+ stream << " mod = " << mod_ << " , chan = " << chan_ ;
3738 his->SetTitle (stream.str ().c_str ());
3839
3940 need_graph_update = false ;
@@ -50,16 +51,15 @@ void Oscilloscope::Plot(ChannelEvent *event_){
5051 time (&cur_time);
5152
5253 // Draw the trace
53- if ((int )difftime (cur_time, last_trace) >= delay ){
54+ if ((int )difftime (cur_time, last_trace) >= delay_ ){
5455 event_->CorrectBaseline ();
5556
5657 if (need_graph_update || event_->trace .size () != x_vals.size ()){ // The length of the trace has changed.
5758 UpdateGraph (event_->trace .size ());
5859 }
5960
6061 int index = 0 ;
61- std::vector<int >::iterator iterx, itery;
62- for (iterx = x_vals.begin (), itery = event_->trace .begin (); iterx != x_vals.end () && itery != event_->trace .end (); iterx++, itery++){
62+ for (auto iterx = x_vals.begin (), itery = event_->trace .begin (); iterx != x_vals.end () && itery != event_->trace .end (); iterx++, itery++){
6363 graph->SetPoint (index++, *iterx, *itery);
6464 }
6565
@@ -70,10 +70,17 @@ void Oscilloscope::Plot(ChannelEvent *event_){
7070 graph->Draw (" PCSAME" );
7171 canvas->Update ();
7272
73+ if (saveFile_ != " " ) {
74+ TFile f (saveFile_.c_str (), " RECREATE" );
75+ graph->Clone (" trace" )->Write ();
76+ f.Close ();
77+ saveFile_ = " " ;
78+ }
79+
7380 time (&last_trace);
7481 num_displayed++;
7582 }
76-
83+
7784 num_traces++;
7885}
7986
@@ -85,49 +92,24 @@ void Oscilloscope::ProcessRawEvent(){
8592 current_event = rawEvent.front ();
8693
8794 // Pass this event to the correct processor
88- if (current_event->modNum == mod && current_event->chanNum == chan){ Plot (current_event); } // This is a signal we wish to plot.
95+ if (current_event->modNum == mod_ && current_event->chanNum == chan_){
96+ // This is a signal we wish to plot.
97+ Plot (current_event);
98+ }
8999
90100 // Remove this event from the raw event deque
91101 delete current_event;
92102 rawEvent.pop_front ();
93103 }
94104}
95105
96- Oscilloscope::Oscilloscope (){
97- mod = 0 ;
98- chan = 0 ;
99- delay = 2 ;
100- num_traces = 0 ;
101- num_displayed = 0 ;
102- time (&last_trace);
103-
104- old_maximum = -9999 ;
105-
106- // Variables for root graphics
107- rootapp = new TApplication (" scope" , 0 , NULL );
108- gSystem ->Load (" libTree" );
109-
110- canvas = new TCanvas (" scope_canvas" , " Oscilloscope" );
111- canvas->cd ();
112-
113- graph = new TGraph ();
114-
115- // Setup the default histogram frame.
116- his = new TH2F (" his" , " " , 1 , 0 , 1 , 1 , 0 , 1 );
117- his->SetStats (false );
118- his->GetYaxis ()->SetTitleOffset (1.4 );
119- his->GetXaxis ()->SetTitle (" Time (ns)" );
120- his->GetYaxis ()->SetTitle (" ADC Channel (a.u.)" );
121-
122- need_graph_update = false ;
123- }
124-
125- Oscilloscope::Oscilloscope (int mod_, int chan_){
126- mod = mod_;
127- chan = chan_;
128- delay = 2 ;
129- num_traces = 0 ;
130- num_displayed = 0 ;
106+ Oscilloscope::Oscilloscope (int mod /* = 0*/ , int chan/* =0*/ ) :
107+ mod_(mod),
108+ chan_(chan),
109+ delay_(2 ),
110+ num_traces(0 ),
111+ num_displayed(0 )
112+ {
131113 time (&last_trace);
132114
133115 old_maximum = -9999 ;
@@ -162,25 +144,34 @@ bool Oscilloscope::Initialize(std::string prefix_){
162144 if (init){ return false ; }
163145
164146 // Print a small welcome message.
165- std::cout << prefix_ << " Displaying traces for mod = " << mod << " , chan = " << chan << " .\n " ;
147+ std::cout << prefix_ << " Displaying traces for mod = " << mod_ << " , chan = " << chan_ << " .\n " ;
166148
167149 return (init = true );
168150}
169151
170- // / Print a command line help dialogue for recognized command line arguments.
152+ /* *
153+ * \param[in] prefix_
154+ */
171155void Oscilloscope::ArgHelp (std::string prefix_){
172156 std::cout << prefix_ << " --mod [module] | Module of signal of interest (default=0)\n " ;
173157 std::cout << prefix_ << " --chan [channel] | Channel of signal of interest (default=0)\n " ;
174158}
175159
176- // / Print an in-terminal help dialogue for recognized commands.
160+ /* *
161+ *
162+ * \param[in] prefix_
163+ */
177164void Oscilloscope::CmdHelp (std::string prefix_){
178165 std::cout << prefix_ << " set [module] [channel] - Set the module and channel of signal of interest (default = 0, 0).\n " ;
179166 std::cout << prefix_ << " delay [time] - Set the delay between drawing traces (in seconds, default = 1 s).\n " ;
180167 std::cout << prefix_ << " log - Toggle log/linear mode on the y-axis.\n " ;
168+ std::cout << prefix_ << " save <fileName> - Save the next trace to the specified file name..\n " ;
181169}
182170
183- // / Scan input arguments and set class variables.
171+ /* *
172+ * \param args_
173+ * \param filename_
174+ */
184175bool Oscilloscope::SetArgs (std::deque<std::string> &args_, std::string &filename_){
185176 std::string current_arg;
186177 while (!args_.empty ()){
@@ -192,15 +183,15 @@ bool Oscilloscope::SetArgs(std::deque<std::string> &args_, std::string &filename
192183 std::cout << " Error: Missing required argument to option '--mod'!\n " ;
193184 return false ;
194185 }
195- mod = atoi (args_.front ().c_str ());
186+ mod_ = atoi (args_.front ().c_str ());
196187 args_.pop_front ();
197188 }
198189 else if (current_arg == " --chan" ){
199190 if (args_.empty ()){
200191 std::cout << " Error: Missing required argument to option '--chan'!\n " ;
201192 return false ;
202193 }
203- chan = atoi (args_.front ().c_str ());
194+ chan_ = atoi (args_.front ().c_str ());
204195 args_.pop_front ();
205196 }
206197 else { filename_ = current_arg; }
@@ -211,9 +202,9 @@ bool Oscilloscope::SetArgs(std::deque<std::string> &args_, std::string &filename
211202
212203bool Oscilloscope::CommandControl (std::string cmd_, const std::vector<std::string> &args_){
213204 if (cmd_ == " set" ){ // Toggle debug mode
214- if (args_.size () > = 2 ){
215- mod = atoi (args_.at (0 ).c_str ());
216- chan = atoi (args_.at (1 ).c_str ());
205+ if (args_.size () = = 2 ){
206+ mod_ = atoi (args_.at (0 ).c_str ());
207+ chan_ = atoi (args_.at (1 ).c_str ());
217208 need_graph_update = true ;
218209 old_maximum = -9999 ;
219210 }
@@ -222,8 +213,17 @@ bool Oscilloscope::CommandControl(std::string cmd_, const std::vector<std::strin
222213 std::cout << message_head << " -SYNTAX- set [module] [channel]\n " ;
223214 }
224215 }
216+ else if (cmd_ == " save" ) {
217+ if (args_.size () == 1 ) {
218+ saveFile_ = args_.at (0 );
219+ }
220+ else {
221+ std::cout << message_head << " Invalid number of parameters to 'save'\n " ;
222+ std::cout << message_head << " -SYNTAX- save <fileName>\n " ;
223+ }
224+ }
225225 else if (cmd_ == " delay" ){
226- if (args_.size () >= 2 ){ delay = atoi (args_.at (0 ).c_str ()); }
226+ if (args_.size () >= 2 ){ delay_ = atoi (args_.at (0 ).c_str ()); }
227227 else {
228228 std::cout << message_head << " Invalid number of parameters to 'delay'\n " ;
229229 std::cout << message_head << " -SYNTAX- delay [time]\n " ;
0 commit comments