ROOTANA
TRB3Decoder.cxx
Go to the documentation of this file.
1 #include "TRB3Decoder.hxx"
2 
3 
4 void Trb3Calib::UseTRB3LinearCalibration(bool uselinear){useLinearCalibration = uselinear;};
5 void Trb3Calib::SetTRB3LinearCalibrationConstants(float low_value, float high_value){
6  trb3LinearLowEnd = low_value;
7  trb3LinearHighEnd = high_value;
8 }
9 
10 // Get endian byte swapping code; use built-in code for non-MacOS Linux
11 #if defined(OS_LINUX) && !defined(OS_DARWIN)
12 #include <byteswap.h>
13 #else
14 /* Swap bytes in 32 bit value. */
15 #define R__bswap_constant_32(x) \
16  ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) | \
17  (((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24))
18 #endif
19 
20 // See figure 23 of TRB3 manual for description of TRB3 packet
21 
22 TrbDecoder::TrbDecoder(int bklen, void *pdata, std::string bankname, int type){
23 
24  fPacketSize = 0;
25  fDecoding = 0;
26  fBoardId = 0;
27  fSeqNr = 0;
28  fRunNr = 0;
29  fDate = 0;
30  fTime = 0;
31 
32  // New decoder, for data read directly from UDP socket
33  if(bankname != std::string("TRB0")){
34  uint32_t* fData = reinterpret_cast<uint32_t*>(pdata);
35 
36  // Bklen should be for 32-bit words. If type is 1 (char type),
37  // then need to fix the bklen
38  if(type == 1) bklen /= 4;
39 
40  // check for correct endian-ness; if wrong, flip.
41  // fourth word is endian encoding check word
42  if(!((fData[3] & 0x1) == 1 && (fData[3] & 0x80000000) == 0)){
43  //std::cout << "Endian swapping" << std::endl;
44  for(int i = 0; i < bklen; i++){
45 #if defined(OS_LINUX) && !defined(OS_DARWIN)
46  fData[i] = __bswap_32 (fData[i]);
47 #else
48  fData[i] = R__bswap_constant_32(fData[i]);
49 #endif
50 
51  //std::cout << bklen/4 << " "
52  // << i << " " << std::hex << "0x"<<fData[i] << std::dec << std::endl;
53 
54  }
55 
56  }
57 
58  // This is the number of words in the sub-event packet, not including this word.
59  int size_subevent = fData[2]/4;
60  // std::cout << "subevent size : " << size_subevent << std::endl;
61 
62  // Decode sub-event ID, trigger number and trigger code!!!
63  fDecoding = fData[3];
64  fSubEventID = fData[4] & 0xffff;
65  fTriggerWord = fData[5];
66 
67 
68  // Loop over rest of bank
69  uint32_t fpgaWord = 0, headerWord = 0;
70  int pointer = 6;
71 
72  // We search for words that look like the FPGA header words (sub-sub-event IDs);
73  // if we don't find them, break out.
74  bool finished = false;
75  while (!finished){
76  uint32_t word = fData[pointer];
77  // Look for word indicating a new FPGA
78  if((word & 0x0000ffff) == 0x00000100 ||
79  (word & 0x0000ffff) == 0x00000101 ||
80  (word & 0x0000ffff) == 0x00000102 ||
81  (word & 0x0000ffff) == 0x00000103 ){
82  fpgaWord = word;
83 
84  int nwords_subevent = ((word & 0xffff0000) >> 16);
85 
86  // std::cout << "Found header: " << std::hex << fpgaWord << std::dec << " at " << pointer << " nwords: " << nwords_subevent<< std::endl;
87 
88  // next word if TDC header
89  pointer++;
90  headerWord = fData[pointer];
91 
92  uint32_t epochWord = 0;
93  // Now loop over the next couple words, grabbing the TDC data
94  for(int i = 0; i < nwords_subevent; i++){
95  pointer++;
96  uint32_t word = fData[pointer];
97  // Look for the epoch counter word; use this epoch word for all subsequent TDCs...
98  if((word & 0xe0000000) == 0x60000000)
99  epochWord = word;
100 
101  if((word & 0xe0000000) == 0x80000000){
102  uint32_t tdcWord = fData[pointer];
103 
104  if((fpgaWord & 0xf) > 3 ){
105  std::cout << "TDC FPGA ID > 3? Not possible... " << std::hex << fpgaWord << " " << headerWord
106  << " " << tdcWord << " " << pointer << " " << size_subevent << std::dec << std::endl;
107  for(int i = 0; i < 6 ; i++)
108  std::cout << i << " 0x"<<std::hex
109  << fData[i] << std::dec << std::endl;
110  }else{
111 
112  // std::cout << std::hex << "Adding TDC " << headerWord << " " << epochWord << " " << tdcWord << std::endl;
113  fMeasurements.push_back(TrbTdcMeas(fpgaWord, headerWord,
114  epochWord, tdcWord));
115  }
116  }
117  }
118 
119  }else{
120  // The next word isn't a sub-sub-event ID word, so break out.
121  finished = true;
122  }
123 
124 
125  }
126 
127  // std::cout << "Number of hits: " << fMeasurements.size() << std::endl;
128 
129  // Go to end of sub-event; check the trailer word
130  int end_packet = 2+ size_subevent -1;
131  if( fData[end_packet-1] != 0x15555){
132  std::cout << "TRB3 sub-event ID trailer word = " << fData[end_packet-1] << "; not expected 0x15555; bank decoding error!!!" << std::endl;
133  }
134 
135 
136  // std::cout << "Check " << std::dec << pointer << " " << end_packet << " num measurements: " << fMeasurements.size() <<"\n_______\n"<< std::endl;
137 
138 
139  }else{ // Old decoder, for data read from the DABC event builder
140 
141  uint32_t* fData = reinterpret_cast<uint32_t*>(pdata);
142 
143  // Get header information
144  fPacketSize = fData[0];
145  fDecoding = fData[1];
146  fBoardId = fData[2];
147  fSeqNr = fData[3];
148  fRunNr = fData[4];
149  fDate = fData[5];
150  fTime = fData[6];
151 
152  // Loop over rest of bank
153  uint32_t fpgaWord = 0, headerWord = 0;
154  //std::cout << "Number of words: " << GetSize() << std::endl;
155  for(int pointer = 7; pointer < bklen; pointer++){
156 
157  uint32_t word = fData[pointer];
158  //std::cout << pointer << " " << std::hex
159  // << word << std::dec << std::endl;
160  // Look for word indicating a new FPGA
161  if((word & 0xfff0ffff) == 0x00000100 ||
162  (word & 0xfff0ffff) == 0x00000101 ||
163  (word & 0xfff0ffff) == 0x00000102 ||
164  (word & 0xfff0ffff) == 0x00000103 ){
165  fpgaWord = word;
166  // next word if TDC header
167  pointer++;
168  headerWord = fData[pointer];
169  //std::cout << "Found header: " << fpgaWord << std::endl;
170  continue;
171  }
172 
173  // Look for the epoch counter word; the TDC data word follows it
174  if((word & 0xe0000000) == 0x60000000){
175  uint32_t epochWord = word;
176  pointer++;
177  uint32_t tdcWord = fData[pointer];
178 
179  // std::cout << "TDC? " << std::hex
180  // << tdcWord << std::dec << std::endl;
181  if((tdcWord & 0xe0000000) == 0x80000000){
182  fMeasurements.push_back(TrbTdcMeas(fpgaWord, headerWord,
183  epochWord, tdcWord));
184  }
185  }
186  }
187  }
188 }
189 
190 
#define R__bswap_constant_32(x)
Definition: TRB3Decoder.cxx:15
void UseTRB3LinearCalibration(bool uselinear)
Definition: TRB3Decoder.cxx:4
bool useLinearCalibration
Definition: TRB3Decoder.hxx:41
float trb3LinearHighEnd
Definition: TRB3Decoder.hxx:44
float trb3LinearLowEnd
Definition: TRB3Decoder.hxx:43
void SetTRB3LinearCalibrationConstants(float low_value, float high_value)
Definition: TRB3Decoder.cxx:5
uint32_t fSubEventID
uint32_t fRunNr
std::vector< TrbTdcMeas > fMeasurements
Vector of TDC Measurements.
uint32_t fPacketSize
uint32_t fDate
uint32_t fTriggerWord
TrbDecoder(int bklen, void *pdata, std::string bankname, int type)
Constructor.
Definition: TRB3Decoder.cxx:22
uint32_t fDecoding
uint32_t fTime
uint32_t fSeqNr
uint32_t fBoardId
Decoder for individual hits from GSI TFB3 FPGA-TDC.
Definition: TRB3Decoder.hxx:54