-
Notifications
You must be signed in to change notification settings - Fork 14
/
daq_device_random.cc
173 lines (135 loc) · 3.31 KB
/
daq_device_random.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include <iostream>
#include <daq_device_random.h>
using namespace std;
daq_device_random::daq_device_random(const int eventtype
, const int subeventid
, const int n_words
, const int low
, const int high
, const int trigger_enabled)
{
m_eventType = eventtype;
m_subeventid = subeventid;
number_of_words = n_words;
low_range = low;
high_range = high;
rfp = fopen("/dev/urandom","r");
if (trigger_enabled)
{
th = new pulserTriggerHandler(m_eventType);
registerTriggerHandler(th);
}
else
{
th = 0;
}
}
daq_device_random::~daq_device_random()
{
if ( rfp) fclose( rfp);
if ( th)
{
clearTriggerHandler();
delete th;
}
}
// the put_data function
int daq_device_random::put_data(const int etype, int * adr, const int length )
{
int len = 0;
if (etype != m_eventType ) // not our id
{
return 0;
}
if ( !rfp) return 0; // if we failed to open the random gen
if ( daq_getEventFormat() ) // we are writing PRDF
{
formatPacketHdr(adr);
packetdata_ptr sevt = (packetdata_ptr) adr;
// update id's etc
sevt->sub_id = m_subeventid;
sevt->sub_type = 4;
sevt->sub_decoding = 30000+ID4EVT;
int *d = (int *) &sevt->data;
int ia;
int data;
for ( ia = 0; ia < number_of_words; ia++)
{
int l = fread ( &data, 4, 1, rfp);
if ( l == 1 )
{
data &= 0x7fffffff; // enforce a posiitive number
double x = data;
x /= ( 1024. * 1024.);
x *= ( high_range - low_range);
x /= ( 2. * 1024.);
*d++ = x + low_range;
len++;
}
}
int padding = len%2;
sevt->structureinfo += padding;
len = len + padding;
sevt->sub_length += len;
return sevt->sub_length;
}
else
{
sevt = (subevtdata_ptr) adr;
// set the initial subevent length
sevt->sub_length = SEVTHEADERLENGTH;
// update id's etc
sevt->sub_id = m_subeventid;
sevt->sub_type=4;
sevt->sub_decoding = ID4EVT;
sevt->reserved[0] = 0;
sevt->reserved[1] = 0;
unsigned int data;
int *d = (int *) &sevt->data;
int ia;
for ( ia = 0; ia < number_of_words; ia++)
{
int l = fread ( &data, 4, 1, rfp);
if ( l == 1 )
{
data &= 0x7fffffff; // enforce a posiitive number
double x = data;
x /= ( 1024. * 1024.);
x *= ( high_range - low_range);
x /= ( 2. * 1024.);
*d++ = x + low_range;
len++;
}
}
sevt->sub_padding = len%2;
len = len + (len%2);
sevt->sub_length += len;
return sevt->sub_length;
}
}
void daq_device_random::identify(std::ostream& os) const
{
os << "Random Device Event Type: " << m_eventType << " Subevent id: " << m_subeventid
<< " n_words: " << number_of_words
<< " range: " << low_range << " - " << high_range;
if (th)
{
os << " ** Trigger enabled";
}
os << endl;
}
int daq_device_random::max_length(const int etype) const
{
if (etype != m_eventType) return 0;
return (number_of_words + SEVTHEADERLENGTH);
}
int daq_device_random::init()
{
return 0;
}
// the rearm() function
int daq_device_random::rearm(const int etype)
{
if (etype != m_eventType) return 0;
return 0;
}