CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
CastorMergerData.cc
Go to the documentation of this file.
1 /*
2  * \author A. Campbell - DESY
3  */
4 #ifndef HTBDAQ_DATA_STANDALONE
6 #else
7 #include "CastorMergerData.h"
8 #endif
9 #include <string.h>
10 #include <stdio.h>
11 #include <algorithm>
12 
13 CastorMergerData::CastorMergerData() : m_formatVersion(-2), m_rawLength(0), m_rawConst(0), m_ownData(0) { }
14 CastorMergerData::CastorMergerData(const unsigned short* data, int length) {
15  adoptData(data,length);
16  m_ownData=0;
17 }
18 CastorMergerData::CastorMergerData(const CastorMergerData& hd) : m_formatVersion(hd.m_formatVersion), m_rawLength(hd.m_rawLength), m_rawConst(hd.m_rawConst), m_ownData(0) { }
19 
20 CastorMergerData::CastorMergerData(int version_to_create) : m_formatVersion(version_to_create) {
21  allocate(version_to_create);
22 }
23 
24 void CastorMergerData::allocate(int version_to_create) {
25  m_formatVersion=version_to_create;
26  // the needed space is for the biggest possible event...
27  const int needed=0x200;
28  // create a buffer big enough...
29  m_ownData=new unsigned short[needed];
30  m_rawLength=0;
32 }
33 
35  if (m_ownData==0) {
39  }
40  return (*this);
41 }
42 
43 void CastorMergerData::adoptData(const unsigned short* data, int length) {
44  m_rawLength=length;
46  if (m_rawLength<5) {
47  m_formatVersion=-2; // invalid!
48  } else {
49  m_formatVersion=(m_rawConst[4]>>12)&0xF;
50  }
51 }
52 
53 // check :: not EE, length is reasonable, length matches wordcount
54 // length required for tp+daq is correct
55 
57  // length checks
58  // minimum length
59  if (m_rawLength<6+12) return false;
60  // matches wordcount
61  if (m_rawLength!=m_rawConst[m_rawLength-3]) return false;
62 
63  return true;
64 }
65 
66 
68  unsigned char* tp_lengths, unsigned short* tp_samples) const {
69 
70  if (tp_lengths!=0) memset(tp_lengths,0,1);
71 
72  int tp_words_total,headerLen,trailerLen;
73  determineSectionLengths(tp_words_total,headerLen,trailerLen);
74 
75  int wordPtr;
76  const unsigned short* tpBase=m_rawConst+headerLen;
77  // process the trigger primitive words
78  if (tp_lengths!=0) {
79  for (wordPtr=0; wordPtr<tp_words_total; wordPtr++) {
80  tp_samples[tp_lengths[0]]=tpBase[wordPtr];
81  tp_lengths[0]++;
82  }
83  }
84 
85 }
86 void CastorMergerData::determineSectionLengths(int& tpWords, int& headerWords, int& trailerWords) const {
87 
88  tpWords=m_rawConst[5]>>8; // should be 8 but could be up to 12
89  headerWords=8;
90  trailerWords=0; // minimum, may be more...
91 }
92 
93 void CastorMergerData::determineStaticLengths(int& headerWords, int& trailerWords) const {
94  headerWords=8;
95  trailerWords=0; // minimum, may be more...
96 
97 }
99  unsigned char* tp_lengths, unsigned short* tp_samples) {
100 
101  int tp_words_total=0, headerLen, trailerLen;
102  determineStaticLengths(headerLen,trailerLen);
103 
104  tp_words_total=0;
105  int isample;
106 
107  // trigger words
108  unsigned short* ptr=m_ownData+headerLen;
109  if (tp_samples!=0 && tp_lengths!=0) {
110  for (isample=0; isample<tp_lengths[0] && isample<12; isample++) {
111  ptr[tp_words_total]=tp_samples[isample];
112  tp_words_total++;
113  }
114  }
115 
116  m_ownData[5]=(tp_words_total<<8)|0x1;
117  unsigned short totalLen=headerLen+tp_words_total+trailerLen;
118 
119  m_rawLength=totalLen;
120  m_ownData[totalLen-2]=totalLen/2; // 32-bit words
121  m_ownData[totalLen-3]=totalLen;
122  m_ownData[totalLen-4]=tp_words_total;
123 
124 }
125 
126 void CastorMergerData::packHeaderTrailer(int L1Anumber, int bcn, int submodule, int orbitn, int pipeline, int ndd, int nps, int firmwareRev) {
127  m_ownData[0]=L1Anumber&0xFF;
128  m_ownData[1]=(L1Anumber&0xFFFF00)>>8;
129 
130  m_ownData[2]=0x8000; // Version is valid, no error bits - status bits need definition
131  m_ownData[3]=((orbitn&0x1F)<<11)|(submodule&0x7FF);
132  m_ownData[4]=((m_formatVersion&0xF)<<12)|(bcn&0xFFF);
133  m_ownData[5]|=((nps&0xF)<<4)|0x1;
134  m_ownData[6]=((firmwareRev&0x70000)>>3)|(firmwareRev&0x1FFF);
135  m_ownData[7]=(pipeline&0xFF) | ((ndd&0x1F)<<8);
136  m_ownData[m_rawLength-4]&=0x7FF;
137  m_ownData[m_rawLength-4]|=(ndd&0x1F)<<11;
138 
139  m_ownData[m_rawLength-2]=m_rawLength/2; // 32-bit words
140  m_ownData[m_rawLength-1]=(L1Anumber&0xFF)<<8;
141 }
142 
143 unsigned int CastorMergerData::getOrbitNumber() const {
144  return (m_rawConst[3]>>11);
145 }
146 
148  return (m_rawConst[6]);
149 }
150 
151 
152 
153 
void adoptData(const unsigned short *data, int length)
void allocate(int version_to_create=0)
bool check() const
Check for a good event Requires a minimum length, matching wordcount and length, not an empty event...
CastorMergerData & operator=(const CastorMergerData &)
const unsigned short * m_rawConst
void unpack(unsigned char *tp_lengths, unsigned short *tp_samples) const
Unpack the HTR data into TP and DAQ data sorted by channel.
void packHeaderTrailer(int L1Anumber, int bcn, int submodule, int orbitn, int pipeline, int ndd, int nps, int firmwareRev=0)
pack header and trailer (call after pack)
unsigned int getOrbitNumber() const
Get the HTR orbit number.
unsigned int getFirmwareRevision() const
Get the Merger firmware version.
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
unsigned short * m_ownData
void pack(unsigned char *tp_lengths, unsigned short *tp_samples)
Unpack the HTR data into TP and DAQ data sorted by channel.
void determineSectionLengths(int &tpWords, int &headerWords, int &trailerWords) const
void determineStaticLengths(int &headerWords, int &trailerWords) const