Skip to content

Commit 7d748f9

Browse files
committed
Added get_traces command to poll2
Added "get_traces" command which should behave in a similar way to the old stand-alone get_traces exe. Added new GetTraces class to PixieSupport. This class is used by the get_traces command.
1 parent c667c8f commit 7d748f9

4 files changed

Lines changed: 325 additions & 42 deletions

File tree

Interface/include/PixieSupport.h

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,27 @@
33

44
#include <string>
55
#include <sstream>
6+
#include <bitset>
67

78
#include "PixieInterface.h"
89

910
extern bool hasColorTerm;
1011

1112
template<typename T=int>
1213
struct PixieFunctionParms{
13-
PixieInterface *pif;
14-
unsigned int mod;
15-
unsigned int ch;
16-
T par;
14+
PixieInterface *pif;
15+
unsigned int mod;
16+
unsigned int ch;
17+
T par;
1718

18-
PixieFunctionParms(PixieInterface *p, T x) : pif(p) {par=x;}
19+
PixieFunctionParms(PixieInterface *p, T x) : pif(p) {par=x;}
1920
};
2021

2122
template<typename T=int>
2223
class PixieFunction : public std::unary_function<bool, struct PixieFunctionParms<T> >{
23-
public:
24-
virtual bool operator()(struct PixieFunctionParms<T> &par) = 0;
25-
virtual ~PixieFunction() {};
24+
public:
25+
virtual bool operator()(struct PixieFunctionParms<T> &par) = 0;
26+
virtual ~PixieFunction() {};
2627
};
2728

2829
template<typename T>
@@ -129,6 +130,41 @@ class BitFlipper : public PixieFunction<std::string>{
129130
bool Test(unsigned int num_bits_, unsigned int input_, const std::string *text_=NULL);
130131
};
131132

133+
class GetTraces : public PixieFunction<int>{
134+
private:
135+
unsigned short *total_data; /// Array for storing all 16 channels of a module.
136+
unsigned short *trace_data; /// Array for storing a single trace.
137+
138+
int threshold; /// The trigger threshold above baseline.
139+
140+
size_t total_len; /// Length of total data array.
141+
size_t trace_len; /// Length of trace data array.
142+
143+
int attempts; /// Number of attempts to reach threshold.
144+
float baseline[NUMBER_OF_CHANNELS]; /// Calculated baseline for each channel.
145+
float maximum[NUMBER_OF_CHANNELS]; /// The maximum ADC value above baseline for each channel.
146+
bool status; /// Set to true when a valid trace is found.
147+
bool correct_baselines; /// Correct the baselines of the output traces.
148+
149+
/// Fit... tau?
150+
double FitTau(const unsigned short* trace, size_t b0, size_t b1, size_t x0, size_t x1);
151+
152+
public:
153+
GetTraces(unsigned short* total_data_, size_t total_size_, unsigned short *trace_data_, size_t trace_size_, int threshold_=0, bool correct_baselines_=false);
154+
155+
int GetAttempts(){ return attempts; }
156+
157+
float GetBaseline(size_t chan_){ return (chan_<NUMBER_OF_CHANNELS?baseline[chan_]:-1); }
158+
159+
float GetMaximum(size_t chan_){ return (chan_<NUMBER_OF_CHANNELS?maximum[chan_]:-1); }
160+
161+
bool GetStatus(){ return status; }
162+
163+
void Help();
164+
165+
bool operator()(PixieFunctionParms<int> &par);
166+
};
167+
132168
class ParameterChannelWriter : public PixieFunction< std::pair<std::string, double> >{
133169
public:
134170
bool operator()(PixieFunctionParms< std::pair<std::string, double> > &par);

Interface/source/PixieSupport.cpp

Lines changed: 161 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <iostream>
22
#include <iomanip>
33
#include <stdlib.h>
4+
#include <cmath>
45

56
#include "Display.h"
67

@@ -60,9 +61,9 @@ const std::string BitFlipper::csr_txt[19] = {"Respond to group triggers only", "
6061
"Trigger positive", "GFLT", "", "", "", "", "", "", "", "HI/LO gain", "", "", "", ""};
6162
#else
6263
const std::string BitFlipper::toggle_names[19] = {"", "", "good", "", "", "polarity", "", "", "trace", "QDC", "CFD",
63-
"global", "raw", "trigger", "gain", "pileup", "catcher", "", "SHE"};
64+
"global", "raw", "trigger", "gain", "pileup", "catcher", "", "SHE"};
6465
const std::string BitFlipper::csr_txt[19] = {"", "", "Good Channel", "", "", "Trigger positive", "", "", "Enable trace capture", "Enable QDC sums capture",
65-
"Enable CFD trigger mode", "Enable global trigger validation", "Enable raw energy sums capture",
66+
"Enable CFD trigger mode", "Enable global trigger validation", "Enable raw energy sums capture",
6667
"Enable channel trigger validation", "HI/LO gain", "Pileup rejection control", "Hybrid bit", "",
6768
"SHE single trace capture"};
6869
#endif
@@ -85,11 +86,11 @@ void BitFlipper::SetCSRAbit(std::string bit_){
8586
SetBit(atoi(bit_.c_str()));
8687

8788
for(unsigned int i = 0; i < num_toggle_bits; i++){
88-
if(bit_ == toggle_names[i]){
89-
SetBit(i);
90-
break;
91-
}
92-
}
89+
if(bit_ == toggle_names[i]){
90+
SetBit(i);
91+
break;
92+
}
93+
}
9394
}
9495

