CMS 3D CMS Logo

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