Skip to content

Commit a38e2cf

Browse files
author
Karl Smith
committed
Updated scope program. (Added save feature).
The scope program now saves the next trace after typing save <fileName>. The trace is saved as a ROOT file. Also, updated the documentation and the naming standard in compliance with issues #3 and #4.
1 parent 2a02fa3 commit a38e2cf

2 files changed

Lines changed: 72 additions & 71 deletions

File tree

Poll/include/scope.hpp

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,30 @@ class TH2F;
1414

1515
class Oscilloscope : public Unpacker{
1616
private:
17-
int mod; /// The module of the signal of interest.
18-
int chan; /// The channel of the signal of interest.
17+
int mod_; ///< The module of the signal of interest.
18+
int chan_; ///< The channel of the signal of interest.
1919

2020
float old_maximum; /// The maximum value of the largest trace.
2121

2222
bool need_graph_update; /// Set to true if the graph range needs updated.
2323

24-
int delay; /// The number of seconds to wait between drawing traces.
24+
int delay_; /// The number of seconds to wait between drawing traces.
2525

26-
time_t last_trace; /// The time of the last trace.
26+
time_t last_trace; ///< The time of the last trace.
2727

28-
unsigned int num_traces; /// The total number of traces.
28+
unsigned int num_traces; ///< The total number of traces.
2929

30-
unsigned int num_displayed; /// The number of displayed traces.
30+
unsigned int num_displayed; ///< The number of displayed traces.
3131

32-
std::vector<int> x_vals; /// The x-axis values of the trace.
32+
std::vector<int> x_vals; ///< The x-axis values of the trace.
33+
34+
std::string saveFile_; ///< The name of the file to save a trace.
3335

3436
TApplication* rootapp;
3537

36-
TCanvas *canvas; /// The main plotting canvas.
38+
TCanvas *canvas; ///< The main plotting canvas.
3739

38-
TGraph *graph; /// The TGraph for plotting traces.
40+
TGraph *graph; ///< The TGraph for plotting traces.
3941

4042
TH2F *his; /// Dummy histogram for updating the plotting ranges.
4143

@@ -50,26 +52,24 @@ class Oscilloscope : public Unpacker{
5052
void ProcessRawEvent();
5153

5254
public:
53-
Oscilloscope();
54-
55-
Oscilloscope(int mod_, int chan_);
55+
Oscilloscope(int mod = 0, int chan = 0);
5656

5757
~Oscilloscope();
5858

5959
bool Initialize(std::string prefix_="");
6060

61-
int GetMod(){ return mod; }
61+
int GetMod(){ return mod_; }
6262

63-
int GetChan(){ return chan; }
63+
int GetChan(){ return chan_; }
6464

65-
int GetDelay(){ return delay; }
65+
int GetDelay(){ return delay_; }
6666

67-
void SetMod(int mod_){ mod = mod_; }
67+
void SetMod(int mod){ mod_ = mod; }
6868

69-
void SetChan(int chan_){ chan = chan_; }
69+
void SetChan(int chan){ chan_ = chan; }
7070

7171
/// Set the number of seconds to wait between drawing of traces.
72-
void SetDelay(int delay_){ delay = (delay_>=1)?delay_:1; }
72+
void SetDelay(int delay){ delay_ = (delay>1)?delay:1; }
7373

7474
/// Return the syntax string for this program.
7575
void SyntaxStr(const char *name_, std::string prefix_=""){ std::cout << prefix_ << "SYNTAX: " << std::string(name_) << " <options> <input>\n"; }
@@ -86,8 +86,9 @@ class Oscilloscope : public Unpacker{
8686
/// Print a status message.
8787
void PrintStatus(std::string prefix_=""){ std::cout << prefix_ << "Found " << num_traces << " traces and displayed " << num_displayed << ".\n"; }
8888

89-
/** Search for an input command and perform the desired action. Return
90-
* true if the command is valid and false otherwise.
89+
/** Search for an input command and perform the desired action.
90+
*
91+
* \return True if the command is valid and false otherwise.
9192
*/
9293
bool CommandControl(std::string cmd_, const std::vector<std::string> &args_);
9394
};

Poll/source/scope.cpp

Lines changed: 51 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
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+
*/
171155
void 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+
*/
177164
void 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+
*/
184175
bool 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

212203
bool 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

Comments
 (0)