9596
void BitFlipper::SetBit(std::string bit_){
@@ -126,8 +127,8 @@ bool BitFlipper::Test(unsigned int num_bits_, unsigned int input_, const std::st
126127
}
127128

128129
std::cout << " Input: 0x" << std::hex << input_ << " (" << std::dec << input_ << ")\n";
129-
if(text_ != NULL){ std::cout << " Bit On? Value Total Bit Function\n"; }
130-
else{ std::cout << " Bit On? Value Total\n"; }
130+
if(text_ != NULL){ std::cout << " Bit On? Value Total Bit Function\n"; }
131+
else{ std::cout << " Bit On? Value Total\n"; }
131132

132133
std::string bit_function;
133134
for(unsigned int i = 0; i < num_bits_; i++){
@@ -137,26 +138,26 @@ bool BitFlipper::Test(unsigned int num_bits_, unsigned int input_, const std::st
137138
if(active_bits[i]){
138139
if(Display::hasColorTerm){
139140
if(i < 10){
140-
std::cout << TermColors::DkGreen << " 0" << i << " 1 " << PadStr(bit_values[i], 12);
141+
std::cout << TermColors::DkGreen << " 0" << i << " 1 " << PadStr(bit_values[i], 12);
141142
std::cout << PadStr(running_total[i], 12) << bit_function << TermColors::Reset << std::endl;
142143
}
143144
else{
144-
std::cout << TermColors::DkGreen << " " << i << " 1 " << PadStr(bit_values[i], 12);
145+
std::cout << TermColors::DkGreen << " " << i << " 1 " << PadStr(bit_values[i], 12);
145146
std::cout << PadStr(running_total[i], 12) << bit_function << TermColors::Reset << std::endl;
146147
}
147148
}
148149
else{
149-
if(i < 10){ std::cout << " " << i << " 1 " << PadStr(bit_values[i], 12) << PadStr(running_total[i], 12) << bit_function << std::endl; }
150-
else{ std::cout << " " << i << " 1 " << PadStr(bit_values[i], 12) << PadStr(running_total[i], 12) << bit_function << std::endl; }
150+
if(i < 10){ std::cout << " " << i << " 1 " << PadStr(bit_values[i], 12) << PadStr(running_total[i], 12) << bit_function << std::endl; }
151+
else{ std::cout << " " << i << " 1 " << PadStr(bit_values[i], 12) << PadStr(running_total[i], 12) << bit_function << std::endl; }
151152
}
152153
}
153154
else{
154155
if(i < 10){
155-
std::cout << " 0" << i << " 0 " << PadStr(bit_values[i], 12);
156+
std::cout << " 0" << i << " 0 " << PadStr(bit_values[i], 12);
156157
std::cout << PadStr(running_total[i], 12) << bit_function << std::endl;
157158
}
158159
else{
159-
std::cout << " " << i << " 0 " << PadStr(bit_values[i], 12);
160+
std::cout << " " << i << " 0 " << PadStr(bit_values[i], 12);
160161
std::cout << PadStr(running_total[i], 12) << bit_function << std::endl;
161162
}
162163
}
@@ -169,6 +170,151 @@ bool BitFlipper::Test(unsigned int num_bits_, unsigned int input_, const std::st
169170
return true;
170171
}
171172

173+
double GetTraces::FitTau(const unsigned short* trace, size_t b0, size_t b1, size_t x0, size_t x1){
174+
double baseline = 0;
175+
for (unsigned i = b0; i < b1; ++i)
176+
baseline += trace[i];
177+
baseline = baseline / double(b1 - b0);
178+
179+
//calculate logarithm
180+
std::vector<double> logtrace;
181+
for (size_t i = x0; i < x1; ++i) {
182+
double val = trace[i] - baseline;
183+
if (val > 0)
184+
logtrace.push_back( std::log(val) );
185+
else
186+
logtrace.push_back( -6.66E-6 ); //such an evil number!
187+
}
188+
189+
// fit a line
190+
double S = (x1 - x0);
191+
double Sx = ( std::pow(x1,2) - x1 - std::pow(x0,2) + x0) / 2.0;
192+
double Sxx = ( 2.0 * std::pow(x1,3) - 3.0 * std::pow(x1,2) + x1
193+
-2.0 * std::pow(x0,3) + 3.0 * std::pow(x0,2) - x0) / 6.0;
194+
double Sy = 0;
195+
double Sxy = 0;
196+
197+
unsigned sz = logtrace.size();
198+
for (unsigned i = 0; i < sz; i++) {
199+
Sy += logtrace[i];
200+
Sxy += logtrace[i] * i;
201+
}
202+
double D = S * Sxx - pow(Sx,2);
203+
double a1 = (S * Sxy - Sx * Sy) / D;
204+
//double a0 = (Sxx * Sy - Sx * Sxy) / D;
205+
206+
if ( a1 != 0.0)
207+
return (-1.0 / a1);
208+
else
209+
return -6.66e6;
210+
}
211+
212+
GetTraces::GetTraces(unsigned short* total_data_, size_t total_size_, unsigned short *trace_data_, size_t trace_size_, int threshold_/*=0*/, bool correct_baselines_/*=false*/){
213+
total_data = total_data_;
214+
total_len = total_size_;
215+
trace_data = trace_data_;
216+
trace_len = trace_size_;
217+
threshold = threshold_;
218+
correct_baselines = correct_baselines_;
219+
220+
attempts = 0;
221+
status = false;
222+
223+
// Set initial values.
224+
for (unsigned int i = 0; i < NUMBER_OF_CHANNELS; i++) {
225+
baseline[i] = -1;
226+
maximum[i] = -9999;
227+
}
228+
}
229+
230+
void GetTraces::Help(){
231+
}
232+
233+
bool GetTraces::operator()(PixieFunctionParms<int> &par){
234+
float temp_val;
235+
236+
// Reset parameters.
237+
for (unsigned int i = 0; i < NUMBER_OF_CHANNELS; i++) {
238+
baseline[i] = -1;
239+
maximum[i] = -9999;
240+
}
241+
status = false;
242+
243+
size_t baselineSamples = trace_len / 10;
244+
unsigned long sum;
245+
246+
// Get a single channel's trace.
247+
// Try to find a pulse above the threshold.
248+
for(attempts = 1; attempts <= 100; attempts++){
249+
par.pif->AcquireTraces(par.mod); // Acquire new traces.
250+
usleep(10);
251+
252+
if (par.pif->ReadSglChanTrace(trace_data, trace_len, par.mod, par.ch)) {
253+
// Reset the parameters for the trigger channel.
254+
baseline[par.ch] = -1;
255+
maximum[par.ch] = -9999;
256+
257+
// Calculate the channel baseline.
258+
sum = 0;
259+
260+
for (unsigned i = 0; i < baselineSamples; i++)
261+
sum += trace_data[i];
262+
263+
baseline[par.ch] = float(sum)/float(baselineSamples);
264+
265+
// Correct the baseline
266+
//for (unsigned i = 0; i < trace_len; ++i)
267+
//data[i] = data[i] - baseline[par.ch];
268+
269+
// Find the maximum value above baseline.
270+
for (unsigned i = 0; i < trace_len; i++){
271+
temp_val = trace_data[i] - baseline[par.ch];
272+
if(temp_val > maximum[par.ch]){ maximum[par.ch] = temp_val; }
273+
274+
if(!correct_baselines){ total_data[(par.ch * trace_len) + i] = trace_data[i]; }
275+
else{ total_data[(par.ch * trace_len) + i] = temp_val; }
276+
}
277+
278+
// Check that this "trace" is above threshold.
279+
if(maximum[par.ch] < threshold){ continue; }
280+
281+
status = true;
282+
break;
283+
}
284+
}
285+
286+
// Threshold was not reached. Copy the most recent pulse into
287+
// the module traces array.
288+
for (unsigned int i = 0; i < NUMBER_OF_CHANNELS; i++) {
289+
if(i == par.ch){ continue; } // Already did this channel.
290+
291+
if(par.pif->ReadSglChanTrace(trace_data, trace_len, par.mod, i)){
292+
// Calculate the channel baseline.
293+
sum = 0;
294+
295+
for (unsigned j = 0; j < baselineSamples; j++)
296+
sum += trace_data[j];
297+
298+
baseline[i] = float(sum)/float(baselineSamples);
299+
300+
// Correct the baseline
301+
//for (unsigned i = 0; i < trace_len; ++i)
302+
//data[i] = data[i] - baseline[par.ch];
303+
304+
// Find the maximum value above baseline.
305+
for (unsigned j = 0; j < trace_len; j++){
306+
temp_val = trace_data[j] - baseline[i];
307+
if(temp_val > maximum[i]){ maximum[i] = temp_val; }
308+
309+
if(!correct_baselines){ total_data[(i * trace_len) + j] = trace_data[j]; }
310+
else{ total_data[(i * trace_len) + j] = temp_val; }
311+
}
312+
}
313+
}
314+
315+
return status;
316+
}
317+
172318
bool ParameterChannelWriter::operator()(PixieFunctionParms< std::pair<std::string, double> > &par){
173319
if(par.pif->WriteSglChanPar(par.par.first.c_str(), par.par.second, par.mod, par.ch)){
174320
par.pif->PrintSglChanPar(par.par.first.c_str(), par.mod, par.ch);

Poll/include/poll2_core.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
*
1111
* \author Cory R. Thornsberry
1212
*
13-
* \date Oct. 2nd, 2015
13+
* \date Oct. 6th, 2015
1414
*
15-
* \version 1.3.09
15+
* \version 1.3.10
1616
*/
1717

1818
#ifndef POLL2_CORE_H
@@ -24,8 +24,8 @@
2424
#include "hribf_buffers.h"
2525
#define maxEventSize 4095 // (0x1FFE0000 >> 17)
2626

27-
#define POLL2_CORE_VERSION "1.3.09"
28-
#define POLL2_CORE_DATE "Oct. 2nd, 2015"
27+
#define POLL2_CORE_VERSION "1.3.10"
28+
#define POLL2_CORE_DATE "Oct. 6th, 2015"
2929

3030
// Maximum length of UDP data packet (in bytes)
3131
#define MAX_ORPH_DATA 1464
@@ -201,9 +201,13 @@ class Poll{
201201

202202
/// Display run status information.
203203
void show_status();
204+
204205
/// Display polling threshold.
205206
void show_thresh();
206207

208+
/// Acquire raw traces from a pixie module.
209+
void get_traces(int mod_, int chan_, int thresh_=0);
210+
207211
/// Method responsible for handling tab complete of commands and pread/pwrite parameters
208212
std::vector<std::string> TabComplete(std::string cmd);
209213

0 commit comments

Comments
 (0)