CMS 3D CMS Logo

CastorCtdcPacker.cc
Go to the documentation of this file.
10 #include <iostream>
11 
12 using namespace std;
13 
14 namespace {
15 template <class Coll, class DetIdClass>
16 int process(const Coll* pt, const DetId& did, unsigned short* buffer, int& presamples) {
17  if (pt==nullptr) return 0;
18  int size=0;
19  typename Coll::const_iterator i=pt->find(DetIdClass(did));
20  if (i!=pt->end()) {
21  presamples=i->presamples();
22  size=i->size();
23  for (int j=0; j<size; j++)
24  buffer[j]=(*i)[j].raw();
25  }
26  return size;
27 }
28 }
29 
31  unsigned short* buffer, int &presamples) {
32 
33  if (did.det()!=DetId::Calo) return 0;
34  int size=0;
35  HcalCastorDetId genId(did);
36 
37  size=process<CastorDigiCollection,HcalCastorDetId>(inputs.castorCont,did,buffer,presamples);
38 
39  return size;
40 }
41 
42 void CastorCtdcPacker::pack(int fedid, int dccnumber,
43  int nl1a, int orbitn, int bcn,
44  const CastorCollections& inputs,
45  const CastorElectronicsMap& emap,
46  FEDRawData& output) {
48  std::vector<unsigned short> trigdata(CastorCORData::CHANNELS_PER_SPIGOT*CastorCORData::MAXIMUM_SAMPLES_PER_CHANNEL);
49  std::vector<unsigned char> preclen(CastorCORData::CHANNELS_PER_SPIGOT);
50  std::vector<unsigned char> triglen(CastorCORData::CHANNELS_PER_SPIGOT);
51  constexpr int CORFormatVersion=1;
52 
53 // CastorCORData spigots[CastorCTDCHeader::SPIGOT_COUNT];
54  CastorCORData spigots[2];
55  // loop over all valid channels in the given ctdc, spigot by spigot.
56  for (int spigot=0; spigot<CastorCTDCHeader::SPIGOT_COUNT; spigot++) {
57  spigots[spigot].allocate(CORFormatVersion);
58  CastorElectronicsId exampleEId;
59  int npresent=0;
60  int presamples=-1, samples=-1;
61  for (int fiber=1; fiber<=12; fiber++)
62  for (int fiberchan=0; fiberchan<3; fiberchan++) {
63  int linear=(fiber-1)*3+fiberchan;
64 // HcalQIESample chanSample(0,0,fiber,fiberchan,false,false);
65 // unsigned short chanid=chanSample.raw()&0xF800;
66  preclen[linear]=0;
67  CastorElectronicsId partialEid(fiberchan,fiber,spigot,dccnumber);
68  // does this partial id exist?
69  CastorElectronicsId fullEid;
70  HcalGenericDetId genId;
71  if (!emap.lookup(partialEid,fullEid,genId)) continue;
72 
73  // next, see if there is a digi with this id
74  unsigned short* database=&(precdata[linear*CastorCORData::MAXIMUM_SAMPLES_PER_CHANNEL]);
75  int mypresamples;
76  int mysamples=findSamples(genId,inputs,database,mypresamples);
77  if (mysamples>0) {
78  if (samples<0) samples=mysamples;
79  else if (samples!=mysamples) {
80  edm::LogError("CASTOR") << "Mismatch of samples in a single COR (unsupported) " << mysamples << " != " << samples;
81  continue;
82  }
83  if (presamples<0) {
84  presamples=mypresamples;
85  exampleEId=fullEid;
86  } else if (mypresamples!=presamples) {
87  edm::LogError("CASTOR") << "Mismatch of presamples in a single COR (unsupported) " << mypresamples << " != " << presamples;
88  continue;
89  }
90  preclen[linear]=(unsigned char)(samples);
91  npresent++;
92 
93  }
94  }
96  if (npresent>0) {
97  spigots[spigot].pack(&(preclen[0]),&(precdata[0]),
98  &(triglen[0]),&(trigdata[0]),
99  true);
100  constexpr int pipeline=0x22;
101  constexpr int firmwareRev=0;
102  int submodule=exampleEId.htrTopBottom()&0x1;
103  submodule|=(exampleEId.htrSlot()&0x1F)<<1;
104  submodule|=(exampleEId.readoutVMECrateId()&0x1f)<<6;
105  spigots[spigot].packHeaderTrailer(nl1a,
106  bcn,
107  submodule,
108  orbitn,
109  pipeline,
110  samples,
111  presamples,
112  firmwareRev);
113 
114  }
115  }
116  // calculate the total length, and resize the FEDRawData
117  int theSize=0;
118  for (int spigot=0; spigot<2; spigot++) {
119  theSize+=spigots[spigot].getRawLength()*sizeof(unsigned short);
120  }
121  // the merger payload - not yet defined
122  CastorMergerData mergerdata;
123  // would need to fill mergdata here
124  theSize+=mergerdata.getRawLength()*sizeof(unsigned short);
125 
126  theSize+=sizeof(CastorCTDCHeader)+8; // 8 for trailer
127  theSize+=(8-(theSize%8))%8; // even number of 64-bit words.
128  output.resize(theSize);
129 
130  // construct the bare CTDC Header
131  CastorCTDCHeader* dcc=(CastorCTDCHeader*)(output.data());
132  dcc->clear();
133  dcc->setHeader(fedid,bcn,nl1a,orbitn);
134 
135  // pack the HTR data into the FEDRawData block using CastorCTDCHeader
136  for (int spigot=0; spigot<2; spigot++) {
137  if (spigots[spigot].getRawLength()>0)
138  dcc->copySpigotData(spigot,spigots[spigot],true,0);
139  }
140  if ( mergerdata.getRawLength()>0)
141  dcc->copyMergerData(mergerdata,true);
142  // trailer
143  FEDTrailer fedTrailer(output.data()+(output.size()-8));
144  fedTrailer.set(output.data()+(output.size()-8),
145  output.size()/8,
146  evf::compute_crc(output.data(),output.size()), 0, 0);
147 
148 }
size
Write out results.
static const int CHANNELS_PER_SPIGOT
Definition: CastorCORData.h:16
void pack(unsigned char *daq_lengths, unsigned short *daq_samples, unsigned char *tp_lengths, unsigned short *tp_samples, bool do_capid=false)
Unpack the HTR data into TP and DAQ data sorted by channel.
int readoutVMECrateId() const
const int getRawLength() const
Get the length of the raw data.
void setHeader(int sourceid, int bcn, int l1aN, int orbN)
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)
const CastorDigiCollection * castorCont
static const int SPIGOT_COUNT
size_t size() const
Lenght of the data buffer in bytes.
Definition: FEDRawData.h:47
static void pack(int fedid, int dccnumber, int nl1a, int orbitn, int bcn, const CastorCollections &inputs, const CastorElectronicsMap &emap, FEDRawData &output)
void copySpigotData(unsigned int spigot_id, const CastorCORData &data, bool valid=true, unsigned char LRB_error_word=0)
Add the given CastorCORData as the given spigot&#39;s data. This should be done in increasing spigot orde...
const DetId lookup(CastorElectronicsId fId) const
lookup the logical detid associated with the given electronics id
static void set(unsigned char *trailer, uint32_t lenght, uint16_t crc, uint8_t evt_stat, uint8_t tts, bool moreTrailers=false)
Set all fields in the trailer.
Definition: FEDTrailer.cc:60
void copyMergerData(const CastorMergerData &data, bool valid)
void resize(size_t newsize)
Definition: FEDRawData.cc:32
static const int MAXIMUM_SAMPLES_PER_CHANNEL
Definition: CastorCORData.h:17
unsigned short compute_crc(unsigned char *buffer, unsigned int bufSize)
Definition: CRC16.h:46
void allocate(int version_to_create=0)
Definition: DetId.h:18
const int getRawLength() const
Get the length of the raw data.
Definition: CastorCORData.h:36
static int findSamples(const DetId &did, const CastorCollections &inputs, unsigned short *buffer, int &presamples)
const unsigned char * data() const
Return a const pointer to the beginning of the data buffer.
Definition: FEDRawData.cc:28
Readout chain identification for Castor Bits for the readout chain : some names need change! [31:26] ...
float linear(float x)
#define constexpr
constexpr Detector det() const
get the detector field from this detid
Definition: DetId.h:39