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