CMS 3D CMS Logo

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