CMS 3D CMS Logo

HcalPacker.cc
Go to the documentation of this file.
8 
10  hbhe=0;
11  hoCont=0;
12  hfCont=0;
13  tpCont=0;
14  zdcCont=0;
15  calibCont=0;
16 }
17 
18 template <class Coll, class DetIdClass>
19 int process(const Coll* pt, const DetId& did, unsigned short* buffer, int& presamples,bool& isUS, bool& isMP) {
20  isUS=false; isMP=false;
21  if (pt==0) { return 0; }
22  int size=0;
23  typename Coll::const_iterator i=pt->find(DetIdClass(did));
24  if (i!=pt->end()) {
25  isUS=i->zsUnsuppressed();
26  isMP=i->zsMarkAndPass();
27  presamples=i->presamples();
28  size=i->size();
29  for (int j=0; j<size; j++) {
30  buffer[j]=(*i)[j].raw();
31  }
32  }
33  return size;
34 }
35 
36 static unsigned char processTrig(const HcalTrigPrimDigiCollection* pt, const HcalTrigTowerDetId& tid, unsigned short* buffer) {
37  if (pt==0) { return 0; }
38  int size=0;
40  bool any_nonzero=false;
41  if (i!=pt->end()) {
42  int presamples=i->presamples();
43  size=i->size();
44 
45  for (int j=0; j<size; j++) {
46  buffer[j]=(*i)[j].raw();
47  if ((buffer[j]&0x1FF)!=0) any_nonzero=true;
48  if (j==presamples) { buffer[j]|=0x0200; }
49  }
50  }
51  return (any_nonzero)?(size):(0);
52 }
53 
55  unsigned short* buffer, int &presamples, bool& isUS, bool& isMP) {
56  if (!(did.det()==DetId::Hcal || (did.det()== DetId::Calo && did.subdetId()==HcalZDCDetId::SubdetectorId)) ) { return 0; }
57  int size=0;
58  HcalGenericDetId genId(did);
59 
60  switch (genId.genericSubdet()) {
63  size=process<HBHEDigiCollection,HcalDetId>(inputs.hbhe,did,buffer,presamples,isUS,isMP);
64  break;
66  size=process<HODigiCollection,HcalDetId>(inputs.hoCont,did,buffer,presamples,isUS,isMP);
67  break;
69  size=process<HFDigiCollection,HcalDetId>(inputs.hfCont,did,buffer,presamples,isUS,isMP);
70  break;
72  size=process<ZDCDigiCollection,HcalZDCDetId>(inputs.zdcCont,did,buffer,presamples,isUS,isMP);
73  break;
75  size=process<HcalCalibDigiCollection,HcalCalibDetId>(inputs.calibCont,did,buffer,presamples,isUS,isMP);
76  break;
77  default: size=0;
78  }
79  return size;
80 }
81 
82 void HcalPacker::pack(int fedid, int dccnumber,
83  int nl1a, int orbitn, int bcn,
84  const Collections& inputs,
85  const HcalElectronicsMap& emap,
86  FEDRawData& output) {
88  std::vector<unsigned short> trigdata(HcalHTRData::CHANNELS_PER_SPIGOT*HcalHTRData::MAXIMUM_SAMPLES_PER_CHANNEL);
89  std::vector<unsigned char> preclen(HcalHTRData::CHANNELS_PER_SPIGOT);
90  std::vector<unsigned char> triglen(HcalHTRData::CHANNELS_PER_SPIGOT);
91  static const int HTRFormatVersion=5;
92  bool channelIsMP[HcalHTRData::CHANNELS_PER_SPIGOT];
93 
94  HcalHTRData spigots[15];
95  // loop over all valid channels in the given dcc, spigot by spigot.
96  for (int spigot=0; spigot<15; spigot++) {
97  spigots[spigot].allocate(HTRFormatVersion);
98  HcalElectronicsId exampleEId;
99  int npresent=0, npresenttp=0;
100  int presamples=-1, samples=-1;
101  bool haveUnsuppressed=false;
102  for (int fiber=1; fiber<=8; fiber++) {
103  for (int fiberchan=0; fiberchan<3; fiberchan++) {
104  int linear=(fiber-1)*3+fiberchan;
105  HcalQIESample chanSample(0,0,fiber,fiberchan,false,false);
106  unsigned short chanid=chanSample.raw()&0xF800;
107  preclen[linear]=0;
108  channelIsMP[linear]=false;
109 
110  HcalElectronicsId partialEid(fiberchan,fiber,spigot,dccnumber);
111  // does this partial id exist?
112  HcalElectronicsId fullEid;
113  HcalGenericDetId genId;
114  if (!emap.lookup(partialEid,fullEid,genId)) { continue; }
115 
116 
117  // next, see if there is a digi with this id
118  unsigned short* database=&(precdata[linear*HcalHTRData::MAXIMUM_SAMPLES_PER_CHANNEL]);
119  int mypresamples=-1;
120  bool isUS=false, isMP=false;
121  int mysamples=findSamples(genId,inputs,database,mypresamples,isUS,isMP);
122  haveUnsuppressed=haveUnsuppressed || isUS;
123  channelIsMP[linear]=isMP;
124 
125  if (mysamples>0) {
126  if (samples<0) { samples=mysamples; }
127  else if (samples!=mysamples) {
128  edm::LogError("HCAL") << "Mismatch of samples in a single HTR (unsupported) " << mysamples << " != " << samples;
129  continue;
130  }
131  if (presamples<0) {
132  presamples=mypresamples;
133  exampleEId=fullEid;
134  } else if (mypresamples!=presamples) {
135  edm::LogError("HCAL") << "Mismatch of presamples in a single HTR (unsupported) " << mypresamples << " != " << presamples;
136  continue;
137  }
138  for (int ii=0; ii<samples; ii++) {
139  database[ii]=(database[ii]&0x7FF)|chanid;
140  }
141  preclen[linear]=(unsigned char)(samples);
142  npresent++;
143  }
144  }
145  }
146  for (int slb=1; slb<=6; slb++) {
147  for (int slbchan=0; slbchan<=3; slbchan++) {
148  int linear=(slb-1)*4+slbchan;
149  HcalTriggerPrimitiveSample idCvt(0,0,slb,slbchan);
150  unsigned short chanid=idCvt.raw()&0xF800;
151  triglen[linear]=0;
152 
153  HcalElectronicsId partialEid(slbchan,slb,spigot,dccnumber,0,0,0);
154  // does this partial id exist?
155  HcalElectronicsId fullEid;
156  HcalTrigTowerDetId tid;
157  if (!emap.lookup(partialEid,fullEid,tid)) {
158 // std::cout << "TPGPACK : no match for " << partialEid << std::endl;
159  continue;
160  } //else std::cout << "TPGPACK : converted " << partialEid << " to " << fullEid << "/" << tid << std::endl;
161 
162  // finally, what about a trigger channel?
163  if (!tid.null()) {
164  if (presamples < 0) {
165  exampleEId = fullEid;
166  }
167  unsigned short* trigbase=&(trigdata[linear*HcalHTRData::MAXIMUM_SAMPLES_PER_CHANNEL]);
168  triglen[linear]=processTrig(inputs.tpCont,tid,trigbase);
169  if (triglen[linear]) {
170  npresent++;
171  npresenttp++;
172  }
173 
174  for (unsigned char q=0; q<triglen[linear]; q++) {
175  trigbase[q]=(trigbase[q]&0x7FF)|chanid;
176  }
177 
178  }
179  }
180  }
182  if (npresent>0) {
183  spigots[spigot].pack(&(preclen[0]),&(precdata[0]),
184  &(triglen[0]),&(trigdata[0]),
185  false);
186  static const int pipeline=0x22;
187  static const int firmwareRev=0;
188  int submodule=exampleEId.htrTopBottom()&0x1;
189  submodule|=(exampleEId.htrSlot()&0x1F)<<1;
190  submodule|=(exampleEId.readoutVMECrateId()&0x1f)<<6;
191  // Samples and Presamples can't be negative, or the HeaderTrailer will
192  // generate a large large number using them (unsigned int roll over)
193  if (samples < 0) {
194  samples = 0;
195  }
196  if (presamples < 0) {
197  presamples = 0;
198  }
199  spigots[spigot].packHeaderTrailer(nl1a,
200  bcn,
201  submodule,
202  orbitn,
203  pipeline,
204  samples,
205  presamples,
206  firmwareRev,
207  1); // need non-zero falvor
208  if (haveUnsuppressed) {
209  spigots[spigot].packUnsuppressed(channelIsMP);
210  }
211 
212  }
213  }
214  // calculate the total length, and resize the FEDRawData
215  int theSize=0;
216  for (int spigot=0; spigot<15; spigot++) {
217  theSize+=spigots[spigot].getRawLength()*sizeof(unsigned short);
218  }
219  theSize+=sizeof(HcalDCCHeader)+8; // 8 for trailer
220  theSize+=(8-(theSize%8))%8; // even number of 64-bit words.
221  output.resize(theSize);
222 
223  // construct the bare DCC Header
224  HcalDCCHeader* dcc=(HcalDCCHeader*)(output.data());
225  dcc->clear();
226  dcc->setHeader(fedid,bcn,nl1a,orbitn);
227 
228  // pack the HTR data into the FEDRawData block using HcalDCCHeader
229  for (int spigot=0; spigot<15; spigot++) {
230  if (spigots[spigot].getRawLength()>0) {
231  dcc->copySpigotData(spigot,spigots[spigot],true,0);
232  }
233  }
234  // trailer
235  FEDTrailer fedTrailer(output.data()+(output.size()-8));
236  fedTrailer.set(output.data()+(output.size()-8),
237  output.size()/8,
238  evf::compute_crc(output.data(),output.size()), 0, 0);
239 }
size
Write out results.
int i
Definition: DBlmapReader.cc:9
void pack(int fedid, int dccnumber, int nl1a, int orbitn, int bcn, const Collections &inputs, const HcalElectronicsMap &emap, FEDRawData &output)
Definition: HcalPacker.cc:82
uint16_t raw() const
get the raw word
Definition: HcalQIESample.h:20
static int slb(const HcalTriggerPrimitiveSample &theSample)
void packUnsuppressed(const bool *mp)
pack trailer with Mark and Pass bits
Definition: HcalHTRData.cc:308
int htrSlot() const
get the htr slot
std::vector< HcalTriggerPrimitiveDigi >::const_iterator const_iterator
const HcalCalibDigiCollection * calibCont
Definition: HcalPacker.h:19
static const int CHANNELS_PER_SPIGOT
Definition: HcalHTRData.h:18
static const int MAXIMUM_SAMPLES_PER_CHANNEL
Definition: HcalHTRData.h:19
static void set(unsigned char *trailer, int evt_lgth, int crc, int evt_stat, int tts, bool T=false)
Set all fields in the trailer.
Definition: FEDTrailer.cc:42
size_t size() const
Lenght of the data buffer in bytes.
Definition: FEDRawData.h:47
int readoutVMECrateId() const
get the readout VME crate number
void allocate(int version_to_create=0)
Definition: HcalHTRData.cc:26
void copySpigotData(unsigned int spigot_id, const HcalHTRData &data, bool valid=true, unsigned char LRB_error_word=0)
Add the given HcalHTRData as the given spigot&#39;s data. This should be done in increasing spigot order!...
static unsigned char processTrig(const HcalTrigPrimDigiCollection *pt, const HcalTrigTowerDetId &tid, unsigned short *buffer)
Definition: HcalPacker.cc:36
int htrTopBottom() const
get the htr top/bottom (1=top/0=bottom), valid for VME
void resize(size_t newsize)
Definition: FEDRawData.cc:32
unsigned short compute_crc(unsigned char *buffer, unsigned int bufSize)
Definition: CRC16.h:67
const int getRawLength() const
Get the length of the raw data.
Definition: HcalHTRData.h:39
void setHeader(int sourceid, int bcn, int l1aN, int orbN)
uint16_t raw() const
get the raw word
int j
Definition: DBlmapReader.cc:9
const HcalTrigPrimDigiCollection * tpCont
Definition: HcalPacker.h:21
const ZDCDigiCollection * zdcCont
Definition: HcalPacker.h:20
const HBHEDigiCollection * hbhe
Definition: HcalPacker.h:16
int subdetId() const
get the contents of the subdetector field (not cast into any detector&#39;s numbering enum) ...
Definition: DetId.h:37
ii
Definition: cuy.py:588
const_iterator end() const
Definition: DetId.h:18
static const int SubdetectorId
Definition: HcalZDCDetId.h:25
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:224
bool null() const
is this a null id ?
Definition: DetId.h:45
int process(const Coll *pt, const DetId &did, unsigned short *buffer, int &presamples, bool &isUS, bool &isMP)
Definition: HcalPacker.cc:19
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:283
iterator find(key_type k)
const HODigiCollection * hoCont
Definition: HcalPacker.h:17
const unsigned char * data() const
Return a const pointer to the beginning of the data buffer.
Definition: FEDRawData.cc:28
int findSamples(const DetId &did, const Collections &inputs, unsigned short *buffer, int &presamples, bool &zsUS, bool &zsMP)
Definition: HcalPacker.cc:54
const HFDigiCollection * hfCont
Definition: HcalPacker.h:18
float linear(float x)
HcalGenericSubdetector genericSubdet() const
Detector det() const
get the detector field from this detid
Definition: DetId.h:35
Readout chain identification for Hcal.
const DetId lookup(HcalElectronicsId fId) const
lookup the logical detid associated with the given electronics id