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