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  * $Date: 2009/12/28 23:16:05 $
3  * $Revision: 1.2 $
4  * \author A. Campbell - DESY
5  */
6 #ifndef HTBDAQ_DATA_STANDALONE
8 #else
9 #include "CastorMergerData.h"
10 #endif
11 #include <string.h>
12 #include <stdio.h>
13 #include <algorithm>
14 
15 CastorMergerData::CastorMergerData() : m_formatVersion(-2), m_rawLength(0), m_rawConst(0), m_ownData(0) { }
16 CastorMergerData::CastorMergerData(const unsigned short* data, int length) {
17  adoptData(data,length);
18  m_ownData=0;
19 }
20 CastorMergerData::CastorMergerData(const CastorMergerData& hd) : m_formatVersion(hd.m_formatVersion), m_rawLength(hd.m_rawLength), m_rawConst(hd.m_rawConst), m_ownData(0) { }
21 
22 CastorMergerData::CastorMergerData(int version_to_create) : m_formatVersion(version_to_create) {
23  allocate(version_to_create);
24 }
25 
26 void CastorMergerData::allocate(int version_to_create) {
27  m_formatVersion=version_to_create;
28  // the needed space is for the biggest possible event...
29  const int needed=0x200;
30  // create a buffer big enough...
31  m_ownData=new unsigned short[needed];
32  m_rawLength=0;
34 }
35 
37  if (m_ownData==0) {
41  }
42  return (*this);
43 }
44 
45 void CastorMergerData::adoptData(const unsigned short* data, int length) {
46  m_rawLength=length;
48  if (m_rawLength<5) {
49  m_formatVersion=-2; // invalid!
50  } else {
51  m_formatVersion=(m_rawConst[4]>>12)&0xF;
52  }
53 }
54 
55 // check :: not EE, length is reasonable, length matches wordcount
56 // length required for tp+daq is correct
57 
59  // length checks
60  // minimum length
61  if (m_rawLength<6+12) return false;
62  // matches wordcount
63  if (m_rawLength!=m_rawConst[m_rawLength-3]) return false;
64 
65  return true;
66 }
67 
68 
70  unsigned char* tp_lengths, unsigned short* tp_samples) const {
71 
72  if (tp_lengths!=0) memset(tp_lengths,0,1);
73 
74  int tp_words_total,headerLen,trailerLen;
75  determineSectionLengths(tp_words_total,headerLen,trailerLen);
76 
77  int wordPtr;
78  const unsigned short* tpBase=m_rawConst+headerLen;
79  // process the trigger primitive words
80  if (tp_lengths!=0) {
81  for (wordPtr=0; wordPtr<tp_words_total; wordPtr++) {
82  tp_samples[tp_lengths[0]]=tpBase[wordPtr];
83  tp_lengths[0]++;
84  }
85  }
86 
87 }
88 void CastorMergerData::determineSectionLengths(int& tpWords, int& headerWords, int& trailerWords) const {
89 
90  tpWords=m_rawConst[5]>>8; // should be 8 but could be up to 12
91  headerWords=8;
92  trailerWords=0; // minimum, may be more...
93 }
94 
95 void CastorMergerData::determineStaticLengths(int& headerWords, int& trailerWords) const {
96  headerWords=8;
97  trailerWords=0; // minimum, may be more...
98 
99 }
101  unsigned char* tp_lengths, unsigned short* tp_samples) {
102 
103  int tp_words_total=0, headerLen, trailerLen;
104  determineStaticLengths(headerLen,trailerLen);
105 
106  tp_words_total=0;
107  int isample;
108 
109  // trigger words
110  unsigned short* ptr=m_ownData+headerLen;
111  if (tp_samples!=0 && tp_lengths!=0) {
112  for (isample=0; isample<tp_lengths[0] && isample<12; isample++) {
113  ptr[tp_words_total]=tp_samples[isample];
114  tp_words_total++;
115  }
116  }
117 
118  m_ownData[5]=(tp_words_total<<8)|0x1;
119  unsigned short totalLen=headerLen+tp_words_total+trailerLen;
120 
121  m_rawLength=totalLen;
122  m_ownData[totalLen-2]=totalLen/2; // 32-bit words
123  m_ownData[totalLen-3]=totalLen;
124  m_ownData[totalLen-4]=tp_words_total;
125 
126 }
127 
128 void CastorMergerData::packHeaderTrailer(int L1Anumber, int bcn, int submodule, int orbitn, int pipeline, int ndd, int nps, int firmwareRev) {
129  m_ownData[0]=L1Anumber&0xFF;
130  m_ownData[1]=(L1Anumber&0xFFFF00)>>8;
131 
132  m_ownData[2]=0x8000; // Version is valid, no error bits - status bits need definition
133  m_ownData[3]=((orbitn&0x1F)<<11)|(submodule&0x7FF);
134  m_ownData[4]=((m_formatVersion&0xF)<<12)|(bcn&0xFFF);
135  m_ownData[5]|=((nps&0xF)<<4)|0x1;
136  m_ownData[6]=((firmwareRev&0x70000)>>3)|(firmwareRev&0x1FFF);
137  m_ownData[7]=(pipeline&0xFF) | ((ndd&0x1F)<<8);
138  m_ownData[m_rawLength-4]&=0x7FF;
139  m_ownData[m_rawLength-4]|=(ndd&0x1F)<<11;
140 
141  m_ownData[m_rawLength-2]=m_rawLength/2; // 32-bit words
142  m_ownData[m_rawLength-1]=(L1Anumber&0xFF)<<8;
143 }
144 
145 unsigned int CastorMergerData::getOrbitNumber() const {
146  return (m_rawConst[3]>>11);
147 }
148 
150  return (m_rawConst[6]);
151 }
152 
153 
154 
155 
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